-
Notifications
You must be signed in to change notification settings - Fork 9
/
ping.go
212 lines (202 loc) · 6 KB
/
ping.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package dc
import (
"context"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
dc "github.com/direct-connect/go-dc"
adcp "github.com/direct-connect/go-dc/adc"
nmdcp "github.com/direct-connect/go-dc/nmdc"
"github.com/direct-connect/go-dcpp/adc"
"github.com/direct-connect/go-dcpp/nmdc"
)
type PingConfig = adc.PingConfig
// Ping fetches the information about the specified hub.
func Ping(ctx context.Context, addr string, conf *PingConfig) (*HubInfo, error) {
if conf == nil {
conf = &PingConfig{}
}
if conf.Name == "" {
num := int64(time.Now().Nanosecond())
conf.Name = "pinger_" + strconv.FormatInt(num, 16)
}
if conf.Hubs == 0 {
conf.Hubs = 1 + rand.Intn(10)
}
if conf.Slots == 0 {
conf.Slots = 5
}
if conf.ShareFiles == 0 {
conf.ShareFiles = 100 + rand.Intn(1000)
}
if conf.ShareSize == 0 {
conf.ShareSize = uint64(100+rand.Intn(200)) * 1023 * 1023 * 1023
}
// probe first, if protocol is not specified
i := strings.Index(addr, "://")
if i < 0 {
u, err := Probe(ctx, addr)
if err != nil {
return nil, err
}
addr = u.String()
i = strings.Index(addr, "://")
}
switch addr[:i] {
case nmdcSchema, nmdcsSchema:
hub, err := nmdc.Ping(ctx, addr, nmdc.PingConfig{
Name: conf.Name, Share: conf.ShareSize,
Slots: conf.Slots, Hubs: conf.Hubs,
})
if err == nmdc.ErrRegisteredOnly {
// TODO: should support error code in NMDC
err = adcp.Error{Status: adcp.Status{
Sev: adcp.Fatal, Code: 26,
Msg: err.Error(),
}}
} else if e, ok := err.(*nmdc.ErrBanned); ok {
// TODO: distinguish temporary and permanent bans
err = adcp.Error{Status: adcp.Status{
Sev: adcp.Fatal, Code: 30,
Msg: e.Reason,
}}
}
if err != nil && hub == nil {
return nil, err
}
info := &HubInfo{
Name: hub.Name,
Desc: hub.Desc,
Addr: []string{addr},
KeyPrints: hub.KeyPrints,
Enc: hub.Encoding,
Server: &Software{
Name: hub.Server.Name,
Version: hub.Server.Version,
Ext: hub.Ext,
},
Users: len(hub.Users),
UserList: make([]HubUser, 0, len(hub.Users)),
Redirect: hub.Redirect,
}
if info.Desc == "" {
info.Desc = hub.Topic
}
if hub.Redirect != "" {
if uri, err := dc.NormalizeAddr(hub.Redirect); err == nil && uri != addr {
info.Addr = append([]string{uri}, info.Addr...)
}
}
if hub.Addr != "" {
if uri, err := nmdcp.NormalizeAddr(hub.Addr); err == nil && uri != addr {
info.Addr = append(info.Addr, uri)
}
}
for _, a := range hub.Failover {
uri, err := nmdcp.NormalizeAddr(a)
if err == nil {
info.Addr = append(info.Addr, uri)
}
}
for _, u := range hub.Users {
info.Share += uint64(u.ShareSize)
user := HubUser{
Name: string(u.Name),
Share: u.ShareSize,
Email: u.Email,
Client: &Software{
Name: u.Client.Name,
Version: u.Client.Version,
},
}
if u.Flag&nmdcp.FlagTLS != 0 {
user.Client.Ext = append(user.Client.Ext, "TLS")
}
if u.Flag&nmdcp.FlagIPv4 != 0 {
user.Client.Ext = append(user.Client.Ext, adcp.FeaTCP4.String())
}
if u.Flag&nmdcp.FlagIPv6 != 0 {
user.Client.Ext = append(user.Client.Ext, adcp.FeaTCP6.String())
}
info.UserList = append(info.UserList, user)
}
return info, err
case adcSchema, adcsSchema:
hub, err := adc.Ping(ctx, addr, *conf)
if err != nil && hub == nil {
return nil, err
}
info := &HubInfo{
Name: hub.Name,
Desc: hub.Desc,
Addr: []string{addr},
KeyPrints: hub.KeyPrints,
Enc: "utf-8",
Website: hub.Website,
Owner: hub.Owner,
Uptime: uint64(hub.Uptime),
Server: &Software{
Name: hub.Application,
Version: hub.Version,
Ext: hub.Ext,
},
Users: len(hub.Users),
UserList: make([]HubUser, 0, len(hub.Users)),
}
for _, u := range hub.Users {
u.Normalize()
info.Files += uint64(u.ShareFiles)
info.Share += uint64(u.ShareSize)
user := HubUser{
Name: string(u.Name),
Files: u.ShareFiles,
Share: uint64(u.ShareSize),
Email: u.Email,
Client: &Software{
Name: u.Application,
Version: u.Version,
},
}
for _, f := range u.Features {
user.Client.Ext = append(user.Client.Ext, f.String())
}
info.UserList = append(info.UserList, user)
}
return info, err
default:
return nil, fmt.Errorf("unsupported protocol: %q", addr)
}
}
type HubInfo struct {
Name string `json:"name" xml:"Name,attr"`
Desc string `json:"desc,omitempty" xml:"Description,attr,omitempty"`
Addr []string `json:"addr,omitempty" xml:"Address,attr,omitempty"`
KeyPrints []string `json:"kp,omitempty" xml:"KP,attr,omitempty"`
Icon string `json:"icon,omitempty" xml:"Icon,attr,omitempty"`
Owner string `json:"owner,omitempty" xml:"Owner,attr,omitempty"`
Website string `json:"website,omitempty" xml:"Website,attr,omitempty"`
Email string `json:"email,omitempty" xml:"Email,attr,omitempty"`
Enc string `json:"encoding,omitempty" xml:"Encoding,attr,omitempty"`
Server *Software `json:"soft,omitempty" xml:"Software,omitempty"`
Uptime uint64 `json:"uptime,omitempty" xml:"Uptime,attr,omitempty"`
Users int `json:"users" xml:"Users,attr"`
Files uint64 `json:"files,omitempty" xml:"Files,attr,omitempty"`
Share uint64 `json:"share,omitempty" xml:"Shared,attr,omitempty"`
Redirect string `json:"redirect,omitempty" xml:"Redirect,attr,omitempty"`
UserList []HubUser `json:"userlist,omitempty" xml:"User,attr,omitempty"`
}
type HubUser struct {
Name string `json:"name" xml:"Name,attr"`
Client *Software `json:"soft,omitempty" xml:"Software,omitempty"`
Files int `json:"files,omitempty" xml:"Files,attr,omitempty"`
Share uint64 `json:"share,omitempty" xml:"Shared,attr,omitempty"`
Email string `json:"email,omitempty" xml:"Email,attr,omitempty"`
}
// Software version.
type Software struct {
Name string `json:"name" xml:"Name,attr"`
Version string `json:"vers,omitempty" xml:"Version,attr,omitempty"`
Ext []string `json:"ext,omitempty" xml:"Ext,attr,omitempty"`
}