Refactor main application logic and update Dockerfile for new entry point
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:waylume_server/utils.dart';
|
||||
import 'package:waylume_server/core/utils.dart';
|
||||
import 'package:waylume_server/wireguard/utils.dart';
|
||||
import 'package:waylume_server/wireguard/traffic_control.dart';
|
||||
|
||||
class WireGuardPeer {
|
||||
final String privateKey;
|
||||
@@ -49,16 +50,14 @@ PersistentKeepalive = 25
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> generateClientConfig(WireGuardPeer peer) async {
|
||||
|
||||
String server_endpoint = '${await getWanIp()}:51820'; // Assuming default WireGuard port
|
||||
Future<String> generateClientConfigForPeer(WireGuardPeer peer) async {
|
||||
String serverEndpoint = '${await getWanIp()}:51820';
|
||||
String serverPublicKey = await getServerPublicKey();
|
||||
|
||||
return peer.generateClientConfig(
|
||||
serverPublicKey: serverPublicKey,
|
||||
serverEndpoint: server_endpoint,
|
||||
serverEndpoint: serverEndpoint,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/// Creates a new WireGuard peer and returns the peer info
|
||||
@@ -99,24 +98,15 @@ Future<bool> deletePeer(String publicKey) async {
|
||||
Future<void> setSpeedLimit(String publicKey, int speedKbps) async {
|
||||
final peerIP = await _getPeerIP(publicKey);
|
||||
if (peerIP == null) throw Exception('Peer not found');
|
||||
|
||||
final mark = _getMarkForIP(peerIP);
|
||||
|
||||
await Process.run('iptables', ['-I', 'FORWARD', '-s', peerIP, '-j', 'MARK', '--set-mark', mark.toString()]);
|
||||
await Process.run('iptables', ['-I', 'FORWARD', '-d', peerIP, '-j', 'MARK', '--set-mark', mark.toString()]);
|
||||
await Process.run('tc', ['class', 'add', 'dev', 'wg0', 'parent', '1:1', 'classid', '1:$mark', 'htb', 'rate', '${speedKbps}kbit', 'ceil', '${speedKbps}kbit']);
|
||||
await Process.run('tc', ['filter', 'add', 'dev', 'wg0', 'protocol', 'ip', 'parent', '1:', 'prio', '1', 'handle', mark.toString(), 'fw', 'flowid', '1:$mark']);
|
||||
|
||||
await TrafficControlService.setSpeedLimit(peerIP, speedKbps);
|
||||
}
|
||||
|
||||
Future<void> setDataCap(String publicKey, int dataCapMB) async {
|
||||
final peerIP = await _getPeerIP(publicKey);
|
||||
if (peerIP == null) throw Exception('Peer not found');
|
||||
|
||||
final quotaBytes = dataCapMB * 1024 * 1024;
|
||||
await Process.run('iptables', ['-I', 'FORWARD', '-s', peerIP, '-m', 'quota', '--quota', quotaBytes.toString(), '-j', 'ACCEPT']);
|
||||
await Process.run('iptables', ['-I', 'FORWARD', '-d', peerIP, '-m', 'quota', '--quota', quotaBytes.toString(), '-j', 'ACCEPT']);
|
||||
await Process.run('iptables', ['-A', 'FORWARD', '-s', peerIP, '-j', 'DROP']);
|
||||
await Process.run('iptables', ['-A', 'FORWARD', '-d', peerIP, '-j', 'DROP']);
|
||||
|
||||
await TrafficControlService.setDataCap(peerIP, dataCapMB);
|
||||
}
|
||||
|
||||
Future<String?> _getPeerIP(String publicKey) async {
|
||||
@@ -133,10 +123,6 @@ Future<String?> _getPeerIP(String publicKey) async {
|
||||
return null;
|
||||
}
|
||||
|
||||
int _getMarkForIP(String ip) {
|
||||
return ip.split('.').last.hashCode % 65535 + 1;
|
||||
}
|
||||
|
||||
Future<String> _getNextIP() async {
|
||||
final result = await Process.run('wg', ['show', 'wg0', 'allowed-ips']);
|
||||
final usedIPs = RegExp(r'10\.(\d+)\.(\d+)\.(\d+)')
|
||||
@@ -155,22 +141,3 @@ Future<String> _getNextIP() async {
|
||||
}
|
||||
throw Exception('No available IPs');
|
||||
}
|
||||
|
||||
void main() async {
|
||||
try {
|
||||
final peer = await createPeer();
|
||||
print('New WireGuard Peer Created:');
|
||||
print('Private Key: ${peer.privateKey}');
|
||||
print('Public Key: ${peer.publicKey}');
|
||||
print('IP Address: ${peer.ip}');
|
||||
|
||||
// Example usage of generating client config
|
||||
final clientConfig = peer.generateClientConfig(
|
||||
serverPublicKey: 'your_server_public_key',
|
||||
serverEndpoint: 'your_server_endpoint:51820',
|
||||
);
|
||||
print('Client Config:\n$clientConfig');
|
||||
} catch (e) {
|
||||
print('Error creating WireGuard peer: $e');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user