Skip to content

Commit

Permalink
Fix android system dns issues
Browse files Browse the repository at this point in the history
Optimize dns default option

Fix some issues
  • Loading branch information
chen08209 committed Sep 20, 2024
1 parent 2c5f852 commit 30dc3f4
Show file tree
Hide file tree
Showing 53 changed files with 275 additions and 220 deletions.
8 changes: 8 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@

<data android:host="install-config" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.follow.clash.action.START" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.follow.clash.action.STOP" />
</intent-filter>
</activity>

<!-- <meta-data-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object GlobalState {
return currentEngine?.plugins?.get(AppPlugin::class.java) as AppPlugin?
}

fun getCurrentTitlePlugin(): TilePlugin? {
fun getCurrentTilePlugin(): TilePlugin? {
val currentEngine = if (flutterEngine != null) flutterEngine else serviceEngine
return currentEngine?.plugins?.get(TilePlugin::class.java) as TilePlugin?
}
Expand Down
14 changes: 14 additions & 0 deletions android/app/src/main/kotlin/com/follow/clash/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.follow.clash


import android.content.Intent
import android.os.Bundle
import com.follow.clash.plugins.AppPlugin
import com.follow.clash.plugins.ServicePlugin
import com.follow.clash.plugins.VpnPlugin
Expand All @@ -9,6 +11,18 @@ import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity : FlutterActivity() {
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
when (intent.action) {
"com.follow.clash.action.START" -> {
GlobalState.getCurrentTilePlugin()?.handleStart()
}

"com.follow.clash.action.STOP" -> {
GlobalState.getCurrentTilePlugin()?.handleStop()
}
}
}

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ fun Metadata.getProtocol(): Int? {
}


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

fun InetAddress.asSocketAddressText(port: Int): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.follow.clash.BaseServiceInterface
import com.follow.clash.GlobalState
import com.follow.clash.RunState
import com.follow.clash.extensions.getProtocol
import com.follow.clash.extensions.resolvePrimaryDns
import com.follow.clash.extensions.resolveDns
import com.follow.clash.models.Props
import com.follow.clash.models.TunProps
import com.follow.clash.services.FlClashService
Expand Down Expand Up @@ -177,9 +177,11 @@ class VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
val networks = mutableSetOf<Network>()

fun onUpdateNetwork() {
val dns = networks.mapNotNull {
connectivity?.resolvePrimaryDns(it)
}.joinToString(separator = ",")
val dns = networks.flatMap { network ->
connectivity?.resolveDns(network) ?: emptyList()
}
.toSet()
.joinToString(",")
scope.launch {
withContext(Dispatchers.Main) {
flutterMethodChannel.invokeMethod("dnsChanged", dns)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ class FlClashTileService : TileService() {
activityTransfer()
if (GlobalState.runState.value == RunState.STOP) {
GlobalState.runState.value = RunState.PENDING
val titlePlugin = GlobalState.getCurrentTitlePlugin()
if (titlePlugin != null) {
titlePlugin.handleStart()
val tilePlugin = GlobalState.getCurrentTilePlugin()
if (tilePlugin != null) {
tilePlugin.handleStart()
} else {
GlobalState.initServiceEngine(applicationContext)
}
} else if (GlobalState.runState.value == RunState.START) {
GlobalState.runState.value = RunState.PENDING
GlobalState.getCurrentTitlePlugin()?.handleStop()
GlobalState.getCurrentTilePlugin()?.handleStop()
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
PendingIntent.FLAG_UPDATE_CURRENT
)
}

val stopPendingIntent = if (Build.VERSION.SDK_INT >= 31) {
PendingIntent.getActivity(
this,
0,
Intent("com.follow.clash.action.STOP"),
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
} else {
PendingIntent.getActivity(
this,
0,
Intent("com.follow.clash.action.STOP"),
PendingIntent.FLAG_UPDATE_CURRENT
)
}
with(NotificationCompat.Builder(this, CHANNEL)) {
setSmallIcon(R.drawable.ic_stat_name)
setContentTitle("FlClash")
Expand All @@ -172,6 +188,7 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
setShowWhen(false)
setOnlyAlertOnce(true)
setAutoCancel(true)
addAction(0, "Stop", stopPendingIntent);
}
}

Expand Down Expand Up @@ -210,7 +227,7 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
val isSuccess = super.onTransact(code, data, reply, flags)
if (!isSuccess) {
CoroutineScope(Dispatchers.Main).launch {
GlobalState.getCurrentTitlePlugin()?.handleStop()
GlobalState.getCurrentTilePlugin()?.handleStop()
}
}
return isSuccess
Expand Down
2 changes: 1 addition & 1 deletion core/tun/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Start(tunProps Props) (*sing_tun.Listener, error) {
options := LC.Tun{
Enable: true,
Device: sing_tun.InterfaceName,
Stack: constant.TunSystem,
Stack: constant.TunMixed,
DNSHijack: dnsHijack,
AutoRoute: false,
AutoDetectInterface: false,
Expand Down
17 changes: 13 additions & 4 deletions lib/application.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:dynamic_color/dynamic_color.dart';
import 'package:fl_clash/l10n/l10n.dart';
import 'package:fl_clash/common/common.dart';
Expand Down Expand Up @@ -29,6 +27,9 @@ runAppWithPreferences(
ChangeNotifierProvider<Config>(
create: (_) => config,
),
ChangeNotifierProvider<AppFlowingState>(
create: (_) => AppFlowingState(),
),
ChangeNotifierProxyProvider2<Config, ClashConfig, AppState>(
create: (_) => appState,
update: (_, config, clashConfig, appState) {
Expand Down Expand Up @@ -85,6 +86,7 @@ class ApplicationState extends State<Application> {
super.initState();
_initTimer();
globalState.appController = AppController(context);
globalState.measure = Measure.of(context);
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
final currentContext = globalState.navigatorKey.currentContext;
if (currentContext != null) {
Expand Down Expand Up @@ -179,8 +181,15 @@ class ApplicationState extends State<Application> {
GlobalWidgetsLocalizations.delegate
],
builder: (_, child) {
return MediaManager(
child: _buildPage(child!),
return LayoutBuilder(
builder: (_, container) {
final appController = globalState.appController;
final maxWidth = container.maxWidth;
if (appController.appState.viewWidth != maxWidth) {
globalState.appController.updateViewWidth(maxWidth);
}
return _buildPage(child!);
},
);
},
scrollBehavior: BaseScrollBehavior(),
Expand Down
2 changes: 1 addition & 1 deletion lib/common/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension BuildContextExtension on BuildContext {
return MediaQuery.of(this).size;
}

double get width {
double get viewWidth {
return appSize.width;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/common/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ class FlClashHttpOverrides extends HttpOverrides {
client.badCertificateCallback = (_, __, ___) => true;
client.findProxy = (url) {
debugPrint("find $url");
final port = globalState.appController.clashConfig.mixedPort;
final isStart = globalState.appController.appState.isStart;
final appController = globalState.appController;
final port = appController.clashConfig.mixedPort;
final isStart = appController.appFlowingState.isStart;
if (!isStart) return "DIRECT";
if (appController.appState.groups.isEmpty) {
return "DIRECT";
}
return "PROXY localhost:$port";
};
return client;
Expand Down
2 changes: 1 addition & 1 deletion lib/common/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ extension DoubleListExt on List<double> {
}
}

return -1; // 这行理论上不会执行到,但为了完整性保留
return -1;
}
}
4 changes: 3 additions & 1 deletion lib/common/measure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class Measure {
final TextScaler _textScale;
late BuildContext context;

Measure.of(this.context) : _textScale = MediaQuery.of(context).textScaler;
Measure.of(this.context)
: _textScale = TextScaler.linear(
WidgetsBinding.instance.platformDispatcher.textScaleFactor);

Size computeTextSize(Text text) {
final textPainter = TextPainter(
Expand Down
6 changes: 6 additions & 0 deletions lib/common/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ class Other {
String getBackupFileName() {
return "${appName}_backup_${DateTime.now().show}.zip";
}

Size getScreenSize() {
final view = WidgetsBinding.instance.platformDispatcher.views.first;
return view.physicalSize / view.devicePixelRatio;
}

}

final other = Other();
1 change: 0 additions & 1 deletion lib/common/system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:fl_clash/plugins/app.dart';
import 'package:flutter/services.dart';
import 'package:window_manager/window_manager.dart';

import 'window.dart';

Expand Down
20 changes: 11 additions & 9 deletions lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'common/common.dart';
class AppController {
final BuildContext context;
late AppState appState;
late AppFlowingState appFlowingState;
late Config config;
late ClashConfig clashConfig;
late Function updateClashConfigDebounce;
Expand All @@ -30,6 +31,7 @@ class AppController {
appState = context.read<AppState>();
config = context.read<Config>();
clashConfig = context.read<ClashConfig>();
appFlowingState = context.read<AppFlowingState>();
updateClashConfigDebounce = debounce<Function()>(() async {
await updateClashConfig();
});
Expand Down Expand Up @@ -60,9 +62,9 @@ class AppController {
} else {
await globalState.handleStop();
clashCore.resetTraffic();
appState.traffics = [];
appState.totalTraffic = Traffic();
appState.runTime = null;
appFlowingState.traffics = [];
appFlowingState.totalTraffic = Traffic();
appFlowingState.runTime = null;
addCheckIpNumDebounce();
}
}
Expand All @@ -76,15 +78,15 @@ class AppController {
if (startTime != null) {
final startTimeStamp = startTime.millisecondsSinceEpoch;
final nowTimeStamp = DateTime.now().millisecondsSinceEpoch;
appState.runTime = nowTimeStamp - startTimeStamp;
appFlowingState.runTime = nowTimeStamp - startTimeStamp;
} else {
appState.runTime = null;
appFlowingState.runTime = null;
}
}

updateTraffic() {
globalState.updateTraffic(
appState: appState,
appFlowingState: appFlowingState,
);
}

Expand Down Expand Up @@ -163,7 +165,7 @@ class AppController {
try {
updateProfile(profile);
} catch (e) {
appState.addLog(
appFlowingState.addLog(
Log(
logLevel: LogLevel.info,
payload: e.toString(),
Expand Down Expand Up @@ -241,7 +243,7 @@ class AppController {
clashCore.startLog();
} else {
clashCore.stopLog();
appState.logs = [];
appFlowingState.logs = [];
}
}

Expand Down Expand Up @@ -546,7 +548,7 @@ class AppController {
}

updateStart() {
updateStatus(!appState.isStart);
updateStatus(!appFlowingState.isStart);
}

updateAutoLaunch() {
Expand Down
2 changes: 0 additions & 2 deletions lib/fragments/config/app.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/models/config.dart';
import 'package:fl_clash/state.dart';
Expand Down
11 changes: 9 additions & 2 deletions lib/fragments/config/dns.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:collection/collection.dart';
import 'package:fl_clash/common/app_localizations.dart';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/models/models.dart';
Expand All @@ -18,7 +17,15 @@ class OverrideItem extends StatelessWidget {
commonScaffoldState?.actions = [
IconButton(
onPressed: () {
globalState.appController.clashConfig.dns = const Dns();
globalState.showMessage(
title: appLocalizations.resetDns,
message: TextSpan(
text: appLocalizations.dnsResetTip,
),
onTab: () {
globalState.appController.clashConfig.dns = const Dns();
Navigator.of(context).pop();
});
},
tooltip: appLocalizations.resetDns,
icon: const Icon(
Expand Down
1 change: 0 additions & 1 deletion lib/fragments/dashboard/dashboard.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:io';
import 'dart:math';

import 'package:fl_clash/common/common.dart';
Expand Down
3 changes: 2 additions & 1 deletion lib/fragments/dashboard/network_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class _NetworkDetectionState extends State<NetworkDetection> {

_checkIp() async {
final appState = globalState.appController.appState;
final appFlowingState = globalState.appController.appFlowingState;
final isInit = appState.isInit;
if (!isInit) return;
final isStart = appState.isStart;
final isStart = appFlowingState.isStart;
if (_preIsStart == false && _preIsStart == isStart) return;
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/fragments/dashboard/network_speed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class _NetworkSpeedState extends State<NetworkSpeed> {
label: appLocalizations.networkSpeed,
iconData: Icons.speed_sharp,
),
child: Selector<AppState, List<Traffic>>(
selector: (_, appState) => appState.traffics,
child: Selector<AppFlowingState, List<Traffic>>(
selector: (_, appFlowingState) => appFlowingState.traffics,
builder: (_, traffics, __) {
return Container(
padding: const EdgeInsets.all(16),
Expand Down
Loading

0 comments on commit 30dc3f4

Please sign in to comment.