This commit is contained in:
2022-02-23 11:41:32 +08:00
parent 808b315360
commit e92e9f612a

24
main.go
View File

@@ -38,28 +38,27 @@ func fetchHandler(w http.ResponseWriter, r *http.Request) {
path = strings.TrimPrefix(path, "/") // 获取到的URL path = strings.TrimPrefix(path, "/") // 获取到的URL
path = strings.Replace(path, "https:/", "https://", 1) path = strings.Replace(path, "https:/", "https://", 1)
u, _ := url.Parse(path) match1, _ := regexp.MatchString(exp1, path)
match2, _ := regexp.MatchString(exp2, path)
match1, _ := regexp.MatchString(exp1, u.String()) match3, _ := regexp.MatchString(exp3, path)
match2, _ := regexp.MatchString(exp2, u.String()) match4, _ := regexp.MatchString(exp4, path)
match3, _ := regexp.MatchString(exp3, u.String()) match5, _ := regexp.MatchString(exp5, path)
match4, _ := regexp.MatchString(exp4, u.String()) match6, _ := regexp.MatchString(exp6, path)
match5, _ := regexp.MatchString(exp5, u.String())
match6, _ := regexp.MatchString(exp6, u.String())
if match1 || match5 || match6 || match3 || match4 { if match1 || match5 || match6 || match3 || match4 {
httpHandler(w, r, u.String()) httpHandler(w, r, path)
return return
} else if match2 { } else if match2 {
u.Path = strings.ReplaceAll(u.Path, "/blob/", "/raw/") path = strings.ReplaceAll(path, "/blob/", "/raw/")
httpHandler(w, r, u.String()) httpHandler(w, r, path)
return return
} }
http.Error(w, fmt.Sprintf("%s is not support", u.String()), http.StatusOK) http.Error(w, fmt.Sprintf("%s is not support", path), http.StatusOK)
} }
func httpHandler(w http.ResponseWriter, r *http.Request, pathname string) { func httpHandler(w http.ResponseWriter, r *http.Request, pathname string) {
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") != "" {
@@ -84,6 +83,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request, pathname string) {
} }
func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname string) { func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname string) {
fmt.Println("Proxy: ", pathname)
r.URL, _ = url.Parse(pathname) r.URL, _ = url.Parse(pathname)
req, _ := http.NewRequest(r.Method, r.URL.String(), r.Body) req, _ := http.NewRequest(r.Method, r.URL.String(), r.Body)
for k, v := range r.Header { for k, v := range r.Header {