diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..b568838 --- /dev/null +++ b/config/config.go @@ -0,0 +1,30 @@ +package config + +import ( + "os" +) + +type Config struct { + Debug bool + ListenAddress string + Key string + IPV6 bool + RequestTimeout uint +} + +var DefaultConfig *Config + +func init() { + 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") + DefaultConfig = &Config{ + Debug: os.Getenv("DEBUG") != "false", + ListenAddress: default_listen_addr, + Key: default_key, + IPV6: true, + RequestTimeout: 5, + } +} diff --git a/morty.go b/morty.go index 0f5c4b5..0f9964b 100644 --- a/morty.go +++ b/morty.go @@ -27,6 +27,7 @@ import ( "golang.org/x/net/html/charset" "golang.org/x/text/encoding" + "github.com/asciimoo/morty/config" "github.com/asciimoo/morty/contenttype" ) @@ -38,14 +39,12 @@ const ( const VERSION = "v0.2.0" -var DEBUG = os.Getenv("DEBUG") != "false" - var CLIENT *fasthttp.Client = &fasthttp.Client{ MaxResponseBodySize: 10 * 1024 * 1024, // 10M ReadBufferSize: 16 * 1024, // 16K } -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{ // html @@ -177,6 +176,8 @@ var LINK_HTTP_EQUIV_SAFE_VALUES [][]byte = [][]byte{ []byte("content-language"), } +var CSS_URL_REGEXP *regexp.Regexp = regexp.MustCompile("url\\((['\"]?)[ \\t\\f]*([\u0009\u0021\u0023-\u0026\u0028\u002a-\u007E]+)(['\"]?)\\)?") + type Proxy struct { Key []byte RequestTimeout time.Duration @@ -336,7 +337,7 @@ func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) { requestURIStr := string(requestURI) - if DEBUG { + if cfg.Debug { log.Println("getting", requestURIStr) } @@ -374,7 +375,7 @@ func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) { if err == nil { ctx.SetStatusCode(resp.StatusCode()) ctx.Response.Header.Add("Location", url) - if DEBUG { + if cfg.Debug { log.Println("redirect to", string(loc)) } return @@ -469,7 +470,7 @@ func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) { } err := HTML_BODY_EXTENSION.Execute(ctx, p) if err != nil { - if DEBUG { + if cfg.Debug { fmt.Println("failed to inject body extension", err) } } @@ -555,7 +556,7 @@ func sanitizeCSS(rc *RequestConfig, out io.Writer, css []byte) { out.Write(css[startIndex:urlStart]) out.Write([]byte(uri)) startIndex = urlEnd - } else if DEBUG { + } else if cfg.Debug { log.Println("cannot proxify css uri:", string(css[urlStart:urlEnd])) } } @@ -676,7 +677,7 @@ func sanitizeHTML(rc *RequestConfig, out io.Writer, htmlDoc []byte) { } 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) } } @@ -693,7 +694,7 @@ func sanitizeHTML(rc *RequestConfig, out io.Writer, htmlDoc []byte) { } err := HTML_BODY_EXTENSION.Execute(out, p) if err != nil { - if DEBUG { + if cfg.Debug { fmt.Println("failed to inject body extension", err) } } @@ -832,7 +833,7 @@ func sanitizeAttr(rc *RequestConfig, out io.Writer, attrName, attrValue, escaped case "src", "href", "action": 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)) } case "style": @@ -982,7 +983,7 @@ func verifyRequestURI(uri, hashMsg, key []byte) bool { h := make([]byte, hex.DecodedLen(len(hashMsg))) _, err := hex.Decode(h, hashMsg) if err != nil { - if DEBUG { + if cfg.Debug { log.Println("hmac error:", err) } return false @@ -1010,7 +1011,7 @@ func (p *Proxy) serveMainPage(ctx *fasthttp.RequestCtx, statusCode int, err erro ctx.SetStatusCode(statusCode) ctx.Write([]byte(MORTY_HTML_PAGE_START)) if err != nil { - if DEBUG { + if cfg.Debug { log.Println("error:", err) } ctx.Write([]byte("