Skip to content

Commit

Permalink
[Add] ✨ Vercel BingAPI Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed Mar 7, 2024
1 parent 807aa59 commit d1a3473
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 58 deletions.
2 changes: 1 addition & 1 deletion api/bypass.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"adams549659584/go-proxy-bingai/common/helper"
"encoding/json"
"io"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion api/challenge.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common/helper"
"fmt"
"net/http"
)
Expand Down
2 changes: 1 addition & 1 deletion api/designer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"adams549659584/go-proxy-bingai/common/helper"
"net/http"
"strings"
)
Expand Down
2 changes: 1 addition & 1 deletion api/edgesvc.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

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

Expand Down
6 changes: 3 additions & 3 deletions api/index.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package api

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

func Index(w http.ResponseWriter, r *http.Request) {
wr := newResponseWriter(w)
wr := helper.NewResponseWriter(w)
if r.URL.Path == "/" {
http.Redirect(wr, r, common.PROXY_WEB_PAGE_PATH, http.StatusFound)
return
Expand All @@ -24,5 +24,5 @@ func Index(w http.ResponseWriter, r *http.Request) {
if ip == "" {
ip = strings.Split(r.RemoteAddr, ":")[0]
}
common.Logger.Debug("%s - %s %s - %d - %s", ip, r.Method, r.URL.Path, wr.statusCode, r.Header.Get("User-Agent"))
common.Logger.Debug("%s - %s %s - %d - %s", ip, r.Method, r.URL.Path, wr.StatusCode, r.Header.Get("User-Agent"))
}
2 changes: 1 addition & 1 deletion api/sydney.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

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

Expand Down
2 changes: 1 addition & 1 deletion api/sys-config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

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

Expand Down
39 changes: 39 additions & 0 deletions api/v1/vercel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { bingapiChat, bingapiModel, bingapiModels } from '../../cloudflare/bingapi.js'

export const config = {
runtime: 'edge',
supportsResponseStreaming: true,
};

export default function ChatHandler(request) {
const CUSTOM_OPTIONS = {
cookie: request.headers.get('Cookie') || '',
BYPASS_SERVER: 'https://bypass.zklcdc.xyz',
APIKEY: process.env.APIKEY,
}

const currentUrl = new URL(request.url);
if ((currentUrl.pathname.startsWith('/v1/models/')) || (currentUrl.pathname.startsWith('/api/v1/models/'))) {
return bingapiModel(request, CUSTOM_OPTIONS);
}
if ((currentUrl.pathname === '/v1/models') || (currentUrl.pathname === '/api/v1/models')) {
return bingapiModels(request, CUSTOM_OPTIONS);
}
if ((currentUrl.pathname === '/v1/chat/completions') || (currentUrl.pathname === '/api/v1/chat/completions')) {
if (request.method == 'OPTIONS') {
return Response.json({ code: 200, message: 'OPTIONS', data: null }, {
headers: {
"Allow": "POST, OPTIONS",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, Cookie",
}
});
}
if (request.method != 'POST') {
return Response.json({ code: 405, message: 'Method Not Allowed', data: null }, { status: 405 });
}
return bingapiChat(request, CUSTOM_OPTIONS);
}
return Response.json({ code: 404, message: 'API No Found', data: null }, { status: 404 });
}
2 changes: 1 addition & 1 deletion api/verify.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"adams549659584/go-proxy-bingai/common/helper"
"encoding/json"
"net/http"
"net/url"
Expand Down
2 changes: 1 addition & 1 deletion api/web.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"adams549659584/go-proxy-bingai/common/helper"
"adams549659584/go-proxy-bingai/web"
"net/http"
)
Expand Down
4 changes: 2 additions & 2 deletions cloudflare/bingapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ export async function bingapiChat(request, options) {
let newReq = new Request(options.BYPASS_SERVER, {
method: 'POST',
headers: {
cookie: options.Cookie,
cookie: options.cookie,
},
body: JSON.stringify({
cookies: options.Cookie,
cookies: options.cookie,
iframeid: 'local-gen-' + crypto.randomUUID(),
IG: IG,
T: await aesEncrypt(AUTHOR, IG),
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions api/middleware.go → common/helper/middleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package helper

import (
"adams549659584/go-proxy-bingai/common"
Expand All @@ -11,15 +11,15 @@ import (

type responseWriter struct {
http.ResponseWriter
statusCode int
StatusCode int
}

func newResponseWriter(w http.ResponseWriter) *responseWriter {
func NewResponseWriter(w http.ResponseWriter) *responseWriter {
return &responseWriter{w, http.StatusOK}
}

func (rw *responseWriter) WriteHeader(code int) {
rw.statusCode = code
rw.StatusCode = code
rw.ResponseWriter.WriteHeader(code)
}

Expand All @@ -44,7 +44,7 @@ func Middleware(next http.HandlerFunc) http.HandlerFunc {
w.WriteHeader(http.StatusUnavailableForLegalReasons)
return
}
wr := newResponseWriter(w)
wr := NewResponseWriter(w)
next(wr, r)
ip := r.Header.Get("X-Real-IP")
if ip == "" {
Expand All @@ -54,9 +54,9 @@ func Middleware(next http.HandlerFunc) http.HandlerFunc {
ip = strings.Split(r.RemoteAddr, ":")[0]
}
if strings.HasPrefix(r.URL.Path, "/web/") {
common.Logger.Debug("%s - %s %s - %d - %s", ip, r.Method, r.URL.Path, wr.statusCode, r.Header.Get("User-Agent"))
common.Logger.Debug("%s - %s %s - %d - %s", ip, r.Method, r.URL.Path, wr.StatusCode, r.Header.Get("User-Agent"))
} else {
common.Logger.Info("%s - %s %s - %d - %s", ip, r.Method, r.URL.Path, wr.statusCode, r.Header.Get("User-Agent"))
common.Logger.Info("%s - %s %s - %d - %s", ip, r.Method, r.URL.Path, wr.StatusCode, r.Header.Get("User-Agent"))
}
})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 19 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@ package main

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

func main() {
http.HandleFunc("/v1/chat/completions", api.Middleware(v1.ChatHandler))
http.HandleFunc("/v1/images/generations", api.Middleware(v1.ImageHandler))
http.HandleFunc("/v1/models/", api.Middleware(v1.ModelHandler))
http.HandleFunc("/v1/models", api.Middleware(v1.ModelsHandler))
http.HandleFunc("/api/v1/chat/completions", api.Middleware(v1.ChatHandler))
http.HandleFunc("/api/v1/images/generations", api.Middleware(v1.ImageHandler))
http.HandleFunc("/api/v1/models/", api.Middleware(v1.ModelHandler))
http.HandleFunc("/api/v1/models", api.Middleware(v1.ModelsHandler))
http.HandleFunc("/v1/chat/completions", helper.Middleware(v1.ChatHandler))
http.HandleFunc("/v1/images/generations", helper.Middleware(v1.ImageHandler))
http.HandleFunc("/v1/models/", helper.Middleware(v1.ModelHandler))
http.HandleFunc("/v1/models", helper.Middleware(v1.ModelsHandler))
http.HandleFunc("/api/v1/chat/completions", helper.Middleware(v1.ChatHandler))
http.HandleFunc("/api/v1/images/generations", helper.Middleware(v1.ImageHandler))
http.HandleFunc("/api/v1/models/", helper.Middleware(v1.ModelHandler))
http.HandleFunc("/api/v1/models", helper.Middleware(v1.ModelsHandler))

http.HandleFunc("/sysconf", api.Middleware(api.SysConf))
http.HandleFunc("/sysconf", helper.Middleware(api.SysConf))

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

http.HandleFunc("/designer/", api.Middleware(api.Designer))
http.HandleFunc("/designer/", helper.Middleware(api.Designer))

http.HandleFunc("/edgesvc/", api.Middleware(api.Edgesvc))
http.HandleFunc("/sydney/", api.Middleware(api.Sydney))
http.HandleFunc("/edgesvc/", helper.Middleware(api.Edgesvc))
http.HandleFunc("/sydney/", helper.Middleware(api.Sydney))

if common.IS_DEBUG_MODE {
http.HandleFunc("/web/", api.Middleware(web.DebugWebHandler))
http.HandleFunc("/web/", helper.Middleware(web.DebugWebHandler))
} else {
http.HandleFunc("/web/", api.Middleware(api.WebStatic))
http.HandleFunc("/web/", helper.Middleware(api.WebStatic))
}

http.HandleFunc("/", api.Index)
Expand Down
30 changes: 10 additions & 20 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"name": "go-proxy-bingai",
"version": 2,
"builds": [
{
"src": "/api/{index,web,sydney,edgesvc,designer,sys-config,bypass,challenge,verify}.go",
"use": "@vercel/go"
"functions": {
"api/*.go": {
"runtime": "@vercel/[email protected]"
},
{
"src": "/api/v1/{chat,image}.go",
"use": "@vercel/go"
"api/v1/*.js": {
"memory": 512
}
],
},
"routes": [
{
"src": "/sysconf",
Expand All @@ -29,20 +27,12 @@
"dest": "/api/verify.go"
},
{
"src": "/v1/chat/completions",
"dest": "/api/v1/chat.go"
},
{
"src": "/api/v1/chat/completions",
"dest": "/api/v1/chat.go"
},
{
"src": "/v1/images/generations",
"dest": "/api/v1/image.go"
"src": "/v1/.*",
"dest": "/api/v1/vercel.js"
},
{
"src": "/api/v1/images/generations",
"dest": "/api/v1/image.go"
"src": "/api/v1/.*",
"dest": "/api/v1/vercel.js"
},
{
"src": "/designer/.*",
Expand Down

0 comments on commit d1a3473

Please sign in to comment.