Skip to content

Commit

Permalink
socks5状态菜单
Browse files Browse the repository at this point in the history
  • Loading branch information
shiguanghuxian committed Dec 2, 2021
1 parent 0925dd9 commit 794aa44
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/assets/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@
"auto_start_label": "Auto running the socks5 proxy",
"auto_start": "Auto socks5",
"hosts_title": "Accelerated domain name",
"hosts_tooltip": "Accelerating the domain name may not be 100% accelerated, which is related to the network environment"
"hosts_tooltip": "Accelerating the domain name may not be 100% accelerated, which is related to the network environment",
"socks5_tooltip_macos": "System Preferences -> Network -> Advanced -> proxy -> SOCKS Proxy is set to the above local Socks5 address",
"socks5_tooltip_windows": "System Preferences -> Network -> Advanced -> proxy -> SOCKS Proxy is set to the above local Socks5 address",
"open_preference": "Open preferences",
"tray_socks5_proxy": "Socks5 proxy"
},
"settings": {
"title": "Settings",
Expand Down
6 changes: 5 additions & 1 deletion lib/assets/locale/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@
"auto_start_label": "软件启动运行socks5代理",
"auto_start": "自动启动socks5",
"hosts_title": "加速域名",
"hosts_tooltip": "加速域名,不一定100%可以加速,和网络环境有关"
"hosts_tooltip": "加速域名,不一定100%可以加速,和网络环境有关",
"socks5_tooltip_macos": "系统偏好设置->网络->高级->代理->SOCKS代理 设置为上述本地socks5地址",
"socks5_tooltip_windows": "系统偏好设置->网络->高级->代理->SOCKS代理 设置为上述本地socks5地址",
"open_preference": "打开偏好设置",
"tray_socks5_proxy": "Socks5代理"
},
"settings": {
"title": "设置",
Expand Down
10 changes: 10 additions & 0 deletions lib/event_manage/event_manage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ class ChangeContextMenuDNSToHome {
class ChangeContextMenuHomeToDNS {
const ChangeContextMenuHomeToDNS();
}

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

// 状态菜单变化事件 home通知Socks5
class ChangeContextMenuHomeToSocks5 {
const ChangeContextMenuHomeToSocks5();
}
37 changes: 37 additions & 0 deletions lib/views/home/widgets/home_hosts_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,26 @@ class _HomeHostsListState extends State<HomeHostsList> {
_setContextMenu();
});
_subscription.resume();
//监听socks5启动变化
_subscriptionSocks5 = eventBus
.on<ChangeContextMenuSocks5ToHome>()
.listen((ChangeContextMenuSocks5ToHome data) {
_setContextMenu();
});
_subscriptionSocks5.resume();
}

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

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

Expand Down Expand Up @@ -86,6 +95,15 @@ class _HomeHostsListState extends State<HomeHostsList> {
} else {
dnsLabel = lang.get('dns.start') + lang.get('dns.tray_dns_proxy');
}
// 获取socks5代理启动情况
bool socks5Run = socks5GetIsStart() == 1;
String socks5RunLabel = '';
if (socks5Run) {
socks5RunLabel = lang.get('dns.stop') + lang.get('socks5.tray_socks5_proxy');
} else {
socks5RunLabel = lang.get('dns.start') + lang.get('socks5.tray_socks5_proxy');
}

// 菜单内容
final List<system_tray.MenuItemBase> menuBase = [
system_tray.MenuSeparator(),
Expand All @@ -108,6 +126,25 @@ class _HomeHostsListState extends State<HomeHostsList> {
},
),
system_tray.MenuSeparator(),
system_tray.MenuItem(
label: socks5RunLabel,
onClicked: () async {
log(socks5RunLabel);
if (socks5Run) {
socks5Stop();
} else {
await startSocks5Proxy();
}

// 等半秒钟
Future.delayed(const Duration(milliseconds: 500), () async {
_setContextMenu();
// 通知菜单发生变化
eventBus.fire(const ChangeContextMenuHomeToSocks5());
});
},
),
system_tray.MenuSeparator(),
system_tray.MenuItem(
label: lang.get('home.show_edit_hosts'),
onClicked: () {
Expand Down
5 changes: 5 additions & 0 deletions lib/views/socks5/socks5_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:hosts_manage/views/socks5/widgets/socks5_auto_start.dart';
import 'package:hosts_manage/views/socks5/widgets/socks5_cert.dart';
import 'package:hosts_manage/views/socks5/widgets/socks5_hosts.dart';
import 'package:hosts_manage/views/socks5/widgets/socks5_local_ip.dart';
import 'package:hosts_manage/views/socks5/widgets/socks5_tooltip.dart';
import 'package:macos_ui/macos_ui.dart';

// 本地Socks5代理
Expand Down Expand Up @@ -147,6 +148,10 @@ class _Socks5PageState extends State<Socks5Page> {
const Divider(
height: 30,
),
const SizedBox(
width: 400,
child: Socks5Tooltip(),
),
],
),
),
Expand Down
10 changes: 10 additions & 0 deletions lib/views/socks5/widgets/socks5_action_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 Down Expand Up @@ -36,6 +37,13 @@ class _Socks5ActionButtonState extends State<Socks5ActionButton> {
_initIsRun();
_socks5Bloc = context.read<Socks5Bloc>();
_getLocalIP();
//监听dns启动变化
_subscription = eventBus
.on<ChangeContextMenuHomeToSocks5>()
.listen((ChangeContextMenuHomeToSocks5 data) {
_initIsRun();
});
_subscription.resume();
}

Socks5Bloc _socks5Bloc;
Expand Down Expand Up @@ -91,6 +99,8 @@ class _Socks5ActionButtonState extends State<Socks5ActionButton> {
_getLocalIP();
// 等半秒钟,看一下是否启动错误
Future.delayed(const Duration(milliseconds: 500), () async {
// 通知菜单发生变化
eventBus.fire(const ChangeContextMenuSocks5ToHome());
// 获取go socks5启动错误信息
Pointer<Int8> errPrt = socks5GetErr();
String errStr = errPrt.cast<Utf8>().toDartString();
Expand Down
76 changes: 76 additions & 0 deletions lib/views/socks5/widgets/socks5_tooltip.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'dart:io';

import 'package:cli_script/cli_script.dart';
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/i18n/i18n.dart';
import 'package:hosts_manage/store/store.dart';
import 'package:hosts_manage/views/socks5/bloc/socks5_bloc.dart';
import 'package:macos_ui/macos_ui.dart';

// 提示文本
class Socks5Tooltip extends StatefulWidget {
const Socks5Tooltip({
Key key,
}) : super(key: key);

@override
_Socks5TooltipState createState() => _Socks5TooltipState();
}

class _Socks5TooltipState extends State<Socks5Tooltip> {
@override
void initState() {
super.initState();
}

I18N lang;
bool isAuto = false;

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
return StoreBuilder<ZState>(
builder: (context, store) {
lang = StoreProvider.of<ZState>(context).state.lang;
return BlocBuilder<Socks5Bloc, Socks5State>(
buildWhen: (previous, current) {
return false;
},
builder: (context, state) {
return RichText(
text: TextSpan(children: [
TextSpan(
text: Platform.isMacOS ? lang.get('socks5.socks5_tooltip_macos') : lang.get('socks5.socks5_tooltip_windows'),
style: MacosTheme.of(context).typography.headline,
),
WidgetSpan(
child: InkWell(
onTap: () {
if (Platform.isMacOS) {
run('open /System/Library/PreferencePanes/Network.prefPane');
}
},
child: Text(
Platform.isMacOS ? lang.get('socks5.open_preference') : '',
style: MacosTheme.of(context)
.typography
.headline
.copyWith(color: MacosTheme.of(context).primaryColor),
),
),
),
]),
);
},
);
},
);
}
}

0 comments on commit 794aa44

Please sign in to comment.