Skip to content

Commit 0003dd1

Browse files
authored
Update wireguard-install.sh
Just added re-install feature(it deletes old) and can choose subnet.
1 parent 39caf2f commit 0003dd1

File tree

1 file changed

+117
-106
lines changed

1 file changed

+117
-106
lines changed

wireguard-install.sh

Lines changed: 117 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function installQuestions() {
128128
done
129129

130130
until [[ ${SERVER_WG_IPV4} =~ ^([0-9]{1,3}\.){3} ]]; do
131-
read -rp "Server WireGuard IPv4: " -e -i 10.66.66.1 SERVER_WG_IPV4
131+
read -rp "Server WireGuard IPv4: " -e -i 10.66.0.1 SERVER_WG_IPV4
132132
done
133133

134134
until [[ ${SERVER_WG_IPV6} =~ ^([a-f0-9]{1,4}:){3,4}: ]]; do
@@ -168,6 +168,7 @@ function installQuestions() {
168168
169169
function installWireGuard() {
170170
# Run setup questions first
171+
choose_subnet
171172
installQuestions
172173
173174
# Install WireGuard tools and module
@@ -227,31 +228,34 @@ CLIENT_DNS_1=${CLIENT_DNS_1}
227228
CLIENT_DNS_2=${CLIENT_DNS_2}
228229
ALLOWED_IPS=${ALLOWED_IPS}" >/etc/wireguard/params
229230
230-
# Add server interface
231-
echo "[Interface]
232-
Address = ${SERVER_WG_IPV4}/24,${SERVER_WG_IPV6}/64
231+
# Add server interface
232+
echo "[Interface]
233+
Address = ${SERVER_WG_IPV4}/${SUBNET_CHOICE},${SERVER_WG_IPV6}/64
233234
ListenPort = ${SERVER_PORT}
234235
PrivateKey = ${SERVER_PRIV_KEY}" >"/etc/wireguard/${SERVER_WG_NIC}.conf"
235236
236-
if pgrep firewalld; then
237-
FIREWALLD_IPV4_ADDRESS=$(echo "${SERVER_WG_IPV4}" | cut -d"." -f1-3)".0"
238-
FIREWALLD_IPV6_ADDRESS=$(echo "${SERVER_WG_IPV6}" | sed 's/:[^:]*$/:0/')
239-
echo "PostUp = firewall-cmd --add-port ${SERVER_PORT}/udp && firewall-cmd --add-rich-rule='rule family=ipv4 source address=${FIREWALLD_IPV4_ADDRESS}/24 masquerade' && firewall-cmd --add-rich-rule='rule family=ipv6 source address=${FIREWALLD_IPV6_ADDRESS}/24 masquerade'
240-
PostDown = firewall-cmd --remove-port ${SERVER_PORT}/udp && firewall-cmd --remove-rich-rule='rule family=ipv4 source address=${FIREWALLD_IPV4_ADDRESS}/24 masquerade' && firewall-cmd --remove-rich-rule='rule family=ipv6 source address=${FIREWALLD_IPV6_ADDRESS}/24 masquerade'" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
241-
else
242-
echo "PostUp = iptables -I INPUT -p udp --dport ${SERVER_PORT} -j ACCEPT
237+
BASE_IPV4=$(echo "${SERVER_WG_IPV4}" | cut -d"." -f1-3)
238+
BASE_IPV6=$(echo "${SERVER_WG_IPV6}" | sed 's/:[^:]*$/:0/')
239+
240+
if pgrep firewalld; then
241+
echo "PostUp = firewall-cmd --add-port ${SERVER_PORT}/udp && firewall-cmd --add-rich-rule='rule family=ipv4 source address=${BASE_IPV4}.0/${SUBNET_CHOICE} masquerade' && firewall-cmd --add-rich-rule='rule family=ipv6 source address=${BASE_IPV6}/64 masquerade'
242+
PostDown = firewall-cmd --remove-port ${SERVER_PORT}/udp && firewall-cmd --remove-rich-rule='rule family=ipv4 source address=${BASE_IPV4}.0/${SUBNET_CHOICE} masquerade' && firewall-cmd --remove-rich-rule='rule family=ipv6 source address=${BASE_IPV6}/64 masquerade'" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
243+
else
244+
echo "PostUp = iptables -I INPUT -p udp --dport ${SERVER_PORT} -j ACCEPT
243245
PostUp = iptables -I FORWARD -i ${SERVER_PUB_NIC} -o ${SERVER_WG_NIC} -j ACCEPT
244246
PostUp = iptables -I FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
245-
PostUp = iptables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE
247+
PostUp = iptables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -s ${BASE_IPV4}.0/${SUBNET_CHOICE} -j MASQUERADE
246248
PostUp = ip6tables -I FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
247249
PostUp = ip6tables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE
248250
PostDown = iptables -D INPUT -p udp --dport ${SERVER_PORT} -j ACCEPT
249251
PostDown = iptables -D FORWARD -i ${SERVER_PUB_NIC} -o ${SERVER_WG_NIC} -j ACCEPT
250252
PostDown = iptables -D FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
251-
PostDown = iptables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE
253+
PostDown = iptables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -s ${BASE_IPV4}.0/${SUBNET_CHOICE} -j MASQUERADE
252254
PostDown = ip6tables -D FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
253255
PostDown = ip6tables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
254-
fi
256+
fi
257+
258+
255259
256260
# Enable routing on the server
257261
echo "net.ipv4.ip_forward = 1
@@ -282,80 +286,63 @@ net.ipv6.conf.all.forwarding = 1" >/etc/sysctl.d/wg.conf
282286
}
283287
284288
function newClient() {
285-
# If SERVER_PUB_IP is IPv6, add brackets if missing
286-
if [[ ${SERVER_PUB_IP} =~ .*:.* ]]; then
287-
if [[ ${SERVER_PUB_IP} != *"["* ]] || [[ ${SERVER_PUB_IP} != *"]"* ]]; then
288-
SERVER_PUB_IP="[${SERVER_PUB_IP}]"
289-
fi
290-
fi
291-
ENDPOINT="${SERVER_PUB_IP}:${SERVER_PORT}"
292-
293-
echo ""
294-
echo "Client configuration"
295-
echo ""
296-
echo "The client name must consist of alphanumeric character(s). It may also include underscores or dashes and can't exceed 15 chars."
297-
298-
until [[ ${CLIENT_NAME} =~ ^[a-zA-Z0-9_-]+$ && ${CLIENT_EXISTS} == '0' && ${#CLIENT_NAME} -lt 16 ]]; do
299-
read -rp "Client name: " -e CLIENT_NAME
300-
CLIENT_EXISTS=$(grep -c -E "^### Client ${CLIENT_NAME}\$" "/etc/wireguard/${SERVER_WG_NIC}.conf")
301-
302-
if [[ ${CLIENT_EXISTS} != 0 ]]; then
303-
echo ""
304-
echo -e "${ORANGE}A client with the specified name was already created, please choose another name.${NC}"
305-
echo ""
306-
fi
307-
done
308-
309-
for DOT_IP in {2..254}; do
310-
DOT_EXISTS=$(grep -c "${SERVER_WG_IPV4::-1}${DOT_IP}" "/etc/wireguard/${SERVER_WG_NIC}.conf")
311-
if [[ ${DOT_EXISTS} == '0' ]]; then
312-
break
313-
fi
314-
done
315-
316-
if [[ ${DOT_EXISTS} == '1' ]]; then
317-
echo ""
318-
echo "The subnet configured supports only 253 clients."
319-
exit 1
320-
fi
321-
322-
BASE_IP=$(echo "$SERVER_WG_IPV4" | awk -F '.' '{ print $1"."$2"."$3 }')
323-
until [[ ${IPV4_EXISTS} == '0' ]]; do
324-
read -rp "Client WireGuard IPv4: ${BASE_IP}." -e -i "${DOT_IP}" DOT_IP
325-
CLIENT_WG_IPV4="${BASE_IP}.${DOT_IP}"
326-
IPV4_EXISTS=$(grep -c "$CLIENT_WG_IPV4/32" "/etc/wireguard/${SERVER_WG_NIC}.conf")
327-
328-
if [[ ${IPV4_EXISTS} != 0 ]]; then
329-
echo ""
330-
echo -e "${ORANGE}A client with the specified IPv4 was already created, please choose another IPv4.${NC}"
331-
echo ""
332-
fi
333-
done
334-
335-
BASE_IP=$(echo "$SERVER_WG_IPV6" | awk -F '::' '{ print $1 }')
336-
until [[ ${IPV6_EXISTS} == '0' ]]; do
337-
read -rp "Client WireGuard IPv6: ${BASE_IP}::" -e -i "${DOT_IP}" DOT_IP
338-
CLIENT_WG_IPV6="${BASE_IP}::${DOT_IP}"
339-
IPV6_EXISTS=$(grep -c "${CLIENT_WG_IPV6}/128" "/etc/wireguard/${SERVER_WG_NIC}.conf")
340-
341-
if [[ ${IPV6_EXISTS} != 0 ]]; then
342-
echo ""
343-
echo -e "${ORANGE}A client with the specified IPv6 was already created, please choose another IPv6.${NC}"
344-
echo ""
345-
fi
346-
done
347-
348-
# Generate key pair for the client
349-
CLIENT_PRIV_KEY=$(wg genkey)
350-
CLIENT_PUB_KEY=$(echo "${CLIENT_PRIV_KEY}" | wg pubkey)
351-
CLIENT_PRE_SHARED_KEY=$(wg genpsk)
352-
353-
HOME_DIR=$(getHomeDirForClient "${CLIENT_NAME}")
354-
355-
# Create client file and add the server as a peer
356-
echo "[Interface]
289+
# If SERVER_PUB_IP is IPv6, this section is no longer necessary since we are removing IPv6 support
290+
# SERVER_PUB_IP would be expected to be IPv4 only.
291+
ENDPOINT="${SERVER_PUB_IP}:${SERVER_PORT}"
292+
293+
echo ""
294+
echo "Client configuration"
295+
echo ""
296+
echo "The client name must consist of alphanumeric character(s). It may also include underscores or dashes and can't exceed 15 chars."
297+
298+
until [[ ${CLIENT_NAME} =~ ^[a-zA-Z0-9_-]+$ && ${CLIENT_EXISTS} == '0' && ${#CLIENT_NAME} -lt 16 ]]; do
299+
read -rp "Client name: " -e CLIENT_NAME
300+
CLIENT_EXISTS=$(grep -c -E "^### Client ${CLIENT_NAME}\$" "/etc/wireguard/${SERVER_WG_NIC}.conf")
301+
302+
if [[ ${CLIENT_EXISTS} != 0 ]]; then
303+
echo ""
304+
echo -e "${ORANGE}A client with the specified name was already created, please choose another name.${NC}"
305+
echo ""
306+
fi
307+
done
308+
309+
for DOT_IP in {2..254}; do
310+
DOT_EXISTS=$(grep -c "${SERVER_WG_IPV4::-1}${DOT_IP}" "/etc/wireguard/${SERVER_WG_NIC}.conf")
311+
if [[ ${DOT_EXISTS} == '0' ]]; then
312+
break
313+
fi
314+
done
315+
316+
if [[ ${DOT_EXISTS} == '1' ]]; then
317+
echo ""
318+
echo "The subnet configured supports only 253 clients."
319+
exit 1
320+
fi
321+
322+
BASE_IP=$(echo "$SERVER_WG_IPV4" | awk -F '.' '{ print $1"."$2"."$3 }')
323+
until [[ ${IPV4_EXISTS} == '0' ]]; do
324+
read -rp "Client WireGuard IPv4: ${BASE_IP}." -e -i "${DOT_IP}" DOT_IP
325+
CLIENT_WG_IPV4="${BASE_IP}.${DOT_IP}"
326+
IPV4_EXISTS=$(grep -c "$CLIENT_WG_IPV4/$SUBNET_CHOICE" "/etc/wireguard/${SERVER_WG_NIC}.conf")
327+
328+
if [[ ${IPV4_EXISTS} != 0 ]]; then
329+
echo ""
330+
echo -e "${ORANGE}A client with the specified IPv4 was already created, please choose another IPv4.${NC}"
331+
echo ""
332+
fi
333+
done
334+
335+
# Generate key pair for the client
336+
CLIENT_PRIV_KEY=$(wg genkey)
337+
CLIENT_PUB_KEY=$(echo "${CLIENT_PRIV_KEY}" | wg pubkey)
338+
CLIENT_PRE_SHARED_KEY=$(wg genpsk)
339+
340+
HOME_DIR=$(getHomeDirForClient "${CLIENT_NAME}")
341+
342+
# Create client file and add the server as a peer
343+
echo "[Interface]
357344
PrivateKey = ${CLIENT_PRIV_KEY}
358-
Address = ${CLIENT_WG_IPV4}/32,${CLIENT_WG_IPV6}/128
345+
Address = ${CLIENT_WG_IPV4}/${SUBNET_CHOICE}
359346
DNS = ${CLIENT_DNS_1},${CLIENT_DNS_2}
360347
361348
[Peer]
@@ -364,25 +351,26 @@ PresharedKey = ${CLIENT_PRE_SHARED_KEY}
364351
Endpoint = ${ENDPOINT}
365352
AllowedIPs = ${ALLOWED_IPS}" >"${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
366353
367-
# Add the client as a peer to the server
368-
echo -e "\n### Client ${CLIENT_NAME}
354+
# Add the client as a peer to the server
355+
echo -e "\n### Client ${CLIENT_NAME}
369356
[Peer]
370357
PublicKey = ${CLIENT_PUB_KEY}
371358
PresharedKey = ${CLIENT_PRE_SHARED_KEY}
372-
AllowedIPs = ${CLIENT_WG_IPV4}/32,${CLIENT_WG_IPV6}/128" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
359+
AllowedIPs = ${CLIENT_WG_IPV4}/${SUBNET_CHOICE}" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
373360
374-
wg syncconf "${SERVER_WG_NIC}" <(wg-quick strip "${SERVER_WG_NIC}")
361+
wg syncconf "${SERVER_WG_NIC}" <(wg-quick strip "${SERVER_WG_NIC}")
375362
376-
# Generate QR code if qrencode is installed
377-
if command -v qrencode &>/dev/null; then
378-
echo -e "${GREEN}\nHere is your client config file as a QR Code:\n${NC}"
379-
qrencode -t ansiutf8 -l L <"${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
380-
echo ""
381-
fi
363+
# Generate QR code if qrencode is installed
364+
if command -v qrencode &>/dev/null; then
365+
echo -e "${GREEN}\nHere is your client config file as a QR Code:\n${NC}"
366+
qrencode -t ansiutf8 -l L <"${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
367+
echo ""
368+
fi
382369
383-
echo -e "${GREEN}Your client config file is in ${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf${NC}"
370+
echo -e "${GREEN}Your client config file is in ${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf${NC}"
384371
}
385372
373+
386374
function listClients() {
387375
NUMBER_OF_CLIENTS=$(grep -c -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf")
388376
if [[ ${NUMBER_OF_CLIENTS} -eq 0 ]]; then
@@ -475,7 +463,6 @@ function uninstallWg() {
475463
exit 1
476464
else
477465
echo "WireGuard uninstalled successfully."
478-
exit 0
479466
fi
480467
else
481468
echo ""
@@ -517,13 +504,37 @@ function manageMenu() {
517504
esac
518505
}
519506
507+
SUBNET_CHOICE=""
508+
509+
function choose_subnet() {
510+
while true; do
511+
echo "Please choose a subnet (between /22 and /16):"
512+
513+
# Show options
514+
for i in $(seq 22 -1 16); do
515+
# Calculate the number of available IP addresses
516+
available_ips=$(( 2**(32-i) - 2 ))
517+
echo "$i: /$i (Approximately $available_ips available IPs)"
518+
done
519+
520+
# Read user's choice
521+
read -r choice
522+
523+
# Check if the choice is valid
524+
if [[ "$choice" -ge 16 && "$choice" -le 22 ]]; then
525+
SUBNET_CHOICE="$choice"
526+
echo "Your selected subnet: $SUBNET_CHOICE"
527+
break
528+
else
529+
echo "Invalid selection! Please enter a value between /22 and /16."
530+
fi
531+
done
532+
}
533+
520534
# Check for root, virt, OS...
535+
uninstallWg
521536
initialCheck
522537

523-
# Check if WireGuard is already installed and load params
524-
if [[ -e /etc/wireguard/params ]]; then
525-
source /etc/wireguard/params
526-
manageMenu
527-
else
528-
installWireGuard
529-
fi
538+
installWireGuard
539+
540+
reboot

0 commit comments

Comments
 (0)