Index: trunk/morty.go
===================================================================
--- trunk/morty.go	(revision 126)
+++ trunk/morty.go	(revision 127)
@@ -28,4 +28,5 @@
 	"golang.org/x/text/encoding"
 
+	"github.com/asciimoo/morty/config"
 	"github.com/asciimoo/morty/contenttype"
 )
@@ -39,6 +40,4 @@
 const VERSION = "v0.2.0"
 
-var DEBUG = os.Getenv("DEBUG") != "false"
-
 var CLIENT *fasthttp.Client = &fasthttp.Client{
 	MaxResponseBodySize: 10 * 1024 * 1024, // 10M
@@ -46,5 +45,5 @@
 }
 
-var CSS_URL_REGEXP *regexp.Regexp = regexp.MustCompile("url\\((['\"]?)[ \\t\\f]*([\u0009\u0021\u0023-\u0026\u0028\u002a-\u007E]+)(['\"]?)\\)?")
+var cfg *config.Config = config.DefaultConfig
 
 var ALLOWED_CONTENTTYPE_FILTER contenttype.Filter = contenttype.NewFilterOr([]contenttype.Filter{
@@ -178,4 +177,6 @@
 }
 
+var CSS_URL_REGEXP *regexp.Regexp = regexp.MustCompile("url\\((['\"]?)[ \\t\\f]*([\u0009\u0021\u0023-\u0026\u0028\u002a-\u007E]+)(['\"]?)\\)?")
+
 type Proxy struct {
 	Key            []byte
@@ -337,5 +338,5 @@
 	requestURIStr := string(requestURI)
 
-	if DEBUG {
+	if cfg.Debug {
 		log.Println("getting", requestURIStr)
 	}
@@ -375,5 +376,5 @@
 					ctx.SetStatusCode(resp.StatusCode())
 					ctx.Response.Header.Add("Location", url)
-					if DEBUG {
+					if cfg.Debug {
 						log.Println("redirect to", string(loc))
 					}
@@ -470,5 +471,5 @@
 			err := HTML_BODY_EXTENSION.Execute(ctx, p)
 			if err != nil {
-				if DEBUG {
+				if cfg.Debug {
 					fmt.Println("failed to inject body extension", err)
 				}
@@ -556,5 +557,5 @@
 			out.Write([]byte(uri))
 			startIndex = urlEnd
-		} else if DEBUG {
+		} else if cfg.Debug {
 			log.Println("cannot proxify css uri:", string(css[urlStart:urlEnd]))
 		}
@@ -677,5 +678,5 @@
 					err := HTML_FORM_EXTENSION.Execute(out, HTMLFormExtParam{urlStr, key})
 					if err != nil {
-						if DEBUG {
+						if cfg.Debug {
 							fmt.Println("failed to inject body extension", err)
 						}
@@ -694,5 +695,5 @@
 					err := HTML_BODY_EXTENSION.Execute(out, p)
 					if err != nil {
-						if DEBUG {
+						if cfg.Debug {
 							fmt.Println("failed to inject body extension", err)
 						}
@@ -833,5 +834,5 @@
 		if uri, err := rc.ProxifyURI(attrValue); err == nil {
 			fmt.Fprintf(out, " %s=\"%s\"", attrName, uri)
-		} else if DEBUG {
+		} else if cfg.Debug {
 			log.Println("cannot proxify uri:", string(attrValue))
 		}
@@ -983,5 +984,5 @@
 	_, err := hex.Decode(h, hashMsg)
 	if err != nil {
-		if DEBUG {
+		if cfg.Debug {
 			log.Println("hmac error:", err)
 		}
@@ -1011,5 +1012,5 @@
 	ctx.Write([]byte(MORTY_HTML_PAGE_START))
 	if err != nil {
-		if DEBUG {
+		if cfg.Debug {
 			log.Println("error:", err)
 		}
@@ -1031,12 +1032,9 @@
 
 func main() {
-	default_listen_addr := os.Getenv("MORTY_ADDRESS")
-	if default_listen_addr == "" {
-		default_listen_addr = "127.0.0.1:3000"
-	}
-	default_key := os.Getenv("MORTY_KEY")
-	listen := flag.String("listen", default_listen_addr, "Listen address")
-	key := flag.String("key", default_key, "HMAC url validation key (base64 encoded) - leave blank to disable validation")
-	ipv6 := flag.Bool("ipv6", false, "Allow IPv6 HTTP requests")
+	cfg.ListenAddress = *flag.String("listen", cfg.ListenAddress, "Listen address")
+	cfg.Key = *flag.String("key", cfg.Key, "HMAC url validation key (base64 encoded) - leave blank to disable validation")
+	cfg.IPV6 = *flag.Bool("ipv6", cfg.IPV6, "Allow IPv6 HTTP requests")
+	cfg.Debug = *flag.Bool("debug", cfg.Debug, "Debug mode")
+	cfg.RequestTimeout = *flag.Uint("timeout", cfg.RequestTimeout, "Request timeout")
 	version := flag.Bool("version", false, "Show version")
 	requestTimeout := flag.Uint("timeout", 2, "Request timeout")
@@ -1057,10 +1055,13 @@
 		CLIENT.Dial = fasthttpproxy.FasthttpSocksDialer(*socks5)
 	}
-
-	p := &Proxy{RequestTimeout: time.Duration(*requestTimeout) * time.Second}
-
-	if *key != "" {
+	if cfg.IPV6 {
+		CLIENT.Dial = fasthttp.DialDualStack
+	}
+
+	p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second}
+
+	if cfg.Key != "" {
 		var err error
-		p.Key, err = base64.StdEncoding.DecodeString(*key)
+		p.Key, err = base64.StdEncoding.DecodeString(cfg.Key)
 		if err != nil {
 			log.Fatal("Error parsing -key", err.Error())
@@ -1069,7 +1070,7 @@
 	}
 
-	log.Println("listening on", *listen)
-
-	if err := fasthttp.ListenAndServe(*listen, p.RequestHandler); err != nil {
+	log.Println("listening on", cfg.ListenAddress)
+
+	if err := fasthttp.ListenAndServe(cfg.ListenAddress, p.RequestHandler); err != nil {
 		log.Fatal("Error in ListenAndServe:", err)
 	}
