@sunjiehao/zyhy-network 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,23 +1,22 @@
1
1
  # ZYHY-Network
2
2
 
3
- 当前版本:`V0.2.1`
3
+ 当前版本:`V0.2.2`
4
4
 
5
- 办公时不用再在「网线」和「公司 Wi-Fi」之间来回切。
5
+ Mac 同时使用公司网线和 `ZYHY-Private` Wi-Fi:普通网络走网线,OA、邮箱等内网地址自动走 Wi-Fi。
6
6
 
7
- - 看网页、刷视频、走代理:用网线
8
- - 打开 OA、邮箱、iHR:自动走公司 Wi-Fi `ZYHY-Private`
7
+ ## 功能
9
8
 
10
- 两个可以同时开着。
9
+ - 仅在网线已连接且 Wi-Fi 为 `ZYHY-Private` 时启用分流
10
+ - 监听 macOS 网络变化并自动更新,不再每 5 秒轮询
11
+ - 默认每 300 秒低频校验一次,可修改间隔或关闭
12
+ - 支持 `zyhy-network refresh` 立即刷新 DNS、路由和分流配置
13
+ - GitHub 与 npm 安装提供相同的 `zyhy-network` 命令
11
14
 
12
- ## 你需要
15
+ ## 安装
13
16
 
14
- 1. 一台 Mac
15
- 2. 插上公司网线
16
- 3. Wi-Fi 连上 **ZYHY-Private**(不要连别的公司 Wi-Fi)
17
+ GitHub:
17
18
 
18
- ## 怎么装
19
-
20
- 打开「终端」,把下面三行粘贴进去,回车,输入开机密码:
19
+ 请按顺序逐行执行下面三条命令。安装完成后不需要再手动运行其他脚本。
21
20
 
22
21
  ```bash
23
22
  git clone https://github.com/lampardrodgers/ZYHY-Network.git
@@ -25,50 +24,33 @@ cd ZYHY-Network
25
24
  sudo ./install.sh
26
25
  ```
27
26
 
28
- 装完后:
29
-
30
- 1. 用 **新标签页** 打开 OA、邮箱(不要刷新刚才失败的那一页)
31
- 2. 如果用的是 Chrome:设置 → 隐私和安全 → 安全 → 关掉「使用安全 DNS」
32
-
33
- 能打开就行,不用再配域名。
34
-
35
- ## 用的时候注意
36
-
37
- - 网线要插着,Wi-Fi 必须是 `ZYHY-Private`,分流才会开
38
- - 拔掉网线、或换成别的 Wi-Fi:全部按系统原来的方式上网,内网入口可能会打不开
39
- - 插回网线并连回 `ZYHY-Private`:等几秒会自动恢复
40
-
41
- 后台会监听 macOS 的网络状态变化,只在有线网、公司 Wi-Fi、网关或 DNS
42
- 等实际发生变化时更新分流配置。另有每 5 分钟一次的低频校验,用来补偿
43
- 偶尔漏掉的系统事件和内网域名解析变化;配置内容没变时不会重写网络设置。
44
-
45
- GitHub 安装和 npm 安装提供完全相同的命令。需要立即重新解析内网 DNS
46
- 并校验全部分流配置时:
47
-
48
- ```bash
49
- zyhy-network refresh
50
- ```
51
-
52
- ## npm 命令行版本
27
+ `sudo ./install.sh` 会安装并启动后台服务。之后,网线、Wi-Fi、IP、网关或
28
+ DNS 发生变化时,程序会自动重新判断是否启用内网分流;网络状态没有变化时
29
+ 不会重复修改系统网络配置。
53
30
 
54
- 也可以从 npm 安装:
31
+ npm
55
32
 
56
33
  ```bash
57
34
  npm install -g @sunjiehao/zyhy-network
58
35
  zyhy-network install
59
36
  ```
60
37
 
61
- 无论使用哪种安装方式,需要修改系统配置时都会自动请求管理员权限。常用命令:
38
+ 需要修改系统配置时,命令会自动请求管理员权限。
39
+
40
+ ## 常用命令
62
41
 
63
42
  ```bash
64
- zyhy-network status
65
- zyhy-network refresh
66
- zyhy-network add '*.hq.cmcc'
67
- zyhy-network uninstall
43
+ zyhy-network status # 查看状态
44
+ zyhy-network refresh # 强制刷新
45
+ zyhy-network interval # 查看定时校验设置
46
+ zyhy-network interval 600 # 改为每 600 秒校验
47
+ zyhy-network interval 0 # 关闭定时校验
48
+ zyhy-network add '*.hq.cmcc' # 添加域名后缀
49
+ zyhy-network add 172.21.0.0/16 # 添加网段
50
+ zyhy-network uninstall # 卸载,保留配置
68
51
  ```
69
52
 
70
- ## 不想用了
53
+ ## 注意
71
54
 
72
- ```bash
73
- zyhy-network uninstall
74
- ```
55
+ - 网线和 `ZYHY-Private` Wi-Fi 需要同时连接;网络变化后会自动恢复分流
56
+ - Chrome 若无法打开内网,请关闭“设置 → 隐私和安全 → 安全 → 使用安全 DNS”
package/bin/zyhy-network CHANGED
@@ -16,6 +16,11 @@ case "$command" in
16
16
  exec /usr/bin/sudo "$TARGET" "$@"
17
17
  fi
18
18
  ;;
19
+ interval)
20
+ if [[ $# -gt 1 && $(id -u) -ne 0 ]]; then
21
+ exec /usr/bin/sudo "$TARGET" "$@"
22
+ fi
23
+ ;;
19
24
  esac
20
25
 
21
26
  exec "$TARGET" "$@"
@@ -29,11 +29,12 @@ const rootCommands = new Set([
29
29
  'remove',
30
30
  'uninstall'
31
31
  ]);
32
+ const intervalNeedsRoot = command === 'interval' && args.length > 1;
32
33
 
33
34
  let executable = '/bin/bash';
34
35
  let commandArgs = [script, ...args];
35
36
 
36
- if (rootCommands.has(command) && process.getuid?.() !== 0) {
37
+ if ((rootCommands.has(command) || intervalNeedsRoot) && process.getuid?.() !== 0) {
37
38
  executable = '/usr/bin/sudo';
38
39
  commandArgs = ['/bin/bash', script, ...args];
39
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sunjiehao/zyhy-network",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "macOS Ethernet and ZYHY-Private Wi-Fi split routing CLI",
5
5
  "bin": {
6
6
  "zyhy-network": "bin/zyhy-network.js"
@@ -17,7 +17,7 @@
17
17
  set -euo pipefail
18
18
 
19
19
  LABEL="com.zyhy.network.split"
20
- VERSION="0.2.1"
20
+ VERSION="0.2.2"
21
21
  PLIST="/Library/LaunchDaemons/${LABEL}.plist"
22
22
  RECONCILE_LABEL="${LABEL}.reconcile"
23
23
  RECONCILE_PLIST="/Library/LaunchDaemons/${RECONCILE_LABEL}.plist"
@@ -87,6 +87,7 @@ save_config() {
87
87
  # add hostname: sudo zyhy-private-split add foo.hq.cmcc
88
88
  # add suffix: sudo zyhy-private-split add '*.hq.cmcc'
89
89
  # add CIDR: sudo zyhy-private-split add 172.21.0.0/16
90
+ # timed check: sudo zyhy-private-split interval 300 (0 disables)
90
91
 
91
92
  WIFI_DOMAIN="${WIFI_DOMAIN}"
92
93
  DOMAINS="${DOMAINS}"
@@ -617,6 +618,41 @@ cmd_refresh() {
617
618
  echo "refreshed: routes, DNS, hosts, resolvers and proxy exceptions checked"
618
619
  }
619
620
 
621
+ cmd_interval() {
622
+ load_config
623
+ local seconds=${1-}
624
+ if [[ -z "$seconds" ]]; then
625
+ if (( RECONCILE_INTERVAL == 0 )); then
626
+ echo "timed check: disabled (0 seconds)"
627
+ else
628
+ echo "timed check: every ${RECONCILE_INTERVAL} seconds"
629
+ fi
630
+ return
631
+ fi
632
+
633
+ require_root
634
+ [[ "$seconds" =~ ^[0-9]+$ ]] || {
635
+ echo "usage: $0 interval <seconds>; 0 disables timed checks" >&2
636
+ exit 2
637
+ }
638
+ RECONCILE_INTERVAL=$seconds
639
+ save_config
640
+
641
+ launchctl bootout "system/${RECONCILE_LABEL}" >/dev/null 2>&1 || true
642
+ if [[ -x "$INSTALL_BIN" && -f "$PLIST" ]]; then
643
+ write_install_plists "$INSTALL_BIN"
644
+ if (( RECONCILE_INTERVAL > 0 )); then
645
+ bootstrap_with_retry "$RECONCILE_PLIST"
646
+ fi
647
+ fi
648
+
649
+ if (( RECONCILE_INTERVAL == 0 )); then
650
+ echo "timed check disabled; network-change watching remains enabled"
651
+ else
652
+ echo "timed check interval: ${RECONCILE_INTERVAL} seconds"
653
+ fi
654
+ }
655
+
620
656
  cmd_status() {
621
657
  load_config
622
658
  local wifi eth pair
@@ -657,7 +693,11 @@ cmd_status() {
657
693
  $0==e{on=0}
658
694
  ' /etc/hosts || true
659
695
  echo "watcher : $(launchctl print "system/${LABEL}" >/dev/null 2>&1 && echo loaded || echo not-loaded)"
660
- echo "reconcile : $(launchctl print "system/${RECONCILE_LABEL}" >/dev/null 2>&1 && echo "loaded (${RECONCILE_INTERVAL}s)" || echo not-loaded)"
696
+ if (( RECONCILE_INTERVAL == 0 )); then
697
+ echo "reconcile : disabled (0s)"
698
+ else
699
+ echo "reconcile : $(launchctl print "system/${RECONCILE_LABEL}" >/dev/null 2>&1 && echo "loaded (${RECONCILE_INTERVAL}s)" || echo "not-loaded (${RECONCILE_INTERVAL}s)")"
700
+ fi
661
701
  echo "sys proxy : $(scutil --proxy | awk '/HTTPEnable/{e=$3} /HTTPProxy/{p=$3} /HTTPPort/{n=$3} END{if(e==1) print p":"n; else print "off"}')"
662
702
  echo "proxy bypass:"
663
703
  read_proxy_exceptions | sed 's/^/ /' || true
@@ -817,6 +857,12 @@ write_install_plists() {
817
857
  </plist>
818
858
  EOF
819
859
 
860
+ chmod 644 "$PLIST"
861
+ if (( RECONCILE_INTERVAL == 0 )); then
862
+ rm -f "$RECONCILE_PLIST"
863
+ return
864
+ fi
865
+
820
866
  cat >"$RECONCILE_PLIST" <<EOF
821
867
  <?xml version="1.0" encoding="UTF-8"?>
822
868
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@@ -839,7 +885,7 @@ EOF
839
885
  </dict>
840
886
  </plist>
841
887
  EOF
842
- chmod 644 "$PLIST" "$RECONCILE_PLIST"
888
+ chmod 644 "$RECONCILE_PLIST"
843
889
  }
844
890
 
845
891
  bootstrap_with_retry() {
@@ -880,7 +926,7 @@ cmd_install() {
880
926
  write_install_plists "$INSTALL_BIN"
881
927
  cmd_apply --force
882
928
  bootstrap_with_retry "$PLIST"
883
- if ! bootstrap_with_retry "$RECONCILE_PLIST"; then
929
+ if (( RECONCILE_INTERVAL > 0 )) && ! bootstrap_with_retry "$RECONCILE_PLIST"; then
884
930
  launchctl bootout "system/${LABEL}" >/dev/null 2>&1 || true
885
931
  return 1
886
932
  fi
@@ -957,6 +1003,7 @@ usage: $0 <command>
957
1003
  add <item> add host, '*.suffix', or CIDR (needs sudo)
958
1004
  remove <item> remove one of the above (needs sudo)
959
1005
  refresh force DNS resolution and reconcile now (needs sudo)
1006
+ interval [seconds] show/set timed-check interval; 0 disables (set needs sudo)
960
1007
  apply [--force] apply now; --force also reconciles DNS (needs sudo)
961
1008
  clear tear down routes (needs sudo)
962
1009
  install copy to /usr/local/sbin and load daemon (needs sudo)
@@ -972,6 +1019,7 @@ load_config
972
1019
  case "$cmd" in
973
1020
  apply) cmd_apply "${1-}" ;;
974
1021
  refresh) cmd_refresh ;;
1022
+ interval) cmd_interval "${1-}" ;;
975
1023
  watch) cmd_watch ;;
976
1024
  clear) cmd_clear ;;
977
1025
  status) cmd_status ;;
@@ -5,6 +5,8 @@ DOMAINS="oa.hq.cmcc mail.cmhi.chinamobile.com"
5
5
  SUFFIXES="hq.cmcc cmhi.chinamobile.com"
6
6
  CIDRS="172.21.0.0/16 172.16.0.0/16"
7
7
 
8
- # 网络变化后等待接口稳定的秒数;另每 300 秒做一次只读为主的兜底校验。
8
+ # 网络变化后等待接口稳定的秒数。
9
9
  EVENT_SETTLE_SECONDS="2"
10
+
11
+ # 定时兜底校验间隔(秒);设为 0 可关闭,网络变化事件监听不受影响。
10
12
  RECONCILE_INTERVAL="300"