Skip to content

Commit

Permalink
automatically append listening port to endpoint address (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
h44z committed Jan 26, 2025
1 parent d35889d commit 1b8cdc3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion frontend/src/lang/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
"endpoint": {
"label": "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": {
"label": "IP Networks",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lang/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@
"endpoint": {
"label": "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": {
"label": "IP Networks",
Expand Down
4 changes: 4 additions & 0 deletions internal/app/wireguard/wireguard_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ func (m Manager) saveInterface(ctx context.Context, iface *domain.Interface) (
*domain.Interface,
error,
) {
if err := iface.Validate(); err != nil {
return nil, fmt.Errorf("interface validation failed: %w", err)
}

stateChanged := m.hasInterfaceStateChanged(ctx, iface)

if err := m.handleInterfacePreSaveHooks(stateChanged, iface); err != nil {
Expand Down
21 changes: 19 additions & 2 deletions internal/domain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"fmt"
"math"
"net"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -71,8 +72,24 @@ type Interface struct {
PeerDefPostDown string // default action that is executed after the device is down
}

func (i *Interface) IsValid() bool {
return true // TODO: implement check
// Validate performs checks to ensure that the interface is valid.
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 {
Expand Down

0 comments on commit 1b8cdc3

Please sign in to comment.