Skip to content

Commit 56e766d

Browse files
committed
endpoint DNS to config, serve resolve
1 parent dca56a0 commit 56e766d

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/cmd/serve.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"sync"
1313
"time"
14+
"net"
1415

1516
"github.com/spf13/cobra"
1617
"github.com/spf13/viper"
@@ -353,7 +354,11 @@ func (c serveCmdConfig) Run() {
353354
Peers: []peer.PeerConfigArgs{
354355
{
355356
PublicKey: viper.GetString("Relay.Peer.publickey"),
356-
Endpoint: viper.GetString("Relay.Peer.endpoint"),
357+
Endpoint: func() string {
358+
endpoint, err := net.ResolveUDPAddr("udp", (viper.GetString("Relay.Peer.endpoint")))
359+
check("failed to resolve DNS", err)
360+
return endpoint.String()
361+
}(),
357362
PersistentKeepaliveInterval: func() int {
358363
if len(viper.GetString("Relay.Peer.endpoint")) > 0 {
359364
return viper.GetInt("Relay.Peer.keepalive")

src/peer/config.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,22 @@ func (c *Config) GetPeerPublicKey(i int) string {
378378

379379
func (c *Config) GetPeerEndpoint(i int) string {
380380
if len(c.peers) > i {
381-
endpoint := c.peers[i].config.Endpoint
382-
if endpoint != nil {
383-
return endpoint.String()
384-
}
381+
if len(c.peers[i].endpoint) > 0 {
382+
endpoint := c.peers[i].endpoint
383+
if endpoint != "" {
384+
return endpoint
385+
}
386+
387+
return ""
385388

386-
return ""
389+
} else {
390+
endpoint := c.peers[i].config.Endpoint
391+
if endpoint != nil {
392+
return endpoint.String()
393+
}
394+
395+
return ""
396+
}
387397
}
388398

389399
return ""

src/peer/peer_config.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
"net/netip"
99
"strings"
1010
"time"
11+
"regexp"
1112

1213
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
1314
)
1415

1516
type PeerConfig struct {
1617
config wgtypes.PeerConfig
1718
privateKey *wgtypes.Key
19+
endpoint string
1820
nickname string
1921
}
2022

@@ -170,12 +172,19 @@ func (p *PeerConfig) SetPresharedKey(presharedKey string) error {
170172
}
171173

172174
func (p *PeerConfig) SetEndpoint(addr string) error {
173-
endpoint, err := net.ResolveUDPAddr("udp", addr)
174-
if err != nil {
175-
return err
175+
host, _, _ := net.SplitHostPort(addr)
176+
ip := regexp.MustCompile(`\d`).MatchString(host)
177+
if ip {
178+
endpoint, err := net.ResolveUDPAddr("udp", addr)
179+
if err != nil {
180+
return err
181+
}
182+
p.config.Endpoint = endpoint
183+
return nil
184+
} else {
185+
endpoint := addr
186+
p.endpoint = endpoint
176187
}
177-
178-
p.config.Endpoint = endpoint
179188
return nil
180189
}
181190

@@ -275,6 +284,9 @@ func (p *PeerConfig) AsFile() string {
275284
if p.config.Endpoint != nil {
276285
s.WriteString(fmt.Sprintf("Endpoint = %s\n", p.config.Endpoint.String()))
277286
}
287+
if p.endpoint != "" {
288+
s.WriteString(fmt.Sprintf("Endpoint = %s\n", p.endpoint))
289+
}
278290
if p.config.PersistentKeepaliveInterval != nil {
279291
s.WriteString(fmt.Sprintf("PersistentKeepalive = %d\n", *p.config.PersistentKeepaliveInterval/time.Second))
280292
}
@@ -289,6 +301,9 @@ func (p *PeerConfig) AsIPC() string {
289301
if p.config.Endpoint != nil {
290302
s.WriteString(fmt.Sprintf("endpoint=%s\n", p.config.Endpoint.String()))
291303
}
304+
if p.endpoint != "" {
305+
s.WriteString(fmt.Sprintf("Endpoint = %s\n", p.endpoint))
306+
}
292307
for _, a := range p.config.AllowedIPs {
293308
s.WriteString(fmt.Sprintf("allowed_ip=%s\n", a.String()))
294309
}

0 commit comments

Comments
 (0)