feat: pause and resume functionality improvements + UI

https://github.com/safing/portmaster/issues/2050
This commit is contained in:
Alexandr Stelnykovych
2025-11-05 18:36:52 +02:00
parent c063bda700
commit 7709a6600c
8 changed files with 385 additions and 125 deletions

View File

@@ -1,11 +1,19 @@
package control
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/safing/portmaster/base/api"
)
const (
APIEndpointPause = "control/pause"
APIEndpointResume = "control/resume"
)
type pauseRequestParams struct {
Duration int `json:"duration"` // Duration in seconds
OnlySPN bool `json:"onlySPN"` // Whether to pause only the SPN service
@@ -14,7 +22,7 @@ type pauseRequestParams struct {
func (c *Control) registerAPIEndpoints() error {
if err := api.RegisterEndpoint(api.Endpoint{
Path: "control/pause",
Path: APIEndpointPause,
Write: api.PermitAdmin,
ActionFunc: c.handlePause,
Name: "Pause Portmaster",
@@ -36,7 +44,7 @@ func (c *Control) registerAPIEndpoints() error {
}
if err := api.RegisterEndpoint(api.Endpoint{
Path: "control/resume",
Path: APIEndpointResume,
Write: api.PermitAdmin,
ActionFunc: c.handleResume,
Name: "Resume Portmaster",
@@ -47,3 +55,32 @@ func (c *Control) registerAPIEndpoints() error {
return nil
}
func (c *Control) handlePause(r *api.Request) (msg string, err error) {
params := pauseRequestParams{}
if r.InputData != nil {
if err := json.Unmarshal(r.InputData, &params); err != nil {
return "Bad Request: invalid input data", err
}
}
if params.OnlySPN {
c.mgr.Info(fmt.Sprintf("Received SPN Pause(%v) action request ", params.Duration))
} else {
c.mgr.Info(fmt.Sprintf("Received Pause(%v) action request ", params.Duration))
}
if err := c.pause(time.Duration(params.Duration)*time.Second, params.OnlySPN); err != nil {
return "Failed to pause", err
}
return "Pause initiated", nil
}
func (c *Control) handleResume(_ *api.Request) (msg string, err error) {
c.mgr.Info("Received Resume action request")
if err := c.resume(); err != nil {
return "Failed to resume", err
}
return "Resume initiated", nil
}