- Timestamp:
- Jul 24, 2020, 2:56:05 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/config/config.go
r127 r131 11 11 IPV6 bool 12 12 RequestTimeout uint 13 FollowRedirect bool 13 14 } 14 15 … … 27 28 IPV6: true, 28 29 RequestTimeout: 5, 30 FollowRedirect: false, 29 31 } 30 32 } -
trunk/morty.go
r130 r131 39 39 40 40 const VERSION = "v0.2.0" 41 42 const MAX_REDIRECT_COUNT = 5 41 43 42 44 var CLIENT *fasthttp.Client = &fasthttp.Client{ … … 182 184 Key []byte 183 185 RequestTimeout time.Duration 186 FollowRedirect bool 184 187 } 185 188 … … 313 316 } 314 317 315 parsedURI, err := url.Parse(string(requestURI)) 318 p.ProcessUri(ctx, string(requestURI), 0) 319 } 320 321 func (p *Proxy) ProcessUri(ctx *fasthttp.RequestCtx, requestURIStr string, redirectCount int) { 322 parsedURI, err := url.Parse(requestURIStr) 316 323 317 324 if err != nil { … … 322 329 323 330 if parsedURI.Scheme == "" { 324 requestURI = append([]byte("https://"), requestURI...)325 parsedURI, err = url.Parse( string(requestURI))331 requestURIStr = "https://" + requestURIStr 332 parsedURI, err = url.Parse(requestURIStr) 326 333 if err != nil { 327 334 p.serveMainPage(ctx, 500, err) … … 339 346 defer fasthttp.ReleaseRequest(req) 340 347 req.SetConnectionClose() 341 342 requestURIStr := string(requestURI)343 348 344 349 if cfg.Debug { … … 375 380 loc := resp.Header.Peek("Location") 376 381 if loc != nil { 377 rc := &RequestConfig{Key: p.Key, BaseURL: parsedURI} 378 url, err := rc.ProxifyURI(loc) 379 if err == nil { 380 ctx.SetStatusCode(resp.StatusCode()) 381 ctx.Response.Header.Add("Location", url) 382 if cfg.Debug { 383 log.Println("redirect to", string(loc)) 382 if p.FollowRedirect && ctx.IsGet() { 383 // GET method: Morty follows the redirect 384 if redirectCount < MAX_REDIRECT_COUNT { 385 if cfg.Debug { 386 log.Println("follow redirect to", string(loc)) 387 } 388 p.ProcessUri(ctx, string(loc), redirectCount+1) 389 } else { 390 p.serveMainPage(ctx, 310, errors.New("Too many redirects")) 384 391 } 385 392 return 393 } else { 394 // Other HTTP methods: Morty does NOT follow the redirect 395 rc := &RequestConfig{Key: p.Key, BaseURL: parsedURI} 396 url, err := rc.ProxifyURI(loc) 397 if err == nil { 398 ctx.SetStatusCode(resp.StatusCode()) 399 ctx.Response.Header.Add("Location", url) 400 if cfg.Debug { 401 log.Println("redirect to", string(loc)) 402 } 403 return 404 } 386 405 } 387 406 } … … 1041 1060 cfg.Debug = *flag.Bool("debug", cfg.Debug, "Debug mode") 1042 1061 cfg.RequestTimeout = *flag.Uint("timeout", cfg.RequestTimeout, "Request timeout") 1062 cfg.FollowRedirect = *flag.Bool("followredirect", cfg.FollowRedirect, "Follow HTTP GET redirect") 1043 1063 version := flag.Bool("version", false, "Show version") 1044 1064 socks5 := flag.String("socks5", "", "SOCKS5 proxy") … … 1058 1078 } 1059 1079 1060 p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second} 1080 p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second, 1081 FollowRedirect: cfg.FollowRedirect} 1061 1082 1062 1083 if cfg.Key != "" {
Note:
See TracChangeset
for help on using the changeset viewer.