Skip to content

Commit

Permalink
[Fix] 🐛 Vercel Deploy Chinese Q&A Error #409
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed May 8, 2024
1 parent 9ed548c commit 9be538d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 52 deletions.
15 changes: 15 additions & 0 deletions cloudflare/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,21 @@ export default {
return pass(request, cookies);
}

const cookieStr = cookies;
let cookieObjects = {};
cookieStr.split(';').forEach(item => {
if (!item) {
return;
}
const arr = item.split('=');
const key = arr[0].trim();
const val = arr.slice(1, arr.length+1).join('=').trim();
cookieObjects[key] = val;
})
delete cookieObjects['BingAI_Rand_IP'];

cookies = Object.keys(cookieObjects).map(key => key + '=' + cookieObjects[key]).join('; ');

newHeaders.set('Cookie', cookies);
const oldUA = request.headers.get('user-agent') || '';
const isMobile = oldUA.includes('Mobile') || oldUA.includes('Android');
Expand Down
83 changes: 31 additions & 52 deletions common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ var (
"https://cn.bing.com",
"https://www.bing.com",
}
USER_TOKEN_COOKIE_NAME = "_U"
USER_KievRPSSecAuth_COOKIE_NAME = "KievRPSSecAuth"
User_MUID_COOKIE_NAME = "MUID"
USER_RwBf_COOKIE_NAME = "_RwBf"
CLOUDFLARE_CHALLENGE_TOKEN_COOKIE_NAME = "cct"
RAND_COOKIE_INDEX_NAME = "BingAI_Rand_CK"
RAND_IP_COOKIE_NAME = "BingAI_Rand_IP"
PASS_SERVER_COOKIE_NAME = "BingAI_Pass_Server"
PROXY_WEB_PREFIX_PATH = "/web/"
PROXY_WEB_PAGE_PATH = PROXY_WEB_PREFIX_PATH + "index.html"
USER_TOKEN_COOKIE_NAME = "_U"
USER_KievRPSSecAuth_COOKIE_NAME = "KievRPSSecAuth"
User_MUID_COOKIE_NAME = "MUID"
USER_RwBf_COOKIE_NAME = "_RwBf"
RAND_COOKIE_INDEX_NAME = "BingAI_Rand_CK"
RAND_IP_COOKIE_NAME = "BingAI_Rand_IP"
PASS_SERVER_COOKIE_NAME = "BingAI_Pass_Server"
PROXY_WEB_PREFIX_PATH = "/web/"
PROXY_WEB_PAGE_PATH = PROXY_WEB_PREFIX_PATH + "index.html"

DEBUG_PROXY_WEB, _ = url.Parse("http://localhost:4000")
)
Expand All @@ -98,26 +97,20 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
req.URL.Host = target.Host
req.Host = target.Host

cct, _ := req.Cookie(CLOUDFLARE_CHALLENGE_TOKEN_COOKIE_NAME)
ckRandIP, _ := req.Cookie(RAND_IP_COOKIE_NAME) // 同一会话尽量保持相同的随机IP
ckUserMUID, _ := req.Cookie(User_MUID_COOKIE_NAME)
ckUserKievRPSSecAuth, _ := req.Cookie(USER_KievRPSSecAuth_COOKIE_NAME)
ckUserRwBf, _ := req.Cookie(USER_RwBf_COOKIE_NAME)
ckUserToken, _ := req.Cookie(USER_TOKEN_COOKIE_NAME) // 未登录用户

if strings.Contains(req.Referer(), "web/compose.html") {
req.Header.Set("Referer", fmt.Sprintf("%s/edgesvc/compose", EDGE_SVC_URL.String()))
req.Header.Set("Origin", EDGE_SVC_URL.String())
} else if strings.Contains(originalPath, "/sydney/") {
req.Header.Set("Referer", fmt.Sprintf("%s/chat?q=Microsoft+Copilot", BING_URL.String()))
req.Header.Set("Referer", fmt.Sprintf("%s/chat?q=Bing+AI", BING_URL.String()))
req.Header.Set("Origin", BING_URL.String())
req.Header.Set("Host", BING_SYDNEY_URL.Host)
req.Header.Del("Cookie")
} else {
req.Header.Set("Referer", fmt.Sprintf("%s/chat?q=Microsoft+Copilot", BING_URL.String()))
req.Header.Set("Referer", fmt.Sprintf("%s/chat?q=Bing+AI", BING_URL.String()))
req.Header.Set("Origin", target.String())
}

// 同一会话尽量保持相同的随机IP
ckRandIP, _ := req.Cookie(RAND_IP_COOKIE_NAME)
if ckRandIP != nil && ckRandIP.Value != "" {
randIP = ckRandIP.Value
}
Expand All @@ -126,6 +119,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
}
req.Header.Set("X-Forwarded-For", randIP)

ckUserMUID, _ := req.Cookie(User_MUID_COOKIE_NAME)
if (ckUserMUID == nil || ckUserMUID.Value == "") && USER_MUID != "" {
// 添加 MUID Cookie
req.AddCookie(&http.Cookie{
Expand All @@ -134,6 +128,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
})
}

ckUserKievRPSSecAuth, _ := req.Cookie(USER_KievRPSSecAuth_COOKIE_NAME)
if (ckUserKievRPSSecAuth == nil || ckUserKievRPSSecAuth.Value == "") && USER_KievRPSSecAuth != "" {
// 添加 KievRPSSecAuth Cookie
req.AddCookie(&http.Cookie{
Expand All @@ -142,6 +137,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
})
}

ckUserRwBf, _ := req.Cookie(USER_RwBf_COOKIE_NAME)
if (ckUserRwBf == nil || ckUserRwBf.Value == "") && USER_RwBf != "" {
// 添加 RwBf Cookie
req.AddCookie(&http.Cookie{
Expand All @@ -150,6 +146,8 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
})
}

// 未登录用户
ckUserToken, _ := req.Cookie(USER_TOKEN_COOKIE_NAME)
if ckUserToken == nil || ckUserToken.Value == "" {
randCKIndex, randCkVal := getRandCookie(req)
if randCkVal != "" {
Expand All @@ -165,39 +163,22 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
// }
}

if strings.Contains(originalPath, "/sydney/") {
if ckUserMUID != nil && ckUserMUID.Value != "" {
req.AddCookie(&http.Cookie{
Name: User_MUID_COOKIE_NAME,
Value: ckUserMUID.Value,
})
}
if ckUserKievRPSSecAuth != nil && ckUserKievRPSSecAuth.Value != "" {
req.AddCookie(&http.Cookie{
Name: USER_KievRPSSecAuth_COOKIE_NAME,
Value: ckUserKievRPSSecAuth.Value,
})
}
if ckUserRwBf != nil && ckUserRwBf.Value != "" {
req.AddCookie(&http.Cookie{
Name: USER_RwBf_COOKIE_NAME,
Value: ckUserRwBf.Value,
})
}
if ckUserToken != nil && ckUserToken.Value != "" {
req.AddCookie(&http.Cookie{
Name: USER_TOKEN_COOKIE_NAME,
Value: ckUserToken.Value,
})
}
if cct != nil && cct.Value != "" {
req.AddCookie(&http.Cookie{
Name: CLOUDFLARE_CHALLENGE_TOKEN_COOKIE_NAME,
Value: cct.Value,
})
cookies := req.Cookies()
for i, cookie := range cookies {
// 删除 `BingAI_Rand_IP` Cookie, 以使用多语种问答
if cookie.Name == RAND_IP_COOKIE_NAME {
// 删除切片中的元素
cookies = append(cookies[:i], cookies[i+1:]...)
break
}
}

// 重新设置 Cookie 头
req.Header.Del("Cookie")
for _, cookie := range cookies {
req.AddCookie(cookie)
}

ua := req.UserAgent()
isMobile := strings.Contains(ua, "Mobile") || strings.Contains(ua, "Android")

Expand All @@ -214,8 +195,6 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
}
}

// req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")

// reqHeaderByte, _ := json.Marshal(req.Header)
// log.Println("剩余请求头 : ", string(reqHeaderByte))
}
Expand Down

0 comments on commit 9be538d

Please sign in to comment.