-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-key.sh
executable file
·77 lines (62 loc) · 1.91 KB
/
add-key.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
#!/bin/bash
. .env
. common.sh
umask 077
MAX_OFFSET=$(($(maskSize ${MASK})-3))
if [ "$#" -ne 2 ]
then
>&2 echo usage:
>&2 echo $0 client_id_fron_0_to_${MAX_OFFSET} client_name \>\> wg${WGID}.conf
>&2 echo
>&2 echo example:
>&2 echo $0 1 user01 \>\> wg${WGID}.conf
>&2 echo $0 2 user02 \>\> wg${WGID}.conf
exit 1
fi
if ! [[ $1 =~ ^[0-9]+$ ]] ; then echo "error: first argument \"$1\" id must be a number between 1 and ${MAX_OFFSET} " >&2; exit 1; fi;
if [[ $2 =~ ^[0-9]+$ ]] ; then echo "error: second argument \"$2\" username can not be a number" >&2; exit 1; fi;
if [[ ! $1 -gt 0 ]]; then echo first param must must be a number 1-${MAX_OFFSET}; exit; fi
if [[ ! $1 -le $MAX_OFFSET ]]; then echo first param must must be a number 1-${MAX_OFFSET}; exit; fi
CLIENT_IP=$(trIP $IP_FIRST $1)
REGEXP=$(echo ${CLIENT_IP}/${MASK} | sed s/\\./\\\\\./g)
C1=$(grep -c ${REGEXP} wg${WGID}.conf)
if [ $C1 -ne 0 ]
then
USERNAME=$(grep -B4 ${REGEXP} wg${WGID}.conf | grep ' user:' | cut -d: -f2)
>&2 echo IP ${CLIENT_IP} already taken by user ${USERNAME}
exit 1
fi
C2="$(grep -c user:$2$ wg${WGID}.conf)"
if [ $C2 -ne 0 ]
then
IP="$(grep -A4 user:$2$ wg${WGID}.conf | grep 'AllowedIps ' | cut -d= -f2)"
>&2 echo user $2 already exists using ${IP}
exit 1
fi
k=
if [ -f allKeys.txt ]
then
K=$(grep ^${2}: allKeys.txt | cut -d: -f2)
fi
if [ -z "$K" ]
then
K=$(wg genkey);
echo ${2}:$K >> allKeys.txt
fi
P=$(echo $K | wg pubkey)
cat << EOF
[Peer]
# user:$2
#PrivateKey = $K
PublicKey = $P
AllowedIps = ${CLIENT_IP}/32
EOF
if [ -t 1 ] ; then
>&2 printf '\n\e[1;31m%-6s\e[m\n' "Add this key in wg${WGID}.conf by hand or with:"
>&2 echo "$0" "$@" \>\> wg${WGID}.conf
else
>&2 echo -e "\e[31m$2\e[0m Key generated With IP: \e[32m${CLIENT_IP}\e[0m"
fi
>&2 printf '\n\e[1;31m%-6s\e[m\n' "Now restart wireguard service to take effect"
# >&2 echo "add this key in wg${WGID}.conf then"
>&2 echo systemctl reload wg-quick@wg${WGID}.service