From 62853ca63a22236956c8fe71257ff51223efd053 Mon Sep 17 00:00:00 2001 From: ImBenji Date: Tue, 5 Aug 2025 18:37:10 +0100 Subject: [PATCH] Refactor peer creation to include geolocation data and server details --- lib/core/utils.dart | 13 ------------- lib/web/peer_routes.dart | 24 ++++++++++++++++++------ lib/wireguard/peers.dart | 9 --------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/lib/core/utils.dart b/lib/core/utils.dart index 3959bb7..28fcde7 100644 --- a/lib/core/utils.dart +++ b/lib/core/utils.dart @@ -139,19 +139,6 @@ Future getGeolocationData() async { } } -@deprecated -/// Get the current WAN IP address of the device. -Future 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. Future isWireguardInstalled() async { try { diff --git a/lib/web/peer_routes.dart b/lib/web/peer_routes.dart index c5f5d7b..b6995d7 100644 --- a/lib/web/peer_routes.dart +++ b/lib/web/peer_routes.dart @@ -3,6 +3,8 @@ import 'dart:convert'; import 'package:shelf/shelf.dart'; import 'package:shelf_router/shelf_router.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'; class PeerRoutes { @@ -21,14 +23,24 @@ class PeerRoutes { Future _createPeer(Request request) async { try { final peer = await createPeer(); - final config = await generateClientConfigForPeer(peer); + final geoData = await getGeolocationData(); + String serverEndpoint = '${geoData.ip}:51820'; + String serverPublicKey = await getServerPublicKey(); + + final responseData = { + 'peer': { + 'ip': peer.ip, + 'privateKey': peer.privateKey, + }, + 'server': { + 'publicKey': serverPublicKey, + 'endpoint': serverEndpoint, + }, + 'success': true, + }; return Response.ok( - jsonEncode({ - 'success': true, - 'peer': peer.toJson(), - 'config': config, - }), + jsonEncode(responseData), headers: {'Content-Type': 'application/json'}, ); } catch (e) { diff --git a/lib/wireguard/peers.dart b/lib/wireguard/peers.dart index 1a0bfab..18ed22a 100644 --- a/lib/wireguard/peers.dart +++ b/lib/wireguard/peers.dart @@ -50,15 +50,6 @@ PersistentKeepalive = 25 } } -Future 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 Future createPeer() async {