From 78c56861ab0d25d95e03159cfaec1b0843994782 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 17 May 2021 14:43:17 +0200 Subject: [PATCH] Link api endpoints and http handlers to their modules --- core/api.go | 3 +++ netenv/api.go | 15 +++++++++------ network/api.go | 6 ++++-- resolver/api.go | 7 +++++-- ui/api.go | 1 + ui/serve.go | 3 +++ updates/api.go | 5 +++-- 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/core/api.go b/core/api.go index f965b85f..399903c9 100644 --- a/core/api.go +++ b/core/api.go @@ -27,6 +27,7 @@ func registerAPIEndpoints() error { if err := api.RegisterEndpoint(api.Endpoint{ Path: "core/shutdown", Write: api.PermitSelf, + BelongsTo: module, ActionFunc: shutdown, Name: "Shut Down Portmaster", Description: "Shut down the Portmaster Core Service and all UI components.", @@ -37,6 +38,7 @@ func registerAPIEndpoints() error { if err := api.RegisterEndpoint(api.Endpoint{ Path: "core/restart", Write: api.PermitAdmin, + BelongsTo: module, ActionFunc: restart, Name: "Restart Portmaster", Description: "Restart the Portmaster Core Service.", @@ -47,6 +49,7 @@ func registerAPIEndpoints() error { if err := api.RegisterEndpoint(api.Endpoint{ Path: "debug/core", Read: api.PermitAnyone, + BelongsTo: module, DataFunc: debugInfo, Name: "Get Debug Information", Description: "Returns network debugging information, similar to debug/info, but with system status data.", diff --git a/netenv/api.go b/netenv/api.go index 656f860e..776eda73 100644 --- a/netenv/api.go +++ b/netenv/api.go @@ -8,8 +8,9 @@ import ( func registerAPIEndpoints() error { if err := api.RegisterEndpoint(api.Endpoint{ - Path: "network/gateways", - Read: api.PermitUser, + Path: "network/gateways", + Read: api.PermitUser, + BelongsTo: module, StructFunc: func(ar *api.Request) (i interface{}, err error) { return Gateways(), nil }, @@ -20,8 +21,9 @@ func registerAPIEndpoints() error { } if err := api.RegisterEndpoint(api.Endpoint{ - Path: "network/nameservers", - Read: api.PermitUser, + Path: "network/nameservers", + Read: api.PermitUser, + BelongsTo: module, StructFunc: func(ar *api.Request) (i interface{}, err error) { return Nameservers(), nil }, @@ -32,8 +34,9 @@ func registerAPIEndpoints() error { } if err := api.RegisterEndpoint(api.Endpoint{ - Path: "network/location", - Read: api.PermitUser, + Path: "network/location", + Read: api.PermitUser, + BelongsTo: module, StructFunc: func(ar *api.Request) (i interface{}, err error) { locs, ok := GetInternetLocation() if ok { diff --git a/network/api.go b/network/api.go index 659d77b3..3f52bc8a 100644 --- a/network/api.go +++ b/network/api.go @@ -20,6 +20,7 @@ func registerAPIEndpoints() error { if err := api.RegisterEndpoint(api.Endpoint{ Path: "debug/network", Read: api.PermitUser, + BelongsTo: module, DataFunc: debugInfo, Name: "Get Network Debug Information", Description: "Returns network debugging information, similar to debug/core, but with connection data.", @@ -48,8 +49,9 @@ func registerAPIEndpoints() error { } if err := api.RegisterEndpoint(api.Endpoint{ - Path: "debug/network/state", - Read: api.PermitUser, + Path: "debug/network/state", + Read: api.PermitUser, + BelongsTo: module, StructFunc: func(ar *api.Request) (i interface{}, err error) { return state.GetInfo(), nil }, diff --git a/resolver/api.go b/resolver/api.go index 39ac80bc..a31b19a1 100644 --- a/resolver/api.go +++ b/resolver/api.go @@ -11,6 +11,7 @@ func registerAPI() error { if err := api.RegisterEndpoint(api.Endpoint{ Path: "dns/clear", Write: api.PermitUser, + BelongsTo: module, ActionFunc: clearNameCache, Name: "Clear cached DNS records", Description: "Deletes all saved DNS records from the database.", @@ -21,6 +22,7 @@ func registerAPI() error { if err := api.RegisterEndpoint(api.Endpoint{ Path: "dns/resolvers", Read: api.PermitAnyone, + BelongsTo: module, StructFunc: exportDNSResolvers, Name: "List DNS Resolvers", Description: "List currently configured DNS resolvers and their status.", @@ -29,8 +31,9 @@ func registerAPI() error { } if err := api.RegisterEndpoint(api.Endpoint{ - Path: `dns/cache/{query:[a-z0-9\.-]{0,512}\.[A-Z]{1,32}}`, - Read: api.PermitUser, + Path: `dns/cache/{query:[a-z0-9\.-]{0,512}\.[A-Z]{1,32}}`, + Read: api.PermitUser, + BelongsTo: module, RecordFunc: func(r *api.Request) (record.Record, error) { return recordDatabase.Get(nameRecordsKeyPrefix + r.URLVars["query"]) }, diff --git a/ui/api.go b/ui/api.go index 863ffbc9..e81200df 100644 --- a/ui/api.go +++ b/ui/api.go @@ -10,6 +10,7 @@ func registerAPIEndpoints() error { return api.RegisterEndpoint(api.Endpoint{ Path: "ui/reload", Write: api.PermitUser, + BelongsTo: module, ActionFunc: reloadUI, Name: "Reload UI Assets", Description: "Removes all assets from the cache and reloads the current (possibly updated) version from disk when requested.", diff --git a/ui/serve.go b/ui/serve.go index b7d0d401..4a341f8f 100644 --- a/ui/serve.go +++ b/ui/serve.go @@ -13,6 +13,7 @@ import ( "github.com/safing/portbase/api" "github.com/safing/portbase/log" + "github.com/safing/portbase/modules" "github.com/safing/portbase/updater" "github.com/safing/portmaster/updates" ) @@ -53,6 +54,8 @@ type bundleServer struct { defaultModuleName string } +func (bs *bundleServer) BelongsTo() *modules.Module { return module } + func (bs *bundleServer) ReadPermission(*http.Request) api.Permission { return api.PermitAnyone } func (bs *bundleServer) WritePermission(*http.Request) api.Permission { return api.NotSupported } diff --git a/updates/api.go b/updates/api.go index 5a00216f..d4cf9fad 100644 --- a/updates/api.go +++ b/updates/api.go @@ -10,8 +10,9 @@ const ( func registerAPIEndpoints() error { return api.RegisterEndpoint(api.Endpoint{ - Path: apiPathCheckForUpdates, - Write: api.PermitUser, + Path: apiPathCheckForUpdates, + Write: api.PermitUser, + BelongsTo: module, ActionFunc: func(_ *api.Request) (msg string, err error) { if err := TriggerUpdate(); err != nil { return "", err