-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (83 loc) · 2.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"time"
)
type Network struct {
LocalAddress string `json:"localAddress"`
RemoteAddress string `json:"remoteAddress"`
Inbound bool `json:"inbound"`
Trusted bool `json:"trusted"`
Static bool `json:"static"`
}
type Result struct {
Enr string `json:"enr"`
Enode string `json:"enode"`
ID string `json:"id"`
Name string `json:"name"`
Network Network `json:"network"`
}
type Root struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Result []Result `json:"result"`
}
func main() {
ip := flag.String("ip", "", "The IP address of the node")
flag.Parse()
if *ip == "" {
panic("The 'ip' flag is required.")
}
for {
getPeers(ip)
fmt.Println("Sleeping for 5 seconds...")
time.Sleep(5 * time.Second)
}
}
func getPeers(ip *string) {
data := `{"jsonrpc":"2.0","method":"admin_peers","id":0}`
body := bytes.NewBuffer([]byte(data))
url := fmt.Sprintf("http://%s:8545", *ip)
req, err := http.NewRequest("POST", url, body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var root Root
err = json.Unmarshal(respBody, &root)
if err != nil {
panic("Error reading HTTP response")
}
for _, result := range root.Result {
fmt.Println("***********************************************************************************************")
fmt.Println("Enr: ", result.Enr)
fmt.Println("Enode: ", result.Enode)
fmt.Println("ID: ", result.ID)
fmt.Println("Name: ", result.Name)
// Access network details
fmt.Println("Network LocalAddress: ", result.Network.LocalAddress)
fmt.Println("Network RemoteAddress: ", result.Network.RemoteAddress)
fmt.Println("Network Inbound: ", result.Network.Inbound)
fmt.Println("Network Trusted: ", result.Network.Trusted)
fmt.Println("Network Static: ", result.Network.Static)
}
}