mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-04-29 23:05:55 +08:00
tweaks (#1788)
* tweak * opt: show bar * opt: crc32 * opt: appsign * opt: Get * opt: compress only if large * opt: wbi * tweak Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me> --------- Signed-off-by: My-Responsitories <107370289+My-Responsitories@users.noreply.github.com> Co-authored-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
committed by
GitHub
parent
31e5692dff
commit
521ca3ad18
@@ -77,8 +77,6 @@ class AccountManager extends Interceptor {
|
||||
if (!account.accessKey.isNullOrEmpty) {
|
||||
dataPtr['access_key'] = account.accessKey!;
|
||||
}
|
||||
dataPtr['ts'] ??= (DateTime.now().millisecondsSinceEpoch ~/ 1000)
|
||||
.toString();
|
||||
AppSign.appSign(dataPtr);
|
||||
// if (kDebugMode) debugPrint(dataPtr.toString());
|
||||
}
|
||||
|
||||
@@ -9,10 +9,14 @@ abstract final class AppSign {
|
||||
String appkey = Constants.appKey,
|
||||
String appsec = Constants.appSec,
|
||||
}) {
|
||||
params['appkey'] = appkey;
|
||||
final sorted = Map.fromEntries(
|
||||
params.entries.toList()..sort((a, b) => a.key.compareTo(b.key)),
|
||||
assert(
|
||||
params['appkey'] == null,
|
||||
'appkey-appsec should be provided in appSign',
|
||||
);
|
||||
params['appkey'] = appkey;
|
||||
params['ts'] ??= (DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
|
||||
final sorted = params.entries.toList()
|
||||
..sort((a, b) => a.key.compareTo(b.key));
|
||||
params['sign'] = md5
|
||||
.convert(utf8.encode(_makeQueryFromParametersDefault(sorted) + appsec))
|
||||
.toString(); // 获取MD5哈希值
|
||||
@@ -20,12 +24,14 @@ abstract final class AppSign {
|
||||
|
||||
/// from [Uri]
|
||||
static String _makeQueryFromParametersDefault(
|
||||
Map<String, dynamic /*String?|Iterable<String>*/> queryParameters,
|
||||
List<MapEntry<String, dynamic /*String?|Iterable<String>*/>>
|
||||
queryParameters,
|
||||
) {
|
||||
var result = StringBuffer();
|
||||
var separator = '';
|
||||
|
||||
void writeParameter(String key, String? value) {
|
||||
assert(value != null, 'remove null value');
|
||||
result.write(separator);
|
||||
separator = '&';
|
||||
result.write(Uri.encodeQueryComponent(key));
|
||||
@@ -36,15 +42,15 @@ abstract final class AppSign {
|
||||
}
|
||||
}
|
||||
|
||||
queryParameters.forEach((key, value) {
|
||||
if (value case Iterable<String> values) {
|
||||
for (var i in queryParameters) {
|
||||
if (i.value case Iterable<String> values) {
|
||||
for (final String value in values) {
|
||||
writeParameter(key, value);
|
||||
writeParameter(i.key, value);
|
||||
}
|
||||
} else {
|
||||
writeParameter(key, value?.toString());
|
||||
writeParameter(i.key, i.value?.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ abstract final class IdUtils {
|
||||
return '';
|
||||
}
|
||||
|
||||
var midByte = utf8.encode(uid.toString());
|
||||
var midByte = ascii.encode(uid.toString());
|
||||
|
||||
const key = 'ad1va46a7lza';
|
||||
for (int i = 0; i < midByte.length; i++) {
|
||||
|
||||
@@ -11,13 +11,11 @@ import 'package:PiliPlus/utils/storage_key.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:synchronized/synchronized.dart';
|
||||
|
||||
abstract final class WbiSign {
|
||||
static Box localCache = GStorage.localCache;
|
||||
static final Lock lock = Lock();
|
||||
static final RegExp chrFilter = RegExp(r"[!\'\(\)\*]");
|
||||
static const mixinKeyEncTab = <int>[
|
||||
static Box get _localCache => GStorage.localCache;
|
||||
static final RegExp _chrFilter = RegExp(r"[!\'\(\)\*]");
|
||||
static const _mixinKeyEncTab = <int>[
|
||||
46,
|
||||
47,
|
||||
18,
|
||||
@@ -52,20 +50,23 @@ abstract final class WbiSign {
|
||||
13,
|
||||
];
|
||||
|
||||
static Future<String>? _future;
|
||||
|
||||
// 对 imgKey 和 subKey 进行字符顺序打乱编码
|
||||
static String getMixinKey(String orig) {
|
||||
return mixinKeyEncTab.map((i) => orig[i]).join();
|
||||
final codeUnits = orig.codeUnits;
|
||||
return String.fromCharCodes(_mixinKeyEncTab.map((i) => codeUnits[i]));
|
||||
}
|
||||
|
||||
// 为请求参数进行 wbi 签名
|
||||
static void encWbi(Map<String, dynamic> params, String mixinKey) {
|
||||
static void encWbi(Map<String, Object> params, String mixinKey) {
|
||||
params['wts'] = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
// 按照 key 重排参数
|
||||
final List<String> keys = params.keys.toList()..sort();
|
||||
final queryStr = keys
|
||||
.map(
|
||||
(i) =>
|
||||
'${Uri.encodeComponent(i)}=${Uri.encodeComponent(params[i].toString().replaceAll(chrFilter, ''))}',
|
||||
'${Uri.encodeComponent(i)}=${Uri.encodeComponent(params[i].toString().replaceAll(_chrFilter, ''))}',
|
||||
)
|
||||
.join('&');
|
||||
params['w_rid'] = md5
|
||||
@@ -73,30 +74,19 @@ abstract final class WbiSign {
|
||||
.toString(); // 计算 w_rid
|
||||
}
|
||||
|
||||
// 获取最新的 img_key 和 sub_key 可以从缓存中获取
|
||||
static Future<String> getWbiKeys() async {
|
||||
final DateTime nowDate = DateTime.now();
|
||||
String? mixinKey = localCache.get(LocalCacheKey.mixinKey);
|
||||
if (mixinKey != null &&
|
||||
DateTime.fromMillisecondsSinceEpoch(
|
||||
localCache.get(LocalCacheKey.timeStamp) as int,
|
||||
).day ==
|
||||
nowDate.day) {
|
||||
return mixinKey;
|
||||
}
|
||||
static Future<String> _getWbiKeys(DateTime nowDate) async {
|
||||
final resp = await Request().get(Api.userInfo);
|
||||
|
||||
try {
|
||||
final wbiUrls = resp.data['data']['wbi_img'];
|
||||
|
||||
mixinKey = getMixinKey(
|
||||
final mixinKey = getMixinKey(
|
||||
Utils.getFileName(wbiUrls['img_url'], fileExt: false) +
|
||||
Utils.getFileName(wbiUrls['sub_url'], fileExt: false),
|
||||
);
|
||||
|
||||
localCache
|
||||
_localCache
|
||||
..put(LocalCacheKey.mixinKey, mixinKey)
|
||||
..put(LocalCacheKey.timeStamp, nowDate.millisecondsSinceEpoch);
|
||||
..put(LocalCacheKey.timeStamp, DateTime.now().millisecondsSinceEpoch);
|
||||
|
||||
return mixinKey;
|
||||
} catch (_) {
|
||||
@@ -104,11 +94,25 @@ abstract final class WbiSign {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> makSign(
|
||||
Map<String, dynamic> params,
|
||||
static Future<String> getWbiKeys() async {
|
||||
final DateTime nowDate = DateTime.now();
|
||||
if (DateTime.fromMillisecondsSinceEpoch(
|
||||
_localCache.get(LocalCacheKey.timeStamp, defaultValue: 0) as int,
|
||||
).day ==
|
||||
nowDate.day) {
|
||||
final String? mixinKey = _localCache.get(LocalCacheKey.mixinKey);
|
||||
if (mixinKey != null) return mixinKey;
|
||||
return _future ??= _getWbiKeys(nowDate);
|
||||
} else {
|
||||
return _future = _getWbiKeys(nowDate);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, Object>> makSign(
|
||||
Map<String, Object> params,
|
||||
) async {
|
||||
// params 为需要加密的请求参数
|
||||
final String mixinKey = await lock.synchronized(getWbiKeys);
|
||||
final String mixinKey = await getWbiKeys();
|
||||
encWbi(params, mixinKey);
|
||||
return params;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user