forked from inhies/cjdcmd
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpeers.go
151 lines (130 loc) · 3.13 KB
/
peers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"os"
"github.com/fc00/go-cjdns/key"
"github.com/inhies/go-bytesize"
"github.com/spf13/cobra"
)
func showLocalPeers() {
c := Connect()
stats, err := c.InterfaceController_peerStats()
if err != nil {
fmt.Println("Error getting local peers,", err)
}
var tIn, tOut int64
for _, node := range stats {
tIn += node.BytesIn
tOut += node.BytesOut
}
//var host addr string
for _, node := range stats {
ip := node.PublicKey.IP().String()
host, _ := resolveIP(ip)
if len(host) == 0 {
host = ip
}
if Verbose {
var inP, outP int64
if tIn == 0 {
inP = 0
} else {
inP = (node.BytesIn * 100) / tIn
}
if tOut == 0 {
outP = 0
} else {
outP = (node.BytesOut * 100) / tOut
}
fmt.Fprintf(os.Stdout, "%s %s\n"+
"\tIncoming: %-5t\n"+
"\tState: %s \n"+
"\tBytes In: %s (%d%%)\n"+
"\tBytes Out: %s (%d%%)\n"+
"\tTraffic Ratio: %s\n"+
"\tLost Packets: %d\n\n",
// Last seen: %s\n",
node.PublicKey, host,
node.IsIncoming, node.State,
bytesize.New(float64(node.BytesIn)), inP, bytesize.New(float64(node.BytesOut)), outP,
ratio(node.BytesIn, node.BytesOut), node.LostPackets,
// time.Duration(node.Last),
)
} else {
fmt.Fprintln(os.Stdout, ip)
}
}
if Verbose {
fmt.Fprintln(os.Stdout, "Total Traffic:", bytesize.New(float64(tIn)), ratio(tIn, tOut), bytesize.New(float64(tOut)))
}
}
func peersCmd(cmd *cobra.Command, args []string) {
if len(args) == 0 {
showLocalPeers()
return
}
if len(args) > 1 {
cmd.Usage()
os.Exit(1)
}
_, ip, err := resolve(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Could not resolve %q.\n", args[0])
os.Exit(1)
}
c := Connect()
node, err := c.NodeStore_nodeForAddr(ip)
if err != nil {
die(err.Error())
}
peers, _, err := c.RouterModule_getPeers(node.RouteLabel, 0, "")
if err != nil {
die(err.Error())
}
for _, s := range peers {
k, err := key.DecodePublic(s[24:])
if err != nil {
fmt.Fprintln(os.Stderr, "received malformed key ", s[24:])
continue
}
if Verbose {
fmt.Fprintln(os.Stdout, s[:3], s[3:24], s[24:], k.IP())
} else {
fmt.Fprintln(os.Stdout, k.IP())
}
}
}
const maxSpread = 32
func ratio(in, out int64) string {
if in == 0 || out == 0 {
return "∞"
}
var factor int64
if in > out {
factor = in / maxSpread
} else if out > in {
factor = out / maxSpread
} else {
return "1/1"
}
out /= factor
in /= factor
for out%2 == 0 && in%2 == 0 {
out /= 2
in /= 2
}
return fmt.Sprintf("↓%d/%d↑", in, out)
}