mirror of
https://github.com/safing/portmaster
synced 2025-04-20 02:49: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>
161 lines
4.2 KiB
Go
161 lines
4.2 KiB
Go
package updates
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/safing/portmaster/base/api"
|
|
"github.com/safing/portmaster/base/log"
|
|
"github.com/safing/portmaster/base/utils"
|
|
)
|
|
|
|
const (
|
|
apiPathCheckForUpdates = "updates/check"
|
|
)
|
|
|
|
func registerAPIEndpoints() error {
|
|
if err := api.RegisterEndpoint(api.Endpoint{
|
|
Name: "Check for Updates",
|
|
Description: "Checks if new versions are available. If automatic updates are enabled, they are also downloaded and applied.",
|
|
Parameters: []api.Parameter{{
|
|
Method: http.MethodPost,
|
|
Field: "download",
|
|
Value: "",
|
|
Description: "Force downloading and applying of all updates, regardless of auto-update settings.",
|
|
}},
|
|
Path: apiPathCheckForUpdates,
|
|
Write: api.PermitUser,
|
|
ActionFunc: func(r *api.Request) (msg string, err error) {
|
|
// Check if we should also download regardless of settings.
|
|
downloadAll := r.URL.Query().Has("download")
|
|
|
|
// Trigger update task.
|
|
err = TriggerUpdate(true, downloadAll)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Report how we triggered.
|
|
if downloadAll {
|
|
return "downloading all updates...", nil
|
|
}
|
|
return "checking for updates...", nil
|
|
},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := api.RegisterEndpoint(api.Endpoint{
|
|
Name: "Get Resource",
|
|
Description: "Returns the requested resource from the udpate system",
|
|
Path: `updates/get/{identifier:[A-Za-z0-9/\.\-_]{1,255}}`,
|
|
Read: api.PermitUser,
|
|
ReadMethod: http.MethodGet,
|
|
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
|
|
// Get identifier from URL.
|
|
var identifier string
|
|
if ar := api.GetAPIRequest(r); ar != nil {
|
|
identifier = ar.URLVars["identifier"]
|
|
}
|
|
if identifier == "" {
|
|
http.Error(w, "no resource speicified", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Get resource.
|
|
resource, err := registry.GetFile(identifier)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
// Open file for reading.
|
|
file, err := os.Open(resource.Path())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer file.Close() //nolint:errcheck,gosec
|
|
|
|
// Assign file to reader
|
|
var reader io.Reader = file
|
|
|
|
// Add version to header.
|
|
w.Header().Set("Resource-Version", resource.Version())
|
|
|
|
// Set Content-Type.
|
|
contentType, _ := utils.MimeTypeByExtension(filepath.Ext(resource.Path()))
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
// Check if the content type may be returned.
|
|
accept := r.Header.Get("Accept")
|
|
if accept != "" {
|
|
mimeTypes := strings.Split(accept, ",")
|
|
// First, clean mime types.
|
|
for i, mimeType := range mimeTypes {
|
|
mimeType = strings.TrimSpace(mimeType)
|
|
mimeType, _, _ = strings.Cut(mimeType, ";")
|
|
mimeTypes[i] = mimeType
|
|
}
|
|
// Second, check if we may return anything.
|
|
var acceptsAny bool
|
|
for _, mimeType := range mimeTypes {
|
|
switch mimeType {
|
|
case "*", "*/*":
|
|
acceptsAny = true
|
|
}
|
|
}
|
|
// Third, check if we can convert.
|
|
if !acceptsAny {
|
|
var converted bool
|
|
sourceType, _, _ := strings.Cut(contentType, ";")
|
|
findConvertiblePair:
|
|
for _, mimeType := range mimeTypes {
|
|
switch {
|
|
case sourceType == "application/yaml" && mimeType == "application/json":
|
|
yamlData, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
jsonData, err := yaml.YAMLToJSON(yamlData)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
reader = bytes.NewReader(jsonData)
|
|
converted = true
|
|
break findConvertiblePair
|
|
}
|
|
}
|
|
|
|
// If we could not convert to acceptable format, return an error.
|
|
if !converted {
|
|
http.Error(w, "conversion to requested format not supported", http.StatusNotAcceptable)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// Write file.
|
|
w.WriteHeader(http.StatusOK)
|
|
if r.Method != http.MethodHead {
|
|
_, err = io.Copy(w, reader)
|
|
if err != nil {
|
|
log.Errorf("updates: failed to serve resource file: %s", err)
|
|
return
|
|
}
|
|
}
|
|
},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|