|
1 | 1 | #!/system/bin/sh |
2 | 2 | # shellcheck shell=ash |
3 | 3 |
|
4 | | -# Allow Android tethered clients to forward through the module TUN interface. |
5 | | -# This avoids flushing Android's whole FORWARD chain while preserving hotspot use. |
| 4 | +MODDIR=${MODDIR:-${0%/*}} |
| 5 | +. "${MODDIR}/lib/kamfw/.kamfwrc" || exit 1 |
| 6 | +. "${MODDIR}/lib/magicnet.sh" |
6 | 7 |
|
7 | | -magicnet_log() { |
8 | | - if command -v info >/dev/null 2>&1; then |
9 | | - info "$1" |
10 | | - elif command -v print >/dev/null 2>&1; then |
11 | | - print "MagicNet: $1" |
12 | | - fi |
13 | | -} |
14 | | - |
15 | | -magicnet_warn() { |
16 | | - if command -v warn >/dev/null 2>&1; then |
17 | | - warn "$1" |
18 | | - elif command -v print >/dev/null 2>&1; then |
19 | | - print "MagicNet: $1" |
20 | | - fi |
21 | | -} |
22 | | - |
23 | | -magicnet_cmd_exists() { |
24 | | - command -v "$1" >/dev/null 2>&1 |
25 | | -} |
26 | | - |
27 | | -magicnet_iface_exists() { |
28 | | - [ -n "$1" ] && [ -d "/sys/class/net/$1" ] |
29 | | -} |
30 | | - |
31 | | -magicnet_iface_has_hotspot_addr() { |
32 | | - magicnet_cmd_exists ip || return 1 |
33 | | - ip -o -4 addr show dev "$1" 2>/dev/null | grep -Eq 'inet (192\.168\.[0-9]+\.1|172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]+\.1|10\.[0-9]+\.[0-9]+\.1)/' |
34 | | -} |
35 | | - |
36 | | -magicnet_collect_tun_ifaces() { |
37 | | - _magicnet_tun_ifaces="" |
38 | | - |
39 | | - if [ -n "${MAGIC_TUN_IFACES:-}" ]; then |
40 | | - _magicnet_tun_ifaces="$MAGIC_TUN_IFACES" |
41 | | - fi |
42 | | - |
43 | | - if [ -f "${MODDIR}/.config/mihomo/config.yaml" ]; then |
44 | | - _mihomo_tun=$(awk -F: ' |
45 | | - /^[[:space:]]*device[[:space:]]*:/ { |
46 | | - gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2) |
47 | | - gsub(/["'\'']/, "", $2) |
48 | | - if ($2 != "") { |
49 | | - print $2 |
50 | | - exit |
51 | | - } |
52 | | - } |
53 | | - ' "${MODDIR}/.config/mihomo/config.yaml" 2>/dev/null) |
54 | | - [ -n "$_mihomo_tun" ] && _magicnet_tun_ifaces="$_magicnet_tun_ifaces $_mihomo_tun" |
55 | | - fi |
56 | | - |
57 | | - if [ -f "${MODDIR}/.config/sing-box/config.json" ]; then |
58 | | - _singbox_tun=$(sed -n 's/.*"interface_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "${MODDIR}/.config/sing-box/config.json" | head -n 1) |
59 | | - [ -n "$_singbox_tun" ] && _magicnet_tun_ifaces="$_magicnet_tun_ifaces $_singbox_tun" |
60 | | - fi |
61 | | - |
62 | | - _magicnet_tun_ifaces="$_magicnet_tun_ifaces Meta mihoyo utun tun0" |
63 | | - for _iface in $_magicnet_tun_ifaces; do |
64 | | - if magicnet_iface_exists "$_iface"; then |
65 | | - printf '%s\n' "$_iface" |
66 | | - fi |
67 | | - done | awk '!seen[$0]++' |
68 | | -} |
69 | | - |
70 | | -magicnet_collect_hotspot_ifaces() { |
71 | | - if [ -n "${MAGIC_HOTSPOT_IFACES:-}" ]; then |
72 | | - for _iface in $MAGIC_HOTSPOT_IFACES; do |
73 | | - printf '%s\n' "$_iface" |
74 | | - done |
75 | | - return 0 |
76 | | - fi |
77 | | - |
78 | | - for _path in /sys/class/net/*; do |
79 | | - [ -d "$_path" ] || continue |
80 | | - _iface=${_path##*/} |
81 | | - case "$_iface" in |
82 | | - ap[0-9]*|swlan[0-9]*|softap[0-9]*|rndis[0-9]*|usb[0-9]*|bt-pan) |
83 | | - printf '%s\n' "$_iface" |
84 | | - ;; |
85 | | - wlan[0-9]*|wifi[0-9]*) |
86 | | - magicnet_iface_has_hotspot_addr "$_iface" && printf '%s\n' "$_iface" |
87 | | - ;; |
88 | | - esac |
89 | | - done | awk '!seen[$0]++' |
90 | | -} |
91 | | - |
92 | | -magicnet_pick_forward_chain() { |
93 | | - if iptables -nL tetherctrl_FORWARD >/dev/null 2>&1; then |
94 | | - printf '%s\n' "tetherctrl_FORWARD" |
95 | | - else |
96 | | - printf '%s\n' "FORWARD" |
97 | | - fi |
98 | | -} |
99 | | - |
100 | | -magicnet_iptables_ensure() { |
101 | | - _table="" |
102 | | - if [ "$1" = "-t" ]; then |
103 | | - _table="$2" |
104 | | - shift 2 |
105 | | - fi |
106 | | - |
107 | | - if [ -n "$_table" ]; then |
108 | | - iptables -t "$_table" -C "$@" >/dev/null 2>&1 || iptables -t "$_table" -A "$@" >/dev/null 2>&1 |
109 | | - else |
110 | | - iptables -C "$@" >/dev/null 2>&1 || iptables -I "$@" >/dev/null 2>&1 |
111 | | - fi |
112 | | -} |
113 | | - |
114 | | -magicnet_enable_hotspot_forward() { |
115 | | - [ "${MAGIC_HOTSPOT_FORWARD:-1}" != "0" ] || return 0 |
116 | | - |
117 | | - if ! magicnet_cmd_exists iptables; then |
118 | | - magicnet_warn "iptables not found; hotspot forwarding fix skipped" |
119 | | - return 0 |
120 | | - fi |
121 | | - |
122 | | - _forward_chain=$(magicnet_pick_forward_chain) |
123 | | - _tun_ifaces=$(magicnet_collect_tun_ifaces) |
124 | | - _hotspot_ifaces=$(magicnet_collect_hotspot_ifaces) |
125 | | - |
126 | | - if [ -z "$_tun_ifaces" ]; then |
127 | | - magicnet_warn "No active TUN interface found; hotspot forwarding fix skipped" |
128 | | - return 0 |
129 | | - fi |
130 | | - if [ -z "$_hotspot_ifaces" ]; then |
131 | | - magicnet_warn "No hotspot interface candidate found; hotspot forwarding fix skipped" |
132 | | - return 0 |
133 | | - fi |
134 | | - |
135 | | - for _tun in $_tun_ifaces; do |
136 | | - for _hotspot in $_hotspot_ifaces; do |
137 | | - [ "$_tun" != "$_hotspot" ] || continue |
138 | | - magicnet_iptables_ensure "$_forward_chain" -i "$_hotspot" -o "$_tun" -j ACCEPT || true |
139 | | - magicnet_iptables_ensure "$_forward_chain" -i "$_tun" -o "$_hotspot" -m state --state RELATED,ESTABLISHED -j ACCEPT || \ |
140 | | - magicnet_iptables_ensure "$_forward_chain" -i "$_tun" -o "$_hotspot" -j ACCEPT || true |
141 | | - done |
142 | | - magicnet_iptables_ensure -t nat POSTROUTING -o "$_tun" -j MASQUERADE || true |
143 | | - done |
144 | | - |
145 | | - magicnet_log "Hotspot forwarding rules applied via $_forward_chain" |
146 | | -} |
| 8 | +magicnet_enable_hotspot_forward "$@" |
0 commit comments