From ba259a7eedee533a49c373152081d7a5a07708f7 Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Sun, 10 Nov 2024 17:13:52 +0200 Subject: [PATCH 1/2] - write node location info on a file while registering - add location endpoint to get this file info --- pkg/geoip/geoip.go | 4 ++++ pkg/registrar/register.go | 16 ++++++++++++++++ pkg/zos_api/location.go | 26 ++++++++++++++++++++++++++ pkg/zos_api/routes.go | 3 +++ 4 files changed, 49 insertions(+) create mode 100644 pkg/zos_api/location.go diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index df108568c..cbeeb2285 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -9,6 +9,10 @@ import ( "github.com/rs/zerolog/log" ) +const ( + LocationFile = "/tmp/location" +) + // Location holds the result of a geoip request type Location struct { Longitude float64 `json:"longitude"` diff --git a/pkg/registrar/register.go b/pkg/registrar/register.go index f077bbcdf..7b5ef338e 100644 --- a/pkg/registrar/register.go +++ b/pkg/registrar/register.go @@ -3,8 +3,10 @@ package registrar import ( "context" "crypto/ed25519" + "encoding/json" "fmt" "net" + "os" "time" "github.com/centrifuge/go-substrate-rpc-client/v4/types" @@ -144,6 +146,10 @@ func registerNode( City: info.Location.City, } + if err := writeLocationOnFile(info.Location, geoip.LocationFile); err != nil { + return 0, 0, errors.Wrap(err, "failed to set location on disk") + } + log.Info().Str("id", mgr.NodeID(ctx).Identity()).Msg("start registration of the node") log.Info().Msg("registering node on blockchain") @@ -231,3 +237,13 @@ func ensureTwin(ctx context.Context, substrateGateway *stubs.SubstrateGatewayStu return twinID, nil } + +func writeLocationOnFile(loc geoip.Location, filepath string) error { + file, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE, 0644) + if err != nil { + return errors.Wrap(err, "failed to open location file") + } + defer file.Close() + + return json.NewEncoder(file).Encode(loc) +} diff --git a/pkg/zos_api/location.go b/pkg/zos_api/location.go new file mode 100644 index 000000000..a1b8e9899 --- /dev/null +++ b/pkg/zos_api/location.go @@ -0,0 +1,26 @@ +package zosapi + +import ( + "context" + "encoding/json" + "os" + + "github.com/pkg/errors" + "github.com/threefoldtech/zos/pkg/geoip" +) + +func (g *ZosAPI) locationGet(ctx context.Context, payload []byte) (interface{}, error) { + if _, err := os.Stat(geoip.LocationFile); err != nil { + return nil, errors.Wrap(err, "couldn't found a location info") + } + + f, err := os.Open(geoip.LocationFile) + if err != nil { + return nil, errors.Wrap(err, "couldn't get the location info") + } + defer f.Close() + + var loc geoip.Location + err = json.NewDecoder(f).Decode(&loc) + return loc, err +} diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index 8006f23b7..a0a3fa214 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -48,4 +48,7 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) { admin.WithHandler("interfaces", g.adminInterfacesHandler) admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler) admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler) + + location := root.SubRoute("location") + location.WithHandler("get", g.locationGet) } From 96ddfc04802285ec951c86965e54c8f5c807636a Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Wed, 13 Nov 2024 09:58:41 +0200 Subject: [PATCH 2/2] let locationGet endpoint calls the geoip directly --- pkg/geoip/geoip.go | 4 ---- pkg/registrar/register.go | 16 ---------------- pkg/zos_api/location.go | 17 +---------------- 3 files changed, 1 insertion(+), 36 deletions(-) diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index cbeeb2285..df108568c 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -9,10 +9,6 @@ import ( "github.com/rs/zerolog/log" ) -const ( - LocationFile = "/tmp/location" -) - // Location holds the result of a geoip request type Location struct { Longitude float64 `json:"longitude"` diff --git a/pkg/registrar/register.go b/pkg/registrar/register.go index 7b5ef338e..f077bbcdf 100644 --- a/pkg/registrar/register.go +++ b/pkg/registrar/register.go @@ -3,10 +3,8 @@ package registrar import ( "context" "crypto/ed25519" - "encoding/json" "fmt" "net" - "os" "time" "github.com/centrifuge/go-substrate-rpc-client/v4/types" @@ -146,10 +144,6 @@ func registerNode( City: info.Location.City, } - if err := writeLocationOnFile(info.Location, geoip.LocationFile); err != nil { - return 0, 0, errors.Wrap(err, "failed to set location on disk") - } - log.Info().Str("id", mgr.NodeID(ctx).Identity()).Msg("start registration of the node") log.Info().Msg("registering node on blockchain") @@ -237,13 +231,3 @@ func ensureTwin(ctx context.Context, substrateGateway *stubs.SubstrateGatewayStu return twinID, nil } - -func writeLocationOnFile(loc geoip.Location, filepath string) error { - file, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE, 0644) - if err != nil { - return errors.Wrap(err, "failed to open location file") - } - defer file.Close() - - return json.NewEncoder(file).Encode(loc) -} diff --git a/pkg/zos_api/location.go b/pkg/zos_api/location.go index a1b8e9899..4952f1133 100644 --- a/pkg/zos_api/location.go +++ b/pkg/zos_api/location.go @@ -2,25 +2,10 @@ package zosapi import ( "context" - "encoding/json" - "os" - "github.com/pkg/errors" "github.com/threefoldtech/zos/pkg/geoip" ) func (g *ZosAPI) locationGet(ctx context.Context, payload []byte) (interface{}, error) { - if _, err := os.Stat(geoip.LocationFile); err != nil { - return nil, errors.Wrap(err, "couldn't found a location info") - } - - f, err := os.Open(geoip.LocationFile) - if err != nil { - return nil, errors.Wrap(err, "couldn't get the location info") - } - defer f.Close() - - var loc geoip.Location - err = json.NewDecoder(f).Decode(&loc) - return loc, err + return geoip.Fetch() }