forked from bropines/tailscale-termux-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote-install.sh
More file actions
150 lines (132 loc) · 5.33 KB
/
Copy pathremote-install.sh
File metadata and controls
150 lines (132 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
set -eu
echo "Tailscale Termux Remote Installer"
echo "=============================="
REPO="bropines/tailscale-termux-cli"
BIN_DIR="${PREFIX:-/data/data/com.termux/files/usr}/bin"
STATE_DIR="$HOME/.tailscale"
LOG_FILE="$STATE_DIR/tailscaled.log"
SOCKET="$STATE_DIR/tailscaled.sock"
ENV_FILE="$STATE_DIR/.env"
VER_FILE="$STATE_DIR/version"
SOCKS_ADDR_FILE="$STATE_DIR/socks_addr"
echo "[1/3] Fetching latest release info..."
LATEST_TAG=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")')
if [ -z "$LATEST_TAG" ]; then
echo "Error: No releases found."
exit 1
fi
echo "-> Latest Release: $LATEST_TAG"
echo "[2/3] Downloading binaries to $BIN_DIR..."
pkill -f tailscaled || true
mkdir -p "$BIN_DIR"
wget -q --show-progress -O "$BIN_DIR/tailscaled" "https://github.com/$REPO/releases/download/$LATEST_TAG/tailscaled"
wget -q --show-progress -O "$BIN_DIR/tailscale" "https://github.com/$REPO/releases/download/$LATEST_TAG/tailscale"
chmod +x "$BIN_DIR/tailscaled" "$BIN_DIR/tailscale"
echo "$LATEST_TAG" > "$VER_FILE"
echo "[3/3] Setting up helper scripts..."
# Tailscaled START
cat << EOF > "$BIN_DIR/tailscaled-start"
#!/usr/bin/env bash
mkdir -p "$STATE_DIR"
if pgrep -f "tailscaled.*$STATE_DIR" > /dev/null; then
echo "tailscaled is already running."
exit 0
fi
if [ -f "$ENV_FILE" ]; then
set -a; source "$ENV_FILE"; set +a
fi
USER_ARGS=("\$@")
FINAL_ARGS=()
SOCKS_VAL=""
has_flag() {
local pattern="\$1"
for arg in "\${USER_ARGS[@]}"; do
if [[ "\$arg" == "\$pattern"* ]]; then
if [[ "\$arg" == *"="* ]]; then SOCKS_VAL="\${arg#*=}"; else SOCKS_VAL="NEXT"; fi
return 0
fi
done
return 1
}
has_flag "--statedir" || FINAL_ARGS+=("--statedir=$STATE_DIR")
has_flag "--socket" || FINAL_ARGS+=("--socket=$SOCKET")
has_flag "--tun" || FINAL_ARGS+=("--tun=userspace-networking")
if ! has_flag "--socks5-server"; then
if [ -n "\${TS_SOCKS5_SERVER:-}" ]; then SOCKS_VAL="\$TS_SOCKS5_SERVER"
elif [ -n "\${TS_SOCKS5_PORT:-}" ]; then SOCKS_VAL="127.0.0.1:\$TS_SOCKS5_PORT"
else
RANDOM_PORT=\$((RANDOM % 64511 + 1024))
SOCKS_VAL="127.0.0.1:\$RANDOM_PORT"
echo "Using random SOCKS5 port: \$RANDOM_PORT"
fi
FINAL_ARGS+=("--socks5-server=\$SOCKS_VAL")
else
if [ "\$SOCKS_VAL" == "NEXT" ]; then
for ((i=0; i<\${#USER_ARGS[@]}; i++)); do
if [[ "\${USER_ARGS[i]}" == "--socks5-server" ]]; then SOCKS_VAL="\${USER_ARGS[i+1]}"; break; fi
done
fi
fi
echo "\$SOCKS_VAL" > "$SOCKS_ADDR_FILE"
if ! has_flag "--outbound-http-proxy-listen" && [ -n "\${TS_HTTP_PROXY:-}" ]; then FINAL_ARGS+=("--outbound-http-proxy-listen=\$TS_HTTP_PROXY"); fi
if ! has_flag "--port" && [ -n "\${TS_PORT:-}" ]; then FINAL_ARGS+=("--port=\$TS_PORT"); fi
FINAL_ARGS+=("\${USER_ARGS[@]}")
if [ -n "\${TS_EXTRA_ARGS:-}" ]; then
read -ra EXTRA_ARR <<< "\$TS_EXTRA_ARGS"
FINAL_ARGS+=("\${EXTRA_ARR[@]}")
fi
echo "Starting tailscaled..."
nohup "$BIN_DIR/tailscaled" "\${FINAL_ARGS[@]}" >> "$LOG_FILE" 2>&1 &
sleep 2
if pgrep -f "tailscaled.*$STATE_DIR" > /dev/null; then echo "Done."; else echo "Error: tailscaled failed to start."; exit 1; fi
EOF
# Tailscaled STOP
cat << EOF > "$BIN_DIR/tailscaled-stop"
#!/usr/bin/env bash
pkill -f "tailscaled.*$STATE_DIR" || echo "tailscaled was not running."
rm -f "$SOCKS_ADDR_FILE"
EOF
# Tailscaled LOG
cat << EOF > "$BIN_DIR/tailscaled-log"
#!/usr/bin/env bash
tail -f "$LOG_FILE"
EOF
# Tailscale CLI
cat << EOF > "$BIN_DIR/tailscale-cli"
#!/usr/bin/env bash
exec "$BIN_DIR/tailscale" --socket="$SOCKET" "\$@"
EOF
# Tailscale UPDATE
cat << EOF > "$BIN_DIR/tailscale-update"
#!/usr/bin/env bash
echo "Checking for updates..."
REPO="bropines/tailscale-termux-cli"
LATEST_TAG=\$(curl -s "https://api.github.com/repos/\$REPO/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")')
CURRENT_VERSION="unknown"
if [ -f "$VER_FILE" ]; then CURRENT_VERSION=\$(cat "$VER_FILE"); fi
if [ "\$LATEST_TAG" == "\$CURRENT_VERSION" ]; then
echo "You are already on the latest version (\$CURRENT_VERSION)."
exit 0
fi
echo "New version available: \$LATEST_TAG (Current: \$CURRENT_VERSION)"
curl -fsSL https://raw.githubusercontent.com/\$REPO/main/remote-install.sh | bash
EOF
# Tailscale TEST
cat << EOF > "$BIN_DIR/tailscale-test"
#!/usr/bin/env bash
echo "Tailscale Functional Test"
echo "========================="
if ! pgrep -f "tailscaled.*$STATE_DIR" > /dev/null; then echo "[-] Error: tailscaled is not running."; exit 1; fi
IP=\$(tailscale-cli ip -4 2>/dev/null || echo "")
if [ -n "\$IP" ]; then echo "[+] Authenticated. IP: \$IP"; else echo "[-] Error: Not authenticated."; exit 1; fi
if [ -f "$SOCKS_ADDR_FILE" ]; then
SOCKS_ADDR=\$(cat "$SOCKS_ADDR_FILE")
echo "[*] Testing SOCKS5 on \$SOCKS_ADDR..."
if curl -s --socks5 "\$SOCKS_ADDR" https://1.1.1.1 > /dev/null; then echo "[+] SOCKS5 Connectivity (Direct IP): OK"; else echo "[-] SOCKS5 Connectivity (Direct IP): FAILED"; fi
if curl -s --socks5-hostname "\$SOCKS_ADDR" https://api.ipify.org > /dev/null; then echo "[+] SOCKS5 Resolution (Hostname): OK"; else echo "[-] SOCKS5 Resolution (Hostname): FAILED"; fi
fi
echo "========================="
EOF
chmod +x "$BIN_DIR/tailscaled-start" "$BIN_DIR/tailscaled-stop" "$BIN_DIR/tailscaled-log" "$BIN_DIR/tailscale-cli" "$BIN_DIR/tailscale-update" "$BIN_DIR/tailscale-test"
echo "Installation Complete!"