Refactor peer creation to include geolocation data and server details

This commit is contained in:
ImBenji
2025-08-05 18:37:10 +01:00
parent 5de98ec501
commit 62853ca63a
3 changed files with 18 additions and 28 deletions

View File

@@ -139,19 +139,6 @@ Future<GeolocationData> getGeolocationData() async {
} }
} }
@deprecated
/// Get the current WAN IP address of the device.
Future<String> getWanIp() async {
// Get the IP address of the server. https://api.ipify.org?format=json
var ipResponse = await http.get(Uri.parse('https://api.ipify.org?format=json'));
if (ipResponse.statusCode == 200) {
var ipData = jsonDecode(ipResponse.body);
return ipData['ip'] as String;
} else {
throw Exception('Failed to get server IP address');
}
}
/// Check if Wireguard is installed on the system. /// Check if Wireguard is installed on the system.
Future<bool> isWireguardInstalled() async { Future<bool> isWireguardInstalled() async {
try { try {

View File

@@ -3,6 +3,8 @@ import 'dart:convert';
import 'package:shelf/shelf.dart'; import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart'; import 'package:shelf_router/shelf_router.dart';
import 'package:waylume_server/wireguard/peers.dart'; import 'package:waylume_server/wireguard/peers.dart';
import 'package:waylume_server/wireguard/utils.dart';
import 'package:waylume_server/core/utils.dart';
import 'package:waylume_server/services/vpn_session_service.dart'; import 'package:waylume_server/services/vpn_session_service.dart';
class PeerRoutes { class PeerRoutes {
@@ -21,14 +23,24 @@ class PeerRoutes {
Future<Response> _createPeer(Request request) async { Future<Response> _createPeer(Request request) async {
try { try {
final peer = await createPeer(); final peer = await createPeer();
final config = await generateClientConfigForPeer(peer); final geoData = await getGeolocationData();
String serverEndpoint = '${geoData.ip}:51820';
String serverPublicKey = await getServerPublicKey();
final responseData = <String, dynamic>{
'peer': {
'ip': peer.ip,
'privateKey': peer.privateKey,
},
'server': {
'publicKey': serverPublicKey,
'endpoint': serverEndpoint,
},
'success': true,
};
return Response.ok( return Response.ok(
jsonEncode({ jsonEncode(responseData),
'success': true,
'peer': peer.toJson(),
'config': config,
}),
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
); );
} catch (e) { } catch (e) {

View File

@@ -50,15 +50,6 @@ PersistentKeepalive = 25
} }
} }
Future<String> generateClientConfigForPeer(WireGuardPeer peer) async {
String serverEndpoint = '${await getWanIp()}:51820';
String serverPublicKey = await getServerPublicKey();
return peer.generateClientConfig(
serverPublicKey: serverPublicKey,
serverEndpoint: serverEndpoint,
);
}
/// Creates a new WireGuard peer and returns the peer info /// Creates a new WireGuard peer and returns the peer info
Future<WireGuardPeer> createPeer() async { Future<WireGuardPeer> createPeer() async {