Skip to content

Commit

Permalink
状态菜单管理dns代理
Browse files Browse the repository at this point in the history
  • Loading branch information
shiguanghuxian committed Nov 21, 2021
1 parent c1f2a32 commit 60cf955
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/assets/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"local_ip_copy_tooltip": "Click Copy DNS proxy address",
"tooltip": "After configuring the hosts of each group on the hosts management page, click Start agent in the lower right corner to add the above local DNS proxy address to the first one in the system DNS configuration. After that, the DNS resolution of the domain name visited by the website will be the IP configured by the hosts. If you are on the same network, mobile phones and other devices can also be configured as this DNS address. If the above third DNS server is not configured, please configure multiple DNS server addresses on the system DNS page and ensure that the above second local DNS address is the first.",
"open_preference": "Open preferences",
"server_tooltip": "Click restart DNS to save this configuration"
"server_tooltip": "Click restart DNS to save this configuration",
"tray_dns_proxy": "DNS Proxy"
},
"settings": {
"title": "Settings",
Expand Down
3 changes: 2 additions & 1 deletion lib/assets/locale/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"local_ip_copy_tooltip": "点击复制DNS代理地址",
"tooltip": "在Hosts管理页面配置好各个分组的hosts后,点击右下角启动代理后,可将上述本机DNS代理地址添加到系统DNS配置第一个。之后网站访问域名dns解析将会是hosts配置的ip。如果在同一个网络手机等设备也可以配置为此DNS地址。如果上述第三项DNS服务器未配置,请在系统DNS页面配置多个DNS服务器地址,并确保上述第二项本地DNS地址为第一个。",
"open_preference": "打开偏好设置",
"server_tooltip": "点击重启dns后会保存此配置"
"server_tooltip": "点击重启dns后会保存此配置",
"tray_dns_proxy": "DNS代理"
},
"settings": {
"title": "设置",
Expand Down
13 changes: 13 additions & 0 deletions lib/event_manage/event_manage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:event_bus/event_bus.dart';

EventBus eventBus = EventBus();

// 状态菜单变化事件 dns通知home
class ChangeContextMenuDNSToHome {
const ChangeContextMenuDNSToHome();
}

// 状态菜单变化事件 home通知dns
class ChangeContextMenuHomeToDNS {
const ChangeContextMenuHomeToDNS();
}
4 changes: 4 additions & 0 deletions lib/golib/dns_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (dp *DnsProxy) SetPublicDnsServer(addrs []string) {

// Start 启动dns代理服务
func (dp *DnsProxy) Start() {
if dp.isStart {
log.Println("代理运行中,请先停止")
return
}
udpService := &dns.Server{
Addr: "0.0.0.0:53",
Net: "udp",
Expand Down
24 changes: 23 additions & 1 deletion lib/views/dns/widgets/dns_action_button.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:developer';
import 'dart:ffi';

Expand All @@ -7,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:hosts_manage/event_manage/event_manage.dart';
import 'package:hosts_manage/golib/godart.dart';
import 'package:hosts_manage/golib/golib.dart';
import 'package:hosts_manage/i18n/i18n.dart';
Expand All @@ -33,14 +35,23 @@ class _DnsActionButtonState extends State<DnsActionButton> {
_initIsRun();
_dnsBloc = context.read<DNSBloc>();
_getLocalIP();
//监听dns启动变化
_subscription = eventBus
.on<ChangeContextMenuHomeToDNS>()
.listen((ChangeContextMenuHomeToDNS data) {
_initIsRun();
});
_subscription.resume();
}

DNSBloc _dnsBloc;
I18N lang;
bool isRun = false;
StreamSubscription _subscription;

@override
void dispose() {
_subscription?.cancel();
super.dispose();
}

Expand All @@ -56,13 +67,22 @@ class _DnsActionButtonState extends State<DnsActionButton> {
/// 停止dns代理
void _stopDNS() {
stopDNS();
// 通知菜单发生变化
eventBus.fire(const ChangeContextMenuDNSToHome());
setState(() {
isRun = false;
});
}

/// 启动dns代理
void _startDns() async {
int isStart = getIsStart();
if (isStart == 1) {
setState(() {
isRun = isStart == 1;
});
return;
}
// 设置hosts域名ip映射
String hostsBody = await getAllHostsVal();
setAddressBookDNS(GoString.fromString(hostsBody));
Expand All @@ -77,8 +97,10 @@ class _DnsActionButtonState extends State<DnsActionButton> {
_getLocalIP();
// 等半秒钟,看一下是否启动错误
Future.delayed(const Duration(milliseconds: 500), () async {
// 通知菜单发生变化
eventBus.fire(const ChangeContextMenuDNSToHome());
// 获取go dns启动错误信息
Pointer<Int8> errPrt = getErr();

String errStr = errPrt.cast<Utf8>().toDartString();
log('启动错误信息: ${errStr}');
if (errStr != null && errStr != '') {
Expand Down
40 changes: 40 additions & 0 deletions lib/views/home/widgets/home_hosts_list.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import 'dart:async';
import 'dart:developer';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:hosts_manage/event_manage/event_manage.dart';
import 'package:hosts_manage/golib/golib.dart';
import 'package:hosts_manage/i18n/i18n.dart';
import 'package:hosts_manage/models/const.dart';
import 'package:hosts_manage/models/hosts_info_model.dart';
import 'package:hosts_manage/store/store.dart';
import 'package:hosts_manage/views/common/common.dart';
import 'package:hosts_manage/views/home/bloc/home_bloc.dart';
import 'package:hosts_manage/views/home/widgets/hosts_add_widget.dart';
import 'package:hosts_manage/views/home/widgets/hosts_list_widget.dart';
Expand All @@ -30,15 +34,24 @@ class _HomeHostsListState extends State<HomeHostsList> {
void initState() {
super.initState();
_homeBloc = context.read<HomeBloc>();
//监听dns启动变化
_subscription = eventBus
.on<ChangeContextMenuDNSToHome>()
.listen((ChangeContextMenuDNSToHome data) {
_setContextMenu();
});
_subscription.resume();
}

HomeBloc _homeBloc;
I18N lang;
final system_tray.AppWindow _appWindow = system_tray.AppWindow();
bool isInitSystemTray = false; // 是否初始化了状态菜单
StreamSubscription _subscription;

@override
void dispose() {
_subscription?.cancel();
super.dispose();
}

Expand All @@ -65,8 +78,35 @@ class _HomeHostsListState extends State<HomeHostsList> {

/// 更新菜单选项
Future<void> _setContextMenu() async {
// 获取dns代理启动情况
bool dnsRun = getIsStart() == 1;
String dnsLabel = '';
if (dnsRun) {
dnsLabel = lang.get('dns.stop') + lang.get('dns.tray_dns_proxy');
} else {
dnsLabel = lang.get('dns.start') + lang.get('dns.tray_dns_proxy');
}
// 菜单内容
final List<system_tray.MenuItemBase> menuBase = [
system_tray.MenuSeparator(),
system_tray.MenuItem(
label: dnsLabel,
onClicked: () async {
log(dnsLabel);
if (dnsRun) {
stopDNS();
} else {
await startDnsProxy();
}

// 等半秒钟
Future.delayed(const Duration(milliseconds: 500), () async {
_setContextMenu();
// 通知菜单发生变化
eventBus.fire(const ChangeContextMenuHomeToDNS());
});
},
),
system_tray.MenuSeparator(),
system_tray.MenuItem(
label: lang.get('home.show_edit_hosts'),
Expand Down
Binary file modified macos/dylib/libdns.dylib
Binary file not shown.
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.3"
event_bus:
dependency: "direct main"
description:
name: event_bus
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.0"
fake_async:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dependencies:
cli_script: ^0.2.3+1
call: ^0.3.0
fluent_ui: ^3.4.0
event_bus: ^2.0.0


dev_dependencies:
Expand Down

0 comments on commit 60cf955

Please sign in to comment.