Skip to content

Commit

Permalink
[Add] ✨ Support Mutil AUTH_KEY adams549659584#407 adams549659584#416
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed May 18, 2024
1 parent e9c79dd commit 1292d37
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions cloudflare/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CUSTOM_OPTIONS = {
APIKEY: '',
Go_Proxy_BingAI_BLANK_API_KEY: false,

Go_Proxy_BingAI_AUTH_KEY: '',
Go_Proxy_BingAI_AUTH_KEYS: '',

INFO: '',
NIGHTLY: false,
Expand Down Expand Up @@ -426,7 +426,7 @@ export default {
CUSTOM_OPTIONS.Go_Proxy_BingAI_BLANK_API_KEY = (env.Go_Proxy_BingAI_BLANK_API_KEY != '' && env.Go_Proxy_BingAI_BLANK_API_KEY != undefined &&env.Go_Proxy_BingAI_BLANK_API_KEY != null);
CUSTOM_OPTIONS.INFO = env.INFO || '';
CUSTOM_OPTIONS.NIGHTLY = (env.NIGHTLY != '' && env.NIGHTLY != undefined && env.NIGHTLY != null);
CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEY = env.Go_Proxy_BingAI_AUTH_KEY || '';
CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEYS = env.Go_Proxy_BingAI_AUTH_KEY.split(',') || '';

const currentUrl = new URL(request.url);
if (WEB_CONFIG.WORKER_URL == '') {
Expand All @@ -438,7 +438,7 @@ export default {
}
if (currentUrl.pathname.startsWith('/sysconf')) {
let isAuth = true;
if (CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEY.length !== 0) {
if (CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEYS.length !== 0) {
const cookieStr = request.headers.get('Cookie') || '';
let cookieObjects = {};
cookieStr.split(';').forEach(item => {
Expand All @@ -450,7 +450,7 @@ export default {
const val = arr.slice(1, arr.length+1).join('=').trim();
cookieObjects[key] = val;
})
if (cookieObjects[AUTH_KEY_COOKIE_NAME] !== CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEY) {
if (CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEYS.indexOf(cookieObjects[AUTH_KEY_COOKIE_NAME]) === -1) {
isAuth = false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions common/api/v1/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func init() {

func getCookie(reqCookie, convId, rid string) (cookie string, err error) {
cookie = reqCookie
if common.AUTH_KEY != "" {
cookie += "; " + common.AUTH_KEY_COOKIE_NAME + "=" + common.AUTH_KEY
if len(common.AUTH_KEYS) > 0 {
cookie += "; " + common.AUTH_KEY_COOKIE_NAME + "=" + common.AUTH_KEYS[0]
}
c := request.NewRequest()
res := c.SetUrl("http://localhost:"+common.PORT+"/chat?q=Bing+AI&showconv=1&FORM=hpcodx&ajaxhist=0&ajaxserp=0&cc=us").
Expand Down Expand Up @@ -65,8 +65,8 @@ func getCookie(reqCookie, convId, rid string) (cookie string, err error) {
if len(common.USER_TOKEN_LIST) == 0 {
cookie += "; _U=" + hex.NewHex(128)
}
if common.AUTH_KEY != "" {
cookie += "; " + common.AUTH_KEY_COOKIE_NAME + "=" + common.AUTH_KEY
if len(common.AUTH_KEYS) > 0 {
cookie += "; " + common.AUTH_KEY_COOKIE_NAME + "=" + common.AUTH_KEYS[0]
}
return cookie, nil
}
4 changes: 2 additions & 2 deletions common/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
USER_RwBf string
USER_MUID string
// 访问权限密钥,可选
AUTH_KEY string
AUTH_KEYS []string
AUTH_KEY_COOKIE_NAME = "BingAI_Auth_Key"

BypassServer string
Expand Down Expand Up @@ -56,7 +56,7 @@ func initEnv() {
// is debug
IS_DEBUG_MODE = os.Getenv("Go_Proxy_BingAI_Debug") != ""
// auth
AUTH_KEY = os.Getenv("Go_Proxy_BingAI_AUTH_KEY")
AUTH_KEYS = strings.Split(os.Getenv("Go_Proxy_BingAI_AUTH_KEY"), ",")
// KievRPSSecAuth Cookie
USER_KievRPSSecAuth = os.Getenv("USER_KievRPSSecAuth")
// MUID Cookie
Expand Down
4 changes: 2 additions & 2 deletions common/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func UnauthorizedResult(w http.ResponseWriter) error {

func CheckAuth(r *http.Request) bool {
isAuth := true
if len(common.AUTH_KEY) > 0 {
if len(common.AUTH_KEYS) > 0 {
ckAuthKey, _ := r.Cookie(common.AUTH_KEY_COOKIE_NAME)
isAuth = ckAuthKey != nil && len(ckAuthKey.Value) > 0 && common.AUTH_KEY == ckAuthKey.Value
isAuth = ckAuthKey != nil && len(ckAuthKey.Value) > 0 && common.IsInArray(common.AUTH_KEYS, ckAuthKey.Value)
}
return isAuth
}

0 comments on commit 1292d37

Please sign in to comment.