/
https.go
95 lines (76 loc) · 2.02 KB
/
https.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
package link
import (
"fmt"
"regexp"
"strings"
"github.com/keys-pub/keys/encoding"
"github.com/pkg/errors"
)
type https struct{}
// HTTPS service.
var HTTPS = &https{}
func (s *https) ID() string {
return "https"
}
func (s *https) NormalizeName(name string) string {
name = strings.ToLower(name)
return name
}
func (s *https) ValidateName(name string) error {
isASCII := encoding.IsASCII([]byte(name))
if !isASCII {
return errors.Errorf("name has non-ASCII characters")
}
hu := encoding.HasUpper(name)
if hu {
return errors.Errorf("name should be lowercase")
}
if len(name) > 256 {
return errors.Errorf("name is too long")
}
if !isValidHostname(name) {
return errors.Errorf("not a valid domain name")
}
if regexIP.MatchString(name) {
return errors.Errorf("not a valid domain name")
}
return nil
}
func (s *https) NormalizeURLString(name string, urs string) (string, error) {
return basicURLString(strings.ToLower(urs))
}
func (s *https) ValidateURLString(name string, urs string) (string, error) {
if err := s.ValidateName(name); err != nil {
return "", errors.Wrapf(err, "invalid url")
}
matches := []string{
fmt.Sprintf("https://%s/keyspub.txt", name),
fmt.Sprintf("https://%s/.well-known/keyspub.txt", name),
}
for _, m := range matches {
if urs == m {
return urs, nil
}
}
return "", errors.Errorf("invalid url: %s", urs)
}
func (s *https) CheckContent(name string, b []byte) ([]byte, error) {
return b, nil
}
var regexIP = regexp.MustCompile(`^[0-9].*\.[0-9].*\.[0-9].*\.[0-9].*$`)
// From github.com/keybase/client/libkb/util.go
// Found regex here: http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
var regexHostname = regexp.MustCompile("^(?i:[a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])$")
func isValidHostname(s string) bool {
parts := strings.Split(s, ".")
if len(parts) < 2 {
return false
}
for _, p := range parts {
if !regexHostname.MatchString(p) {
return false
}
}
// TLDs must be >=2 chars
return len(parts[len(parts)-1]) >= 2
}