Skip to content

Commit

Permalink
[Add] 🍒 Cookie MUID Setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed Aug 27, 2023
1 parent 99e822e commit 0f8011a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
4 changes: 4 additions & 0 deletions common/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
// USer Cookie
USER_KievRPSSecAuth string
USER_RwBf string
USER_MUID string
// 访问权限密钥,可选
AUTH_KEY string
AUTH_KEY_COOKIE_NAME = "BingAI_Auth_Key"
Expand All @@ -31,6 +32,9 @@ func initEnv() {
AUTH_KEY = os.Getenv("Go_Proxy_BingAI_AUTH_KEY")
// KievRPSSecAuth Cookie
USER_KievRPSSecAuth = os.Getenv("USER_KievRPSSecAuth")
// MUID Cookie
USER_MUID = os.Getenv("USER_MUID")
// _RwBf Cookie
USER_RwBf = os.Getenv("USER_RwBf")
}

Expand Down
22 changes: 20 additions & 2 deletions common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
USER_TOKEN_COOKIE_NAME = "_U"
USER_KievRPSSecAuth_COOKIE_NAME = "KievRPSSecAuth"
USER_RwBf_COOKIE_NAME = "_RwBf"
User_MUID_COOKIE_NAME = "MUID"
RAND_COOKIE_INDEX_NAME = "BingAI_Rand_CK"
RAND_IP_COOKIE_NAME = "BingAI_Rand_IP"
PROXY_WEB_PREFIX_PATH = "/web/"
Expand Down Expand Up @@ -100,6 +101,17 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
}
req.Header.Set("X-Forwarded-For", randIP)

ckUserMUID, _ := req.Cookie(User_MUID_COOKIE_NAME)
if ckUserMUID == nil || ckUserMUID.Value == "" {
if USER_MUID != "" {
// 添加 MUID Cookie
req.AddCookie(&http.Cookie{
Name: User_MUID_COOKIE_NAME,
Value: USER_MUID,
})
}
}

ckUserKievRPSSecAuth, _ := req.Cookie(USER_KievRPSSecAuth_COOKIE_NAME)
if ckUserKievRPSSecAuth == nil || ckUserKievRPSSecAuth.Value == "" {
if USER_KievRPSSecAuth != "" {
Expand Down Expand Up @@ -160,6 +172,12 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
}
//改写返回信息
modifyFunc := func(res *http.Response) error {
cookies := res.Cookies()
res.Header.Set("Set-Cookie", "")
for _, cookie := range cookies {
values := strings.Split(cookie.String(), ";")
res.Header.Add("Set-Cookie", values[0]+"; "+values[1])
}
contentType := res.Header.Get("Content-Type")
if strings.Contains(contentType, "text/javascript") {
contentEncoding := res.Header.Get("Content-Encoding")
Expand Down Expand Up @@ -191,7 +209,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
Value: resCKRandIndex,
Path: "/",
}
res.Header.Set("Set-Cookie", ckRandIndex.String())
res.Header.Add("Set-Cookie", ckRandIndex.String())
}

// 删除 CSP
Expand All @@ -218,7 +236,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
Value: randIP,
Path: "/",
}
res.Header.Set("Set-Cookie", ckRandIP.String())
res.Header.Add("Set-Cookie", ckRandIP.String())

// 跨域
// if IS_DEBUG_MODE {
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/components/ChatNav/ChatNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const isShowSetAboutModal = ref(false);
const userToken = ref('');
const userKievRPSSecAuth = ref('');
const userRwBf = ref('');
const userMUID = ref('');
const message = useMessage();
const promptStore = usePromptStore();
const { isShowPromptSotre } = storeToRefs(promptStore);
Expand Down Expand Up @@ -138,6 +139,7 @@ const handleSelect = (key: string) => {
userToken.value = userStore.getUserToken();
userKievRPSSecAuth.value = userStore.getUserKievRPSSecAuth();
userRwBf.value = userStore.getUserRwBf();
userMUID.value = userStore.getUserMUID();
history.value = historyEnable.value;
cookiesEnable.value = fullCookiesEnable.value;
if (cookiesEnable.value) { cookies.value = cookiesStr.value; }
Expand Down Expand Up @@ -195,6 +197,11 @@ const saveSetting = () => {
} else {
userStore.saveUserRwBf(userRwBf.value);
}
if (!userMUID.value) {
message.warning('请先填入用户 MUID Cookie');
} else {
userStore.saveUserMUID(userMUID.value);
}
}
fullCookiesEnable.value = cookiesEnable.value;
historyEnable.value = history.value;
Expand Down Expand Up @@ -257,6 +264,9 @@ const saveSetting = () => {
<NFormItem v-show="!cookiesEnable" path="token" label="_RwBf">
<NInput size="large" v-model:value="userRwBf" type="text" placeholder="用户 Cookie ,仅需要 _RwBf 的值" />
</NFormItem>
<NFormItem v-show="!cookiesEnable" path="token" label="MUID">
<NInput size="large" v-model:value="userMUID" type="text" placeholder="用户 Cookie ,仅需要 MUID 的值" />
</NFormItem>
<NFormItem v-show="cookiesEnable" path="token" label="Cookies">
<NInput size="large" v-model:value="cookies" type="text" placeholder="完整用户 Cookie" />
</NFormItem>
Expand Down
17 changes: 15 additions & 2 deletions frontend/src/stores/modules/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ export const useUserStore = defineStore(
return userCookieVal;
};

const checkUserToken = () => {
cookies.set(userMUIDCookieName, '3AC75B6BED5B6C3B03384913EC756D93', 365 * 24 * 60, '/')
const checkUserToken = async () => {
await fetch('/search?q=Bing+AI&showconv=1&FORM=hpcodx&ajaxhist=0&ajaxserp=0&cc=us', {
credentials: 'include',
})
if (historyEnable.value) {
CIB.vm.sidePanel.isVisibleDesktop = true;
document.querySelector('cib-serp')?.setAttribute('alignment', 'left');
Expand Down Expand Up @@ -136,6 +138,15 @@ export const useUserStore = defineStore(
cookies.set(userRwBfCookieName, token, 7 * 24 * 60, '/');
};

const getUserMUID = () => {
const userCookieVal = cookies.get(userMUIDCookieName) || '';
return userCookieVal;
};

const saveUserMUID = (token: string) => {
cookies.set(userMUIDCookieName, token, 7 * 24 * 60, '/');
};

const resetCache = async () => {
const keys = document.cookie.split(";");
if (keys) {
Expand Down Expand Up @@ -169,6 +180,8 @@ export const useUserStore = defineStore(
saveUserKievRPSSecAuth,
getUserRwBf,
saveUserRwBf,
getUserMUID,
saveUserMUID,
saveCookies,
cookiesStr,
historyEnable,
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/views/chat/components/Chat/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const initSysConfig = async () => {
isShowUnauthorizedModal.value = true;
return;
}
afterAuth(res.data);
await afterAuth(res.data);
}
break;
default:
Expand All @@ -121,9 +121,9 @@ const initSysConfig = async () => {
}
};
const afterAuth = (data: SysConfig) => {
const afterAuth = async (data: SysConfig) => {
if (!data.isSysCK) {
userStore.checkUserToken();
await userStore.checkUserToken();
}
initChatService();
};
Expand All @@ -143,6 +143,12 @@ const hackStyle = () => {
const conversationEle = serpEle?.shadowRoot?.querySelector('cib-conversation') as HTMLElement;
// todo 反馈暂时无法使用,先移除
const welcomeEle = conversationEle?.shadowRoot?.querySelector('cib-welcome-container');
const loginTip = welcomeEle?.shadowRoot?.querySelectorAll("div[class='muid-upsell']");
if (loginTip?.length) {
loginTip.forEach((ele) => {
ele.remove();
});
}
welcomeEle?.shadowRoot?.querySelector('.preview-container')?.remove();
serpEle?.shadowRoot?.querySelector('cib-serp-feedback')?.remove();
if (isMobile()) {
Expand Down

0 comments on commit 0f8011a

Please sign in to comment.