Skip to content

Commit d3d0c12

Browse files
committed
Optimize startTun performance
1 parent be8cf03 commit d3d0c12

File tree

12 files changed

+136
-54
lines changed

12 files changed

+136
-54
lines changed

android/app/src/main/kotlin/com/follow/clash/services/FlClashVpnService.kt

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
3434
if (options.ipv4Address.isNotEmpty()) {
3535
val cidr = options.ipv4Address.toCIDR()
3636
addAddress(cidr.address, cidr.prefixLength)
37+
Log.d(
38+
"addAddress",
39+
"address: ${cidr.address} prefixLength:${cidr.prefixLength}"
40+
)
3741
val routeAddress = options.getIpv4RouteAddress()
3842
if (routeAddress.isNotEmpty()) {
3943
try {
@@ -50,26 +54,39 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
5054
} else {
5155
addRoute("0.0.0.0", 0)
5256
}
57+
} else {
58+
addRoute("0.0.0.0", 0)
5359
}
54-
if (options.ipv6Address.isNotEmpty()) {
55-
val cidr = options.ipv6Address.toCIDR()
56-
addAddress(cidr.address, cidr.prefixLength)
57-
val routeAddress = options.getIpv6RouteAddress()
58-
if (routeAddress.isNotEmpty()) {
59-
try {
60-
routeAddress.forEach { i ->
61-
Log.d(
62-
"addRoute6",
63-
"address: ${i.address} prefixLength:${i.prefixLength}"
64-
)
65-
addRoute(i.address, i.prefixLength)
60+
try {
61+
if (options.ipv6Address.isNotEmpty()) {
62+
val cidr = options.ipv6Address.toCIDR()
63+
Log.d(
64+
"addAddress6",
65+
"address: ${cidr.address} prefixLength:${cidr.prefixLength}"
66+
)
67+
addAddress(cidr.address, cidr.prefixLength)
68+
val routeAddress = options.getIpv6RouteAddress()
69+
if (routeAddress.isNotEmpty()) {
70+
try {
71+
routeAddress.forEach { i ->
72+
Log.d(
73+
"addRoute6",
74+
"address: ${i.address} prefixLength:${i.prefixLength}"
75+
)
76+
addRoute(i.address, i.prefixLength)
77+
}
78+
} catch (_: Exception) {
79+
addRoute("::", 0)
6680
}
67-
} catch (_: Exception) {
81+
} else {
6882
addRoute("::", 0)
6983
}
70-
} else {
71-
addRoute("::", 0)
7284
}
85+
}catch (_:Exception){
86+
Log.d(
87+
"addAddress6",
88+
"IPv6 is not supported."
89+
)
7390
}
7491
addDnsServer(options.dnsServerAddress)
7592
setMtu(9000)

core/lib_android.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,27 @@ func handleStopTun() {
9999
}
100100

101101
func handleStartTun(fd int, callback unsafe.Pointer) bool {
102-
handleStopTun()
103-
now := time.Now()
104-
runTime = &now
105-
if fd != 0 {
106-
tunLock.Lock()
107-
defer tunLock.Unlock()
108-
tunHandler = &TunHandler{
109-
callback: callback,
110-
limit: semaphore.NewWeighted(4),
111-
}
112-
initTunHook()
113-
tunListener, _ := t.Start(fd, currentConfig.General.Tun.Device, currentConfig.General.Tun.Stack)
114-
if tunListener != nil {
115-
log.Infoln("TUN address: %v", tunListener.Address())
116-
} else {
117-
removeTunHook()
118-
return false
102+
go func() {
103+
handleStopTun()
104+
now := time.Now()
105+
runTime = &now
106+
if fd != 0 {
107+
tunLock.Lock()
108+
defer tunLock.Unlock()
109+
tunHandler = &TunHandler{
110+
callback: callback,
111+
limit: semaphore.NewWeighted(4),
112+
}
113+
initTunHook()
114+
tunListener, _ := t.Start(fd, currentConfig.General.Tun.Device, currentConfig.General.Tun.Stack)
115+
if tunListener != nil {
116+
log.Infoln("TUN address: %v", tunListener.Address())
117+
tunHandler.listener = tunListener
118+
} else {
119+
removeTunHook()
120+
}
119121
}
120-
tunHandler.listener = tunListener
121-
}
122+
}()
122123
return true
123124
}
124125

lib/fragments/config/network.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,11 @@ class RouteAddressItem extends ConsumerWidget {
301301
title: appLocalizations.routeAddress,
302302
widget: Consumer(
303303
builder: (_, ref, __) {
304-
final routeAddress = ref.watch(patchClashConfigProvider
305-
.select((state) => state.tun.routeAddress));
304+
final routeAddress = ref.watch(
305+
patchClashConfigProvider.select(
306+
(state) => state.tun.routeAddress,
307+
),
308+
);
306309
return ListInputPage(
307310
title: appLocalizations.routeAddress,
308311
items: routeAddress,
@@ -371,7 +374,9 @@ class NetworkListView extends ConsumerWidget {
371374
return;
372375
}
373376
ref.read(vpnSettingProvider.notifier).updateState(
374-
(state) => defaultVpnProps,
377+
(state) => defaultVpnProps.copyWith(
378+
accessControl: state.accessControl,
379+
),
375380
);
376381
ref.read(patchClashConfigProvider.notifier).updateState(
377382
(state) => state.copyWith(

lib/models/clash_config.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class Tun with _$Tun {
147147
const factory Tun({
148148
@Default(false) bool enable,
149149
@Default(appName) String device,
150+
@JsonKey(name: "auto-route") @Default(false) bool autoRoute,
150151
@Default(TunStack.gvisor) TunStack stack,
151152
@JsonKey(name: "dns-hijack") @Default(["any:53"]) List<String> dnsHijack,
152153
@JsonKey(name: "route-address") @Default([]) List<String> routeAddress,

lib/models/generated/clash_config.freezed.dart

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

lib/models/generated/clash_config.g.dart

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

0 commit comments

Comments
 (0)