Skip to content

Commit 840022a

Browse files
committed
[Fix] 🐛 Bypass Repeat when Token haven
1 parent e4c3ca4 commit 840022a

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

api/v1/chat.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func ChatHandler(w http.ResponseWriter, r *http.Request) {
7272
var resq chatRequest
7373
json.Unmarshal(resqB, &resq)
7474

75-
if !isInArray(chatMODELS, resq.Model) {
75+
if !common.IsInArray(chatMODELS, resq.Model) {
7676
w.WriteHeader(http.StatusBadRequest)
7777
w.Write([]byte("Model Not Found"))
7878
return
@@ -197,12 +197,3 @@ func ChatHandler(w http.ResponseWriter, r *http.Request) {
197197
globalChat.SetCookies(chat.GetCookies())
198198
}
199199
}
200-
201-
func isInArray(arr []string, str string) bool {
202-
for _, v := range arr {
203-
if v == str {
204-
return true
205-
}
206-
}
207-
return false
208-
}

api/v1/model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"adams549659584/go-proxy-bingai/common"
45
"encoding/json"
56
"net/http"
67
"strings"
@@ -25,7 +26,7 @@ func ModelHandler(w http.ResponseWriter, r *http.Request) {
2526
return
2627
}
2728

28-
if modelId != "dall-e-3" && !isInArray(chatMODELS, modelId) {
29+
if modelId != "dall-e-3" && !common.IsInArray(chatMODELS, modelId) {
2930
w.WriteHeader(http.StatusNotFound)
3031
w.Write([]byte("Not Found"))
3132
return

api/verify.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"adams549659584/go-proxy-bingai/api/helper"
55
"adams549659584/go-proxy-bingai/common"
66
"encoding/json"
7-
"fmt"
87
"net/http"
98
"net/url"
109
"strings"
@@ -14,6 +13,8 @@ import (
1413
binglib "github.com/Harry-zklcdc/bing-lib"
1514
)
1615

16+
var removeCookieName = []string{common.USER_TOKEN_COOKIE_NAME, common.USER_KievRPSSecAuth_COOKIE_NAME, common.USER_RwBf_COOKIE_NAME, common.PASS_SERVER_COOKIE_NAME, common.RAND_COOKIE_INDEX_NAME}
17+
1718
func VerifyHandler(w http.ResponseWriter, r *http.Request) {
1819
if !helper.CheckAuth(r) {
1920
helper.UnauthorizedResult(w)
@@ -30,7 +31,6 @@ func VerifyHandler(w http.ResponseWriter, r *http.Request) {
3031
T, _ := url.QueryUnescape(r.URL.Query().Get("T"))
3132
token, err := aes.Decrypt(T, IG)
3233
if err != nil {
33-
fmt.Println(err)
3434
helper.ErrorResult(w, http.StatusInternalServerError, "Server Error")
3535
return
3636
}
@@ -39,21 +39,27 @@ func VerifyHandler(w http.ResponseWriter, r *http.Request) {
3939
return
4040
}
4141

42-
reqCookies := strings.Split(r.Header.Get("Cookie"), "; ")
4342
bypassServer := common.BypassServer
44-
for _, cookie := range reqCookies {
45-
if strings.HasPrefix(cookie, "BingAI_Pass_Server") {
46-
tmp := strings.ReplaceAll(cookie, "BingAI_Pass_Server=", "")
47-
if tmp != "" {
48-
bypassServer = tmp
49-
}
43+
44+
header := http.Header{}
45+
header.Add("Cookie", r.Header.Get("Cookie"))
46+
req := &http.Request{
47+
Header: header,
48+
}
49+
if cookie, err := req.Cookie(common.PASS_SERVER_COOKIE_NAME); err == nil {
50+
bypassServer = cookie.Value
51+
}
52+
reqCookies := []string{}
53+
for _, cookie := range req.Cookies() {
54+
if !common.IsInArray(removeCookieName, cookie.Name) {
55+
reqCookies = append(reqCookies, cookie.String())
5056
}
5157
}
5258

5359
iframeid, _ := url.QueryUnescape(queryRaw.Get("iframeid"))
5460
convId, _ := url.QueryUnescape(queryRaw.Get("convId"))
5561
rid, _ := url.QueryUnescape(queryRaw.Get("rid"))
56-
resp, status, err := binglib.Bypass(bypassServer, r.Header.Get("Cookie"), iframeid, IG, convId, rid, T)
62+
resp, status, err := binglib.Bypass(bypassServer, strings.Join(reqCookies, "; "), iframeid, IG, convId, rid, T)
5763
if err != nil {
5864
helper.ErrorResult(w, http.StatusInternalServerError, err.Error())
5965
return
@@ -70,7 +76,9 @@ func VerifyHandler(w http.ResponseWriter, r *http.Request) {
7076

7177
cookies := strings.Split(resp.Result.Cookies, "; ")
7278
for _, cookie := range cookies {
73-
w.Header().Add("Set-Cookie", cookie+"; path=/")
79+
if !common.IsInArray(removeCookieName, strings.Split(cookie, "=")[0]) {
80+
w.Header().Add("Set-Cookie", cookie+"; path=/")
81+
}
7482
}
7583

7684
helper.CommonResult(w, http.StatusOK, "ok", resp)

common/func.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package common
2+
3+
func IsInArray(arr []string, str string) bool {
4+
for _, v := range arr {
5+
if v == str {
6+
return true
7+
}
8+
}
9+
return false
10+
}

common/proxy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ var (
6868
USER_RwBf_COOKIE_NAME = "_RwBf"
6969
RAND_COOKIE_INDEX_NAME = "BingAI_Rand_CK"
7070
RAND_IP_COOKIE_NAME = "BingAI_Rand_IP"
71+
PASS_SERVER_COOKIE_NAME = "BingAI_Pass_Server"
7172
PROXY_WEB_PREFIX_PATH = "/web/"
7273
PROXY_WEB_PAGE_PATH = PROXY_WEB_PREFIX_PATH + "index.html"
7374

0 commit comments

Comments
 (0)