/
util.go
184 lines (157 loc) · 3.89 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
package util
import (
"math/rand"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"github.com/lkarlslund/adalanche/modules/ui"
"github.com/gofrs/uuid"
"github.com/lkarlslund/stringsplus"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var legalMatch = regexp.MustCompile("[[:alnum:] _.=,-]") // dash must be LAST! doh
func cleanfilename(input string) string {
normalized, _, _ := transform.String(transform.Chain(norm.NFD, transform.RemoveFunc(func(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}), norm.NFC), input)
var output string
for _, chr := range normalized {
if legalMatch.MatchString(string(chr)) {
if chr == '*' || chr == '/' {
ui.Fatal().Msgf("This isn't working")
}
output += string(chr)
}
}
return output
}
func SwapUUIDEndianess(u uuid.UUID) uuid.UUID {
var r uuid.UUID
r[0], r[1], r[2], r[3] = u[3], u[2], u[1], u[0]
r[4], r[5] = u[5], u[4]
r[6], r[7] = u[7], u[6]
copy(r[8:], u[8:])
return r
}
func ParseBool(input string) (bool, error) {
result, err := strconv.ParseBool(input)
if err == nil {
return result, err
}
switch input {
case "on", "On":
return true, nil
case "off", "Off":
return false, nil
}
return result, err
}
var nolaterthan, _ = time.Parse("20060102", "99991231")
// Filetype converts 100-nanoseconds intervals since Jan 1, 1601 UTC to time.Time
func FiletimeToTime(filetime uint64) time.Time {
// We assume that a zero time is a blank time
if filetime == 0 || filetime == 0xFFFFFFFFFFFFFFFF {
return time.Time{}
}
// First convert 100-ns intervals to microseconds, then adjust for the epoch difference
unixsec := int64((filetime / 10000000) - 11644473600)
unixns := int64((filetime * 10) % 1000000000)
t := time.Unix(unixsec, unixns)
if t.After(nolaterthan) {
t = nolaterthan
}
return t
}
func IsASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
func Hexify(s string) string {
var o string
for _, c := range s {
if unicode.IsPrint(c) {
o += string(c)
} else {
o += "\\x" + strconv.FormatInt(int64(c), 16)
}
}
return o
}
func Default(values ...string) string {
for _, value := range values {
if len(value) != 0 {
return value
}
}
return ""
}
func StringScrambler(s string) string {
var result string
for _, c := range s {
switch {
case c == ' ', c == '(', c == '-', c == ')':
result += string(c)
case c >= '0' && c <= '9':
result += string('0' + byte(rand.Intn(9)))
case c >= 'A' && c <= 'Z':
result += string('A' + byte(rand.Intn(25)))
case c >= 'a' && c <= 'z':
result += string('a' + byte(rand.Intn(25)))
}
}
return result
}
func ExtractNetbiosFromBase(dn string) string {
elements := strings.Split(dn, ",")
_, netbios, _ := strings.Cut(elements[0], "=")
return netbios
}
func ParentDistinguishedName(dn string) string {
for {
firstcomma := strings.Index(dn, ",")
if firstcomma == -1 {
return "" // At the top
}
if firstcomma > 0 {
if dn[firstcomma-1] == '\\' {
// False alarm, strip it and go on
dn = dn[firstcomma+1:]
continue
}
}
dn = dn[firstcomma+1:]
break
}
return dn
}
func ExtractDomainContextFromDistinguishedName(dn string) string {
elements := strings.Split(dn, ",")
last := len(elements)
first := last // assume we have nothing
for i := len(elements) - 1; i >= 0; i-- {
if stringsplus.EqualFoldHasPrefix(elements[i], "dc=") {
first = i
} else {
break
}
}
return strings.ToLower(strings.Join(elements[first:last], ","))
}
func DomainContextToDomainSuffix(dn string) string {
elements := strings.Split(dn, ",")
for i, element := range elements {
elements[i] = strings.TrimPrefix(strings.ToLower(element), "dc=")
}
return strings.Join(elements, ".")
}
func DomainSuffixToDomainContext(domain string) string {
parts := strings.Split(domain, ".")
return strings.ToLower("dc=" + strings.Join(parts, ",dc="))
}