-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathff-ca
More file actions
executable file
·181 lines (145 loc) · 4.07 KB
/
Copy pathff-ca
File metadata and controls
executable file
·181 lines (145 loc) · 4.07 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
set -euo pipefail
usage() {
cat <<EOF
Usage:
$(basename "$0") <exit-node-ip>
Example:
$(basename "$0") 100.85.34.142
Description:
Launches tailsocks using the provided exit node IP,
waits until the SOCKS proxy is fully usable,
then opens Firefox with the SecureProxy profile.
Arguments:
exit-node-ip Required. The Tailscale exit node IP (e.g. 100.x.x.x).
EOF
}
# Require exactly one argument
if [[ $# -ne 1 ]]; then
echo "ERROR: exit node IP is required."
echo
usage
exit 1
fi
EXIT_NODE="$1"
STATE_DIR="$HOME/.config/tsnet-state"
TAILSOCKS_BIN="$HOME/go/bin/tailsocks"
FIREFOX_BIN="/Applications/Firefox.app/Contents/MacOS/firefox"
FIREFOX_PROFILE="SecureProxy"
SOCKS_HOST="127.0.0.1"
SOCKS_PORT="5040"
SOCKS_ADDR="${SOCKS_HOST}:${SOCKS_PORT}"
ts_pid=""
ff_pid=""
find_existing_firefox_pid() {
ps -axww -o pid= -o command= | while read -r pid cmd; do
if [[ "${cmd}" == *"${FIREFOX_BIN}"* ]] && [[ "${cmd}" == *"-P ${FIREFOX_PROFILE}"* ]]; then
echo "${pid}"
return 0
fi
done
}
wait_for_port_listen() {
local host="$1"
local port="$2"
local timeout_s="${3:-15}"
local deadline=$(( $(date +%s) + timeout_s ))
echo "Waiting for SOCKS proxy to listen on ${host}:${port} (timeout ${timeout_s}s)..."
while true; do
if nc -z -w 1 "${host}" "${port}" >/dev/null 2>&1; then
echo "SOCKS proxy is listening on ${host}:${port}"
return 0
fi
if (( $(date +%s) >= deadline )); then
echo "ERROR: Timed out waiting for SOCKS proxy on ${host}:${port}" >&2
return 1
fi
sleep 0.2
done
}
wait_for_socks_ready() {
local host="$1"
local port="$2"
local test_url="https://icanhazip.com"
local proxy="socks5h://${host}:${port}"
local ready_timeout_s="20"
local deadline=$(( $(date +%s) + ready_timeout_s ))
wait_for_port_listen "${host}" "${port}"
echo "Waiting for SOCKS proxy to be usable via curl through ${proxy}..."
echo "Test URL: ${test_url}"
while true; do
out="$(
curl -fsS \
--proxy "${proxy}" \
--max-time 2 \
"${test_url}" 2>/dev/null || true
)"
if [[ -n "${out}" ]]; then
if echo "${out}" | grep -Eq '^([0-9]{1,3}\.){3}[0-9]{1,3}$|^[0-9a-fA-F:]+$'; then
echo "SOCKS proxy is usable. Probe response: ${out}"
return 0
fi
echo "SOCKS proxy request succeeded (non-IP response): ${out}"
return 0
fi
if (( $(date +%s) >= deadline )); then
echo "ERROR: Timed out waiting for SOCKS proxy to be usable via ${proxy}" >&2
return 1
fi
sleep 0.3
done
}
cleanup() {
set +e
if [[ -n "${ff_pid}" ]] && kill -0 "${ff_pid}" 2>/dev/null; then
echo "Stopping Firefox (PID ${ff_pid})..."
kill "${ff_pid}" 2>/dev/null || true
wait "${ff_pid}" 2>/dev/null || true
fi
if [[ -n "${ts_pid}" ]] && kill -0 "${ts_pid}" 2>/dev/null; then
echo "Stopping tailsocks (PID ${ts_pid})..."
kill "${ts_pid}" 2>/dev/null || true
wait "${ts_pid}" 2>/dev/null || true
fi
}
trap cleanup EXIT INT TERM
echo "Starting tailsocks using exit node ${EXIT_NODE}..."
"${TAILSOCKS_BIN}" \
--ephemeral=false \
--exit-node="${EXIT_NODE}" \
--state-dir="${STATE_DIR}" \
--socks-addr="${SOCKS_ADDR}" \
&
ts_pid=$!
echo "tailsocks started with PID ${ts_pid}"
wait_for_socks_ready "${SOCKS_HOST}" "${SOCKS_PORT}"
maybe_existing_ff_pid="$(find_existing_firefox_pid || true)"
if [[ -n "${maybe_existing_ff_pid}" ]]; then
ff_pid="${maybe_existing_ff_pid}"
echo "Firefox is already running with profile '${FIREFOX_PROFILE}' (PID ${ff_pid})"
else
echo "Starting Firefox profile '${FIREFOX_PROFILE}'..."
"${FIREFOX_BIN}" \
-P "${FIREFOX_PROFILE}" \
&
ff_pid=$!
echo "Firefox started with PID ${ff_pid}"
sleep 1
osascript -e '
tell application "System Events"
if exists (process "Firefox") then
tell process "Firefox"
set frontmost to true
end tell
end if
end tell
' || true
fi
if jobs -p | grep -qx "${ff_pid}"; then
wait "${ff_pid}"
else
while kill -0 "${ff_pid}" 2>/dev/null; do
sleep 10
done
fi
echo "Firefox has been closed."