From 2e69b606f851664b83ab8d3bba178df9b199e0e7 Mon Sep 17 00:00:00 2001 From: adams549659584 <13760614423@163.com> Date: Mon, 8 May 2023 12:07:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E5=8A=A0=E4=B8=AA=E6=92=B0=E5=86=99=E5=90=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 3 +- README.md | 2 +- api/index.go | 2 +- api/web.go | 7 +- common/proxy.go | 9 +- main.go | 7 +- web/chat.html | 24 +- web/compose.html | 2665 +++++++++++++++++++++++++++++++++++++++++++ web/css/index.css | 24 +- web/img/compose.svg | 1 + web/img/github.svg | 1 + web/img/setting.svg | 1 + web/web.go | 49 +- 13 files changed, 2759 insertions(+), 36 deletions(-) create mode 100644 web/compose.html create mode 100644 web/img/compose.svg create mode 100644 web/img/github.svg create mode 100644 web/img/setting.svg 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 @@ //]]>
-