This commit is contained in:
2025-05-06 22:16:31 +08:00
commit 54f5caeeaa
33 changed files with 7191 additions and 0 deletions

5
js-demo/server/README.md Normal file
View File

@@ -0,0 +1,5 @@
### 说明
基于 npm 16.13.0 构建
koa server服务

View File

@@ -0,0 +1,22 @@
import axios from "axios"
import { getEncodeHeader } from "./tool/index"
// axios 拦截器
const api = axios.create({
baseURL: "https://live-open.biliapi.com"
// baseURL: "http://test-live-open.biliapi.net" //test
})
// 鉴权加密处理headers下次请求自动带上
api.interceptors.request.use(config => {
const headers = getEncodeHeader(
config.data,
global.appKey,
global.appSecret
)
console.log('headers: ', headers)
config.headers = headers
return config
})
export default api

1777
js-demo/server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
{
"name": "poros-demo-server",
"version": "1.0.0",
"description": "",
"main": "server.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server": "node --loader ts-node/esm --experimental-specifier-resolution=node server.ts"
},
"keywords": [],
"author": "sometimes & re",
"type": "module",
"license": "ISC",
"devDependencies": {
"axios": "0.26.1",
"http": "0.0.1-security",
"https": "1.0.0",
"koa": "2.13.4",
"koa-bodyparser": "4.3.0",
"koa-logger": "3.2.1",
"koa-router": "10.1.1",
"koa2-cors": "2.0.6",
"ts-node": "10.8.0",
"tslib": "2.3.1",
"typescript": "4.6.3",
"@types/node": "17.0.25"
}
}

View File

@@ -0,0 +1,17 @@
import api from "../interceptor"
/**
* 批量心跳接口
* @param ctx
*/
export default async function GameBatchHeartBeat(ctx) {
const params = ctx.request.body
await api
.post("/v2/app/batchHeartbeat", params)
.then(({ data }) => {
ctx.body = data
})
.catch(err => {
ctx.body = err
})
}

View File

@@ -0,0 +1,17 @@
import api from "../interceptor"
/**
* 互动玩法游戏结束接口
* @param ctx
*/
export default async function GameEnd(ctx) {
const params = ctx.request.body
await api
.post("/v2/app/end", params)
.then(({ data }) => {
ctx.body = data
})
.catch(err => {
ctx.body = err
})
}

View File

@@ -0,0 +1,17 @@
import api from "../interceptor"
/**
* 心跳接口
* @param ctx
*/
export default async function GameHeartBeat(ctx) {
const params = ctx.request.body
await api
.post("/v2/app/heartbeat", params)
.then(({ data }) => {
ctx.body = data
})
.catch(err => {
ctx.body = err
})
}

View File

@@ -0,0 +1,17 @@
import api from "../interceptor"
/**
* 互动玩法游戏结束接口
* @param ctx
*/
export default async function GameStart(ctx) {
const params = ctx.request.body
await api
.post("/v2/app/start", params)
.then(({ data }) => {
ctx.body = data
})
.catch(err => {
ctx.body = err
})
}

View File

@@ -0,0 +1,30 @@
import api from "../interceptor"
/**
* 鉴权签名接口
* @param ctx
*/
export default async function GetAuth(ctx) {
const params = ctx.request.body
// 为了方便存在全局
global.appKey = params.appKey
global.appSecret = params.appSecret
// [只为验证鉴权,可删除]
await api
.post("/v2/app/start", {})
.then(({ data }) => {
// 非文档描述性codesign success
if (data.code === -400) {
ctx.body = {
msg: "auth success",
type: "success",
state: 200
}
} else {
ctx.body = data
}
})
.catch(err => {
ctx.body = err
})
}

View File

@@ -0,0 +1,17 @@
import Router from "koa-router"
import GetAuth from "./getAuth"
import GameStart from "./gameStart"
import GameEnd from "./gameEnd"
import GameHeartBeat from "./gameHeartBeat"
import GameBatchHeartBeat from "./gameBatchHeartBeat"
const router = new Router()
// 开启路由
router.post("/getAuth", GetAuth)
router.post("/gameStart", GameStart)
router.post("/gameEnd", GameEnd)
router.post("/gameHeartBeat", GameHeartBeat)
router.post("/gameBatchHeartBeat", GameBatchHeartBeat)
export default router

37
js-demo/server/server.ts Normal file
View File

@@ -0,0 +1,37 @@
import Koa from "koa"
import logger from "koa-logger"
import cors from "koa2-cors"
import bodyParser from "koa-bodyparser"
import router from "./routes/index"
// axios 拦截
import "./interceptor"
// init
const app = new Koa()
// 开启logger
app.use(logger())
// 开启bodyParser
app.use(bodyParser())
// 开启跨域
app.use(cors())
// 开启router
app.use(router.routes())
app.use(router.allowedMethods())
app.use(async ctx => {
ctx.body = "bilibili创作者服务中心"
})
const protocol = "http"
const host = "127.0.0.1"
const port = 3000
app.listen(port, () => {
console.log(`Listening on ${protocol}://${host}:${port}`)
})

View File

@@ -0,0 +1,50 @@
import crypto from "crypto"
/**
* 鉴权加密
* @param {*} params
* @returns
*/
export function getEncodeHeader(
params = {},
appKey: string,
appSecret: string
) {
const timestamp = parseInt(Date.now() / 1000 + "")
const nonce = parseInt(Math.random() * 100000 + "") + timestamp
const header = {
"x-bili-accesskeyid": appKey,
"x-bili-content-md5": getMd5Content(JSON.stringify(params)),
"x-bili-signature-method": "HMAC-SHA256",
"x-bili-signature-nonce": nonce + "",
"x-bili-signature-version": "1.0",
"x-bili-timestamp": timestamp
}
const data: string[] = []
for (const key in header) {
data.push(`${key}:${header[key]}`)
}
const signature = crypto
.createHmac("sha256", appSecret)
.update(data.join("\n"))
.digest("hex")
return {
Accept: "application/json",
"Content-Type": "application/json",
...header,
Authorization: signature
}
}
/**
* MD5加密
* @param {*} str
* @returns
*/
export function getMd5Content(str) {
return crypto
.createHash("md5")
.update(str)
.digest("hex")
}

View File

@@ -0,0 +1,48 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Language and Environment */
"target": "es2016",
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Modules */
"module": "ESNext",
/* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node",
/* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": ["./types"],
/* Specify multiple folders that act like `./node_modules/@types`. */
"types": ["node"],
/* Specify type package names to be included without being referenced in a source file. */
/* JavaScript Support */
// "allowJs": true,
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
/* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true,
/* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true,
/* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true
/* Enable all strict type-checking options. */
/* Completeness */
// "declaration": true // 生成*.d.ts
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
// "skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"ts-node": {
"esm": true
}
}