Skip to content

Commit 751efd3

Browse files
committed
refactor: route runtime through kamfw
1 parent 12bc63f commit 751efd3

9 files changed

Lines changed: 329 additions & 328 deletions

File tree

.github/workflows/exec.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,35 @@ jobs:
6767
enable-cache: 'true'
6868
private-key: ${{ secrets.KAM_PRIVATE_KEY }}
6969

70+
- name: Install artifact tools
71+
run: sudo apt-get update && sudo apt-get install -y unzip
72+
7073
- name: Verify tools
7174
shell: bash
7275
run: |
76+
set -euo pipefail
7377
kam --version
7478
gh --version
7579
7680
- name: Build module
7781
shell: bash
7882
run: |
83+
set -euo pipefail
7984
chmod +x hooks/**/*.sh src/MagicNet/*.sh src/MagicNet/system/bin/* || true
8085
rm -rf dist
8186
kam build
8287
88+
- name: Verify artifact contents
89+
shell: bash
90+
run: |
91+
set -euo pipefail
92+
test -f dist/MagicNet.zip
93+
unzip -l dist/MagicNet.zip | grep -F 'lib/kamfw/README.md'
94+
unzip -l dist/MagicNet.zip | grep -F 'lib/magicnet.sh'
95+
unzip -l dist/MagicNet.zip | grep -F 'boot-completed.sh'
96+
unzip -l dist/MagicNet.zip | grep -F 'action.sh'
97+
! unzip -l dist/MagicNet.zip | grep -F 'lib/kamfw/src/'
98+
8399
- name: Upload build artifact
84100
if: always() && hashFiles('dist/*.zip') != ''
85101
uses: actions/upload-artifact@v4

.github/workflows/init.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@ jobs:
3535
- name: Validate repository
3636
shell: bash
3737
run: |
38+
set -euo pipefail
3839
kam validate
39-
shellcheck -e SC1091,SC2329,SC2059 \
40+
shellcheck -s bash -e SC1091,SC2329,SC2059 \
4041
hooks/pre-build/*.sh \
4142
hooks/post-build/*.sh \
4243
hooks/lib/*.sh \
44+
kam.sh
45+
shellcheck -s sh -e SC1091,SC1090,SC2329,SC2059 \
4346
src/MagicNet/*.sh \
4447
src/MagicNet/system/bin/* \
45-
kam.sh
48+
src/MagicNet/lib/magicnet.sh
4649
python3 -c 'import yaml, pathlib; yaml.safe_load(pathlib.Path("src/MagicNet/.config/mihomo/config.yaml").read_text())'
4750
jq empty src/MagicNet/.config/sing-box/config.json
51+
KAM_HOOKS_ROOT=hooks KAM_MODULE_ROOT=src/MagicNet bash hooks/pre-build/6000.check_config.sh
52+
53+
- name: Verify kamfw runtime wiring
54+
shell: bash
55+
run: |
56+
set -euo pipefail
57+
MODPATH="$PWD/src/MagicNet" sh -c '. "$MODPATH/lib/kamfw/.kamfwrc"; import __runtime__; . "$MODPATH/lib/magicnet.sh"; kamfw run post-mount -- smoke'

src/MagicNet/.config/kamfw/.envrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# shellcheck shell=ash
22

33
export HOME=${MODDIR}
4+
export KAM_HOME=${KAM_HOME:-${MODDIR}}
45
export KAMFW_DIR=${MODDIR}/lib/kamfw
56
export KAM_MODULES=${KAM_MODULES:-""}
67
export KAM_LOGFILE=${MODDIR}/kam.log
78
export MAGIC_MIHOMO="${MAGIC_MIHOMO:-1}"
89
export MAGIC_SINGBOX="${MAGIC_SINGBOX:-1}"
910
export MAGIC_HOTSPOT_FORWARD="${MAGIC_HOTSPOT_FORWARD:-1}"
11+
export MAGIC_VPN_COEXIST="${MAGIC_VPN_COEXIST:-0}"

src/MagicNet/action.sh

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,6 @@ else
88
fi
99

1010
import __runtime__
11+
. "${MODDIR}/lib/magicnet.sh"
1112

12-
kamfw_init_home
13-
kamfw_init_paths
14-
15-
if has_command "sing-box"; then
16-
import __singbox__
17-
singbox_ask_webui
18-
ask_toggle_singbox
19-
[ -f "${MODDIR}/hotspot-forward.sh" ] && . "${MODDIR}/hotspot-forward.sh" && magicnet_enable_hotspot_forward
20-
[ -f "${MODDIR}/vpn-coexist.sh" ] && . "${MODDIR}/vpn-coexist.sh" && magicnet_enable_vpn_coexist
21-
elif has_command "mihomo"; then
22-
import __mihomo__
23-
ask_webui
24-
ask_toggle_mihomo
25-
[ -f "${MODDIR}/hotspot-forward.sh" ] && . "${MODDIR}/hotspot-forward.sh" && magicnet_enable_hotspot_forward
26-
[ -f "${MODDIR}/vpn-coexist.sh" ] && . "${MODDIR}/vpn-coexist.sh" && magicnet_enable_vpn_coexist
27-
else
28-
abort "No supported kernel found!"
29-
fi
13+
kamfw run action -- "$@"

src/MagicNet/boot-completed.sh

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,6 @@ else
88
fi
99

1010
import __runtime__
11-
import wait
11+
. "${MODDIR}/lib/magicnet.sh"
1212

13-
# Initialize runtime environment (sets PATH, LD_LIBRARY_PATH, KAM_HOME)
14-
kamfw_init_home
15-
kamfw_init_paths
16-
17-
# Wait for device to be ready
18-
wait_boot_if_magisk
19-
wait_unlock 3
20-
21-
# Allow toggling kernel start via env vars (default: enabled)
22-
MAGIC_MIHOMO=${MAGIC_MIHOMO:-1}
23-
MAGIC_SINGBOX=${MAGIC_SINGBOX:-1}
24-
25-
# Conditionally start the available kernel (respect env flags and fallback)
26-
if [ "${MAGIC_SINGBOX}" -ne 0 ] && has_command "sing-box"; then
27-
import __singbox__
28-
if singbox_start; then
29-
[ -f "${MODDIR}/hotspot-forward.sh" ] && . "${MODDIR}/hotspot-forward.sh" && magicnet_enable_hotspot_forward
30-
[ -f "${MODDIR}/vpn-coexist.sh" ] && . "${MODDIR}/vpn-coexist.sh" && magicnet_enable_vpn_coexist
31-
else
32-
print "MagicNet: sing-box failed to start; attempting mihomo fallback..."
33-
if [ "${MAGIC_MIHOMO}" -ne 0 ] && has_command "mihomo"; then
34-
import __mihomo__
35-
mihomo_start && [ -f "${MODDIR}/hotspot-forward.sh" ] && . "${MODDIR}/hotspot-forward.sh" && magicnet_enable_hotspot_forward
36-
[ -f "${MODDIR}/vpn-coexist.sh" ] && . "${MODDIR}/vpn-coexist.sh" && magicnet_enable_vpn_coexist
37-
fi
38-
fi
39-
elif [ "${MAGIC_MIHOMO}" -ne 0 ] && has_command "mihomo"; then
40-
import __mihomo__
41-
mihomo_start && [ -f "${MODDIR}/hotspot-forward.sh" ] && . "${MODDIR}/hotspot-forward.sh" && magicnet_enable_hotspot_forward
42-
[ -f "${MODDIR}/vpn-coexist.sh" ] && . "${MODDIR}/vpn-coexist.sh" && magicnet_enable_vpn_coexist
43-
else
44-
print "MagicNet: No supported kernel found or starting disabled (mihomo or sing-box)."
45-
fi
13+
kamfw run boot-completed -- "$@"

src/MagicNet/hotspot-forward.sh

Lines changed: 4 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,8 @@
11
#!/system/bin/sh
22
# shellcheck shell=ash
33

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"
67

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

Comments
 (0)