-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtun2socks
59 lines (50 loc) · 1.64 KB
/
tun2socks
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
#!/bin/sh
# PROVIDE: tun2socks
# REQUIRE: NETWORKING
# KEYWORD: shutdown
. /etc/rc.subr
name="tun2socks"
rcvar="tun2socks_enable"
pidfile="/var/run/${name}.pid"
logfile="/var/log/${name}.log"
command="/usr/local/bin/tun2socks"
command_args="/usr/local/etc/tun2socks/config.yaml"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
restart_cmd="${name}_restart"
status_cmd="${name}_status"
tun2socks_start() {
if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null; then
echo "${name} is already running." | tee -a "$logfile"
return 1
fi
#ifconfig hevsocks5 destroy || true
echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting ${name}..." | tee -a "$logfile"
nohup ${command} ${command_args} >> "$logfile" 2>&1 &
echo $! > "$pidfile"
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${name} started successfully." | tee -a "$logfile"
}
tun2socks_stop() {
if [ ! -f "$pidfile" ] || ! kill -0 $(cat "$pidfile") 2>/dev/null; then
echo "${name} is not running." | tee -a "$logfile"
return 1
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - Stopping ${name}..." | tee -a "$logfile"
kill $(cat "$pidfile") && rm -f "$pidfile"
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${name} has stopped." | tee -a "$logfile"
}
tun2socks_restart() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - Restarting ${name}..." | tee -a "$logfile"
tun2socks_stop
sleep 1
tun2socks_start
}
tun2socks_status() {
if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null; then
echo "tun2socks is running (PID: $(cat "$pidfile"))."
else
echo "tun2socks is not running."
fi
}
load_rc_config $name
run_rc_command "$1"