Skip to content

Commit

Permalink
adding os and ip info to NodeStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhavvvvv committed Jun 10, 2024
1 parent e86d165 commit a9e2524
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 38 deletions.
23 changes: 23 additions & 0 deletions D:\jjsahdd/server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Address": [
"10.0.0.1/24"
],
"ListenPort": 51820,
"PrivateKey": "QADILa0leJ1Jbo6Cp/CSC7MlBPHbTgEo4zPi5AB981M=",
"PublicKey": "LchOEEEzD4Z6GY3KyV91iLp3/94XoqltFtmpRxh48hs=",
"Endpoint": "ip_addr",
"PersistentKeepalive": 16,
"DNS": [
"1.1.1.1"
],
"AllowedIPs": [
"0.0.0.0/0",
"::/0"
],
"PreUp": "echo WireGuard PreUp",
"PostUp": "iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE",
"PreDown": "echo WireGuard PreDown",
"PostDown": "iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE",
"CreatedAt": 327724432,
"UpdatedAt": 327724432
}
10 changes: 10 additions & 0 deletions D:\jjsahdd/wg0.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Updated: 327724432 / Created: 327724432
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = QADILa0leJ1Jbo6Cp/CSC7MlBPHbTgEo4zPi5AB981M=

PreUp = echo WireGuard PreUp
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PreDown = echo WireGuard PreDown
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
52 changes: 52 additions & 0 deletions util/pkg/node/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package node

import (
"fmt"
"net"
"os"
"runtime"
)

var (
osInfo OSInfo
ipInfo IPInfo
)

func init() {
osInfo = OSInfo{
Name: runtime.GOOS,
Architecture: runtime.GOARCH,
NumCPU: runtime.NumCPU(),
}

hostname, err := os.Hostname()
if err != nil {
fmt.Println("Error:", err)
return
}
osInfo.Hostname = hostname

addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println("Error:", err)
return
}

for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
ipInfo.IPv4Addresses = append(ipInfo.IPv4Addresses, ipNet.IP.String())
} else if ipNet.IP.To16() != nil {
ipInfo.IPv6Addresses = append(ipInfo.IPv6Addresses, ipNet.IP.String())
}
}
}
}

func GetOSInfo() OSInfo {
return osInfo
}

func GetIPInfo() IPInfo {
return ipInfo
}
133 changes: 95 additions & 38 deletions util/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,110 @@ import (
"github.com/sirupsen/logrus"
)

// type NodeStatus struct {
// Id string `json:"id"`
// HttpPort string `json:"httpPort"`
// Domain string `json:"domain"`
// Address string `json:"address"`
// Region string `json:"region"`
// NodeName string `json:"nodename"`
// DownloadSpeed float64 `json:"downloadSpeed"`
// UploadSpeed float64 `json:"uploadSpeed"`
// StartTimeStamp int64 `json:"startTimeStamp"`
// Name string `json:"name"`
// WalletAddress string `json:"walletAddress"`
// ChainName string `json:"chainName"`
// IpInfoIP string `json:"ipinfoip"`
// IpInfoCity string `json:"ipinfocity"`
// IpInfoCountry string `json:"ipinfocountry"`
// IpInfoLocation string `json:"ipinfolocation"`
// IpInfoOrg string `json:"ipinfoorg"`
// IpInfoPostal string `json:"ipinfopostal"`
// IpInfoTimezone string `json:"ipinfotimezone"`
// }

type NodeStatus struct {
Id string `json:"id"`
HttpPort string `json:"httpPort"`
Domain string `json:"domain"`
Address string `json:"address"`
Region string `json:"region"`
NodeName string `json:"nodename"`
DownloadSpeed float64 `json:"downloadSpeed"`
UploadSpeed float64 `json:"uploadSpeed"`
StartTimeStamp int64 `json:"startTimeStamp"`
Name string `json:"name"`
WalletAddress string `json:"walletAddress"`
ChainName string `json:"chainName"`
IpInfoIP string `json:"ipinfoip"`
IpInfoCity string `json:"ipinfocity"`
IpInfoCountry string `json:"ipinfocountry"`
IpInfoLocation string `json:"ipinfolocation"`
IpInfoOrg string `json:"ipinfoorg"`
IpInfoPostal string `json:"ipinfopostal"`
IpInfoTimezone string `json:"ipinfotimezone"`
PeerId string `json:"peerId" gorm:"primaryKey"`
Name string `json:"name"`
HttpPort string `json:"httpPort"`
Host string `json:"host"` //domain
PeerAddress string `json:"peerAddress"`
Region string `json:"region"`
Status string `json:"status"` // offline 1, online 2, maintainance 3,block 4
DownloadSpeed float64 `json:"downloadSpeed"`
UploadSpeed float64 `json:"uploadSpeed"`
RegistrationTime int64 `json:"registrationTime"` //StartTimeStamp
LastPing int64 `json:"lastPing"`
Chain string `json:"chain"`
WalletAddress string `json:"walletAddress"`
Version string `json:"version"`
CodeHash string `json:"codeHash"`
SystemInfo OSInfo `json:"systemInfo"`
IpInfo IPInfo `json:"ipinfo"`
}

type TestNodeStatus struct {
PeerId string `json:"peerId" gorm:"primaryKey"`
Name string `json:"name"`
HttpPort string `json:"httpPort"`
Host string `json:"host"` //domain
PeerAddress string `json:"peerAddress"`
Region string `json:"region"`
Status string `json:"status"` // offline 1, online 2, maintainance 3,block 4
DownloadSpeed float64 `json:"downloadSpeed"`
UploadSpeed float64 `json:"uploadSpeed"`
RegistrationTime int64 `json:"registrationTime"` //StartTimeStamp
LastPing int64 `json:"lastPing"`
Chain string `json:"chain"`
WalletAddress string `json:"walletAddress"`
Version string `json:"version"`
CodeHash string `json:"codeHash"`
SystemInfo string `json:"systemInfo" gorm:"type:jsonb"`
IpInfo string `json:"ipinfo" gorm:"type:jsonb"`
}

type OSInfo struct {
Name string // Name of the operating system
Hostname string // Hostname of the system
Architecture string // Architecture of the system
NumCPU int // Number of CPUs
}

type IPInfo struct {
IPv4Addresses []string
IPv6Addresses []string
}

func CreateNodeStatus(address string, id string, startTimeStamp int64, name string) *NodeStatus {

speedtestResult, err := speedtest.GetSpeedtestResults()
if err != nil {
logrus.Error("failed to fetch network speed: ", err.Error())
}
nodeStatus := &NodeStatus{
HttpPort: os.Getenv("HTTP_PORT"),
Domain: os.Getenv("DOMAIN"),
Address: address,
NodeName: os.Getenv("NODE_NAME"),
Region: core.GlobalIPInfo.Country,
Id: id,
DownloadSpeed: speedtestResult.DownloadSpeed,
UploadSpeed: speedtestResult.UploadSpeed,
StartTimeStamp: startTimeStamp,
Name: name,
WalletAddress: core.WalletAddress,
ChainName: 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,
HttpPort: os.Getenv("HTTP_PORT"),
Host: os.Getenv("DOMAIN"),
PeerAddress: address,
Region: core.GlobalIPInfo.Country,
PeerId: id,
DownloadSpeed: speedtestResult.DownloadSpeed,
UploadSpeed: speedtestResult.UploadSpeed,
RegistrationTime: startTimeStamp,
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,
Version: "v1",
CodeHash: "xxxxxxxxxxxxxxxxxxx",
SystemInfo: GetOSInfo(),
IpInfo: GetIPInfo(),
}

return nodeStatus
}

0 comments on commit a9e2524

Please sign in to comment.