-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhit_proxy_v2.bash
More file actions
executable file
·228 lines (199 loc) · 5.65 KB
/
Copy pathhit_proxy_v2.bash
File metadata and controls
executable file
·228 lines (199 loc) · 5.65 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
set -ef -o pipefail
usage() {
cat <<EOF
Usage: $0 [options] [proxy_ip]
A script to run the Lantern client against a specific proxy,
fetched dynamically based on track, provider, or protocol.
Options:
-T, --track <track> Fetch proxy config for a specific track.
-p, --protocol <protocol> Fetch proxy config for a specific protocol. Ignored if --track is provided.
-P, --provider <provider> Filter proxies by a specific provider.
--proxyless Enable proxyless transport.
--no-build Do not rebuild lantern-client before running.
--force-refresh Force download of a new proxy list, ignoring cache.
--help Display this help message.
Details:
- If a [proxy_ip] is provided, it is used directly and all other flags for proxy selection (--track, --provider, --protocol) are ignored.
- If no [proxy_ip] is provided, --track, --provider, or --protocol must be used to find a proxy.
EOF
exit 1
}
error() {
echo "$1" >&2
exit 1
}
# --- Argument Parsing ---
TRACK=""
PROVIDER=""
PROTOCOL=""
BUILD=true
FORCE_REFRESH=false
USE_PROXYLESS=false
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-T | --track)
TRACK="$2"
shift 2
;;
-P | --provider)
PROVIDER="$2"
shift 2
;;
-p | --protocol)
PROTOCOL="$2"
shift 2
;;
--proxyless)
USE_PROXYLESS=true
shift
;;
--no-build)
BUILD=false
shift
;;
--force-refresh)
FORCE_REFRESH=true
shift
;;
--help)
usage
;;
-*)
echo "Unknown option: $1"
usage
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
# --- Argument Validation ---
if [[ -z "$TRACK" && -z "$PROVIDER" && -z "$PROTOCOL" && ${#POSITIONAL_ARGS[@]} -eq 0 ]]; then
usage
fi
LANTERN_CLOUD="${LANTERN_CLOUD:-../lantern-cloud}"
PROXY=""
fetch_proxies() {
local args=("${@:1:$(($# - 1))}") # All but last arg
local outfile="${!#}" # Last arg
local route_list
if [ -f "$outfile" ]; then
if $FORCE_REFRESH; then
echo "Forcing refresh of cached proxies..." >&2
rm "$outfile"
elif [ "$(find "$outfile" -mtime +1)" ]; then
echo "Cached proxies are older than 1 hour. Refreshing..." >&2
rm "$outfile"
else
echo "Using cached proxies from $outfile" >&2
route_list=$(cat "$outfile")
fi
fi
if [[ -z "$route_list" ]]; then
route_list=$($LANTERN_CLOUD/bin/lc routes list "${args[@]}" 2>&1)
if [[ $? -ne 0 ]]; then
if grep -qv "no routes" <<<"$route_list"; then
error "Failed to fetch proxies: $route_list"
fi
route_list=""
else
route_list=$(echo "$route_list" | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' || true)
echo "$route_list" >"$outfile"
fi
fi
echo "$route_list"
}
# --- Main Logic ---
TMPDIR="${TMP:-/tmp}/hit_lc_proxy"
mkdir -p "$TMPDIR"
if [[ ${#POSITIONAL_ARGS[@]} -eq 1 ]]; then
PROXY="${POSITIONAL_ARGS[0]}"
echo "Using specified proxy IP: "$PROXY""
else
if ! command -v shuf &>/dev/null; then
error "'shuf' command not found. Please install GNU coreutils."
fi
ROUTE_LIST=""
# --protocol
if [[ -n "$PROTOCOL" && -z "$TRACK" ]]; then
echo "Fetching tracks for protocol: "$PROTOCOL""
TRACKS_TABLE=$($LANTERN_CLOUD/bin/lc tracks list --columns name,providers --filter-protocol "$PROTOCOL" || echo "")
if [[ $? -ne 0 ]]; then
error "Could not fetch tracks for protocol '$PROTOCOL'. Check your connection or if the protocol exists."
fi
if [[ -n "$PROVIDER" ]]; then
TRACKS=$(echo "$TRACKS_TABLE" | tail -n +2 | grep -i "$PROVIDER" | awk '{print $1}' || true)
else
TRACKS=$(echo "$TRACKS_TABLE" | tail -n +2 | awk 'NR>2{print a[NR%3]} {a[NR%3]=$1}')
fi
if [ -z "$TRACKS" ]; then
error "No tracks found for protocol '$PROTOCOL' with provider '$PROVIDER'."
fi
for track in $(echo "$TRACKS" | shuf); do
echo "Attempting to fetch proxies for track: "$track""
OUTFILE="$TMPDIR/${track}_all_proxies.txt"
ROUTE_LIST=$(fetch_proxies --track "$track" "$OUTFILE")
if [[ -n "$ROUTE_LIST" ]]; then
echo "Found proxies for track: "$track""
break
fi
echo "No routes for "$track".."
done
# --track and/or --provider
else
LC_ARGS=()
CACHE_KEY="proxies"
if [[ -n "$TRACK" ]]; then
LC_ARGS+=(--track "$TRACK")
CACHE_KEY="${TRACK}"
fi
if [[ -n "$PROVIDER" ]]; then
LC_ARGS+=(--provider "$PROVIDER")
CACHE_KEY="${CACHE_KEY}_${PROVIDER}"
fi
OUTFILE="$TMPDIR/${CACHE_KEY}_all_proxies.txt"
echo "Fetching all proxies for "${LC_ARGS[*]}""
ROUTE_LIST=$(fetch_proxies "${LC_ARGS[@]}" "$OUTFILE")
fi
if [[ -z "$ROUTE_LIST" ]]; then
error "No proxies found. Please check your configuration or network connection."
fi
PROXY=$(echo "$ROUTE_LIST" | sort -R | head -n 1 | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' || true)
if [ -z "$PROXY" ]; then
error "Could not find a valid proxy IP in the fetched list."
fi
echo "Selected random proxy: $PROXY"
fi
# --- Execution ---
if [ -z "$PROXY" ]; then
error "Could not determine a proxy to use."
fi
PROXY_TMP_DIR="${TMP:-/tmp}/hit_lc_proxy/$PROXY"
OUTFILE="$PROXY_TMP_DIR/user.conf"
rm -rf "$PROXY_TMP_DIR"
mkdir -p "$PROXY_TMP_DIR"
echo "Generating config for ${PROXY} in ${OUTFILE}..."
CONFIG=$($LANTERN_CLOUD/bin/lc route dump-config "$PROXY")
# wrap the proxy config to match the format expected by flashlight.
# [ConfigResponse (getlantern/flashlight/apipb/types.proto)]
OUTPUT="{\"country\": \"US\",\"proxy\":{\"proxies\":[${CONFIG}]}}"
if command -v jq &>/dev/null; then
echo "$OUTPUT" | jq . >"$OUTFILE"
else
echo "$OUTPUT" >"$OUTFILE"
fi
if [ "$(uname)" == "Linux" ]; then
if $BUILD; then
make linux-amd64
fi
CMD="flutter run"
else
if $BUILD; then
make macos ffigen
fi
CMD="flutter run -d macOS"
fi
PROXYLESS=$USE_PROXYLESS CONFIG_DIR=$PROXY_TMP_DIR READABLE_CONFIG=true STICKY_CONFIG=true $CMD