Files
FlClash/lib/common/proxy.dart
T

59 lines
1.7 KiB
Dart
Raw Normal View History

import 'package:fl_clash/common/constant.dart';
import 'package:fl_clash/common/print.dart';
2024-08-15 16:18:00 +08:00
import 'package:fl_clash/common/system.dart';
import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/models/models.dart' show ProxyState;
2024-08-15 16:18:00 +08:00
import 'package:proxy/proxy.dart';
2024-04-30 23:38:49 +08:00
final proxy = system.isDesktop && !safeModeBuild ? Proxy() : null;
final systemProxyQueue = proxy == null ? null : SystemProxyQueue(proxy!);
/// Exit's stop goes through the same queue as every other change, so a start
/// still waiting behind a slow call cannot turn the proxy back on after it.
class SystemProxyQueue {
final Proxy _proxy;
Future<void> _tail = Future.value();
bool _closed = false;
SystemProxyQueue(this._proxy);
Future<void> apply(ProxyState state) {
return _enqueue(() async {
if (_closed) {
return;
}
final applied = state.isStart && state.systemProxy
? await _proxy.startProxy(state.port, state.bassDomain)
: await _proxy.stopProxy();
if (!applied) {
commonPrint.log(
'update system proxy failed',
logLevel: LogLevel.warning,
);
}
});
}
Future<void> close() {
_closed = true;
return _enqueue(() async {
await _proxy.stopProxy();
});
}
Future<void> _enqueue(Future<void> Function() task) {
return _tail = _tail.then((_) => task()).catchError((Object error) {
commonPrint.log(
'update system proxy failed: $error',
logLevel: LogLevel.warning,
);
});
}
}
String proxyEnvCommand(int port, {required bool isWindows}) {
final url = 'http://127.0.0.1:$port';
return isWindows ? 'set \$env:all_proxy=$url' : 'export all_proxy=$url';
}