mirror of
https://github.com/Auties00/Reboot-Launcher.git
synced 2026-01-13 03:02:22 +01:00
54 lines
1.9 KiB
Dart
54 lines
1.9 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:reboot_common/common.dart';
|
|
import 'package:shelf/shelf_io.dart';
|
|
import 'package:shelf_proxy/shelf_proxy.dart';
|
|
|
|
final authenticatorDirectory = Directory("${assetsDirectory.path}\\authenticator");
|
|
final authenticatorStartExecutable = File("${authenticatorDirectory.path}\\lawinserver.exe");
|
|
|
|
Future<Win32Process> startEmbeddedAuthenticator(bool detached) async => startProcess(
|
|
executable: authenticatorStartExecutable,
|
|
window: detached,
|
|
|
|
);
|
|
|
|
Future<HttpServer> startRemoteAuthenticatorProxy(Uri uri) async => await serve(proxyHandler(uri), kDefaultAuthenticatorHost, kDefaultAuthenticatorPort);
|
|
|
|
Future<bool> isAuthenticatorPortFree() async => await pingAuthenticator(kDefaultAuthenticatorHost, kDefaultAuthenticatorPort) == null;
|
|
|
|
Future<bool> freeAuthenticatorPort() async {
|
|
await killProcessByPort(kDefaultAuthenticatorPort);
|
|
final standardResult = await isAuthenticatorPortFree();
|
|
if(standardResult) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
Future<Uri?> pingAuthenticator(String host, int port, [bool https=false]) async {
|
|
var hostName = _getHostName(host);
|
|
var declaredScheme = _getScheme(host);
|
|
try{
|
|
var uri = Uri(
|
|
scheme: declaredScheme ?? (https ? "https" : "http"),
|
|
host: hostName,
|
|
port: port,
|
|
path: "unknown"
|
|
);
|
|
var client = HttpClient()
|
|
..connectionTimeout = const Duration(seconds: 5);
|
|
var request = await client.getUrl(uri);
|
|
var response = await request.close();
|
|
return response.statusCode == 200 || response.statusCode == 404 ? uri : null;
|
|
}catch(_){
|
|
return https || declaredScheme != null || isLocalHost(host) ? null : await pingAuthenticator(host, port, true);
|
|
}
|
|
}
|
|
|
|
String? _getHostName(String host) => host.replaceFirst("http://", "").replaceFirst("https://", "");
|
|
|
|
String? _getScheme(String host) => host.startsWith("http://") ? "http" : host.startsWith("https://") ? "https" : null;
|
|
|