-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprint.go
56 lines (46 loc) · 1.54 KB
/
print.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
// Pretty-print objects for easy grepping.
package main
import (
"fmt"
"log"
"sync"
tor "git.torproject.org/user/phw/zoossh.git"
)
var printedBanner bool = false
// PrintInfo prints a router status. If we also have access to router
// descriptors, we print those too.
func PrintInfo(descriptorDir string, status *tor.RouterStatus) {
desc, err := tor.LoadDescriptorFromDigest(descriptorDir, status.Digest, status.Publication)
if err == nil {
if !printedBanner {
fmt.Println("fingerprint,nickname,ip_addr,or_port,dir_port,flags,published,version,platform,bandwidthavg,bandwidthburst,uptime,familysize")
printedBanner = true
}
fmt.Printf("%s,%s,%d,%d,%d,%d\n", status, desc.OperatingSystem, desc.BandwidthAvg, desc.BandwidthBurst, desc.Uptime, len(desc.Family))
} else {
if !printedBanner {
fmt.Println("fingerprint,nickname,ip_addr,or_port,dir_port,flags,published,version")
printedBanner = true
}
fmt.Println(status)
}
}
// PrettyPrint prints all objects within the object sets received over the
// given channel. The output is meant to be human-readable and easy to analyse
// and grep.
func PrettyPrint(channel chan tor.ObjectSet, params *CmdLineParams, group *sync.WaitGroup) {
defer group.Done()
counter := 0
for objects := range channel {
for object := range objects.Iterate(params.Filter) {
counter += 1
switch obj := object.(type) {
case *tor.RouterStatus:
PrintInfo(params.DescriptorDir, obj)
case *tor.RouterDescriptor:
fmt.Println(obj)
}
}
}
log.Printf("Printed %d objects.\n", counter)
}