Skip to content

Commit

Permalink
feat(http): set User-Agent to "LunaSea/{version} {build}"
Browse files Browse the repository at this point in the history
  • Loading branch information
JagandeepBrar committed Feb 11, 2022
1 parent 8751e69 commit 420d11c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/core/system/networking.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
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].
void initialize() {
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;
}

Expand Down

0 comments on commit 420d11c

Please sign in to comment.