-
Notifications
You must be signed in to change notification settings - Fork 8
/
ts-warp.sh.in
214 lines (188 loc) · 7.33 KB
/
ts-warp.sh.in
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
#!/bin/sh
# ---------------------------------------------------------------------------- #
# TS-Warp - rc script
# ---------------------------------------------------------------------------- #
# Copyright (c) 2021-2024, Mikhail Zakharov <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------- #
# Start/stop rc-script
# Requires root priveleges to run
# ---------------------------------------------------------------------------- #
# -- CONFIGURATION VARIABLES ------------------------------------------------- #
tswarp_prefix="/usr/local"
tswarp_inifile="$tswarp_prefix/etc/ts-warp.ini"
tswarp_pidfile="$tswarp_prefix/var/run/ts-warp.pid"
tswarp_logfile="$tswarp_prefix/var/log/ts-warp.log"
tswarp_actfile="$tswarp_prefix/var/spool/ts-warp/ts-warp.act"
tswarp_loglevel="2"
tswarp_logfile_maxsize=3145728
tswarp_options="-c $tswarp_inifile -l $tswarp_logfile -p $tswarp_pidfile -t $tswarp_actfile -d -v $tswarp_loglevel"
# ---------------------------------------------------------------------------- #
_start() {
_status
case $? in
0)
printf "ts-warp is already running: "
return 0
;;
1)
;;
2|3)
printf "ts-warp status unknown: requesting 'stop' "
_stop
;;
esac
_check_root
printf "Starting ts-warp: "
log_size=0
case $(uname -s) in
Linux)
if iptables -V ; then
printf "Using IPTABLES Firewall "
[ -f "$tswarp_prefix"/etc/ts-warp_iptables.sh ] &&
"$tswarp_prefix"/etc/ts-warp_iptables.sh || {
printf "FATAL: Unable to find ts-warp_iptables.sh\n"
exit 1
}
elif nft -v ; then
printf "Using NFTABLES Firewall "
[ -f "$tswarp_prefix"/etc/ts-warp_nftables.sh ] &&
"$tswarp_prefix"/etc/ts-warp_nftables.sh || {
printf "FATAL: Unable to find ts-warp_nftables.sh\n"
exit 1
}
else
printf "FATAL: Unable to find any Linux firewall\n"
exit 1
fi
log_size=$(stat -c "%s" "$tswarp_logfile")
;;
Darwin|FreeBSD|OpenBSD)
/sbin/pfctl -eq
awk -v pf_conf="$tswarp_prefix"/etc/ts-warp_pf.conf '
/ts-warp/ {next}
/ns-warp/ {next}
/rdr-anchor/ {rdrpos = NR}
{pf[NR] = $0}
END {
for (i = 0; i < NR; i++)
if (i == rdrpos + 1) {
print("rdr-anchor \"ts-warp\"")
print("rdr-anchor \"ns-warp\"")
} else
print(pf[i])
print("anchor \"ts-warp\"")
print("anchor \"ns-warp\"")
printf("load anchor \"ts-warp\" from \"%s\"\n", pf_conf)
}
' /etc/pf.conf | /sbin/pfctl -q -f -
log_size=$(stat -f "%z" "$tswarp_logfile")
;;
*)
printf "FATAL: Unsupported OS "
esac
# Truncate logfile if it's too big
[ $tswarp_logfile_maxsize -lt $log_size ] && : > $tswarp_logfile
echo $tswarp_options $* | xargs "$tswarp_prefix"/bin/ts-warp > /dev/null
}
# ---------------------------------------------------------------------------- #
_status() {
# Set $1 if status message is required
[ -s "$tswarp_pidfile" -a -r "$tswarp_pidfile" ] || {
[ "$1" ] && printf "ts-warp is not running: "
return 1
}
pgrep -F "$tswarp_pidfile" > /dev/null 2>&1
case $? in
0)
[ "$1" ] && printf "ts-warp is running PID: %s: " $(cat "$tswarp_pidfile")
return 0
;;
1)
[ "$1" ] && printf "ts-warp is NOT running PID: %s: " $(cat "$tswarp_pidfile")
return 2
;;
*)
[ "$1" ] && printf "ts-warp is not found: "
return 3
;;
esac
}
# ---------------------------------------------------------------------------- #
_stop() {
_check_root
printf "Stopping ts-warp: "
[ -f "$tswarp_pidfile" -a -s "$tswarp_pidfile" -a -r "$tswarp_pidfile" ] && {
cat "$tswarp_pidfile" | xargs kill -TERM > /dev/null 2>&1 || pkill -x ts-warp
}
[ -f "$tswarp_pidfile" ] && rm -f "$tswarp_pidfile"
pkill -x ts-warp
uname -s | grep -i 'Linux' >/dev/null || {
/sbin/pfctl -a ts-warp -F all
/sbin/pfctl -a ns-warp -F all
}
}
# ---------------------------------------------------------------------------- #
_reload() {
# Re-read configuration file on HUP signal
_status && {
_check_root
printf "Reloading ts-warp: "
cat "$tswarp_pidfile" | xargs kill -HUP
} || {
printf "Unable to reload: ts-warp is not running\n"
exit 1
}
}
# ---------------------------------------------------------------------------- #
_restart() {
_check_root
printf "Restarting ts-warp: "
_stop
_start $*
}
# ---------------------------------------------------------------------------- #
_check_root() {
[ $(id -u) -ne 0 ] && {
printf "Fatal: You must be root to proceed\n"
exit 1
}
}
# ---------------------------------------------------------------------------- #
_usage() {
printf "Usage:\n\tts-warp.sh start|stop|reload|restart [options]\n"
printf "\tts-warp.sh status\n"
exit 1
}
# ---------------------------------------------------------------------------- #
[ $# -eq 0 ] && _usage
case "$1" in
[sS][tT][aA][rR][tT]) shift; _start $* ;;
[sS][tT][aA][tT][uU][sS]) _status 1 ;;
[sS][tT][oO][pP]) _stop ;;
[rR][eE][lL][oO][aA][dD]) _reload ;;
[rR][eE][sS][tT][aA][rR][tT]) shift; _restart $* ;;
*) printf "Unknown command: %s\n" "$1"; _usage ;;
esac
printf "done\n"
exit 0