2020-07-18 14:18:54 +08:00
|
|
|
|
package cdn
|
|
|
|
|
|
|
|
|
|
import (
|
2021-08-02 12:01:25 +08:00
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2020-07-18 14:18:54 +08:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
2021-08-02 12:01:25 +08:00
|
|
|
|
"strings"
|
2022-04-03 17:25:55 +08:00
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2020-07-18 14:18:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CDN struct {
|
|
|
|
|
Data CDNDist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CDNDist map[string]CDNResult
|
|
|
|
|
|
|
|
|
|
type CDNResult struct {
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Link string `json:"link"`
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 12:01:25 +08:00
|
|
|
|
func (r CDNResult) String() string {
|
|
|
|
|
return r.Name
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 12:34:11 +08:00
|
|
|
|
func NewCDN(filePath string) (*CDN, error) {
|
2020-07-18 14:18:54 +08:00
|
|
|
|
cdnDist := make(CDNDist)
|
|
|
|
|
cdnData := make([]byte, 0)
|
|
|
|
|
|
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
|
log.Println("文件不存在,尝试从网络获取最新CDN数据库")
|
2020-07-22 07:25:01 +08:00
|
|
|
|
cdnData, err = Download(filePath)
|
2020-07-18 14:18:54 +08:00
|
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
|
return nil, err
|
2020-07-18 14:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
cdnFile, err := os.OpenFile(filePath, os.O_RDONLY, 0400)
|
|
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
|
return nil, err
|
2020-07-18 14:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
defer cdnFile.Close()
|
|
|
|
|
|
|
|
|
|
cdnData, err = ioutil.ReadAll(cdnFile)
|
|
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
|
return nil, err
|
2020-07-18 14:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 17:25:55 +08:00
|
|
|
|
err = yaml.Unmarshal(cdnData, &cdnDist)
|
2020-07-18 14:18:54 +08:00
|
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
|
return nil, err
|
2020-07-18 14:18:54 +08:00
|
|
|
|
}
|
2022-03-02 12:34:11 +08:00
|
|
|
|
return &CDN{Data: cdnDist}, nil
|
2021-08-02 12:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (db CDN) Find(query string, params ...string) (result fmt.Stringer, err error) {
|
|
|
|
|
baseCname := parseBaseCname(query)
|
2021-12-16 13:20:24 +08:00
|
|
|
|
for _, domain := range baseCname {
|
|
|
|
|
if domain != "" {
|
|
|
|
|
cdnResult, found := db.Data[domain]
|
|
|
|
|
if found {
|
|
|
|
|
return cdnResult, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-02 12:01:25 +08:00
|
|
|
|
|
2021-12-16 13:20:24 +08:00
|
|
|
|
if strings.Contains(domain, "kunlun") {
|
|
|
|
|
return CDNResult{
|
|
|
|
|
Name: "阿里云 CDN",
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
2021-08-02 12:01:25 +08:00
|
|
|
|
}
|
2021-12-16 13:20:24 +08:00
|
|
|
|
|
2021-08-02 12:01:25 +08:00
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 13:20:24 +08:00
|
|
|
|
func parseBaseCname(domain string) (result []string) {
|
|
|
|
|
parts := strings.Split(domain, ".")
|
|
|
|
|
size := len(parts)
|
|
|
|
|
if size == 0 {
|
|
|
|
|
return []string{}
|
|
|
|
|
}
|
|
|
|
|
domain = parts[size-1]
|
|
|
|
|
result = append(result, domain)
|
|
|
|
|
for i := len(parts) - 2; i >= 0; i-- {
|
|
|
|
|
domain = parts[i] + "." + domain
|
|
|
|
|
result = append(result, domain)
|
2021-08-02 12:01:25 +08:00
|
|
|
|
}
|
2021-12-16 13:20:24 +08:00
|
|
|
|
return result
|
2020-07-18 14:18:54 +08:00
|
|
|
|
}
|