-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
73 lines (65 loc) · 1.29 KB
/
utils.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
package sshutils
import (
"encoding/base64"
"log"
"regexp"
"strings"
"golang.org/x/crypto/ssh"
)
var wildcardExpr = strings.NewReplacer(
"\\*", ".*",
"\\?", ".?",
)
type wcMatch []*regexp.Regexp
func (wcs wcMatch) Matches(s string) bool {
for _, wc := range wcs {
if wc.MatchString(s) {
return true
}
}
return false
}
// TODO: error reporting
func wildcards(wcs []string) (rs wcMatch) {
for _, wc := range wcs {
wc := wildcardExpr.Replace(regexp.QuoteMeta(wc))
if r, err := regexp.Compile("^" + wc + "$"); err == nil {
rs = append(rs, r)
} else {
log.Println("Wildcard failed:", err)
}
}
return
}
// Format a host to be in `[host]:port` or `host` format
func canonicalHost(s string) string {
var host, port string
if strings.HasPrefix(s, "[") {
c := strings.Index(s, "]")
if c < 0 {
return s
}
host = s[1:c]
if strings.HasPrefix(s[c+1:], ":") {
// Junk at the end shouldn't result in a valid match
port = s[c+2:]
}
} else if c := strings.LastIndex(s, ":"); c > 0 {
host = s[:c]
port = s[c+1:]
} else {
host = s
}
if port == "22" {
port = ""
}
if port != "" {
return "[" + host + "]:" + port
}
return host
}
func pubkey(pk ssh.PublicKey) string {
t := pk.Type()
b := base64.StdEncoding.EncodeToString(pk.Marshal())
return t + " " + b
}