From 5737317dae589b48f309f7dec185743585364a7d Mon Sep 17 00:00:00 2001 From: David Myers Date: Wed, 30 Oct 2019 03:38:39 -0400 Subject: [PATCH] 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 --- config.cfg | 3 +++ roles/common/templates/rules.v4.j2 | 7 ++++++- roles/common/templates/rules.v6.j2 | 7 ++++++- roles/wireguard/defaults/main.yml | 2 ++ roles/wireguard/templates/server.conf.j2 | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/config.cfg b/config.cfg index 5cb3eaa..34d15f5 100644 --- a/config.cfg +++ b/config.cfg @@ -32,6 +32,9 @@ strongswan_network: 10.19.48.0/24 strongswan_network_ipv6: 'fd9d:bc11:4020::/48' # Deploy WireGuard +# WireGuard will listen on 51820/UDP. You might need to change to another port +# if your network blocks this one. Be aware that 53/UDP (DNS) is blocked on some +# mobile data networks. wireguard_enabled: true wireguard_port: 51820 # If you're behind NAT or a firewall and you want to receive incoming connections long after network traffic has gone silent. diff --git a/roles/common/templates/rules.v4.j2 b/roles/common/templates/rules.v4.j2 index 4d8f60b..9708435 100644 --- a/roles/common/templates/rules.v4.j2 +++ b/roles/common/templates/rules.v4.j2 @@ -1,5 +1,5 @@ {% set subnets = ([strongswan_network] if ipsec_enabled else []) + ([wireguard_network_ipv4] if wireguard_enabled else []) %} -{% set ports = (['500', '4500'] if ipsec_enabled else []) + ([wireguard_port] if wireguard_enabled else []) %} +{% 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 []) %} #### The mangle table # This table allows us to modify packet headers @@ -29,6 +29,11 @@ COMMIT :PREROUTING ACCEPT [0:0] :POSTROUTING ACCEPT [0:0] +{% if wireguard_enabled and wireguard_port|int == wireguard_port_avoid|int %} +# Handle the special case of allowing access to WireGuard over an already used +# port like 53 +-A PREROUTING --in-interface {{ ansible_default_ipv4['interface'] }} -p udp --dport {{ wireguard_port_avoid }} -j REDIRECT --to-port {{ wireguard_port_actual }} +{% endif %} # Allow traffic from the VPN network to the outside world, and replies -A POSTROUTING -s {{ subnets|join(',') }} -m policy --pol none --dir out -j MASQUERADE diff --git a/roles/common/templates/rules.v6.j2 b/roles/common/templates/rules.v6.j2 index d586a42..5969a95 100644 --- a/roles/common/templates/rules.v6.j2 +++ b/roles/common/templates/rules.v6.j2 @@ -1,5 +1,5 @@ {% set subnets = ([strongswan_network_ipv6] if ipsec_enabled else []) + ([wireguard_network_ipv6] if wireguard_enabled else []) %} -{% set ports = (['500', '4500'] if ipsec_enabled else []) + ([wireguard_port] if wireguard_enabled else []) %} +{% 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 []) %} #### The mangle table # This table allows us to modify packet headers @@ -28,6 +28,11 @@ COMMIT :PREROUTING ACCEPT [0:0] :POSTROUTING ACCEPT [0:0] +{% if wireguard_enabled and wireguard_port|int == wireguard_port_avoid|int %} +# Handle the special case of allowing access to WireGuard over an already used +# port like 53 +-A PREROUTING --in-interface {{ ansible_default_ipv6['interface'] }} -p udp --dport {{ wireguard_port_avoid }} -j REDIRECT --to-port {{ wireguard_port_actual }} +{% endif %} # Allow traffic from the VPN network to the outside world, and replies -A POSTROUTING -s {{ subnets|join(',') }} -m policy --pol none --dir out -j MASQUERADE diff --git a/roles/wireguard/defaults/main.yml b/roles/wireguard/defaults/main.yml index 190ab61..030511f 100644 --- a/roles/wireguard/defaults/main.yml +++ b/roles/wireguard/defaults/main.yml @@ -3,6 +3,8 @@ wireguard_PersistentKeepalive: 0 wireguard_config_path: "configs/{{ IP_subject_alt_name }}/wireguard/" wireguard_pki_path: "{{ wireguard_config_path }}/.pki/" wireguard_interface: wg0 +wireguard_port_avoid: 53 +wireguard_port_actual: 51820 keys_clean_all: false wireguard_dns_servers: >- {% if algo_dns_adblocking|default(false)|bool or dns_encryption|default(false)|bool %} diff --git a/roles/wireguard/templates/server.conf.j2 b/roles/wireguard/templates/server.conf.j2 index b7a8580..0104f5f 100644 --- a/roles/wireguard/templates/server.conf.j2 +++ b/roles/wireguard/templates/server.conf.j2 @@ -1,6 +1,6 @@ [Interface] Address = {{ wireguard_server_ip }} -ListenPort = {{ wireguard_port }} +ListenPort = {{ wireguard_port_actual if wireguard_port|int == wireguard_port_avoid|int else wireguard_port }} PrivateKey = {{ lookup('file', wireguard_pki_path + '/private/' + IP_subject_alt_name) }} SaveConfig = false