+
+{{ partial("layout_partials/base_dialog",['fields':formDialogEditWireguardClient,'id':'dialogEditWireguardClient','label':lang._('Edit Endpoint')])}}
+{{ partial("layout_partials/base_dialog",['fields':formDialogEditWireguardServer,'id':'dialogEditWireguardServer','label':lang._('Edit Server')])}}
+
+
diff --git a/net/wireguard/src/opnsense/scripts/OPNsense/Wireguard/genkey.sh b/net/wireguard/src/opnsense/scripts/OPNsense/Wireguard/genkey.sh
new file mode 100755
index 000000000..1cb5e5920
--- /dev/null
+++ b/net/wireguard/src/opnsense/scripts/OPNsense/Wireguard/genkey.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+# Copyright (c) 2018 Michael Muenz
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+TMPDIR="/tmp"
+GENPRIV="/usr/local/bin/wg genkey"
+GENPUB="/usr/local/bin/wg pubkey"
+
+cleanup() {
+ # Delete old files
+ rm -f $TMPDIR/wireguard.*
+}
+
+private() {
+ # Generate a private key and put it to /tmp
+ umask 077 && ${GENPRIV} | tee ${TMPDIR}/wireguard.priv
+}
+
+public() {
+ # Generate a public key and put it to /tmp
+ ${GENPUB} < ${TMPDIR}/wireguard.priv | tee ${TMPDIR}/wireguard.pub
+}
+
+case "$1" in
+ private)
+ cleanup
+ private
+ ;;
+ public)
+ public
+ ;;
+esac
diff --git a/net/wireguard/src/opnsense/scripts/OPNsense/Wireguard/setup.sh b/net/wireguard/src/opnsense/scripts/OPNsense/Wireguard/setup.sh
new file mode 100755
index 000000000..75ba580c9
--- /dev/null
+++ b/net/wireguard/src/opnsense/scripts/OPNsense/Wireguard/setup.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+mkdir -p /var/run/wireguard
+chmod 755 /var/run/wireguard
diff --git a/net/wireguard/src/opnsense/service/conf/actions.d/actions_wireguard.conf b/net/wireguard/src/opnsense/service/conf/actions.d/actions_wireguard.conf
new file mode 100644
index 000000000..46932513f
--- /dev/null
+++ b/net/wireguard/src/opnsense/service/conf/actions.d/actions_wireguard.conf
@@ -0,0 +1,23 @@
+[start]
+command:/usr/local/opnsense/scripts/OPNsense/Wireguard/setup.sh;/usr/local/etc/rc.d/opnsense-wireguard start
+parameters:
+type:script
+message:starting Wireguard
+
+[stop]
+command:/usr/local/etc/rc.d/opnsense-wireguard stop
+parameters:
+type:script
+message:stopping Wireguard
+
+[restart]
+command:/usr/local/opnsense/scripts/OPNsense/Wireguard/setup.sh;/usr/local/etc/rc.d/opnsense-wireguard restart
+parameters:
+type:script
+message:restarting Wireguard
+
+[genkey]
+command:/usr/local/opnsense/scripts/OPNsense/Wireguard/genkey.sh
+parameters: %s
+type:script_output
+message:generating Wireguard keys
diff --git a/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/+TARGETS b/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/+TARGETS
new file mode 100644
index 000000000..ca42cbdba
--- /dev/null
+++ b/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/+TARGETS
@@ -0,0 +1,2 @@
+opnsense-wireguard:/etc/rc.conf.d/opnsense-wireguard
+wireguard-server.conf:/usr/local/etc/wireguard/wg[OPNsense.wireguard.server.servers.server.%.instance].conf
diff --git a/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/opnsense-wireguard b/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/opnsense-wireguard
new file mode 100644
index 000000000..215e9958e
--- /dev/null
+++ b/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/opnsense-wireguard
@@ -0,0 +1,15 @@
+{% if helpers.exists('OPNsense.wireguard.general.enabled') and OPNsense.wireguard.general.enabled == '1' %}
+wireguard_var_script="/usr/local/opnsense/scripts/OPNsense/Wireguard/setup.sh"
+wireguard_enable="YES"
+{% if helpers.exists('OPNsense.wireguard.server.servers.server') %}
+{% set activeservers=[] %}
+{% for servers in helpers.toList('OPNsense.wireguard.server.servers.server') %}
+{% if servers.enabled == '1' %}
+{% do activeservers.append("wg" + servers.instance) %}
+{% endif %}
+{% endfor %}
+{% endif %}
+wireguard_config="{{ activeservers | join(' ') }}"
+{% else %}
+wireguard_enable="NO"
+{% endif %}
diff --git a/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/wireguard-server.conf b/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/wireguard-server.conf
new file mode 100644
index 000000000..ca3e5a84d
--- /dev/null
+++ b/net/wireguard/src/opnsense/service/templates/OPNsense/Wireguard/wireguard-server.conf
@@ -0,0 +1,29 @@
+{% 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]
+Address = {{ server_list.tunneladdress }}
+PrivateKey = {{ server_list.privkey }}
+ListenPort = {{ server_list.port }}
+{% 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]
+PublicKey = {{ peerlist2_data.pubkey }}
+AllowedIPs = {{ peerlist2_data.tunneladdress }}
+{% if peerlist2_data.serveraddress|default('') != '' %}
+Endpoint = {{ peerlist2_data.serveraddress }}:{{ peerlist2_data.serverport }}
+{% endif %}
+{% endif %}
+{% endfor %}
+{% endif %}
+{% endif %}
+{% endif %}
+{% endfor %}
+{% endif %}
+
+{% endif %}