mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 11:46:09 +01:00
cleanup
This commit is contained in:
@@ -51,9 +51,6 @@ func amneziaWGFamily(lookup func(string) (*netlink.GenlFamily, error)) (*netlink
|
||||
return family, nil
|
||||
}
|
||||
|
||||
// Requests use their own short-lived sockets, which the netlink library closes.
|
||||
func (c *AmneziaWGClient) Close() error { return nil }
|
||||
|
||||
func (c *AmneziaWGClient) kernelLink(name string) error {
|
||||
link, err := c.linkByName(name)
|
||||
if err != nil {
|
||||
@@ -68,12 +65,12 @@ func (c *AmneziaWGClient) kernelLink(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *AmneziaWGClient) Devices() ([]*wgtypes.Device, error) {
|
||||
func (c *AmneziaWGClient) Devices() ([]WireGuardDevice, error) {
|
||||
links, err := c.links()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var devices []*wgtypes.Device
|
||||
var devices []WireGuardDevice
|
||||
for _, link := range links {
|
||||
if link.Type() != "amneziawg" {
|
||||
continue
|
||||
@@ -90,38 +87,33 @@ func (c *AmneziaWGClient) Devices() ([]*wgtypes.Device, error) {
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
func (c *AmneziaWGClient) Device(name string) (*wgtypes.Device, error) {
|
||||
device, _, err := c.readDevice(name)
|
||||
return device, err
|
||||
}
|
||||
|
||||
func (c *AmneziaWGClient) AmneziaWGOptions(name string) (domain.AmneziaWGOptions, error) {
|
||||
_, options, err := c.readDevice(name)
|
||||
return options, err
|
||||
}
|
||||
|
||||
func (c *AmneziaWGClient) readDevice(name string) (*wgtypes.Device, domain.AmneziaWGOptions, error) {
|
||||
func (c *AmneziaWGClient) Device(name string) (WireGuardDevice, error) {
|
||||
if err := c.kernelLink(name); err != nil {
|
||||
return nil, nil, err
|
||||
return WireGuardDevice{}, err
|
||||
}
|
||||
messages, err := c.exchange(awgGetDevice, unix.NLM_F_DUMP, []*nl.RtAttr{nl.NewRtAttr(unix.WGDEVICE_A_IFNAME, nl.ZeroTerminated(name))})
|
||||
if errors.Is(err, unix.ENODEV) || errors.Is(err, unix.ENOENT) {
|
||||
return nil, nil, os.ErrNotExist
|
||||
return WireGuardDevice{}, os.ErrNotExist
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("AmneziaWG device read: %w", err)
|
||||
return WireGuardDevice{}, fmt.Errorf("AmneziaWG device read: %w", err)
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
return nil, nil, os.ErrNotExist
|
||||
return WireGuardDevice{}, os.ErrNotExist
|
||||
}
|
||||
device, options, err := parseAmneziaWGDevice(messages, c.version)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("invalid AmneziaWG device response: %w", err)
|
||||
return WireGuardDevice{}, fmt.Errorf("invalid AmneziaWG device response: %w", err)
|
||||
}
|
||||
if device.Name != name {
|
||||
return nil, nil, fmt.Errorf("unexpected AmneziaWG interface in response")
|
||||
return WireGuardDevice{}, fmt.Errorf("unexpected AmneziaWG interface in response")
|
||||
}
|
||||
return device, options, nil
|
||||
return WireGuardDevice{Device: device, AmneziaWG: options}, nil
|
||||
}
|
||||
|
||||
func (c *AmneziaWGClient) ValidateOptions(options domain.AmneziaWGOptions) error {
|
||||
_, err := encodeAmneziaWGOptions(options, c.version)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *AmneziaWGClient) ConfigureDevice(name string, cfg wgtypes.Config) error {
|
||||
|
||||
@@ -35,13 +35,13 @@ func amneziaWGConfigBatches(name string, cfg wgtypes.Config, options domain.Amne
|
||||
if *cfg.ListenPort < 0 || *cfg.ListenPort > 65535 {
|
||||
return nil, fmt.Errorf("invalid AmneziaWG listen port")
|
||||
}
|
||||
attrs = append(attrs, nl.NewRtAttr(unix.WGDEVICE_A_LISTEN_PORT, awgNumber(uint64(*cfg.ListenPort), 2)))
|
||||
attrs = append(attrs, nl.NewRtAttr(unix.WGDEVICE_A_LISTEN_PORT, nl.Uint16Attr(uint16(*cfg.ListenPort))))
|
||||
}
|
||||
if cfg.FirewallMark != nil {
|
||||
attrs = append(attrs, nl.NewRtAttr(unix.WGDEVICE_A_FWMARK, awgNumber(uint64(uint32(*cfg.FirewallMark)), 4)))
|
||||
attrs = append(attrs, nl.NewRtAttr(unix.WGDEVICE_A_FWMARK, nl.Uint32Attr(uint32(*cfg.FirewallMark))))
|
||||
}
|
||||
if cfg.ReplacePeers {
|
||||
attrs = append(attrs, nl.NewRtAttr(unix.WGDEVICE_A_FLAGS, awgNumber(unix.WGDEVICE_F_REPLACE_PEERS, 4)))
|
||||
attrs = append(attrs, nl.NewRtAttr(unix.WGDEVICE_A_FLAGS, nl.Uint32Attr(unix.WGDEVICE_F_REPLACE_PEERS)))
|
||||
}
|
||||
custom, err := encodeAmneziaWGOptions(options, version)
|
||||
if err != nil {
|
||||
@@ -103,7 +103,7 @@ func encodeAmneziaWGPeer(peer wgtypes.PeerConfig, ips []net.IPNet, first bool, v
|
||||
flags |= unix.WGPEER_F_REPLACE_ALLOWEDIPS
|
||||
}
|
||||
if flags != 0 {
|
||||
attr.AddRtAttr(unix.WGPEER_A_FLAGS, awgNumber(uint64(flags), 4))
|
||||
attr.AddRtAttr(unix.WGPEER_A_FLAGS, nl.Uint32Attr(flags))
|
||||
}
|
||||
if first {
|
||||
if peer.PresharedKey != nil {
|
||||
@@ -121,18 +121,18 @@ func encodeAmneziaWGPeer(peer wgtypes.PeerConfig, ips []net.IPNet, first bool, v
|
||||
if interval < 0 || interval > 65535*time.Second || interval%time.Second != 0 {
|
||||
return nil, fmt.Errorf("invalid AmneziaWG keepalive interval")
|
||||
}
|
||||
value, size := uint64(interval/time.Second), 2
|
||||
seconds := uint16(interval / time.Second)
|
||||
value := nl.Uint16Attr(seconds)
|
||||
if version >= 3 {
|
||||
value |= value << 16
|
||||
size = 4
|
||||
value = nl.Uint32Attr(uint32(seconds) | uint32(seconds)<<16)
|
||||
}
|
||||
attr.AddRtAttr(unix.WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL, awgNumber(value, size))
|
||||
attr.AddRtAttr(unix.WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL, value)
|
||||
}
|
||||
}
|
||||
if len(ips) != 0 {
|
||||
allowed := attr.AddRtAttr(unix.WGPEER_A_ALLOWEDIPS|unix.NLA_F_NESTED, nil)
|
||||
for _, ip := range ips {
|
||||
address, family, bits := ip.IP.To4(), uint64(unix.AF_INET), 32
|
||||
address, family, bits := ip.IP.To4(), uint16(unix.AF_INET), 32
|
||||
if address == nil {
|
||||
address, family, bits = ip.IP.To16(), unix.AF_INET6, 128
|
||||
}
|
||||
@@ -141,7 +141,7 @@ func encodeAmneziaWGPeer(peer wgtypes.PeerConfig, ips []net.IPNet, first bool, v
|
||||
return nil, fmt.Errorf("invalid AmneziaWG allowed IP")
|
||||
}
|
||||
entry := allowed.AddRtAttr(unix.NLA_F_NESTED, nil)
|
||||
entry.AddRtAttr(unix.WGALLOWEDIP_A_FAMILY, awgNumber(family, 2))
|
||||
entry.AddRtAttr(unix.WGALLOWEDIP_A_FAMILY, nl.Uint16Attr(family))
|
||||
entry.AddRtAttr(unix.WGALLOWEDIP_A_IPADDR, address)
|
||||
entry.AddRtAttr(unix.WGALLOWEDIP_A_CIDR_MASK, []byte{byte(ones)})
|
||||
}
|
||||
|
||||
@@ -72,9 +72,9 @@ func (o awgOption) encode(value string, version uint8) ([]byte, error) {
|
||||
switch o.kind {
|
||||
case awgOptionU16:
|
||||
v, err := strconv.ParseUint(value, 10, 16)
|
||||
return awgNumber(v, 2), err
|
||||
return nl.Uint16Attr(uint16(v)), err
|
||||
case awgOptionHeader:
|
||||
lo, hi, err := awgRange(value, 32)
|
||||
lo, hi, err := domain.ParseAmneziaWGRange(value, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -83,11 +83,11 @@ func (o awgOption) encode(value string, version uint8) ([]byte, error) {
|
||||
if lo != hi {
|
||||
return nil, fmt.Errorf("header ranges require AmneziaWG v2 or newer")
|
||||
}
|
||||
return awgNumber(lo, 4), nil
|
||||
return nl.Uint32Attr(uint32(lo)), nil
|
||||
case 2:
|
||||
return nl.ZeroTerminated(awgRangeString(lo, hi)), nil
|
||||
default:
|
||||
return awgNumber(lo|hi<<32, 8), nil
|
||||
return nl.Uint64Attr(lo | hi<<32), nil
|
||||
}
|
||||
case awgOptionString:
|
||||
return nl.ZeroTerminated(value), nil
|
||||
@@ -95,8 +95,8 @@ func (o awgOption) encode(value string, version uint8) ([]byte, error) {
|
||||
key, err := wgtypes.ParseKey(value)
|
||||
return key[:], err
|
||||
case awgOptionRange16:
|
||||
lo, hi, err := awgRange(value, 16)
|
||||
return awgNumber(lo|hi<<16, 4), err
|
||||
lo, hi, err := domain.ParseAmneziaWGRange(value, 16)
|
||||
return nl.Uint32Attr(uint32(lo | hi<<16)), err
|
||||
case awgOptionBool:
|
||||
if value == "1" || value == "on" {
|
||||
return []byte{1}, nil
|
||||
@@ -121,7 +121,7 @@ func (o awgOption) decode(data []byte, version uint8) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
lo, hi, err := awgRange(value, 32)
|
||||
lo, hi, err := domain.ParseAmneziaWGRange(value, 32)
|
||||
return awgRangeString(lo, hi), err
|
||||
default:
|
||||
v, err := awgReadNumber(data, 8)
|
||||
@@ -145,21 +145,6 @@ func (o awgOption) decode(data []byte, version uint8) (string, error) {
|
||||
return "", fmt.Errorf("unknown AmneziaWG option kind")
|
||||
}
|
||||
|
||||
func awgNumber(v uint64, size int) []byte {
|
||||
b := make([]byte, size)
|
||||
switch size {
|
||||
case 1:
|
||||
b[0] = byte(v)
|
||||
case 2:
|
||||
nl.NativeEndian().PutUint16(b, uint16(v))
|
||||
case 4:
|
||||
nl.NativeEndian().PutUint32(b, uint32(v))
|
||||
case 8:
|
||||
nl.NativeEndian().PutUint64(b, v)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func awgReadNumber(b []byte, size int) (uint64, error) {
|
||||
if len(b) != size {
|
||||
return 0, fmt.Errorf("invalid AmneziaWG numeric attribute size")
|
||||
@@ -184,25 +169,6 @@ func awgString(data []byte) (string, error) {
|
||||
return string(data[:len(data)-1]), nil
|
||||
}
|
||||
|
||||
func awgRange(value string, bits int) (uint64, uint64, error) {
|
||||
parts := strings.Split(value, "-")
|
||||
if len(parts) > 2 {
|
||||
return 0, 0, fmt.Errorf("invalid range")
|
||||
}
|
||||
lo, err := strconv.ParseUint(parts[0], 10, bits)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
hi := lo
|
||||
if len(parts) == 2 {
|
||||
hi, err = strconv.ParseUint(parts[1], 10, bits)
|
||||
}
|
||||
if err != nil || lo > hi {
|
||||
return 0, 0, fmt.Errorf("invalid range")
|
||||
}
|
||||
return lo, hi, nil
|
||||
}
|
||||
|
||||
func awgRangeString(lo, hi uint64) string {
|
||||
if lo == hi {
|
||||
return strconv.FormatUint(lo, 10)
|
||||
|
||||
@@ -26,11 +26,7 @@ func awgKernelLink(name string) (netlink.Link, error) {
|
||||
}
|
||||
|
||||
func awgTestReply(version uint8, attrs ...*nl.RtAttr) []byte {
|
||||
data := []byte{0, version, 0, 0}
|
||||
for _, attr := range attrs {
|
||||
data = append(data, attr.Serialize()...)
|
||||
}
|
||||
return data
|
||||
return append([]byte{0, version, 0, 0}, awgTestSerialize(attrs)...)
|
||||
}
|
||||
|
||||
func awgTestAttrMap(t *testing.T, data []byte) map[uint16][]byte {
|
||||
@@ -139,13 +135,11 @@ func TestAmneziaWGNativeDeviceVersionsAndMultipartDump(t *testing.T) {
|
||||
require.Equal(t, int64(123), peer.ReceiveBytes)
|
||||
require.Equal(t, int64(456), peer.TransmitBytes)
|
||||
require.Equal(t, 25*time.Second, peer.PersistentKeepaliveInterval)
|
||||
options, err := client.AmneziaWGOptions("awg0")
|
||||
require.NoError(t, err)
|
||||
header := "100"
|
||||
if version >= 2 {
|
||||
header = "100-200"
|
||||
}
|
||||
require.Equal(t, domain.AmneziaWGOptions{"H1": header, "Jc": "4"}, options)
|
||||
require.Equal(t, domain.AmneziaWGOptions{"H1": header, "Jc": "4"}, device.AmneziaWG)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"io"
|
||||
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
// A WireGuardClient is a type which can control a WireGuard device.
|
||||
@@ -13,3 +15,9 @@ type WireGuardClient interface {
|
||||
Device(name string) (*wgtypes.Device, error)
|
||||
ConfigureDevice(name string, cfg wgtypes.Config) error
|
||||
}
|
||||
|
||||
// WireGuardDevice carries the device and its backend options from the same read.
|
||||
type WireGuardDevice struct {
|
||||
*wgtypes.Device
|
||||
AmneziaWG domain.AmneziaWGOptions
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user