-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoftether-client.sh
executable file
·237 lines (193 loc) · 6.1 KB
/
softether-client.sh
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
229
230
231
232
233
234
235
236
#!/bin/bash
cd `dirname $0`
. ./common.sh
usage="
Prepares softether vpn server
Usage:
$(basename $0) <server-ip> --username <username> --password <plaintext password> [--ip <static_ip>] [--vpn-hub <hub name>]
[--connection_name <name>] [--nicname <vpn adapter name>] [--port <Server's IP port>]
where
<sever-ip> - Server address
--port - Port number. Defaults to 992
--vpn-hub - Name of the virtual hub to connect to, defaults to 'VPN'
--username - User name (defaults to hostname)
--ip - Static ip to use (good for dhcp servers)
--connection_name - Connection name. Defaults to user name
--password - User password. In plaintext, so make sure this command is not placed in the history
--nicname - Name of the network adapter. Defaults to vpn0.
--debug - Flag that sets debugging mode.
--service - Add as a system service under name 'softether-client-{connection-name}'
--log - Path to the log file that will log all meaningful commands
Example:
./$(basename $0) 172.104.148.166 --username adam --password 12345
"
if [ "$1" == "" ]; then
echo "$usage" >&2
exit 1
fi
if [ "$1" == "--help" ]; then
echo "$usage" >&2
exit 1
fi
server_address=$1
shift
nicname=vpn
password=""
username="$(hostname)"
ip="dhcp"
vpn_hub=VPN
port=992
connection_name=""
while [[ $# > 0 ]]
do
key="$1"
shift
case $key in
--debug)
debug=1
;;
--log)
log=$1
shift
;;
--help)
echo "$usage"
exit 0
;;
--password)
password="$1"
shift
;;
--connection_name)
connection_name="$1"
shift
;;
--username)
username="$1"
shift
;;
--vpn-hub)
vpn_hub="$1"
shift
;;
--ip)
ip="$1"
shift
;;
--nicname)
nicname="$1"
shift
;;
--port)
port="$1"
shift
;;
--)
break
;;
-*)
echo "Error: Unknown option: $1" >&2
echo "$usage" >&2
exit 1
;;
esac
done
if [ -n "$debug" ]; then
opts="$opts --debug"
if [ -z "$log" ]; then
log=/dev/stdout
else
opts="$opts --log $log"
fi
fi
if [ -z "$username" ]; then
errcho "You must specify user name!"
exit 1
fi
if [ -z "$connection_name" ]; then
connection_name=$username
fi
if [ ! "${ip}" == "dhcp" ]; then
pattern='^([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)$'
if [[ ! "$ip" =~ $pattern ]]; then
errcho "Wrong format of the ip!"
exit 1
fi
fi
#add_ppa paskal-07/softethervpn
#install_apt_package softether-vpnclient
get_git_repo https://github.com/SoftEtherVPN/SoftEtherVPN /usr/local/lib
install_apt_packages cmake gcc g++ make pkgconf libncurses5-dev libssl-dev libsodium-dev libreadline-dev zlib1g-dev
cd /usr/local/lib/SoftEtherVPN
logexec ./configure
logexec make -C build -j 4
logexec sudo make -C build install
logexec sudo vpnclient start
if ! vpncmd localhost /CLIENT /CMD AccountList | grep -q "VPN Connection Setting Name |${connection_name}"; then
# Create the connection
if ! vpncmd localhost /CLIENT /CMD NicList | grep -q "Virtual Network Adapter Name|${nicname}"; then
logexec vpncmd localhost /CLIENT /CMD NicCreate ${nicname}
fi
logexec vpncmd localhost /CLIENT /CMD AccountCreate ${connection_name} /SERVER:"${server_address}:${port}" /HUB:${vpn_hub} /USERNAME:${username} /NICNAME:${nicname}
logexec vpncmd localhost /CLIENT /CMD AccountPasswordSet ${connection_name} /PASSWORD:${password} /TYPE:standard
fi
#logexec sudo vpncmd localhost /CLIENT /CMD accountconnect ${connection_name}
textfile /etc/systemd/system/softether_${connection_name}_client.service "[Unit]
Description=SoftEther ${connection_name} Client
After=softether_client.service
Requires=softether_client.service
[Service]
Type=oneshot
ExecStart=/bin/bash /usr/local/lib/softether/start_${connection_name}_vpn.sh
ExecStartPre= /bin/sh -c 'until ping -c1 ${server_address}; do sleep 1; done;'
ExecStop=/usr/local/bin/vpncmd localhost /CLIENT /CMD accountdisconnect ${connection_name}
ExecStop=/bin/bash -c \"ifconfig vpn_${nicname} down\"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target" root
textfile /etc/systemd/system/softether_client.service "[Unit]
Description=SoftEther Client service
After=network.target auditd.service
[Service]
Type=forking
ExecStart=/usr/local/bin/vpnclient start
ExecStop=/usr/local/bin/vpnclient stop
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target" root
#install_file files/softether_svc /etc/systemd/system/softether_${connection_name}_client.service root
logmkdir /usr/local/lib/softether root
if [ "${ip}" == "dhcp" ]; then
textfile /usr/local/lib/softether/start_${connection_name}_vpn.sh "#!/bin/sh
sudo /usr/local/bin/vpncmd localhost /CLIENT /CMD accountconnect ${connection_name}
sudo dhclient vpn_${nicname}" root
else
textfile /usr/local/lib/softether/start_${connection_name}_vpn.sh "#!/bin/sh
sudo /usr/local/bin/vpncmd localhost /CLIENT /CMD accountconnect ${connection_name}
sudo ifconfig vpn_${nicname} ${ip}
if service --status-all | grep -Fq 'isc-dhcp-server'; then
sudo systemctl restart isc-dhcp-server.service
fi" root
fi
logexec sudo systemctl daemon-reload
logexec sudo systemctl enable softether_client.service
logexec sudo systemctl enable softether_${connection_name}_client.service
logexec sudo systemctl stop softether_client.service
logexec sudo systemctl start softether_client.service
logexec sudo systemctl stop softether_${connection_name}_client.service
logexec sudo systemctl start softether_${connection_name}_client.service
#install_apt_package curl
#last_version=$(get_latest_github_release_name SoftEtherVPN/SoftEtherVPN)
#link="https://github.com/SoftEtherVPN/SoftEtherVPN/archive/${last_version}.tar.gz"
#get_git_repo https://github.com/SoftEtherVPN/SoftEtherVPN.git /opt SoftEther
#install_apt_packages curl cmake build-essential libssl-dev zlib1g-dev libreadline-dev
#if ! which vpncmd>/dev/null; then
# logmkdir "/opt/SoftEther" adam
# logmkdir "/opt/SoftEther/build" adam
# pushd "/opt/SoftEther/build"
# logexec cmake ..
# logexec make -j
# logexec sudo make install
#fi
exit 1