mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 11:46:09 +01:00
81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
)
|
|
|
|
// AmneziaWGOptions contains [Interface] options understood by the official awg tool.
|
|
// See: https://docs.amnezia.org/documentation/amnezia-wg#configuration-parameters
|
|
type AmneziaWGOptions map[string]string
|
|
|
|
// Validate checks the custom parameters against the official limits.
|
|
func (o AmneziaWGOptions) Validate() error {
|
|
for key, value := range o {
|
|
if strings.ContainsAny(value, "\r\n\x00#") || len(value) > 4096 {
|
|
return fmt.Errorf("invalid AmneziaWG option %s: %w", key, ErrInvalidData)
|
|
}
|
|
var err error
|
|
switch key {
|
|
case "Jc", "Jmin", "Jmax", "S1", "S2", "S3", "S4":
|
|
_, err = strconv.ParseUint(value, 10, 16)
|
|
case "H1", "H2", "H3", "H4":
|
|
_, _, err = ParseAmneziaWGRange(value, 32)
|
|
case "ContentPaddingAddition", "RekeyAfterTime", "RekeyTimeout", "RejectAfterTime", "KeepaliveTimeout", "MaxHandshakeAttempts":
|
|
_, _, err = ParseAmneziaWGRange(value, 16)
|
|
case "I1", "I2", "I3", "I4", "I5":
|
|
// The kernel validates the CPS description; it must stay on one line.
|
|
if value == "" {
|
|
err = fmt.Errorf("empty CPS description")
|
|
}
|
|
case "HeaderProtectionKey":
|
|
_, err = wgtypes.ParseKey(value)
|
|
case "RandomTrailers", "DisableCookies":
|
|
if value != "on" && value != "off" && value != "0" && value != "1" {
|
|
err = fmt.Errorf("expected on/off or 0/1")
|
|
}
|
|
default:
|
|
return fmt.Errorf("unknown AmneziaWG option %q: %w", key, ErrInvalidData)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("invalid AmneziaWG option %s: %w", key, ErrInvalidData)
|
|
}
|
|
}
|
|
if min, max := o["Jmin"], o["Jmax"]; min != "" && max != "" {
|
|
lo, _ := strconv.ParseUint(min, 10, 16)
|
|
hi, _ := strconv.ParseUint(max, 10, 16)
|
|
if lo > hi {
|
|
return fmt.Errorf("AmneziaWG Jmin exceeds Jmax: %w", ErrInvalidData)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ParseAmneziaWGRange parses a single unsigned value or an inclusive low-high range.
|
|
func ParseAmneziaWGRange(value string, bits int) (uint64, uint64, error) {
|
|
low, high, hasRange := strings.Cut(value, "-")
|
|
lo, err := strconv.ParseUint(low, 10, bits)
|
|
if err != nil || !hasRange {
|
|
return lo, lo, err
|
|
}
|
|
hi, err := strconv.ParseUint(high, 10, bits)
|
|
if err != nil || lo > hi {
|
|
return 0, 0, fmt.Errorf("invalid range")
|
|
}
|
|
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()
|
|
}
|