Skip to content

Commit

Permalink
clean-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
atavism committed Oct 1, 2024
1 parent b70afdc commit 218ed91
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
31 changes: 22 additions & 9 deletions lib/core/service/lantern_ffi_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class LanternFFI {
throw Exception("Platform is not supported");
}

static SendPort? proxySendPort;
static SendPort? _proxySendPort;
static Completer<void> _isolateInitialized = Completer<void>();

static startDesktopService() => _lanternFFI.start();

Expand All @@ -66,11 +67,30 @@ class LanternFFI {
//SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}

// Initialize the system proxy isolate
static Future<void> _initializeSystemProxyIsolate() async {
if (!_isolateInitialized.isCompleted) {
final receivePort = ReceivePort();
// create isolate that listens for system proxy commands
await Isolate.spawn(_proxyIsolateEntry, receivePort.sendPort);
_proxySendPort = await receivePort.first;
_isolateInitialized.complete();
}
}

// initialize the isolate if need be and send the vpnStatus to it
static Future<void> sendVpnStatus(String vpnStatus) async {
if (!_isolateInitialized.isCompleted) {
await _initializeSystemProxyIsolate();
}
_proxySendPort?.send(vpnStatus);
}

// To isolate problematic interactions between signal handling and the Go
// runtime, the FFI code for toggling the system proxy is run on a separate
// isolate. This provides a way to catch and manage signals before they
// propagate and cause the Go runtime to crash.
static void proxyIsolateEntry(SendPort sendPort) {
static void _proxyIsolateEntry(SendPort sendPort) {
final commandPort = ReceivePort();
sendPort.send(commandPort.sendPort);
commandPort.listen((message) async {
Expand All @@ -92,13 +112,6 @@ class LanternFFI {
});
}

// create isolate that listens for system proxy commands
static Future<void> systemProxyIsolate() async {
final receivePort = ReceivePort();
await Isolate.spawn(proxyIsolateEntry, receivePort.sendPort);
proxySendPort = await receivePort.first;
}

static Future<User> ffiUserData() async {
final res = await _lanternFFI.userData().cast<Utf8>().toDartString();
// it's necessary to use mergeFromProto3Json here instead of fromJson; otherwise, a FormatException with
Expand Down
10 changes: 3 additions & 7 deletions lib/features/vpn/vpn_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ class VPNChangeNotifier with ChangeNotifier {
bool isConnected() => vpnStatus.value == 'connected';

void toggleConnection() {
if (isConnected()) {
LanternFFI.sysProxyOff();
_vpnStatus.value = 'disconnected';
} else {
LanternFFI.sysProxyOn();
_vpnStatus.value = 'connected';
}
final newStatus = isConnected() ? 'disconnected' : 'connected';
_vpnStatus.value = newStatus;
LanternFFI.sendVpnStatus(newStatus);
notifyListeners();
}

Expand Down
52 changes: 23 additions & 29 deletions lib/features/vpn/vpn_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,35 @@ class _VPNSwitchState extends State<VPNSwitch> {
});
} else {
// This ui for desktop
return AdvancedSwitch(
width: 160,
height: 70,
borderRadius: BorderRadius.circular(40),
disabledOpacity: 1,
enabled: (internetStatusProvider.isConnected &&
!vpnNotifier.isFlashlightInitializedFailed),
initialValue: vpnNotifier.vpnStatus.value == 'connected' ||
vpnNotifier.vpnStatus.value == 'disconnecting',
activeColor: onSwitchColor,
inactiveColor: (internetStatusProvider.isConnected &&
!vpnNotifier.isFlashlightInitializedFailed)
? offSwitchColor
: grey3,
onChanged: (newValue) {
final newStatus = newValue ? 'connected' : 'disconnected';
context.read<VPNChangeNotifier>().vpnStatus.value = newStatus;
vpnProcessForDesktop(newStatus);
},
);
return ValueListenableBuilder<String>(
valueListenable: vpnNotifier.vpnStatus,
builder: (context, value, child) {
return AdvancedSwitch(
width: 160,
height: 70,
borderRadius: BorderRadius.circular(40),
disabledOpacity: 1,
enabled: (internetStatusProvider.isConnected &&
!vpnNotifier.isFlashlightInitializedFailed),
initialValue: value == 'connected' || value == 'disconnecting',
activeColor: onSwitchColor,
inactiveColor: (internetStatusProvider.isConnected &&
!vpnNotifier.isFlashlightInitializedFailed)
? offSwitchColor
: grey3,
onChanged: (newValue) {
final newStatus = newValue ? 'connected' : 'disconnected';
vpnNotifier.vpnStatus.value = newStatus;
LanternFFI.sendVpnStatus(newStatus);
},
);
});
}
}

bool isIdle(String vpnStatus) =>
vpnStatus != 'connecting' && vpnStatus != 'disconnecting';

Future<void> vpnProcessForDesktop(String vpnStatus) async {
if (LanternFFI.proxySendPort == null) {
// when the button is toggled, check whether the isolate has
// already been started
await LanternFFI.systemProxyIsolate();
}
LanternFFI.proxySendPort?.send(vpnStatus);
}

Future<void> vpnProcessForMobile(
bool newValue, String vpnStatus, bool userHasPermission) async {
//Make sure user has permission all the permission
Expand Down

0 comments on commit 218ed91

Please sign in to comment.