mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-22 01:56:47 +08:00
remove duplicate code (#312)
* remove duplicate code * Update login.dart
This commit is contained in:
committed by
GitHub
parent
192cd60a4f
commit
9e242fb902
@@ -1,4 +1,3 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
@@ -14,14 +13,12 @@ import 'package:PiliPlus/pages/dynamics/tab/controller.dart';
|
||||
import 'package:PiliPlus/pages/live/controller.dart';
|
||||
import 'package:PiliPlus/pages/main/controller.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:PiliPlus/pages/dynamics/index.dart';
|
||||
import 'package:PiliPlus/pages/home/index.dart';
|
||||
import 'package:PiliPlus/pages/media/index.dart';
|
||||
import 'package:PiliPlus/pages/mine/index.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:webview_cookie_manager/webview_cookie_manager.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart' as web;
|
||||
import 'package:PiliPlus/pages/dynamics/controller.dart';
|
||||
@@ -31,14 +28,16 @@ import 'package:PiliPlus/pages/mine/controller.dart';
|
||||
import 'package:PiliPlus/http/user.dart';
|
||||
|
||||
class LoginUtils {
|
||||
static Future onLogin(Map<String, dynamic> token_info, cookie_info) async {
|
||||
static final random = Random();
|
||||
|
||||
static Future onLogin(Map<String, dynamic> tokenInfo, jsonCookieInfo) async {
|
||||
try {
|
||||
GStorage.localCache.put(LocalCacheKey.accessKey, {
|
||||
'mid': token_info['mid'],
|
||||
'value': token_info['access_token'] ?? token_info['value'],
|
||||
'refresh': token_info['refresh_token'] ?? token_info['refresh']
|
||||
'mid': tokenInfo['mid'],
|
||||
'value': tokenInfo['access_token'] ?? tokenInfo['value'],
|
||||
'refresh': tokenInfo['refresh_token'] ?? tokenInfo['refresh']
|
||||
});
|
||||
List<dynamic> cookieInfo = cookie_info['cookies'];
|
||||
List<dynamic> cookieInfo = jsonCookieInfo['cookies'];
|
||||
List<Cookie> cookies = [];
|
||||
String cookieStrings = cookieInfo.map((cookie) {
|
||||
String cstr =
|
||||
@@ -198,31 +197,25 @@ class LoginUtils {
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static String buvid() {
|
||||
var mac = <String>[];
|
||||
var random = Random();
|
||||
|
||||
for (var i = 0; i < 6; i++) {
|
||||
var min = 0;
|
||||
var max = 0xff;
|
||||
var num = (random.nextInt(max - min + 1) + min).toRadixString(16);
|
||||
mac.add(num);
|
||||
}
|
||||
|
||||
var md5Str = md5.convert(utf8.encode(mac.join(':'))).toString();
|
||||
var md5Arr = md5Str.split('');
|
||||
return 'XY${md5Arr[2]}${md5Arr[12]}${md5Arr[22]}$md5Str';
|
||||
}
|
||||
|
||||
static String getUUID() {
|
||||
return const Uuid().v4().replaceAll('-', '');
|
||||
}
|
||||
|
||||
static String generateBuvid() {
|
||||
String uuid = getUUID() + getUUID();
|
||||
return 'XY${uuid.substring(0, 35).toUpperCase()}';
|
||||
var md5Str =
|
||||
Iterable.generate(32, (_) => random.nextInt(16).toRadixString(16))
|
||||
.join()
|
||||
.toUpperCase();
|
||||
return 'XY${md5Str[2]}${md5Str[12]}${md5Str[22]}$md5Str';
|
||||
}
|
||||
|
||||
static final buvid = generateBuvid();
|
||||
|
||||
// static String getUUID() {
|
||||
// return const Uuid().v4().replaceAll('-', '');
|
||||
// }
|
||||
|
||||
// static String generateBuvid() {
|
||||
// String uuid = getUUID() + getUUID();
|
||||
// return 'XY${uuid.substring(0, 35).toUpperCase()}';
|
||||
// }
|
||||
|
||||
static String genDeviceId() {
|
||||
// https://github.com/bilive/bilive_client/blob/2873de0532c54832f5464a4c57325ad9af8b8698/bilive/lib/app_client.ts#L62
|
||||
final String yyyyMMddHHmmss = DateTime.now()
|
||||
@@ -230,7 +223,6 @@ class LoginUtils {
|
||||
.replaceAll(RegExp(r'[-:TZ]'), '')
|
||||
.substring(0, 14);
|
||||
|
||||
final Random random = Random(); // Random.secure();
|
||||
final String randomHex32 =
|
||||
List.generate(32, (index) => random.nextInt(16).toRadixString(16))
|
||||
.join();
|
||||
@@ -251,12 +243,4 @@ class LoginUtils {
|
||||
|
||||
return deviceID + check;
|
||||
}
|
||||
|
||||
static String generateRandomString(int length) {
|
||||
const chars =
|
||||
'123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
final Random random = Random(); // Random.secure();
|
||||
return List.generate(length, (index) => chars[random.nextInt(chars.length)])
|
||||
.join();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -765,7 +765,6 @@ class Utils {
|
||||
|
||||
static String generateRandomString(int length) {
|
||||
const characters = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
Random random = Random();
|
||||
|
||||
return String.fromCharCodes(Iterable.generate(length,
|
||||
(_) => characters.codeUnitAt(random.nextInt(characters.length))));
|
||||
@@ -777,66 +776,43 @@ class Utils {
|
||||
}
|
||||
|
||||
// 1. Convert UID to a byte array.
|
||||
List<int> midByte = utf8.encode(uid.toString());
|
||||
List<int> resultByte = List<int>.filled(midByte.length, 0);
|
||||
var midByte = utf8.encode(uid.toString());
|
||||
|
||||
// 2. XOR each byte with the corresponding byte from the key.
|
||||
const key = 'ad1va46a7lza';
|
||||
for (int i = 0; i < midByte.length; i++) {
|
||||
resultByte[i] = midByte[i] ^ key.codeUnitAt(i % key.length);
|
||||
midByte[i] ^= key.codeUnitAt(i % key.length);
|
||||
}
|
||||
|
||||
// 3. Perform Base64 encoding without padding.
|
||||
String base64Encoded =
|
||||
base64.encode(resultByte).replaceAll('=', ''); // Remove padding
|
||||
base64.encode(midByte).replaceAll('=', ''); // Remove padding
|
||||
|
||||
// Return the resulting x-bili-aurora-eid.
|
||||
return base64Encoded;
|
||||
}
|
||||
|
||||
static String genRandomString(int length) {
|
||||
const characters = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
Random random = Random();
|
||||
return List.generate(
|
||||
length, (index) => characters[random.nextInt(characters.length)])
|
||||
.join();
|
||||
}
|
||||
// static String genRandomString(int length) {
|
||||
// const characters = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
// Random random = Random();
|
||||
// return List.generate(
|
||||
// length, (index) => characters[random.nextInt(characters.length)])
|
||||
// .join();
|
||||
// }
|
||||
|
||||
static String genTraceId() {
|
||||
// 1. Generate a 32-character random string (random_id).
|
||||
String randomId = genRandomString(32);
|
||||
String randomId = generateRandomString(32);
|
||||
|
||||
// 2. Take the first 24 characters of random_id as random_trace_id.
|
||||
StringBuffer randomTraceId = StringBuffer(randomId.substring(0, 24));
|
||||
|
||||
// 3. Initialize an array b_arr with a length of 3, initial values are 0.
|
||||
List<int> bArr = List.filled(3, 0);
|
||||
|
||||
// Get the current timestamp.
|
||||
int ts = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
|
||||
// Using a loop to traverse b_arr from high to low.
|
||||
for (int i = 2; i >= 0; i--) {
|
||||
ts >>= 8; // Right shift ts by 8 bits.
|
||||
bArr[i] = (ts ~/ 128) % 2 == 0
|
||||
? (ts % 256)
|
||||
: (ts % 256) - 256; // Assign value based on condition.
|
||||
}
|
||||
|
||||
// 4. Convert each element in b_arr to a two-digit hexadecimal string and append to random_trace_id.
|
||||
for (int value in bArr) {
|
||||
randomTraceId
|
||||
.write(value.toRadixString(16).padLeft(2, '0')); // Convert to hex.
|
||||
.write(random.nextInt(256).toRadixString(16).padLeft(2, '0'));
|
||||
}
|
||||
|
||||
// 5. Append the 31st and 32nd characters of random_id to random_trace_id.
|
||||
randomTraceId.write(randomId.substring(30, 32));
|
||||
|
||||
// 6. Finally, concatenate as '{random_trace_id}:{random_trace_id[16..32]}:0:0'.
|
||||
String randomTraceIdFinal =
|
||||
'${randomTraceId.toString()}:${randomTraceId.toString().substring(16, 32)}:0:0';
|
||||
|
||||
return randomTraceIdFinal;
|
||||
return '${randomTraceId.toString()}:${randomTraceId.toString().substring(16, 32)}:0:0';
|
||||
}
|
||||
|
||||
static void viewBangumi({
|
||||
|
||||
Reference in New Issue
Block a user