safing-portmaster/service/core/base/module.go
2024-11-27 16:16:15 +01:00

67 lines
1.2 KiB
Go

package base
import (
"errors"
"sync/atomic"
"github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/service/mgr"
)
// DefaultAPIListenAddress is the default listen address for the API.
var DefaultAPIListenAddress = "127.0.0.1:817"
// Base is the base module.
type Base struct {
mgr *mgr.Manager
instance instance
}
// Manager returns the module manager.
func (b *Base) Manager() *mgr.Manager {
return b.mgr
}
// Start starts the module.
func (b *Base) Start() error {
startProfiling()
registerLogCleaner()
return nil
}
// Stop stops the module.
func (b *Base) Stop() error {
return nil
}
var (
module *Base
shimLoaded atomic.Bool
)
// New returns a new Base module.
func New(instance instance) (*Base, error) {
if !shimLoaded.CompareAndSwap(false, true) {
return nil, errors.New("only one instance allowed")
}
m := mgr.New("Base")
module = &Base{
mgr: m,
instance: instance,
}
// Set api listen address.
api.SetDefaultAPIListenAddress(DefaultAPIListenAddress)
if err := registerDatabases(); err != nil {
return nil, err
}
return module, nil
}
type instance interface {
DataDir() string
SetCmdLineOperation(f func() error)
}