aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/conf/writer.go
diff options
context:
space:
mode:
Diffstat (limited to 'conf/writer.go')
-rw-r--r--conf/writer.go125
1 files changed, 125 insertions, 0 deletions
diff --git a/conf/writer.go b/conf/writer.go
new file mode 100644
index 00000000..642d14a7
--- /dev/null
+++ b/conf/writer.go
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package conf
+
+import (
+ "errors"
+ "fmt"
+ "net"
+ "strings"
+)
+
+func (conf *Config) ToWgQuick() string {
+ var output strings.Builder
+ output.WriteString("[Interface]\n")
+
+ output.WriteString(fmt.Sprintf("PrivateKey = %s\n", conf.Interface.PrivateKey.String()))
+
+ if conf.Interface.ListenPort > 0 {
+ output.WriteString(fmt.Sprintf("ListenPort = %d\n", conf.Interface.ListenPort))
+ }
+
+ if len(conf.Interface.Addresses) > 0 {
+ addrStrings := make([]string, len(conf.Interface.Addresses))
+ for i, address := range conf.Interface.Addresses {
+ addrStrings[i] = address.String()
+ }
+ output.WriteString(fmt.Sprintf("Address = %s\n", strings.Join(addrStrings[:], ", ")))
+ }
+
+ if len(conf.Interface.Dns) > 0 {
+ addrStrings := make([]string, len(conf.Interface.Dns))
+ for i, address := range conf.Interface.Dns {
+ addrStrings[i] = address.String()
+ }
+ output.WriteString(fmt.Sprintf("DNS = %s\n", strings.Join(addrStrings[:], ", ")))
+ }
+
+ if conf.Interface.Mtu > 0 {
+ output.WriteString(fmt.Sprintf("MTU = %d\n", conf.Interface.Mtu))
+ }
+
+ for _, peer := range conf.Peers {
+ output.WriteString("\n[Peer]\n")
+
+ output.WriteString(fmt.Sprintf("PublicKey = %s\n", peer.PublicKey.String()))
+
+ if !peer.PresharedKey.IsZero() {
+ output.WriteString(fmt.Sprintf("PresharedKey = %s\n", peer.PresharedKey.String()))
+ }
+
+ if len(peer.AllowedIPs) > 0 {
+ addrStrings := make([]string, len(peer.AllowedIPs))
+ for i, address := range peer.AllowedIPs {
+ addrStrings[i] = address.String()
+ }
+ output.WriteString(fmt.Sprintf("AllowedIPs = %s\n", strings.Join(addrStrings[:], ", ")))
+ }
+
+ if !peer.Endpoint.IsEmpty() {
+ output.WriteString(fmt.Sprintf("Endpoint = %s\n", peer.Endpoint.String()))
+ }
+
+ if peer.PersistentKeepalive > 0 {
+ output.WriteString(fmt.Sprintf("PersistentKeepalive = %d\n", peer.PersistentKeepalive))
+ }
+ }
+ return output.String()
+}
+
+func (conf *Config) ToUAPI() (string, error) {
+ var output strings.Builder
+ output.WriteString(fmt.Sprintf("private_key=%s\n", conf.Interface.PrivateKey.HexString()))
+
+ if conf.Interface.ListenPort > 0 {
+ output.WriteString(fmt.Sprintf("listen_port=%d\n", conf.Interface.ListenPort))
+ }
+
+ if len(conf.Peers) > 0 {
+ output.WriteString("replace_peers=true\n")
+ }
+
+ for _, peer := range conf.Peers {
+ output.WriteString(fmt.Sprintf("public_key=%s\n", peer.PublicKey.HexString()))
+
+ if !peer.PresharedKey.IsZero() {
+ output.WriteString(fmt.Sprintf("preshared_key = %s\n", peer.PresharedKey.String()))
+ }
+
+ if !peer.Endpoint.IsEmpty() {
+ ips, err := net.LookupIP(peer.Endpoint.Host)
+ if err != nil {
+ return "", err
+ }
+ var ip net.IP
+ for _, iterip := range ips {
+ iterip = iterip.To4()
+ if iterip != nil {
+ ip = iterip
+ break
+ }
+ if ip == nil {
+ ip = iterip
+ }
+ }
+ if ip == nil {
+ return "", errors.New("Unable to resolve IP address of endpoint")
+ }
+ resolvedEndpoint := Endpoint{ip.String(), peer.Endpoint.Port}
+ output.WriteString(fmt.Sprintf("endpoint=%s\n", resolvedEndpoint.String()))
+ }
+
+ output.WriteString(fmt.Sprintf("persistent_keepalive_interval=%d\n", peer.PersistentKeepalive))
+
+ if len(peer.AllowedIPs) > 0 {
+ output.WriteString("replace_allowed_ips=true\n")
+ for _, address := range peer.AllowedIPs {
+ output.WriteString(fmt.Sprintf("allowed_ip=%s\n", address.String()))
+ }
+ }
+ }
+ return output.String(), nil
+}