This commit is contained in:
2022-02-23 19:42:35 +08:00
parent 29fcf25c0e
commit 0d5b801c78

12
main.go
View File

@@ -60,7 +60,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request, pathname string) {
fmt.Println("Handle: ", pathname) fmt.Println("Handle: ", pathname)
reqHdrRaw := r.Header reqHdrRaw := r.Header
if r.Method == http.MethodOptions && reqHdrRaw.Get("access-control-request-headers") != "" { if r.Method == http.MethodOptions && reqHdrRaw.Get("Access-Control-Request-Headers") != "" {
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, TRACE, DELETE, HEAD, OPTIONS") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, TRACE, DELETE, HEAD, OPTIONS")
w.Header().Set("Access-Control-Max-Age", "1728000") w.Header().Set("Access-Control-Max-Age", "1728000")
@@ -85,7 +85,7 @@ func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname
fmt.Println("Proxy: ", pathname) fmt.Println("Proxy: ", pathname)
req, _ := http.NewRequest(r.Method, pathname, r.Body) req, _ := http.NewRequest(r.Method, pathname, r.Body)
for k, v := range r.Header { for k, v := range r.Header {
req.Header.Set(k, v[0]) req.Header.Set(k, strings.Join(v, ","))
} }
res, e := client.Do(req) res, e := client.Do(req)
if e != nil { if e != nil {
@@ -94,10 +94,12 @@ func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname
return return
} }
if res.Header.Get("location") != "" { if res.Header.Get("Location") != "" {
location := res.Header.Get("location") location := res.Header.Get("Location")
fmt.Println("Location: ", location)
if checkUrl(location) { if checkUrl(location) {
res.Header.Set("location", PREFIX+location) res.Header.Set("Location", PREFIX+location)
} else { } else {
newReq, _ := http.NewRequest(r.Method, location, res.Body) newReq, _ := http.NewRequest(r.Method, location, res.Body)
client.CheckRedirect = nil client.CheckRedirect = nil