Changeset 127 in code for trunk


Ignore:
Timestamp:
Jul 14, 2020, 9:23:59 PM (5 years ago)
Author:
asciimoo
Message:

[mod] create own module for config

Location:
trunk
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r126 r127  
    2828        "golang.org/x/text/encoding"
    2929
     30        "github.com/asciimoo/morty/config"
    3031        "github.com/asciimoo/morty/contenttype"
    3132)
     
    3940const VERSION = "v0.2.0"
    4041
    41 var DEBUG = os.Getenv("DEBUG") != "false"
    42 
    4342var CLIENT *fasthttp.Client = &fasthttp.Client{
    4443        MaxResponseBodySize: 10 * 1024 * 1024, // 10M
     
    4645}
    4746
    48 var CSS_URL_REGEXP *regexp.Regexp = regexp.MustCompile("url\\((['\"]?)[ \\t\\f]*([\u0009\u0021\u0023-\u0026\u0028\u002a-\u007E]+)(['\"]?)\\)?")
     47var cfg *config.Config = config.DefaultConfig
    4948
    5049var ALLOWED_CONTENTTYPE_FILTER contenttype.Filter = contenttype.NewFilterOr([]contenttype.Filter{
     
    178177}
    179178
     179var CSS_URL_REGEXP *regexp.Regexp = regexp.MustCompile("url\\((['\"]?)[ \\t\\f]*([\u0009\u0021\u0023-\u0026\u0028\u002a-\u007E]+)(['\"]?)\\)?")
     180
    180181type Proxy struct {
    181182        Key            []byte
     
    337338        requestURIStr := string(requestURI)
    338339
    339         if DEBUG {
     340        if cfg.Debug {
    340341                log.Println("getting", requestURIStr)
    341342        }
     
    375376                                        ctx.SetStatusCode(resp.StatusCode())
    376377                                        ctx.Response.Header.Add("Location", url)
    377                                         if DEBUG {
     378                                        if cfg.Debug {
    378379                                                log.Println("redirect to", string(loc))
    379380                                        }
     
    470471                        err := HTML_BODY_EXTENSION.Execute(ctx, p)
    471472                        if err != nil {
    472                                 if DEBUG {
     473                                if cfg.Debug {
    473474                                        fmt.Println("failed to inject body extension", err)
    474475                                }
     
    556557                        out.Write([]byte(uri))
    557558                        startIndex = urlEnd
    558                 } else if DEBUG {
     559                } else if cfg.Debug {
    559560                        log.Println("cannot proxify css uri:", string(css[urlStart:urlEnd]))
    560561                }
     
    677678                                        err := HTML_FORM_EXTENSION.Execute(out, HTMLFormExtParam{urlStr, key})
    678679                                        if err != nil {
    679                                                 if DEBUG {
     680                                                if cfg.Debug {
    680681                                                        fmt.Println("failed to inject body extension", err)
    681682                                                }
     
    694695                                        err := HTML_BODY_EXTENSION.Execute(out, p)
    695696                                        if err != nil {
    696                                                 if DEBUG {
     697                                                if cfg.Debug {
    697698                                                        fmt.Println("failed to inject body extension", err)
    698699                                                }
     
    833834                if uri, err := rc.ProxifyURI(attrValue); err == nil {
    834835                        fmt.Fprintf(out, " %s=\"%s\"", attrName, uri)
    835                 } else if DEBUG {
     836                } else if cfg.Debug {
    836837                        log.Println("cannot proxify uri:", string(attrValue))
    837838                }
     
    983984        _, err := hex.Decode(h, hashMsg)
    984985        if err != nil {
    985                 if DEBUG {
     986                if cfg.Debug {
    986987                        log.Println("hmac error:", err)
    987988                }
     
    10111012        ctx.Write([]byte(MORTY_HTML_PAGE_START))
    10121013        if err != nil {
    1013                 if DEBUG {
     1014                if cfg.Debug {
    10141015                        log.Println("error:", err)
    10151016                }
     
    10311032
    10321033func main() {
    1033         default_listen_addr := os.Getenv("MORTY_ADDRESS")
    1034         if default_listen_addr == "" {
    1035                 default_listen_addr = "127.0.0.1:3000"
    1036         }
    1037         default_key := os.Getenv("MORTY_KEY")
    1038         listen := flag.String("listen", default_listen_addr, "Listen address")
    1039         key := flag.String("key", default_key, "HMAC url validation key (base64 encoded) - leave blank to disable validation")
    1040         ipv6 := flag.Bool("ipv6", false, "Allow IPv6 HTTP requests")
     1034        cfg.ListenAddress = *flag.String("listen", cfg.ListenAddress, "Listen address")
     1035        cfg.Key = *flag.String("key", cfg.Key, "HMAC url validation key (base64 encoded) - leave blank to disable validation")
     1036        cfg.IPV6 = *flag.Bool("ipv6", cfg.IPV6, "Allow IPv6 HTTP requests")
     1037        cfg.Debug = *flag.Bool("debug", cfg.Debug, "Debug mode")
     1038        cfg.RequestTimeout = *flag.Uint("timeout", cfg.RequestTimeout, "Request timeout")
    10411039        version := flag.Bool("version", false, "Show version")
    10421040        requestTimeout := flag.Uint("timeout", 2, "Request timeout")
     
    10571055                CLIENT.Dial = fasthttpproxy.FasthttpSocksDialer(*socks5)
    10581056        }
    1059 
    1060         p := &Proxy{RequestTimeout: time.Duration(*requestTimeout) * time.Second}
    1061 
    1062         if *key != "" {
     1057        if cfg.IPV6 {
     1058                CLIENT.Dial = fasthttp.DialDualStack
     1059        }
     1060
     1061        p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second}
     1062
     1063        if cfg.Key != "" {
    10631064                var err error
    1064                 p.Key, err = base64.StdEncoding.DecodeString(*key)
     1065                p.Key, err = base64.StdEncoding.DecodeString(cfg.Key)
    10651066                if err != nil {
    10661067                        log.Fatal("Error parsing -key", err.Error())
     
    10691070        }
    10701071
    1071         log.Println("listening on", *listen)
    1072 
    1073         if err := fasthttp.ListenAndServe(*listen, p.RequestHandler); err != nil {
     1072        log.Println("listening on", cfg.ListenAddress)
     1073
     1074        if err := fasthttp.ListenAndServe(cfg.ListenAddress, p.RequestHandler); err != nil {
    10741075                log.Fatal("Error in ListenAndServe:", err)
    10751076        }
Note: See TracChangeset for help on using the changeset viewer.