mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 19:56:06 +01:00
cleanup
This commit is contained in:
@@ -95,17 +95,3 @@ func (c AmneziaWGController) SaveInterface(ctx context.Context, id domain.Interf
|
||||
}
|
||||
return c.updateLowLevelInterface(iface)
|
||||
}
|
||||
|
||||
func (c AmneziaWGController) DeleteInterface(_ context.Context, id domain.InterfaceIdentifier) error {
|
||||
link, err := c.nl.LinkByName(string(id))
|
||||
if err != nil {
|
||||
if _, ok := errors.AsType[netlink.LinkNotFoundError](err); ok {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if link.Type() != "amneziawg" {
|
||||
return fmt.Errorf("interface %s is not an AmneziaWG kernel device", id)
|
||||
}
|
||||
return c.nl.LinkDel(link)
|
||||
}
|
||||
|
||||
@@ -404,6 +404,9 @@ func (c LocalController) deleteLowLevelInterface(id domain.InterfaceIdentifier)
|
||||
return fmt.Errorf("unable to find low level interface: %w", err)
|
||||
}
|
||||
|
||||
if c.importSource == domain.ControllerTypeAmneziaWG && link.Type() != "amneziawg" {
|
||||
return fmt.Errorf("interface %s is not an AmneziaWG kernel device", id)
|
||||
}
|
||||
err = c.nl.LinkDel(link)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete low level interface: %w", err)
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
|
||||
# Core settings
|
||||
PrivateKey = {{ .Interface.KeyPair.PrivateKey }}
|
||||
{{- if .Interface.AmneziaWG }}
|
||||
{{ .Interface.AmneziaWG.ConfigLines -}}
|
||||
{{- end }}
|
||||
{{- with .Interface.AmneziaWG }}
|
||||
{{ range $key, $value := . }}{{ $key }} = {{ $value }}
|
||||
{{ end }}{{ end }}
|
||||
Address = {{ CidrsToString .Interface.Addresses }}
|
||||
|
||||
# Misc. settings (optional)
|
||||
|
||||
@@ -23,9 +23,9 @@
|
||||
|
||||
# Core settings
|
||||
PrivateKey = {{ .Peer.Interface.KeyPair.PrivateKey }}
|
||||
{{- if .Peer.Interface.AmneziaWG }}
|
||||
{{ .Peer.Interface.AmneziaWG.ConfigLines -}}
|
||||
{{- end }}
|
||||
{{- with .Peer.Interface.AmneziaWG }}
|
||||
{{ range $key, $value := . }}{{ $key }} = {{ $value }}
|
||||
{{ end }}{{ end }}
|
||||
{{- if eq .Style "wgquick"}}
|
||||
Address = {{ CidrsToString .Peer.Interface.Addresses }}
|
||||
{{- end}}
|
||||
|
||||
@@ -2,8 +2,6 @@ package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -69,12 +67,3 @@ func ParseAmneziaWGRange(value string, bits int) (uint64, uint64, error) {
|
||||
}
|
||||
return lo, hi, nil
|
||||
}
|
||||
|
||||
// ConfigLines is deterministic so saved files and QR codes use the same options.
|
||||
func (o AmneziaWGOptions) ConfigLines() string {
|
||||
var result strings.Builder
|
||||
for _, key := range slices.Sorted(maps.Keys(o)) {
|
||||
_, _ = fmt.Fprintf(&result, "%s = %s\n", key, o[key])
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
|
||||
@@ -182,19 +182,10 @@ func encodeAmneziaWGSockaddr(endpoint *net.UDPAddr) ([]byte, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Validate lengths before using netlink's attribute decoder, including padding
|
||||
// that would otherwise let a truncated attribute panic during decoding.
|
||||
// With an aligned buffer, a valid attribute and its padding both fit.
|
||||
func amneziaWGAttrs(data []byte) ([]syscall.NetlinkRouteAttr, error) {
|
||||
for rest := data; len(rest) != 0; {
|
||||
if len(rest) < unix.SizeofRtAttr {
|
||||
return nil, fmt.Errorf("truncated AmneziaWG attribute")
|
||||
}
|
||||
length := int(nl.NativeEndian().Uint16(rest))
|
||||
padded := (length + 3) &^ 3
|
||||
if length < unix.SizeofRtAttr || padded > len(rest) {
|
||||
return nil, fmt.Errorf("invalid AmneziaWG attribute length")
|
||||
}
|
||||
rest = rest[padded:]
|
||||
if len(data)%unix.RTA_ALIGNTO != 0 {
|
||||
return nil, fmt.Errorf("unaligned AmneziaWG attributes")
|
||||
}
|
||||
return nl.ParseRouteAttr(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user