-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomains.go
More file actions
48 lines (38 loc) · 1.13 KB
/
domains.go
File metadata and controls
48 lines (38 loc) · 1.13 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
package main
import (
"fmt"
"strings"
"golang.org/x/net/idna"
)
// Converts given string of domains to punnycode
func toPunnycode(domains string) ([]string, error) {
var punnycode []string
for _, d := range strings.Split(domains, ",") {
domain, err := idna.ToASCII(d)
if err != nil {
err = fmt.Errorf("cannot convert domain '%v' to punnycode: %v", d, err)
return punnycode, err
}
punnycode = append(punnycode, domain)
}
return punnycode, nil
}
// Converts given string from punnycode to string
func fromPunnycode(domain string) (string, error) {
var decoded string
decoded, err := idna.ToUnicode(domain)
if err != nil {
err = fmt.Errorf("cannot convert punnycode '%v' to domain: %v", decoded, err)
return decoded, err
}
return decoded, nil
}
// Returns the bare domain from the subdomain+domain string
func getDomain(subdomain string) (string, error) {
components := strings.Split(subdomain, ".")
comLen := len(components)
if comLen == 3 {
return fmt.Sprintf("%v.%v", components[comLen-2], components[comLen-1]), nil
}
return "", fmt.Errorf("invalid subdomain, expected 'sub.domain.tld', got '%v'", subdomain)
}