-
-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add music card to LinkCard (netease/tencent) (#470)
* feat: add QQMusicSong to LinkCard * feat: add NeteaseMusicSong to LinkCard * fix: music card style * chore: remove unnecessary code
- Loading branch information
Showing
7 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
constants, | ||
createCipheriv, | ||
createHash, | ||
publicEncrypt, | ||
randomBytes, | ||
} from 'node:crypto' | ||
|
||
const iv = Buffer.from('0102030405060708') | ||
const presetKey = Buffer.from('0CoJUm6Qyw8W8jud') | ||
const base62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | ||
const publicKey = | ||
'-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB\n-----END PUBLIC KEY-----' | ||
const eapiKey = 'e82ckenh8dichen8' | ||
|
||
const aesEncrypt = ( | ||
buffer: Buffer, | ||
mode: string, | ||
key: Uint8Array | Buffer | string, | ||
iv: Buffer | string, | ||
) => { | ||
const cipher = createCipheriv(`aes-128-${mode}`, key, iv) | ||
return Buffer.concat([cipher.update(buffer), cipher.final()]) | ||
} | ||
|
||
const rsaEncrypt = (buffer: Uint8Array) => | ||
publicEncrypt( | ||
{ key: publicKey, padding: constants.RSA_NO_PADDING }, | ||
Buffer.concat([Buffer.alloc(128 - buffer.length), buffer]), | ||
) | ||
|
||
export const weapi = ( | ||
object: Record<string, number | string | boolean>, | ||
): { params: string; encSecKey: string } => { | ||
const text = JSON.stringify(object) | ||
const secretKey = randomBytes(16).map((n) => | ||
base62.charAt(n % 62).charCodeAt(0), | ||
) | ||
return { | ||
params: aesEncrypt( | ||
Buffer.from( | ||
aesEncrypt(Buffer.from(text), 'cbc', presetKey, iv).toString('base64'), | ||
), | ||
'cbc', | ||
secretKey, | ||
iv, | ||
).toString('base64'), | ||
encSecKey: rsaEncrypt(secretKey.reverse()).toString('hex'), | ||
} | ||
} | ||
|
||
export const eapi = ( | ||
url: string, | ||
object: Record<string, unknown>, | ||
): { params: string } => { | ||
const text = JSON.stringify(object) | ||
const message = `nobody${url}use${text}md5forencrypt` | ||
const digest = createHash('md5').update(message).digest('hex') | ||
const data = `${url}-36cd479b6b5-${text}-36cd479b6b5-${digest}` | ||
return { | ||
params: aesEncrypt(Buffer.from(data), 'ecb', eapiKey, '') | ||
.toString('hex') | ||
.toUpperCase(), | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { NextRequest } from 'next/server' | ||
import { NextResponse } from 'next/server' | ||
|
||
import { weapi } from './crypto' | ||
|
||
export const POST = async (req: NextRequest) => { | ||
const requestBody = await req.json() | ||
const { songId } = requestBody | ||
const data = { | ||
c: JSON.stringify([{ id: songId, v: 0 }]), | ||
} | ||
const body: any = weapi(data) | ||
const bodyString = `params=${encodeURIComponent(body.params)}&encSecKey=${encodeURIComponent(body.encSecKey)}` | ||
|
||
const response = await fetch('http://music.163.com/weapi/v3/song/detail', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
body: bodyString, | ||
}) | ||
|
||
return NextResponse.json(await response.json()) | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { NextRequest } from 'next/server' | ||
import { NextResponse } from 'next/server' | ||
|
||
export const POST = async (req: NextRequest) => { | ||
const requestBody = await req.json() | ||
const { songId } = requestBody | ||
|
||
const response = await fetch( | ||
`https://c.y.qq.com/v8/fcg-bin/fcg_play_single_song.fcg?songmid=${songId}&platform=yqq&format=json`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
) | ||
|
||
return NextResponse.json(await response.json()) | ||
} |
This file contains 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 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 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 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