Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mod] follow HTTP redirect (only GET HTTP method) #76

Merged
merged 2 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Config struct {
Key string
IPV6 bool
RequestTimeout uint
FollowRedirect bool
}

var DefaultConfig *Config
Expand All @@ -26,5 +27,6 @@ func init() {
Key: default_key,
IPV6: true,
RequestTimeout: 5,
FollowRedirect: false,
}
}
47 changes: 34 additions & 13 deletions morty.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (

const VERSION = "v0.2.0"

const MAX_REDIRECT_COUNT = 5

var CLIENT *fasthttp.Client = &fasthttp.Client{
MaxResponseBodySize: 10 * 1024 * 1024, // 10M
ReadBufferSize: 16 * 1024, // 16K
Expand Down Expand Up @@ -181,6 +183,7 @@ var CSS_URL_REGEXP *regexp.Regexp = regexp.MustCompile("url\\((['\"]?)[ \\t\\f]*
type Proxy struct {
Key []byte
RequestTimeout time.Duration
FollowRedirect bool
}

type RequestConfig struct {
Expand Down Expand Up @@ -312,7 +315,11 @@ func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) {
requestURI = append(requestURI, requestURIQuery...)
}

parsedURI, err := url.Parse(string(requestURI))
p.ProcessUri(ctx, string(requestURI), 0)
}

func (p *Proxy) ProcessUri(ctx *fasthttp.RequestCtx, requestURIStr string, redirectCount int) {
parsedURI, err := url.Parse(requestURIStr)

if err != nil {
// HTTP status code 500 : Internal Server Error
Expand All @@ -321,8 +328,8 @@ func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) {
}

if parsedURI.Scheme == "" {
requestURI = append([]byte("https://"), requestURI...)
parsedURI, err = url.Parse(string(requestURI))
requestURIStr = "https://" + requestURIStr
parsedURI, err = url.Parse(requestURIStr)
if err != nil {
p.serveMainPage(ctx, 500, err)
return
Expand All @@ -339,8 +346,6 @@ func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) {
defer fasthttp.ReleaseRequest(req)
req.SetConnectionClose()

requestURIStr := string(requestURI)

if cfg.Debug {
log.Println(string(ctx.Method()), requestURIStr)
}
Expand Down Expand Up @@ -374,15 +379,29 @@ func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) {
case 301, 302, 303, 307, 308:
loc := resp.Header.Peek("Location")
if loc != nil {
rc := &RequestConfig{Key: p.Key, BaseURL: parsedURI}
url, err := rc.ProxifyURI(loc)
if err == nil {
ctx.SetStatusCode(resp.StatusCode())
ctx.Response.Header.Add("Location", url)
if cfg.Debug {
log.Println("redirect to", string(loc))
if p.FollowRedirect && ctx.IsGet() {
// GET method: Morty follows the redirect
if redirectCount < MAX_REDIRECT_COUNT {
if cfg.Debug {
log.Println("follow redirect to", string(loc))
}
p.ProcessUri(ctx, string(loc), redirectCount+1)
} else {
p.serveMainPage(ctx, 310, errors.New("Too many redirects"))
}
return
} else {
// Other HTTP methods: Morty does NOT follow the redirect
rc := &RequestConfig{Key: p.Key, BaseURL: parsedURI}
url, err := rc.ProxifyURI(loc)
if err == nil {
ctx.SetStatusCode(resp.StatusCode())
ctx.Response.Header.Add("Location", url)
if cfg.Debug {
log.Println("redirect to", string(loc))
}
return
}
}
}
}
Expand Down Expand Up @@ -1040,6 +1059,7 @@ func main() {
cfg.IPV6 = *flag.Bool("ipv6", cfg.IPV6, "Allow IPv6 HTTP requests")
cfg.Debug = *flag.Bool("debug", cfg.Debug, "Debug mode")
cfg.RequestTimeout = *flag.Uint("timeout", cfg.RequestTimeout, "Request timeout")
cfg.FollowRedirect = *flag.Bool("followredirect", cfg.FollowRedirect, "Follow HTTP GET redirect")
version := flag.Bool("version", false, "Show version")
socks5 := flag.String("socks5", "", "SOCKS5 proxy")
flag.Parse()
Expand All @@ -1057,7 +1077,8 @@ func main() {
CLIENT.Dial = fasthttp.DialDualStack
}

p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second}
p := &Proxy{RequestTimeout: time.Duration(cfg.RequestTimeout) * time.Second,
FollowRedirect: cfg.FollowRedirect}

if cfg.Key != "" {
var err error
Expand Down