Skip to content

Commit

Permalink
chore: 使用固定 UA 、可配置默认用户 Cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
adams549659584 committed May 16, 2023
1 parent 62656b7 commit 8ca6c9d
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"Go_Proxy_BingAI_SOCKS_URL": "192.168.0.88:1070",
// "Go_Proxy_BingAI_SOCKS_USER": "xxx",
// "Go_Proxy_BingAI_SOCKS_PWD": "xxx",
// "Go_Proxy_BingAI_USER_TOKEN_1": "xxx",
// "Go_Proxy_BingAI_USER_TOKEN_2": "xxx"
}
}
]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Go_Proxy_BingAI_SOCKS_URL=192.168.0.88:1070
# Socks 账号、密码 可选
Go_Proxy_BingAI_SOCKS_USER=xxx
Go_Proxy_BingAI_SOCKS_PWD=xxx
# 默认用户 Cookie 设置,可选,固定前缀 Go_Proxy_BingAI_USER_TOKEN 可设置多个,未登录用户将随机使用
Go_Proxy_BingAI_USER_TOKEN_1=xxx
Go_Proxy_BingAI_USER_TOKEN_2=xxx
Go_Proxy_BingAI_USER_TOKEN_3=xxx ...
```

## 部署
Expand Down
81 changes: 73 additions & 8 deletions common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/andybalholm/brotli"
"golang.org/x/net/proxy"
Expand Down Expand Up @@ -41,19 +43,36 @@ var (
"https://cn.bing.com",
"https://www.bing.com",
}
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 + "index.html"
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 + "index.html"
USER_TOKEN_ENV_NAME_PREFIX = "Go_Proxy_BingAI_USER_TOKEN"
USER_TOKEN_LIST []string
RAND_COOKIE_INDEX_NAME = "BingAI_Rand_CK"
)

func init() {
initUserToken()
}

func initUserToken() {
for _, env := range os.Environ() {
if strings.HasPrefix(env, USER_TOKEN_ENV_NAME_PREFIX) {
parts := strings.SplitN(env, "=", 2)
USER_TOKEN_LIST = append(USER_TOKEN_LIST, parts[1])
}
}
}

func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
originalScheme := "http"
httpsSchemeName := "https"
var originalHost string
var originalPath string
var originalDomain string
var randIP string
var resCKRandIndex string
director := func(req *http.Request) {
if req.URL.Scheme == httpsSchemeName || req.Header.Get("X-Forwarded-Proto") == httpsSchemeName {
originalScheme = httpsSchemeName
Expand Down Expand Up @@ -83,14 +102,23 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
}
req.Header.Set("X-Forwarded-For", randIP)

// 未登录用户,ua 包含 iPhone Mobile 居然秒创建会话id,应该搞了手机优先策略, Android 不行
// 未登录用户
ckUserToken, _ := req.Cookie(USER_TOKEN_COOKIE_NAME)
if ckUserToken == nil || ckUserToken.Value == "" {
ua := req.UserAgent()
if !strings.Contains(ua, "iPhone") && !strings.Contains(ua, "Mobile") {
req.Header.Set("User-Agent", "iPhone Mobile "+ua)
randCKIndex, randCkVal := getRandCookie(req)
if randCkVal != "" {
resCKRandIndex = strconv.Itoa(randCKIndex)
req.AddCookie(&http.Cookie{
Name: USER_TOKEN_COOKIE_NAME,
Value: randCkVal,
})
}
// ua := req.UserAgent()
// if !strings.Contains(ua, "iPhone") || !strings.Contains(ua, "Mobile") {
// req.Header.Set("User-Agent", "iPhone Mobile "+ua)
// }
}
req.Header.Set("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 15_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.7 Mobile/15E148 Safari/605.1.15 BingSapphire/1.0.410427012")

for hKey, _ := range req.Header {
if _, ok := KEEP_REQ_HEADER_MAP[hKey]; !ok {
Expand Down Expand Up @@ -135,6 +163,16 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
}
res.Header.Set("Set-Cookie", ckRandIP.String())

// 设置服务器 cookie 对应索引
if resCKRandIndex != "" {
ckRandIndex := &http.Cookie{
Name: RAND_COOKIE_INDEX_NAME,
Value: resCKRandIndex,
Path: "/",
}
res.Header.Set("Set-Cookie", ckRandIndex.String())
}

// 删除 CSP
res.Header.Del("Content-Security-Policy-Report-Only")
res.Header.Del("Report-To")
Expand Down Expand Up @@ -202,6 +240,33 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
return reverseProxy
}

// return cookie index and cookie
func getRandCookie(req *http.Request) (int, string) {
utLen := len(USER_TOKEN_LIST)
if utLen == 0 {
return 0, ""
}
if utLen == 1 {
return 0, USER_TOKEN_LIST[0]
}

ckRandIndex, _ := req.Cookie(RAND_COOKIE_INDEX_NAME)
if ckRandIndex != nil && ckRandIndex.Value != "" {
tmpIndex, err := strconv.Atoi(ckRandIndex.Value)
if err != nil {
log.Println("ckRandIndex err :", err)
return 0, ""
}
if tmpIndex < utLen {
return tmpIndex, USER_TOKEN_LIST[tmpIndex]
}
}
seed := time.Now().UnixNano()
rng := rand.New(rand.NewSource(seed))
randIndex := rng.Intn(len(USER_TOKEN_LIST))
return randIndex, USER_TOKEN_LIST[randIndex]
}

func replaceResBody(originalBody string, originalScheme string, originalHost string) string {
modifiedBodyStr := originalBody
originalDomain := fmt.Sprintf("%s://%s", originalScheme, originalHost)
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "go-proxy-bingai",
"version": "1.6.4",
"version": "1.6.5",
"private": true,
"scripts": {
"dev": "vite",
Expand Down
38 changes: 19 additions & 19 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/src/components/ChatNav/ChatNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const clearCache = async () => {
<NInput size="large" v-model:value="userToken" type="text" placeholder="用户 Cookie ,仅需要 _U 的值" />
<template #action>
<NButton size="large" @click="isShowSetTokenModal = false">取消</NButton>
<NButton size="large" type="info" @click="saveUserToken">保存</NButton>
<NButton ghost size="large" type="info" @click="saveUserToken">保存</NButton>
</template>
</NModal>
<NModal v-model:show="isShowClearCacheModal" preset="dialog" :show-icon="false">
Expand All @@ -144,7 +144,7 @@ const clearCache = async () => {
</template>
<template #action>
<NButton size="large" @click="isShowClearCacheModal = false">取消</NButton>
<NButton size="large" type="warning" @click="resetCache">确定</NButton>
<NButton ghost size="large" type="error" @click="resetCache">确定</NButton>
</template>
</NModal>
</template>

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions web/assets/index-aec03a9e.js → web/assets/index-c6f02c8b.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<script src="/web/js/bing/chat/global.js"></script>
<script src="/web/js/bing/chat/amd.js"></script>
<script src="/web/js/bing/chat/config.js"></script>
<script type="module" crossorigin src="/web/assets/index-fb0759db.js"></script>
<script type="module" crossorigin src="/web/assets/index-2db47a63.js"></script>
<link rel="stylesheet" href="/web/assets/index-2128d00b.css">
<link rel="manifest" href="/web/manifest.webmanifest"><script id="vite-plugin-pwa:register-sw" src="/web/registerSW.js"></script></head>

Expand Down
2 changes: 1 addition & 1 deletion web/sw.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 8ca6c9d

@vercel
Copy link

@vercel vercel bot commented on 8ca6c9d May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.