-
Notifications
You must be signed in to change notification settings - Fork 390
/
source.go
93 lines (78 loc) · 2.62 KB
/
source.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package trust
import (
"context"
"regexp"
"github.com/zeebo/errs"
)
// Entry represents a trust entry.
type Entry struct {
// SatelliteURL is the URL of the satellite
SatelliteURL SatelliteURL
// Authoritative indicates whether this entry came from an authoritative
// source. This impacts how URLS are aggregated.
Authoritative bool `json:"authoritative"`
}
// Source is a trust source for trusted Satellites.
type Source interface {
// String is the string representation of the source. It is used as a key
// into the cache.
String() string
// Static returns true if the source is static. Static sources are not cached.
Static() bool
// FetchEntries returns the list of trust entries from the source.
FetchEntries(context.Context) ([]Entry, error)
}
// NewSource takes a configuration string returns a Source for that string.
func NewSource(config string) (Source, error) {
schema, ok := isReserved(config)
if ok {
switch schema {
case "http", "https":
return NewHTTPSource(config)
case "storj":
return NewStaticURLSource(config)
default:
return nil, errs.New("unsupported schema %q", schema)
}
}
if isProbablySatelliteURL(config) {
return NewStaticURLSource(config)
}
return NewFileSource(config), nil
}
var reReserved = regexp.MustCompile(`^([a-zA-Z]{2,})://`)
// isReserved returns the true if the string is within the reserved namespace
// for trust sources, i.e. things that look like a URI scheme. Single letter
// schemes are not in the reserved namespace since those collide with paths
// starting with Windows drive letters.
func isReserved(s string) (schema string, ok bool) {
m := reReserved.FindStringSubmatch(s)
if m == nil {
return "", false
}
return m[1], true
}
// reProbablySatelliteURL matches config strings that are (intended, but
// possibly misconfigured) satellite URLs, like the following:
//
// - @
// - id@
// - host:9999
// - id@host:9999
var reProbablySatelliteURL = regexp.MustCompile(`@|(^[^/\\]{2,}:\d+$)`)
func isProbablySatelliteURL(s string) bool {
// Painful esoteric paths to consider if you want to change the regex. None
// of the paths below should be parsed as satellite URLs, which the
// exception of the last one, which would fail since it does not contain an
// ID portion but would fail with good diagnostics.
// 1. http://basic:auth@example.com
// 2. c:/windows
// 3. c:\\windows
// 3. \\?\c:\\windows
// 4. probably other nightmarish windows paths
// 5. /posix/paths:are/terrible
// 6. posix.paths.are.really.terrible:7777
return reProbablySatelliteURL.MatchString(s)
}