forked from edouard-lopez/sshuttle-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-ssh
executable file
·125 lines (107 loc) · 2.47 KB
/
proxy-ssh
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
#!/usr/bin/env bash
# DESCRIPTION
# Automatic proxy opening based on sshuttle
#
# USAGE
# sudo proxy-ssh start[ username@server[:port]]
# sudo proxy-ssh restart[ username@server[:port]]
# sudo proxy-ssh stop
# sudo proxy-ssh status
#
# AUTHOR
# Credit for the original script go to Salem Harrache's article:
# http://salem.harrache.info/proxy-socks-partout.html
### BEGIN INIT INFO
# Provides: proxy-ssh
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop Transparent proxy server for VPN over SSH
# Description: Provide a wrapper around sshuttle
### END INIT INFO
name="proxy-ssh"
sshserver="$2"
user="${sshserver%%@*}"
server="${sshserver##*@}"
host="${server%%:*}"
port=${server##*:}
port=${port:-22}
whoAmI="$LOGNAME" # user running the service
pidFile="$HOME"/.local/var/run/$name
mkdir -p "$(dirname "$pidFile")"
# Arrêt du service
proxy_stop() {
if [[ -f "$pidFile" ]] ; then
echo "Stopping $name..."
sudo -u "$whoAmI" kill -9 $(cat "$pidFile")
sudo -u "$whoAmI" rm "$pidFile" && echo "OK"
fi
}
# Démarrage du service
proxy_start() {
# malformed ssh address
[[ "$sshserver" != *@* || -z "$user" || -z "$host" ]] && proxy_usage
#-e 'ssh -i '$ssh_identity
echo "Starting $name..."
sshuttleArgs=(
-r "$user@$host:$port" 0/0
--dns -D
--pidfile "$pidFile"
)
sudo -u "$whoAmI" sshuttle "${sshuttleArgs[@]}" && echo "OK"
}
proxy_status() {
PID=$(proxy_pid) || true
if [ -n "$PID" ]; then
echo "$name is running (pid $PID)."
exit 0
else
echo "$name is NOT running."
if [ -e "$PIDFILE" ]; then
exit 1
else
exit 3
fi
fi
}
# TODO: fake it as I'm not aware a config file to 'reload'
proxy_reload() {
proxy_stop
proxy_start
}
proxy_usage() {
echo "Usage: $(basename $0) start|restart[ username@server[:port]]|stop|status "
exit 1
}
proxy_pid() {
# if there is actually an apache2 process whose pid is in pidFile,
# print it and return 0.
if [ -e "$pidFile" ]; then
if pidof "python" | tr ' ' '\n' | grep -w $(cat $pidFile); then
return 0
fi
fi
return 1
}
# commandes
case $1 in
start)
proxy_start
;;
stop)
proxy_stop
;;
restart)
proxy_stop
proxy_start
;;
reload | force-reload | graceful)
proxy_reload
;;
status)
proxy_status
;;
*)
proxy_usage
esac