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 @@ //]]> - Bing AI + BingAI - 聊天 @@ -14,7 +14,8 @@ + + + BingAI - 撰写 + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
    +
  1. +
    + +
    +
    +
    +
    + + +
    +
    +
    +
    +
  2. +
  3. +
    +
  4. +
+
+ + +
+ + + + \ No newline at end of file diff --git a/web/css/index.css b/web/css/index.css index 98a71009f5..21bb539f29 100644 --- a/web/css/index.css +++ b/web/css/index.css @@ -31,6 +31,9 @@ html { overflow-y: scroll } +a:visited { + color: #111; +} .chat-nav { position: fixed; @@ -43,7 +46,8 @@ html { } .nav__title-github, -.nav__title-setting { +.nav__title-setting, +.nav__title-compose { display: inline-block; width: 32px; height: 32px; @@ -57,7 +61,8 @@ html { } .nav__title-github-icon, -.nav__title-setting-icon { +.nav__title-setting-icon, +.nav__title-compose-icon { float: left; width: 32px; height: 32px; @@ -65,7 +70,8 @@ html { } .nav__title-github-content, -.nav__title-setting-content { +.nav__title-setting-content, +.nav__title-compose-content { float: left; margin-left: 5px; } @@ -77,21 +83,17 @@ html { } .nav__title-github, - .nav__title-setting { + .nav__title-setting, + .nav__title-compose { margin-bottom: 20px; } .nav__title-github:hover, - .nav__title-setting:hover { + .nav__title-setting:hover, + .nav__title-compose:hover { width: 112px; - background-color: #212529; - color: #fff; } - .nav__title-github:hover .nav__title-github-icon, - .nav__title-setting:hover .nav__title-setting-icon { - background-color: #fff; - } } diff --git a/web/img/compose.svg b/web/img/compose.svg new file mode 100644 index 0000000000..ec1f6d2fcb --- /dev/null +++ b/web/img/compose.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/web/img/github.svg b/web/img/github.svg new file mode 100644 index 0000000000..cee52b9a4c --- /dev/null +++ b/web/img/github.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/web/img/setting.svg b/web/img/setting.svg new file mode 100644 index 0000000000..2e48a90cb5 --- /dev/null +++ b/web/img/setting.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/web/web.go b/web/web.go index c4b40d6bfb..299a7ee2ea 100644 --- a/web/web.go +++ b/web/web.go @@ -2,16 +2,61 @@ package web import ( "embed" + "io/fs" "net/http" "os" + "path/filepath" ) //go:embed * var webFS embed.FS +var IS_DEBUG_MODE bool + +var WEB_PATH_MAP = make(map[string]bool) + +func init() { + IS_DEBUG_MODE = os.Getenv("Go-Proxy-BingAI-Debug") != "" + + var err error + if IS_DEBUG_MODE { + err = initWebPathMapByDir() + } else { + err = initWebPathMapByFS() + } + if err != nil { + panic(err) + } +} + +func initWebPathMapByDir() error { + err := filepath.WalkDir("web", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if !d.IsDir() { + WEB_PATH_MAP["/"+path] = true + } + return nil + }) + return err +} + +func initWebPathMapByFS() error { + err := fs.WalkDir(webFS, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if !d.IsDir() { + WEB_PATH_MAP["/web/"+path] = true + } + return nil + }) + return err +} + func GetWebFS() http.FileSystem { - debugMode := os.Getenv("Go-Proxy-BingAI-Debug") - if debugMode != "" { + if IS_DEBUG_MODE { return http.Dir("web") } else { return http.FS(webFS)