Skip to content

Commit

Permalink
fix: 修复cookie在http协议下设置不生效的问题 weaigc#65
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Sep 11, 2023
1 parent f5e355f commit ecd32b3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function Settings() {
}
setImageOnly(checked)
} else {
toast.error('请先配置用户信息')
setImageOnly(checked)
}
}, [curlValue])

Expand Down Expand Up @@ -78,7 +78,10 @@ export function Settings() {
<Input
value={curlValue}
placeholder="在此填写用户信息,格式: curl 'https://www.bing.com/turing/captcha/challenge' ..."
onChange={e => setCurlValue(e.target.value)}
onChange={e => {
setCurlValue(e.target.value)
setImageOnly(!Boolean(e.target.value))
}}
/>
<div className="flex gap-2">
<Switch
Expand Down Expand Up @@ -126,8 +129,7 @@ export function Settings() {
toast.error('用户信息格式不正确')
return
}
const maxAge = 86400 * 30
encodeHeadersToCookie(headerValue).forEach(cookie => document.cookie = `${cookie}; Max-Age=${maxAge}; Path=/; SameSite=None; Secure`)
encodeHeadersToCookie(headerValue).forEach(cookie => setCookie(cookie))
} else {
[...ChunkKeys, 'BING_COOKIE', 'BING_UA', 'BING_IP', '_U'].forEach(key => setCookie(key, ''))
}
Expand Down
10 changes: 6 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function parseHeadersFromCurl(content: string) {
const re = /-H '([^:]+):\s*([^']+)/mg
const headers: HeadersInit = {}
content = content.replaceAll('-H "', '-H \'').replaceAll('" ^', '\'\\').replaceAll('^\\^"', '"') // 将 cmd curl 转成 bash curl
content.replace(re, (_: string, key: string, value: string) => {
content.split('curl ').shift()?.replace(re, (_: string, key: string, value: string) => {
headers[key] = value
return ''
})
Expand Down Expand Up @@ -111,9 +111,11 @@ export function parseCookie(cookie: string, cookieName: string) {
return targetCookie ? decodeURIComponent(targetCookie).trim() : cookie.indexOf('=') === -1 ? cookie.trim() : ''
}

export function setCookie(key: string, value: string) {
const maxAge = value ? 86400 * 30 : 0
document.cookie = `${key}=${value || ''}; Path=/; Max-Age=${maxAge}; SameSite=None; Secure`
export function setCookie(key: string, value?: string) {
const cookie = value === undefined ? key : `${key}=${value || ''}`
const maxAge = cookie.endsWith('=') ? 0 : 86400 * 30
const cookieSuffix = location.protocol === 'http:' ? '' : 'SameSite=None; Secure'
document.cookie = `${cookie}; Path=/; Max-Age=${maxAge}; ${cookieSuffix}`
}

export function getCookie(cookieName: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
const cookies = [`BING_IP=${headers['x-forwarded-for']}`]

res.setHeader('set-cookie', cookies.map(cookie => `${cookie.trim()}; Max-Age=${86400 * 30}; Path=/; SameSite=None; Secure`))
res.setHeader('set-cookie', cookies.map(cookie => `${cookie.trim()}; Max-Age=${86400 * 30}; Path=/;`))
debug('headers', headers)
res.writeHead(200, {
'Content-Type': 'application/json',
Expand Down

0 comments on commit ecd32b3

Please sign in to comment.