Skip to content

Commit 30dc3f4

Browse files
committed
Fix android system dns issues
Optimize dns default option Fix some issues
1 parent 2c5f852 commit 30dc3f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+275
-220
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464

6565
<data android:host="install-config" />
6666
</intent-filter>
67+
<intent-filter>
68+
<category android:name="android.intent.category.DEFAULT" />
69+
<action android:name="com.follow.clash.action.START" />
70+
</intent-filter>
71+
<intent-filter>
72+
<category android:name="android.intent.category.DEFAULT" />
73+
<action android:name="com.follow.clash.action.STOP" />
74+
</intent-filter>
6775
</activity>
6876

6977
<!-- <meta-data-->

android/app/src/main/kotlin/com/follow/clash/GlobalState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object GlobalState {
3333
return currentEngine?.plugins?.get(AppPlugin::class.java) as AppPlugin?
3434
}
3535

36-
fun getCurrentTitlePlugin(): TilePlugin? {
36+
fun getCurrentTilePlugin(): TilePlugin? {
3737
val currentEngine = if (flutterEngine != null) flutterEngine else serviceEngine
3838
return currentEngine?.plugins?.get(TilePlugin::class.java) as TilePlugin?
3939
}

android/app/src/main/kotlin/com/follow/clash/MainActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.follow.clash
22

33

4+
import android.content.Intent
5+
import android.os.Bundle
46
import com.follow.clash.plugins.AppPlugin
57
import com.follow.clash.plugins.ServicePlugin
68
import com.follow.clash.plugins.VpnPlugin
@@ -9,6 +11,18 @@ import io.flutter.embedding.android.FlutterActivity
911
import io.flutter.embedding.engine.FlutterEngine
1012

1113
class MainActivity : FlutterActivity() {
14+
override fun onNewIntent(intent: Intent) {
15+
super.onNewIntent(intent)
16+
when (intent.action) {
17+
"com.follow.clash.action.START" -> {
18+
GlobalState.getCurrentTilePlugin()?.handleStart()
19+
}
20+
21+
"com.follow.clash.action.STOP" -> {
22+
GlobalState.getCurrentTilePlugin()?.handleStop()
23+
}
24+
}
25+
}
1226

1327
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
1428
super.configureFlutterEngine(flutterEngine)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ fun Metadata.getProtocol(): Int? {
3434
}
3535

3636

37-
fun ConnectivityManager.resolvePrimaryDns(network: Network?): String? {
38-
val properties = getLinkProperties(network) ?: return null
39-
return properties.dnsServers.firstOrNull()?.asSocketAddressText(53)
37+
fun ConnectivityManager.resolveDns(network: Network?): List<String> {
38+
val properties = getLinkProperties(network) ?: return listOf()
39+
return properties.dnsServers.map { it.asSocketAddressText(53) }
4040
}
4141

4242
fun InetAddress.asSocketAddressText(port: Int): String {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.follow.clash.BaseServiceInterface
1616
import com.follow.clash.GlobalState
1717
import com.follow.clash.RunState
1818
import com.follow.clash.extensions.getProtocol
19-
import com.follow.clash.extensions.resolvePrimaryDns
19+
import com.follow.clash.extensions.resolveDns
2020
import com.follow.clash.models.Props
2121
import com.follow.clash.models.TunProps
2222
import com.follow.clash.services.FlClashService
@@ -177,9 +177,11 @@ class VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
177177
val networks = mutableSetOf<Network>()
178178

179179
fun onUpdateNetwork() {
180-
val dns = networks.mapNotNull {
181-
connectivity?.resolvePrimaryDns(it)
182-
}.joinToString(separator = ",")
180+
val dns = networks.flatMap { network ->
181+
connectivity?.resolveDns(network) ?: emptyList()
182+
}
183+
.toSet()
184+
.joinToString(",")
183185
scope.launch {
184186
withContext(Dispatchers.Main) {
185187
flutterMethodChannel.invokeMethod("dnsChanged", dns)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ class FlClashTileService : TileService() {
6767
activityTransfer()
6868
if (GlobalState.runState.value == RunState.STOP) {
6969
GlobalState.runState.value = RunState.PENDING
70-
val titlePlugin = GlobalState.getCurrentTitlePlugin()
71-
if (titlePlugin != null) {
72-
titlePlugin.handleStart()
70+
val tilePlugin = GlobalState.getCurrentTilePlugin()
71+
if (tilePlugin != null) {
72+
tilePlugin.handleStart()
7373
} else {
7474
GlobalState.initServiceEngine(applicationContext)
7575
}
7676
} else if (GlobalState.runState.value == RunState.START) {
7777
GlobalState.runState.value = RunState.PENDING
78-
GlobalState.getCurrentTitlePlugin()?.handleStop()
78+
GlobalState.getCurrentTilePlugin()?.handleStop()
7979
}
8080

8181
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
159159
PendingIntent.FLAG_UPDATE_CURRENT
160160
)
161161
}
162+
163+
val stopPendingIntent = if (Build.VERSION.SDK_INT >= 31) {
164+
PendingIntent.getActivity(
165+
this,
166+
0,
167+
Intent("com.follow.clash.action.STOP"),
168+
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
169+
)
170+
} else {
171+
PendingIntent.getActivity(
172+
this,
173+
0,
174+
Intent("com.follow.clash.action.STOP"),
175+
PendingIntent.FLAG_UPDATE_CURRENT
176+
)
177+
}
162178
with(NotificationCompat.Builder(this, CHANNEL)) {
163179
setSmallIcon(R.drawable.ic_stat_name)
164180
setContentTitle("FlClash")
@@ -172,6 +188,7 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
172188
setShowWhen(false)
173189
setOnlyAlertOnce(true)
174190
setAutoCancel(true)
191+
addAction(0, "Stop", stopPendingIntent);
175192
}
176193
}
177194

@@ -210,7 +227,7 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
210227
val isSuccess = super.onTransact(code, data, reply, flags)
211228
if (!isSuccess) {
212229
CoroutineScope(Dispatchers.Main).launch {
213-
GlobalState.getCurrentTitlePlugin()?.handleStop()
230+
GlobalState.getCurrentTilePlugin()?.handleStop()
214231
}
215232
}
216233
return isSuccess

core/tun/tun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Start(tunProps Props) (*sing_tun.Listener, error) {
4747
options := LC.Tun{
4848
Enable: true,
4949
Device: sing_tun.InterfaceName,
50-
Stack: constant.TunSystem,
50+
Stack: constant.TunMixed,
5151
DNSHijack: dnsHijack,
5252
AutoRoute: false,
5353
AutoDetectInterface: false,

lib/application.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import 'dart:async';
2-
import 'dart:io';
3-
42
import 'package:dynamic_color/dynamic_color.dart';
53
import 'package:fl_clash/l10n/l10n.dart';
64
import 'package:fl_clash/common/common.dart';
@@ -29,6 +27,9 @@ runAppWithPreferences(
2927
ChangeNotifierProvider<Config>(
3028
create: (_) => config,
3129
),
30+
ChangeNotifierProvider<AppFlowingState>(
31+
create: (_) => AppFlowingState(),
32+
),
3233
ChangeNotifierProxyProvider2<Config, ClashConfig, AppState>(
3334
create: (_) => appState,
3435
update: (_, config, clashConfig, appState) {
@@ -85,6 +86,7 @@ class ApplicationState extends State<Application> {
8586
super.initState();
8687
_initTimer();
8788
globalState.appController = AppController(context);
89+
globalState.measure = Measure.of(context);
8890
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
8991
final currentContext = globalState.navigatorKey.currentContext;
9092
if (currentContext != null) {
@@ -179,8 +181,15 @@ class ApplicationState extends State<Application> {
179181
GlobalWidgetsLocalizations.delegate
180182
],
181183
builder: (_, child) {
182-
return MediaManager(
183-
child: _buildPage(child!),
184+
return LayoutBuilder(
185+
builder: (_, container) {
186+
final appController = globalState.appController;
187+
final maxWidth = container.maxWidth;
188+
if (appController.appState.viewWidth != maxWidth) {
189+
globalState.appController.updateViewWidth(maxWidth);
190+
}
191+
return _buildPage(child!);
192+
},
184193
);
185194
},
186195
scrollBehavior: BaseScrollBehavior(),

lib/common/context.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension BuildContextExtension on BuildContext {
1111
return MediaQuery.of(this).size;
1212
}
1313

14-
double get width {
14+
double get viewWidth {
1515
return appSize.width;
1616
}
1717

0 commit comments

Comments
 (0)