Add logging for speed limit and traffic control commands
This commit is contained in:
@@ -96,7 +96,9 @@ Future<bool> deletePeer(String publicKey) async {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> setSpeedLimit(String publicKey, int speedKbps) async {
|
Future<void> setSpeedLimit(String publicKey, int speedKbps) async {
|
||||||
|
print('Setting speed limit for peer $publicKey to ${speedKbps}kbps');
|
||||||
final peerIP = await _getPeerIP(publicKey);
|
final peerIP = await _getPeerIP(publicKey);
|
||||||
|
print('Found peer IP: $peerIP for publicKey: $publicKey');
|
||||||
if (peerIP == null) throw Exception('Peer not found');
|
if (peerIP == null) throw Exception('Peer not found');
|
||||||
|
|
||||||
await TrafficControlService.setSpeedLimit(peerIP, speedKbps);
|
await TrafficControlService.setSpeedLimit(peerIP, speedKbps);
|
||||||
|
|||||||
@@ -3,12 +3,24 @@ import 'dart:io';
|
|||||||
class TrafficControlService {
|
class TrafficControlService {
|
||||||
static Future<void> setSpeedLimit(String peerIP, int speedKbps) async {
|
static Future<void> setSpeedLimit(String peerIP, int speedKbps) async {
|
||||||
final mark = _getMarkForIP(peerIP);
|
final mark = _getMarkForIP(peerIP);
|
||||||
|
print('Setting speed limit for peer $peerIP to ${speedKbps}kbps (mark: $mark)');
|
||||||
|
|
||||||
|
try {
|
||||||
|
print('Running iptables MARK commands for $peerIP...');
|
||||||
await _runIptablesCommand(['-I', 'FORWARD', '-s', peerIP, '-j', 'MARK', '--set-mark', mark.toString()]);
|
await _runIptablesCommand(['-I', 'FORWARD', '-s', peerIP, '-j', 'MARK', '--set-mark', mark.toString()]);
|
||||||
await _runIptablesCommand(['-I', 'FORWARD', '-d', peerIP, '-j', 'MARK', '--set-mark', mark.toString()]);
|
await _runIptablesCommand(['-I', 'FORWARD', '-d', peerIP, '-j', 'MARK', '--set-mark', mark.toString()]);
|
||||||
|
|
||||||
|
print('Running tc class add command...');
|
||||||
await _runTcCommand(['class', 'add', 'dev', 'wg0', 'parent', '1:1', 'classid', '1:$mark', 'htb', 'rate', '${speedKbps}kbit', 'ceil', '${speedKbps}kbit']);
|
await _runTcCommand(['class', 'add', 'dev', 'wg0', 'parent', '1:1', 'classid', '1:$mark', 'htb', 'rate', '${speedKbps}kbit', 'ceil', '${speedKbps}kbit']);
|
||||||
|
|
||||||
|
print('Running tc filter add command...');
|
||||||
await _runTcCommand(['filter', 'add', 'dev', 'wg0', 'protocol', 'ip', 'parent', '1:', 'prio', '1', 'handle', mark.toString(), 'fw', 'flowid', '1:$mark']);
|
await _runTcCommand(['filter', 'add', 'dev', 'wg0', 'protocol', 'ip', 'parent', '1:', 'prio', '1', 'handle', mark.toString(), 'fw', 'flowid', '1:$mark']);
|
||||||
|
|
||||||
|
print('Speed limit set successfully for $peerIP');
|
||||||
|
} catch (e) {
|
||||||
|
print('ERROR setting speed limit for $peerIP: $e');
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> setDataCap(String peerIP, int dataCapMB) async {
|
static Future<void> setDataCap(String peerIP, int dataCapMB) async {
|
||||||
@@ -25,14 +37,32 @@ class TrafficControlService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> _runIptablesCommand(List<String> args) async {
|
static Future<void> _runIptablesCommand(List<String> args) async {
|
||||||
|
final command = 'iptables ${args.join(' ')}';
|
||||||
|
print('Executing: $command');
|
||||||
final result = await Process.run('iptables', args);
|
final result = await Process.run('iptables', args);
|
||||||
|
print('iptables exit code: ${result.exitCode}');
|
||||||
|
if (result.stdout.toString().isNotEmpty) {
|
||||||
|
print('iptables stdout: ${result.stdout}');
|
||||||
|
}
|
||||||
|
if (result.stderr.toString().isNotEmpty) {
|
||||||
|
print('iptables stderr: ${result.stderr}');
|
||||||
|
}
|
||||||
if (result.exitCode != 0) {
|
if (result.exitCode != 0) {
|
||||||
throw Exception('iptables command failed: ${result.stderr}');
|
throw Exception('iptables command failed: ${result.stderr}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> _runTcCommand(List<String> args) async {
|
static Future<void> _runTcCommand(List<String> args) async {
|
||||||
|
final command = 'tc ${args.join(' ')}';
|
||||||
|
print('Executing: $command');
|
||||||
final result = await Process.run('tc', args);
|
final result = await Process.run('tc', args);
|
||||||
|
print('tc exit code: ${result.exitCode}');
|
||||||
|
if (result.stdout.toString().isNotEmpty) {
|
||||||
|
print('tc stdout: ${result.stdout}');
|
||||||
|
}
|
||||||
|
if (result.stderr.toString().isNotEmpty) {
|
||||||
|
print('tc stderr: ${result.stderr}');
|
||||||
|
}
|
||||||
if (result.exitCode != 0) {
|
if (result.exitCode != 0) {
|
||||||
throw Exception('tc command failed: ${result.stderr}');
|
throw Exception('tc command failed: ${result.stderr}');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user