Skip to content

Commit

Permalink
[Fix] 🐛 Bypass Repeat when Token haven
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed Jan 25, 2024
1 parent 19f17f3 commit 70e096a
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 39 deletions.
11 changes: 9 additions & 2 deletions api/bypass.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import (
)

type passRequestStruct struct {
IG string `json:"IG,omitempty"`
Cookies string `json:"cookies"`
Iframeid string `json:"iframeid,omitempty"`
ConvId string `json:"convId,omitempty"`
RId string `json:"rid,omitempty"`
}

type requestStruct struct {
Url string `json:"url"`
IG string `json:"IG,omitempty"`
}

type PassResponseStruct struct {
Expand Down Expand Up @@ -61,7 +65,7 @@ func BypassHandler(w http.ResponseWriter, r *http.Request) {
request.Url = common.BypassServer
}

resp, err := Bypass(request.Url, r.Header.Get("Cookie"), "local-gen-"+hex.NewUUID())
resp, err := Bypass(request.Url, r.Header.Get("Cookie"), "local-gen-"+hex.NewUUID(), request.IG, "", "")
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, err.Error(), nil)
return
Expand All @@ -70,10 +74,13 @@ func BypassHandler(w http.ResponseWriter, r *http.Request) {
w.Write(body)
}

func Bypass(bypassServer, cookie, iframeid string) (passResp PassResponseStruct, err error) {
func Bypass(bypassServer, cookie, iframeid, IG, convId, rid string) (passResp PassResponseStruct, err error) {
passRequest := passRequestStruct{
Cookies: cookie,
Iframeid: iframeid,
IG: IG,
ConvId: convId,
RId: rid,
}
passResq, err := json.Marshal(passRequest)
if err != nil {
Expand Down
52 changes: 22 additions & 30 deletions api/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@ package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"fmt"
"net/http"
"strings"
)

const respHtml = `
const respChallengeHtml = `
<script type="text/javascript">
function verificationComplete(){
window.parent.postMessage("verificationComplete", "*");
async function ChallengeComplete(){
let IG = window.parent._G.IG,
convId = window.parent.CIB.manager.conversation.id,
rid = window.parent.CIB.manager.conversation.messages[0].requestId,
iframeid = '%s';
await fetch('/challenge/verify?IG='+encodeURI(IG)+'&iframeid='+encodeURI(iframeid)+'&convId='+encodeURI(convId)+'&rid='+encodeURI(rid), {
credentials: 'include',
}).then((res) => {
if (res.ok) {
window.parent.postMessage("verificationComplete", "*");
} else {
window.parent.postMessage("verificationFailed", "*");
}
}).cache(() => {
window.parent.postMessage("verificationFailed", "*");
});
}
window.onload = verificationComplete;
window.onload = ChallengeComplete;
</script>
`

Expand All @@ -27,28 +41,6 @@ func ChallengeHandler(w http.ResponseWriter, r *http.Request) {
return
}

reqCookies := strings.Split(r.Header.Get("Cookie"), "; ")
bypassServer := common.BypassServer
for _, cookie := range reqCookies {
if strings.HasPrefix(cookie, "BingAI_Pass_Server") {
tmp := strings.ReplaceAll(cookie, "BingAI_Pass_Server=", "")
if tmp != "" {
bypassServer = tmp
}
}
}

resp, err := Bypass(bypassServer, r.Header.Get("Cookie"), "")
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, err.Error(), nil)
return
}

cookies := strings.Split(resp.Result.Cookies, "; ")
for _, cookie := range cookies {
w.Header().Add("Set-Cookie", cookie+"; path=/")
}

// helper.CommonResult(w, http.StatusOK, "ok", resp)
w.Write([]byte(respHtml))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(fmt.Sprintf(respChallengeHtml, r.URL.Query().Get("iframeid"))))
}
4 changes: 2 additions & 2 deletions api/v1/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func ChatHandler(w http.ResponseWriter, r *http.Request) {

if tmp == "User needs to solve CAPTCHA to continue." && common.BypassServer != "" {
go func(cookie string) {
t, _ := getCookie(cookie)
t, _ := getCookie(cookie, chat.GetChatHub().GetConversationId(), hex.NewUUID())
if t != "" {
globalChat.SetCookies(t)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func ChatHandler(w http.ResponseWriter, r *http.Request) {

if text == "User needs to solve CAPTCHA to continue." && common.BypassServer != "" {
go func(cookie string) {
t, _ := getCookie(cookie)
t, _ := getCookie(cookie, chat.GetChatHub().GetConversationId(), hex.NewUUID())
if t != "" {
globalChat.SetCookies(t)
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/Harry-zklcdc/bing-lib/lib/request"
)

func getCookie(reqCookie string) (cookie string, err error) {
func getCookie(reqCookie, convId, rid string) (cookie string, err error) {
cookie = reqCookie
c := request.NewRequest()
res := c.SetUrl(common.BingBaseUrl+"/search?q=Bing+AI&showconv=1&FORM=hpcodx&ajaxhist=0&ajaxserp=0&cc=us").
Expand All @@ -24,7 +24,7 @@ func getCookie(reqCookie string) (cookie string, err error) {
}
}
cookie = strings.TrimLeft(strings.Trim(cookie, "; "), "; ")
resp, err := api.Bypass(common.BypassServer, cookie, "local-gen-"+hex.NewUUID())
resp, err := api.Bypass(common.BypassServer, cookie, "local-gen-"+hex.NewUUID(), strings.ToUpper(hex.NewHex(32)), convId, rid)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ImageHandler(w http.ResponseWriter, r *http.Request) {
cookie = common.USER_TOKEN_LIST[rng.Intn(len(common.USER_TOKEN_LIST))]
} else {
if common.BypassServer != "" {
t, _ := getCookie(cookie)
t, _ := getCookie(cookie, "", "")
if t != "" {
cookie = t
}
Expand Down
50 changes: 50 additions & 0 deletions api/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"net/http"
"net/url"
"strings"
)

func VerifyHandler(w http.ResponseWriter, r *http.Request) {
if !helper.CheckAuth(r) {
helper.UnauthorizedResult(w)
return
}

if r.Method != "GET" {
helper.CommonResult(w, http.StatusMethodNotAllowed, "Method Not Allowed", nil)
return
}

reqCookies := strings.Split(r.Header.Get("Cookie"), "; ")
bypassServer := common.BypassServer
for _, cookie := range reqCookies {
if strings.HasPrefix(cookie, "BingAI_Pass_Server") {
tmp := strings.ReplaceAll(cookie, "BingAI_Pass_Server=", "")
if tmp != "" {
bypassServer = tmp
}
}
}

queryRaw := r.URL.Query()
iframeid, _ := url.QueryUnescape(queryRaw.Get("iframeid"))
IG, _ := url.QueryUnescape(queryRaw.Get("IG"))
convId, _ := url.QueryUnescape(queryRaw.Get("convId"))
rid, _ := url.QueryUnescape(queryRaw.Get("rid"))
resp, err := Bypass(bypassServer, r.Header.Get("Cookie"), iframeid, IG, convId, rid)
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, err.Error(), nil)
return
}

cookies := strings.Split(resp.Result.Cookies, "; ")
for _, cookie := range cookies {
w.Header().Add("Set-Cookie", cookie+"; path=/")
}

helper.CommonResult(w, http.StatusOK, "ok", resp)
}
2 changes: 1 addition & 1 deletion frontend/public/js/bing/chat/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ _G = {
SUIH: randomString(22),
adc: 'b_ad',
// logsb 启用 sendBeacon 推送日志,并在 sendBeacon 阻止
EF: { cookss: 1, bmcov: 1, crossdomainfix: 1, bmasynctrigger: 1, bmasynctrigger3: 1, newtabsloppyclick: 1, chevroncheckmousemove: 1, logsb: 1 },
EF: { cookss: 1, bmcov: 1, crossdomainfix: 1, bmasynctrigger: 1, bmasynctrigger3: 1, getslctspt: 1, newtabsloppyclick: 1, chevroncheckmousemove: 1, sharepreview: 1, sharenewlayout: 1, cdxsugchip2: 1 },
gpUrl: '/fd/ls/GLinkPing.aspx?',
};
_G.lsUrl = '/fd/ls/l?IG=' + _G.IG;
Expand Down
1 change: 1 addition & 0 deletions frontend/types/bing/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ interface BingConversation {
conversationType: string;
hashedSignature: string;
id: string;
convId: string;
isExpired: boolean;
messages: TextMessageModel[];
state: string;
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {

http.HandleFunc("/pass", api.BypassHandler)
http.HandleFunc("/turing/captcha/challenge", api.ChallengeHandler)
http.HandleFunc("/challenge/verify", api.VerifyHandler)

http.HandleFunc("/sydney/", api.Sydney)

Expand Down
6 changes: 5 additions & 1 deletion vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": 2,
"builds": [
{
"src": "/api/{index,web,sydney,sys-config,bypass,challenge}.go",
"src": "/api/{index,web,sydney,sys-config,bypass,challenge,verify}.go",
"use": "@vercel/go"
},
{
Expand All @@ -24,6 +24,10 @@
"src": "/turing/captcha/challenge",
"dest": "/api/challenge.go"
},
{
"src": "/challenge/verify",
"dest": "/api/verify.go"
},
{
"src": "/v1/chat/completions",
"dest": "/api/v1/chat.go"
Expand Down

0 comments on commit 70e096a

Please sign in to comment.