-
Notifications
You must be signed in to change notification settings - Fork 18
/
conf.go
142 lines (121 loc) · 3.11 KB
/
conf.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"context"
"fmt"
"net"
"net/netip"
"time"
"github.com/zhsj/wghttp/internal/resolver"
"golang.zx2c4.com/wireguard/device"
)
type peer struct {
resolver *resolver.Resolver
pubKey keyT
psk keyT
host string
ip netip.Addr
port uint16
}
func newPeerEndpoint() (*peer, error) {
p := &peer{
pubKey: opts.PeerKey,
psk: opts.PresharedKey,
host: opts.PeerEndpoint.host,
port: opts.PeerEndpoint.port,
}
var err error
p.ip, err = netip.ParseAddr(p.host)
if err == nil {
return p, nil
}
p.resolver = resolver.New(
opts.ResolveDNS,
func(ctx context.Context, network, address string) (net.Conn, error) {
netConn, err := (&net.Dialer{}).DialContext(ctx, network, address)
logger.Verbosef("Using %s to resolve peer endpoint: %v", opts.ResolveDNS, err)
return netConn, err
},
)
p.ip, err = p.resolveHost()
if err != nil {
return nil, fmt.Errorf("resolve peer endpoint ip: %w", err)
}
return p, err
}
func (p *peer) initConf() string {
conf := fmt.Sprintf("public_key=%s\n", p.pubKey)
conf += fmt.Sprintf("endpoint=%s\n", netip.AddrPortFrom(p.ip, p.port))
conf += "allowed_ip=0.0.0.0/0\n"
conf += "allowed_ip=::/0\n"
if opts.KeepaliveInterval > 0 {
conf += fmt.Sprintf("persistent_keepalive_interval=%d\n", opts.KeepaliveInterval)
}
if p.psk != "" {
conf += fmt.Sprintf("preshared_key=%s\n", p.psk)
}
return conf
}
func (p *peer) updateConf() (string, bool) {
newIP, err := p.resolveHost()
if err != nil {
logger.Verbosef("Resolve peer endpoint: %v", err)
return "", false
}
if p.ip == newIP {
return "", false
}
p.ip = newIP
logger.Verbosef("PeerEndpoint is changed to: %s", p.ip)
conf := fmt.Sprintf("public_key=%s\n", p.pubKey)
conf += "update_only=true\n"
conf += fmt.Sprintf("endpoint=%s\n", netip.AddrPortFrom(p.ip, p.port))
return conf, true
}
func (p *peer) resolveHost() (netip.Addr, error) {
ips, err := p.resolver.LookupNetIP(context.Background(), "ip", p.host)
if err != nil {
return netip.Addr{}, fmt.Errorf("resolve ip for %s: %w", p.host, err)
}
for _, ip := range ips {
// netstack doesn't seem to understand IPv4-mapped IPv6 addresses.
ip = ip.Unmap()
conn, err := net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(netip.AddrPortFrom(ip, p.port)))
if err == nil {
conn.Close()
return ip, nil
} else {
logger.Verbosef("Dial %s: %s", ip, err)
}
}
return netip.Addr{}, fmt.Errorf("no available ip for %s", p.host)
}
func ipcSet(dev *device.Device) error {
conf := fmt.Sprintf("private_key=%s\n", opts.PrivateKey)
if opts.ClientPort != 0 {
conf += fmt.Sprintf("listen_port=%d\n", opts.ClientPort)
}
peer, err := newPeerEndpoint()
if err != nil {
return err
}
conf += peer.initConf()
logger.Verbosef("Device config:\n%s", conf)
if err := dev.IpcSet(conf); err != nil {
return err
}
if peer.resolver != nil {
go func() {
c := time.Tick(time.Duration(opts.ResolveInterval) * time.Second)
for range c {
conf, needUpdate := peer.updateConf()
if !needUpdate {
continue
}
if err := dev.IpcSet(conf); err != nil {
logger.Errorf("Config device: %v", err)
}
}
}()
}
return nil
}