From 420d11c37188fe1d56d76b51b98325b384a45cff Mon Sep 17 00:00:00 2001 From: Jagandeep Brar Date: Fri, 11 Feb 2022 13:30:09 -0500 Subject: [PATCH] feat(http): set User-Agent to "LunaSea/{version} {build}" --- lib/core/system/networking.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/core/system/networking.dart b/lib/core/system/networking.dart index 0303fc403..f24df3eeb 100644 --- a/lib/core/system/networking.dart +++ b/lib/core/system/networking.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:lunasea/core.dart'; +import 'package:package_info_plus/package_info_plus.dart'; class LunaNetworking extends HttpOverrides { /// Initialize the default HTTP client by setting [HttpOverrides]'s global HTTP client to [LunaNetworking]. @@ -8,14 +9,26 @@ class LunaNetworking extends HttpOverrides { HttpOverrides.global = LunaNetworking(); } + String generateUserAgent(PackageInfo info) { + return '${info.appName}/${info.version} ${info.buildNumber}'; + } + /// Overrides the default [createHttpClient] function and ensures all HTTPS connections allow bad certificate connections. /// /// Returns a new [HttpClient] using the given [context]. @override HttpClient createHttpClient(SecurityContext? context) { final HttpClient client = super.createHttpClient(context); + + // Disable TLS validation if (!LunaDatabaseValue.NETWORKING_TLS_VALIDATION.data) client.badCertificateCallback = (cert, host, port) => true; + + // Set User-Agent + PackageInfo.fromPlatform() + .then((info) => client.userAgent = generateUserAgent(info)) + .catchError((_) => client.userAgent = 'LunaSea/Unknown'); + return client; }