Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #68 from jianjianai/dev
Browse files Browse the repository at this point in the history
cloudflare 回来了!
  • Loading branch information
jianjianai authored May 25, 2023
2 parents d52b5de + a4de595 commit f7c5620
Showing 1 changed file with 22 additions and 72 deletions.
94 changes: 22 additions & 72 deletions cloudflareWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ async function handleRequest(request) {
}

if (path === '/sydney/ChatHub') { //魔法聊天
return await websocketHandler(request)
let h = {
"sec-fetch-site": "same-origin",
"referer": "https://www.bing.com/search?q=bingAI"
}
let randomAddress = url.searchParams.get("randomAddress");
if(randomAddress){
h["X-forwarded-for"] = randomAddress;
}
return goUrl(request,"https://sydney.bing.com/sydney/ChatHub",h);
}
if (path === "/turing/conversation/create") { //创建聊天
return goUrl(request, "https://www.bing.com/turing/conversation/create");
Expand Down Expand Up @@ -182,12 +190,21 @@ async function goUrl(request, url, addHeaders) {
}
//保留头部信息
let reqHeaders = request.headers;
let dropHeaders = ["user-agent", "accept", "accept-language"];
let dropHeaders = ["user-agent", "accept", "accept-language","Connection","Upgrade"];
for (let h of dropHeaders) {
if (reqHeaders.has(h)) {
fp.headers[h] = reqHeaders.get(h);
}
}

//客户端指定的随机地址
let randomAddress = reqHeaders.get("randomAddress");
if(!randomAddress){
randomAddress = "12.24.144.227";
}
//添加X-forwarded-for
fp.headers['X-forwarded-for'] = randomAddress;

if (addHeaders) {
//添加头部信息
for (let h in addHeaders) {
Expand Down Expand Up @@ -216,22 +233,10 @@ async function goUrl(request, url, addHeaders) {
fp.headers["cookie"] = reqHeaders.get('cookie');
}

//客户端指定的随机地址
let randomAddress = reqHeaders.get("randomAddress");
if(!randomAddress){
randomAddress = "12.24.144.227";
}
//添加X-forwarded-for
fp.headers['X-forwarded-for'] = randomAddress;

let res = await fetch(url, fp);
let headers = new Headers(res.headers);
headers.set("cookieID",cookieID);
return new Response(res.body,{
status:res.status,
statusText:res.statusText,
headers:headers
});
let newRes = new Response(res.body,res);
newRes.headers.set("cookieID",cookieID);
return newRes;
}

//获取用于返回的错误信息
Expand Down Expand Up @@ -261,58 +266,3 @@ function getRedirect(url) {
})
}




/**
* @param serverWebSocket {WebSocket}
* */
async function handleSession(serverWebSocket) {
let isAccept = false;
let bingws = new WebSocket('wss://sydney.bing.com/sydney/ChatHub');
serverWebSocket.addEventListener("message", event => {
bingws.send(event.data);
});
bingws.addEventListener("message", event => {
serverWebSocket.send(event.data)
});
bingws.addEventListener("open", event => {
isAccept = true;
serverWebSocket.accept();
})
bingws.addEventListener("close", event => {
serverWebSocket.close(event.code, event.reason);
})
bingws.addEventListener("error", event => {
if(!isAccept){
serverWebSocket.accept();
}
serverWebSocket.send(JSON.stringify({
type: 'error',
mess: "workers接到bing错误:" + event.message
}));
serverWebSocket.close();
});
serverWebSocket.addEventListener("error", event => {
serverWebSocket.close();
})
serverWebSocket.addEventListener("close",event => {
bingws.close(event.code, event.reason);
});
}

const websocketHandler = async request => {
const upgradeHeader = request.headers.get("Upgrade")
if (upgradeHeader !== "websocket") {
return new Response("Expected websocket", { status: 400 })
}

const [client, server] = Object.values(new WebSocketPair())
await handleSession(server)

return new Response(null, {
status: 101,
webSocket: client
})
}

0 comments on commit f7c5620

Please sign in to comment.