automatically append listening port to endpoint address (#352)

This commit is contained in:
Christoph Haas 2025-01-26 09:52:09 +01:00
parent d35889de73
commit 1b8cdc3417
4 changed files with 25 additions and 4 deletions

View File

@ -354,7 +354,7 @@
"endpoint": { "endpoint": {
"label": "Endpoint Address", "label": "Endpoint Address",
"placeholder": "Endpoint Address", "placeholder": "Endpoint Address",
"description": "The endpoint address that peers will connect to." "description": "The endpoint address that peers will connect to. (e.g. wg.example.com or wg.example.com:51820)"
}, },
"networks": { "networks": {
"label": "IP Networks", "label": "IP Networks",

View File

@ -355,7 +355,7 @@
"endpoint": { "endpoint": {
"label": "Endpoint Address", "label": "Endpoint Address",
"placeholder": "Endpoint Address", "placeholder": "Endpoint Address",
"description": "The endpoint address that peers will connect to." "description": "The endpoint address that peers will connect to. (e.g. wg.example.com or wg.example.com:51820)"
}, },
"networks": { "networks": {
"label": "IP Networks", "label": "IP Networks",

View File

@ -456,6 +456,10 @@ func (m Manager) saveInterface(ctx context.Context, iface *domain.Interface) (
*domain.Interface, *domain.Interface,
error, error,
) { ) {
if err := iface.Validate(); err != nil {
return nil, fmt.Errorf("interface validation failed: %w", err)
}
stateChanged := m.hasInterfaceStateChanged(ctx, iface) stateChanged := m.hasInterfaceStateChanged(ctx, iface)
if err := m.handleInterfacePreSaveHooks(stateChanged, iface); err != nil { if err := m.handleInterfacePreSaveHooks(stateChanged, iface); err != nil {

View File

@ -3,6 +3,7 @@ package domain
import ( import (
"fmt" "fmt"
"math" "math"
"net"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -71,8 +72,24 @@ type Interface struct {
PeerDefPostDown string // default action that is executed after the device is down PeerDefPostDown string // default action that is executed after the device is down
} }
func (i *Interface) IsValid() bool { // Validate performs checks to ensure that the interface is valid.
return true // TODO: implement check func (i *Interface) Validate() error {
// validate peer default endpoint, add port if needed
if i.PeerDefEndpoint != "" {
host, port, err := net.SplitHostPort(i.PeerDefEndpoint)
switch {
case err != nil && !strings.Contains(err.Error(), "missing port in address"):
return fmt.Errorf("invalid default endpoint: %w", err)
case err != nil && strings.Contains(err.Error(), "missing port in address"):
// In this case, the entire string is the host, and there's no port.
host = i.PeerDefEndpoint
port = strconv.Itoa(i.ListenPort)
}
i.PeerDefEndpoint = net.JoinHostPort(host, port)
}
return nil
} }
func (i *Interface) IsDisabled() bool { func (i *Interface) IsDisabled() bool {