[go: up one dir, main page]

Skip to content

Commit

Permalink
Add DNS override
Browse files Browse the repository at this point in the history
Fixed some bugs
Optimize more detail
  • Loading branch information
chen08209 committed Sep 2, 2024
1 parent b685165 commit ac39739
Show file tree
Hide file tree
Showing 59 changed files with 4,057 additions and 1,004 deletions.
Binary file added assets/fonts/Icons.ttf
Binary file not shown.
2 changes: 1 addition & 1 deletion core/Clash.Meta
35 changes: 24 additions & 11 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ConfigExtendedParams struct {
IsCompatible bool `json:"is-compatible"`
SelectedMap map[string]string `json:"selected-map"`
TestURL *string `json:"test-url"`
OverrideDns bool `json:"override-dns"`
}

type GenerateConfigParams struct {
Expand Down Expand Up @@ -380,14 +381,19 @@ func generateProxyGroupAndRule(proxyGroup *[]map[string]any, rule *[]string) {
*rule = computedRule
}

func genHosts(hosts, patchHosts map[string]any) {
for k, v := range patchHosts {
hosts[k] = v
}
}

func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfig) {
targetConfig.ExternalController = patchConfig.ExternalController
targetConfig.ExternalUI = ""
targetConfig.Interface = ""
targetConfig.ExternalUIURL = ""
targetConfig.TCPConcurrent = patchConfig.TCPConcurrent
targetConfig.UnifiedDelay = patchConfig.UnifiedDelay
//targetConfig.GeodataMode = false
targetConfig.IPv6 = patchConfig.IPv6
targetConfig.LogLevel = patchConfig.LogLevel
targetConfig.Port = 0
Expand All @@ -405,19 +411,23 @@ func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfi
targetConfig.Profile.StoreSelected = false
targetConfig.GeoXUrl = patchConfig.GeoXUrl
targetConfig.GlobalUA = patchConfig.GlobalUA
if targetConfig.DNS.Enable == false {
//if targetConfig.DNS.Enable == false {
// targetConfig.DNS = patchConfig.DNS
//}
genHosts(targetConfig.Hosts, patchConfig.Hosts)
if configParams.OverrideDns {
targetConfig.DNS = patchConfig.DNS
}
//if runtime.GOOS == "android" {
// targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, "dhcp://"+dns.SystemDNSPlaceholder)
//} else if runtime.GOOS == "windows" {
// targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, dns.SystemDNSPlaceholder)
//}
if configParams.IsCompatible == false {
targetConfig.ProxyProvider = make(map[string]map[string]any)
targetConfig.RuleProvider = make(map[string]map[string]any)
generateProxyGroupAndRule(&targetConfig.ProxyGroup, &targetConfig.Rule)
}
//if configParams.IsCompatible == false {
// targetConfig.ProxyProvider = make(map[string]map[string]any)
// targetConfig.RuleProvider = make(map[string]map[string]any)
// generateProxyGroupAndRule(&targetConfig.ProxyGroup, &targetConfig.Rule)
//}
}

func patchConfig(general *config.General) {
Expand All @@ -440,6 +450,11 @@ var isRunning = false
var runLock sync.Mutex

func updateListeners(general *config.General, listeners map[string]constant.InboundListener) {
if !isRunning {
return
}
runLock.Lock()
defer runLock.Unlock()
listener.PatchInboundListeners(listeners, tunnel.Tunnel, true)
listener.SetAllowLan(general.AllowLan)
inbound.SetSkipAuthPrefixes(general.SkipAuthPrefixes)
Expand Down Expand Up @@ -525,10 +540,8 @@ func applyConfig() error {
hub.UltraApplyConfig(cfg)
patchSelectGroup()
}
if isRunning {
updateListeners(cfg.General, cfg.Listeners)
hcCompatibleProvider(cfg.Providers)
}
updateListeners(cfg.General, cfg.Listeners)
hcCompatibleProvider(cfg.Providers)
externalProviders = getExternalProvidersRaw()
return err
}
9 changes: 6 additions & 3 deletions core/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ func start() {
//export stop
func stop() {
runLock.Lock()
defer runLock.Unlock()
isRunning = false
stopListeners()
go func() {
defer runLock.Unlock()
isRunning = false
stopListeners()
}()
}

//export initClash
Expand Down Expand Up @@ -236,6 +238,7 @@ func asyncTestDelay(s *C.char, port C.longlong) {

proxies := tunnel.ProxiesWithProviders()
proxy := proxies[params.ProxyName]
proxy.Name()

delayData := &Delay{
Name: params.ProxyName,
Expand Down
16 changes: 12 additions & 4 deletions lib/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ class ApplicationState extends State<Application> {
);
}

_buildPage(Widget page) {
if (system.isDesktop) {
return WindowHeaderContainer(
child: page,
);
}
return VpnContainer(
child: page,
);
}

_updateSystemColorSchemes(
ColorScheme? lightDynamic,
ColorScheme? darkDynamic,
Expand Down Expand Up @@ -147,10 +158,7 @@ class ApplicationState extends State<Application> {
GlobalWidgetsLocalizations.delegate
],
builder: (_, child) {
if (system.isDesktop) {
return WindowHeaderContainer(child: child!);
}
return child!;
return _buildPage(child!);
},
scrollBehavior: BaseScrollBehavior(),
title: appName,
Expand Down
4 changes: 3 additions & 1 deletion lib/common/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export 'package.dart';
export 'measure.dart';
export 'windows.dart';
export 'iterable.dart';
export 'scroll.dart';
export 'scroll.dart';
export 'icons.dart';
export 'http.dart';
19 changes: 19 additions & 0 deletions lib/common/http.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'dart:io';

import '../state.dart';

class FlClashHttpOverrides extends HttpOverrides {

@override
HttpClient createHttpClient(SecurityContext? context) {
final client = super.createHttpClient(context);
client.badCertificateCallback = (_, __, ___) => true;
client.findProxy = (url) {
final port = globalState.appController.clashConfig.mixedPort;
final isStart = globalState.appController.appState.isStart;
if(!isStart) return "DIRECT";
return "PROXY localhost:$port;DIRECT";
};
return client;
}
}
6 changes: 6 additions & 0 deletions lib/common/icons.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:flutter/material.dart';

class IconsExt{
static const IconData target =
IconData(0xe900, fontFamily: "Icons");
}
7 changes: 7 additions & 0 deletions lib/common/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:typed_data';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/enum/enum.dart';
import 'package:flutter/material.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:zxing2/qrcode.dart';
import 'package:image/image.dart' as img;

Expand Down Expand Up @@ -130,6 +131,12 @@ class Other {
return build1.compareTo(build2);
}

String getPinyin(String value) {
return value.isNotEmpty
? PinyinHelper.getFirstWordPinyin(value.substring(0, 1))
: "";
}

Future<String?> parseQRCode(Uint8List? bytes) {
return Isolate.run<String?>(() {
if (bytes == null) return null;
Expand Down
44 changes: 17 additions & 27 deletions lib/common/request.dart
Original file line number Diff line number Diff line change
@@ -1,50 +1,27 @@
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/models/ip.dart';
import 'package:fl_clash/state.dart';
import 'package:flutter/cupertino.dart';

class Request {
late final Dio _dio;
int? _port;
bool _isStart = false;
String? userAgent;

Request() {
_dio = Dio();
_dio.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) {
_updateAdapter();
return handler.next(options); // 继续请求
},
),
);
}

_updateAdapter() {
final port = globalState.appController.clashConfig.mixedPort;
final isStart = globalState.appController.appState.isStart;
if (_port != port || isStart != _isStart) {
_port = port;
_isStart = isStart;
_dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
final client = HttpClient();
if (!_isStart) return client;
client.userAgent = globalState.appController.clashConfig.globalUa;
client.findProxy = (url) {
return "PROXY localhost:$_port;DIRECT";
};
return client;
},
validateCertificate: (_, __, ___) => true,
);
}
}

Future<Response> getFileResponseForUrl(String url) async {
final response = await _dio
.get(
Expand All @@ -62,6 +39,19 @@ class Request {
return response;
}

Future<MemoryImage?> getImage(String url) async {
if (url.isEmpty) return null;
final response = await _dio.get<Uint8List>(
url,
options: Options(
responseType: ResponseType.bytes,
),
);
final data = response.data;
if (data == null) return null;
return MemoryImage(data);
}

Future<Map<String, dynamic>?> checkForUpdate() async {
final response = await _dio.get(
"https://api.github.com/repos/$repository/releases/latest",
Expand Down Expand Up @@ -101,7 +91,7 @@ class Request {
return source.value(response.data!);
}
} catch (e) {
if(cancelToken?.isCancelled == true){
if (cancelToken?.isCancelled == true) {
throw "cancelled";
}
continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/common/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Window {
// await windowManager.setTitleBarStyle(TitleBarStyle.hidden);
// }
await windowManager.waitUntilReadyToShow(windowOptions, () async {
// await windowManager.setPreventClose(true);
await windowManager.setPreventClose(true);
});
}

Expand Down
9 changes: 7 additions & 2 deletions lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ class AppController {
);
}

updateTray(){

}

Future applyProfile({bool isPrue = false}) async {
if (isPrue) {
await globalState.applyProfile(
Expand Down Expand Up @@ -232,6 +236,7 @@ class AppController {

handleExit() async {
await updateStatus(false);
await proxy?.stopProxy();
await savePreferences();
clashCore.shutdown();
system.exit();
Expand Down Expand Up @@ -433,8 +438,8 @@ class AppController {
return List.of(proxies)
..sort(
(a, b) => other.sortByChar(
PinyinHelper.getPinyin(a.name),
PinyinHelper.getPinyin(b.name),
other.getPinyin(a.name),
other.getPinyin(b.name),
),
);
}
Expand Down
15 changes: 14 additions & 1 deletion lib/enum/enum.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: constant_identifier_names

import 'package:freezed_annotation/freezed_annotation.dart';

enum GroupType { Selector, URLTest, Fallback, LoadBalance, Relay }

enum GroupName { GLOBAL, Proxy, Auto, Fallback }
Expand Down Expand Up @@ -86,6 +88,17 @@ enum CommonCardType { plain, filled }

enum ProxiesType { tab, list }

enum ProxiesLayout{ loose, standard, tight }
enum ProxiesLayout { loose, standard, tight }

enum ProxyCardType { expand, shrink, min }


enum DnsMode {
normal,
@JsonValue("fake-ip")
fakeIp,
@JsonValue("redir-host")
redirHost,
hosts
}

36 changes: 32 additions & 4 deletions lib/fragments/config/app.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:fl_clash/common/app_localizations.dart';
import 'dart:io';

import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/models/config.dart';
import 'package:fl_clash/state.dart';
import 'package:fl_clash/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' show dirname, join;
import 'package:provider/provider.dart';

class CloseConnectionsSwitch extends StatelessWidget {
Expand Down Expand Up @@ -55,7 +58,32 @@ class UsageSwitch extends StatelessWidget {
}
}

const appItems = [
CloseConnectionsSwitch(),
UsageSwitch(),
class UWPLoopbackUtil extends StatelessWidget {
const UWPLoopbackUtil({super.key});

@override
Widget build(BuildContext context) {
return Selector<Config, bool>(
selector: (_, config) => config.onlyProxy,
builder: (_, onlyProxy, __) {
return ListItem(
leading: const Icon(Icons.lock_open),
title: Text(appLocalizations.loopback),
subtitle: Text(appLocalizations.loopbackDesc),
onTap: () {
windows?.runas(
'"${join(dirname(Platform.resolvedExecutable), "EnableLoopback.exe")}"',
"",
);
},
);
},
);
}
}

final appItems = [
if (Platform.isWindows) const UWPLoopbackUtil(),
const CloseConnectionsSwitch(),
const UsageSwitch(),
];
Loading

0 comments on commit ac39739

Please sign in to comment.