@@ -8,13 +8,15 @@ import (
88 "net/netip"
99 "strings"
1010 "time"
11+ "regexp"
1112
1213 "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
1314)
1415
1516type 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
172174func (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