Skip to content

Commit

Permalink
[Fix] 🐛 Redirect URL Error #182
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed Feb 2, 2024
1 parent 7646d89 commit 07ace09
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
15 changes: 15 additions & 0 deletions api/edgesvc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

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

func Edgesvc(w http.ResponseWriter, r *http.Request) {
if !helper.CheckAuth(r) {
helper.UnauthorizedResult(w)
return
}
common.NewSingleHostReverseProxy(common.EDGE_SVC_URL).ServeHTTP(w, r)
}
9 changes: 6 additions & 3 deletions cloudflare/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const WEB_CONFIG = {

const SYDNEY_ORIGIN = 'https://sydney.bing.com';
const BING_ORIGIN = 'https://www.bing.com';
const EDGE_ORIGIN = 'https://edgeservices.bing.com';
const DESIGNER_ORIGIN = 'https://designer.microsoft.com';
const KEEP_REQ_HEADERS = [
'accept',
Expand Down Expand Up @@ -144,8 +145,9 @@ const rewriteBody = async (res) => {
}
if (decodedContent) {
// @ts-ignore
body = decodedContent.replaceAll("www.bing.com", WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", ""));
body = body.replaceAll("designer.microsoft.com", WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "")+'/designer');
body = decodedContent.replaceAll(BING_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", ""));
body = body.replaceAll(EDGE_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", ""));
body = body.replaceAll(DESIGNER_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "")+'/designer');
}
}
}
Expand Down Expand Up @@ -301,9 +303,10 @@ export default {
let targetUrl;
if (currentUrl.pathname.includes('/sydney')) {
targetUrl = new URL(SYDNEY_ORIGIN + currentUrl.pathname + currentUrl.search);
} else if (currentUrl.pathname.includes('/edgesvc')) {
targetUrl = new URL(EDGE_ORIGIN + currentUrl.pathname + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer/')) {
targetUrl = new URL(DESIGNER_ORIGIN + currentUrl.pathname.replaceAll('/designer/', '/') + currentUrl.search);
console.log(targetUrl)
} else{
targetUrl = new URL(BING_ORIGIN + currentUrl.pathname + currentUrl.search);
}
Expand Down
5 changes: 5 additions & 0 deletions common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
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, "/edgesvc/") {
req.Header.Set("Referer", fmt.Sprintf("%s/search?q=Bing+AI", BING_URL.String()))
req.Header.Set("Origin", EDGE_SVC_URL.String())
} else if strings.Contains(originalPath, "/designer/") {
req.URL.Path = strings.ReplaceAll(req.URL.Path, "/designer/", "/")
req.Header.Set("Referer", fmt.Sprintf("%s/search?q=Bing+AI", BING_URL.String()))
Expand Down Expand Up @@ -321,12 +324,14 @@ func replaceResBody(originalBody string, originalScheme string, originalHost str
if originalScheme == "https" {
if strings.Contains(modifiedBodyStr, BING_URL.Host) {
modifiedBodyStr = strings.ReplaceAll(modifiedBodyStr, BING_URL.Host, originalHost)
modifiedBodyStr = strings.ReplaceAll(modifiedBodyStr, EDGE_SVC_URL.Host, originalHost)
modifiedBodyStr = strings.ReplaceAll(modifiedBodyStr, DISIGNER_URL.Host, originalHost+"/designer")
}
} else {
originalDomain := fmt.Sprintf("%s://%s", originalScheme, originalHost)
if strings.Contains(modifiedBodyStr, BING_URL.String()) {
modifiedBodyStr = strings.ReplaceAll(modifiedBodyStr, BING_URL.String(), originalDomain)
modifiedBodyStr = strings.ReplaceAll(modifiedBodyStr, EDGE_SVC_URL.Host, originalDomain)
modifiedBodyStr = strings.ReplaceAll(modifiedBodyStr, DISIGNER_URL.String(), originalDomain+"/designer")
}
}
Expand Down
8 changes: 0 additions & 8 deletions frontend/src/views/chat/components/Chat/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ const initChatService = () => {
}
chatStore.checkAllSydneyConfig();
}
CIB.config.captcha.baseUrl = location.origin;
CIB.config.bing.baseUrl = location.origin;
CIB.config.bing.signIn.baseUrl = location.origin;
CIB.config.answers.baseUrl = location.origin;
CIB.config.answers.secondTurnScreenshotBaseUrl = location.origin;
CIB.config.contentCreator.baseUrl = location.origin;
CIB.config.visualSearch.baseUrl = location.origin;
CIB.config.suggestionsv2.baseUrl = location.origin;
};
const initSysConfig = async () => {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
http.HandleFunc("/turing/captcha/challenge", api.ChallengeHandler)
http.HandleFunc("/challenge/verify", api.VerifyHandler)

http.HandleFunc("/edgesvc/", api.Edgesvc)
http.HandleFunc("/designer/", api.Designer)
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,designer,sys-config,bypass,challenge,verify}.go",
"src": "/api/{index,web,sydney,designer,edgesvc,sys-config,bypass,challenge,verify}.go",
"use": "@vercel/go"
},
{
Expand Down Expand Up @@ -44,6 +44,10 @@
"src": "/api/v1/images/generations",
"dest": "/api/v1/image.go"
},
{
"src": "/edgesvc/.*",
"dest": "/api/edgesvc.go"
},
{
"src": "/designer/.*",
"dest": "/api/designer.go"
Expand Down

0 comments on commit 07ace09

Please sign in to comment.