This commit is contained in:
2022-02-23 11:55:56 +08:00
parent 1de0be23af
commit ce2159abc7

13
main.go
View File

@@ -3,7 +3,6 @@ package main
import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
)
@@ -84,14 +83,10 @@ func httpHandler(w http.ResponseWriter, r *http.Request, pathname string) {
func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname string) {
fmt.Println("Proxy: ", pathname)
r.URL, _ = url.Parse(pathname)
req, _ := http.NewRequest(r.Method, r.URL.String(), r.Body)
req, _ := http.NewRequest(r.Method, pathname, r.Body)
for k, v := range r.Header {
req.Header.Set(k, v[0])
}
for _, value := range r.Cookies() {
w.Header().Add(value.Name, value.Value)
}
res, e := client.Do(req)
if e != nil {
fmt.Println("Proxy Error: ", pathname, e)
@@ -105,7 +100,8 @@ func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname
res.Header.Set("location", PREFIX+location)
} else {
newReq, _ := http.NewRequest(r.Method, location, res.Body)
proxy(w, newReq, http.DefaultClient, location)
client.CheckRedirect = nil
proxy(w, newReq, client, location)
return
}
}
@@ -117,11 +113,12 @@ func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname
res.Header.Del("Content-Security-Policy-Report-Only")
res.Header.Del("Clear-Site-Data")
w.WriteHeader(res.StatusCode)
for k, v := range res.Header {
w.Header().Set(k, strings.Join(v, ","))
}
_ = res.Write(w)
w.WriteHeader(res.StatusCode)
fmt.Println("result: ", res.StatusCode)
}
func main() {