feat: 简单加个撰写吧

This commit is contained in:
adams549659584 2023-05-08 12:07:55 +08:00
parent 49fb27e42b
commit 2e69b606f8
13 changed files with 2759 additions and 36 deletions

3
.vscode/launch.json vendored
View File

@ -11,7 +11,8 @@
"mode": "auto", "mode": "auto",
"program": "${workspaceFolder}", "program": "${workspaceFolder}",
"env": { "env": {
"Go-Proxy-BingAI-Debug": "true" "Go-Proxy-BingAI-Debug": "true",
"PORT": "8888"
} }
} }
] ]

View File

@ -55,7 +55,7 @@
- https://bing-vercel.vcanbb.top - https://bing-vercel.vcanbb.top
- https://go-proxy-bingai-vercel.vercel.app - https://go-proxy-bingai-git-master-adams549659584.vercel.app
### Render搭建 ### Render搭建

View File

@ -7,7 +7,7 @@ import (
func Index(w http.ResponseWriter, r *http.Request) { func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" { 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 { } else {
common.NewSingleHostReverseProxy(common.BING_URL).ServeHTTP(w, r) common.NewSingleHostReverseProxy(common.BING_URL).ServeHTTP(w, r)
} }

View File

@ -1,10 +1,15 @@
package api package api
import ( import (
"adams549659584/go-proxy-bingai/common"
"adams549659584/go-proxy-bingai/web" "adams549659584/go-proxy-bingai/web"
"net/http" "net/http"
) )
func WebStatic(w http.ResponseWriter, r *http.Request) { 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)
}
} }

View File

@ -19,7 +19,8 @@ var (
BING_CHAT_DOMAIN = "https://sydney.bing.com" BING_CHAT_DOMAIN = "https://sydney.bing.com"
BING_CHAT_URL, _ = url.Parse(BING_CHAT_DOMAIN + "/sydney/ChatHub") BING_CHAT_URL, _ = url.Parse(BING_CHAT_DOMAIN + "/sydney/ChatHub")
BING_URL, _ = url.Parse("https://www.bing.com") 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": true,
"Accept-Encoding": true, "Accept-Encoding": true,
"Accept-Language": true, "Accept-Language": true,
@ -40,6 +41,8 @@ var (
} }
USER_TOKEN_COOKIE_NAME = "_U" USER_TOKEN_COOKIE_NAME = "_U"
RAND_IP_COOKIE_NAME = "BingAI_Rand_IP" 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 { func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
@ -62,7 +65,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
req.Host = target.Host req.Host = target.Host
originalRefer := req.Referer() 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())) req.Header.Set("Referer", strings.ReplaceAll(originalRefer, originalDomain, BING_URL.String()))
} else { } else {
req.Header.Set("Referer", fmt.Sprintf("%s/search?q=Bing+AI", BING_URL.String())) 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 { for hKey, _ := range req.Header {
if _, isExist := KEEP_HEADERS[hKey]; !isExist { if _, ok := KEEP_REQ_HEADER_MAP[hKey]; !ok {
req.Header.Del(hKey) req.Header.Del(hKey)
} }
} }

View File

@ -4,6 +4,7 @@ import (
"adams549659584/go-proxy-bingai/api" "adams549659584/go-proxy-bingai/api"
"log" "log"
"net/http" "net/http"
"os"
"time" "time"
) )
@ -14,7 +15,11 @@ func main() {
http.HandleFunc("/", api.Index) http.HandleFunc("/", api.Index)
addr := ":8080" port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
addr := ":" + port
log.Println("Starting BingAI Proxy At " + addr) log.Println("Starting BingAI Proxy At " + addr)

File diff suppressed because one or more lines are too long

2665
web/compose.html Normal file

File diff suppressed because one or more lines are too long

View File

@ -31,6 +31,9 @@ html {
overflow-y: scroll overflow-y: scroll
} }
a:visited {
color: #111;
}
.chat-nav { .chat-nav {
position: fixed; position: fixed;
@ -43,7 +46,8 @@ html {
} }
.nav__title-github, .nav__title-github,
.nav__title-setting { .nav__title-setting,
.nav__title-compose {
display: inline-block; display: inline-block;
width: 32px; width: 32px;
height: 32px; height: 32px;
@ -57,7 +61,8 @@ html {
} }
.nav__title-github-icon, .nav__title-github-icon,
.nav__title-setting-icon { .nav__title-setting-icon,
.nav__title-compose-icon {
float: left; float: left;
width: 32px; width: 32px;
height: 32px; height: 32px;
@ -65,7 +70,8 @@ html {
} }
.nav__title-github-content, .nav__title-github-content,
.nav__title-setting-content { .nav__title-setting-content,
.nav__title-compose-content {
float: left; float: left;
margin-left: 5px; margin-left: 5px;
} }
@ -77,21 +83,17 @@ html {
} }
.nav__title-github, .nav__title-github,
.nav__title-setting { .nav__title-setting,
.nav__title-compose {
margin-bottom: 20px; margin-bottom: 20px;
} }
.nav__title-github:hover, .nav__title-github:hover,
.nav__title-setting:hover { .nav__title-setting:hover,
.nav__title-compose:hover {
width: 112px; 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;
}
} }

1
web/img/compose.svg Normal file
View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1683517490184" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5963" xmlns:xlink="http://www.w3.org/1999/xlink" width="32" height="32"><path d="M862.709333 116.042667a32 32 0 1 1 45.248 45.248L455.445333 613.813333a32 32 0 1 1-45.258666-45.258666L862.709333 116.053333zM853.333333 448a32 32 0 0 1 64 0v352c0 64.8-52.533333 117.333333-117.333333 117.333333H224c-64.8 0-117.333333-52.533333-117.333333-117.333333V224c0-64.8 52.533333-117.333333 117.333333-117.333333h341.333333a32 32 0 0 1 0 64H224a53.333333 53.333333 0 0 0-53.333333 53.333333v576a53.333333 53.333333 0 0 0 53.333333 53.333333h576a53.333333 53.333333 0 0 0 53.333333-53.333333V448z" fill="#000000" p-id="5964"></path></svg>

After

Width:  |  Height:  |  Size: 876 B

1
web/img/github.svg Normal file
View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1683517824116" class="icon" viewBox="0 0 1049 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1530" xmlns:xlink="http://www.w3.org/1999/xlink" width="32.78125" height="32"><path d="M524.979332 0C234.676191 0 0 234.676191 0 524.979332c0 232.068678 150.366597 428.501342 358.967656 498.035028 26.075132 5.215026 35.636014-11.299224 35.636014-25.205961 0-12.168395-0.869171-53.888607-0.869171-97.347161-146.020741 31.290159-176.441729-62.580318-176.441729-62.580318-23.467619-60.841976-58.234462-76.487055-58.234463-76.487055-47.804409-32.15933 3.476684-32.15933 3.476685-32.15933 53.019436 3.476684 80.83291 53.888607 80.83291 53.888607 46.935238 79.963739 122.553122 57.365291 152.97411 43.458554 4.345855-33.897672 18.252593-57.365291 33.028501-70.402857-116.468925-12.168395-239.022047-57.365291-239.022047-259.012982 0-57.365291 20.860106-104.300529 53.888607-140.805715-5.215026-13.037566-23.467619-66.926173 5.215027-139.067372 0 0 44.327725-13.906737 144.282399 53.888607 41.720212-11.299224 86.917108-17.383422 131.244833-17.383422s89.524621 6.084198 131.244833 17.383422C756.178839 203.386032 800.506564 217.29277 800.506564 217.29277c28.682646 72.1412 10.430053 126.029806 5.215026 139.067372 33.897672 36.505185 53.888607 83.440424 53.888607 140.805715 0 201.64769-122.553122 245.975415-239.891218 259.012982 19.121764 16.514251 35.636014 47.804409 35.636015 97.347161 0 70.402857-0.869171 126.898978-0.869172 144.282399 0 13.906737 9.560882 30.420988 35.636015 25.205961 208.601059-69.533686 358.967656-265.96635 358.967655-498.035028C1049.958663 234.676191 814.413301 0 524.979332 0z" fill="#191717" p-id="1531"></path><path d="M199.040177 753.571326c-0.869171 2.607513-5.215026 3.476684-8.691711 1.738342s-6.084198-5.215026-4.345855-7.82254c0.869171-2.607513 5.215026-3.476684 8.691711-1.738342s5.215026 5.215026 4.345855 7.82254z m-6.953369-4.345856M219.900283 777.038945c-2.607513 2.607513-7.82254 0.869171-10.430053-2.607514-3.476684-3.476684-4.345855-8.691711-1.738342-11.299224 2.607513-2.607513 6.953369-0.869171 10.430053 2.607514 3.476684 4.345855 4.345855 9.560882 1.738342 11.299224z m-5.215026-5.215027M240.760389 807.459932c-3.476684 2.607513-8.691711 0-11.299224-4.345855-3.476684-4.345855-3.476684-10.430053 0-12.168395 3.476684-2.607513 8.691711 0 11.299224 4.345855 3.476684 4.345855 3.476684 9.560882 0 12.168395z m0 0M269.443034 837.011749c-2.607513 3.476684-8.691711 2.607513-13.906737-1.738342-4.345855-4.345855-6.084198-10.430053-2.607513-13.037566 2.607513-3.476684 8.691711-2.607513 13.906737 1.738342 4.345855 3.476684 5.215026 9.560882 2.607513 13.037566z m0 0M308.555733 853.526c-0.869171 4.345855-6.953369 6.084198-13.037566 4.345855-6.084198-1.738342-9.560882-6.953369-8.691711-10.430053 0.869171-4.345855 6.953369-6.084198 13.037566-4.345855 6.084198 1.738342 9.560882 6.084198 8.691711 10.430053z m0 0M351.145116 857.002684c0 4.345855-5.215026 7.82254-11.299224 7.82254-6.084198 0-11.299224-3.476684-11.299224-7.82254s5.215026-7.82254 11.299224-7.82254c6.084198 0 11.299224 3.476684 11.299224 7.82254z m0 0M391.126986 850.049315c0.869171 4.345855-3.476684 8.691711-9.560882 9.560882-6.084198 0.869171-11.299224-1.738342-12.168395-6.084197-0.869171-4.345855 3.476684-8.691711 9.560881-9.560882 6.084198-0.869171 11.299224 1.738342 12.168396 6.084197z m0 0" fill="#191717" p-id="1532"></path></svg>

After

Width:  |  Height:  |  Size: 3.4 KiB

1
web/img/setting.svg Normal file
View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1683517165625" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4902" width="32" height="32" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M902.4 576c-25.6 6.4-51.2 12.8-83.2 12.8-6.4 32-19.2 57.6-32 83.2 19.2 25.6 32 44.8 51.2 70.4L742.4 832c-19.2-12.8-44.8-32-70.4-51.2-25.6 12.8-51.2 25.6-83.2 32-6.4 32-6.4 57.6-12.8 83.2L448 896c-6.4-25.6-12.8-51.2-12.8-83.2-32-6.4-57.6-19.2-83.2-32-25.6 19.2-44.8 32-70.4 51.2L192 742.4c12.8-19.2 32-44.8 51.2-70.4-12.8-25.6-25.6-51.2-32-83.2C172.8 588.8 147.2 582.4 121.6 576L121.6 448c25.6-6.4 51.2-12.8 83.2-12.8 6.4-32 19.2-57.6 32-83.2C217.6 326.4 204.8 300.8 192 281.6L281.6 192c19.2 12.8 44.8 32 70.4 51.2 25.6-12.8 51.2-25.6 83.2-32C435.2 172.8 441.6 147.2 448 121.6l128 0c6.4 25.6 12.8 51.2 12.8 83.2 32 6.4 57.6 19.2 83.2 32 25.6-19.2 44.8-32 70.4-51.2L832 281.6c-12.8 19.2-32 44.8-51.2 70.4 12.8 25.6 25.6 51.2 32 83.2 32 6.4 57.6 6.4 83.2 12.8L896 576zM915.2 390.4C896 390.4 883.2 384 864 384c-6.4-6.4-6.4-19.2-12.8-25.6 12.8-12.8 19.2-32 32-44.8C896 288 896 262.4 876.8 243.2l-89.6-89.6c-12.8-12.8-25.6-19.2-38.4-19.2-12.8 0-19.2 0-32 6.4-12.8 6.4-25.6 19.2-44.8 32-6.4-6.4-19.2-6.4-25.6-12.8 0-19.2-6.4-38.4-12.8-51.2C627.2 83.2 601.6 64 576 64L448 64C422.4 64 396.8 83.2 390.4 108.8 390.4 121.6 384 140.8 384 160 371.2 166.4 364.8 166.4 352 172.8 339.2 160 326.4 153.6 313.6 140.8 300.8 134.4 288 134.4 281.6 134.4c-12.8 0-32 6.4-38.4 19.2L147.2 243.2C128 262.4 128 288 140.8 313.6c6.4 12.8 19.2 25.6 32 44.8C166.4 364.8 166.4 371.2 160 384 140.8 384 121.6 390.4 108.8 390.4 83.2 396.8 64 422.4 64 448l0 128c0 25.6 19.2 51.2 44.8 57.6C121.6 633.6 140.8 640 160 640c6.4 6.4 6.4 19.2 12.8 25.6-12.8 12.8-19.2 32-32 44.8C128 736 128 761.6 147.2 780.8l89.6 89.6c12.8 12.8 25.6 19.2 38.4 19.2 12.8 0 19.2 0 32-6.4 12.8-6.4 25.6-19.2 44.8-32 6.4 6.4 19.2 6.4 25.6 12.8 0 19.2 6.4 38.4 12.8 51.2C396.8 940.8 422.4 960 448 960l128 0c25.6 0 51.2-19.2 57.6-44.8 6.4-19.2 6.4-32 12.8-51.2 6.4-6.4 19.2-6.4 25.6-12.8 12.8 12.8 32 19.2 44.8 32 6.4 6.4 19.2 6.4 32 6.4 12.8 0 32-6.4 38.4-19.2l89.6-89.6c19.2-19.2 19.2-51.2 6.4-70.4-6.4-12.8-19.2-25.6-32-44.8 6.4-6.4 6.4-19.2 12.8-25.6 19.2 0 38.4-6.4 51.2-6.4C940.8 627.2 960 601.6 960 576L960 448C960 422.4 940.8 396.8 915.2 390.4zM512 627.2C448 627.2 396.8 576 396.8 512 396.8 448 448 403.2 512 403.2 576 403.2 620.8 448 620.8 512 620.8 576 576 627.2 512 627.2zM512 345.6C422.4 345.6 345.6 422.4 345.6 512s76.8 166.4 166.4 166.4 166.4-76.8 166.4-166.4S601.6 345.6 512 345.6z" p-id="4903"></path></svg>

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

@ -2,16 +2,61 @@ package web
import ( import (
"embed" "embed"
"io/fs"
"net/http" "net/http"
"os" "os"
"path/filepath"
) )
//go:embed * //go:embed *
var webFS embed.FS 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 { func GetWebFS() http.FileSystem {
debugMode := os.Getenv("Go-Proxy-BingAI-Debug") if IS_DEBUG_MODE {
if debugMode != "" {
return http.Dir("web") return http.Dir("web")
} else { } else {
return http.FS(webFS) return http.FS(webFS)