Skip to content

Commit

Permalink
feat: 使用新的 web 接口 (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
enpitsuLin authored Oct 2, 2024
1 parent 4e8fbe8 commit ade4468
Show file tree
Hide file tree
Showing 10 changed files with 674 additions and 18 deletions.
15 changes: 10 additions & 5 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { antfu } from '@antfu/eslint-config'

export default antfu({}, [
export default antfu(
{
rules: {
'no-console': 'off',
},
ignores: ['src/*.js'],
},
])
[
{
rules: {
'no-console': 'off',
},
},
],
)
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"canvas": "^2.11.2",
"dotenv": "^16.3.1",
"jsdom": "^25.0.1",
"ofetch": "^1.3.4",
"proxy-agent": "^6.4.0",
"tsx": "^4.6.0",
Expand All @@ -23,6 +25,7 @@
"devDependencies": {
"@antfu/eslint-config": "^2.1.1",
"@types/eslint": "^8.44.7",
"@types/jsdom": "^21.1.7",
"@types/node": "^20.10.0",
"eslint": "^8.54.0",
"typescript": "^5.3.2"
Expand Down
597 changes: 597 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/api/hypergryph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function auth(token: string) {
type: 0,
}),
})

if (data.status !== 0 || !data.data)
throw new Error(`登录获取 cred 错误:${data.msg}`)

Expand Down
19 changes: 14 additions & 5 deletions src/api/skland.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createFetch } from 'ofetch'
import { ProxyAgent } from 'proxy-agent'
import type { AttendanceResponse, BindingResponse, CredResponse, GetAttendanceResponse, SklandBoard } from '../types'
import { command_header, onSignatureRequest } from '../utils'
import { command_header, createDeviceId, onSignatureRequest } from '../utils'
import { SKLAND_BOARD_IDS } from '../constant'

const fetch = createFetch({
Expand All @@ -19,10 +19,19 @@ const fetch = createFetch({
*/
export async function signIn(grant_code: string) {
const data = await fetch<CredResponse>(
'/api/v1/user/auth/generate_cred_by_code',
'/web/v1/user/auth/generate_cred_by_code',
{
method: 'POST',
headers: command_header,
headers: {
'content-type': 'application/json',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
'referer': 'https://www.skland.com/',
'origin': 'https://www.skland.com',
'dId': await createDeviceId(),
'platform': '3',
'timestamp': `${Math.floor(Date.now() / 1000)}`,
'vName': '1.0.0',
},
body: {
code: grant_code,
kind: 1,
Expand Down Expand Up @@ -60,8 +69,8 @@ export async function getScoreIsCheckIn(cred: string, token: string) {
{
headers: Object.assign({ token, cred }, command_header),
query: {
gameIds: SKLAND_BOARD_IDS
}
gameIds: SKLAND_BOARD_IDS,
},
},
)
return data
Expand Down
13 changes: 13 additions & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ export const SKLAND_BOARD_NAME_MAPPING: Record<SklandBoard, string> = {
[SklandBoard.Neste]: '纳斯特港',
[SklandBoard.Coreblazer]: '开拓芯',
}

/**
* 数美科技配置
* `window._smConf`
*/
export const SKLAND_SM_CONFIG = {
organization: 'UWXspnCCJN4sfYlNfqps',
appId: 'default',
publicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmxMNr7n8ZeT0tE1R9j/mPixoinPkeM+k4VGIn/s0k7N5rJAfnZ0eMER+QhwFvshzo0LNmeUkpR8uIlU/GEVr8mN28sKmwd2gpygqj0ePnBmOW4v0ZVwbSYK+izkhVFk2V/doLoMbWy6b+UnA8mkjvg0iYWRByfRsK2gdl7llqCwIDAQAB',
protocol: 'https',
apiHost: 'fp-it.portal101.cn',
apiPath: '/deviceprofile/v4',
}
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import process from 'node:process'
import { setTimeout } from 'node:timers/promises'
import { attendance, auth, checkIn, getBinding, getScoreIsCheckIn, signIn } from './api'
import { attendance, auth, getBinding, signIn } from './api'
import { bark, serverChan } from './notifications'
import { getPrivacyName } from './utils'
import { SKLAND_BOARD_IDS, SKLAND_BOARD_NAME_MAPPING } from './constant'

interface Options {
/** server 酱推送功能的启用,false 或者 server 酱的token */
Expand Down Expand Up @@ -80,7 +79,6 @@ export async function doAttendanceForAccount(token: string, options: Options) {
else {
combineMessage(`${(Number(character.channelMasterId) - 1) ? 'B 服' : '官服'}角色 ${getPrivacyName(character.nickName)} 今天已经签到过了`)
}

}))
if (successAttendance !== 0)
combineMessage(`成功签到${successAttendance}个角色`)
Expand All @@ -93,7 +91,7 @@ export async function doAttendanceForAccount(token: string, options: Options) {
// await Promise.all(
// SKLAND_BOARD_IDS
// .map(async (id) => {
// // 过滤已经签到过的
// // 过滤已经签到过的
// const name = SKLAND_BOARD_NAME_MAPPING[id]
// if (isCheckIn.data.list.find(i => i.gameId === id)?.checked !== 1) {
// const data = await checkIn(cred, signToken, id)
Expand Down
1 change: 1 addition & 0 deletions src/sm.sdk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ export enum SklandBoard {
/** 纳斯特港 */
Neste = 100,
/** 开拓芯 */
Coreblazer = 101
Coreblazer = 101,
}
34 changes: 32 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createHash, createHmac } from 'node:crypto'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { Script } from 'node:vm'
import { JSDOM } from 'jsdom'
import type { FetchContext } from 'ofetch'
import { stringifyQuery } from 'ufo'
import { SKLAND_SM_CONFIG } from './constant'

export const command_header = {
'User-Agent': 'Skland/1.21.0 (com.hypergryph.skland; build:102100065; iOS 17.6.0; ) Alamofire/5.7.1',
Expand Down Expand Up @@ -31,7 +36,7 @@ export function getRequestURL(request: RequestInfo, baseURL?: string) {
return new URL(url, baseURL)
}

const WHITE_LIST = ['/api/v1/user/auth/generate_cred_by_code']
const WHITE_LIST = ['/web/v1/user/auth/generate_cred_by_code']

export function onSignatureRequest(ctx: FetchContext) {
const { pathname } = getRequestURL(ctx.request, ctx.options.baseURL)
Expand Down Expand Up @@ -67,6 +72,31 @@ export function onSignatureRequest(ctx: FetchContext) {
})
headers.append('sign', sign)
headers.delete('token')

ctx.options.headers = headers
}

export function createDeviceId() {
// @ts-expect-error ignore
const sdkJsPath = path.resolve(import.meta.dirname, './sm.sdk.js')
return new Promise<string>((resolve) => {
const dom = new JSDOM(
'',
{
runScripts: 'outside-only',
beforeParse(window) {
window._smReadyFuncs = [
() => {
resolve(window.SMSdk.getDeviceId())
},
]
window._smConf = SKLAND_SM_CONFIG
},
},
)

const script = new Script(readFileSync(sdkJsPath, 'utf-8'))
const vmContext = dom.getInternalVMContext()
script.runInNewContext(vmContext)
})
}

0 comments on commit ade4468

Please sign in to comment.