Changeset 140 in code for trunk/morty.go
- Timestamp:
- Apr 20, 2023, 6:12:13 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/morty.go
r137 r140 66 66 contenttype.NewFilterEquals("image", "x-ms-bmp", ""), 67 67 contenttype.NewFilterEquals("image", "x-icon", ""), 68 contenttype.NewFilterEquals("image", "svg", "xml"), 68 69 // fonts 69 70 contenttype.NewFilterEquals("application", "font-otf", ""), … … 196 197 BaseURL string 197 198 HasMortyKey bool 199 URLParamName string 198 200 } 199 201 … … 201 203 BaseURL string 202 204 MortyHash string 205 URLParamName string 206 HashParamName string 207 } 208 type HTMLMainPageFormParam struct { 209 URLParamName string 203 210 } 204 211 205 212 var HTML_FORM_EXTENSION *template.Template 206 213 var HTML_BODY_EXTENSION *template.Template 214 var HTML_MAIN_PAGE_FORM *template.Template 207 215 var HTML_HEAD_CONTENT_TYPE string = `<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 208 216 <meta http-equiv="X-UA-Compatible" content="IE=edge"> … … 261 269 <label for="mortytoggle">hide</label> 262 270 <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}} /> 264 272 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>. 265 273 </form> … … 281 289 panic(err) 282 290 } 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 } 283 299 } 284 300 … … 289 305 } 290 306 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)) 294 310 295 311 if requestURI == nil { … … 301 317 if !verifyRequestURI(requestURI, requestHash, p.Key) { 302 318 // 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)) 304 321 return 305 322 } … … 352 369 353 370 req.SetRequestURI(requestURIStr) 354 req.Header.SetUserAgentBytes([]byte("Mozilla/5.0 (Windows NT 1 0.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")) 355 372 356 373 resp := fasthttp.AcquireResponse() … … 488 505 sanitizeHTML(rc, ctx, responseBody) 489 506 if !rc.BodyInjected { 490 p := HTMLBodyExtParam{rc.BaseURL.String(), false }507 p := HTMLBodyExtParam{rc.BaseURL.String(), false, cfg.UrlParameter} 491 508 if len(rc.Key) > 0 { 492 509 p.HasMortyKey = true … … 699 716 key = hash(urlStr, rc.Key) 700 717 } 701 err := HTML_FORM_EXTENSION.Execute(out, HTMLFormExtParam{urlStr, key })718 err := HTML_FORM_EXTENSION.Execute(out, HTMLFormExtParam{urlStr, key, cfg.UrlParameter, cfg.HashParameter}) 702 719 if err != nil { 703 720 if cfg.Debug { … … 712 729 switch string(tag) { 713 730 case "body": 714 p := HTMLBodyExtParam{rc.BaseURL.String(), false }731 p := HTMLBodyExtParam{rc.BaseURL.String(), false, cfg.UrlParameter} 715 732 if len(rc.Key) > 0 { 716 733 p.HasMortyKey = true … … 983 1000 984 1001 if rc.Key == nil { 985 return fmt.Sprintf("./? mortyurl=%s%s", url.QueryEscape(morty_uri), fragment), nil986 } 987 return fmt.Sprintf("./? mortyhash=%s&mortyurl=%s%s", hash(morty_uri, rc.Key), url.QueryEscape(morty_uri), fragment), nil1002 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 988 1005 } 989 1006 … … 1043 1060 } 1044 1061 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 } 1050 1069 } else { 1051 1070 ctx.Write([]byte(`<h3>Warning! This instance does not support direct URL opening.</h3>`)) … … 1064 1083 proxy := flag.String("proxy", "", "Use the specified HTTP proxy (ie: '[user:pass@]hostname:port'). Overrides -socks5, -ipv6.") 1065 1084 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=...')") 1066 1087 version := flag.Bool("version", false, "Show version") 1067 1088 flag.Parse() … … 1073 1094 cfg.RequestTimeout = *requestTimeout 1074 1095 cfg.FollowRedirect = *followRedirect 1096 cfg.UrlParameter = *urlParameter 1097 cfg.HashParameter = *hashParameter 1075 1098 1076 1099 if *version {
Note:
See TracChangeset
for help on using the changeset viewer.