-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintKey.sh
executable file
·67 lines (57 loc) · 1.28 KB
/
printKey.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
#!/bin/bash
. .env
. common.sh
# systemctl restart wg-quick@wg${WGID}.service
if [ "$#" -eq 0 ]
then
echo usage:
echo $0 username
echo example:
echo $0 user-1
echo $0 user-1 QR
# \| qrencode -t ansiutf8
exit 1
fi
DATA=$(grep -A3 \ user:${1}$ wg${WGID}.conf)
if [ -z "$DATA" ]
then
echo no key for $1
exit 1;
fi
IP=$(echo "$DATA" | grep -E -o 'AllowedIps\ =\ [0-9.]+' | cut -d\ -f3)
KEY=$(echo "$DATA" | grep -E -o 'PrivateKey\ =\ [A-Z/a-z0-9=+-]+' | cut -d\ -f3)
SRVPUB=$(cat wg${WGID}.conf | grep ^PrivateKey | cut -d\ -f3 | wg pubkey)
DNS=
if [ ! -z "${CLIENT_DNS}" ]
then
DNS="DNS = ${CLIENT_DNS}"
fi
IP_ID=$(getIPOffset ${IP_FIRST} ${IP})
cat > Tmp << EOF
[Interface]
# user:$1 IP_number:${IP_ID}
PrivateKey = ${KEY}
Address = ${IP}/${MASK}
$DNS
[Peer]
PublicKey = ${SRVPUB}
AllowedIPs = ${IP_FIRST}/${MASK}${EXTRA_ROUTE}
Endpoint = ${END_POINT}
PersistentKeepalive = ${PersistentKeepalive}
EOF
if [ "$#" -eq 1 ]
then
echo "cat > /etc/wireguard/wg${WGID}.conf << EOK"
cat Tmp
echo "EOK"
else
cat Tmp | qrencode -t ansiutf8
fi
rm Tmp
>&2 echo
# >&2 echo nano /etc/wireguard/wg${WGID}.conf\;
>&2 echo systemctl start wg-quick@wg${WGID}\;
>&2 echo systemctl enable wg-quick@wg${WGID}\;
OFFSET=$(($(maskSize ${MASK})-2))
SRV=$(trIP $IP_FIRST $OFFSET)
>&2 echo ping -c 3 ${SRV}\;