-
Notifications
You must be signed in to change notification settings - Fork 45
/
debug.go
137 lines (130 loc) · 4.54 KB
/
debug.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
package main
import (
"fmt"
"net"
"net/http"
"os"
"runtime"
"time"
ptp "github.com/subutai-io/p2p/lib"
)
// CommandDebug prints debug information
func CommandDebug(restPort int) {
out, err := sendRequest(restPort, "debug", &DaemonArgs{})
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Println(out.Message)
os.Exit(out.Code)
}
func (d *Daemon) execRESTDebug(w http.ResponseWriter, r *http.Request) {
if !ReadyToServe {
resp, _ := getResponse(105, "P2P Daemon is in initialization state")
w.Write(resp)
return
}
if !bootstrap.isActive {
resp, _ := getResponse(106, "Not connected to DHT nodes")
w.Write(resp)
return
}
if bootstrap.ip == "" {
resp, _ := getResponse(107, "Didn't received outbound IP yet")
w.Write(resp)
return
}
args := new(DaemonArgs)
err := getJSON(r.Body, args)
if handleMarshalError(err, w) != nil {
return
}
response := new(Response)
d.Debug(&Args{
Command: args.Command,
Args: args.Args,
}, response)
resp, err := getResponse(response.ExitCode, response.Output)
if err != nil {
ptp.Log(ptp.Error, "Internal error: %s", err)
return
}
w.Write(resp)
}
// Debug output debug information
func (p *Daemon) Debug(args *Args, resp *Response) error {
resp.Output = fmt.Sprintf("Version: %s Build: %s\n", AppVersion, BuildID)
resp.Output += fmt.Sprintf("Uptime: %d h %d m %d s\n", int(time.Since(StartTime).Hours()), int(time.Since(StartTime).Minutes())%60, int(time.Since(StartTime).Seconds())%60)
resp.Output += fmt.Sprintf("Number of gouroutines: %d\n", runtime.NumGoroutine())
if ptp.UsePMTU {
resp.Output += fmt.Sprintf("PMTU: Enabled\n")
} else {
resp.Output += fmt.Sprintf("PMTU: Disabled\n")
}
resp.Output += fmt.Sprintf("Bootstrap nodes information:\n")
for _, node := range bootstrap.routers {
if node != nil {
resp.Output += fmt.Sprintf(" %s Rx: %d Tx: %d Version: %s Packet version: %s\n", node.addr.String(), node.rx, node.tx, node.version, node.packetVersion)
}
}
resp.Output += fmt.Sprintf("Instances information:\n")
instances := p.Instances.get()
for _, inst := range instances {
resp.Output += fmt.Sprintf("Hash: %s\n", inst.ID)
resp.Output += fmt.Sprintf("ID: %s\n", inst.PTP.Dht.ID)
resp.Output += fmt.Sprintf("UDP Port: %d\n", inst.PTP.UDPSocket.GetPort())
resp.Output += fmt.Sprintf("Network interfaces: ")
for _, ip := range inst.PTP.LocalIPs {
resp.Output += fmt.Sprintf("%s ", ip.String())
}
resp.Output += "\n"
resp.Output += fmt.Sprintf("P2P Interface %s, HW Addr: %s, IP: %s\n", inst.PTP.Interface.GetName(), inst.PTP.Interface.GetHardwareAddress().String(), inst.PTP.Interface.GetIP().String())
resp.Output += fmt.Sprintf("Proxies: ")
proxyList := inst.PTP.ProxyManager.GetList()
if len(proxyList) == 0 {
resp.Output += fmt.Sprintf("No proxies in use")
}
for _, proxy := range proxyList {
if proxy.Addr == nil || proxy.Endpoint == nil {
continue
}
resp.Output += fmt.Sprintf("%s/%d [%d] ", proxy.Addr.String(), proxy.Endpoint.Port, ptp.NanoToMilliseconds(proxy.Latency.Nanoseconds()))
}
resp.Output += "\n"
resp.Output += fmt.Sprintf("Peers:\n")
peers := inst.PTP.Swarm.Get()
for _, peer := range peers {
resp.Output += fmt.Sprintf("\t--- %s ---\n", peer.ID)
resp.Output += fmt.Sprintf("\tStates: %s | %s\n", ptp.StringifyState(peer.State), ptp.StringifyState(peer.RemoteState))
if peer.PeerLocalIP == nil {
resp.Output += "\tNo IP assigned\n"
} else if peer.PeerHW == nil {
resp.Output += "\tNo MAC assigned\n"
} else {
resp.Output += fmt.Sprintf("\tNetwork: %s %s\n", peer.PeerLocalIP.String(), peer.PeerHW.String())
resp.Output += fmt.Sprintf("\tEndpoint: %s\n", peer.Endpoint)
resp.Output += fmt.Sprintf("\tAll Endpoints: ")
for _, ep := range peer.EndpointsHeap {
resp.Output += fmt.Sprintf("%s [%d] ", ep.Addr.String(), ptp.NanoToMilliseconds(ep.Latency.Nanoseconds()))
}
resp.Output += "\n"
c := peer.Stat.GetConnectionsNum()
r := peer.Stat.GetReconnectsNum()
hp := peer.Stat.GetHolePunchNum()
cdelta := peer.Stat.GetConnectionTimeDelta()
rdelta := peer.Stat.GetReconnectionTimeDelta()
resp.Output += fmt.Sprintf("Stats: [C: %d] [R: %d] [HP: %d] [CDelta: %d] [RDelta: %d]\n", c, r, hp, cdelta, rdelta)
}
resp.Output += fmt.Sprintf("\tEndpoints pool: ")
pool := []*net.UDPAddr{}
pool = append(pool, peer.KnownIPs...)
pool = append(pool, peer.Proxies...)
for _, v := range pool {
resp.Output += fmt.Sprintf("%s ", v.String())
}
resp.Output += "\n"
resp.Output += fmt.Sprintf("\t--- End of %s ---\n", peer.ID)
}
}
return nil
}