forked from drew2a/wireguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwg-genconf-user.sh
197 lines (175 loc) · 4.92 KB
/
wg-genconf-user.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
#!/usr/bin/env bash
# usage: wg-genconf-user.sh [-ad|-h] [Wireguard config file] [Name of client] [Client IP last octet]
echo "Wireguard User Management"
echo "========================="
# Variable declaration
curr_args=
valid_args=
server_config=
client_name=
client_ip_octet=
action=
curr_args=$(getopt -n wg-genconf-user -o c:a:d:o:h --long config:,add:,delete:,octet:,help -- "$@")
echo "Working from the $(pwd) directory."
echo "Running with the following parameters:${curr_args}"
# Syntax of the script
usage()
{
echo -e "Syntax: wg-genconf-user.sh [<-ad>][Wireguard config file] [Name of client] [<Client IP last octet>]."
echo "Flags"
echo -e "\t-a or --add: Add a client to a configuration file. Requires the Client IP last octet."
echo -e "\t-d or --delete: Delete a client from the configuration file."
echo "Parameters:"
echo -e "\tWireguard config file: Location of the configuration file to edit."
echo -e "\tName of the client: Name of the client configuration to include in Wireguard."
echo -e "\tIP last octet: Unique IP octet to append to a particular client."
exit 2 #No error
}
# Add user function
add_user()
{
#
# Checking for octet
#
if [ -z ${client_ip_octet} ]
then
echo "Client IP octet was not provided. Exiting."
exit #no octed provided
fi
#
# assign parameters
#
server_private_key=$(cat /etc/wireguard/wg0.conf | grep "PrivateKey = " | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g" | sed "s/PrivateKey = //")
server_public_key=$(echo "${server_private_key}" | wg pubkey)
#
# obtaining current IP address to link vpn server
#
server_ip=$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}')
#
# add configuration for a particular user on Wireguard
#
client_private_key=$(wg genkey)
client_public_key=$(echo "${client_private_key}" | wg pubkey)
client_ip=10.0.0.${client_ip_octet}/32
client_config=${client_name}.conf
#
# writing configuration to file
#
echo -e "Generating client configuration file on:"
echo -e "\t$(pwd)/${client_config}"
cat > "${client_config}" <<EOL
[Interface]
PrivateKey = ${client_private_key}
ListenPort = 1194
Address = ${client_ip}
DNS = 10.0.0.1
[Peer]
PublicKey = ${server_public_key}
AllowedIPs = 0.0.0.0/0
Endpoint = ${server_ip}:1194
PersistentKeepalive = 21
EOL
#
# add client configuration to Wireguard
#
echo -e "Updating wireguard configuration file on:"
echo -e "\t${server_config}"
cat >> "${server_config}" <<EOL
# ${client_name}_START
[Peer]
PublicKey = ${client_public_key}
AllowedIPs = ${client_ip}
# ${client_name}_END
EOL
}
# Delete user function
del_user()
{
echo "Removing client ${client_name}"
line_start = $(grep -nr "# ${client_name}_START" ${server_config} | cut -f1 -d:)
line_end = $(grep -nr "# ${client_name}_END" ${server_config} | cut -f1 -d:)
sed '${line_start},${line_end}d' ${server_config}
}
# Main program
valid_args=$?
if [ "$valid_args" != "0" ]; then
usage
fi
eval set -- "$curr_args"
while :
do
case "$1" in
-c | --config)
server_config="$2"
if ! test -f "${server_config}"
then
echo "Wireguard configuration file could not be found or was not provided. Exiting."
exit #file not found
fi
shift 2
;;
-a | --add)
echo "Adding user."
if [ -z ${action} ]; then
action="add"
client_name="$2"
shift 2
else
echo "Multiple actions invoked. Aborting."
usage
fi
;;
-d | --delete)
echo "Deleting user."
if [ -z ${action} ]; then
action="delete"
client_name="$2"
shift 2
else
echo "Multiple actions invoked. Aborting."
usage
fi
;;
-o | --octet)
client_ip_octet=$2
shift 2
;;
-h | --help)
usage
;;
--)
shift
break
;;
*)
echo "Unknown flag $1. Aborting."
usage
;;
esac
done
# Checking for client name
if [ -z ${client_name} ]
then
echo "Client name was not provided. Aborting."
usage
fi
if [ -z ${action} ]
then
echo "No action was requested. Exiting."
usage
fi
case "$action" in
"add")
add_user
;;
"delete")
del_user
;;
*)
echo "You should not be here. Exiting."
usage
;;
esac
echo
echo -e "Make sure to restart Wireguard for changes to take effect. For example:"
echo -e "\tsystemctl restart wg-quick@wg0"