aboutsummaryrefslogtreecommitdiff
path: root/wgcfg/writer.go
blob: 246a57d0b604d325deabda8eecac0b55948d8189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package wgcfg

import (
	"fmt"
	"net"
	"strings"
)

func (conf *Config) ToUAPI() (string, error) {
	output := new(strings.Builder)
	fmt.Fprintf(output, "private_key=%s\n", conf.PrivateKey.HexString())

	if conf.ListenPort > 0 {
		fmt.Fprintf(output, "listen_port=%d\n", conf.ListenPort)
	}

	output.WriteString("replace_peers=true\n")

	for _, peer := range conf.Peers {
		fmt.Fprintf(output, "public_key=%s\n", peer.PublicKey.HexString())
		fmt.Fprintf(output, "protocol_version=1\n")
		fmt.Fprintf(output, "replace_allowed_ips=true\n")

		if !peer.PresharedKey.IsZero() {
			fmt.Fprintf(output, "preshared_key = %s\n", peer.PresharedKey.String())
		}

		if len(peer.AllowedIPs) > 0 {
			for _, address := range peer.AllowedIPs {
				fmt.Fprintf(output, "allowed_ip=%s\n", address.String())
			}
		}

		if len(peer.Endpoints) > 0 {
			var reps []string
			for _, ep := range peer.Endpoints {
				ips, err := net.LookupIP(ep.Host)
				if err != nil {
					return "", err
				}
				var ip net.IP
				for _, iterip := range ips {
					if ip4 := iterip.To4(); ip4 != nil {
						ip = ip4
						break
					}
					if ip == nil {
						ip = iterip
					}
				}
				if ip == nil {
					return "", fmt.Errorf("unable to resolve IP address of endpoint %q (%v)", ep.Host, ips)
				}
				resolvedEndpoint := Endpoint{ip.String(), ep.Port}
				reps = append(reps, resolvedEndpoint.String())
			}
			fmt.Fprintf(output, "endpoint=%s\n", strings.Join(reps, ","))
		} else {
			fmt.Fprint(output, "endpoint=\n")
		}

		// Note: this needs to come *after* endpoint definitions,
		// because setting it will trigger a handshake to all
		// already-defined endpoints.
		fmt.Fprintf(output, "persistent_keepalive_interval=%d\n", peer.PersistentKeepalive)
	}
	return output.String(), nil
}