-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtraversal.go
52 lines (44 loc) · 1.85 KB
/
traversal.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
// Copyright © by Jeff Foley 2022. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"strings"
)
// FQDNToRegistered executes the provided callback routine for domain names, starting
// with the FQDN to the registered domain name, removing one label with each execution.
// The process stops if the callback routine returns true, indicating completion.
func FQDNToRegistered(fqdn, registered string, callback func(domain string) bool) {
base := len(strings.Split(registered, "."))
labels := strings.Split(fqdn, ".")
max := len(labels) - base
for i := 0; i <= max; i++ {
if callback(strings.Join(labels[i:], ".")) {
break
}
}
}
// RegisteredToFQDN executes the provided callback routine for domain names, starting
// with the registered domain name to the FQDN, adding one label with each execution.
// The process stops if the callback routine returns true, indicating completion.
func RegisteredToFQDN(registered, fqdn string, callback func(domain string) bool) {
base := len(strings.Split(registered, "."))
labels := strings.Split(fqdn, ".")
for i := len(labels) - base; i >= 0; i-- {
if callback(strings.Join(labels[i:], ".")) {
break
}
}
}
// SplitRegisteredToFQDN executes the provided callback routine for domain names, splitting
// the registered domain into a prefix and suffix part and omitting the part in between.
// The process stops if the callback routine returns true, indicating completion.
func SplitRegisteredToFQDN(registered, fqdn string, callback func(prefix, suffix string) bool) {
base := len(strings.Split(registered, "."))
labels := strings.Split(fqdn, ".")
for i := 1; i <= len(labels)-base-1; i++ {
if callback(strings.Join(labels[:i], "."), strings.Join(labels[i+1:], ".")) {
break
}
}
}