Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lantern-core/ffi/ffi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
35 changes: 2 additions & 33 deletions lib/features/private_server/join_private_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,42 +220,11 @@ class _JoinPrivateServerState extends ConsumerState<JoinPrivateServer> {
]);
}

Future<void> 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<void> 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");
Expand Down
13 changes: 13 additions & 0 deletions lib/features/private_server/provider/private_server_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class PrivateServerNotifier extends _$PrivateServerNotifier {
);
}

Future<Either<Failure, Unit>> addServerBasedOnURLs(
String urls, bool skipCertVerification, String serverName) async {
return ref.read(lanternServiceProvider).addServerBasedOnURLs(
urls: urls,
skipCertVerification: skipCertVerification,
serverName: serverName,
);
}

Future<Either<Failure, Unit>> setCert(String fingerprint) async {
return ref.read(lanternServiceProvider).setCert(fingerprint: fingerprint);
}

void watchPrivateServerLogs() {
_privateServerStatusSub = ref
.read(lanternServiceProvider)
Expand Down
5 changes: 5 additions & 0 deletions lib/lantern/lantern_core_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ abstract class LanternCoreService {
required String accessToken,
required String serverName});

Future<Either<Failure, Unit>> addServerBasedOnURLs(
{required String urls,
required bool skipCertVerification,
required String serverName});

Future<Either<Failure, Unit>> cancelDeployment();

Future<Either<Failure, String>> inviteToServerManagerInstance({
Expand Down
35 changes: 29 additions & 6 deletions lib/lantern/lantern_ffi_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1072,12 +1072,35 @@ class LanternFFIService implements LanternCoreService {
}

@override
Future<Either<Failure, String>> inviteToServerManagerInstance({
required String ip,
required String port,
required String accessToken,
required String inviteName,
}) async {
Future<Either<Failure, Unit>> addServerBasedOnURLs(
{required String urls,
required bool skipCertVerification,
required String serverName}) async {
try {
final result = await runInBackground<String>(
() 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<Either<Failure, String>> inviteToServerManagerInstance(
{required String ip,
required String port,
required String accessToken,
required String inviteName}) async {
try {
final result = await runInBackground<String>(() async {
return _ffiService
Expand Down
18 changes: 18 additions & 0 deletions lib/lantern/lantern_platform_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,24 @@ class LanternPlatformService implements LanternCoreService {
}
}

@override
Future<Either<Failure, Unit>> 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<Either<Failure, String>> connectToServer(
String location, String tag) async {
Expand Down
19 changes: 19 additions & 0 deletions lib/lantern/lantern_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,25 @@ class LanternService implements LanternCoreService {
ip: ip, port: port, accessToken: accessToken, serverName: serverName);
}

@override
Future<Either<Failure, Unit>> 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
Expand Down
Loading