2024-10-02 18:24:22 +02:00
|
|
|
package device
|
2023-02-13 18:34:56 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
// WGAddress WireGuard parsed address
|
2023-02-13 18:34:56 +01:00
|
|
|
type WGAddress struct {
|
|
|
|
IP net.IP
|
|
|
|
Network *net.IPNet
|
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
// ParseWGAddress parse a string ("1.2.3.4/24") address to WG Address
|
|
|
|
func ParseWGAddress(address string) (WGAddress, error) {
|
2023-02-13 18:34:56 +01:00
|
|
|
ip, network, err := net.ParseCIDR(address)
|
|
|
|
if err != nil {
|
|
|
|
return WGAddress{}, err
|
|
|
|
}
|
|
|
|
return WGAddress{
|
|
|
|
IP: ip,
|
|
|
|
Network: network,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (addr WGAddress) String() string {
|
|
|
|
maskSize, _ := addr.Network.Mask.Size()
|
|
|
|
return fmt.Sprintf("%s/%d", addr.IP.String(), maskSize)
|
|
|
|
}
|