Skip to content

Commit

Permalink
add socks5 tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
anasfanani committed May 1, 2024
1 parent a7d9e17 commit c446160
Showing 10 changed files with 197 additions and 0 deletions.
Binary file added files/hev-socks5-tunnel-linux-arm
Binary file not shown.
Binary file added files/hev-socks5-tunnel-linux-arm64
Binary file not shown.
3 changes: 3 additions & 0 deletions system/bin/tailscaled.tun
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/system/bin/sh
export PATH=/data/adb/tailscale/scripts:$PATH
tailscaled.tun "$@"
7 changes: 7 additions & 0 deletions tailscale/scripts/start.sh
Original file line number Diff line number Diff line change
@@ -22,9 +22,16 @@ start_inotifyd() {
echo "${current_time} [Info]: Starting tailscaled inotify service" > "${tailscaled_service_log}"
inotifyd "${tailscaled_inotify}" "${module_dir}" >> "/dev/null" 2>&1 &
}
start_socks5tunnel(){
if [ ! -f "${module_dir}/disable" ]; then
echo "${current_time} [Info]: Starting socks5tunnel service" > "${tailscaled_service_log}"
"${tailscaled_tun}" start >> "/dev/null" 2>&1
fi
}
mkdir -p ${tailscaled_run_dir}
rm -f ${tailscaled_runs_log}
module_version=$(busybox awk -F'=' '!/^ *#/ && /version=/ { print $2 }' "$module_prop" 2>/dev/null)
log Info "Magisk Tailscaled version : ${module_version}."
start_service
start_socks5tunnel
start_inotifyd
2 changes: 2 additions & 0 deletions tailscale/scripts/tailscaled.inotify
Original file line number Diff line number Diff line change
@@ -11,9 +11,11 @@ service_control() {
if [ "${events}" = "d" ]; then
echo "${current_time} [Info]: ✔ Starting tailscaled service" >> "${tailscaled_service_log}"
"${tailscaled_service}" start 2>&1 >> "${tailscaled_service_log}"
"${tailscaled_tun}" start 2>&1 >> "${tailscaled_service_log}"
elif [ "${events}" = "n" ]; then
echo "${current_time} [Info]: ✘ Stopping tailscaled service" >> "${tailscaled_service_log}"
"${tailscaled_service}" stop 2>&1 >> "${tailscaled_service_log}"
"${tailscaled_tun}" stop 2>&1 >> "${tailscaled_service_log}"
fi
fi
}
100 changes: 100 additions & 0 deletions tailscale/scripts/tailscaled.tun
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/system/bin/sh
DIR=${0%/*}
source $DIR/../settings.ini
scripts_name=$(basename $0)

start_tun(){
PID=$(busybox pidof "${socks5_tun_bin}")
if [ -n "$PID" ]; then
log Info "✔ socks5-tunnel service already running with PID : ( $PID )."
return
fi
# The logs time cannot set to current timezone, default is UTC.
nohup ${socks5_tun_bin} ${socks5_tun_conf} > "${socks5_tun_log}" 2>&1 &
PID=$!
echo -n $PID > "${socks5_tun_pid}"
log Info "✔ socks5-tunnel service running with PID : ( $PID )."
sh ${socks5_tun_pre_up}
}

stop_tun(){
# Check if the binary is running using pgrep
if busybox pgrep "${socks5_tun_bin}" >/dev/null; then
sh ${socks5_tun_pre_down}
# Use `busybox pkill` to kill the binary with signal 15, otherwise use `killall`.
if busybox pkill -15 -e "${socks5_tun_bin}" >/dev/null 2>&1; then
: # Do nothing if busybox pkill is successful
else
killall -15 "${socks5_tun_bin}" >/dev/null 2>&1 || kill -15 "$(busybox pidof "${socks5_tun_bin}")" >/dev/null 2>&1
fi
else
log Info "✘ socks5-tunnel service already stop."
rm -f "${socks5_tun_pid}"
[ -t 1 ] && echo -e "${white}--------------------------------------------${normal}"
return
fi
# Check if the binary has stopped
sleep 0.5
if ! busybox pidof "${socks5_tun_bin}" >/dev/null 2>&1; then
# Delete the `socks5-tunnel.pid` file if it exists
rm -f "${socks5_tun_pid}"
log Info "✘ socks5-tunnel shutting down, service is stopped."
log Info "✘ socks5-tunnel disconnected."
[ -t 1 ] && echo -e "${white}--------------------------------------------${normal}"
else
log Warning "⚠️ socks5-tunnel Not stopped; may still be shutting down or failed to shut down."
force_stop
fi

}
force_stop() {
# try forcing it to shut down.
log Warning "⚠️ try forcing it to shut down."
# Use `busybox pkill` to kill the binary with signal 9, otherwise use `killall`.
if busybox pkill -9 "${socks5_tun_bin}"; then
: # Do nothing if busybox pkill is successful
else
if command -v killall >/dev/null 2>&1; then
killall -9 "${socks5_tun_bin}" >/dev/null 2>&1 || true
else
pkill -9 "${socks5_tun_bin}" >/dev/null 2>&1 || true
fi
fi
sleep 0.5
if ! busybox pidof "${socks5_tun_bin}" >/dev/null 2>&1; then
log Info "✔ done, you can sleep peacefully."
[ -t 1 ] && echo -e "${white}--------------------------------------------${normal}"
rm -f "${socks5_tun_pid}"
fi
}
case "$1" in
start)
start_tun
;;
stop)
stop_tun
;;
restart)
stop_tun
sleep 0.5
start_tun
;;
status)
# Check whether the service is running or not
PID=$(busybox pidof "${socks5_tun_bin}")
if [ -n "$PID" ]; then
log Info "✔ socks5-tunnel for tailscaled service is running with PID : ( $PID )."
else
log Info "✘ socks5-tunnel for tailscaled service is stopped."
fi
;;
log)
log_view $2
;;
*)
if [ -n "$1" ]; then
echo "${red}${scripts_name} $1 not found${normal}"
fi
echo "${yellow}usage${normal}: ${green}${scripts_name}${normal} {${yellow}start|stop|restart|status${normal}}"
;;
esac
47 changes: 47 additions & 0 deletions tailscale/scripts/tailscaled.tun.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
tunnel:
# Interface name
name: tailscale0
# Interface MTU
mtu: 8500
# Multi-queue
multi-queue: false
# IPv4 address
ipv4: 100.100.100.100
# IPv6 address
ipv6: 'fc00::1'
# Post up script
#post-up-script: /data/adb/tailscale/scripts/tailscaled.tun.up
# Pre down script
#pre-down-script: /data/adb/tailscale/scripts/tailscaled.tun.down

socks5:
# Socks5 server port
port: 1099
# Socks5 server address (ipv4/ipv6)
address: 127.0.0.1
# Socks5 UDP relay mode (tcp|udp)
udp: 'udp'
# Socks5 handshake using pipeline mode
# pipeline: false
# Socks5 server username
# username: 'username'
# Socks5 server password
# password: 'password'
# Socket mark
mark: 1099

misc:
# task stack size (bytes)
# task-stack-size: 20480
# connect timeout (ms)
# connect-timeout: 5000
# read-write timeout (ms)
# read-write-timeout: 60000
# stdout, stderr or file-path
log-file: stdout
# debug, info, warn or error
log-level: debug
# If present, run as a daemon with this pid file
#pid-file: /data/adb/tailscale/run/hev-socks5-tunnel.pid
# If present, set rlimit nofile; else use default value
# limit-nofile: 65535
10 changes: 10 additions & 0 deletions tailscale/scripts/tailscaled.tun.down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/system/bin/sh
DIR=${0%/*}
source $DIR/../settings.ini

ip rule del fwmark 1099 table 1099
ip rule del fwmark 1099 lookup 1099
ip route del default dev tailscale0 table 1099
iptables -t mangle -D OUTPUT -d 100.64.0.0/10 -j MARK --set-mark 1099
ip route del 100.64.0.0/10 dev tailscale0
log Info "✘ iptables route deleted"
17 changes: 17 additions & 0 deletions tailscale/scripts/tailscaled.tun.up
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/system/bin/sh
DIR=${0%/*}
source $DIR/../settings.ini

# Add route
ip route add 100.64.0.0/10 dev tailscale0 metric 1

# Set iptables rules to mark packets
iptables -t mangle -A OUTPUT -d 100.64.0.0/10 -j MARK --set-mark 1099

# Add IP rule
ip rule add fwmark 1099 table 1099 pref 18500
ip rule add fwmark 1099 lookup 1099

# Add default route for marked traffic
ip route add default dev tailscale0 table 1099 metric 1
log Info "✔ iptables route added"
11 changes: 11 additions & 0 deletions tailscale/settings.ini
Original file line number Diff line number Diff line change
@@ -28,9 +28,20 @@ tailscaled_pid="${tailscaled_run_dir}/tailscaled.pid"
tailscaled_runs_log="${tailscaled_run_dir}/runs.log"
tailscaled_service_log="${tailscaled_run_dir}/service.log"


# HevSocks5Tunnel
socks5_tun_bin="${tailscale_dir}/bin/socks5-tunnel"
socks5_tun_conf="${tailscaled_scripts_dir}/tailscaled.tun.config.yaml"
socks5_tun_pre_up="${tailscaled_scripts_dir}/tailscaled.tun.up"
socks5_tun_pre_down="${tailscaled_scripts_dir}/tailscaled.tun.down"
socks5_tun_log="${tailscaled_run_dir}/socks5-tun.log"
socks5_tun_pid="${tailscaled_run_dir}/socks5-tun.pid"


# Set tailscaled services variables
tailscaled_service="${tailscaled_scripts_dir}/tailscaled.service"
tailscaled_inotify="${tailscaled_scripts_dir}/tailscaled.inotify"
tailscaled_tun="${tailscaled_scripts_dir}/tailscaled.tun"

# Take the current time
current_time=$(date +"%I:%M %P")

0 comments on commit c446160

Please sign in to comment.