-
Notifications
You must be signed in to change notification settings - Fork 2
/
cdncname.go
55 lines (46 loc) · 1.08 KB
/
cdncname.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
// Source: https://raw.githubusercontent.com/turbobytes/cdnfinder/master/assets/cnamechain.json
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"sync"
)
// cdnCNAMEs type will hold CDN CNAMEs information
type cdnCNAMEs struct {
entries [][]string
// initialized is used to find out if entries is laoded or not in an efficient way
initialized bool
sync.Mutex
}
// CDNs holds CDN CNAME information
var cdns cdnCNAMEs
var dbFile = "./data/cnamechain.json"
func parseCNAMEs(loc string) error {
jsonFile, err := readFile(loc)
if err != nil {
return err
}
if err := json.Unmarshal(jsonFile, &cdns.entries); err != nil {
return fmt.Errorf("Error unmarshalling JSON from file to struct: %s", err)
}
cdns.initialized = true
return nil
}
func readFile(loc string) ([]byte, error) {
raw, err := ioutil.ReadFile(loc)
if err != nil {
return raw, err
}
return raw, nil
}
func getCDN(domain string) (cdn string, err error) {
for _, cdnEntry := range cdns.entries {
if strings.Contains(domain, cdnEntry[0]) {
cdn = cdnEntry[1]
break
}
}
return cdn, nil
}