forked from NullHypothesis/zoossh
/
util.go
233 lines (184 loc) · 6.09 KB
/
util.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
// Provides utility functions.
package zoossh
import (
"bufio"
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type QueueUnit struct {
Blurb string
Err error
}
type Annotation struct {
Type string
Major string
Minor string
}
// Extracts a string unit from an archive file that can be readily thrown into
// the respective parser.
type StringExtractor func(string) (string, bool, error)
func (a *Annotation) String() string {
return fmt.Sprintf("@type %s %s.%s", a.Type, a.Major, a.Minor)
}
// Equals checks whether the two given annotations have the same content.
func (a *Annotation) Equals(b *Annotation) bool {
return (*a).Type == (*b).Type && (*a).Major == (*b).Major && (*a).Minor == (*b).Minor
}
// Decodes the given Base64-encoded string and returns the resulting string.
// If there are errors during decoding, an error string is returned.
func Base64ToString(encoded string) (string, error) {
// dir-spec.txt says that Base64 padding is removed so we have to account
// for that here.
if rem := len(encoded) % 4; rem != 0 {
encoded += strings.Repeat("=", 4-rem)
}
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return "", err
}
return hex.EncodeToString(decoded), nil
}
// GetAnnotation obtains and returns the given file's annotation. If anything
// fails in the process, an error string is returned.
func GetAnnotation(fileName string) (*Annotation, error) {
fd, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer fd.Close()
// Fetch the file's first line which should be the annotation.
scanner := bufio.NewScanner(fd)
scanner.Scan()
annotationText := scanner.Text()
annotation := new(Annotation)
// We expect "@type TYPE VERSION".
words := strings.Split(annotationText, " ")
if len(words) != 3 {
return nil, fmt.Errorf("Could not parse file annotation for \"%s\".", fileName)
}
annotation.Type = words[1]
// We expect "MAJOR.MINOR".
version := strings.Split(words[2], ".")
if len(version) != 2 {
return nil, fmt.Errorf("Could not parse file annotation for \"%s\".", fileName)
}
annotation.Major = version[0]
annotation.Minor = version[1]
return annotation, nil
}
// Checks the type annotation in the given file. The Annotation struct
// determines what we want to see in the file. If we don't see the expected
// annotation, an error string is returned.
func CheckAnnotation(fd *os.File, expected map[Annotation]bool) error {
before, err := fd.Seek(0, os.SEEK_CUR)
if err != nil {
return err
}
// The annotation is placed in the first line of the file. See the
// following URL for details:
// <https://collector.torproject.org/formats.html>
scanner := bufio.NewScanner(fd)
scanner.Scan()
annotation := scanner.Text()
// Set file descriptor back because NewScanner() reads and buffers large
// chunks of data.
fd.Seek(before+int64(len(annotation)), os.SEEK_SET)
invalidFormat := fmt.Errorf("Unexpected file annotation: %s", annotation)
// We expect "@type TYPE VERSION".
words := strings.Split(annotation, " ")
if len(words) != 3 {
return invalidFormat
}
// We expect "MAJOR.MINOR".
version := strings.Split(words[2], ".")
if len(version) != 2 {
return invalidFormat
}
observed := Annotation{words[1], version[0], version[1]}
for annotation, _ := range expected {
// We support the observed annotation.
if annotation.Equals(&observed) {
return nil
}
}
return invalidFormat
}
// Dissects the given file into string chunks by using the given string
// extraction function. The resulting string chunks are then written to the
// given queue where the receiving end parses them.
func DissectFile(fd *os.File, extractor StringExtractor, queue chan QueueUnit) {
defer close(queue)
blurb, err := ioutil.ReadAll(fd)
if err != nil {
queue <- QueueUnit{"", err}
}
rawContent := string(blurb)
for {
unit, done, err := extractor(rawContent)
if err != nil {
log.Println("Error in extraction function: ", err)
break
}
queue <- QueueUnit{unit, nil}
rawContent = rawContent[len(unit):]
if done {
break
}
}
}
// Convert the given port string to an unsigned 16-bit integer. If the
// conversion fails or the number cannot be represented in 16 bits, 0 is
// returned.
func StringToPort(portStr string) uint16 {
portNum, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return uint16(0)
}
return uint16(portNum)
}
// SanitiseFingerprint returns a sanitised version of the given fingerprint by
// making it upper case and removing leading and trailing white spaces.
func SanitiseFingerprint(fingerprint Fingerprint) Fingerprint {
sanitised := strings.ToUpper(strings.TrimSpace(string(fingerprint)))
return Fingerprint(sanitised)
}
// LoadDescriptorFromDigest takes as input the descriptor directory, a
// descriptor's digest, and the date the digest was created. It then attempts
// to parse and return the descriptor referenced by the digest. The descriptor
// directory expects to contain CollecTor server descriptor archives such as:
// server-descriptors-2015-03/
// server-descriptors-2015-04/
// ...
func LoadDescriptorFromDigest(descriptorDir, digest string, date time.Time) (*RouterDescriptor, error) {
topDir := fmt.Sprintf("server-descriptors-%s", date.Format("2006-01"))
prevTopDir := fmt.Sprintf("server-descriptors-%s", date.AddDate(0, -1, 0).Format("2006-01"))
fileName := filepath.Join(descriptorDir, topDir, digest[0:1], digest[1:2], digest)
// If we cannot find the descriptor file, go one month back in time.
if _, err := os.Stat(fileName); os.IsNotExist(err) {
fileName = filepath.Join(descriptorDir, prevTopDir, digest[0:1], digest[1:2], digest)
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return nil, fmt.Errorf("Could not find digest file %s in %s", digest, descriptorDir)
}
}
descs, err := ParseDescriptorFile(fileName)
if err != nil {
return nil, err
}
if descs.Length() != 1 {
return nil, fmt.Errorf("More than one descriptor in digest file %s. Bug?", fileName)
}
var d *RouterDescriptor
for _, getDesc := range descs.RouterDescriptors {
d = getDesc()
break
}
return d, nil
}