1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-23 05:39:03 +08:00
nali/pkg/cdn/cdn.go
2020-07-22 07:25:01 +08:00

51 lines
859 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cdn
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
type CDN struct {
Data CDNDist
}
type CDNDist map[string]CDNResult
type CDNResult struct {
Name string `json:"name"`
Link string `json:"link"`
}
func NewCDN(filePath string) CDN {
cdnDist := make(CDNDist)
cdnData := make([]byte, 0)
_, err := os.Stat(filePath)
if err != nil && os.IsNotExist(err) {
log.Println("文件不存在尝试从网络获取最新CDN数据库")
cdnData, err = Download(filePath)
if err != nil {
os.Exit(1)
}
} else {
cdnFile, err := os.OpenFile(filePath, os.O_RDONLY, 0400)
if err != nil {
panic(err)
}
defer cdnFile.Close()
cdnData, err = ioutil.ReadAll(cdnFile)
if err != nil {
panic(err)
}
}
err = json.Unmarshal(cdnData, &cdnDist)
if err != nil {
panic("cdn data parse failed!")
}
return CDN{Data: cdnDist}
}