Merge branch 'main' into feature-cookies

This commit is contained in:
guozhigq
2024-12-01 21:13:49 +08:00
168 changed files with 2481 additions and 1772 deletions

View File

@ -1,9 +1,6 @@
// ignore_for_file: avoid_print
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:math' show Random;
import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
@ -12,7 +9,6 @@ import 'package:hive/hive.dart';
import 'package:pilipala/utils/id_utils.dart';
import '../utils/storage.dart';
import '../utils/utils.dart';
import 'api.dart';
import 'constants.dart';
import 'interceptor.dart';
@ -21,19 +17,17 @@ class Request {
static late CookieManager cookieManager;
static late final Dio dio;
factory Request() => _instance;
Box setting = GStrorage.setting;
static Box localCache = GStrorage.localCache;
Box setting = GStorage.setting;
static Box localCache = GStorage.localCache;
late bool enableSystemProxy;
late String systemProxyHost;
late String systemProxyPort;
static final RegExp spmPrefixExp =
RegExp(r'<meta name="spm_prefix" content="([^"]+?)">');
static String? buvid;
/// 设置cookie
static setCookie() async {
Box userInfoCache = GStrorage.userInfo;
Box setting = GStrorage.setting;
Box userInfoCache = GStorage.userInfo;
Box setting = GStorage.setting;
final String cookiePath = await Utils.getCookiePath();
final PersistCookieJar cookieJar = PersistCookieJar(
ignoreExpires: true,
@ -50,11 +44,6 @@ class Request {
baseUrlType = 'bangumi';
}
setBaseUrl(type: baseUrlType);
try {
await buvidActivate();
} catch (e) {
log("setCookie, ${e.toString()}");
}
final String cookieString = cookie
.map((Cookie cookie) => '${cookie.name}=${cookie.value}')
@ -80,9 +69,12 @@ class Request {
}
final List<Cookie> cookies = await cookieManager.cookieJar
.loadForRequest(Uri.parse(HttpString.baseUrl));
buvid = cookies.firstWhere((cookie) => cookie.name == 'buvid3').value;
if (buvid == null) {
.loadForRequest(Uri.parse(HttpString.apiBaseUrl));
buvid = cookies
.firstWhere((cookie) => cookie.name == 'buvid3',
orElse: () => Cookie('buvid3', ''))
.value;
if (buvid == null || buvid!.isEmpty) {
try {
var result = await Request().get(
"${HttpString.apiBaseUrl}/x/frontend/finger/spi",
@ -110,30 +102,6 @@ class Request {
dio.options.headers['referer'] = 'https://www.bilibili.com/';
}
static Future buvidActivate() async {
var html = await Request().get(Api.dynamicSpmPrefix);
String spmPrefix = spmPrefixExp.firstMatch(html.data)!.group(1)!;
Random rand = Random();
String rand_png_end = base64.encode(
List<int>.generate(32, (_) => rand.nextInt(256)) +
List<int>.filled(4, 0) +
[73, 69, 78, 68] +
List<int>.generate(4, (_) => rand.nextInt(256)));
String jsonData = json.encode({
'3064': 1,
'39c8': '${spmPrefix}.fp.risk',
'3c43': {
'adca': 'Linux',
'bfe9': rand_png_end.substring(rand_png_end.length - 50),
},
});
await Request().post(Api.activateBuvidApi,
data: {'payload': jsonData},
options: Options(contentType: 'application/json'));
}
/*
* config it and create
*/
@ -198,17 +166,13 @@ class Request {
*/
get(url, {data, Options? options, cancelToken, extra}) async {
Response response;
options ??= Options(); // 如果 options 为 null则初始化一个新的 Options 对象
ResponseType resType = ResponseType.json;
if (extra != null) {
resType = extra['resType'] ?? ResponseType.json;
if (extra['ua'] != null) {
options.headers = {'user-agent': headerUa(type: extra['ua'])};
options ??= Options();
options.headers ??= <String, dynamic>{};
options.headers?['user-agent'] = headerUa(type: extra['ua']);
}
}
options.responseType = resType;
try {
response = await dio.get(
url,