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"
|
2022-06-29 17:00:34 +08:00
|
|
|
|
"regexp"
|
2021-08-02 12:01:25 +08:00
|
|
|
|
"strings"
|
2022-04-03 17:25:55 +08:00
|
|
|
|
|
2022-07-21 14:11:27 +08:00
|
|
|
|
"github.com/zu1k/nali/pkg/download"
|
2022-06-29 17:00:34 +08:00
|
|
|
|
"github.com/zu1k/nali/pkg/re"
|
2022-07-21 14:14:08 +08:00
|
|
|
|
"gopkg.in/yaml.v2"
|
2020-07-18 14:18:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
2022-07-21 14:11:27 +08:00
|
|
|
|
var DownloadUrls = []string{
|
|
|
|
|
"https://cdn.jsdelivr.net/gh/4ft35t/cdn/src/cdn.yml",
|
|
|
|
|
"https://raw.githubusercontent.com/4ft35t/cdn/master/src/cdn.yml",
|
|
|
|
|
"https://raw.githubusercontent.com/SukkaLab/cdn/master/src/cdn.yml",
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-18 14:18:54 +08:00
|
|
|
|
type CDN struct {
|
2022-06-29 17:00:34 +08:00
|
|
|
|
Map map[string]CDNResult
|
|
|
|
|
ReMap []CDNReTuple
|
2020-07-18 14:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 17:00:34 +08:00
|
|
|
|
type CDNReTuple struct {
|
|
|
|
|
*regexp.Regexp
|
|
|
|
|
CDNResult
|
|
|
|
|
}
|
2020-07-18 14:18:54 +08:00
|
|
|
|
|
|
|
|
|
type CDNResult struct {
|
2023-05-20 17:14:03 +08:00
|
|
|
|
Name string `yaml:"name" json:"name"`
|
|
|
|
|
Link string `yaml:"link" json:"link"`
|
2020-07-18 14:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2023-08-24 11:10:52 +08:00
|
|
|
|
var fileData []byte
|
2020-07-18 14:18:54 +08:00
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
|
log.Println("文件不存在,尝试从网络获取最新CDN数据库")
|
2022-07-21 14:11:27 +08:00
|
|
|
|
fileData, err = download.Download(filePath, DownloadUrls...)
|
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()
|
|
|
|
|
|
2022-06-29 17:00:34 +08:00
|
|
|
|
fileData, err = ioutil.ReadAll(cdnFile)
|
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-06-29 17:00:34 +08:00
|
|
|
|
cdnMap := make(map[string]CDNResult)
|
|
|
|
|
err = yaml.Unmarshal(fileData, &cdnMap)
|
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-06-29 17:00:34 +08:00
|
|
|
|
cdnReMap := make([]CDNReTuple, 0)
|
|
|
|
|
for k, v := range cdnMap {
|
|
|
|
|
if re.MaybeRegexp(k) {
|
|
|
|
|
rex, err := regexp.Compile(k)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("[CDN Database] entry %s not a valid regexp", k)
|
|
|
|
|
}
|
|
|
|
|
cdnReMap = append(cdnReMap, CDNReTuple{
|
|
|
|
|
Regexp: rex,
|
|
|
|
|
CDNResult: v,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &CDN{Map: cdnMap, ReMap: cdnReMap}, 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 != "" {
|
2022-06-29 17:00:34 +08:00
|
|
|
|
cdnResult, found := db.Map[domain]
|
2021-12-16 13:20:24 +08:00
|
|
|
|
if found {
|
|
|
|
|
return cdnResult, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-02 12:01:25 +08:00
|
|
|
|
|
2022-06-29 17:00:34 +08:00
|
|
|
|
for _, entry := range db.ReMap {
|
|
|
|
|
if entry.Regexp.MatchString(domain) {
|
|
|
|
|
return entry.CDNResult, nil
|
|
|
|
|
}
|
2021-12-16 13:20:24 +08:00
|
|
|
|
}
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 17:53:43 +08:00
|
|
|
|
func (db CDN) Name() string {
|
|
|
|
|
return "cdn"
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|