Skip to content

Commit

Permalink
chore: 细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Oct 9, 2023
1 parent 6b78214 commit 2d096d8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 32 deletions.
6 changes: 1 addition & 5 deletions src/app/globals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,8 @@ body {
}

.scroll-button {
bottom: 4rem;
bottom: 8rem;
z-index: 50;

@media (max-width: 767px) {
bottom: 8rem;
}
}

.header-title {
Expand Down
33 changes: 14 additions & 19 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,34 +163,29 @@ export function mockUser(cookies: Partial<{ [key: string]: string }>) {
'User-Agent': ua!,
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32',
'referer': 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch',
cookie: `_U=${_U || defaultUID}; MUID=${MUID || muid()}`,
cookie: `_U=${_U || defaultUID}; MUID=${MUID || randomString(32)}`,
}
}

export function createHeaders(cookies: Partial<{ [key: string]: string }>, type?: 'image') {
export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMock?: boolean) {
let {
BING_HEADER = process.env.BING_HEADER,
BING_IP = process.env.BING_IP || '',
IMAGE_ONLY = process.env.IMAGE_ONLY ?? '1',
} = cookies || {}
const imageOnly = /^(1|true|yes)$/.test(String(IMAGE_ONLY))
if (BING_HEADER) {
if (
(imageOnly && type === 'image')
|| !imageOnly
) {
const headers = extraHeadersFromCookie({
BING_HEADER,
...cookies,
})
// headers['x-forwarded-for'] = BING_IP || randomIP()
headers['user-agent'] = parseUA(headers['user-agent'])
headers['referer'] = 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch'
headers['x-ms-useragent'] = headers['x-ms-useragent'] || 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32'
return headers
}
useMock = useMock ?? /^(1|true|yes)$/i.test(String(IMAGE_ONLY))
if (!BING_HEADER || useMock) {
return mockUser(cookies)
}
return mockUser(cookies)
const headers = extraHeadersFromCookie({
BING_HEADER,
...cookies,
})
headers['x-forwarded-for'] = BING_IP || randomIP()
headers['user-agent'] = parseUA(headers['user-agent'])
headers['referer'] = 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch'
headers['x-ms-useragent'] = headers['x-ms-useragent'] || 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32'
return headers
}

export class WatchDog {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/blob.jpg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
{
method: 'GET',
headers: {
...createHeaders(req.cookies, 'image'),
...createHeaders(req.cookies, false),
Referer: 'https://www.bing.com/search?q=Bing+AI&showconv=1',
'Sec-Fetch-Dest': 'iframe',
},
Expand Down
9 changes: 5 additions & 4 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

debug('headers', headers)
const cookies = [`BING_IP=${headers['x-forwarded-for']}`]
res.setHeader('set-cookie', cookies.map(cookie => `${cookie.trim()}; Max-Age=${86400 * 30}; Path=/;`))

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

res.end(JSON.stringify(json))
return
}
await sleep(2000)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
})
}
try {
const headers = createHeaders(req.cookies, 'image')
const headers = createHeaders(req.cookies, false)

debug('headers', headers)
const response = await createImage(String(prompt), String(id), {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/kblob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const config = {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { knowledgeRequest, imageBase64 } = req.body as KBlobRequest
const headers = createHeaders(req.cookies, 'image')
const headers = createHeaders(req.cookies, false)

const formData = new FormData()
formData.append('knowledgeRequest', JSON.stringify(knowledgeRequest))
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/sydney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { WS_ENDPOINT = 'sydney.bing.com' } = process.env

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const conversationContext = req.body
const headers = createHeaders(req.cookies, req.cookies['BING_HEADER1'] ? undefined : 'image')
const headers = createHeaders(req.cookies, Boolean(req.cookies['BING_HEADER1']))
const id = headers['x-forwarded-for']
// headers['x-forwarded-for'] = conversationContext?.userIpAddress || headers['x-forwarded-for']

Expand Down

0 comments on commit 2d096d8

Please sign in to comment.