diff --git a/.vscode/launch.json b/.vscode/launch.json index 892d0a9990..e6be0f443d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,8 @@ "mode": "auto", "program": "${workspaceFolder}", "env": { - "Go-Proxy-BingAI-Debug": "true" + "Go-Proxy-BingAI-Debug": "true", + "PORT": "8888" } } ] diff --git a/README.md b/README.md index ca05672727..7ae2eafcae 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ - https://bing-vercel.vcanbb.top -- https://go-proxy-bingai-vercel.vercel.app +- https://go-proxy-bingai-git-master-adams549659584.vercel.app ### Render搭建 diff --git a/api/index.go b/api/index.go index 4f0e016bf3..ea86b13777 100644 --- a/api/index.go +++ b/api/index.go @@ -7,7 +7,7 @@ import ( func Index(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { - http.Redirect(w, r, "/web/chat.html", http.StatusFound) + http.Redirect(w, r, common.PROXY_WEB_PAGE_PATH, http.StatusFound) } else { common.NewSingleHostReverseProxy(common.BING_URL).ServeHTTP(w, r) } diff --git a/api/web.go b/api/web.go index 80ae3552be..e3fce7c8a9 100644 --- a/api/web.go +++ b/api/web.go @@ -1,10 +1,15 @@ package api import ( + "adams549659584/go-proxy-bingai/common" "adams549659584/go-proxy-bingai/web" "net/http" ) func WebStatic(w http.ResponseWriter, r *http.Request) { - http.StripPrefix("/web/", http.FileServer(web.GetWebFS())).ServeHTTP(w, r) + if _, ok := web.WEB_PATH_MAP[r.URL.Path]; ok { + http.StripPrefix(common.PROXY_WEB_PREFIX_PATH, http.FileServer(web.GetWebFS())).ServeHTTP(w, r) + } else { + common.NewSingleHostReverseProxy(common.BING_URL).ServeHTTP(w, r) + } } diff --git a/common/proxy.go b/common/proxy.go index df170475b2..d1b39b06e9 100644 --- a/common/proxy.go +++ b/common/proxy.go @@ -19,7 +19,8 @@ var ( BING_CHAT_DOMAIN = "https://sydney.bing.com" BING_CHAT_URL, _ = url.Parse(BING_CHAT_DOMAIN + "/sydney/ChatHub") BING_URL, _ = url.Parse("https://www.bing.com") - KEEP_HEADERS = map[string]bool{ + // EDGE_SVC_URL, _ = url.Parse("https://edgeservices.bing.com") + KEEP_REQ_HEADER_MAP = map[string]bool{ "Accept": true, "Accept-Encoding": true, "Accept-Language": true, @@ -40,6 +41,8 @@ var ( } USER_TOKEN_COOKIE_NAME = "_U" RAND_IP_COOKIE_NAME = "BingAI_Rand_IP" + PROXY_WEB_PREFIX_PATH = "/web/" + PROXY_WEB_PAGE_PATH = PROXY_WEB_PREFIX_PATH + "chat.html" ) func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy { @@ -62,7 +65,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy { req.Host = target.Host originalRefer := req.Referer() - if originalRefer != "" && !strings.Contains(originalRefer, "/web/chat.html") { + if originalRefer != "" && !strings.Contains(originalRefer, PROXY_WEB_PAGE_PATH) { req.Header.Set("Referer", strings.ReplaceAll(originalRefer, originalDomain, BING_URL.String())) } else { req.Header.Set("Referer", fmt.Sprintf("%s/search?q=Bing+AI", BING_URL.String())) @@ -88,7 +91,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy { } for hKey, _ := range req.Header { - if _, isExist := KEEP_HEADERS[hKey]; !isExist { + if _, ok := KEEP_REQ_HEADER_MAP[hKey]; !ok { req.Header.Del(hKey) } } diff --git a/main.go b/main.go index 5273d6b615..94bc9288da 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "adams549659584/go-proxy-bingai/api" "log" "net/http" + "os" "time" ) @@ -14,7 +15,11 @@ func main() { http.HandleFunc("/", api.Index) - addr := ":8080" + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + addr := ":" + port log.Println("Starting BingAI Proxy At " + addr) diff --git a/web/chat.html b/web/chat.html index 66ceb3850f..e8ec3cc05d 100644 --- a/web/chat.html +++ b/web/chat.html @@ -5,7 +5,7 @@ //]]>
-