From ab89a326c5d2a4654167297fedff4beebe5a23e5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 29 Mar 2021 13:47:56 +0200 Subject: [PATCH] Add network api endpoints --- netenv/api.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ netenv/main.go | 4 ++++ 2 files changed, 55 insertions(+) create mode 100644 netenv/api.go diff --git a/netenv/api.go b/netenv/api.go new file mode 100644 index 00000000..656f860e --- /dev/null +++ b/netenv/api.go @@ -0,0 +1,51 @@ +package netenv + +import ( + "errors" + + "github.com/safing/portbase/api" +) + +func registerAPIEndpoints() error { + if err := api.RegisterEndpoint(api.Endpoint{ + Path: "network/gateways", + Read: api.PermitUser, + StructFunc: func(ar *api.Request) (i interface{}, err error) { + return Gateways(), nil + }, + Name: "Get Default Gateways", + Description: "Returns the current active default gateways of the network.", + }); err != nil { + return err + } + + if err := api.RegisterEndpoint(api.Endpoint{ + Path: "network/nameservers", + Read: api.PermitUser, + StructFunc: func(ar *api.Request) (i interface{}, err error) { + return Nameservers(), nil + }, + Name: "Get System Nameservers", + Description: "Returns the currently configured nameservers on the OS.", + }); err != nil { + return err + } + + if err := api.RegisterEndpoint(api.Endpoint{ + Path: "network/location", + Read: api.PermitUser, + StructFunc: func(ar *api.Request) (i interface{}, err error) { + locs, ok := GetInternetLocation() + if ok { + return locs, nil + } + return nil, errors.New("no location data available") + }, + Name: "Get Approximate Internet Location", + Description: "Returns an approximation of where the device is on the Internet.", + }); err != nil { + return err + } + + return nil +} diff --git a/netenv/main.go b/netenv/main.go index fb5d5a15..1fca17c6 100644 --- a/netenv/main.go +++ b/netenv/main.go @@ -29,6 +29,10 @@ func prep() error { } func start() error { + if err := registerAPIEndpoints(); err != nil { + return err + } + module.StartServiceWorker( "monitor network changes", 0,