forked from altairwei/wireguard-server-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwgserver-lib.sh
More file actions
385 lines (358 loc) · 10.3 KB
/
wgserver-lib.sh
File metadata and controls
385 lines (358 loc) · 10.3 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash
readonly E_NO_WIREGUARD=1
readonly E_NO_RUNNING_INT=2
readonly E_NO_CLIENT_DIR=3
readonly E_NO_VALID_CONF=4
readonly E_NO_INTERFACE=5
readonly E_NO_CLIENT_CONF=6
readonly E_NO_MATCH_INT=7
readonly E_NO_SUPPORT=8
readonly E_NO_PERMISSION=9
readonly BASE64_REG='(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?'
# Define colors
readonly COLOR_NC='\e[0m' # No Color
readonly COLOR_GREEN='\e[0;32m'
readonly COLOR_BOLD_GREEN='\e[1;32m'
readonly COLOR_YELLOW='\e[0;33m'
readonly COLOR_BOLD_YELLOW='\e[1;33m'
#######################################
# Throw error messages without exiting
# Globals:
# None
# Arguments:
# msg=$@ - The message to throw
# Returns:
# None
#######################################
err() {
local msg=$@
echo "[x] Error: $msg" >&2
}
#######################################
# Ask for sudo authority
# Globals:
# BASH - has a default value "/bin/bash"
# SELF - must be defined by SELF="$(readlink -f "${BASH_SOURCE[0]}")"
# ARGS - must be defined by ARGS=( "$@" )
# E_NO_PERMISSION
# Arguments:
# None
# Returns:
# E_NO_PERMISSION
#######################################
request_administrator_authority() {
if [[ "$EUID" -ne 0 ]]; then
if command -v sudo >/dev/null 2>&1 ; then
exec sudo -p "[sudo] wg-server must be run as root: " -- "${BASH-"/bin/bash"}" -- "$SELF" "${ARGS[@]}"
else
die "Sorry, you need to run this script as root." "${E_NO_PERMISSION}"
fi
fi
}
#######################################
# Check if wireguard is installed
# Globals:
# E_NO_WIREGUARD
# Arguments:
# None
# Returns:
# E_NO_WIREGUARD
#######################################
assert_wireguard_existence() {
# assert wg command existence
if ! command -v wg >/dev/null 2>&1 ; then
die "Command 'wg' is not avaliable, please intall wireguard first." "${E_NO_WIREGUARD}"
fi
}
#######################################
# Check if wireguard is running
# Globals:
# E_NO_RUNNING_INT
# Arguments:
# None
# Returns:
# E_NO_RUNNING_INT
#######################################
assert_wireguard_running() {
assert_wireguard_existence
# assert whether any interfaces is running or not.
if [[ -z "$(wg show interfaces)" ]]; then
die "Cannot detect any running wireguard interfaces." "${E_NO_RUNNING_INT}"
fi
}
#######################################
# Check if /etc/wireguard/client is exist
# Globals:
# E_NO_CLIENT_DIR
# Arguments:
# None
# Returns:
# E_NO_CLIENT_DIR
#######################################
assert_client_config_dir() {
assert_wireguard_existence
# Enter client configuration files folder
if ! [[ -d "/etc/wireguard/client" ]]; then
die "Client folder does not exist at /etc/wireguard/client , please \
make sure wireguard server is installed by 'wg-server --install-wireguard' ." "${E_NO_CLIENT_DIR}"
fi
}
#######################################
# Check if interface config file exist
# Globals:
# E_NO_INTERFACE
# Arguments:
# None
# Returns:
# E_NO_INTERFACE
#######################################
assert_interface_valid() {
if ! [[ -f "/etc/wireguard/$1.conf" ]]; then
die "$1.conf does not exist at /etc/wireguard." "${E_NO_INTERFACE}"
fi
}
assert_qrencode_existance() {
if ! command -v qrencode >/dev/null 2>&1 ; then
die "qrencode can not be found." 1
fi
}
#######################################
# Get section contents of ini file
# Globals:
# None
# Arguments:
# inifile="$1" - file name
# section="$2" - section name
# Returns:
# stdout - section contents
#######################################
listIniSectionContents()
{
local inifile="$1" section="$2"
values=$(sed -n '/\['$section'\]/,/^$/p' $inifile | grep -Ev '\[|\]|^$')
echo ${values}
}
#######################################
# Get value from given section and key
# Globals:
# None
# Arguments:
# config_file="$1" - file name
# section="$2" - section name
# Returns:
# stdout - value
#######################################
parse_config_file() {
local config_file="$1" target_section="$2" target_key="$3"
local inside_section=0 line key value stripped
# check file
[[ -e ${config_file} ]] || die "\`${config_file}' does not exist" 1
[[ ${config_file} =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
config_file="$(readlink -f "${config_file}")"
# parsing
shopt -s nocasematch
while read -r line || [[ -n ${line} ]]; do
# remove comments
stripped="${line%%\#*}"
# extract key and remove witespcae
key="${stripped%%=*}"; key="${key#"${key%%[![:space:]]*}"}"; key="${key%"${key##*[![:space:]]}"}"
# extract value and remove witespcae
value="${stripped#*=}"; value="${value#"${value%%[![:space:]]*}"}"; value="${value%"${value##*[![:space:]]}"}"
# check target section interval
[[ $key == "["* ]] && inside_section=0
[[ $key == "[${target_section}]" ]] && inside_section=1
if [[ $inside_section -eq 1 ]]; then
if [[ "${key}" = "${target_key}" ]]; then
echo "${value}"
return 0
fi
fi
done < "${config_file}"
shopt -u nocasematch
}
#######################################
# Get interface private key
# Globals:
# None
# Arguments:
# conf_file=$1 - file name
# Returns:
# stdout - base64 format private key
#######################################
get_int_pri_key() {
# Get interface private key from config file.
local conf_file=$1
echo "$(parse_config_file "${conf_file}" "Interface" "PrivateKey")"
}
#######################################
# Get interface name and public-key pair
# Globals:
# None
# Arguments:
# $@ - file name globs
# Returns:
# stdout - tab-delimited pair on each row
#######################################
get_name_pubkey_pair() {
local results
for file in $@; do
local name=$(basename -s ".conf" ${file}) pubkey=$(get_int_pri_key "${file}" | wg pubkey)
#results="${results:+"$(echo "${results}\n")"}${name}\t${pubkey}"
results="$(printf '%s\n%s\t%s' "${results:-""}" "${name}" "${pubkey}" )"
done
echo "${results}"
}
#######################################
# Convert unix timestamp to human readable
# Globals:
# None
# Arguments:
# unix_time=$1 - unix timestamp
# unix_time=stdin - unix timestamp
# Returns:
# stdout - human-readable date
#######################################
convert_unix_time_readable() {
# How to set time zone: export TZ='Asia/Shanghai'
local unix_time=${1:-"$(cat)"}
date -d "@${unix_time}" +'%Y-%m-%d %H:%M:%S%z'
}
#######################################
# Convert bytes to human readable
# Globals:
# None
# Arguments:
# size=$1 - bytes
# size=stdin - bytes
# Returns:
# stdout - human-readable size
#######################################
convert_bytes_human_readable() {
local size=${1:-"$(cat)"} factor="KMGTEPZY" scale="scale=2"
if (( ${size} < 1024 )); then
echo "${size} bytes"
return 0
else
size=$(echo "${scale}; ${size}/1024" | bc)
fi
while (( $(echo "${size} >= 1024" | bc -l) && ${#factor} > 1 )); do
size=$(echo "${scale}; ${size}/1024" | bc)
factor=${factor:1}
done
echo "${size} ${factor:0:1}iB"
}
#######################################
# Create server config file with necessary information
# Globals:
# None
# Arguments:
# interface=$1 - interface name
# eth=$2 - ethernet name
# port=$3 - udp listen port
# int_prikey=$4 - interface private key
# int_addr=$5 - interface address
# int_dns=$6 - interface DNS
# int_mtu=$7 - interface MTU
# Returns:
# None
#######################################
create_server_config_file() {
local func_name=$0
if [[ $# != 6 ]]; then
die "${func_name}: args are not enough." 1
fi
local interface=$1 eth=$2 port=$3
local int_prikey=$4 int_addr=$5 int_dns=$6 int_mtu=$7
cat > /etc/wireguard/${interface}.conf <<-EOF
[Interface]
PrivateKey = ${int_prikey}
Address = ${int_addr}
PostUp = iptables -A FORWARD -i ${interface} -j ACCEPT; iptables -A FORWARD -o ${interface} -j ACCEPT; iptables -t nat -A POSTROUTING -o ${eth} -j MASQUERADE
PostDown = iptables -D FORWARD -i ${interface} -j ACCEPT; iptables -D FORWARD -o ${interface} -j ACCEPT; iptables -t nat -D POSTROUTING -o ${eth} -j MASQUERADE
ListenPort = $port
DNS = ${int_dns}
MTU = ${int_mtu}
EOF
}
#######################################
# Create client config file with necessary information
# Globals:
# None
# Arguments:
# conf_file=$1 - config file path
# int_prikey=$2 - the private key of interface
# int_addr=$3 - interface address
# int_dns=$4 - interface DNS
# int_mtu=$5 - interface MTU
# peer_pubkey=$6 - server public key
# peer_endpoint=$7 - server address on internet
# peer_allowedips=$8 - server allowndIps
# peer_alive=$9 - server PersistentKeepalive
# Returns:
# None
#######################################
create_client_config_file() {
local func_name=$0
if [[ $# != 9 ]]; then
die "${func_name}: no enough args." 1
fi
local conf_file=$1 int_prikey=$2 int_addr=$3 int_dns=$4 int_mtu=$5
local peer_pubkey=$6 peer_endpoint=$7 peer_allowedips=$8 peer_alive=$9
local int_pubkey="$(echo "${int_prikey}" | wg pubkey)"
# write to file
cat > "${conf_file}" <<-EOF
[Interface]
PrivateKey = ${int_prikey}
Address = ${int_addr}
DNS = ${int_dns}
MTU = ${int_mtu}
[Peer]
PublicKey = ${peer_pubkey}
Endpoint = ${peer_endpoint}
AllowedIPs = ${peer_allowedips}
PersistentKeepalive = ${peer_alive}
EOF
}
#######################################
# Create client config file with templete
# Globals:
# None
# Arguments:
# conf_file=$1 - client config file base name
# int_prikey=$2 - the private key of interface
# int_addr=$3 - interface address
# peer_pubkey=$4 - server public key
# peer_endpoint=$5 - server address on internet
# Returns:
# None
#######################################
create_default_client() {
local conf_file=$1 int_prikey=$2 int_addr=$3
local peer_pubkey=$4 peer_endpoint=$5
create_client_config_file "${conf_file}" \
"${int_prikey}" "${int_addr}" "8.8.8.8" "1420" \
"${peer_pubkey}" "${peer_endpoint}" "0.0.0.0/0, ::0/0" "25"
}
#######################################
# Get a unused address from peers
# Globals:
# None
# Arguments:
# int_addr=$1 - interface address
# peer_addr_list=$2 - a list of peers' address
# Returns:
# stdout - unused ip address without CIDR
#######################################
get_unused_ip() {
local int_addr=$1 peer_addr_list=$2
local int_addr_part="$(echo ${int_addr} | cut -d "." -f "1 2 3")"
for i in {2..254}; do
if [[ "${peer_addr_list}" == *"${int_addr_part}.$i"* ]]; then
continue
else
echo "${int_addr_part}.$i"
return 0
fi
done
return 1
}