This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
forked from NullHypothesis/zoossh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
272 lines (215 loc) · 7.49 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
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
// Provides utility functions.
package zoossh
import (
"bufio"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"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)
// DescCache maps a descriptor's digest to its router descriptor.
var DescCache = make(map[string]*RouterDescriptor)
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
}
// This is the same regexp Stem uses.
// https://gitweb.torproject.org/stem.git/tree/stem/descriptor/__init__.py?id=1.4.1#n182
var annotationRegexp = regexp.MustCompile(`^@type (\S+) (\d+)\.(\d+)$`)
// parseAnnotation parses a type annotation string in the form
// "@type $descriptortype $major.$minor".
func parseAnnotation(annotationText string) (*Annotation, error) {
matches := annotationRegexp.FindStringSubmatch(annotationText)
if matches == nil {
return nil, fmt.Errorf("bad syntax: %q", annotationText)
}
annotation := new(Annotation)
annotation.Type = matches[1]
annotation.Major = matches[2]
annotation.Minor = matches[3]
return annotation, nil
}
// 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
}
// readAnnotation reads and parses the first line of the the io.Reader, then
// returns the resulting *Annotation as well as a new io.Reader ready to read
// the rest of the file.
func readAnnotation(r io.Reader) (*Annotation, io.Reader, error) {
br := bufio.NewReader(r)
// The annotation is placed in the first line of the file. See the
// following URL for details:
// <https://collector.torproject.org/formats.html>
// Use ReadSlice rather than ReadBytes in order to get ErrBufferFull
// when there is no '\n' byte.
slice, err := br.ReadSlice('\n')
if err != nil {
return nil, nil, err
}
// Trim the trailing '\n'.
line := string(slice[:len(slice)-1])
annotation, err := parseAnnotation(line)
if err != nil {
return nil, nil, err
}
return annotation, br, nil
}
// Checks the type annotation in the given io.Reader. The Annotation struct
// determines what we want to see. If we don't see the expected annotation, an
// error string is returned.
func readAndCheckAnnotation(r io.Reader, expected map[Annotation]bool) (io.Reader, error) {
observed, r, err := readAnnotation(r)
if err != nil {
return nil, err
}
for annotation, _ := range expected {
// We support the observed annotation.
if annotation.Equals(observed) {
return r, nil
}
}
return nil, fmt.Errorf("Unexpected file annotation: %s", observed)
}
// 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()
annotation, _, err := readAnnotation(fd)
if err != nil {
return nil, fmt.Errorf("Could not read file annotation for \"%s\": %s", fileName, err)
}
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://metrics.torproject.org/collector.html#data-formats>
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)
observed, err := parseAnnotation(annotation)
if err != nil {
return err
}
for annotation, _ := range expected {
// We support the observed annotation.
if annotation.Equals(observed) {
return nil
}
}
return fmt.Errorf("Unexpected file annotation: %q", annotation)
}
// 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(r io.Reader, extractor bufio.SplitFunc, queue chan QueueUnit) {
defer close(queue)
scanner := bufio.NewScanner(r)
scanner.Split(extractor)
for scanner.Scan() {
unit := scanner.Text()
queue <- QueueUnit{unit, nil}
}
if err := scanner.Err(); err != nil {
queue <- QueueUnit{"", err}
}
}
// 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) {
// Check if we already have the descriptor in our local cache.
if desc, exists := DescCache[digest]; exists {
return desc, nil
}
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
}
DescCache[digest] = d
return d, nil
}