Skip to content

Commit

Permalink
[WIP] Fix setting for enabling and disabling the SPN module
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Jul 9, 2024
1 parent 6d83596 commit e9ae583
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 117 deletions.
6 changes: 3 additions & 3 deletions base/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ func (api *API) Manager() *mgr.Manager {

// Start starts the module.
func (api *API) Start() error {
if err := prep(); err != nil {
return err
}
if err := start(); err != nil {
return err
}
Expand Down Expand Up @@ -54,6 +51,9 @@ func New(instance instance) (*API, error) {
instance: instance,
}

if err := prep(); err != nil {
return nil, err
}
return module, nil
}

Expand Down
8 changes: 5 additions & 3 deletions base/config/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ func (u *Config) Manager() *mgr.Manager {

// Start starts the module.
func (u *Config) Start() error {
if err := prep(); err != nil {
return err
}
return start()
}

Expand All @@ -49,6 +46,11 @@ func New(instance instance) (*Config, error) {
instance: instance,
EventConfigChange: mgr.NewEventMgr[struct{}](ChangeEvent, m),
}

if err := prep(); err != nil {
return nil, err
}

return module, nil
}

Expand Down
6 changes: 3 additions & 3 deletions base/metrics/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ func (met *Metrics) Manager() *mgr.Manager {
}

func (met *Metrics) Start() error {
if err := prepConfig(); err != nil {
return err
}
return start()
}

Expand Down Expand Up @@ -202,6 +199,9 @@ func New(instance instance) (*Metrics, error) {
mgr: m,
instance: instance,
}
if err := prepConfig(); err != nil {
return nil, err
}

return module, nil
}
Expand Down
8 changes: 4 additions & 4 deletions service/firewall/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ func prep() error {
return false, err
})

if err := registerConfig(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -149,6 +145,10 @@ func New(instance instance) (*Firewall, error) {
return nil, err
}

if err := registerConfig(); err != nil {
return nil, err
}

return module, nil
}

Expand Down
152 changes: 80 additions & 72 deletions service/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,10 @@ type Instance struct {
rng *rng.Rng
base *base.Base

core *core.Core
updates *updates.Updates
geoip *geoip.GeoIP
netenv *netenv.NetEnv

access *access.Access
cabin *cabin.Cabin
navigator *navigator.Navigator
captain *captain.Captain
crew *crew.Crew
docks *docks.Docks
patrol *patrol.Patrol
ships *ships.Ships
sluice *sluice.SluiceModule
terminal *terminal.TerminalModule

core *core.Core
updates *updates.Updates
geoip *geoip.GeoIP
netenv *netenv.NetEnv
ui *ui.UI
profile *profile.ProfileModule
network *network.Network
Expand All @@ -89,6 +77,20 @@ type Instance struct {
process *process.ProcessModule
resolver *resolver.ResolverModule
sync *sync.Sync

access *access.Access

// SPN modules
SpnGroup *mgr.Group
cabin *cabin.Cabin
navigator *navigator.Navigator
captain *captain.Captain
crew *crew.Crew
docks *docks.Docks
patrol *patrol.Patrol
ships *ships.Ships
sluice *sluice.SluiceModule
terminal *terminal.TerminalModule
}

// New returns a new portmaster service instance.
Expand Down Expand Up @@ -134,7 +136,7 @@ func New(version string, svcCfg *ServiceConfig) (*Instance, error) {
return nil, fmt.Errorf("create base module: %w", err)
}

// Global service modules
// Service modules
instance.core, err = core.New(instance)
if err != nil {
return nil, fmt.Errorf("create core module: %w", err)
Expand All @@ -151,50 +153,6 @@ func New(version string, svcCfg *ServiceConfig) (*Instance, error) {
if err != nil {
return nil, fmt.Errorf("create netenv module: %w", err)
}

// SPN modules
instance.access, err = access.New(instance)
if err != nil {
return nil, fmt.Errorf("create access module: %w", err)
}
instance.cabin, err = cabin.New(instance)
if err != nil {
return nil, fmt.Errorf("create cabin module: %w", err)
}
instance.navigator, err = navigator.New(instance)
if err != nil {
return nil, fmt.Errorf("create navigator module: %w", err)
}
instance.captain, err = captain.New(instance, svcCfg.ShutdownFunc)
if err != nil {
return nil, fmt.Errorf("create captain module: %w", err)
}
instance.crew, err = crew.New(instance)
if err != nil {
return nil, fmt.Errorf("create crew module: %w", err)
}
instance.docks, err = docks.New(instance)
if err != nil {
return nil, fmt.Errorf("create docks module: %w", err)
}
instance.patrol, err = patrol.New(instance)
if err != nil {
return nil, fmt.Errorf("create patrol module: %w", err)
}
instance.ships, err = ships.New(instance)
if err != nil {
return nil, fmt.Errorf("create ships module: %w", err)
}
instance.sluice, err = sluice.New(instance)
if err != nil {
return nil, fmt.Errorf("create sluice module: %w", err)
}
instance.terminal, err = terminal.New(instance)
if err != nil {
return nil, fmt.Errorf("create terminal module: %w", err)
}

// Service modules
instance.ui, err = ui.New(instance)
if err != nil {
return nil, fmt.Errorf("create ui module: %w", err)
Expand Down Expand Up @@ -255,6 +213,48 @@ func New(version string, svcCfg *ServiceConfig) (*Instance, error) {
if err != nil {
return nil, fmt.Errorf("create sync module: %w", err)
}
instance.access, err = access.New(instance)
if err != nil {
return nil, fmt.Errorf("create access module: %w", err)
}

// SPN modules
instance.cabin, err = cabin.New(instance)
if err != nil {
return nil, fmt.Errorf("create cabin module: %w", err)
}
instance.navigator, err = navigator.New(instance)
if err != nil {
return nil, fmt.Errorf("create navigator module: %w", err)
}
instance.captain, err = captain.New(instance, svcCfg.ShutdownFunc)
if err != nil {
return nil, fmt.Errorf("create captain module: %w", err)
}
instance.crew, err = crew.New(instance)
if err != nil {
return nil, fmt.Errorf("create crew module: %w", err)
}
instance.docks, err = docks.New(instance)
if err != nil {
return nil, fmt.Errorf("create docks module: %w", err)
}
instance.patrol, err = patrol.New(instance)
if err != nil {
return nil, fmt.Errorf("create patrol module: %w", err)
}
instance.ships, err = ships.New(instance)
if err != nil {
return nil, fmt.Errorf("create ships module: %w", err)
}
instance.sluice, err = sluice.New(instance)
if err != nil {
return nil, fmt.Errorf("create sluice module: %w", err)
}
instance.terminal, err = terminal.New(instance)
if err != nil {
return nil, fmt.Errorf("create terminal module: %w", err)
}

// Add all modules to instance group.
instance.Group = mgr.NewGroup(
Expand All @@ -272,17 +272,6 @@ func New(version string, svcCfg *ServiceConfig) (*Instance, error) {
instance.geoip,
instance.netenv,

instance.access,
instance.cabin,
instance.navigator,
instance.captain,
instance.crew,
instance.docks,
instance.patrol,
instance.ships,
instance.sluice,
instance.terminal,

instance.ui,
instance.profile,
instance.network,
Expand All @@ -298,6 +287,20 @@ func New(version string, svcCfg *ServiceConfig) (*Instance, error) {
instance.process,
instance.resolver,
instance.sync,
instance.access,
)

// SPN Group
instance.SpnGroup = mgr.NewGroup(
instance.cabin,
instance.navigator,
instance.captain,
instance.crew,
instance.docks,
instance.patrol,
instance.ships,
instance.sluice,
instance.terminal,
)

// FIXME: call this before to trigger shutdown/restart event
Expand Down Expand Up @@ -502,6 +505,11 @@ func (i *Instance) Core() *core.Core {
return i.core
}

// SPNGroup returns the group of all SPN modules.
func (i *Instance) SPNGroup() *mgr.Group {
return i.SpnGroup
}

// Events
// SPN connected
func (i *Instance) GetEventSPNConnected() *mgr.EventMgr[struct{}] {
Expand Down
12 changes: 6 additions & 6 deletions service/intel/customlists/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ var (
func prep() error {
initFilterLists()

// Register the config in the ui.
err := registerConfig()
if err != nil {
return err
}

// Register api endpoint for updating the filter list.
if err := api.RegisterEndpoint(api.Endpoint{
Path: "customlists/update",
Expand Down Expand Up @@ -226,6 +220,12 @@ func New(instance instance) (*CustomList, error) {
States: mgr.NewStateMgr(m),
updateFilterListWorkerMgr: m.NewWorkerMgr("update custom filter list", checkAndUpdateFilterList, nil),
}
// Register the config in the ui.
err := registerConfig()
if err != nil {
return nil, err
}

return module, nil
}

Expand Down
7 changes: 4 additions & 3 deletions service/nameserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ func (ns *NameServer) Manager() *mgr.Manager {
}

func (ns *NameServer) Start() error {
if err := prep(); err != nil {
return err
}
return start()
}

Expand Down Expand Up @@ -325,6 +322,10 @@ func New(instance instance) (*NameServer, error) {

States: mgr.NewStateMgr(m),
}
if err := prep(); err != nil {
return nil, err
}

return module, nil
}

Expand Down
8 changes: 4 additions & 4 deletions service/process/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ func (pm *ProcessModule) Manager() *mgr.Manager {
}

func (pm *ProcessModule) Start() error {
if err := prep(); err != nil {
return err
}

return start()
}

Expand Down Expand Up @@ -68,6 +64,10 @@ func New(instance instance) (*ProcessModule, error) {
mgr: m,
instance: instance,
}

if err := prep(); err != nil {
return nil, err
}
return module, nil
}

Expand Down
8 changes: 4 additions & 4 deletions service/profile/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ func (pm *ProfileModule) Manager() *mgr.Manager {
}

func (pm *ProfileModule) Start() error {
if err := prep(); err != nil {
return err
}

return start()
}

Expand Down Expand Up @@ -151,6 +147,10 @@ func NewModule(instance instance) (*ProfileModule, error) {
States: mgr.NewStateMgr(m),
}

if err := prep(); err != nil {
return nil, err
}

return module, nil
}

Expand Down
4 changes: 0 additions & 4 deletions service/updates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ func init() {
}

func prep() error {
if err := registerConfig(); err != nil {
return err
}

// Check if update server URL supplied via flag is a valid URL.
if updateServerFromFlag != "" {
u, err := url.Parse(updateServerFromFlag)
Expand Down
Loading

0 comments on commit e9ae583

Please sign in to comment.