Compare commits
2 Commits
5d946e4e93
...
b15f394199
| Author | SHA1 | Date | |
|---|---|---|---|
| b15f394199 | |||
| f225e15b2c |
+22
-2
@@ -3,6 +3,7 @@ package wireguard
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
@@ -44,10 +45,29 @@ type PeerConfig struct {
|
||||
|
||||
// EndpointAddress is a host:port pair to reach the Peer
|
||||
type EndpointAddress struct {
|
||||
Name string
|
||||
Host string
|
||||
Port uint16
|
||||
}
|
||||
|
||||
// Name returns the first part of a hostname
|
||||
func (ep EndpointAddress) Name() string {
|
||||
before, _, _ := strings.Cut(ep.Host, ".")
|
||||
return before
|
||||
}
|
||||
|
||||
func (ep EndpointAddress) String() string {
|
||||
switch {
|
||||
case ep.Host == "":
|
||||
return ""
|
||||
case ep.Port == 0:
|
||||
return ep.Host
|
||||
case !strings.ContainsRune(ep.Host, ':'):
|
||||
return fmt.Sprintf("%s:%v", ep.Host, ep.Port)
|
||||
default:
|
||||
return fmt.Sprintf("[%s]:%v", ep.Host, ep.Port)
|
||||
}
|
||||
}
|
||||
|
||||
// FromString sets the EndpointAddress from a given "[host]:port"
|
||||
func (ep *EndpointAddress) FromString(s string) error {
|
||||
host, port, err := core.SplitHostPort(s)
|
||||
@@ -55,7 +75,7 @@ func (ep *EndpointAddress) FromString(s string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ep.Name = host
|
||||
ep.Host = host
|
||||
|
||||
switch {
|
||||
case port != "":
|
||||
|
||||
Reference in New Issue
Block a user