Skip to content

Commit

Permalink
[Fix] 🐛 Mutil AUTH_KEY Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed May 20, 2024
1 parent 1292d37 commit 7e37dfb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
30 changes: 16 additions & 14 deletions cloudflare/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export default {
CUSTOM_OPTIONS._U = env.Go_Proxy_BingAI_USER_TOKEN || '';
CUSTOM_OPTIONS.BYPASS_SERVER = env.BYPASS_SERVER || '';
CUSTOM_OPTIONS.APIKEY = env.APIKEY || '';
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.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_KEYS = env.Go_Proxy_BingAI_AUTH_KEY.split(',') || '';
Expand All @@ -439,19 +439,21 @@ export default {
if (currentUrl.pathname.startsWith('/sysconf')) {
let isAuth = true;
if (CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEYS.length !== 0) {
const cookieStr = request.headers.get('Cookie') || '';
let cookieObjects = {};
cookieStr.split(';').forEach(item => {
if (!item) {
return;
if (CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEYS[0] != '') {
const cookieStr = request.headers.get('Cookie') || '';
let cookieObjects = {};
cookieStr.split(';').forEach(item => {
if (!item) {
return;
}
const arr = item.split('=');
const key = arr[0].trim();
const val = arr.slice(1, arr.length + 1).join('=').trim();
cookieObjects[key] = val;
})
if (CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEYS.indexOf(cookieObjects[AUTH_KEY_COOKIE_NAME]) === -1) {
isAuth = false;
}
const arr = item.split('=');
const key = arr[0].trim();
const val = arr.slice(1, arr.length+1).join('=').trim();
cookieObjects[key] = val;
})
if (CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEYS.indexOf(cookieObjects[AUTH_KEY_COOKIE_NAME]) === -1) {
isAuth = false;
}
}
return Response.json({ code: 200, message: 'success', data: { isSysCK: false, isAuth: isAuth, info: CUSTOM_OPTIONS.INFO } })
Expand Down Expand Up @@ -558,7 +560,7 @@ export default {
}
const arr = item.split('=');
const key = arr[0].trim();
const val = arr.slice(1, arr.length+1).join('=').trim();
const val = arr.slice(1, arr.length + 1).join('=').trim();
cookieObjects[key] = val;
})
delete cookieObjects[RAND_IP_COOKIE_NAME];
Expand Down
4 changes: 3 additions & 1 deletion common/api/v1/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func getCookie(reqCookie, convId, rid string) (cookie string, err error) {
cookie += "; _U=" + hex.NewHex(128)
}
if len(common.AUTH_KEYS) > 0 {
cookie += "; " + common.AUTH_KEY_COOKIE_NAME + "=" + common.AUTH_KEYS[0]
if common.AUTH_KEYS[0] != "" {
cookie += "; " + common.AUTH_KEY_COOKIE_NAME + "=" + common.AUTH_KEYS[0]
}
}
return cookie, nil
}
6 changes: 4 additions & 2 deletions common/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func UnauthorizedResult(w http.ResponseWriter) error {
func CheckAuth(r *http.Request) bool {
isAuth := true
if len(common.AUTH_KEYS) > 0 {
ckAuthKey, _ := r.Cookie(common.AUTH_KEY_COOKIE_NAME)
isAuth = ckAuthKey != nil && len(ckAuthKey.Value) > 0 && common.IsInArray(common.AUTH_KEYS, ckAuthKey.Value)
if common.AUTH_KEYS[0] != "" {
ckAuthKey, _ := r.Cookie(common.AUTH_KEY_COOKIE_NAME)
isAuth = ckAuthKey != nil && len(ckAuthKey.Value) > 0 && common.IsInArray(common.AUTH_KEYS, ckAuthKey.Value)
}
}
return isAuth
}

0 comments on commit 7e37dfb

Please sign in to comment.