-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain.go
More file actions
132 lines (105 loc) · 3.08 KB
/
domain.go
File metadata and controls
132 lines (105 loc) · 3.08 KB
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
package goriffle
import (
"regexp"
"strings"
)
const ACTION_SEPARATOR string = "/"
const DOMAIN_SEPARATOR string = "."
// Check out the golang tester for more info:
// https://regex-golang.appspot.com/assets/html/index.html
func validEndpoint(s string) bool {
r, _ := regexp.Compile("(^([a-z]+)(.[a-z]+)*$)")
return r.MatchString(s)
}
func validDomain(s string) bool {
return true
}
func validAction(s string) bool {
return true
}
// Extract action from endpoint. Endpoint without a closing slash
// indicating an action is considered an error.
func extractActions(s string) (string, error) {
i := strings.Index(s, "/")
// No slash found, error
if i == -1 {
return "", InvalidURIError(s)
}
i += 1
return s[i:], nil
}
// Extract domain from endpoint, returning an error
func extractDomain(s string) (string, error) {
i := strings.Index(s, "/")
// out.Critical("Index of string: %s", i)
if i == -1 {
return "", InvalidURIError(s)
}
return s[:i], nil
}
// Extract the top level from a domain.
// Example: xs.a.b -> xs
func topLevelDomain(subdomain string) string {
parts := strings.Split(subdomain, DOMAIN_SEPARATOR)
return parts[0]
}
// Generate list of ancestors up to the top level.
// If add is not "", it will be appended to each result.
// A result is not returned if it is equal to the argument domain.
//
// Example:
// ancestorDomains("xs.X.Y", "") -> ["xs.X", "xs"]
// ancestorDomains("xs.X.Y.Z", "auth") -> ["xs.X.Y.auth", "xs.X.auth", "xs.auth"]
// ancestorDomains("xs.X.Y.auth", "auth") -> ["xs.X.auth", "xs.auth"]
func ancestorDomains(domain string, add string) []string {
var results []string
parts := strings.Split(domain, ".")
for len(parts) > 1 {
// Pop the last part of the domain.
parts = parts[:len(parts)-1]
newResult := strings.Join(parts, ".")
if add != "" {
newResult = newResult + "." + add
}
if newResult != domain {
results = append(results, newResult)
}
}
return results
}
// Checks if the target domain is "down" from the given domain.
// That is-- it is either a subdomain or the same domain.
// Assumes the passed domains are well constructed.
// Agent should be purely a domain string, target may be a domain or an
// endpoint.
func subdomain(agent, target string) bool {
targetDomain, err := extractDomain(target)
if err != nil {
// Error means the target was already a domain (no action string).
targetDomain = target
}
agentParts := strings.Split(agent, DOMAIN_SEPARATOR)
targetParts := strings.Split(targetDomain, DOMAIN_SEPARATOR)
// Target cannot be a subdomain of agent if it is shorter.
if len(targetParts) < len(agentParts) {
return false
}
for i := 0; i < len(agentParts) && i < len(targetParts); i++ {
if targetParts[i] != agentParts[i] {
return false
}
}
return true
}
// breaks down an endpoint into domain and action, or returns an error
func breakdownEndpoint(s string) (string, string, error) {
d, errDomain := extractDomain(s)
if errDomain != nil {
return "", "", InvalidURIError(s)
}
a, errAction := extractActions(s)
if errAction != nil {
return "", "", InvalidURIError(s)
}
return d, a, nil
}