Changeset 131 in code for trunk/morty.go


Ignore:
Timestamp:
Jul 24, 2020, 2:56:05 PM (5 years ago)
Author:
alex
Message:

Merge remote-tracking branch 'origin/master' into redirect

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r130 r131  
    3939
    4040const VERSION = "v0.2.0"
     41
     42const MAX_REDIRECT_COUNT = 5
    4143
    4244var CLIENT *fasthttp.Client = &fasthttp.Client{
     
    182184        Key            []byte
    183185        RequestTimeout time.Duration
     186        FollowRedirect bool
    184187}
    185188
     
    313316        }
    314317
    315         parsedURI, err := url.Parse(string(requestURI))
     318        p.ProcessUri(ctx, string(requestURI), 0)
     319}
     320
     321func (p *Proxy) ProcessUri(ctx *fasthttp.RequestCtx, requestURIStr string, redirectCount int) {
     322        parsedURI, err := url.Parse(requestURIStr)
    316323
    317324        if err != nil {
     
    322329
    323330        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)
    326333                if err != nil {
    327334                        p.serveMainPage(ctx, 500, err)
     
    339346        defer fasthttp.ReleaseRequest(req)
    340347        req.SetConnectionClose()
    341 
    342         requestURIStr := string(requestURI)
    343348
    344349        if cfg.Debug {
     
    375380                        loc := resp.Header.Peek("Location")
    376381                        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"))
    384391                                        }
    385392                                        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                                        }
    386405                                }
    387406                        }
     
    10411060        cfg.Debug = *flag.Bool("debug", cfg.Debug, "Debug mode")
    10421061        cfg.RequestTimeout = *flag.Uint("timeout", cfg.RequestTimeout, "Request timeout")
     1062        cfg.FollowRedirect = *flag.Bool("followredirect", cfg.FollowRedirect, "Follow HTTP GET redirect")
    10431063        version := flag.Bool("version", false, "Show version")
    10441064        socks5 := flag.String("socks5", "", "SOCKS5 proxy")
     
    10581078        }
    10591079
    1060         p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second}
     1080        p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second,
     1081                FollowRedirect: cfg.FollowRedirect}
    10611082
    10621083        if cfg.Key != "" {
Note: See TracChangeset for help on using the changeset viewer.