177 lines
7.4 KiB
Bash
177 lines
7.4 KiB
Bash
#!/system/bin/sh
|
|
# VeilBox profile switcher - trusttunnel | olcrtc
|
|
# Usage:
|
|
# su -c '/data/adb/box/scripts/box.profile list'
|
|
# su -c '/data/adb/box/scripts/box.profile use trusttunnel'
|
|
# su -c '/data/adb/box/scripts/box.profile use olcrtc'
|
|
# su -c '/data/adb/box/scripts/box.profile status'
|
|
|
|
if ! /system/bin/sh -n /data/adb/box/settings.ini 2>/dev/null; then
|
|
echo "Err: settings.ini syntax error" >&2
|
|
exit 1
|
|
fi
|
|
|
|
scripts_dir="${0%/*}"
|
|
. /data/adb/box/settings.ini
|
|
|
|
profiles_dir="${box_dir}/sing-box"
|
|
ini=/data/adb/box/settings.ini
|
|
|
|
profile_apply_settings() {
|
|
name="$1"
|
|
case "${name}" in
|
|
trusttunnel|tt)
|
|
sed -i 's/^name_sing_config=.*/name_sing_config="config-trusttunnel.json"/' "${ini}"
|
|
sed -i 's/^sidecar_enable=.*/sidecar_enable="true"/' "${ini}"
|
|
# both + set_final=false → BFR Manager can also flip without breaking finals
|
|
sed -i 's/^sidecar_protocol=.*/sidecar_protocol="both"/' "${ini}"
|
|
sed -i 's/^sidecar_listen_port=.*/sidecar_listen_port="10800"/' "${ini}"
|
|
sed -i 's/^sidecar_outbound_tag=.*/sidecar_outbound_tag="sidecar-proxy"/' "${ini}"
|
|
sed -i 's/^sidecar_set_final=.*/sidecar_set_final="false"/' "${ini}"
|
|
sed -i 's/^wgobfs_enable=.*/wgobfs_enable="false"/' "${ini}"
|
|
grep -q '^wgobfs_enable=' "${ini}" || echo 'wgobfs_enable="false"' >> "${ini}"
|
|
echo "trusttunnel"
|
|
;;
|
|
olcrtc|olc)
|
|
sed -i 's/^name_sing_config=.*/name_sing_config="config-olcrtc.json"/' "${ini}"
|
|
sed -i 's/^sidecar_enable=.*/sidecar_enable="true"/' "${ini}"
|
|
sed -i 's/^sidecar_protocol=.*/sidecar_protocol="both"/' "${ini}"
|
|
sed -i 's/^sidecar_listen_port=.*/sidecar_listen_port="10801"/' "${ini}"
|
|
sed -i 's/^sidecar_outbound_tag=.*/sidecar_outbound_tag="sidecar-proxy"/' "${ini}"
|
|
sed -i 's/^sidecar_set_final=.*/sidecar_set_final="false"/' "${ini}"
|
|
sed -i 's/^wgobfs_enable=.*/wgobfs_enable="false"/' "${ini}"
|
|
grep -q '^wgobfs_enable=' "${ini}" || echo 'wgobfs_enable="false"' >> "${ini}"
|
|
# Prefer LAN DNS for olcrtc PreferGo resolver under root:net_admin
|
|
if [ -f "${box_dir}/sidecar/olcrtc.yaml" ] && [ -x "${box_dir}/bin/yq" ]; then
|
|
"${box_dir}/bin/yq" -i '.net.dns = "192.168.0.1:53"' "${box_dir}/sidecar/olcrtc.yaml" 2>/dev/null || true
|
|
fi
|
|
echo "olcrtc"
|
|
;;
|
|
wg-obfs|wgobfs|wg|riga4)
|
|
sed -i 's/^name_sing_config=.*/name_sing_config="config-wg-obfs.json"/' "${ini}"
|
|
sed -i 's/^wgobfs_enable=.*/wgobfs_enable="true"/' "${ini}"
|
|
grep -q '^wgobfs_enable=' "${ini}" || echo 'wgobfs_enable="true"' >> "${ini}"
|
|
sed -i 's/^wgobfs_listen_port=.*/wgobfs_listen_port="41320"/' "${ini}"
|
|
grep -q '^wgobfs_listen_port=' "${ini}" || echo 'wgobfs_listen_port="41320"' >> "${ini}"
|
|
# sidecars optional; WG is the exit - keep both ready but don't force final rewrite
|
|
sed -i 's/^sidecar_set_final=.*/sidecar_set_final="false"/' "${ini}"
|
|
echo "wg-obfs"
|
|
;;
|
|
*)
|
|
echo "Unknown profile: ${name}" >&2
|
|
echo "Available: trusttunnel | olcrtc | wg-obfs" >&2
|
|
return 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
profile_seed_from_active() {
|
|
# If profile json missing, clone current sing config and patch final/dns
|
|
src="${sing_config}"
|
|
[ -f "${src}" ] || src="${profiles_dir}/config.json"
|
|
[ -f "${src}" ] || return 1
|
|
yq="${box_dir}/bin/yq"
|
|
[ -x "${yq}" ] || return 1
|
|
|
|
if [ ! -f "${profiles_dir}/config-trusttunnel.json" ]; then
|
|
cp -f "${src}" "${profiles_dir}/config-trusttunnel.json"
|
|
"${yq}" -i -o=json '
|
|
.route.final = "sidecar-proxy" |
|
|
(.dns.servers[] | select(.tag == "dns-remote") | .detour) = "sidecar-proxy" |
|
|
.dns.final = "dns-remote" |
|
|
.dns.strategy = "ipv4_only"
|
|
' "${profiles_dir}/config-trusttunnel.json"
|
|
if ! "${yq}" -e '.outbounds[] | select(.tag == "sidecar-proxy")' "${profiles_dir}/config-trusttunnel.json" >/dev/null 2>&1; then
|
|
"${yq}" -i -o=json '.outbounds += [{"type":"socks","tag":"sidecar-proxy","server":"127.0.0.1","server_port":10800,"version":"5"}]' "${profiles_dir}/config-trusttunnel.json"
|
|
fi
|
|
if ! "${yq}" -e '.outbounds[] | select(.tag == "olcrtc-proxy")' "${profiles_dir}/config-trusttunnel.json" >/dev/null 2>&1; then
|
|
"${yq}" -i -o=json '.outbounds += [{"type":"socks","tag":"olcrtc-proxy","server":"127.0.0.1","server_port":10801,"version":"5"}]' "${profiles_dir}/config-trusttunnel.json"
|
|
fi
|
|
log Info "[Profile] seeded config-trusttunnel.json"
|
|
fi
|
|
if [ ! -f "${profiles_dir}/config-olcrtc.json" ]; then
|
|
cp -f "${src}" "${profiles_dir}/config-olcrtc.json"
|
|
"${yq}" -i -o=json '
|
|
.route.final = "olcrtc-proxy" |
|
|
(.dns.servers[] | select(.tag == "dns-remote") | .detour) = "olcrtc-proxy" |
|
|
.dns.final = "dns-remote" |
|
|
.dns.strategy = "ipv4_only"
|
|
' "${profiles_dir}/config-olcrtc.json"
|
|
if ! "${yq}" -e '.outbounds[] | select(.tag == "sidecar-proxy")' "${profiles_dir}/config-olcrtc.json" >/dev/null 2>&1; then
|
|
"${yq}" -i -o=json '.outbounds += [{"type":"socks","tag":"sidecar-proxy","server":"127.0.0.1","server_port":10800,"version":"5"}]' "${profiles_dir}/config-olcrtc.json"
|
|
fi
|
|
if ! "${yq}" -e '.outbounds[] | select(.tag == "olcrtc-proxy")' "${profiles_dir}/config-olcrtc.json" >/dev/null 2>&1; then
|
|
"${yq}" -i -o=json '.outbounds += [{"type":"socks","tag":"olcrtc-proxy","server":"127.0.0.1","server_port":10801,"version":"5"}]' "${profiles_dir}/config-olcrtc.json"
|
|
fi
|
|
log Info "[Profile] seeded config-olcrtc.json"
|
|
fi
|
|
}
|
|
|
|
profile_list() {
|
|
echo "Profiles (also visible in BFR Manager → Manage Profiles):"
|
|
echo " trusttunnel -> config-trusttunnel.json (final=sidecar-proxy :10800)"
|
|
echo " olcrtc -> config-olcrtc.json (final=olcrtc-proxy :10801)"
|
|
echo " wg-obfs -> config-wg-obfs.json (Riga4 WG via wg-obfuscator :41320)"
|
|
echo " sidecar mode -> both (set_final=false) so app switching keeps finals"
|
|
echo " NOTE: wg-obfs needs VPS obfuscator on :13255 + same key"
|
|
echo
|
|
echo "Files in ${profiles_dir}:"
|
|
ls -1 "${profiles_dir}"/config*.json 2>/dev/null
|
|
echo
|
|
echo "Active:"
|
|
grep -E '^name_sing_config=|^sidecar_protocol=|^sidecar_listen_port=|^sidecar_outbound_tag=' "${ini}"
|
|
}
|
|
|
|
profile_status() {
|
|
. /data/adb/box/settings.ini
|
|
echo "name_sing_config=${name_sing_config}"
|
|
echo "sing_config=${sing_config}"
|
|
echo "sidecar_protocol=${sidecar_protocol} port=${sidecar_listen_port} tag=${sidecar_outbound_tag}"
|
|
if [ -x "${box_dir}/bin/yq" ] && [ -f "${sing_config}" ]; then
|
|
echo "route.final=$(${box_dir}/bin/yq -r '.route.final' "${sing_config}")"
|
|
fi
|
|
echo "tt=$(pidof trusttunnel_client 2>/dev/null)"
|
|
echo "olc=$(pidof olcrtc 2>/dev/null)"
|
|
echo "sb=$(pidof sing-box 2>/dev/null)"
|
|
}
|
|
|
|
profile_use() {
|
|
name="$1"
|
|
[ -n "${name}" ] || { echo "usage: $0 use {trusttunnel|olcrtc|wg-obfs}"; exit 1; }
|
|
profile_seed_from_active || true
|
|
applied=$(profile_apply_settings "${name}") || exit 1
|
|
|
|
# reload settings after sed
|
|
. /data/adb/box/settings.ini
|
|
|
|
if [ ! -f "${sing_config}" ]; then
|
|
log Error "[Profile] missing ${sing_config}"
|
|
exit 1
|
|
fi
|
|
|
|
log Info "[Profile] switching to ${applied} (${name_sing_config})"
|
|
"${scripts_dir}/box.service" restart
|
|
sleep 2
|
|
"${scripts_dir}/box.iptables" renew >/dev/null 2>&1 || true
|
|
profile_status
|
|
}
|
|
|
|
case "$1" in
|
|
list)
|
|
profile_list
|
|
;;
|
|
status)
|
|
profile_status
|
|
;;
|
|
use|switch)
|
|
profile_use "$2"
|
|
;;
|
|
seed)
|
|
profile_seed_from_active
|
|
;;
|
|
*)
|
|
echo "usage: $0 {list|status|use trusttunnel|use olcrtc|use wg-obfs|seed}"
|
|
exit 1
|
|
;;
|
|
esac
|