dev
dart 68 lines 1.64 KB
Raw
1 import 'dart:io';
2
3 import 'package:cw_core/utils/print_verbose.dart';
4 import 'package:cw_core/utils/tor/disabled.dart';
5 import 'package:cw_core/utils/tor/socks.dart';
6 import 'package:cw_core/utils/tor/torch.dart';
7
8 abstract class CakeTorInstance {
9 bool get started;
10
11 int get port => -1;
12
13 bool get enabled => false;
14
15 bool get bootstrapped => false;
16
17 Future<void> start();
18 Future<void> stop();
19
20 static Future<CakeTorInstance> getInstance() async {
21 if (Platform.isLinux) {
22 try {
23 String? socksServer;
24 if (Platform.environment["SOCKS_SERVER"] != null) {
25 socksServer = Platform.environment["SOCKS_SERVER"]!;
26 }
27 if (Platform.environment["SOCKS_PROXY"] != null) {
28 socksServer = Platform.environment["SOCKS_PROXY"]!;
29 }
30 if (socksServer != null) {
31 final uri = Uri.tryParse(socksServer);
32 if (uri != null) {
33 return CakeTorSocks(uri.port);
34 } else {
35 final uri = Uri.tryParse("socks5://$socksServer");
36 if (uri != null) {
37 return CakeTorSocks(uri.port);
38 }
39 }
40 }
41 } catch (e) {
42 printV(
43 "Failed to identify linux version - no SOCKS_SERVER variable found or malformed",
44 );
45 }
46 }
47 try {
48 final torch = await CakeTorTorch.getInstance();
49 if (torch != null) {
50 return torch;
51 }
52 } catch (e) {
53 printV("Failed to initialize torch: $e");
54 }
55 return CakeTorDisabled();
56 }
57
58 String toString() {
59 return """
60 CakeTorInstance(
61 port: $port,
62 started: $started,
63 bootstrapped: $bootstrapped,
64 enabled: $enabled,
65 )
66 """;
67 }
68 }