{% if helpers.exists('OPNsense.wireguard.general.enabled') and OPNsense.wireguard.general.enabled == '1' %}
{% if helpers.exists('OPNsense.wireguard.server.servers.server') %}
{%   for server_list in helpers.toList('OPNsense.wireguard.server.servers.server') %}
{%     if TARGET_FILTERS['OPNsense.wireguard.server.servers.server.' ~ loop.index0] or TARGET_FILTERS['OPNsense.wireguard.server.servers.server'] %}
{%       if server_list.enabled == '1' %}
####################################################
# Interface settings, not used by `wg`             #
# Only used for reference and detection of changes #
# in the configuration                             #
####################################################
# Address =  {{server_list.tunneladdress|default('')}}
# DNS = {{ server_list.dns|default('')}}
# MTU = {{ server_list.mtu|default('') }}
# disableroutes = {{server_list.disableroutes}}
# gateway = {{server_list.gateway}}

[Interface]
PrivateKey = {{ server_list.privkey }}
{%         if server_list.port|default('') != '' %}
ListenPort = {{ server_list.port }}
{%         endif %}
{%         if server_list.peers|default('') != '' %}
{%           for peerlist in server_list.peers.split(",") %}
{%             set peerlist2_data = helpers.getUUID(peerlist) %}
{%             if peerlist2_data != {} and peerlist2_data.enabled == '1' %}

[Peer]
# friendly_name = {{ peerlist2_data.name }}
PublicKey = {{ peerlist2_data.pubkey }}
{%               if peerlist2_data.psk|default('') != '' %}
PresharedKey = {{ peerlist2_data.psk }}
{%               endif %}
{%               if peerlist2_data.serveraddress|default('') != '' %}
Endpoint = {{ peerlist2_data.serveraddress }}{% if peerlist2_data.serverport|default('') != '' %}:{{ peerlist2_data.serverport }}{% else %}:51820{% endif %}
{%               endif %}

AllowedIPs = {{ peerlist2_data.tunneladdress }}
{%               if peerlist2_data.keepalive|default('') != '' %}
PersistentKeepalive = {{ peerlist2_data.keepalive }}
{%               endif %}
{%             endif %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%     endif %}
{%   endfor %}
{% endif %}
{% endif %}
