Skip to content

Commit a990e71

Browse files
committed
Merge branch 'master' of github.com:thekhanj/salsa
2 parents 0cf85f8 + 826214d commit a990e71

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

bin/salsa

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/env bash
2+
3+
PID=$$
4+
PROC_DIR="/var/run/salsa/$PID"
5+
[ "$USER" != 'root' ] && PROC_DIR="/run/user/$(id -u)/salsa/$PID"
6+
SLEEP_INTERVAL=5
7+
CHECK_PROXY=
8+
PROXIES_ADDRESS=()
9+
LISTENING_ADDRESS=
10+
HELP=0
11+
12+
help() {
13+
exec man salsa
14+
}
15+
16+
error() {
17+
echo "$1" >&2
18+
}
19+
20+
parse_args() {
21+
while [ $# -gt 0 ]; do
22+
case "$1" in
23+
'-l')
24+
LISTENING_ADDRESS="$2"
25+
shift 2
26+
;;
27+
'-t')
28+
SLEEP_INTERVAL="$2"
29+
shift 2
30+
;;
31+
'-c')
32+
CHECK_PROXY="$2"
33+
shift 2
34+
;;
35+
'-h' | '--help')
36+
HELP=1
37+
shift
38+
;;
39+
*)
40+
PROXIES_ADDRESS+=("$1")
41+
shift
42+
;;
43+
esac
44+
done
45+
}
46+
47+
validate_proxy_address() {
48+
local address="$1"
49+
50+
[ -z "$address" ] && {
51+
error 'empty proxy address.'
52+
exit 3
53+
}
54+
echo "$address" |
55+
grep -E '^(([0-9a-z]+\.)*[0-9a-z]+|([0-9]{1,3}\.){3}[0-9]{1,3})?:[0-9]{1,5}$' >/dev/null ||
56+
{
57+
error "invalid proxy address. ($address)"
58+
exit 3
59+
}
60+
}
61+
62+
validate_input() {
63+
for address in "${PROXIES_ADDRESS[@]}"; do
64+
validate_proxy_address "$address"
65+
done
66+
67+
[ -z "$LISTENING_ADDRESS" ] && help
68+
69+
echo "$LISTENING_ADDRESS" |
70+
grep -E '^(([0-9]{1,3}\.){3}[0-9]{1,3})?:[0-9]{1,5}$' >/dev/null ||
71+
{
72+
error "invalid listening address. ($LISTENING_ADDRESS)"
73+
exit 1
74+
}
75+
76+
echo "$SLEEP_INTERVAL" |
77+
grep '^[0-9]\+$' >/dev/null ||
78+
{
79+
error "invalid sleep interval. ($SLEEP_INTERVAL)"
80+
exit 2
81+
}
82+
}
83+
84+
get_host() {
85+
local ret="$(echo "$1" |
86+
grep -oP '^(([0-9a-z]+\.)*[0-9a-z]+|([0-9]{1,3}\.){3}[0-9]{1,3})?')"
87+
88+
[ -z "$ret" ] && echo -n '0.0.0.0' || echo -n "$ret"
89+
}
90+
91+
get_port() {
92+
echo "$1" |
93+
grep -oP '(?<=:)[0-9]{1,5}$'
94+
}
95+
96+
check_proxy() {
97+
local proxy="$1"
98+
99+
[ -z "$CHECK_PROXY" ] && {
100+
local port="$(get_port "$proxy")"
101+
ss -tnlp | grep :$port >/dev/null
102+
return $?
103+
}
104+
105+
$CHECK_PROXY "$proxy"
106+
}
107+
108+
check_proxies() {
109+
while true; do
110+
for proxy in "${PROXIES_ADDRESS[@]}"; do
111+
if check_proxy "$proxy"; then
112+
[ -f "$PROC_DIR/${proxy}.unhealthy" ] && {
113+
echo "proxy $proxy is back up"
114+
rm "${PROC_DIR}/${proxy}.unhealthy"
115+
}
116+
else
117+
[ -f "$PROC_DIR/${proxy}.unhealthy" ] || {
118+
echo "proxy $proxy is down"
119+
touch "${PROC_DIR}/${proxy}.unhealthy"
120+
}
121+
fi
122+
done
123+
124+
sleep $SLEEP_INTERVAL
125+
done
126+
}
127+
128+
parse_args "$@"
129+
validate_input
130+
131+
[ $HELP -eq 1 ] && help
132+
133+
mkdir -p "$PROC_DIR"
134+
135+
listening_host="$(get_host "$LISTENING_ADDRESS")"
136+
listening_port="$(get_port "$LISTENING_ADDRESS")"
137+
138+
error "process id is $PID"
139+
error "listening on address $listening_host:$listening_port"
140+
printf "forwarding requests to proxies %s\n" "${PROXIES_ADDRESS[@]}"
141+
142+
command="salsa-handler '$PROC_DIR' '${PROXIES_ADDRESS[@]}'"
143+
144+
check_proxies &
145+
CHECK_PROXIES_PID=$!
146+
147+
socat TCP-LISTEN:$listening_port,bind=$listening_host,fork,reuseaddr,forever \
148+
EXEC:"$command"
149+
150+
kill -9 $CHECK_PROXIES_PID
151+
rm -rf $PROC_DIR

0 commit comments

Comments
 (0)