Skip to content

Commit 5737317

Browse files
davidemyersjackivanov
authored andcommitted
Allow WireGuard to listen on port 53 (#1594)
* Allow WireGuard to listen on port 53 * Use a variable for the port to avoid * Add comment to config.cfg
1 parent 88eaf30 commit 5737317

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

config.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ strongswan_network: 10.19.48.0/24
3232
strongswan_network_ipv6: 'fd9d:bc11:4020::/48'
3333

3434
# Deploy WireGuard
35+
# WireGuard will listen on 51820/UDP. You might need to change to another port
36+
# if your network blocks this one. Be aware that 53/UDP (DNS) is blocked on some
37+
# mobile data networks.
3538
wireguard_enabled: true
3639
wireguard_port: 51820
3740
# If you're behind NAT or a firewall and you want to receive incoming connections long after network traffic has gone silent.

roles/common/templates/rules.v4.j2

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% set subnets = ([strongswan_network] if ipsec_enabled else []) + ([wireguard_network_ipv4] if wireguard_enabled else []) %}
2-
{% set ports = (['500', '4500'] if ipsec_enabled else []) + ([wireguard_port] if wireguard_enabled else []) %}
2+
{% set ports = (['500', '4500'] if ipsec_enabled else []) + ([wireguard_port] if wireguard_enabled else []) + ([wireguard_port_actual] if wireguard_enabled and wireguard_port|int == wireguard_port_avoid|int else []) %}
33

44
#### The mangle table
55
# This table allows us to modify packet headers
@@ -29,6 +29,11 @@ COMMIT
2929
:PREROUTING ACCEPT [0:0]
3030
:POSTROUTING ACCEPT [0:0]
3131

32+
{% if wireguard_enabled and wireguard_port|int == wireguard_port_avoid|int %}
33+
# Handle the special case of allowing access to WireGuard over an already used
34+
# port like 53
35+
-A PREROUTING --in-interface {{ ansible_default_ipv4['interface'] }} -p udp --dport {{ wireguard_port_avoid }} -j REDIRECT --to-port {{ wireguard_port_actual }}
36+
{% endif %}
3237
# Allow traffic from the VPN network to the outside world, and replies
3338
-A POSTROUTING -s {{ subnets|join(',') }} -m policy --pol none --dir out -j MASQUERADE
3439

roles/common/templates/rules.v6.j2

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% set subnets = ([strongswan_network_ipv6] if ipsec_enabled else []) + ([wireguard_network_ipv6] if wireguard_enabled else []) %}
2-
{% set ports = (['500', '4500'] if ipsec_enabled else []) + ([wireguard_port] if wireguard_enabled else []) %}
2+
{% set ports = (['500', '4500'] if ipsec_enabled else []) + ([wireguard_port] if wireguard_enabled else []) + ([wireguard_port_actual] if wireguard_enabled and wireguard_port|int == wireguard_port_avoid|int else []) %}
33

44
#### The mangle table
55
# This table allows us to modify packet headers
@@ -28,6 +28,11 @@ COMMIT
2828
:PREROUTING ACCEPT [0:0]
2929
:POSTROUTING ACCEPT [0:0]
3030

31+
{% if wireguard_enabled and wireguard_port|int == wireguard_port_avoid|int %}
32+
# Handle the special case of allowing access to WireGuard over an already used
33+
# port like 53
34+
-A PREROUTING --in-interface {{ ansible_default_ipv6['interface'] }} -p udp --dport {{ wireguard_port_avoid }} -j REDIRECT --to-port {{ wireguard_port_actual }}
35+
{% endif %}
3136
# Allow traffic from the VPN network to the outside world, and replies
3237
-A POSTROUTING -s {{ subnets|join(',') }} -m policy --pol none --dir out -j MASQUERADE
3338

roles/wireguard/defaults/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ wireguard_PersistentKeepalive: 0
33
wireguard_config_path: "configs/{{ IP_subject_alt_name }}/wireguard/"
44
wireguard_pki_path: "{{ wireguard_config_path }}/.pki/"
55
wireguard_interface: wg0
6+
wireguard_port_avoid: 53
7+
wireguard_port_actual: 51820
68
keys_clean_all: false
79
wireguard_dns_servers: >-
810
{% if algo_dns_adblocking|default(false)|bool or dns_encryption|default(false)|bool %}

roles/wireguard/templates/server.conf.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Interface]
22
Address = {{ wireguard_server_ip }}
3-
ListenPort = {{ wireguard_port }}
3+
ListenPort = {{ wireguard_port_actual if wireguard_port|int == wireguard_port_avoid|int else wireguard_port }}
44
PrivateKey = {{ lookup('file', wireguard_pki_path + '/private/' + IP_subject_alt_name) }}
55
SaveConfig = false
66

0 commit comments

Comments
 (0)