Refactor peer creation to include geolocation data and server details
This commit is contained in:
@@ -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.
|
||||
Future<bool> isWireguardInstalled() async {
|
||||
try {
|
||||
|
||||
@@ -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<Response> _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 = <String, dynamic>{
|
||||
'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) {
|
||||
|
||||
@@ -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
|
||||
Future<WireGuardPeer> createPeer() async {
|
||||
|
||||
Reference in New Issue
Block a user