Changeset 140 in code for trunk/morty.go


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@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.