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,