mirror of
https://github.com/h44z/wg-portal.git
synced 2025-10-04 15:36:18 +00:00
wip: create different backend handlers (#426)
This commit is contained in:
24
internal/domain/controller.go
Normal file
24
internal/domain/controller.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package domain
|
||||
|
||||
// ControllerType defines the type of controller used to manage interfaces.
|
||||
|
||||
const (
|
||||
ControllerTypeMikrotik = "mikrotik"
|
||||
ControllerTypeLocal = "wgctrl"
|
||||
)
|
||||
|
||||
// Controller extras can be used to store additional information available for specific controllers only.
|
||||
|
||||
type MikrotikInterfaceExtras struct {
|
||||
Comment string
|
||||
Disabled bool
|
||||
}
|
||||
|
||||
type MikrotikPeerExtras struct {
|
||||
Name string
|
||||
Comment string
|
||||
IsResponder bool
|
||||
ClientEndpoint string
|
||||
ClientAddress string
|
||||
Disabled bool
|
||||
}
|
@@ -208,9 +208,26 @@ type PhysicalInterface struct {
|
||||
|
||||
BytesUpload uint64
|
||||
BytesDownload uint64
|
||||
|
||||
backendExtras any // additional backend-specific extras, e.g., domain.MikrotikInterfaceExtras
|
||||
}
|
||||
|
||||
func (p *PhysicalInterface) GetExtras() any {
|
||||
return p.backendExtras
|
||||
}
|
||||
|
||||
func (p *PhysicalInterface) SetExtras(extras any) {
|
||||
switch extras.(type) {
|
||||
case MikrotikInterfaceExtras: // OK
|
||||
default: // we only support MikrotikInterfaceExtras for now
|
||||
panic(fmt.Sprintf("unsupported interface backend extras type %T", extras))
|
||||
}
|
||||
|
||||
p.backendExtras = extras
|
||||
}
|
||||
|
||||
func ConvertPhysicalInterface(pi *PhysicalInterface) *Interface {
|
||||
// create a new basic interface with the data from the physical interface
|
||||
iface := &Interface{
|
||||
Identifier: pi.Identifier,
|
||||
KeyPair: pi.KeyPair,
|
||||
@@ -245,6 +262,23 @@ func ConvertPhysicalInterface(pi *PhysicalInterface) *Interface {
|
||||
PeerDefPostDown: "",
|
||||
}
|
||||
|
||||
if pi.GetExtras() == nil {
|
||||
return iface
|
||||
}
|
||||
|
||||
// enrich the data with controller-specific extras
|
||||
now := time.Now()
|
||||
switch pi.ImportSource {
|
||||
case ControllerTypeMikrotik:
|
||||
extras := pi.GetExtras().(MikrotikInterfaceExtras)
|
||||
iface.DisplayName = extras.Comment
|
||||
if extras.Disabled {
|
||||
iface.Disabled = &now
|
||||
} else {
|
||||
iface.Disabled = nil
|
||||
}
|
||||
}
|
||||
|
||||
return iface
|
||||
}
|
||||
|
||||
|
@@ -129,7 +129,7 @@ func (p *Peer) GenerateDisplayName(prefix string) {
|
||||
p.DisplayName = fmt.Sprintf("%sPeer %s", prefix, internal.TruncateString(string(p.Identifier), 8))
|
||||
}
|
||||
|
||||
// OverwriteUserEditableFields overwrites the user editable fields of the peer with the values from the userPeer
|
||||
// OverwriteUserEditableFields overwrites the user-editable fields of the peer with the values from the userPeer
|
||||
func (p *Peer) OverwriteUserEditableFields(userPeer *Peer, cfg *config.Config) {
|
||||
p.DisplayName = userPeer.DisplayName
|
||||
if cfg.Core.EditableKeys {
|
||||
@@ -182,10 +182,11 @@ type PhysicalPeer struct {
|
||||
BytesUpload uint64 // upload bytes are the number of bytes that the remote peer has sent to the server
|
||||
BytesDownload uint64 // upload bytes are the number of bytes that the remote peer has received from the server
|
||||
|
||||
BackendExtras map[string]any // additional backend specific extras, e.g. for the mikrotik backend this contains the name of the peer
|
||||
ImportSource string // import source (wgctrl, file, ...)
|
||||
backendExtras any // additional backend-specific extras, e.g., domain.MikrotikPeerExtras
|
||||
}
|
||||
|
||||
func (p PhysicalPeer) GetPresharedKey() *wgtypes.Key {
|
||||
func (p *PhysicalPeer) GetPresharedKey() *wgtypes.Key {
|
||||
if p.PresharedKey == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -197,7 +198,7 @@ func (p PhysicalPeer) GetPresharedKey() *wgtypes.Key {
|
||||
return &key
|
||||
}
|
||||
|
||||
func (p PhysicalPeer) GetEndpointAddress() *net.UDPAddr {
|
||||
func (p *PhysicalPeer) GetEndpointAddress() *net.UDPAddr {
|
||||
if p.Endpoint == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -209,7 +210,7 @@ func (p PhysicalPeer) GetEndpointAddress() *net.UDPAddr {
|
||||
return addr
|
||||
}
|
||||
|
||||
func (p PhysicalPeer) GetPersistentKeepaliveTime() *time.Duration {
|
||||
func (p *PhysicalPeer) GetPersistentKeepaliveTime() *time.Duration {
|
||||
if p.PersistentKeepalive == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -218,7 +219,7 @@ func (p PhysicalPeer) GetPersistentKeepaliveTime() *time.Duration {
|
||||
return &keepAliveDuration
|
||||
}
|
||||
|
||||
func (p PhysicalPeer) GetAllowedIPs() []net.IPNet {
|
||||
func (p *PhysicalPeer) GetAllowedIPs() []net.IPNet {
|
||||
allowedIPs := make([]net.IPNet, len(p.AllowedIPs))
|
||||
for i, ip := range p.AllowedIPs {
|
||||
allowedIPs[i] = *ip.IpNet()
|
||||
@@ -227,6 +228,20 @@ func (p PhysicalPeer) GetAllowedIPs() []net.IPNet {
|
||||
return allowedIPs
|
||||
}
|
||||
|
||||
func (p *PhysicalPeer) GetExtras() any {
|
||||
return p.backendExtras
|
||||
}
|
||||
|
||||
func (p *PhysicalPeer) SetExtras(extras any) {
|
||||
switch extras.(type) {
|
||||
case MikrotikPeerExtras: // OK
|
||||
default: // we only support MikrotikPeerExtras for now
|
||||
panic(fmt.Sprintf("unsupported peer backend extras type %T", extras))
|
||||
}
|
||||
|
||||
p.backendExtras = extras
|
||||
}
|
||||
|
||||
func ConvertPhysicalPeer(pp *PhysicalPeer) *Peer {
|
||||
peer := &Peer{
|
||||
Endpoint: NewConfigOption(pp.Endpoint, true),
|
||||
@@ -245,6 +260,27 @@ func ConvertPhysicalPeer(pp *PhysicalPeer) *Peer {
|
||||
},
|
||||
}
|
||||
|
||||
if pp.GetExtras() == nil {
|
||||
return peer
|
||||
}
|
||||
|
||||
// enrich the data with controller-specific extras
|
||||
now := time.Now()
|
||||
switch pp.ImportSource {
|
||||
case ControllerTypeMikrotik:
|
||||
extras := pp.GetExtras().(MikrotikPeerExtras)
|
||||
peer.Notes = extras.Comment
|
||||
peer.DisplayName = extras.Name
|
||||
peer.Endpoint = NewConfigOption(extras.ClientEndpoint, true)
|
||||
if extras.Disabled {
|
||||
peer.Disabled = &now
|
||||
peer.DisabledReason = "Disabled by Mikrotik controller"
|
||||
} else {
|
||||
peer.Disabled = nil
|
||||
peer.DisabledReason = ""
|
||||
}
|
||||
}
|
||||
|
||||
return peer
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user