Skip to content

Commit

Permalink
feat: ✨ 简单加个撰写吧
Browse files Browse the repository at this point in the history
  • Loading branch information
adams549659584 committed May 8, 2023
1 parent 49fb27e commit 2e69b60
Show file tree
Hide file tree
Showing 13 changed files with 2,759 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"mode": "auto",
"program": "${workspaceFolder}",
"env": {
"Go-Proxy-BingAI-Debug": "true"
"Go-Proxy-BingAI-Debug": "true",
"PORT": "8888"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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搭建

Expand Down
2 changes: 1 addition & 1 deletion api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion api/web.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 6 additions & 3 deletions common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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()))
Expand All @@ -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)
}
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"adams549659584/go-proxy-bingai/api"
"log"
"net/http"
"os"
"time"
)

Expand All @@ -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)

Expand Down
24 changes: 9 additions & 15 deletions web/chat.html

Large diffs are not rendered by default.

2,665 changes: 2,665 additions & 0 deletions web/compose.html

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions web/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ html {
overflow-y: scroll
}

a:visited {
color: #111;
}

.chat-nav {
position: fixed;
Expand All @@ -43,7 +46,8 @@ html {
}

.nav__title-github,
.nav__title-setting {
.nav__title-setting,
.nav__title-compose {
display: inline-block;
width: 32px;
height: 32px;
Expand All @@ -57,15 +61,17 @@ html {
}

.nav__title-github-icon,
.nav__title-setting-icon {
.nav__title-setting-icon,
.nav__title-compose-icon {
float: left;
width: 32px;
height: 32px;
border-radius: 32px;
}

.nav__title-github-content,
.nav__title-setting-content {
.nav__title-setting-content,
.nav__title-compose-content {
float: left;
margin-left: 5px;
}
Expand All @@ -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;
}

}

Expand Down
1 change: 1 addition & 0 deletions web/img/compose.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions web/img/github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions web/img/setting.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 47 additions & 2 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2e69b60

Please sign in to comment.