Skip to content

Commit

Permalink
[Fix] 🐛 Vercel Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed Jan 15, 2024
1 parent 5b44ef3 commit 8192f9a
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 154 deletions.
47 changes: 0 additions & 47 deletions api/bypass.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"io"
"net/http"
"strings"
"time"

"github.com/Harry-zklcdc/bing-lib/lib/hex"
Expand Down Expand Up @@ -71,52 +70,6 @@ func BypassHandler(w http.ResponseWriter, r *http.Request) {
w.Write(body)
}

const respHtml = `
<script type="text/javascript">
function verificationComplete(){
window.parent.postMessage("verificationComplete", "*");
}
window.onload = verificationComplete;
</script>
`

func ChallengeHandler(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
}
}
}

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))
}

func Bypass(bypassServer, cookie, iframeid string) (passResp PassResponseStruct, err error) {
passRequest := passRequestStruct{
Cookies: cookie,
Expand Down
54 changes: 54 additions & 0 deletions api/challenge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package api

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

const respHtml = `
<script type="text/javascript">
function verificationComplete(){
window.parent.postMessage("verificationComplete", "*");
}
window.onload = verificationComplete;
</script>
`

func ChallengeHandler(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
}
}
}

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))
}
106 changes: 2 additions & 104 deletions api/v1/openai.go → api/v1/chat.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package v1

import (
"adams549659584/go-proxy-bingai/api"
"adams549659584/go-proxy-bingai/common"
"encoding/json"
"io"
"math/rand"
"net/http"
"os"
"strings"
"time"

binglib "github.com/Harry-zklcdc/bing-lib"
"github.com/Harry-zklcdc/bing-lib/lib/hex"
"github.com/Harry-zklcdc/bing-lib/lib/request"
)

var (
apikey = os.Getenv("APIKEY")
globalChat = binglib.NewChat("").SetBingBaseUrl("http://localhost:" + common.PORT).SetSydneyBaseUrl("ws://localhost:" + common.PORT)

globalChat = binglib.NewChat("").SetBingBaseUrl("http://localhost:" + common.PORT).SetSydneyBaseUrl("ws://localhost:" + common.PORT)
globalImage = binglib.NewImage("").SetBingBaseUrl("http://localhost:" + common.PORT)
STOPFLAG = "stop"
)

var STOPFLAG = "stop"

func ChatHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
Expand Down Expand Up @@ -186,98 +179,3 @@ func ChatHandler(w http.ResponseWriter, r *http.Request) {
}
}
}

func ImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method Not Allowed"))
return
}

if apikey != "" {
if r.Header.Get("Authorization") != "Bearer "+apikey {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
}

image := globalImage.Clone()

cookie := r.Header.Get("Cookie")
if cookie == "" {
if len(common.USER_TOKEN_LIST) > 0 {
seed := time.Now().UnixNano()
rng := rand.New(rand.NewSource(seed))
cookie = common.USER_TOKEN_LIST[rng.Intn(len(common.USER_TOKEN_LIST))]
} else {
if common.BypassServer != "" {
t, _ := getCookie(cookie)
if t != "" {
cookie = t
}
}
}
}
image.SetCookies(cookie)

resqB, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

var resq imageRequest
json.Unmarshal(resqB, &resq)

imgs, _, err := image.Image(resq.Prompt)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

resp := imageResponse{
Created: time.Now().Unix(),
}
for _, img := range imgs {
resp.Data = append(resp.Data, imageData{
Url: img,
})
}

resData, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(resData)
}

func ModelsHandler(w http.ResponseWriter, r *http.Request) {

}

func getCookie(reqCookie 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").
SetHeader("User-Agent", common.User_Agent).
SetHeader("Cookie", cookie).Do()
headers := res.GetHeaders()
for k, v := range headers {
if strings.ToLower(k) == "set-cookie" {
for _, i := range v {
cookie += strings.Split(i, "; ")[0] + "; "
}
}
}
cookie = strings.TrimLeft(strings.Trim(cookie, "; "), "; ")
resp, err := api.Bypass(common.BypassServer, cookie, "local-gen-"+hex.NewUUID())
if err != nil {
return
}
return resp.Result.Cookies, nil
}
32 changes: 32 additions & 0 deletions api/v1/func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package v1

import (
"adams549659584/go-proxy-bingai/api"
"adams549659584/go-proxy-bingai/common"
"strings"

"github.com/Harry-zklcdc/bing-lib/lib/hex"
"github.com/Harry-zklcdc/bing-lib/lib/request"
)

func getCookie(reqCookie 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").
SetHeader("User-Agent", common.User_Agent).
SetHeader("Cookie", cookie).Do()
headers := res.GetHeaders()
for k, v := range headers {
if strings.ToLower(k) == "set-cookie" {
for _, i := range v {
cookie += strings.Split(i, "; ")[0] + "; "
}
}
}
cookie = strings.TrimLeft(strings.Trim(cookie, "; "), "; ")
resp, err := api.Bypass(common.BypassServer, cookie, "local-gen-"+hex.NewUUID())
if err != nil {
return
}
return resp.Result.Cookies, nil
}
85 changes: 85 additions & 0 deletions api/v1/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package v1

import (
"adams549659584/go-proxy-bingai/common"
"encoding/json"
"io"
"math/rand"
"net/http"
"time"

binglib "github.com/Harry-zklcdc/bing-lib"
)

var (
globalImage = binglib.NewImage("").SetBingBaseUrl("http://localhost:" + common.PORT)
)

func ImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method Not Allowed"))
return
}

if apikey != "" {
if r.Header.Get("Authorization") != "Bearer "+apikey {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
}

image := globalImage.Clone()

cookie := r.Header.Get("Cookie")
if cookie == "" {
if len(common.USER_TOKEN_LIST) > 0 {
seed := time.Now().UnixNano()
rng := rand.New(rand.NewSource(seed))
cookie = common.USER_TOKEN_LIST[rng.Intn(len(common.USER_TOKEN_LIST))]
} else {
if common.BypassServer != "" {
t, _ := getCookie(cookie)
if t != "" {
cookie = t
}
}
}
}
image.SetCookies(cookie)

resqB, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

var resq imageRequest
json.Unmarshal(resqB, &resq)

imgs, _, err := image.Image(resq.Prompt)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

resp := imageResponse{
Created: time.Now().Unix(),
}
for _, img := range imgs {
resp.Data = append(resp.Data, imageData{
Url: img,
})
}

resData, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(resData)
}
8 changes: 7 additions & 1 deletion api/v1/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package v1

import binglib "github.com/Harry-zklcdc/bing-lib"
import (
"os"

binglib "github.com/Harry-zklcdc/bing-lib"
)

var apikey = os.Getenv("APIKEY")

type chatRequest struct {
Messages []binglib.Message `json:"messages"`
Expand Down
7 changes: 7 additions & 0 deletions api/v1/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v1

import "net/http"

func ModelsHandler(w http.ResponseWriter, r *http.Request) {

}
Loading

0 comments on commit 8192f9a

Please sign in to comment.