diff --git a/lantern-core/ffi/ffi.go b/lantern-core/ffi/ffi.go index 1d6d2fb09a..f7219c76c6 100644 --- a/lantern-core/ffi/ffi.go +++ b/lantern-core/ffi/ffi.go @@ -940,6 +940,26 @@ func revokeServerManagerInvite(_ip, _port, _accessToken, _inviteName *C.char) *C return C.CString("ok") } +// addServerBasedOnURLs adds a server based on the provided URLs. +// +//export addServerBasedOnURLs +func addServerBasedOnURLs(_urls *C.char, _skipCertVerification C.int, _serverName *C.char) *C.char { + c, errStr := requireCore() + if errStr != nil { + return errStr + } + urls := C.GoString(_urls) + skipCertVerification := _skipCertVerification != 0 + serverName := C.GoString(_serverName) + slog.Debug("Adding server based on URLs:", "urls", urls, "skipCertVerification", skipCertVerification) + err := c.AddServerBasedOnURLs(urls, skipCertVerification, serverName) + if err != nil { + return SendError(fmt.Errorf("Error adding server based on URLs: %v", err)) + } + slog.Debug("Server added successfully based on URLs:", "urls", urls) + return C.CString("ok") +} + //export setBlockAdsEnabled func setBlockAdsEnabled(enabled C.int) *C.char { c, errStr := requireCore() diff --git a/lib/features/private_server/join_private_server.dart b/lib/features/private_server/join_private_server.dart index a47a8989ae..a02bdfde02 100644 --- a/lib/features/private_server/join_private_server.dart +++ b/lib/features/private_server/join_private_server.dart @@ -220,42 +220,11 @@ class _JoinPrivateServerState extends ConsumerState { ]); } - Future onJoinServer(String uri, String name) async { - final Uri url; - try { - url = Uri.parse(uri); - } catch (_) { - context.showSnackBar('invalid_server_details'.i18n); - return; - } - appLogger.info("Verifying server with URL: $url"); - final data = url.queryParameters; - final ip = data['ip'] ?? ''; - final port = data['port'] ?? ''; - final accessToken = data['token'] ?? ''; - final expStr = data['exp']; - final expiration = int.tryParse(expStr ?? ''); - - if (ip.isEmpty || - port.isEmpty || - accessToken.isEmpty || - expiration == null) { - context.showSnackBar('invalid_server_details'.i18n); - return; - } - final expired = DateTime.fromMillisecondsSinceEpoch(expiration * 1000); - // check if date is expired - if (expired.isBefore(DateTime.now())) { - appLogger.debug("DeepLink expired: $expired"); - context.showSnackBar('deep_link_expired'.i18n); - return; - } - + Future onJoinServer(String urls, String serverName) async { context.showLoadingDialog(); final result = await ref .read(privateServerProvider.notifier) - .addServerManually(ip, port, accessToken, name); - + .addServerBasedOnURLs(urls, true, serverName); result.fold( (error) { appLogger.error("Failed to join private server: $error"); diff --git a/lib/features/private_server/provider/private_server_notifier.dart b/lib/features/private_server/provider/private_server_notifier.dart index 2ff78d1fd7..7a46b7ef16 100644 --- a/lib/features/private_server/provider/private_server_notifier.dart +++ b/lib/features/private_server/provider/private_server_notifier.dart @@ -69,6 +69,19 @@ class PrivateServerNotifier extends _$PrivateServerNotifier { ); } + Future> addServerBasedOnURLs( + String urls, bool skipCertVerification, String serverName) async { + return ref.read(lanternServiceProvider).addServerBasedOnURLs( + urls: urls, + skipCertVerification: skipCertVerification, + serverName: serverName, + ); + } + + Future> setCert(String fingerprint) async { + return ref.read(lanternServiceProvider).setCert(fingerprint: fingerprint); + } + void watchPrivateServerLogs() { _privateServerStatusSub = ref .read(lanternServiceProvider) diff --git a/lib/lantern/lantern_core_service.dart b/lib/lantern/lantern_core_service.dart index cb8d6aaaa9..94e4823f26 100644 --- a/lib/lantern/lantern_core_service.dart +++ b/lib/lantern/lantern_core_service.dart @@ -185,6 +185,11 @@ abstract class LanternCoreService { required String accessToken, required String serverName}); + Future> addServerBasedOnURLs( + {required String urls, + required bool skipCertVerification, + required String serverName}); + Future> cancelDeployment(); Future> inviteToServerManagerInstance({ diff --git a/lib/lantern/lantern_ffi_service.dart b/lib/lantern/lantern_ffi_service.dart index 6257e823fc..1b118973d9 100644 --- a/lib/lantern/lantern_ffi_service.dart +++ b/lib/lantern/lantern_ffi_service.dart @@ -1072,12 +1072,35 @@ class LanternFFIService implements LanternCoreService { } @override - Future> inviteToServerManagerInstance({ - required String ip, - required String port, - required String accessToken, - required String inviteName, - }) async { + Future> addServerBasedOnURLs( + {required String urls, + required bool skipCertVerification, + required String serverName}) async { + try { + final result = await runInBackground( + () async { + return _ffiService + .addServerManagerInstanceBasedOnURLs( + urls.toCharPtr, + skipCertVerification ? 1 : 0, + serverName.toCharPtr) + .toDartString(); + }, + ); + checkAPIError(result); + return Right(unit); + } catch (e, stackTrace) { + appLogger.error('Error adding server based on URLs', e, stackTrace); + return Left(e.toFailure()); + } + } + + @override + Future> inviteToServerManagerInstance( + {required String ip, + required String port, + required String accessToken, + required String inviteName}) async { try { final result = await runInBackground(() async { return _ffiService diff --git a/lib/lantern/lantern_platform_service.dart b/lib/lantern/lantern_platform_service.dart index b8113086cc..c58c3b56a2 100644 --- a/lib/lantern/lantern_platform_service.dart +++ b/lib/lantern/lantern_platform_service.dart @@ -1049,6 +1049,24 @@ class LanternPlatformService implements LanternCoreService { } } + @override + Future> addServerBasedOnURLs({ + required String urls, + required bool skipCertVerification, + required String serverName}) async { + try { + await _methodChannel.invokeMethod('addServerBasedOnURLs', { + 'urls': urls, + 'skipCertVerification': skipCertVerification, + 'serverName': serverName, + }); + return Right(unit); + } catch (e, stackTrace) { + appLogger.error('Error adding server based on URLs', e, stackTrace); + return Left(e.toFailure()); + } + } + @override Future> connectToServer( String location, String tag) async { diff --git a/lib/lantern/lantern_service.dart b/lib/lantern/lantern_service.dart index 354d1c539a..51d9d0e13c 100644 --- a/lib/lantern/lantern_service.dart +++ b/lib/lantern/lantern_service.dart @@ -460,6 +460,25 @@ class LanternService implements LanternCoreService { ip: ip, port: port, accessToken: accessToken, serverName: serverName); } + @override + Future> addServerBasedOnURLs( + {required String urls, + required bool skipCertVerification, + required String serverName}) { + if (PlatformUtils.isFFISupported) { + return _ffiService.addServerBasedOnURLs( + urls: urls, + skipCertVerification: skipCertVerification, + serverName: serverName, + ); + } + return _platformService.addServerBasedOnURLs( + urls: urls, + skipCertVerification: skipCertVerification, + serverName: serverName, + ); + } + /// connectToServer is used to connect to a server /// this will work with lantern customer and private server /// requires location and tag