-
Notifications
You must be signed in to change notification settings - Fork 41
/
bucket_checker.go
56 lines (42 loc) · 1.01 KB
/
bucket_checker.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
package main
import (
"errors"
"fmt"
"github.com/miekg/dns"
"strings"
)
type Resolver interface {
IsBucket(string) bool
}
func NewS3Resolver() *S3Resolver {
return &S3Resolver{
dnsClient: dns.Client{},
}
}
type S3Resolver struct {
dnsClient dns.Client
}
const s3host = "s3.amazonaws.com"
// IsBucket determines wheter this prefix is a valid S3 bucket name.
func (s *S3Resolver) IsBucket(name string) bool {
result, err := s.resolveCNAME(fmt.Sprintf("%s.%s.", name, s3host))
if err == nil && !strings.Contains(result, "s3-directional") {
return true
}
return false
}
func (s *S3Resolver) resolveCNAME(name string) (string, error) {
msg := dns.Msg{}
msg.SetQuestion(name, dns.TypeCNAME)
// TODO: Allow the name server to be set by the user.
r, _, err := s.dnsClient.Exchange(&msg, "8.8.8.8:53")
if err != nil {
return "", errors.New("probably a timeout")
}
var answer = r.Answer
if len(answer) == 0 {
return "", errors.New("no resp")
}
var answ = answer[0].(*dns.CNAME).Target
return answ, err
}