wip: implement mikrotik rest api client (#426)

This commit is contained in:
Christoph Haas
2025-05-31 17:17:08 +02:00
parent d5ce889e4f
commit e934232e0b
8 changed files with 574 additions and 23 deletions

View File

@@ -13,6 +13,7 @@ import (
)
type InterfaceController interface {
GetId() domain.InterfaceBackend
GetInterfaces(_ context.Context) ([]domain.PhysicalInterface, error)
GetInterface(_ context.Context, id domain.InterfaceIdentifier) (*domain.PhysicalInterface, error)
GetPeers(_ context.Context, deviceId domain.InterfaceIdentifier) ([]domain.PhysicalPeer, error)
@@ -92,7 +93,7 @@ func (c *ControllerManager) registerMikrotikControllers() error {
continue
}
controller, err := wgcontroller.NewMikrotikController() // TODO: Pass backendConfig to the controller constructor
controller, err := wgcontroller.NewMikrotikController(c.cfg, &backendConfig)
if err != nil {
return fmt.Errorf("failed to create Mikrotik controller for backend %s: %w", backendConfig.Id, err)
}

View File

@@ -149,7 +149,7 @@ func (m Manager) ImportNewInterfaces(ctx context.Context, filter ...domain.Inter
return 0, err
}
err = m.importInterface(ctx, &physicalInterface, physicalPeers)
err = m.importInterface(ctx, wgBackend, &physicalInterface, physicalPeers)
if err != nil {
return 0, fmt.Errorf("import of %s failed: %w", physicalInterface.Identifier, err)
}
@@ -770,7 +770,12 @@ func (m Manager) getFreshListenPort(ctx context.Context) (port int, err error) {
return
}
func (m Manager) importInterface(ctx context.Context, in *domain.PhysicalInterface, peers []domain.PhysicalPeer) error {
func (m Manager) importInterface(
ctx context.Context,
backend InterfaceController,
in *domain.PhysicalInterface,
peers []domain.PhysicalPeer,
) error {
now := time.Now()
iface := domain.ConvertPhysicalInterface(in)
iface.BaseModel = domain.BaseModel{
@@ -779,6 +784,7 @@ func (m Manager) importInterface(ctx context.Context, in *domain.PhysicalInterfa
CreatedAt: now,
UpdatedAt: now,
}
iface.Backend = backend.GetId()
iface.PeerDefAllowedIPsStr = iface.AddressStr()
existingInterface, err := m.db.GetInterface(ctx, iface.Identifier)
@@ -843,6 +849,18 @@ func (m Manager) importPeer(ctx context.Context, in *domain.Interface, p *domain
peer.DisplayName = "Autodetected Client (" + peer.Interface.PublicKey[0:8] + ")"
}
if p.BackendExtras != nil {
if val, ok := p.BackendExtras["MT-NAME"]; ok {
peer.DisplayName = val.(string)
}
if val, ok := p.BackendExtras["MT-COMMENT"]; ok {
peer.Notes = val.(string)
}
if val, ok := p.BackendExtras["MT-ENDPOINT"]; ok {
peer.Endpoint = domain.NewConfigOption(val.(string), true)
}
}
err := m.db.SavePeer(ctx, peer.Identifier, func(_ *domain.Peer) (*domain.Peer, error) {
return peer, nil
})