Skip to content

Commit

Permalink
Subjecting ip geo informations
Browse files Browse the repository at this point in the history
  • Loading branch information
p-shubh committed Jun 13, 2024
1 parent 29e9fa7 commit 06fcb0b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
4 changes: 2 additions & 2 deletions core/ipinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package core
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -49,6 +51,7 @@ func init() {
log.WithFields(util.StandardFields).Fatalf("Error in reading the config file: %v", err)
}
}
core.GetIPInfo()

auth.Init()
}
Expand Down Expand Up @@ -130,7 +133,6 @@ func main() {
core.GenerateWalletAddress()
// core.GenerateWalletAddressSolana()
// core.GenerateEthereumWalletAddress()
core.GetIPInfo()

go p2p.Init()
//running updater
Expand Down
23 changes: 20 additions & 3 deletions util/pkg/node/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -50,3 +63,7 @@ func GetOSInfo() OSInfo {
func GetIPInfo() IPInfo {
return ipInfo
}

func GetIpData() IpGeoAddress {
return ipGeoData
}
54 changes: 46 additions & 8 deletions util/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package node

import (
"encoding/json"
"fmt"
"os"
"unicode"

"github.com/NetSepio/erebrus/core"
"github.com/NetSepio/erebrus/util/pkg/speedtest"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"),
Expand All @@ -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
}

0 comments on commit 06fcb0b

Please sign in to comment.