Hello.
My goal is to access my local network from the internet through the Wireguard VPN.
To install the Wireguard server in OpenWRT, I followed the following guide:
And to test the client, I installed Wireguard on an Android phone. I obtained the tunnel configuration from the Luci QR code (server).
To configure the server in OpenWRT, I followed the guide. To automate it, I created a script (due to a lot of trial and error) with some additions (marked with MY_ADD).
I've been watching videos on the subject, and many of them set options like keepalive, route_allowed_ips, while others use port forwarding. It doesn't appear in the guide.
And to test the client, I installed WIreguard on an Android phone. I got the tunnel configuration from the Luci QR code.
The problem is that I can't perform the handsake. I don't know why. I don't know if it's a problem with the keys, the server configuration, the client, the firewall, etc.
The additional problem is that I don't know how to debug or view the logs. I don't see anything on the server, and the client only tells me that the handshake cannot be performed and that it will retry in 5 seconds.
What can I do?
My script:
#!/bin/sh
VPN_IF="vpn"
VPN_PEER="android"
VPN_PORT="51820"
VPN_ADDR="192.168.9.1/24"
VPN_ADDR6="fd00:9::1/64"
rm -rf wgserver.key wgserver.pub wgclient.key wgclient.pub wgclient.psk 2> /dev/null
umask go=
wg genkey | tee wgserver.key | wg pubkey > wgserver.pub
wg genkey | tee wgclient.key | wg pubkey > wgclient.pub
wg genpsk > wgclient.psk
# Server private key
VPN_SERVER_KEY="$(cat wgserver.key)"
VPN_SERVER_PUB="$(cat wgserver.pub)"
# Pre-shared key
VPN_CLIENT_PSK="$(cat wgclient.psk)"
# Client public key
VPN_CLIENT_PUB="$(cat wgclient.pub)"
VPN_CLIENT_KEY="$(cat wgclient.key)"
# Configure firewall
uci rename firewall.@zone[0]="lan"
uci rename firewall.@zone[1]="wan"
uci del_list firewall.lan.network="${VPN_IF}"
uci add_list firewall.lan.network="${VPN_IF}"
uci -q delete firewall.wg
uci set firewall.wg="rule"
uci set firewall.wg.name="Allow-WireGuard"
uci set firewall.wg.src="wan"
uci set firewall.wg.dest_port="${VPN_PORT}"
uci set firewall.wg.proto="udp"
uci set firewall.wg.target="ACCEPT"
uci commit firewall
service firewall restart
# Configure network
uci -q delete network.${VPN_IF}
uci set network.${VPN_IF}="interface"
uci set network.${VPN_IF}.proto="wireguard"
uci set network.${VPN_IF}.private_key="${VPN_SERVER_KEY}"
uci set network.${VPN_IF}.public_key="${VPN_SERVER_PUB}" ## MY_ADD
uci set network.${VPN_IF}.listen_port="${VPN_PORT}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR6}"
# Add VPN peers
uci -q delete network.wgclient
uci set network.wgclient="wireguard_${VPN_IF}"
uci set network.wgclient.description="${VPN_PEER}" ## MY_ADD
uci set network.wgclient.public_key="${VPN_CLIENT_PUB}"
uci set network.wgclient.private_key="${VPN_CLIENT_KEY}" ## MY_ADD FOR GENERATE QR IN LUCI
uci set network.wgclient.preshared_key="${VPN_CLIENT_PSK}"
uci set network.wgclient.route_allowed_ips='1' ## MY_ADD
uci set network.wgclient.persistent_keepalive='25' ## MY_ADD
uci add_list network.wgclient.allowed_ips="${VPN_ADDR%.*}.2/32"
uci add_list network.wgclient.allowed_ips="${VPN_ADDR6%:*}:2/128"
uci commit network
service network restart
The network file:
config interface 'lan'
option device 'br-lan'
option proto 'static'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'
option ip6assign '60'
list dns '192.168.1.40'
config device
option name 'wan'
config device
option name 'vpn'
config interface 'vpn'
option proto 'wireguard'
option private_key '___IzIVSaMp+xx6csS6GdFDsYrbi5BA3Eczz16UMj20='
option public_key '___c/rcpy0uQj2DUSVhpyNiJrHjy/J3FaGQLLG7NNSc='
option listen_port '51820'
list addresses '192.168.9.1/24'
list addresses 'fd00:9::1/64'
config wireguard_vpn 'wgclient'
option description 'android'
option public_key '___PrQfw5lTRaG28wEl/RKFWpMs09NDM+eEZQXCVbhg='
option private_key '___+TzyttPZJQiAqbif1wFwESlUs9k4oAMectboNgWs='
option preshared_key '___HJRGK9ojAx6J2fe6hnQJMcIWU1LYr3jIVoUnYB2w='
option route_allowed_ips '1'
option persistent_keepalive '25'
list allowed_ips '192.168.9.2/32'
list allowed_ips 'fd00:9::2/128'
The firewall file:
config rule 'wg'
option name 'Allow-WireGuard'
option src 'wan'
option dest_port '51820'
option proto 'udp'
option target 'ACCEPT'
The client configuration vía Luci QR code:
[Interface]
PrivateKey = ___+TzyttPZJQiAqbif1wFwESlUs9k4oAMectboNgWs=
Address = 192.168.9.2/32, fd00:9::2/128
# ListenPort not defined
DNS = 192.168.1.1, 1.1.1.1
[Peer]
PublicKey = ___c/rcpy0uQj2DUSVhpyNiJrHjy/J3FaGQLLG7NNSc=
PresharedKey = ___HJRGK9ojAx6J2fe6hnQJMcIWU1LYr3jIVoUnYB2w=
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = ##########.duckdns.org:51820
PersistentKeepAlive = 25