From 06fcb0be382f9094a75bb1fab2d0a021c711ff59 Mon Sep 17 00:00:00 2001 From: Shubham Prajapati Date: Thu, 13 Jun 2024 06:09:58 +0530 Subject: [PATCH] Subjecting ip geo informations --- core/ipinfo.go | 4 ++-- main.go | 4 +++- util/pkg/node/file.go | 23 +++++++++++++++--- util/pkg/node/node.go | 54 ++++++++++++++++++++++++++++++++++++------- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/core/ipinfo.go b/core/ipinfo.go index ad58e08..a8ad27d 100644 --- a/core/ipinfo.go +++ b/core/ipinfo.go @@ -3,7 +3,7 @@ package core import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" ) @@ -28,7 +28,7 @@ func GetIPInfo() { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("Error:", err) return diff --git a/main.go b/main.go index 699019c..3103e84 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "github.com/NetSepio/erebrus/p2p" "github.com/NetSepio/erebrus/util" "github.com/NetSepio/erebrus/util/pkg/auth" + "github.com/NetSepio/erebrus/util/pkg/node" "github.com/gin-contrib/static" helmet "github.com/danielkov/gin-helmet" @@ -30,6 +31,7 @@ func init() { log.SetFormatter(&log.JSONFormatter{}) log.SetOutput(os.Stderr) log.SetLevel(log.DebugLevel) + node.Init() // Get Hostname for updating Log StandardFields HostName, err := os.Hostname() @@ -49,6 +51,7 @@ func init() { log.WithFields(util.StandardFields).Fatalf("Error in reading the config file: %v", err) } } + core.GetIPInfo() auth.Init() } @@ -130,7 +133,6 @@ func main() { core.GenerateWalletAddress() // core.GenerateWalletAddressSolana() // core.GenerateEthereumWalletAddress() - core.GetIPInfo() go p2p.Init() //running updater diff --git a/util/pkg/node/file.go b/util/pkg/node/file.go index a163ff7..d911f29 100644 --- a/util/pkg/node/file.go +++ b/util/pkg/node/file.go @@ -5,20 +5,33 @@ import ( "net" "os" "runtime" + + "github.com/NetSepio/erebrus/core" ) var ( - osInfo OSInfo - ipInfo IPInfo + osInfo OSInfo + ipInfo IPInfo + ipGeoData IpGeoAddress ) -func init() { +func Init() { osInfo = OSInfo{ Name: runtime.GOOS, Architecture: runtime.GOARCH, NumCPU: runtime.NumCPU(), } + ipGeoData = IpGeoAddress{ + IpInfoIP: core.GlobalIPInfo.IP, + IpInfoCity: core.GlobalIPInfo.City, + IpInfoCountry: core.GlobalIPInfo.Country, + IpInfoLocation: core.GlobalIPInfo.Location, + IpInfoOrg: core.GlobalIPInfo.Org, + IpInfoPostal: core.GlobalIPInfo.Postal, + IpInfoTimezone: core.GlobalIPInfo.Timezone, + } + hostname, err := os.Hostname() if err != nil { fmt.Println("Error:", err) @@ -50,3 +63,7 @@ func GetOSInfo() OSInfo { func GetIPInfo() IPInfo { return ipInfo } + +func GetIpData() IpGeoAddress { + return ipGeoData +} diff --git a/util/pkg/node/node.go b/util/pkg/node/node.go index bcdc534..aa8e93e 100644 --- a/util/pkg/node/node.go +++ b/util/pkg/node/node.go @@ -2,7 +2,9 @@ package node import ( "encoding/json" + "fmt" "os" + "unicode" "github.com/NetSepio/erebrus/core" "github.com/NetSepio/erebrus/util/pkg/speedtest" @@ -69,6 +71,7 @@ type NodeStatus struct { CodeHash string `json:"codeHash"` SystemInfo string `json:"systemInfo" gorm:"type:jsonb"` IpInfo string `json:"ipinfo" gorm:"type:jsonb"` + IpGeoData string `json:"ipGeoData" gorm:"type:jsonb"` } func ToJSON(data interface{}) string { @@ -96,12 +99,33 @@ type IPInfo struct { IPv6Addresses []string } +type IpGeoAddress struct { + IpInfoIP string + IpInfoCity string + IpInfoCountry string + IpInfoLocation string + IpInfoOrg string + IpInfoPostal string + IpInfoTimezone string +} + func CreateNodeStatus(address string, id string, startTimeStamp int64, name string) *NodeStatus { + fmt.Println("Printing GetIpData : ") + fmt.Printf("%+v\n", core.GlobalIPInfo) + fmt.Println() speedtestResult, err := speedtest.GetSpeedtestResults() if err != nil { logrus.Error("failed to fetch network speed: ", err.Error()) } + IPGeo := IpGeoAddress{IpInfoIP: core.GlobalIPInfo.IP, + IpInfoCity: core.GlobalIPInfo.City, + IpInfoCountry: core.GlobalIPInfo.Country, + IpInfoLocation: core.GlobalIPInfo.Location, + IpInfoOrg: core.GlobalIPInfo.Org, + IpInfoPostal: core.GlobalIPInfo.Postal, + IpInfoTimezone: core.GlobalIPInfo.Timezone} + fmt.Println("Ip Geo : ", IPGeo) nodeStatus := &NodeStatus{ HttpPort: os.Getenv("HTTP_PORT"), Host: os.Getenv("DOMAIN"), @@ -114,18 +138,32 @@ func CreateNodeStatus(address string, id string, startTimeStamp int64, name stri Name: name, WalletAddress: core.WalletAddress, Chain: os.Getenv("CHAIN_NAME"), - // IpInfoIP: core.GlobalIPInfo.IP, - // IpInfoCity: core.GlobalIPInfo.City, - // IpInfoCountry: core.GlobalIPInfo.Country, - // IpInfoLocation: core.GlobalIPInfo.Location, - // IpInfoOrg: core.GlobalIPInfo.Org, - // IpInfoPostal: core.GlobalIPInfo.Postal, - // IpInfoTimezone: core.GlobalIPInfo.Timezone, + // IpInfoIP: core.GlobalIPInfo.IP, + // IpInfoCity: core.GlobalIPInfo.City, + // IpInfoCountry: core.GlobalIPInfo.Country, + // IpInfoLocation: core.GlobalIPInfo.Location, + // IpInfoOrg: core.GlobalIPInfo.Org, + // IpInfoPostal: core.GlobalIPInfo.Postal, + // IpInfoTimezone: core.GlobalIPInfo.Timezone, + IpGeoData: ToJSON(IPGeo), Version: "v1", - CodeHash: "xxxxxxxxxxxxxxxxxxx", + CodeHash: "yyyyyyyyyyyyyyyyyy", SystemInfo: ToJSON(GetOSInfo()), IpInfo: ToJSON(GetIPInfo()), } return nodeStatus } + +func MakeItString(str string) string { + + result := "" + for _, char := range str { + if unicode.IsLetter(char) { + result += string(unicode.ToLower(char)) + } else { + result += string(char) + } + } + return result +}