-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_info.go
355 lines (320 loc) · 9.19 KB
/
server_info.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright 2009-2013,2017 Phil Pennock
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sks_spider
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"
// "go-html-transform" -- crashes parsing SKS output
// ehtml "exp/html" -- handles it, no xpath
htmlp "github.com/sergioangulo/gokogiri/html"
xml "github.com/sergioangulo/gokogiri/xml"
)
type SksNode struct {
// Be sure that types of Exported fields are loadable from JSON!
Hostname string
Port int
initialised bool
uriRel string
uri string
Status string
ServerHeader string
ViaHeader string
Settings map[string]string
GossipPeers map[string]string
GossipPeerList []string
MailsyncPeers []string
Version string
Software string
Keycount int
pageContent *htmlp.HtmlDocument
pageJson map[string]interface{}
analyzeError error
// And these are populated when converted into a HostMap
AnalyzeError string
IpList []string
Aliases []string
Distance int
}
var initHTTPOnce sync.Once
var ourTransport *http.Transport
var ourHTTPClient *http.Client
func getHTTPClient() *http.Client {
initHTTPOnce.Do(func() {
ourTransport = &http.Transport{
DisableKeepAlives: true,
ResponseHeaderTimeout: *flHttpFetchTimeout,
}
ourHTTPClient = &http.Client{Transport: ourTransport}
})
return ourHTTPClient
}
func (sn *SksNode) Dump(out io.Writer) {
fmt.Fprintf(out, "NODE: %s %d <%s>\n\tServer: %s\n\tVia: %s\n",
sn.Hostname, sn.Port, sn.uri, sn.ServerHeader, sn.ViaHeader)
fmt.Fprintf(out, "\tSoftware: %s\tVersion: %s\n\tKeycount: %d\n",
sn.Software, sn.Version, sn.Keycount)
for _, mail := range sn.MailsyncPeers {
fmt.Fprintf(out, "\tMailsync to: %s\n", mail)
}
for k, v := range sn.Settings {
fmt.Fprintf(out, "\tS: \"%s\" = \"%s\"\n", k, v)
}
for k, v := range sn.GossipPeers {
fmt.Fprintf(out, "\tP: %s %s\n", k, v)
}
if sn.pageContent != nil {
fmt.Fprintf(out, "\t%+v\n", sn.pageContent)
} else {
fmt.Fprint(out, "\tno page content\n")
}
}
func (sn *SksNode) Normalize() bool {
if sn.Hostname == "" {
return false
}
if sn.initialised {
return true
}
if sn.Port == 0 {
sn.Port = *flSksPortHkp
}
if sn.Distance == 0 {
// Will be overriden from the spider later
sn.Distance = -1
}
sn.uriRel = "/pks/lookup?op=stats&options=mr"
sn.uri = fmt.Sprintf("http://%s:%d%s", sn.Hostname, sn.Port, sn.uriRel)
sn.initialised = true
return true
}
// Dump the large content, let garbage collection reclaim space
func (sn *SksNode) Minimize() {
if sn.pageContent != nil {
sn.pageContent.Free()
sn.pageContent = nil
}
// TODO: work out how to Free() the unstructured JSON object
// if sn.pageJson != nil {
// sn.pageJson.Free()
// sn.pageJson = nil
// }
}
func (sn *SksNode) Fetch() error {
sn.Normalize()
req, err := http.NewRequest("GET", sn.uri, nil)
if err != nil {
return err
}
// allow more time for whole context than for HTTP headers, hanoi stacking
ctx, cancel := context.WithTimeout(context.Background(), *flHttpFetchTimeout+2*time.Second)
defer cancel()
req = req.WithContext(ctx)
req.Header.Set("User-Agent", "sks_peers/0.2 (SKS mesh spidering)")
cl := getHTTPClient()
resp, err := cl.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
sn.Status = resp.Status
Log.Printf("[%s] Response status: %s", sn.Hostname, sn.Status)
sn.ServerHeader = resp.Header.Get("Server")
sn.ViaHeader = resp.Header.Get("Via")
//doc, err := ehtml.Parse(resp.Body)
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// try unmarshaling as JSON first
var foo map[string]interface{}
err = json.Unmarshal([]byte(buf), &foo)
if err == nil {
sn.pageJson = foo
return nil
}
// otherwise assume it's an SKS-style HTML page
doc, err := htmlp.Parse(buf, htmlp.DefaultEncodingBytes, nil, htmlp.DefaultParseOption, htmlp.DefaultEncodingBytes)
if err != nil {
return err
}
sn.pageContent = doc
return nil
}
func (sn *SksNode) tableFollowing(search string) (table *xml.Node, err error) {
if strings.ContainsRune(search, '"') {
panic(fmt.Sprintf("Malformed search pattern {{{%s}}}", search))
}
res, err := sn.pageContent.Root().Search(fmt.Sprintf(`//*[text()="%s"]`, search))
if err != nil {
return nil, err
}
if len(res) < 1 {
return nil, fmt.Errorf("Failed to find search text \"%s\"", search)
}
// NB: This only works for siblings, doesn't chase up, like python html5lib's .getnext() [lxml treebuilder]
s := res[0]
for s.Name() != "table" {
s = s.NextSibling()
}
return &s, nil
}
func (sn *SksNode) plainRowsOf(search string) ([]string, error) {
var rows []string
table, err := sn.tableFollowing(search)
if err != nil {
return nil, err
}
//debugDumpMethods(*table)
nodelist, err := (*table).Search(".//td")
if err != nil {
return nil, err
}
for i := range nodelist {
text := strings.TrimSpace(nodelist[i].Content())
rows = append(rows, text)
}
return rows, nil
}
func (sn *SksNode) dictFromPlainRows(search string) (map[string]string, error) {
rows, err := sn.plainRowsOf(search)
if err != nil {
return nil, err
}
var dict = make(map[string]string)
for i := range rows {
elems := strings.SplitN(rows[i], " ", 2)
dict[elems[0]] = elems[1]
}
return dict, err
}
func (sn *SksNode) kvdictFromTable(search string) (map[string]string, error) {
table, err := sn.tableFollowing(search)
if err != nil {
return nil, err
}
nodelist, err := (*table).Search(".//tr")
if err != nil {
return nil, err
}
var dict = make(map[string]string)
for i := range nodelist {
columns, err := nodelist[i].Search(".//td")
if err != nil {
continue
}
key := strings.TrimSpace(columns[0].Content())
value := strings.TrimSpace(columns[1].Content())
key = strings.TrimRight(key, ":")
dict[key] = value
}
return dict, nil
}
func (sn *SksNode) Analyze() {
if !strings.HasPrefix(sn.Status, "200") {
sn.Keycount = -2
sn.analyzeError = fmt.Errorf("HTTP GET failure: %s", sn.Status)
return
}
if sn.pageJson != nil {
settings := make(map[string]string, 10)
for key, val := range sn.pageJson {
if valString, ok := val.(string); ok == true {
settings[strings.Title(key)] = valString
} else if valNum, ok := val.(float64); ok == true {
settings[strings.Title(key)] = strconv.Itoa(int(valNum))
}
}
sn.Settings = settings
sn.Version = sn.Settings["Version"]
sn.Software = sn.Settings["Software"]
err := error(nil)
sn.Keycount, err = strconv.Atoi(sn.Settings["Numkeys"])
if err != nil {
sn.Keycount, err = strconv.Atoi(sn.Settings["Total"])
if err != nil {
sn.Keycount = -1
}
}
if peerArray, ok := sn.pageJson["peers"].([]interface{}); ok == true {
sn.GossipPeers = make(map[string]string, len(peerArray))
sn.GossipPeerList = make([]string, len(peerArray))
for i, peer := range peerArray {
if peerMap, ok := peer.(map[string]interface{}); ok == true {
if reconAddr, ok := peerMap["reconAddr"].(string); ok == true {
if strings.ContainsAny(reconAddr, ":") {
// TODO: split this on the LAST colon, but only if there are no spaces (ipv6 parsing)
reconAddr = strings.Replace(reconAddr, ":", " ", 1)
}
sn.GossipPeers[strings.Fields(reconAddr)[0]] = strings.Fields(reconAddr)[1]
sn.GossipPeerList[i] = strings.Fields(reconAddr)[0]
}
}
}
}
} else {
if mailsync, err := sn.plainRowsOf("Outgoing Mailsync Peers"); err == nil {
sn.MailsyncPeers = mailsync
}
if settings, err := sn.kvdictFromTable("Settings"); err == nil {
sn.Settings = settings
}
sn.Version = sn.Settings["Version"]
sn.Software = sn.Settings["Software"]
if res, err := sn.pageContent.Root().Search(`//h2[text()="Statistics"]`); err == nil {
content := res[0].NextSibling().Content()
if strings.HasPrefix(content, "Total number of keys") {
content = strings.TrimSpace(strings.SplitN(content, ":", 2)[1])
sn.Keycount, err = strconv.Atoi(content)
if err != nil {
sn.Keycount = -1
}
}
}
if peers, err := sn.dictFromPlainRows("Gossip Peers"); err == nil {
sn.GossipPeerList = make([]string, len(peers))
var i = 0
for k := range peers {
sn.GossipPeerList[i] = k
i += 1
}
for _, k := range sn.GossipPeerList {
if strings.ContainsAny(peers[k], " \t") {
peers[k] = strings.Fields(peers[k])[0]
}
}
sn.GossipPeers = peers
}
}
sn.Minimize()
}
func (sn *SksNode) Url() string {
if sn.uri != "" {
return sn.uri
}
// JSON reloaded
return fmt.Sprintf("http://%s:%d/pks/lookup?op=stats&options=mr", sn.Hostname, sn.Port)
}
func NodeUrl(name string, sn *SksNode) string {
if sn != nil {
return sn.Url()
}
return fmt.Sprintf("http://%s:%d/pks/lookup?op=stats&options=mr", name, *flSksPortHkp)
}