-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudm-wireguard.sh
executable file
·112 lines (95 loc) · 5.13 KB
/
udm-wireguard.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
#!/bin/bash
######################################################################################
#
# Description:
# ------------
# Initialize Wireguard setup, start wireguard tunnel and setup firewall rules
# for VPN tunnels. As firewall rules may be reseted whenever ruleset is changed
# in GUI, this script should be executed regularly to ensure that VPN firewall
# is permanently activated.
#
######################################################################################
##############################################################################################
#
# Configuration
#
# directory with wireguard config files. All *.conf files in
# the directory will be considered as valid wireguard configs
conf_dir="/data/custom/wireguard/conf/"
# enforce integration of VPN interfaces as LAN interfaces.
# WARNING: LAN integration is considered less secure!
lan_integration=false
#
# No further changes should be necessary beyond this line.
#
######################################################################################
# set scriptname
me=$(basename $0)
# include local configuration if available
[ -e "$(dirname $0)/${me%.*}.conf" ] && source "$(dirname $0)/${me%.*}.conf"
# set zone variable for integration
[[ $lan_integration == "true" ]] && zone="LAN" || zone="WAN"
for conf_file in ${conf_dir}/*.conf; do
if [ $(basename $conf_file) != "${me%.*}.conf" ]; then
wg_if=$(basename $conf_file .conf)
wg show $wg_if || wg-quick up $conf_file
# As ruleset is reset, when changes are made via GUI it can be assumed that script
# can be stopped when wireguard interfaces are not considered in fw rule set.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# add rule for VPN interface to UBIOS_INPUT_USER_HOOK
#
rule="-A UBIOS_INPUT_USER_HOOK -i ${wg_if} -j UBIOS_${zone}_LOCAL_USER"
iptables --list-rules | grep -e "${rule}" &> /dev/null || (
iptables ${rule} &&
logger "$me: added IPv4 rule: ${rule}" ||
logger "$me: failed to add IPv4 ${rule}"
) && logger "$me: IPv4 rule ${rule} already exists. Nothing to do."
ip6tables --list-rules | grep -e "${rule}" &> /dev/null || (
ip6tables ${rule} &&
logger "$me: added IPv6 rule: ${rule}" ||
logger "$me: failed to add IPv6 ${rule}"
) && logger "$me: IPv6 rule ${rule} already exists. Nothing to do."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# add rule for VPN interface to UBIOS_FORWARD_IN_USER
#
rule="-A UBIOS_FORWARD_IN_USER -i ${wg_if} -j UBIOS_${zone}_IN_USER"
iptables --list-rules | grep -e "${rule}" &> /dev/null || (
iptables ${rule} &&
logger "$me: added IPv4 rule: ${rule}" ||
logger "$me: failed to add IPv4 ${rule}"
) && logger "$me: IPv4 rule ${rule} already exists. Nothing to do."
ip6tables --list-rules | grep -e "${rule}" &> /dev/null || (
ip6tables ${rule} &&
logger "$me: added IPv6 rule: ${rule}" ||
logger "$me: failed to add IPv6 ${rule}"
) && logger "$me: IPv6 rule ${rule} already exists. Nothing to do."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# add rule for VPN interface to UBIOS_FORWARD_OUT_USER
#
rule="-A UBIOS_FORWARD_OUT_USER -o ${wg_if} -j UBIOS_${zone}_OUT_USER"
iptables --list-rules | grep -e "${rule}" &> /dev/null || (
iptables ${rule} &&
logger "$me: added IPv4 rule: ${rule}" ||
logger "$me: failed to add IPv4 ${rule}"
) && logger "$me: IPv4 rule ${rule} already exists. Nothing to do."
ip6tables --list-rules | grep -e "${rule}" &> /dev/null || (
ip6tables ${rule} &&
logger "$me: added IPv6 rule: ${rule}" ||
logger "$me: failed to add IPv6 ${rule}"
) && logger "$me: IPv6 rule ${rule} already exists. Nothing to do."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# add allow related/established to UBIOS_${zone}_IN_USER if necessary
#
rule="-A UBIOS_${zone}_IN_USER -m conntrack --ctstate RELATED,ESTABLISHED.*-j RETURN"
iptables --list-rules | grep -e "$rule" &> /dev/null || (
iptables -I UBIOS_${zone}_IN_USER 1 -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN &&
logger "$me: added IPv4 rule: ${rule}" ||
logger "$me: failed to add IPv4 ${rule}"
) && logger "$me: IPv4 rule ${rule} already exists. Nothing to do."
ip6tables --list-rules | grep -e "$rule" &> /dev/null || (
ip6tables -I UBIOS_${zone}_IN_USER 1 -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN &&
logger "$me: added IPv6 rule: ${rule}" ||
logger "$me: failed to add IPv6 ${rule}"
) && logger "$me: IPv6 rule ${rule} already exists. Nothing to do."
fi
done