Restructure modules (#1572)

* Move portbase into monorepo

* Add new simple module mgr

* [WIP] Switch to new simple module mgr

* Add StateMgr and more worker variants

* [WIP] Switch more modules

* [WIP] Switch more modules

* [WIP] swtich more modules

* [WIP] switch all SPN modules

* [WIP] switch all service modules

* [WIP] Convert all workers to the new module system

* [WIP] add new task system to module manager

* [WIP] Add second take for scheduling workers

* [WIP] Add FIXME for bugs in new scheduler

* [WIP] Add minor improvements to scheduler

* [WIP] Add new worker scheduler

* [WIP] Fix more bug related to new module system

* [WIP] Fix start handing of the new module system

* [WIP] Improve startup process

* [WIP] Fix minor issues

* [WIP] Fix missing subsystem in settings

* [WIP] Initialize managers in constructor

* [WIP] Move module event initialization to constrictors

* [WIP] Fix setting for enabling and disabling the SPN module

* [WIP] Move API registeration into module construction

* [WIP] Update states mgr for all modules

* [WIP] Add CmdLine operation support

* Add state helper methods to module group and instance

* Add notification and module status handling to status package

* Fix starting issues

* Remove pilot widget and update security lock to new status data

* Remove debug logs

* Improve http server shutdown

* Add workaround for cleanly shutting down firewall+netquery

* Improve logging

* Add syncing states with notifications for new module system

* Improve starting, stopping, shutdown; resolve FIXMEs/TODOs

* [WIP] Fix most unit tests

* Review new module system and fix minor issues

* Push shutdown and restart events again via API

* Set sleep mode via interface

* Update example/template module

* [WIP] Fix spn/cabin unit test

* Remove deprecated UI elements

* Make log output more similar for the logging transition phase

* Switch spn hub and observer cmds to new module system

* Fix log sources

* Make worker mgr less error prone

* Fix tests and minor issues

* Fix observation hub

* Improve shutdown and restart handling

* Split up big connection.go source file

* Move varint and dsd packages to structures repo

* Improve expansion test

* Fix linter warnings

* Fix interception module on windows

* Fix linter errors

---------

Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
This commit is contained in:
Daniel Hååvi
2024-08-09 17:15:48 +02:00
committed by GitHub
parent 10a77498f4
commit 80664d1a27
647 changed files with 37690 additions and 3366 deletions

View File

@@ -1,31 +1,45 @@
package sluice
import (
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"errors"
"sync/atomic"
"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/netenv"
"github.com/safing/portmaster/spn/conf"
)
var (
module *modules.Module
type SluiceModule struct {
mgr *mgr.Manager
instance instance
}
func (s *SluiceModule) Manager() *mgr.Manager {
return s.mgr
}
func (s *SluiceModule) Start() error {
return start()
}
func (s *SluiceModule) Stop() error {
return stop()
}
var (
entrypointInfoMsg = []byte("You have reached the local SPN entry port, but your connection could not be matched to an SPN tunnel.\n")
// EnableListener indicates if it should start the sluice listeners. Must be set at startup.
EnableListener bool = true
)
func init() {
module = modules.Register("sluice", nil, start, stop, "terminal")
}
func start() error {
// TODO:
// Listening on all interfaces for now, as we need this for Windows.
// Handle similarly to the nameserver listener.
if conf.Client() && EnableListener {
if conf.Integrated() && EnableListener {
StartSluice("tcp4", "0.0.0.0:717")
StartSluice("udp4", "0.0.0.0:717")
@@ -44,3 +58,23 @@ func stop() error {
stopAllSluices()
return nil
}
var (
module *SluiceModule
shimLoaded atomic.Bool
)
// New returns a new Config module.
func New(instance instance) (*SluiceModule, error) {
if !shimLoaded.CompareAndSwap(false, true) {
return nil, errors.New("only one instance allowed")
}
m := mgr.New("SluiceModule")
module = &SluiceModule{
mgr: m,
instance: instance,
}
return module, nil
}
type instance interface{}

View File

@@ -1,7 +1,6 @@
package sluice
import (
"context"
"io"
"net"
"sync"
@@ -9,6 +8,8 @@ import (
"time"
"github.com/tevino/abool"
"github.com/safing/portmaster/service/mgr"
)
// PacketListener is a listener for packet based protocols.
@@ -37,8 +38,8 @@ func ListenPacket(network, address string) (net.Listener, error) {
newConns: make(chan *PacketConn),
conns: make(map[string]*PacketConn),
}
module.StartServiceWorker("packet listener reader", 0, ln.reader)
module.StartServiceWorker("packet listener cleaner", time.Minute, ln.cleaner)
module.mgr.Go("packet listener reader", ln.reader)
module.mgr.Go("packet listener cleaner", ln.cleaner)
return ln, nil
}
@@ -99,7 +100,7 @@ func (ln *PacketListener) setConn(conn *PacketConn) {
ln.conns[conn.addr.String()] = conn
}
func (ln *PacketListener) reader(_ context.Context) error {
func (ln *PacketListener) reader(_ *mgr.WorkerCtx) error {
for {
// Read data from connection.
buf := make([]byte, 512)
@@ -145,7 +146,7 @@ func (ln *PacketListener) reader(_ context.Context) error {
}
}
func (ln *PacketListener) cleaner(ctx context.Context) error {
func (ln *PacketListener) cleaner(ctx *mgr.WorkerCtx) error {
for {
select {
case <-time.After(1 * time.Minute):

View File

@@ -1,14 +1,14 @@
package sluice
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"time"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/netenv"
)
@@ -46,9 +46,8 @@ func StartSluice(network, address string) {
}
// Start service worker.
module.StartServiceWorker(
module.mgr.Go(
s.network+" sluice listener",
10*time.Second,
s.listenHandler,
)
}
@@ -189,7 +188,7 @@ func (s *Sluice) handleConnection(conn net.Conn) {
success = true
}
func (s *Sluice) listenHandler(_ context.Context) error {
func (s *Sluice) listenHandler(_ *mgr.WorkerCtx) error {
defer s.abandon()
err := s.init()
if err != nil {
@@ -201,7 +200,7 @@ func (s *Sluice) listenHandler(_ context.Context) error {
for {
conn, err := s.listener.Accept()
if err != nil {
if module.IsStopping() {
if module.mgr.IsDone() {
return nil
}
return fmt.Errorf("failed to accept connection: %w", err)

View File

@@ -1,7 +1,6 @@
package sluice
import (
"context"
"io"
"net"
"runtime"
@@ -12,6 +11,8 @@ import (
"github.com/tevino/abool"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"github.com/safing/portmaster/service/mgr"
)
const onWindows = runtime.GOOS == "windows"
@@ -64,8 +65,8 @@ func ListenUDP(network, address string) (net.Listener, error) {
}
// Start workers.
module.StartServiceWorker("udp listener reader", 0, ln.reader)
module.StartServiceWorker("udp listener cleaner", time.Minute, ln.cleaner)
module.mgr.Go("udp listener reader", ln.reader)
module.mgr.Go("udp listener cleaner", ln.cleaner)
return ln, nil
}
@@ -126,7 +127,7 @@ func (ln *UDPListener) setConn(conn *UDPConn) {
ln.conns[conn.addr.String()] = conn
}
func (ln *UDPListener) reader(_ context.Context) error {
func (ln *UDPListener) reader(_ *mgr.WorkerCtx) error {
for {
// TODO: Find good buf size.
// With a buf size of 512 we have seen this error on Windows:
@@ -180,7 +181,7 @@ func (ln *UDPListener) reader(_ context.Context) error {
}
}
func (ln *UDPListener) cleaner(ctx context.Context) error {
func (ln *UDPListener) cleaner(ctx *mgr.WorkerCtx) error {
for {
select {
case <-time.After(1 * time.Minute):