Skip to content

Commit d798628

Browse files
committed
Fix windows tun issues
Optimize android get system dns
1 parent a06e813 commit d798628

File tree

8 files changed

+56
-32
lines changed

8 files changed

+56
-32
lines changed

android/app/src/main/kotlin/com/follow/clash/extensions/Ext.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fun ConnectivityManager.resolveDns(network: Network?): List<String> {
104104
fun InetAddress.asSocketAddressText(port: Int): String {
105105
return when (this) {
106106
is Inet6Address ->
107-
"[${numericToTextFormat(this.address)}]:$port"
107+
"[${numericToTextFormat(this)}]:$port"
108108

109109
is Inet4Address ->
110110
"${this.hostAddress}:$port"
@@ -141,7 +141,8 @@ fun Context.getActionPendingIntent(action: String): PendingIntent {
141141
}
142142
}
143143

144-
private fun numericToTextFormat(src: ByteArray): String {
144+
private fun numericToTextFormat(address: Inet6Address): String {
145+
val src = address.address
145146
val sb = StringBuilder(39)
146147
for (i in 0 until 8) {
147148
sb.append(
@@ -154,6 +155,10 @@ private fun numericToTextFormat(src: ByteArray): String {
154155
sb.append(":")
155156
}
156157
}
158+
if (address.scopeId > 0) {
159+
sb.append("%")
160+
sb.append(address.scopeId)
161+
}
157162
return sb.toString()
158163
}
159164

android/app/src/main/kotlin/com/follow/clash/plugins/VpnPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ data object VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
100100
}
101101

102102
fun handleStart(options: VpnOptions): Boolean {
103+
onUpdateNetwork();
103104
if (options.enable != this.options?.enable) {
104105
this.flClashService = null
105106
}

lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Future<void> _service(List<String> flags) async {
6262
vpn?.addListener(
6363
_VpnListenerWithService(
6464
onDnsChanged: (String dns) {
65+
print("handle dns $dns");
6566
clashLibHandler.updateDns(dns);
6667
},
6768
),

lib/models/clash_config.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,24 @@ class Tun with _$Tun {
200200
}
201201
}
202202

203+
extension TunExt on Tun {
204+
Tun getRealTun(RouteMode routeMode) {
205+
final mRouteAddress = routeMode == RouteMode.bypassPrivate
206+
? defaultBypassPrivateRouteAddress
207+
: routeAddress;
208+
return switch (system.isDesktop) {
209+
true => copyWith(
210+
autoRoute: true,
211+
routeAddress: [],
212+
),
213+
false => copyWith(
214+
autoRoute: mRouteAddress.isEmpty ? true : false,
215+
routeAddress: mRouteAddress,
216+
),
217+
};
218+
}
219+
}
220+
203221
@freezed
204222
class FallbackFilter with _$FallbackFilter {
205223
const factory FallbackFilter({

lib/providers/generated/state.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/providers/state.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ CoreState coreState(Ref ref) {
105105

106106
@riverpod
107107
UpdateParams updateParams(Ref ref) {
108+
final routeMode = ref.watch(
109+
networkSettingProvider.select(
110+
(state) => state.routeMode,
111+
),
112+
);
108113
return ref.watch(
109114
patchClashConfigProvider.select(
110115
(state) => UpdateParams(
111-
tun: state.tun,
116+
tun: state.tun.getRealTun(routeMode),
112117
allowLan: state.allowLan,
113118
findProcessMode: state.findProcessMode,
114119
mode: state.mode,
@@ -153,9 +158,11 @@ TrayState trayState(Ref ref) {
153158
final appSetting = ref.watch(
154159
appSettingProvider,
155160
);
156-
final groups = ref.watch(
157-
groupsProvider,
158-
);
161+
final groups = ref
162+
.watch(
163+
currentGroupsStateProvider,
164+
)
165+
.value;
159166
final brightness = ref.watch(
160167
appBrightnessProvider,
161168
);

lib/state.dart

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -315,19 +315,9 @@ class GlobalState {
315315
final profileId = profile.id;
316316
final configMap = await getProfileConfig(profileId);
317317
final rawConfig = await handleEvaluate(configMap);
318-
final routeAddress =
319-
config.networkProps.routeMode == RouteMode.bypassPrivate
320-
? defaultBypassPrivateRouteAddress
321-
: patchConfig.tun.routeAddress;
322-
final realPatchConfig = !system.isDesktop
323-
? patchConfig.copyWith.tun(
324-
autoRoute: routeAddress.isEmpty ? true : false,
325-
routeAddress: routeAddress,
326-
)
327-
: patchConfig.copyWith.tun(
328-
autoRoute: true,
329-
routeAddress: [],
330-
);
318+
final realPatchConfig = patchConfig.copyWith(
319+
tun: patchConfig.tun.getRealTun(config.networkProps.routeMode),
320+
);
331321
rawConfig["external-controller"] = realPatchConfig.externalController.value;
332322
rawConfig["external-ui"] = "";
333323
rawConfig["interface-name"] = "";
@@ -411,21 +401,23 @@ class GlobalState {
411401
for (final host in realPatchConfig.hosts.entries) {
412402
rawConfig["hosts"][host.key] = host.value.splitByMultipleSeparators;
413403
}
404+
if (rawConfig["dns"] == null) {
405+
rawConfig["dns"] = {};
406+
}
407+
final isEnableDns = rawConfig["dns"]["enable"] == true;
414408
final overrideDns = globalState.config.overrideDns;
415-
if (overrideDns) {
416-
rawConfig["dns"] = realPatchConfig.dns.toJson();
409+
if (overrideDns || !isEnableDns) {
410+
final dns = switch (!isEnableDns) {
411+
true => realPatchConfig.dns.copyWith(
412+
nameserver: [...realPatchConfig.dns.nameserver, "system://"]),
413+
false => realPatchConfig.dns,
414+
};
415+
rawConfig["dns"] = dns.toJson();
417416
rawConfig["dns"]["nameserver-policy"] = {};
418-
for (final entry in realPatchConfig.dns.nameserverPolicy.entries) {
417+
for (final entry in dns.nameserverPolicy.entries) {
419418
rawConfig["dns"]["nameserver-policy"][entry.key] =
420419
entry.value.splitByMultipleSeparators;
421420
}
422-
} else {
423-
if (rawConfig["dns"] == null) {
424-
rawConfig["dns"] = {};
425-
}
426-
if (rawConfig["dns"]["enable"] != false) {
427-
rawConfig["dns"]["enable"] = true;
428-
}
429421
}
430422
var rules = [];
431423
if (rawConfig["rules"] != null) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: fl_clash
22
description: A multi-platform proxy client based on ClashMeta, simple and easy to use, open-source and ad-free.
33
publish_to: 'none'
4-
version: 0.8.85+202506071
4+
version: 0.8.86+202506141
55
environment:
66
sdk: '>=3.1.0 <4.0.0'
77

0 commit comments

Comments
 (0)