mirror of
https://github.com/safing/portmaster
synced 2025-04-23 12:29:10 +00:00
* 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>
149 lines
3.7 KiB
Go
149 lines
3.7 KiB
Go
package crew
|
|
|
|
import (
|
|
"crypto/subtle"
|
|
"time"
|
|
|
|
"github.com/safing/portmaster/base/rng"
|
|
"github.com/safing/portmaster/spn/terminal"
|
|
"github.com/safing/structures/container"
|
|
"github.com/safing/structures/dsd"
|
|
)
|
|
|
|
const (
|
|
// PingOpType is the type ID of the latency test operation.
|
|
PingOpType = "ping"
|
|
|
|
pingOpNonceSize = 16
|
|
pingOpTimeout = 3 * time.Second
|
|
)
|
|
|
|
// PingOp is used to measure latency.
|
|
type PingOp struct {
|
|
terminal.OneOffOperationBase
|
|
|
|
started time.Time
|
|
nonce []byte
|
|
}
|
|
|
|
// PingOpRequest is a ping request.
|
|
type PingOpRequest struct {
|
|
Nonce []byte `json:"n,omitempty"`
|
|
}
|
|
|
|
// PingOpResponse is a ping response.
|
|
type PingOpResponse struct {
|
|
Nonce []byte `json:"n,omitempty"`
|
|
Time time.Time `json:"t,omitempty"`
|
|
}
|
|
|
|
// Type returns the type ID.
|
|
func (op *PingOp) Type() string {
|
|
return PingOpType
|
|
}
|
|
|
|
func init() {
|
|
terminal.RegisterOpType(terminal.OperationFactory{
|
|
Type: PingOpType,
|
|
Start: startPingOp,
|
|
})
|
|
}
|
|
|
|
// NewPingOp runs a latency test.
|
|
func NewPingOp(t terminal.Terminal) (*PingOp, *terminal.Error) {
|
|
// Generate nonce.
|
|
nonce, err := rng.Bytes(pingOpNonceSize)
|
|
if err != nil {
|
|
return nil, terminal.ErrInternalError.With("failed to generate ping nonce: %w", err)
|
|
}
|
|
|
|
// Create operation and init.
|
|
op := &PingOp{
|
|
started: time.Now().UTC(),
|
|
nonce: nonce,
|
|
}
|
|
op.OneOffOperationBase.Init()
|
|
|
|
// Create request.
|
|
pingRequest, err := dsd.Dump(&PingOpRequest{
|
|
Nonce: op.nonce,
|
|
}, dsd.CBOR)
|
|
if err != nil {
|
|
return nil, terminal.ErrInternalError.With("failed to create ping request: %w", err)
|
|
}
|
|
|
|
// Send ping.
|
|
tErr := t.StartOperation(op, container.New(pingRequest), pingOpTimeout)
|
|
if tErr != nil {
|
|
return nil, tErr
|
|
}
|
|
|
|
return op, nil
|
|
}
|
|
|
|
// Deliver delivers a message to the operation.
|
|
func (op *PingOp) Deliver(msg *terminal.Msg) *terminal.Error {
|
|
defer msg.Finish()
|
|
|
|
// Parse response.
|
|
response := &PingOpResponse{}
|
|
_, err := dsd.Load(msg.Data.CompileData(), response)
|
|
if err != nil {
|
|
return terminal.ErrMalformedData.With("failed to parse ping response: %w", err)
|
|
}
|
|
|
|
// Check if the nonce matches.
|
|
if subtle.ConstantTimeCompare(op.nonce, response.Nonce) != 1 {
|
|
return terminal.ErrIntegrity.With("ping nonce mismatched")
|
|
}
|
|
|
|
return terminal.ErrExplicitAck
|
|
}
|
|
|
|
func startPingOp(t terminal.Terminal, opID uint32, data *container.Container) (terminal.Operation, *terminal.Error) {
|
|
// Parse request.
|
|
request := &PingOpRequest{}
|
|
_, err := dsd.Load(data.CompileData(), request)
|
|
if err != nil {
|
|
return nil, terminal.ErrMalformedData.With("failed to parse ping request: %w", err)
|
|
}
|
|
|
|
// Create response.
|
|
response, err := dsd.Dump(&PingOpResponse{
|
|
Nonce: request.Nonce,
|
|
Time: time.Now().UTC(),
|
|
}, dsd.CBOR)
|
|
if err != nil {
|
|
return nil, terminal.ErrInternalError.With("failed to create ping response: %w", err)
|
|
}
|
|
|
|
// Send response.
|
|
msg := terminal.NewMsg(response)
|
|
msg.FlowID = opID
|
|
msg.Unit.MakeHighPriority()
|
|
if terminal.UsePriorityDataMsgs {
|
|
msg.Type = terminal.MsgTypePriorityData
|
|
}
|
|
tErr := t.Send(msg, pingOpTimeout)
|
|
if tErr != nil {
|
|
// Finish message unit on failure.
|
|
msg.Finish()
|
|
return nil, tErr.With("failed to send ping response")
|
|
}
|
|
|
|
// Operation is just one response and finished successfully.
|
|
return nil, nil
|
|
}
|
|
|
|
// HandleStop gives the operation the ability to cleanly shut down.
|
|
// The returned error is the error to send to the other side.
|
|
// Should never be called directly. Call Stop() instead.
|
|
func (op *PingOp) HandleStop(err *terminal.Error) (errorToSend *terminal.Error) {
|
|
// Prevent remote from sending explicit ack, as we use it as a success signal internally.
|
|
if err.Is(terminal.ErrExplicitAck) && err.IsExternal() {
|
|
err = terminal.ErrStopping.AsExternal()
|
|
}
|
|
|
|
// Continue with usual handling of inherited base.
|
|
return op.OneOffOperationBase.HandleStop(err)
|
|
}
|