mirror of
https://github.com/h44z/wg-portal.git
synced 2025-10-04 15:36:18 +00:00
only set endpoint info for "responder" peers (#516)
This commit is contained in:
@@ -3,14 +3,13 @@ package wgcontroller
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"log/slog"
|
|
||||||
|
|
||||||
"github.com/h44z/wg-portal/internal/config"
|
"github.com/h44z/wg-portal/internal/config"
|
||||||
"github.com/h44z/wg-portal/internal/domain"
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
"github.com/h44z/wg-portal/internal/lowlevel"
|
"github.com/h44z/wg-portal/internal/lowlevel"
|
||||||
@@ -678,11 +677,15 @@ func (c *MikrotikController) updatePeer(
|
|||||||
extras := pp.GetExtras().(domain.MikrotikPeerExtras)
|
extras := pp.GetExtras().(domain.MikrotikPeerExtras)
|
||||||
peerId := extras.Id
|
peerId := extras.Id
|
||||||
|
|
||||||
endpoint := pp.Endpoint
|
endpoint := "" // by default, we have no endpoint (the peer does not initiate a connection)
|
||||||
endpointPort := "51820" // default port if not set
|
endpointPort := "0" // by default, we have no endpoint port (the peer does not initiate a connection)
|
||||||
if s := strings.Split(endpoint, ":"); len(s) == 2 {
|
if !extras.IsResponder { // if the peer is not only a responder, it needs the endpoint to initiate a connection
|
||||||
endpoint = s[0]
|
endpoint = pp.Endpoint
|
||||||
endpointPort = s[1]
|
endpointPort = "51820" // default port if not set
|
||||||
|
if s := strings.Split(endpoint, ":"); len(s) == 2 {
|
||||||
|
endpoint = s[0]
|
||||||
|
endpointPort = s[1]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
allowedAddressStr := domain.CidrsToString(pp.AllowedIPs)
|
allowedAddressStr := domain.CidrsToString(pp.AllowedIPs)
|
||||||
|
@@ -544,6 +544,30 @@ func (m Manager) saveInterface(ctx context.Context, iface *domain.Interface) (
|
|||||||
return nil, fmt.Errorf("failed to save interface: %w", err)
|
return nil, fmt.Errorf("failed to save interface: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update the interface type of peers in db
|
||||||
|
peers, err := m.db.GetInterfacePeers(ctx, iface.Identifier)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to load peers for interface %s: %w", iface.Identifier, err)
|
||||||
|
}
|
||||||
|
for _, peer := range peers {
|
||||||
|
err := m.db.SavePeer(ctx, peer.Identifier, func(_ *domain.Peer) (*domain.Peer, error) {
|
||||||
|
switch iface.Type {
|
||||||
|
case domain.InterfaceTypeAny:
|
||||||
|
peer.Interface.Type = domain.InterfaceTypeAny
|
||||||
|
case domain.InterfaceTypeClient:
|
||||||
|
peer.Interface.Type = domain.InterfaceTypeServer
|
||||||
|
case domain.InterfaceTypeServer:
|
||||||
|
peer.Interface.Type = domain.InterfaceTypeClient
|
||||||
|
}
|
||||||
|
|
||||||
|
return &peer, nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to update peer %s for interface %s: %w", peer.Identifier,
|
||||||
|
iface.Identifier, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if iface.IsDisabled() {
|
if iface.IsDisabled() {
|
||||||
physicalInterface, _ := m.wg.GetController(*iface).GetInterface(ctx, iface.Identifier)
|
physicalInterface, _ := m.wg.GetController(*iface).GetInterface(ctx, iface.Identifier)
|
||||||
fwMark := iface.FirewallMark
|
fwMark := iface.FirewallMark
|
||||||
|
@@ -328,7 +328,7 @@ func MergeToPhysicalPeer(pp *PhysicalPeer, p *Peer) {
|
|||||||
Id: "",
|
Id: "",
|
||||||
Name: p.DisplayName,
|
Name: p.DisplayName,
|
||||||
Comment: p.Notes,
|
Comment: p.Notes,
|
||||||
IsResponder: false,
|
IsResponder: p.Interface.Type == InterfaceTypeClient,
|
||||||
Disabled: p.IsDisabled(),
|
Disabled: p.IsDisabled(),
|
||||||
ClientEndpoint: p.Endpoint.GetValue(),
|
ClientEndpoint: p.Endpoint.GetValue(),
|
||||||
ClientAddress: CidrsToString(p.Interface.Addresses),
|
ClientAddress: CidrsToString(p.Interface.Addresses),
|
||||||
|
Reference in New Issue
Block a user