Changeset 140 in code


Ignore:
Timestamp:
Apr 20, 2023, 6:12:13 AM (2 years ago)
Author:
Izuru Yakumo
Message:

Incorporate most of the open pull requests found on GitHub

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/README.md

    r139 r140  
    2727
    2828```
    29 $ go get github.com/asciimoo/morty
     29$ go install github.com/asciimoo/morty@latest
    3030$ "$GOPATH/bin/morty" --help
    3131```
     
    3838  -followredirect
    3939        Follow HTTP GET redirect
     40  -hashparam string
     41        User-defined requesting string HASH parameter name (ie: '/?hash=...' or '/?h=...') (default "mortyhash")
    4042  -ipv6
    4143        Allow IPv6 HTTP requests (default true)
     
    5254  -timeout uint
    5355        Request timeout (default 5)
     56  -urlparam string
     57        User-defined requesting string URL parameter name (ie: '/?url=...' or '/?u=...') (default "mortyurl")
    5458  -version
    5559        Show version
     
    6165- `MORTY_ADDRESS`: Listen address (default to `127.0.0.1:3000`)
    6266- `MORTY_KEY`: HMAC url validation key (base64 encoded) to prevent direct URL opening. Leave blank to disable validation. Use `openssl rand -base64 33` to generate.
     67- `MORTY_URL_PARAM`: User-defined requesting string URL parameter name (ie: `/?url=...` or `/?u=...`) (default `mortyurl`)
     68- `MORTY_HASH_PARAM`: User-defined requesting string HASH parameter name (ie: `/?hash=...` or `/?h=...`) (default `mortyhash`)
    6369- `DEBUG`: Enable/disable proxy and redirection logs (default to `true`). Set to `false` to disable.
    6470
  • trunk/config/config.go

    r132 r140  
    1212        RequestTimeout uint
    1313        FollowRedirect bool
     14        MaxConnsPerHost uint
     15        UrlParameter string
     16        HashParameter string
    1417}
    1518
     
    2124                default_listen_addr = "127.0.0.1:3000"
    2225        }
     26        default_url_parameter := os.Getenv("MORTY_URL_PARAM")
     27        if default_url_parameter == "" {
     28                default_url_parameter = "mortyurl"
     29        }
     30        default_hash_parameter := os.Getenv("MORTY_HASH_PARAM")
     31        if default_hash_parameter == "" {
     32                default_hash_parameter = "mortyhash"
     33        }
    2334        default_key := os.Getenv("MORTY_KEY")
    2435        DefaultConfig = &Config{
     
    2940                RequestTimeout: 5,
    3041                FollowRedirect: false,
     42                MaxConnsPerHost: 4,
     43                UrlParameter: default_url_parameter,
     44                HashParameter: default_hash_parameter,
    3145        }
    3246}
  • trunk/go.mod

    r136 r140  
    44
    55require (
    6         github.com/valyala/fasthttp v1.21.0
    7         golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0
    8         golang.org/x/text v0.3.3
     6        github.com/valyala/fasthttp v1.34.0
     7        golang.org/x/net v0.7.0
     8        golang.org/x/text v0.7.0
    99)
  • trunk/go.sum

    r136 r140  
    1 github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
    2 github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
    3 github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
    4 github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
    5 github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
    6 github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
     1github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
     2github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
     3github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
     4github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
    75github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
    86github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
    9 github.com/valyala/fasthttp v1.14.0 h1:67bfuW9azCMwW/Jlq/C+VeihNpAuJMWkYPBig1gdi3A=
    10 github.com/valyala/fasthttp v1.14.0/go.mod h1:ol1PCaL0dX20wC0htZ7sYCsvCYmrouYra0zHzaclZhE=
    11 github.com/valyala/fasthttp v1.21.0 h1:fJjaQ7cXdaSF9vDBujlHLDGj7AgoMTMIXvICeePzYbU=
    12 github.com/valyala/fasthttp v1.21.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
    13 github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
     7github.com/valyala/fasthttp v1.34.0 h1:d3AAQJ2DRcxJYHm7OXNXtXt2as1vMDfxeIcFvhmGGm4=
     8github.com/valyala/fasthttp v1.34.0/go.mod h1:epZA5N+7pY6ZaEKRmstzOuYJx9HI8DI1oaCGZpdH4h0=
     9github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
     10github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
    1411golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
    15 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
    16 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
    17 golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
    18 golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
    19 golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
    20 golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
    21 golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 h1:5kGOVHlq0euqwzgTC9Vu15p6fV1Wi0ArVi8da2urnVg=
    22 golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
     12golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
     13golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
     14golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
     15golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
     16golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
     17golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
     18golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
     19golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
     20golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
     21golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
     22golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
     23golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
    2324golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
    24 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
    25 golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
    26 golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
     25golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
     26golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
     27golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
     28golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
     29golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
     30golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
     31golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
     32golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
     33golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
     34golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
     35golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
    2736golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
    28 golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
    2937golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
     38golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
     39golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
     40golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
     41golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
    3042golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
     43golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
     44golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
     45golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
  • trunk/morty.go

    r137 r140  
    6666        contenttype.NewFilterEquals("image", "x-ms-bmp", ""),
    6767        contenttype.NewFilterEquals("image", "x-icon", ""),
     68        contenttype.NewFilterEquals("image", "svg", "xml"),
    6869        // fonts
    6970        contenttype.NewFilterEquals("application", "font-otf", ""),
     
    196197        BaseURL     string
    197198        HasMortyKey bool
     199        URLParamName string
    198200}
    199201
     
    201203        BaseURL   string
    202204        MortyHash string
     205        URLParamName string
     206        HashParamName string
     207}
     208type HTMLMainPageFormParam struct {
     209        URLParamName string
    203210}
    204211
    205212var HTML_FORM_EXTENSION *template.Template
    206213var HTML_BODY_EXTENSION *template.Template
     214var HTML_MAIN_PAGE_FORM *template.Template
    207215var HTML_HEAD_CONTENT_TYPE string = `<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    208216<meta http-equiv="X-UA-Compatible" content="IE=edge">
     
    261269    <label for="mortytoggle">hide</label>
    262270    <span><a href="/">Morty Proxy</a></span>
    263     <input type="url" value="{{.BaseURL}}" name="mortyurl" {{if .HasMortyKey }}readonly="true"{{end}} />
     271    <input type="url" value="{{.BaseURL}}" name="{{.URLParamName}}" {{if .HasMortyKey }}readonly="true"{{end}} />
    264272    This is a <a href="https://github.com/asciimoo/morty">proxified and sanitized</a> view of the page, visit <a href="{{.BaseURL}}" rel="noreferrer">original site</a>.
    265273  </form>
     
    281289                panic(err)
    282290        }
     291        HTML_MAIN_PAGE_FORM, err = template.New("html_main_page_form").Parse(`
     292        <form action="post">
     293        Visit url: <input placeholder="https://url.." name="{{.URLParamName}}" autofocus />
     294        <input type="submit" value="go" />
     295        </form>`)
     296        if err != nil {
     297                panic(err)
     298        }
    283299}
    284300
     
    289305        }
    290306
    291         requestHash := popRequestParam(ctx, []byte("mortyhash"))
    292 
    293         requestURI := popRequestParam(ctx, []byte("mortyurl"))
     307        requestHash := popRequestParam(ctx, []byte(cfg.HashParameter))
     308
     309        requestURI := popRequestParam(ctx, []byte(cfg.UrlParameter))
    294310
    295311        if requestURI == nil {
     
    301317                if !verifyRequestURI(requestURI, requestHash, p.Key) {
    302318                        // HTTP status code 403 : Forbidden
    303                         p.serveMainPage(ctx, 403, errors.New(`invalid "mortyhash" parameter`))
     319                        error_message := fmt.Sprintf(`invalid "%s" parameter. hint: Hash URL Parameter`, cfg.HashParameter)
     320                        p.serveMainPage(ctx, 403, errors.New(error_message))
    304321                        return
    305322                }
     
    352369
    353370        req.SetRequestURI(requestURIStr)
    354         req.Header.SetUserAgentBytes([]byte("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0"))
     371        req.Header.SetUserAgentBytes([]byte("Mozilla/5.0 (Windows NT 1.0; Win64; x64; rv:112.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"))
    355372
    356373        resp := fasthttp.AcquireResponse()
     
    488505                sanitizeHTML(rc, ctx, responseBody)
    489506                if !rc.BodyInjected {
    490                         p := HTMLBodyExtParam{rc.BaseURL.String(), false}
     507                        p := HTMLBodyExtParam{rc.BaseURL.String(), false, cfg.UrlParameter}
    491508                        if len(rc.Key) > 0 {
    492509                                p.HasMortyKey = true
     
    699716                                                key = hash(urlStr, rc.Key)
    700717                                        }
    701                                         err := HTML_FORM_EXTENSION.Execute(out, HTMLFormExtParam{urlStr, key})
     718                                        err := HTML_FORM_EXTENSION.Execute(out, HTMLFormExtParam{urlStr, key, cfg.UrlParameter, cfg.HashParameter})
    702719                                        if err != nil {
    703720                                                if cfg.Debug {
     
    712729                                switch string(tag) {
    713730                                case "body":
    714                                         p := HTMLBodyExtParam{rc.BaseURL.String(), false}
     731                                        p := HTMLBodyExtParam{rc.BaseURL.String(), false, cfg.UrlParameter}
    715732                                        if len(rc.Key) > 0 {
    716733                                                p.HasMortyKey = true
     
    9831000
    9841001        if rc.Key == nil {
    985                 return fmt.Sprintf("./?mortyurl=%s%s", url.QueryEscape(morty_uri), fragment), nil
    986         }
    987         return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s%s", hash(morty_uri, rc.Key), url.QueryEscape(morty_uri), fragment), nil
     1002                return fmt.Sprintf("./?%s=%s%s", cfg.UrlParameter, url.QueryEscape(morty_uri), fragment), nil
     1003        }
     1004        return fmt.Sprintf("./?%s=%s&%s=%s%s", cfg.HashParameter, hash(morty_uri, rc.Key), cfg.UrlParameter, url.QueryEscape(morty_uri), fragment), nil
    9881005}
    9891006
     
    10431060        }
    10441061        if p.Key == nil {
    1045                 ctx.Write([]byte(`
    1046                 <form action="post">
    1047                 Visit url: <input placeholder="https://url.." name="mortyurl" autofocus />
    1048                 <input type="submit" value="go" />
    1049                 </form>`))
     1062                p := HTMLMainPageFormParam{cfg.UrlParameter}
     1063                err := HTML_MAIN_PAGE_FORM.Execute(ctx, p)
     1064                if err != nil {
     1065                        if cfg.Debug {
     1066                                fmt.Println("failed to inject main page form", err)
     1067                        }
     1068                }
    10501069        } else {
    10511070                ctx.Write([]byte(`<h3>Warning! This instance does not support direct URL opening.</h3>`))
     
    10641083        proxy := flag.String("proxy", "", "Use the specified HTTP proxy (ie: '[user:pass@]hostname:port'). Overrides -socks5, -ipv6.")
    10651084        socks5 := flag.String("socks5", "", "Use a SOCKS5 proxy (ie: 'hostname:port'). Overrides -ipv6.")
     1085        urlParameter := flag.String("urlparam", cfg.UrlParameter, "user-defined requesting string URL parameter name (ie: '/?url=...' or '/?u=...')")
     1086        hashParameter := flag.String("hashparam", cfg.HashParameter, "user-defined requesting string HASH parameter name (ie: '/?hash=...' or '/?h=...')")
    10661087        version := flag.Bool("version", false, "Show version")
    10671088        flag.Parse()
     
    10731094        cfg.RequestTimeout = *requestTimeout
    10741095        cfg.FollowRedirect = *followRedirect
     1096        cfg.UrlParameter = *urlParameter
     1097        cfg.HashParameter = *hashParameter
    10751098
    10761099        if *version {
Note: See TracChangeset for help on using the changeset viewer.