-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fix login #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hykilpikonna
wants to merge
6
commits into
NeteaseCloudMusicApiEnhanced:main
Choose a base branch
from
AzaContrib:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix login #201
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30ef027
fix: align QR login with web flow
hykilpikonna b97fc59
fix: use web scanlogin QR URL
hykilpikonna 39d8080
fix: add mobile sms login flow
hykilpikonna 873d7b4
fix: emulate web qr login context
hykilpikonna bb3db23
fix: preserve qr login cookie
hykilpikonna b7bf2b5
fix: support qr login security captcha
hykilpikonna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,85 @@ | ||
| const createOption = require('../util/option.js') | ||
| const { generateChainId } = require('../util/index') | ||
| const { | ||
| cookieHeaderToList, | ||
| createWebQrOption, | ||
| mergeCookieLists, | ||
| } = require('../util/web_qr_login.js') | ||
|
|
||
| const qrLoginChainIds = | ||
| globalThis.__neteaseQrLoginChainIds || | ||
| (globalThis.__neteaseQrLoginChainIds = new Map()) | ||
|
|
||
| module.exports = async (query, request) => { | ||
| const platform = query.platform || 'web' | ||
| const chainId = | ||
| query.chainId || | ||
| qrLoginChainIds.get(String(query.key)) || | ||
| (platform === 'web' ? generateChainId(query.cookie || '') : '') | ||
| const data = { | ||
| key: query.key, | ||
| type: 3, | ||
| type: platform === 'web' ? 1 : 3, | ||
| } | ||
| if (platform === 'web') { | ||
| data.noCheckToken = true | ||
| if (query.secureCaptcha) data.secureCaptcha = query.secureCaptcha | ||
| data.ydDeviceToken = query.ydDeviceToken || '' | ||
| } | ||
|
|
||
| let option = createOption(query, '') | ||
| if (platform === 'web') { | ||
| option = createWebQrOption(query, { | ||
| 'X-loginMethod': 'QrCode', | ||
| 'x-login-chain-id': chainId, | ||
| }) | ||
| } | ||
|
|
||
| try { | ||
| let result = await request( | ||
| `/api/login/qrcode/client/login`, | ||
| data, | ||
| createOption(query), | ||
| ) | ||
| let result = await request(`/api/login/qrcode/client/login`, data, option) | ||
| let responseCookies = result.cookie | ||
| if (platform === 'web') { | ||
| responseCookies = mergeCookieLists( | ||
| option.webQrCookieSet, | ||
| result.cookie, | ||
| cookieHeaderToList(result.body.cookie), | ||
| ) | ||
| } | ||
| const responseCookie = result.body.cookie || responseCookies.join(';') | ||
| if ([800, 803].includes(result.body.code)) { | ||
| qrLoginChainIds.delete(String(query.key)) | ||
| } | ||
| result = { | ||
| status: 200, | ||
| body: { | ||
| ...result.body, | ||
| cookie: result.cookie.join(';'), | ||
| cookie: responseCookie, | ||
| }, | ||
| cookie: result.cookie, | ||
| cookie: responseCookies, | ||
| } | ||
| return result | ||
| } catch (error) { | ||
| if ([800, 803].includes(error.body && error.body.code)) { | ||
| qrLoginChainIds.delete(String(query.key)) | ||
| } | ||
| let body = error.body || {} | ||
| let cookies = error.cookie || [] | ||
| if (platform === 'web') { | ||
| cookies = mergeCookieLists( | ||
| option.webQrCookieSet, | ||
| error.cookie, | ||
| cookieHeaderToList(error.body && error.body.cookie), | ||
| ) | ||
| if (error.body) { | ||
| body = { | ||
| ...error.body, | ||
| cookie: error.body.cookie || cookies.join(';'), | ||
| } | ||
| } | ||
| } | ||
| return { | ||
| status: 200, | ||
| body: {}, | ||
| cookie: result.cookie, | ||
| body, | ||
| cookie: cookies, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,26 @@ | ||
| const QRCode = require('qrcode') | ||
| const { generateChainId } = require('../util/index') | ||
|
|
||
| const qrLoginChainIds = | ||
| globalThis.__neteaseQrLoginChainIds || | ||
| (globalThis.__neteaseQrLoginChainIds = new Map()) | ||
|
|
||
| module.exports = (query) => { | ||
| return new Promise(async (resolve) => { | ||
| const platform = query.platform || 'pc' | ||
| const platform = query.platform || 'web' | ||
| const cookie = query.cookie || '' | ||
| const chainId = | ||
| platform === 'web' ? query.chainId || generateChainId(cookie) : '' | ||
|
|
||
| // 构建基础URL | ||
| let url = `https://music.163.com/login?codekey=${query.key}` | ||
|
|
||
| // 如果是web平台,则添加chainId参数 | ||
|
|
||
| if (platform === 'web') { | ||
| const chainId = generateChainId(cookie) | ||
| url += `&chainId=${chainId}` | ||
| if (query.key && chainId) { | ||
| qrLoginChainIds.set(String(query.key), chainId) | ||
| } | ||
|
Comment on lines
+15
to
17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent memory leaks from abandoned QR code sessions, store the if (query.key && chainId) {
qrLoginChainIds.set(String(query.key), { chainId, timestamp: Date.now() })
if (qrLoginChainIds.size > 100) {
const now = Date.now()
for (const [k, v] of qrLoginChainIds.entries()) {
if (!v || !v.timestamp || now - v.timestamp > 300000) {
qrLoginChainIds.delete(k)
}
}
}
} |
||
|
|
||
| // 构建基础URL | ||
| let url = | ||
| platform === 'web' | ||
| ? `https://music.163.com/st/platform/scanlogin?codekey=${query.key}&chainId=${encodeURIComponent(chainId)}&hdw_device=web&hdw_appid=web&hitExp=1` | ||
| : `https://music.163.com/login?codekey=${query.key}` | ||
| return resolve({ | ||
| code: 200, | ||
| status: 200, | ||
|
|
@@ -23,6 +29,7 @@ module.exports = (query) => { | |
| data: { | ||
| qrurl: url, | ||
| qrimg: query.qrimg ? await QRCode.toDataURL(url) : '', | ||
| chainId, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,31 @@ | ||
| const createOption = require('../util/option.js') | ||
| const { | ||
| createWebQrOption, | ||
| mergeCookieLists, | ||
| } = require('../util/web_qr_login.js') | ||
| module.exports = async (query, request) => { | ||
| const platform = query.platform || 'web' | ||
| const data = { | ||
| type: 3, | ||
| type: platform === 'web' ? 1 : 3, | ||
| } | ||
| const result = await request( | ||
| `/api/login/qrcode/unikey`, | ||
| data, | ||
| createOption(query), | ||
| ) | ||
| if (platform === 'web') { | ||
| data.noCheckToken = true | ||
| if (query.lastUnikey) data.lastUnikey = query.lastUnikey | ||
| } | ||
| const option = | ||
| platform === 'web' ? createWebQrOption(query) : createOption(query, '') | ||
|
|
||
| const result = await request(`/api/login/qrcode/unikey`, data, option) | ||
| const cookies = | ||
| platform === 'web' | ||
| ? mergeCookieLists(option.webQrCookieSet, result.cookie) | ||
| : result.cookie | ||
| return { | ||
| status: 200, | ||
| body: { | ||
| data: result.body, | ||
| code: 200, | ||
| }, | ||
| cookie: result.cookie, | ||
| cookie: cookies, | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a global
Map(qrLoginChainIds) to storechainIdvalues can lead to a memory leak if clients generate QR codes but never poll or complete the login flow. To prevent this, we should store thechainIdalong with a timestamp and periodically clean up expired entries.To support this safely, retrieve the
chainIdby checking if the cached value is an object.