mirror of
https://github.com/safing/portmaster
synced 2025-09-02 18:49:14 +00:00
Improve clear network history API endpoint
This commit is contained in:
parent
b2e6557377
commit
98394c1ea6
1 changed files with 9 additions and 14 deletions
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
|
@ -118,29 +117,27 @@ func (m *module) prepare() error {
|
||||||
MimeType: "application/json",
|
MimeType: "application/json",
|
||||||
Write: api.PermitUser,
|
Write: api.PermitUser,
|
||||||
BelongsTo: m.Module,
|
BelongsTo: m.Module,
|
||||||
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
|
ActionFunc: func(ar *api.Request) (msg string, err error) {
|
||||||
|
// TODO: Use query parameters instead.
|
||||||
var body struct {
|
var body struct {
|
||||||
ProfileIDs []string `json:"profileIDs"`
|
ProfileIDs []string `json:"profileIDs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
dec := json.NewDecoder(r.Body)
|
dec := json.NewDecoder(ar.Body)
|
||||||
dec.DisallowUnknownFields()
|
dec.DisallowUnknownFields()
|
||||||
|
|
||||||
if err := dec.Decode(&body); err != nil {
|
if err := dec.Decode(&body); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
return "", err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(body.ProfileIDs) == 0 {
|
if len(body.ProfileIDs) == 0 {
|
||||||
if err := m.mng.store.RemoveAllHistoryData(r.Context()); err != nil {
|
if err := m.mng.store.RemoveAllHistoryData(ar.Context()); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return "", err
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
merr := new(multierror.Error)
|
merr := new(multierror.Error)
|
||||||
for _, profileID := range body.ProfileIDs {
|
for _, profileID := range body.ProfileIDs {
|
||||||
if err := m.mng.store.RemoveHistoryForProfile(r.Context(), profileID); err != nil {
|
if err := m.mng.store.RemoveHistoryForProfile(ar.Context(), profileID); err != nil {
|
||||||
merr.Errors = append(merr.Errors, fmt.Errorf("failed to clear history for %q: %w", profileID, err))
|
merr.Errors = append(merr.Errors, fmt.Errorf("failed to clear history for %q: %w", profileID, err))
|
||||||
} else {
|
} else {
|
||||||
log.Infof("netquery: successfully cleared history for %s", profileID)
|
log.Infof("netquery: successfully cleared history for %s", profileID)
|
||||||
|
@ -148,13 +145,11 @@ func (m *module) prepare() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := merr.ErrorOrNil(); err != nil {
|
if err := merr.ErrorOrNil(); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return "", err
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
return "Successfully cleared history.", nil
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return fmt.Errorf("failed to register API endpoint: %w", err)
|
return fmt.Errorf("failed to register API endpoint: %w", err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue