Skip to content

Commit

Permalink
feat: 增加 MUID
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Sep 7, 2023
1 parent 6bbb94c commit 2ca9846
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bingo",
"version": "0.6.1",
"version": "0.6.2",
"private": true,
"scripts": {
"dev": "cross-env DEBUG=bingo* next dev --hostname 0.0.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function Settings() {
const maxAge = 86400 * 30
encodeHeadersToCookie(headerValue).forEach(cookie => document.cookie = `${cookie}; Max-Age=${maxAge}; Path=/; SameSite=None; Secure`)
} else {
[...ChunkKeys, 'BING_COOKIE', 'BING_UA', 'BING_IP'].forEach(key => setCookie(key, ''))
[...ChunkKeys, 'BING_COOKIE', 'BING_UA', 'BING_IP', '_U'].forEach(key => setCookie(key, ''))
}
setCookie('IMAGE_ONLY', RegExp.$1 === 'cn' || imageOnly || !headerValue ? '1' : '0')

Expand Down
6 changes: 4 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { lookup } from 'dns'
// @ts-ignore
import randomip from 'random-ip'
import cidr from './cidr.json'
import { debug } from './isomorphic'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand Down Expand Up @@ -69,8 +70,8 @@ export function parseHeadersFromCurl(content: string) {
return headers
}


export const ChunkKeys = ['BING_HEADER', 'BING_HEADER1', 'BING_HEADER2']

export function encodeHeadersToCookie(content: string) {
const base64Content = btoa(content)
const contentChunks = base64Content.match(/.{1,4000}/g) || []
Expand Down Expand Up @@ -125,6 +126,7 @@ export function parseUA(ua?: string, default_ua = DEFAULT_UA) {
export function mockUser(cookies: Partial<{ [key: string]: string }>) {
const {
BING_UA = process.env.BING_UA,
MUID = process.env.MUID,
BING_IP = '',
_U = defaultUID,
} = cookies
Expand All @@ -136,7 +138,7 @@ export function mockUser(cookies: Partial<{ [key: string]: string }>) {
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'User-Agent': ua!,
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32',
cookie: `_U=${_U}`,
cookie: `_U=${_U}; MUID=${MUID || ''}`,
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { NextApiRequest, NextApiResponse } from 'next'
import { fetch, debug } from '@/lib/isomorphic'
import { createHeaders, randomIP, lookupPromise } from '@/lib/utils'
import { createHeaders, randomIP, extraHeadersFromCookie } from '@/lib/utils'
import { sleep } from '@/lib/bots/bing/utils'

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
Expand Down Expand Up @@ -32,19 +32,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (!json?.conversationSignature) {
continue
}
const cookies = [
headers.cookie,
`BING_IP=${headers['x-forwarded-for']}`,
]
const { cookie } = extraHeadersFromCookie(req.cookies) || {}
const cookies = cookie?.split(';') || []
cookies.push(`BING_IP=${headers['x-forwarded-for']}`)

res.setHeader('set-cookie', cookies.map(cookie => `${cookie}; Max-Age=${86400 * 30}; Path=/; SameSite=None; Secure`))
res.setHeader('set-cookie', cookies.map(cookie => `${cookie.trim()}; Max-Age=${86400 * 30}; Path=/; SameSite=None; Secure`))
debug('headers', headers)
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(JSON.stringify({
...json,
userIpAddress: endpoint && !endpoint.endsWith('.bing.com') ? await lookupPromise(endpoint.split('/')[0]) : headers['x-forwarded-for']
// userIpAddress: endpoint && !endpoint.endsWith('.bing.com') ? await lookupPromise(endpoint.split('/')[0]) : headers['x-forwarded-for']
}))
return
}
Expand Down

0 comments on commit 2ca9846

Please sign in to comment.