@seayoo-io/combo-sdk-node 1.5.0 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/types.d.ts +0 -4
- package/dist/const.d.ts +10 -2
- package/dist/index.cjs +80 -30
- package/dist/index.js +80 -30
- package/dist/utils/gcm.d.ts +9 -0
- package/package.json +1 -1
package/dist/api/types.d.ts
CHANGED
|
@@ -37,10 +37,6 @@ export interface OrderMetaData {
|
|
|
37
37
|
role_name?: string;
|
|
38
38
|
/** 游戏角色的等级 */
|
|
39
39
|
role_level?: number;
|
|
40
|
-
/** 微信小游戏的 App ID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
41
|
-
weixin_appid?: string;
|
|
42
|
-
/** 微信小游戏的玩家 OpenID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
43
|
-
weixin_openid?: string;
|
|
44
40
|
}
|
|
45
41
|
export interface CreateOrderResponse {
|
|
46
42
|
/** 世游服务端创建的,标识订单的唯一 ID。 */
|
package/dist/const.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** SDK 名称 */
|
|
2
2
|
export declare const SDKName = "combo-sdk-node";
|
|
3
3
|
/** SDK 版本,在编译前会被动态替换 */
|
|
4
|
-
export declare const SDKVersion = "1.5.
|
|
4
|
+
export declare const SDKVersion = "1.5.2";
|
|
5
5
|
/**
|
|
6
6
|
* 世游支持的服务端点列表
|
|
7
7
|
*/
|
|
@@ -60,6 +60,8 @@ export declare enum IdP {
|
|
|
60
60
|
Yingyongbao = "yingyongbao",
|
|
61
61
|
/** 4399 账号登录 */
|
|
62
62
|
F4399 = "4399",
|
|
63
|
+
/** 抖音账号 */
|
|
64
|
+
Douyin = "douyin",
|
|
63
65
|
/** 雷电模拟器账号 */
|
|
64
66
|
Leidian = "leidian",
|
|
65
67
|
/** 猫窝游戏 */
|
|
@@ -78,10 +80,16 @@ export declare enum IdP {
|
|
|
78
80
|
Meituo = "meituo",
|
|
79
81
|
/** 微信小游戏 */
|
|
80
82
|
MinigameWeixin = "minigame_weixin",
|
|
83
|
+
/** 抖音小游戏 */
|
|
84
|
+
MinigameDouyin = "minigame_douyin",
|
|
81
85
|
/** MuMu 模拟器 */
|
|
82
86
|
Mumu = "mumu",
|
|
83
87
|
/** 虎牙游戏 */
|
|
84
|
-
Huya = "huya"
|
|
88
|
+
Huya = "huya",
|
|
89
|
+
/** 好游快爆 */
|
|
90
|
+
Hykb = "hykb",
|
|
91
|
+
/** 233 乐园 */
|
|
92
|
+
L233 = "233"
|
|
85
93
|
}
|
|
86
94
|
/** GM 错误类型枚举 */
|
|
87
95
|
export declare enum GMError {
|
package/dist/index.cjs
CHANGED
|
@@ -12,7 +12,7 @@ var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
|
12
12
|
/** SDK 名称 */
|
|
13
13
|
const SDKName = "combo-sdk-node";
|
|
14
14
|
/** SDK 版本,在编译前会被动态替换 */
|
|
15
|
-
const SDKVersion = "1.5.
|
|
15
|
+
const SDKVersion = "1.5.2";
|
|
16
16
|
/**
|
|
17
17
|
* 世游支持的服务端点列表
|
|
18
18
|
*/
|
|
@@ -74,6 +74,8 @@ exports.IdP = void 0;
|
|
|
74
74
|
IdP["Yingyongbao"] = "yingyongbao";
|
|
75
75
|
/** 4399 账号登录 */
|
|
76
76
|
IdP["F4399"] = "4399";
|
|
77
|
+
/** 抖音账号 */
|
|
78
|
+
IdP["Douyin"] = "douyin";
|
|
77
79
|
/** 雷电模拟器账号 */
|
|
78
80
|
IdP["Leidian"] = "leidian";
|
|
79
81
|
/** 猫窝游戏 */
|
|
@@ -92,13 +94,19 @@ exports.IdP = void 0;
|
|
|
92
94
|
IdP["Meituo"] = "meituo";
|
|
93
95
|
/** 微信小游戏 */
|
|
94
96
|
IdP["MinigameWeixin"] = "minigame_weixin";
|
|
97
|
+
/** 抖音小游戏 */
|
|
98
|
+
IdP["MinigameDouyin"] = "minigame_douyin";
|
|
95
99
|
/** MuMu 模拟器 */
|
|
96
100
|
IdP["Mumu"] = "mumu";
|
|
97
101
|
/** 虎牙游戏 */
|
|
98
102
|
IdP["Huya"] = "huya";
|
|
103
|
+
/** 好游快爆 */
|
|
104
|
+
IdP["Hykb"] = "hykb";
|
|
105
|
+
/** 233 乐园 */
|
|
106
|
+
IdP["L233"] = "233";
|
|
99
107
|
})(exports.IdP || (exports.IdP = {}));
|
|
100
108
|
/** GM 错误类型枚举 */
|
|
101
|
-
|
|
109
|
+
exports.GMError = void 0;
|
|
102
110
|
(function (GMError) {
|
|
103
111
|
/** 请求中的 HTTP method 不正确,没有按照预期使用 POST。*/
|
|
104
112
|
GMError["InvalidHttpMethod"] = "invalid_http_method";
|
|
@@ -128,7 +136,7 @@ var GMError;
|
|
|
128
136
|
GMError["TimeoutError"] = "timeout_error";
|
|
129
137
|
/** 处理 GM 命令时内部出错。可作为兜底的通用错误类型。*/
|
|
130
138
|
GMError["InternalError"] = "internal_error";
|
|
131
|
-
})(GMError || (GMError = {}));
|
|
139
|
+
})(exports.GMError || (exports.GMError = {}));
|
|
132
140
|
|
|
133
141
|
function getUserAgent(game) {
|
|
134
142
|
return `${SDKName}/${SDKVersion} game/${game} node/${process.version} os/${os.platform()} arch/${os.arch()}`;
|
|
@@ -1168,6 +1176,47 @@ function isAdJwtPayload(payload) {
|
|
|
1168
1176
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "placement_id" in payload && "impression_id" in payload;
|
|
1169
1177
|
}
|
|
1170
1178
|
|
|
1179
|
+
/**
|
|
1180
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
1181
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
1182
|
+
*/
|
|
1183
|
+
function decryptAESGCM(sk, encoded) {
|
|
1184
|
+
let data;
|
|
1185
|
+
try {
|
|
1186
|
+
data = Buffer.from(encoded, "base64");
|
|
1187
|
+
} catch (err) {
|
|
1188
|
+
throw new Error(`base64 decode error: ${err}`);
|
|
1189
|
+
}
|
|
1190
|
+
const key = deriveAESKey(sk);
|
|
1191
|
+
const nonceSize = 12;
|
|
1192
|
+
const authTagSize = 16;
|
|
1193
|
+
if (data.length < nonceSize + authTagSize) {
|
|
1194
|
+
throw new Error("ciphertext too short");
|
|
1195
|
+
}
|
|
1196
|
+
const nonce = data.slice(0, nonceSize);
|
|
1197
|
+
// 密文格式: nonce (12) || ciphertext (variable) || authTag (16)
|
|
1198
|
+
const authTag = data.slice(data.length - authTagSize);
|
|
1199
|
+
const ciphertext = data.slice(nonceSize, data.length - authTagSize);
|
|
1200
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, nonce);
|
|
1201
|
+
decipher.setAuthTag(authTag);
|
|
1202
|
+
// Node.js 12 兼容:使用 update 处理所有数据,避免 final() 的问题
|
|
1203
|
+
let plaintext;
|
|
1204
|
+
try {
|
|
1205
|
+
plaintext = decipher.update(ciphertext);
|
|
1206
|
+
// Node.js 12 中 GCM 模式下 final() 通常返回空 Buffer,但会验证 authTag
|
|
1207
|
+
const finalBuffer = decipher.final();
|
|
1208
|
+
if (finalBuffer.length > 0) {
|
|
1209
|
+
plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1210
|
+
}
|
|
1211
|
+
} catch (err) {
|
|
1212
|
+
throw new Error(`gcm decrypt error: ${err}`);
|
|
1213
|
+
}
|
|
1214
|
+
return plaintext.toString("utf8");
|
|
1215
|
+
}
|
|
1216
|
+
function deriveAESKey(sk) {
|
|
1217
|
+
return crypto.createHash("sha256").update(sk).digest();
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1171
1220
|
const IdentityTokenScope = "auth";
|
|
1172
1221
|
const AdTokenScope = "ads";
|
|
1173
1222
|
/**
|
|
@@ -1204,12 +1253,13 @@ class TokenVerifier {
|
|
|
1204
1253
|
if (payload.scope !== IdentityTokenScope) {
|
|
1205
1254
|
return new Error("verifyIdentityToken: 无效的 Scope");
|
|
1206
1255
|
}
|
|
1256
|
+
// spell-checker:ignore AESGCM
|
|
1207
1257
|
return {
|
|
1208
1258
|
combo_id: payload.sub,
|
|
1209
1259
|
idp: payload.idp,
|
|
1210
1260
|
external_id: payload.external_id,
|
|
1211
1261
|
external_name: payload.external_name,
|
|
1212
|
-
weixin_session_key: payload.weixin_session_key
|
|
1262
|
+
weixin_session_key: payload.weixin_session_key ? decryptAESGCM(this.privateKey, payload.weixin_session_key) : "",
|
|
1213
1263
|
distro: payload.distro || "",
|
|
1214
1264
|
variant: payload.variant || "",
|
|
1215
1265
|
device_id: payload.device_id || "",
|
|
@@ -1471,20 +1521,20 @@ class RedisIdempotencyStore {
|
|
|
1471
1521
|
}
|
|
1472
1522
|
|
|
1473
1523
|
const error2httpStatus = {
|
|
1474
|
-
[
|
|
1475
|
-
[
|
|
1476
|
-
[
|
|
1477
|
-
[
|
|
1478
|
-
[
|
|
1479
|
-
[
|
|
1480
|
-
[
|
|
1481
|
-
[
|
|
1482
|
-
[
|
|
1483
|
-
[
|
|
1484
|
-
[
|
|
1485
|
-
[
|
|
1486
|
-
[
|
|
1487
|
-
[
|
|
1524
|
+
[exports.GMError.InvalidHttpMethod]: 405 /* HttpStatus.MethodNotAllowed */,
|
|
1525
|
+
[exports.GMError.InvalidContentType]: 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1526
|
+
[exports.GMError.InvalidSignature]: 401 /* HttpStatus.Unauthorized */,
|
|
1527
|
+
[exports.GMError.InvalidRequest]: 400 /* HttpStatus.BadRequest */,
|
|
1528
|
+
[exports.GMError.InvalidCommand]: 400 /* HttpStatus.BadRequest */,
|
|
1529
|
+
[exports.GMError.InvalidArgs]: 400 /* HttpStatus.BadRequest */,
|
|
1530
|
+
[exports.GMError.NetworkError]: 500 /* HttpStatus.InternalServerError */,
|
|
1531
|
+
[exports.GMError.DatabaseError]: 500 /* HttpStatus.InternalServerError */,
|
|
1532
|
+
[exports.GMError.IdempotencyConflict]: 409 /* HttpStatus.Conflict */,
|
|
1533
|
+
[exports.GMError.IdempotencyMismatch]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1534
|
+
[exports.GMError.TimeoutError]: 500 /* HttpStatus.InternalServerError */,
|
|
1535
|
+
[exports.GMError.MaintenanceError]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1536
|
+
[exports.GMError.ThrottlingError]: 429 /* HttpStatus.TooManyRequests */,
|
|
1537
|
+
[exports.GMError.InternalError]: 500 /* HttpStatus.InternalServerError */
|
|
1488
1538
|
};
|
|
1489
1539
|
/**
|
|
1490
1540
|
* 获取一个处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知,storeHelper 提供存储支持以实现幂等key(idempotencyKey)的自动化处理
|
|
@@ -1498,12 +1548,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1498
1548
|
verifyStoreHelper(storeHelper);
|
|
1499
1549
|
return async function (req, res, rawBodyString) {
|
|
1500
1550
|
if (req.method !== "POST") {
|
|
1501
|
-
responseError(res, 405 /* HttpStatus.MethodNotAllowed */,
|
|
1551
|
+
responseError(res, 405 /* HttpStatus.MethodNotAllowed */, exports.GMError.InvalidHttpMethod, `Expecting POST, got ${req.method}`);
|
|
1502
1552
|
return;
|
|
1503
1553
|
}
|
|
1504
1554
|
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1505
1555
|
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1506
|
-
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1556
|
+
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */, exports.GMError.InvalidContentType, `Expecting application/json, got ${contentType}`);
|
|
1507
1557
|
return;
|
|
1508
1558
|
}
|
|
1509
1559
|
const authString = req.headers[AuthorizationField] || req.headers[AuthorizationField.toLowerCase()];
|
|
@@ -1517,12 +1567,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1517
1567
|
url: req.url || "",
|
|
1518
1568
|
data: body
|
|
1519
1569
|
})) {
|
|
1520
|
-
responseError(res, 401 /* HttpStatus.Unauthorized */,
|
|
1570
|
+
responseError(res, 401 /* HttpStatus.Unauthorized */, exports.GMError.InvalidSignature, "Signature failed");
|
|
1521
1571
|
return;
|
|
1522
1572
|
}
|
|
1523
1573
|
const gmRequest = parseJSON(body, isGMRequestBody);
|
|
1524
1574
|
if (!gmRequest) {
|
|
1525
|
-
responseError(res, 400 /* HttpStatus.BadRequest */,
|
|
1575
|
+
responseError(res, 400 /* HttpStatus.BadRequest */, exports.GMError.InvalidRequest, `GM request body format error`);
|
|
1526
1576
|
return;
|
|
1527
1577
|
}
|
|
1528
1578
|
// 处理幂等key
|
|
@@ -1538,13 +1588,13 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1538
1588
|
const oldRecordStr = await storeHelper.setNX(gmRequest.idempotency_key, toString(currentRecord)).catch(e => e instanceof Error ? e : new Error(String(e)));
|
|
1539
1589
|
if (oldRecordStr instanceof Error) {
|
|
1540
1590
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) setNX error`, oldRecordStr);
|
|
1541
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1591
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, oldRecordStr.message);
|
|
1542
1592
|
return;
|
|
1543
1593
|
}
|
|
1544
1594
|
const oldRecord = oldRecordStr ? parseJSON(oldRecordStr, isIdempotencyRecord) : null;
|
|
1545
1595
|
if (!oldRecord && oldRecordStr) {
|
|
1546
1596
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) parse old idempotency record failed: ${oldRecordStr}`);
|
|
1547
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1597
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, "parse old idempotency record failed");
|
|
1548
1598
|
return;
|
|
1549
1599
|
}
|
|
1550
1600
|
// spell-checker:disable
|
|
@@ -1557,15 +1607,15 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1557
1607
|
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1558
1608
|
if (!isFirstRequest && oldRecord) {
|
|
1559
1609
|
if (!oldRecord.resp) {
|
|
1560
|
-
responseError(res, error2httpStatus[
|
|
1610
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyConflict], exports.GMError.IdempotencyConflict, "previous request is not completed");
|
|
1561
1611
|
return;
|
|
1562
1612
|
}
|
|
1563
1613
|
if (oldRecord.cmd !== gmRequest.command) {
|
|
1564
|
-
responseError(res, error2httpStatus[
|
|
1614
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyMismatch], exports.GMError.IdempotencyMismatch, `cmd mismatch, expecting ${oldRecord.cmd}, got ${gmRequest.command}`);
|
|
1565
1615
|
return;
|
|
1566
1616
|
}
|
|
1567
1617
|
if (oldRecord.args !== currentRecord.args) {
|
|
1568
|
-
responseError(res, error2httpStatus[
|
|
1618
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyMismatch], exports.GMError.IdempotencyMismatch, `args mismatch, expecting ${oldRecord.args}, got ${currentRecord.args}`);
|
|
1569
1619
|
return;
|
|
1570
1620
|
}
|
|
1571
1621
|
oldResponse = oldRecord.resp;
|
|
@@ -1583,11 +1633,11 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1583
1633
|
}
|
|
1584
1634
|
// 游戏主动抛错不打印日志信息
|
|
1585
1635
|
if (result instanceof Error) {
|
|
1586
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1636
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, result.message);
|
|
1587
1637
|
return;
|
|
1588
1638
|
}
|
|
1589
1639
|
if (isGMErrorResponse(result)) {
|
|
1590
|
-
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error ||
|
|
1640
|
+
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error || exports.GMError.InternalError, result.message);
|
|
1591
1641
|
return;
|
|
1592
1642
|
}
|
|
1593
1643
|
// 对游戏处理函数进一步兼容处理,允许返回一个预定义的错误码
|
|
@@ -1603,7 +1653,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1603
1653
|
responseJson(res, result != null ? result : {});
|
|
1604
1654
|
} catch (e) {
|
|
1605
1655
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1606
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1656
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1607
1657
|
}
|
|
1608
1658
|
};
|
|
1609
1659
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { platform, arch } from 'os';
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import https from 'https';
|
|
4
|
-
import { createHash, createHmac } from 'crypto';
|
|
4
|
+
import { createHash, createHmac, createDecipheriv } from 'crypto';
|
|
5
5
|
import { verify } from 'jsonwebtoken';
|
|
6
6
|
|
|
7
7
|
/** SDK 名称 */
|
|
8
8
|
const SDKName = "combo-sdk-node";
|
|
9
9
|
/** SDK 版本,在编译前会被动态替换 */
|
|
10
|
-
const SDKVersion = "1.5.
|
|
10
|
+
const SDKVersion = "1.5.2";
|
|
11
11
|
/**
|
|
12
12
|
* 世游支持的服务端点列表
|
|
13
13
|
*/
|
|
@@ -69,6 +69,8 @@ var IdP;
|
|
|
69
69
|
IdP["Yingyongbao"] = "yingyongbao";
|
|
70
70
|
/** 4399 账号登录 */
|
|
71
71
|
IdP["F4399"] = "4399";
|
|
72
|
+
/** 抖音账号 */
|
|
73
|
+
IdP["Douyin"] = "douyin";
|
|
72
74
|
/** 雷电模拟器账号 */
|
|
73
75
|
IdP["Leidian"] = "leidian";
|
|
74
76
|
/** 猫窝游戏 */
|
|
@@ -87,10 +89,16 @@ var IdP;
|
|
|
87
89
|
IdP["Meituo"] = "meituo";
|
|
88
90
|
/** 微信小游戏 */
|
|
89
91
|
IdP["MinigameWeixin"] = "minigame_weixin";
|
|
92
|
+
/** 抖音小游戏 */
|
|
93
|
+
IdP["MinigameDouyin"] = "minigame_douyin";
|
|
90
94
|
/** MuMu 模拟器 */
|
|
91
95
|
IdP["Mumu"] = "mumu";
|
|
92
96
|
/** 虎牙游戏 */
|
|
93
97
|
IdP["Huya"] = "huya";
|
|
98
|
+
/** 好游快爆 */
|
|
99
|
+
IdP["Hykb"] = "hykb";
|
|
100
|
+
/** 233 乐园 */
|
|
101
|
+
IdP["L233"] = "233";
|
|
94
102
|
})(IdP || (IdP = {}));
|
|
95
103
|
/** GM 错误类型枚举 */
|
|
96
104
|
var GMError;
|
|
@@ -1163,6 +1171,47 @@ function isAdJwtPayload(payload) {
|
|
|
1163
1171
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "placement_id" in payload && "impression_id" in payload;
|
|
1164
1172
|
}
|
|
1165
1173
|
|
|
1174
|
+
/**
|
|
1175
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
1176
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
1177
|
+
*/
|
|
1178
|
+
function decryptAESGCM(sk, encoded) {
|
|
1179
|
+
let data;
|
|
1180
|
+
try {
|
|
1181
|
+
data = Buffer.from(encoded, "base64");
|
|
1182
|
+
} catch (err) {
|
|
1183
|
+
throw new Error(`base64 decode error: ${err}`);
|
|
1184
|
+
}
|
|
1185
|
+
const key = deriveAESKey(sk);
|
|
1186
|
+
const nonceSize = 12;
|
|
1187
|
+
const authTagSize = 16;
|
|
1188
|
+
if (data.length < nonceSize + authTagSize) {
|
|
1189
|
+
throw new Error("ciphertext too short");
|
|
1190
|
+
}
|
|
1191
|
+
const nonce = data.slice(0, nonceSize);
|
|
1192
|
+
// 密文格式: nonce (12) || ciphertext (variable) || authTag (16)
|
|
1193
|
+
const authTag = data.slice(data.length - authTagSize);
|
|
1194
|
+
const ciphertext = data.slice(nonceSize, data.length - authTagSize);
|
|
1195
|
+
const decipher = createDecipheriv("aes-256-gcm", key, nonce);
|
|
1196
|
+
decipher.setAuthTag(authTag);
|
|
1197
|
+
// Node.js 12 兼容:使用 update 处理所有数据,避免 final() 的问题
|
|
1198
|
+
let plaintext;
|
|
1199
|
+
try {
|
|
1200
|
+
plaintext = decipher.update(ciphertext);
|
|
1201
|
+
// Node.js 12 中 GCM 模式下 final() 通常返回空 Buffer,但会验证 authTag
|
|
1202
|
+
const finalBuffer = decipher.final();
|
|
1203
|
+
if (finalBuffer.length > 0) {
|
|
1204
|
+
plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1205
|
+
}
|
|
1206
|
+
} catch (err) {
|
|
1207
|
+
throw new Error(`gcm decrypt error: ${err}`);
|
|
1208
|
+
}
|
|
1209
|
+
return plaintext.toString("utf8");
|
|
1210
|
+
}
|
|
1211
|
+
function deriveAESKey(sk) {
|
|
1212
|
+
return createHash("sha256").update(sk).digest();
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1166
1215
|
const IdentityTokenScope = "auth";
|
|
1167
1216
|
const AdTokenScope = "ads";
|
|
1168
1217
|
/**
|
|
@@ -1199,12 +1248,13 @@ class TokenVerifier {
|
|
|
1199
1248
|
if (payload.scope !== IdentityTokenScope) {
|
|
1200
1249
|
return new Error("verifyIdentityToken: 无效的 Scope");
|
|
1201
1250
|
}
|
|
1251
|
+
// spell-checker:ignore AESGCM
|
|
1202
1252
|
return {
|
|
1203
1253
|
combo_id: payload.sub,
|
|
1204
1254
|
idp: payload.idp,
|
|
1205
1255
|
external_id: payload.external_id,
|
|
1206
1256
|
external_name: payload.external_name,
|
|
1207
|
-
weixin_session_key: payload.weixin_session_key
|
|
1257
|
+
weixin_session_key: payload.weixin_session_key ? decryptAESGCM(this.privateKey, payload.weixin_session_key) : "",
|
|
1208
1258
|
distro: payload.distro || "",
|
|
1209
1259
|
variant: payload.variant || "",
|
|
1210
1260
|
device_id: payload.device_id || "",
|
|
@@ -1466,20 +1516,20 @@ class RedisIdempotencyStore {
|
|
|
1466
1516
|
}
|
|
1467
1517
|
|
|
1468
1518
|
const error2httpStatus = {
|
|
1469
|
-
[
|
|
1470
|
-
[
|
|
1471
|
-
[
|
|
1472
|
-
[
|
|
1473
|
-
[
|
|
1474
|
-
[
|
|
1475
|
-
[
|
|
1476
|
-
[
|
|
1477
|
-
[
|
|
1478
|
-
[
|
|
1479
|
-
[
|
|
1480
|
-
[
|
|
1481
|
-
[
|
|
1482
|
-
[
|
|
1519
|
+
[GMError.InvalidHttpMethod]: 405 /* HttpStatus.MethodNotAllowed */,
|
|
1520
|
+
[GMError.InvalidContentType]: 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1521
|
+
[GMError.InvalidSignature]: 401 /* HttpStatus.Unauthorized */,
|
|
1522
|
+
[GMError.InvalidRequest]: 400 /* HttpStatus.BadRequest */,
|
|
1523
|
+
[GMError.InvalidCommand]: 400 /* HttpStatus.BadRequest */,
|
|
1524
|
+
[GMError.InvalidArgs]: 400 /* HttpStatus.BadRequest */,
|
|
1525
|
+
[GMError.NetworkError]: 500 /* HttpStatus.InternalServerError */,
|
|
1526
|
+
[GMError.DatabaseError]: 500 /* HttpStatus.InternalServerError */,
|
|
1527
|
+
[GMError.IdempotencyConflict]: 409 /* HttpStatus.Conflict */,
|
|
1528
|
+
[GMError.IdempotencyMismatch]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1529
|
+
[GMError.TimeoutError]: 500 /* HttpStatus.InternalServerError */,
|
|
1530
|
+
[GMError.MaintenanceError]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1531
|
+
[GMError.ThrottlingError]: 429 /* HttpStatus.TooManyRequests */,
|
|
1532
|
+
[GMError.InternalError]: 500 /* HttpStatus.InternalServerError */
|
|
1483
1533
|
};
|
|
1484
1534
|
/**
|
|
1485
1535
|
* 获取一个处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知,storeHelper 提供存储支持以实现幂等key(idempotencyKey)的自动化处理
|
|
@@ -1493,12 +1543,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1493
1543
|
verifyStoreHelper(storeHelper);
|
|
1494
1544
|
return async function (req, res, rawBodyString) {
|
|
1495
1545
|
if (req.method !== "POST") {
|
|
1496
|
-
responseError(res, 405 /* HttpStatus.MethodNotAllowed */,
|
|
1546
|
+
responseError(res, 405 /* HttpStatus.MethodNotAllowed */, GMError.InvalidHttpMethod, `Expecting POST, got ${req.method}`);
|
|
1497
1547
|
return;
|
|
1498
1548
|
}
|
|
1499
1549
|
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1500
1550
|
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1501
|
-
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1551
|
+
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */, GMError.InvalidContentType, `Expecting application/json, got ${contentType}`);
|
|
1502
1552
|
return;
|
|
1503
1553
|
}
|
|
1504
1554
|
const authString = req.headers[AuthorizationField] || req.headers[AuthorizationField.toLowerCase()];
|
|
@@ -1512,12 +1562,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1512
1562
|
url: req.url || "",
|
|
1513
1563
|
data: body
|
|
1514
1564
|
})) {
|
|
1515
|
-
responseError(res, 401 /* HttpStatus.Unauthorized */,
|
|
1565
|
+
responseError(res, 401 /* HttpStatus.Unauthorized */, GMError.InvalidSignature, "Signature failed");
|
|
1516
1566
|
return;
|
|
1517
1567
|
}
|
|
1518
1568
|
const gmRequest = parseJSON(body, isGMRequestBody);
|
|
1519
1569
|
if (!gmRequest) {
|
|
1520
|
-
responseError(res, 400 /* HttpStatus.BadRequest */,
|
|
1570
|
+
responseError(res, 400 /* HttpStatus.BadRequest */, GMError.InvalidRequest, `GM request body format error`);
|
|
1521
1571
|
return;
|
|
1522
1572
|
}
|
|
1523
1573
|
// 处理幂等key
|
|
@@ -1533,13 +1583,13 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1533
1583
|
const oldRecordStr = await storeHelper.setNX(gmRequest.idempotency_key, toString(currentRecord)).catch(e => e instanceof Error ? e : new Error(String(e)));
|
|
1534
1584
|
if (oldRecordStr instanceof Error) {
|
|
1535
1585
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) setNX error`, oldRecordStr);
|
|
1536
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1586
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, oldRecordStr.message);
|
|
1537
1587
|
return;
|
|
1538
1588
|
}
|
|
1539
1589
|
const oldRecord = oldRecordStr ? parseJSON(oldRecordStr, isIdempotencyRecord) : null;
|
|
1540
1590
|
if (!oldRecord && oldRecordStr) {
|
|
1541
1591
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) parse old idempotency record failed: ${oldRecordStr}`);
|
|
1542
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1592
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, "parse old idempotency record failed");
|
|
1543
1593
|
return;
|
|
1544
1594
|
}
|
|
1545
1595
|
// spell-checker:disable
|
|
@@ -1552,15 +1602,15 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1552
1602
|
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1553
1603
|
if (!isFirstRequest && oldRecord) {
|
|
1554
1604
|
if (!oldRecord.resp) {
|
|
1555
|
-
responseError(res, error2httpStatus[
|
|
1605
|
+
responseError(res, error2httpStatus[GMError.IdempotencyConflict], GMError.IdempotencyConflict, "previous request is not completed");
|
|
1556
1606
|
return;
|
|
1557
1607
|
}
|
|
1558
1608
|
if (oldRecord.cmd !== gmRequest.command) {
|
|
1559
|
-
responseError(res, error2httpStatus[
|
|
1609
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `cmd mismatch, expecting ${oldRecord.cmd}, got ${gmRequest.command}`);
|
|
1560
1610
|
return;
|
|
1561
1611
|
}
|
|
1562
1612
|
if (oldRecord.args !== currentRecord.args) {
|
|
1563
|
-
responseError(res, error2httpStatus[
|
|
1613
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `args mismatch, expecting ${oldRecord.args}, got ${currentRecord.args}`);
|
|
1564
1614
|
return;
|
|
1565
1615
|
}
|
|
1566
1616
|
oldResponse = oldRecord.resp;
|
|
@@ -1578,11 +1628,11 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1578
1628
|
}
|
|
1579
1629
|
// 游戏主动抛错不打印日志信息
|
|
1580
1630
|
if (result instanceof Error) {
|
|
1581
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1631
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, result.message);
|
|
1582
1632
|
return;
|
|
1583
1633
|
}
|
|
1584
1634
|
if (isGMErrorResponse(result)) {
|
|
1585
|
-
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error ||
|
|
1635
|
+
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error || GMError.InternalError, result.message);
|
|
1586
1636
|
return;
|
|
1587
1637
|
}
|
|
1588
1638
|
// 对游戏处理函数进一步兼容处理,允许返回一个预定义的错误码
|
|
@@ -1598,7 +1648,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1598
1648
|
responseJson(res, result != null ? result : {});
|
|
1599
1649
|
} catch (e) {
|
|
1600
1650
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1601
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1651
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1602
1652
|
}
|
|
1603
1653
|
};
|
|
1604
1654
|
}
|
|
@@ -1665,4 +1715,4 @@ function getGMMiddlewareForKoa(paths, config, handler, storeHelper) {
|
|
|
1665
1715
|
};
|
|
1666
1716
|
}
|
|
1667
1717
|
|
|
1668
|
-
export { ApiClient, Endpoint, IdP, MemoryIdempotencyStore, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|
|
1718
|
+
export { ApiClient, Endpoint, GMError, IdP, MemoryIdempotencyStore, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
type SecretKey = string | Buffer;
|
|
4
|
+
/**
|
|
5
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
6
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
7
|
+
*/
|
|
8
|
+
export declare function decryptAESGCM(sk: SecretKey, encoded: string): string;
|
|
9
|
+
export {};
|