@seayoo-io/combo-sdk-node 1.5.0 → 1.5.1
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/const.d.ts +1 -1
- package/dist/index.cjs +72 -30
- package/dist/index.js +72 -30
- package/dist/utils/gcm.d.ts +9 -0
- package/package.json +1 -1
package/dist/const.d.ts
CHANGED
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.1";
|
|
16
16
|
/**
|
|
17
17
|
* 世游支持的服务端点列表
|
|
18
18
|
*/
|
|
@@ -98,7 +98,7 @@ exports.IdP = void 0;
|
|
|
98
98
|
IdP["Huya"] = "huya";
|
|
99
99
|
})(exports.IdP || (exports.IdP = {}));
|
|
100
100
|
/** GM 错误类型枚举 */
|
|
101
|
-
|
|
101
|
+
exports.GMError = void 0;
|
|
102
102
|
(function (GMError) {
|
|
103
103
|
/** 请求中的 HTTP method 不正确,没有按照预期使用 POST。*/
|
|
104
104
|
GMError["InvalidHttpMethod"] = "invalid_http_method";
|
|
@@ -128,7 +128,7 @@ var GMError;
|
|
|
128
128
|
GMError["TimeoutError"] = "timeout_error";
|
|
129
129
|
/** 处理 GM 命令时内部出错。可作为兜底的通用错误类型。*/
|
|
130
130
|
GMError["InternalError"] = "internal_error";
|
|
131
|
-
})(GMError || (GMError = {}));
|
|
131
|
+
})(exports.GMError || (exports.GMError = {}));
|
|
132
132
|
|
|
133
133
|
function getUserAgent(game) {
|
|
134
134
|
return `${SDKName}/${SDKVersion} game/${game} node/${process.version} os/${os.platform()} arch/${os.arch()}`;
|
|
@@ -1168,6 +1168,47 @@ function isAdJwtPayload(payload) {
|
|
|
1168
1168
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "placement_id" in payload && "impression_id" in payload;
|
|
1169
1169
|
}
|
|
1170
1170
|
|
|
1171
|
+
/**
|
|
1172
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
1173
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
1174
|
+
*/
|
|
1175
|
+
function decryptAESGCM(sk, encoded) {
|
|
1176
|
+
let data;
|
|
1177
|
+
try {
|
|
1178
|
+
data = Buffer.from(encoded, "base64");
|
|
1179
|
+
} catch (err) {
|
|
1180
|
+
throw new Error(`base64 decode error: ${err}`);
|
|
1181
|
+
}
|
|
1182
|
+
const key = deriveAESKey(sk);
|
|
1183
|
+
const nonceSize = 12;
|
|
1184
|
+
const authTagSize = 16;
|
|
1185
|
+
if (data.length < nonceSize + authTagSize) {
|
|
1186
|
+
throw new Error("ciphertext too short");
|
|
1187
|
+
}
|
|
1188
|
+
const nonce = data.slice(0, nonceSize);
|
|
1189
|
+
// 密文格式: nonce (12) || ciphertext (variable) || authTag (16)
|
|
1190
|
+
const authTag = data.slice(data.length - authTagSize);
|
|
1191
|
+
const ciphertext = data.slice(nonceSize, data.length - authTagSize);
|
|
1192
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, nonce);
|
|
1193
|
+
decipher.setAuthTag(authTag);
|
|
1194
|
+
// Node.js 12 兼容:使用 update 处理所有数据,避免 final() 的问题
|
|
1195
|
+
let plaintext;
|
|
1196
|
+
try {
|
|
1197
|
+
plaintext = decipher.update(ciphertext);
|
|
1198
|
+
// Node.js 12 中 GCM 模式下 final() 通常返回空 Buffer,但会验证 authTag
|
|
1199
|
+
const finalBuffer = decipher.final();
|
|
1200
|
+
if (finalBuffer.length > 0) {
|
|
1201
|
+
plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1202
|
+
}
|
|
1203
|
+
} catch (err) {
|
|
1204
|
+
throw new Error(`gcm decrypt error: ${err}`);
|
|
1205
|
+
}
|
|
1206
|
+
return plaintext.toString("utf8");
|
|
1207
|
+
}
|
|
1208
|
+
function deriveAESKey(sk) {
|
|
1209
|
+
return crypto.createHash("sha256").update(sk).digest();
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1171
1212
|
const IdentityTokenScope = "auth";
|
|
1172
1213
|
const AdTokenScope = "ads";
|
|
1173
1214
|
/**
|
|
@@ -1204,12 +1245,13 @@ class TokenVerifier {
|
|
|
1204
1245
|
if (payload.scope !== IdentityTokenScope) {
|
|
1205
1246
|
return new Error("verifyIdentityToken: 无效的 Scope");
|
|
1206
1247
|
}
|
|
1248
|
+
// spell-checker:ignore AESGCM
|
|
1207
1249
|
return {
|
|
1208
1250
|
combo_id: payload.sub,
|
|
1209
1251
|
idp: payload.idp,
|
|
1210
1252
|
external_id: payload.external_id,
|
|
1211
1253
|
external_name: payload.external_name,
|
|
1212
|
-
weixin_session_key: payload.weixin_session_key
|
|
1254
|
+
weixin_session_key: payload.weixin_session_key ? decryptAESGCM(this.privateKey, payload.weixin_session_key) : "",
|
|
1213
1255
|
distro: payload.distro || "",
|
|
1214
1256
|
variant: payload.variant || "",
|
|
1215
1257
|
device_id: payload.device_id || "",
|
|
@@ -1471,20 +1513,20 @@ class RedisIdempotencyStore {
|
|
|
1471
1513
|
}
|
|
1472
1514
|
|
|
1473
1515
|
const error2httpStatus = {
|
|
1474
|
-
[
|
|
1475
|
-
[
|
|
1476
|
-
[
|
|
1477
|
-
[
|
|
1478
|
-
[
|
|
1479
|
-
[
|
|
1480
|
-
[
|
|
1481
|
-
[
|
|
1482
|
-
[
|
|
1483
|
-
[
|
|
1484
|
-
[
|
|
1485
|
-
[
|
|
1486
|
-
[
|
|
1487
|
-
[
|
|
1516
|
+
[exports.GMError.InvalidHttpMethod]: 405 /* HttpStatus.MethodNotAllowed */,
|
|
1517
|
+
[exports.GMError.InvalidContentType]: 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1518
|
+
[exports.GMError.InvalidSignature]: 401 /* HttpStatus.Unauthorized */,
|
|
1519
|
+
[exports.GMError.InvalidRequest]: 400 /* HttpStatus.BadRequest */,
|
|
1520
|
+
[exports.GMError.InvalidCommand]: 400 /* HttpStatus.BadRequest */,
|
|
1521
|
+
[exports.GMError.InvalidArgs]: 400 /* HttpStatus.BadRequest */,
|
|
1522
|
+
[exports.GMError.NetworkError]: 500 /* HttpStatus.InternalServerError */,
|
|
1523
|
+
[exports.GMError.DatabaseError]: 500 /* HttpStatus.InternalServerError */,
|
|
1524
|
+
[exports.GMError.IdempotencyConflict]: 409 /* HttpStatus.Conflict */,
|
|
1525
|
+
[exports.GMError.IdempotencyMismatch]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1526
|
+
[exports.GMError.TimeoutError]: 500 /* HttpStatus.InternalServerError */,
|
|
1527
|
+
[exports.GMError.MaintenanceError]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1528
|
+
[exports.GMError.ThrottlingError]: 429 /* HttpStatus.TooManyRequests */,
|
|
1529
|
+
[exports.GMError.InternalError]: 500 /* HttpStatus.InternalServerError */
|
|
1488
1530
|
};
|
|
1489
1531
|
/**
|
|
1490
1532
|
* 获取一个处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知,storeHelper 提供存储支持以实现幂等key(idempotencyKey)的自动化处理
|
|
@@ -1498,12 +1540,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1498
1540
|
verifyStoreHelper(storeHelper);
|
|
1499
1541
|
return async function (req, res, rawBodyString) {
|
|
1500
1542
|
if (req.method !== "POST") {
|
|
1501
|
-
responseError(res, 405 /* HttpStatus.MethodNotAllowed */,
|
|
1543
|
+
responseError(res, 405 /* HttpStatus.MethodNotAllowed */, exports.GMError.InvalidHttpMethod, `Expecting POST, got ${req.method}`);
|
|
1502
1544
|
return;
|
|
1503
1545
|
}
|
|
1504
1546
|
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1505
1547
|
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1506
|
-
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1548
|
+
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */, exports.GMError.InvalidContentType, `Expecting application/json, got ${contentType}`);
|
|
1507
1549
|
return;
|
|
1508
1550
|
}
|
|
1509
1551
|
const authString = req.headers[AuthorizationField] || req.headers[AuthorizationField.toLowerCase()];
|
|
@@ -1517,12 +1559,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1517
1559
|
url: req.url || "",
|
|
1518
1560
|
data: body
|
|
1519
1561
|
})) {
|
|
1520
|
-
responseError(res, 401 /* HttpStatus.Unauthorized */,
|
|
1562
|
+
responseError(res, 401 /* HttpStatus.Unauthorized */, exports.GMError.InvalidSignature, "Signature failed");
|
|
1521
1563
|
return;
|
|
1522
1564
|
}
|
|
1523
1565
|
const gmRequest = parseJSON(body, isGMRequestBody);
|
|
1524
1566
|
if (!gmRequest) {
|
|
1525
|
-
responseError(res, 400 /* HttpStatus.BadRequest */,
|
|
1567
|
+
responseError(res, 400 /* HttpStatus.BadRequest */, exports.GMError.InvalidRequest, `GM request body format error`);
|
|
1526
1568
|
return;
|
|
1527
1569
|
}
|
|
1528
1570
|
// 处理幂等key
|
|
@@ -1538,13 +1580,13 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1538
1580
|
const oldRecordStr = await storeHelper.setNX(gmRequest.idempotency_key, toString(currentRecord)).catch(e => e instanceof Error ? e : new Error(String(e)));
|
|
1539
1581
|
if (oldRecordStr instanceof Error) {
|
|
1540
1582
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) setNX error`, oldRecordStr);
|
|
1541
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1583
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, oldRecordStr.message);
|
|
1542
1584
|
return;
|
|
1543
1585
|
}
|
|
1544
1586
|
const oldRecord = oldRecordStr ? parseJSON(oldRecordStr, isIdempotencyRecord) : null;
|
|
1545
1587
|
if (!oldRecord && oldRecordStr) {
|
|
1546
1588
|
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 */,
|
|
1589
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, "parse old idempotency record failed");
|
|
1548
1590
|
return;
|
|
1549
1591
|
}
|
|
1550
1592
|
// spell-checker:disable
|
|
@@ -1557,15 +1599,15 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1557
1599
|
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1558
1600
|
if (!isFirstRequest && oldRecord) {
|
|
1559
1601
|
if (!oldRecord.resp) {
|
|
1560
|
-
responseError(res, error2httpStatus[
|
|
1602
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyConflict], exports.GMError.IdempotencyConflict, "previous request is not completed");
|
|
1561
1603
|
return;
|
|
1562
1604
|
}
|
|
1563
1605
|
if (oldRecord.cmd !== gmRequest.command) {
|
|
1564
|
-
responseError(res, error2httpStatus[
|
|
1606
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyMismatch], exports.GMError.IdempotencyMismatch, `cmd mismatch, expecting ${oldRecord.cmd}, got ${gmRequest.command}`);
|
|
1565
1607
|
return;
|
|
1566
1608
|
}
|
|
1567
1609
|
if (oldRecord.args !== currentRecord.args) {
|
|
1568
|
-
responseError(res, error2httpStatus[
|
|
1610
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyMismatch], exports.GMError.IdempotencyMismatch, `args mismatch, expecting ${oldRecord.args}, got ${currentRecord.args}`);
|
|
1569
1611
|
return;
|
|
1570
1612
|
}
|
|
1571
1613
|
oldResponse = oldRecord.resp;
|
|
@@ -1583,11 +1625,11 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1583
1625
|
}
|
|
1584
1626
|
// 游戏主动抛错不打印日志信息
|
|
1585
1627
|
if (result instanceof Error) {
|
|
1586
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1628
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, result.message);
|
|
1587
1629
|
return;
|
|
1588
1630
|
}
|
|
1589
1631
|
if (isGMErrorResponse(result)) {
|
|
1590
|
-
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error ||
|
|
1632
|
+
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error || exports.GMError.InternalError, result.message);
|
|
1591
1633
|
return;
|
|
1592
1634
|
}
|
|
1593
1635
|
// 对游戏处理函数进一步兼容处理,允许返回一个预定义的错误码
|
|
@@ -1603,7 +1645,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1603
1645
|
responseJson(res, result != null ? result : {});
|
|
1604
1646
|
} catch (e) {
|
|
1605
1647
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1606
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1648
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1607
1649
|
}
|
|
1608
1650
|
};
|
|
1609
1651
|
}
|
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.1";
|
|
11
11
|
/**
|
|
12
12
|
* 世游支持的服务端点列表
|
|
13
13
|
*/
|
|
@@ -1163,6 +1163,47 @@ function isAdJwtPayload(payload) {
|
|
|
1163
1163
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "placement_id" in payload && "impression_id" in payload;
|
|
1164
1164
|
}
|
|
1165
1165
|
|
|
1166
|
+
/**
|
|
1167
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
1168
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
1169
|
+
*/
|
|
1170
|
+
function decryptAESGCM(sk, encoded) {
|
|
1171
|
+
let data;
|
|
1172
|
+
try {
|
|
1173
|
+
data = Buffer.from(encoded, "base64");
|
|
1174
|
+
} catch (err) {
|
|
1175
|
+
throw new Error(`base64 decode error: ${err}`);
|
|
1176
|
+
}
|
|
1177
|
+
const key = deriveAESKey(sk);
|
|
1178
|
+
const nonceSize = 12;
|
|
1179
|
+
const authTagSize = 16;
|
|
1180
|
+
if (data.length < nonceSize + authTagSize) {
|
|
1181
|
+
throw new Error("ciphertext too short");
|
|
1182
|
+
}
|
|
1183
|
+
const nonce = data.slice(0, nonceSize);
|
|
1184
|
+
// 密文格式: nonce (12) || ciphertext (variable) || authTag (16)
|
|
1185
|
+
const authTag = data.slice(data.length - authTagSize);
|
|
1186
|
+
const ciphertext = data.slice(nonceSize, data.length - authTagSize);
|
|
1187
|
+
const decipher = createDecipheriv("aes-256-gcm", key, nonce);
|
|
1188
|
+
decipher.setAuthTag(authTag);
|
|
1189
|
+
// Node.js 12 兼容:使用 update 处理所有数据,避免 final() 的问题
|
|
1190
|
+
let plaintext;
|
|
1191
|
+
try {
|
|
1192
|
+
plaintext = decipher.update(ciphertext);
|
|
1193
|
+
// Node.js 12 中 GCM 模式下 final() 通常返回空 Buffer,但会验证 authTag
|
|
1194
|
+
const finalBuffer = decipher.final();
|
|
1195
|
+
if (finalBuffer.length > 0) {
|
|
1196
|
+
plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1197
|
+
}
|
|
1198
|
+
} catch (err) {
|
|
1199
|
+
throw new Error(`gcm decrypt error: ${err}`);
|
|
1200
|
+
}
|
|
1201
|
+
return plaintext.toString("utf8");
|
|
1202
|
+
}
|
|
1203
|
+
function deriveAESKey(sk) {
|
|
1204
|
+
return createHash("sha256").update(sk).digest();
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1166
1207
|
const IdentityTokenScope = "auth";
|
|
1167
1208
|
const AdTokenScope = "ads";
|
|
1168
1209
|
/**
|
|
@@ -1199,12 +1240,13 @@ class TokenVerifier {
|
|
|
1199
1240
|
if (payload.scope !== IdentityTokenScope) {
|
|
1200
1241
|
return new Error("verifyIdentityToken: 无效的 Scope");
|
|
1201
1242
|
}
|
|
1243
|
+
// spell-checker:ignore AESGCM
|
|
1202
1244
|
return {
|
|
1203
1245
|
combo_id: payload.sub,
|
|
1204
1246
|
idp: payload.idp,
|
|
1205
1247
|
external_id: payload.external_id,
|
|
1206
1248
|
external_name: payload.external_name,
|
|
1207
|
-
weixin_session_key: payload.weixin_session_key
|
|
1249
|
+
weixin_session_key: payload.weixin_session_key ? decryptAESGCM(this.privateKey, payload.weixin_session_key) : "",
|
|
1208
1250
|
distro: payload.distro || "",
|
|
1209
1251
|
variant: payload.variant || "",
|
|
1210
1252
|
device_id: payload.device_id || "",
|
|
@@ -1466,20 +1508,20 @@ class RedisIdempotencyStore {
|
|
|
1466
1508
|
}
|
|
1467
1509
|
|
|
1468
1510
|
const error2httpStatus = {
|
|
1469
|
-
[
|
|
1470
|
-
[
|
|
1471
|
-
[
|
|
1472
|
-
[
|
|
1473
|
-
[
|
|
1474
|
-
[
|
|
1475
|
-
[
|
|
1476
|
-
[
|
|
1477
|
-
[
|
|
1478
|
-
[
|
|
1479
|
-
[
|
|
1480
|
-
[
|
|
1481
|
-
[
|
|
1482
|
-
[
|
|
1511
|
+
[GMError.InvalidHttpMethod]: 405 /* HttpStatus.MethodNotAllowed */,
|
|
1512
|
+
[GMError.InvalidContentType]: 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1513
|
+
[GMError.InvalidSignature]: 401 /* HttpStatus.Unauthorized */,
|
|
1514
|
+
[GMError.InvalidRequest]: 400 /* HttpStatus.BadRequest */,
|
|
1515
|
+
[GMError.InvalidCommand]: 400 /* HttpStatus.BadRequest */,
|
|
1516
|
+
[GMError.InvalidArgs]: 400 /* HttpStatus.BadRequest */,
|
|
1517
|
+
[GMError.NetworkError]: 500 /* HttpStatus.InternalServerError */,
|
|
1518
|
+
[GMError.DatabaseError]: 500 /* HttpStatus.InternalServerError */,
|
|
1519
|
+
[GMError.IdempotencyConflict]: 409 /* HttpStatus.Conflict */,
|
|
1520
|
+
[GMError.IdempotencyMismatch]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1521
|
+
[GMError.TimeoutError]: 500 /* HttpStatus.InternalServerError */,
|
|
1522
|
+
[GMError.MaintenanceError]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1523
|
+
[GMError.ThrottlingError]: 429 /* HttpStatus.TooManyRequests */,
|
|
1524
|
+
[GMError.InternalError]: 500 /* HttpStatus.InternalServerError */
|
|
1483
1525
|
};
|
|
1484
1526
|
/**
|
|
1485
1527
|
* 获取一个处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知,storeHelper 提供存储支持以实现幂等key(idempotencyKey)的自动化处理
|
|
@@ -1493,12 +1535,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1493
1535
|
verifyStoreHelper(storeHelper);
|
|
1494
1536
|
return async function (req, res, rawBodyString) {
|
|
1495
1537
|
if (req.method !== "POST") {
|
|
1496
|
-
responseError(res, 405 /* HttpStatus.MethodNotAllowed */,
|
|
1538
|
+
responseError(res, 405 /* HttpStatus.MethodNotAllowed */, GMError.InvalidHttpMethod, `Expecting POST, got ${req.method}`);
|
|
1497
1539
|
return;
|
|
1498
1540
|
}
|
|
1499
1541
|
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1500
1542
|
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1501
|
-
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1543
|
+
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */, GMError.InvalidContentType, `Expecting application/json, got ${contentType}`);
|
|
1502
1544
|
return;
|
|
1503
1545
|
}
|
|
1504
1546
|
const authString = req.headers[AuthorizationField] || req.headers[AuthorizationField.toLowerCase()];
|
|
@@ -1512,12 +1554,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1512
1554
|
url: req.url || "",
|
|
1513
1555
|
data: body
|
|
1514
1556
|
})) {
|
|
1515
|
-
responseError(res, 401 /* HttpStatus.Unauthorized */,
|
|
1557
|
+
responseError(res, 401 /* HttpStatus.Unauthorized */, GMError.InvalidSignature, "Signature failed");
|
|
1516
1558
|
return;
|
|
1517
1559
|
}
|
|
1518
1560
|
const gmRequest = parseJSON(body, isGMRequestBody);
|
|
1519
1561
|
if (!gmRequest) {
|
|
1520
|
-
responseError(res, 400 /* HttpStatus.BadRequest */,
|
|
1562
|
+
responseError(res, 400 /* HttpStatus.BadRequest */, GMError.InvalidRequest, `GM request body format error`);
|
|
1521
1563
|
return;
|
|
1522
1564
|
}
|
|
1523
1565
|
// 处理幂等key
|
|
@@ -1533,13 +1575,13 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1533
1575
|
const oldRecordStr = await storeHelper.setNX(gmRequest.idempotency_key, toString(currentRecord)).catch(e => e instanceof Error ? e : new Error(String(e)));
|
|
1534
1576
|
if (oldRecordStr instanceof Error) {
|
|
1535
1577
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) setNX error`, oldRecordStr);
|
|
1536
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1578
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, oldRecordStr.message);
|
|
1537
1579
|
return;
|
|
1538
1580
|
}
|
|
1539
1581
|
const oldRecord = oldRecordStr ? parseJSON(oldRecordStr, isIdempotencyRecord) : null;
|
|
1540
1582
|
if (!oldRecord && oldRecordStr) {
|
|
1541
1583
|
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 */,
|
|
1584
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, "parse old idempotency record failed");
|
|
1543
1585
|
return;
|
|
1544
1586
|
}
|
|
1545
1587
|
// spell-checker:disable
|
|
@@ -1552,15 +1594,15 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1552
1594
|
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1553
1595
|
if (!isFirstRequest && oldRecord) {
|
|
1554
1596
|
if (!oldRecord.resp) {
|
|
1555
|
-
responseError(res, error2httpStatus[
|
|
1597
|
+
responseError(res, error2httpStatus[GMError.IdempotencyConflict], GMError.IdempotencyConflict, "previous request is not completed");
|
|
1556
1598
|
return;
|
|
1557
1599
|
}
|
|
1558
1600
|
if (oldRecord.cmd !== gmRequest.command) {
|
|
1559
|
-
responseError(res, error2httpStatus[
|
|
1601
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `cmd mismatch, expecting ${oldRecord.cmd}, got ${gmRequest.command}`);
|
|
1560
1602
|
return;
|
|
1561
1603
|
}
|
|
1562
1604
|
if (oldRecord.args !== currentRecord.args) {
|
|
1563
|
-
responseError(res, error2httpStatus[
|
|
1605
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `args mismatch, expecting ${oldRecord.args}, got ${currentRecord.args}`);
|
|
1564
1606
|
return;
|
|
1565
1607
|
}
|
|
1566
1608
|
oldResponse = oldRecord.resp;
|
|
@@ -1578,11 +1620,11 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1578
1620
|
}
|
|
1579
1621
|
// 游戏主动抛错不打印日志信息
|
|
1580
1622
|
if (result instanceof Error) {
|
|
1581
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1623
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, result.message);
|
|
1582
1624
|
return;
|
|
1583
1625
|
}
|
|
1584
1626
|
if (isGMErrorResponse(result)) {
|
|
1585
|
-
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error ||
|
|
1627
|
+
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error || GMError.InternalError, result.message);
|
|
1586
1628
|
return;
|
|
1587
1629
|
}
|
|
1588
1630
|
// 对游戏处理函数进一步兼容处理,允许返回一个预定义的错误码
|
|
@@ -1598,7 +1640,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1598
1640
|
responseJson(res, result != null ? result : {});
|
|
1599
1641
|
} catch (e) {
|
|
1600
1642
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1601
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1643
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1602
1644
|
}
|
|
1603
1645
|
};
|
|
1604
1646
|
}
|
|
@@ -1665,4 +1707,4 @@ function getGMMiddlewareForKoa(paths, config, handler, storeHelper) {
|
|
|
1665
1707
|
};
|
|
1666
1708
|
}
|
|
1667
1709
|
|
|
1668
|
-
export { ApiClient, Endpoint, IdP, MemoryIdempotencyStore, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|
|
1710
|
+
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 {};
|