优化:日志输出

This commit is contained in:
ivan 2026-03-26 14:46:50 +08:00
parent c6449734f9
commit 83c1c56c36
6 changed files with 81 additions and 19 deletions

View File

@ -1,4 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:math';
import 'package:encrypt/encrypt.dart'; import 'package:encrypt/encrypt.dart';
@ -28,18 +29,19 @@ class ApiCrypto {
} }
/// Base64 /// Base64
static String randomBase64([int byteLength = 16]) { static String randomBase64() {
final bytes = List<int>.generate( const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
byteLength, (_) => DateTime.now().millisecondsSinceEpoch % 256); final random = Random();
return base64Encode(bytes); final length = 4 + random.nextInt(5); // 4-8
final randomStr = List.generate(length, (_) => chars[random.nextInt(chars.length)]).join();
return base64Encode(utf8.encode(randomStr));
} }
/// 8 /// 8
static String randomAlnum() { static String randomAlnum() {
const chars = const chars =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
return List.generate(8, final random = Random();
(_) => chars[DateTime.now().microsecondsSinceEpoch % chars.length]) return List.generate(8, (_) => chars[random.nextInt(chars.length)]).join();
.join();
} }
} }

View File

@ -135,7 +135,7 @@ class ProxyClient {
final v2BodyEncoded = jsonEncode(v2Body); final v2BodyEncoded = jsonEncode(v2Body);
final logStr = final logStr =
'========== 原始入参 ===========\npath: $path\nmethod: $method\nqueryParams: $paramsEncoded\nbody(sanctum): ${jsonEncode(sanctum)}'; '========== 原始入参 ===========\npath: $path\nmethod: $method \nheaders: $headersEncoded\nqueryParams: $paramsEncoded\nbody(sanctum): ${jsonEncode(sanctum)}';
logWithEmbeddedJson(logStr); logWithEmbeddedJson(logStr);
final proxyBody = <String, dynamic>{ final proxyBody = <String, dynamic>{
@ -153,6 +153,7 @@ class ProxyClient {
final url = '$_baseUrl${config.proxyPath}'; final url = '$_baseUrl${config.proxyPath}';
logWithEmbeddedJson('========== 请求信息 ===========\n\n目标URL: $url');
logWithEmbeddedJson('========== 实际请求体 ===========\n${jsonEncode(proxyBody)}'); logWithEmbeddedJson('========== 实际请求体 ===========\n${jsonEncode(proxyBody)}');
final response = await http.post( final response = await http.post(
Uri.parse(url), Uri.parse(url),

View File

@ -54,6 +54,7 @@ abstract class AttributionCallbacks {
Future<String?> getReferrer(); Future<String?> getReferrer();
Future<String?> getAdjustReferrer(); Future<String?> getAdjustReferrer();
Future<String?> getPlatformReferrer(); Future<String?> getPlatformReferrer();
Future<String?> getFacebookReferrer();
} }
/// ///
@ -66,6 +67,9 @@ class DefaultAttributionCallbacks implements AttributionCallbacks {
@override @override
Future<String?> getPlatformReferrer() async => null; Future<String?> getPlatformReferrer() async => null;
@override
Future<String?> getFacebookReferrer() async => null;
} }
/// ///
@ -87,4 +91,6 @@ abstract class AttributionService {
static Future<String?> getAdjustReferrer() => callbacks.getAdjustReferrer(); static Future<String?> getAdjustReferrer() => callbacks.getAdjustReferrer();
static Future<String?> getPlatformReferrer() => static Future<String?> getPlatformReferrer() =>
callbacks.getPlatformReferrer(); callbacks.getPlatformReferrer();
static Future<String?> getFacebookReferrer() =>
callbacks.getFacebookReferrer();
} }

View File

@ -50,26 +50,48 @@ void _logLong(String text) {
_proxyLog.d(text); _proxyLog.d(text);
return; return;
} }
final buffer = StringBuffer(); final buffer = StringBuffer();
int chunkIndex = 0; var chunkIndex = 0;
void emitBuffer() {
if (buffer.isEmpty) return;
chunkIndex++;
_proxyLog.d(
chunkIndex > 1 ? '(part $chunkIndex)\n$buffer' : buffer.toString(),
);
buffer.clear();
}
/// print / logcat `"`)。
void emitLineInChunks(String line) {
for (var offset = 0; offset < line.length; offset += _maxLogChunk) {
final end = (offset + _maxLogChunk <= line.length)
? offset + _maxLogChunk
: line.length;
chunkIndex++;
final piece = line.substring(offset, end);
_proxyLog.d(chunkIndex > 1 ? '(part $chunkIndex)\n$piece' : piece);
}
}
for (final line in lines) { for (final line in lines) {
if (line.length > _maxLogChunk) {
emitBuffer();
emitLineInChunks(line);
continue;
}
final lineWithNewline = buffer.isEmpty ? line : '\n$line'; final lineWithNewline = buffer.isEmpty ? line : '\n$line';
if (buffer.length + lineWithNewline.length > _maxLogChunk && if (buffer.length + lineWithNewline.length > _maxLogChunk &&
buffer.isNotEmpty) { buffer.isNotEmpty) {
chunkIndex++; emitBuffer();
_proxyLog.d('(part $chunkIndex)\n$buffer');
buffer.clear();
buffer.write(line); buffer.write(line);
} else { } else {
if (buffer.isNotEmpty) buffer.write('\n'); if (buffer.isNotEmpty) buffer.write('\n');
buffer.write(line); buffer.write(line);
} }
} }
if (buffer.isNotEmpty) { emitBuffer();
chunkIndex++;
_proxyLog
.d(chunkIndex > 1 ? '(part $chunkIndex)\n$buffer' : buffer.toString());
}
} }
/// JSON /// JSON

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../api/api_client.dart'; import '../api/api_client.dart';
import '../api/proxy_client.dart'; import '../api/proxy_client.dart';
@ -90,6 +91,23 @@ abstract class FrameworkAuthService {
debugPrint('[AuthService] start: referer=$referer'); debugPrint('[AuthService] start: referer=$referer');
} }
//
String? referrerType;
final adjustReferrer = await AttributionService.getAdjustReferrer();
final fbReferrer = await AttributionService.getFacebookReferrer();
if (adjustReferrer != null && adjustReferrer.isNotEmpty) {
referrerType = defaultTargetPlatform == TargetPlatform.iOS
? 'ios_adjust'
: 'android_adjust';
} else if (fbReferrer != null && fbReferrer.isNotEmpty) {
referrerType = 'fb';
}
if (kDebugMode) {
debugPrint('[AuthService] start: referrerType=$referrerType');
}
// //
EntityResponse<FastLoginResponse>? res; EntityResponse<FastLoginResponse>? res;
for (var i = 0; i < maxRetries; i++) { for (var i = 0; i < maxRetries; i++) {
@ -100,10 +118,14 @@ abstract class FrameworkAuthService {
await Future<void>.delayed(Duration(seconds: retryDelaySeconds)); await Future<void>.delayed(Duration(seconds: retryDelaySeconds));
} }
try { try {
final appType =
defaultTargetPlatform == TargetPlatform.iOS ? 'HIOS' : 'HAndroid';
res = await UserApi.fastLogin( res = await UserApi.fastLogin(
deviceId: deviceId, deviceId: deviceId,
sign: sign, sign: sign,
referer: referer ?? '', referer: referer ?? '',
app: appType,
type: referrerType,
); );
break; break;
} catch (e) { } catch (e) {

View File

@ -8,21 +8,30 @@ abstract final class UserApi {
static ProxyClient get _client => ApiClient.instance.proxy; static ProxyClient get _client => ApiClient.instance.proxy;
/// ///
/// [deviceId] ID ()
/// [sign] MD5(deviceId)32 ()
/// [referer]
/// [ch]
/// [type] referrer类型 (af/fb/gg/ios_adjust/android_adjust)
/// [app] (iOS: HIOS / Android: HAndroid)
static Future<EntityResponse<FastLoginResponse>> fastLogin({ static Future<EntityResponse<FastLoginResponse>> fastLogin({
required String deviceId, required String deviceId,
required String sign, required String sign,
String? referer, String? referer,
String? ch, String? ch,
String? type, String? type,
String? app,
}) async { }) async {
final config = ApiClient.instance.config;
return _client.requestEntity( return _client.requestEntity(
path: '/v1/user/fast_login', path: '/v1/user/fast_login',
method: 'POST', method: 'POST',
entityFactory: FastLoginResponse.fromJson, entityFactory: FastLoginResponse.fromJson,
queryParams: { queryParams: {
if (ch != null) 'ch': ch, if (ch != null) 'ch': ch,
'pkg': ApiClient.instance.config.packageName, 'pkg': config.packageName,
if (type != null) 'type': type, if (type != null) 'type': type,
if (app != null) 'app': app,
}, },
body: { body: {
'referer': referer ?? '', 'referer': referer ?? '',