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

git-svn-id: file:///srv/svn/repo/yukari/trunk@131 f3bd38d9-da89-464d-a02a-eb04e43141b5
This commit is contained in:
alex 2020-07-24 14:56:05 +00:00
parent af8036aa09
commit 978b5d40ec
2 changed files with 36 additions and 13 deletions

View File

@ -10,6 +10,7 @@ type Config struct {
Key string
IPV6 bool
RequestTimeout uint
FollowRedirect bool
}
var DefaultConfig *Config
@ -26,5 +27,6 @@ func init() {
Key: default_key,
IPV6: true,
RequestTimeout: 5,
FollowRedirect: false,
}
}

View File

@ -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
@ -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 {
@ -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
@ -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
@ -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)
}
@ -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
}
}
}
}
@ -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()
@ -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