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:
121
base/api/client/websocket.go
Normal file
121
base/api/client/websocket.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/tevino/abool"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
)
|
||||
|
||||
type wsState struct {
|
||||
wsConn *websocket.Conn
|
||||
wg sync.WaitGroup
|
||||
failing *abool.AtomicBool
|
||||
failSignal chan struct{}
|
||||
}
|
||||
|
||||
func (c *Client) wsConnect() error {
|
||||
state := &wsState{
|
||||
failing: abool.NewBool(false),
|
||||
failSignal: make(chan struct{}),
|
||||
}
|
||||
|
||||
var err error
|
||||
state.wsConn, _, err = websocket.DefaultDialer.Dial(fmt.Sprintf("ws://%s/api/database/v1", c.server), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.signalOnline()
|
||||
|
||||
state.wg.Add(2)
|
||||
go c.wsReader(state)
|
||||
go c.wsWriter(state)
|
||||
|
||||
// wait for end of connection
|
||||
select {
|
||||
case <-state.failSignal:
|
||||
case <-c.shutdownSignal:
|
||||
state.Error("")
|
||||
}
|
||||
_ = state.wsConn.Close()
|
||||
state.wg.Wait()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) wsReader(state *wsState) {
|
||||
defer state.wg.Done()
|
||||
for {
|
||||
_, data, err := state.wsConn.ReadMessage()
|
||||
log.Tracef("client: read message")
|
||||
if err != nil {
|
||||
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
|
||||
state.Error(fmt.Sprintf("client: read error: %s", err))
|
||||
} else {
|
||||
state.Error("client: connection closed by server")
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Tracef("client: received message: %s", string(data))
|
||||
m, err := ParseMessage(data)
|
||||
if err != nil {
|
||||
log.Warningf("client: failed to parse message: %s", err)
|
||||
} else {
|
||||
select {
|
||||
case c.recv <- m:
|
||||
case <-state.failSignal:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) wsWriter(state *wsState) {
|
||||
defer state.wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-state.failSignal:
|
||||
return
|
||||
case m := <-c.resend:
|
||||
data, err := m.Pack()
|
||||
if err == nil {
|
||||
err = state.wsConn.WriteMessage(websocket.BinaryMessage, data)
|
||||
}
|
||||
if err != nil {
|
||||
state.Error(fmt.Sprintf("client: write error: %s", err))
|
||||
return
|
||||
}
|
||||
log.Tracef("client: sent message: %s", string(data))
|
||||
if m.sent != nil {
|
||||
m.sent.Set()
|
||||
}
|
||||
case m := <-c.send:
|
||||
data, err := m.Pack()
|
||||
if err == nil {
|
||||
err = state.wsConn.WriteMessage(websocket.BinaryMessage, data)
|
||||
}
|
||||
if err != nil {
|
||||
c.resend <- m
|
||||
state.Error(fmt.Sprintf("client: write error: %s", err))
|
||||
return
|
||||
}
|
||||
log.Tracef("client: sent message: %s", string(data))
|
||||
if m.sent != nil {
|
||||
m.sent.Set()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (state *wsState) Error(message string) {
|
||||
if state.failing.SetToIf(false, true) {
|
||||
close(state.failSignal)
|
||||
if message != "" {
|
||||
log.Warning(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user