mirror of
https://github.com/zu1k/nali.git
synced 2025-01-22 13:19:02 +08:00
fix: CDN support regex
Signed-off-by: zu1k <i@zu1k.com>
This commit is contained in:
parent
673e5f4ef7
commit
6bf5d94920
4
go.mod
4
go.mod
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.13.0
|
||||
github.com/ip2location/ip2location-go/v9 v9.3.0
|
||||
github.com/ip2location/ip2location-go/v9 v9.4.0
|
||||
github.com/ipipdotnet/ipdb-go v1.3.1
|
||||
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220624075035-53e6777abd9d
|
||||
@ -35,7 +35,7 @@ require (
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.4.0 // indirect
|
||||
github.com/ulikunitz/xz v0.5.10 // indirect
|
||||
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
|
||||
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
|
||||
golang.org/x/tools v0.1.7 // indirect
|
||||
gopkg.in/ini.v1 v1.66.6 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
4
go.sum
4
go.sum
@ -129,6 +129,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/ip2location/ip2location-go/v9 v9.3.0 h1:qfgr4ppf21ZyrOrmh4/db4SamL9hj+YYEl06dbf1cGM=
|
||||
github.com/ip2location/ip2location-go/v9 v9.3.0/go.mod h1:s5SV6YZL10TpfPpXw//7fEJC65G/yH7Oh+Tjq9JcQEQ=
|
||||
github.com/ip2location/ip2location-go/v9 v9.4.0 h1:VV+clcfWo+BCi37+1zVOLCoyy01GT18/RTtAeIR5EP0=
|
||||
github.com/ip2location/ip2location-go/v9 v9.4.0/go.mod h1:s5SV6YZL10TpfPpXw//7fEJC65G/yH7Oh+Tjq9JcQEQ=
|
||||
github.com/ipipdotnet/ipdb-go v1.3.1 h1:iMTt7a4o8r5FmTMzuHLg8XPtz8vb06gpEzJVSZzDZMY=
|
||||
github.com/ipipdotnet/ipdb-go v1.3.1/go.mod h1:yZ+8puwe3R37a/3qRftXo40nZVQbxYDLqls9o5foexs=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
@ -340,6 +342,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0=
|
||||
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8=
|
||||
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
@ -78,7 +78,7 @@ func GetDefaultDBList() List {
|
||||
|
||||
&DB{
|
||||
Name: "cdn",
|
||||
Format: FormatCDNSkkYml,
|
||||
Format: FormatCDNYml,
|
||||
File: "cdn.yml",
|
||||
Languages: LanguagesZH,
|
||||
Types: TypesCDN,
|
||||
|
@ -46,7 +46,7 @@ func (d *DB) get() (db dbif.DB) {
|
||||
db, err = ip2region.NewIp2Region(filePath)
|
||||
case FormatIP2Location:
|
||||
db, err = ip2location.NewIP2Location(filePath)
|
||||
case FormatCDNSkkYml:
|
||||
case FormatCDNYml:
|
||||
db, err = cdn.NewCDN(filePath)
|
||||
default:
|
||||
panic("DB format not supported!")
|
||||
@ -70,7 +70,7 @@ const (
|
||||
FormatIP2Region = "ip2region"
|
||||
FormatIP2Location = "ip2location"
|
||||
|
||||
FormatCDNSkkYml = "cdn-skk-yml"
|
||||
FormatCDNYml = "cdn-yml"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -84,7 +84,7 @@ func getUpdateFuncByName(name string) (func() error, string) {
|
||||
}
|
||||
return err
|
||||
}, FormatZXIPv6Wry
|
||||
case FormatCDNSkkYml:
|
||||
case FormatCDNYml:
|
||||
return func() error {
|
||||
log.Println("正在下载最新 CDN服务提供商数据库...")
|
||||
_, err := cdn.Download(getDbByName("cdn").File)
|
||||
|
@ -6,16 +6,23 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/zu1k/nali/pkg/re"
|
||||
)
|
||||
|
||||
type CDN struct {
|
||||
Data CDNDist
|
||||
Map map[string]CDNResult
|
||||
ReMap []CDNReTuple
|
||||
}
|
||||
|
||||
type CDNDist map[string]CDNResult
|
||||
type CDNReTuple struct {
|
||||
*regexp.Regexp
|
||||
CDNResult
|
||||
}
|
||||
|
||||
type CDNResult struct {
|
||||
Name string `yaml:"name"`
|
||||
@ -27,13 +34,11 @@ func (r CDNResult) String() string {
|
||||
}
|
||||
|
||||
func NewCDN(filePath string) (*CDN, error) {
|
||||
cdnDist := make(CDNDist)
|
||||
cdnData := make([]byte, 0)
|
||||
|
||||
fileData := make([]byte, 0)
|
||||
_, err := os.Stat(filePath)
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
log.Println("文件不存在,尝试从网络获取最新CDN数据库")
|
||||
cdnData, err = Download(filePath)
|
||||
fileData, err = Download(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -44,33 +49,48 @@ func NewCDN(filePath string) (*CDN, error) {
|
||||
}
|
||||
defer cdnFile.Close()
|
||||
|
||||
cdnData, err = ioutil.ReadAll(cdnFile)
|
||||
fileData, err = ioutil.ReadAll(cdnFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(cdnData, &cdnDist)
|
||||
cdnMap := make(map[string]CDNResult)
|
||||
err = yaml.Unmarshal(fileData, &cdnMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &CDN{Data: cdnDist}, nil
|
||||
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
|
||||
}
|
||||
|
||||
func (db CDN) Find(query string, params ...string) (result fmt.Stringer, err error) {
|
||||
baseCname := parseBaseCname(query)
|
||||
for _, domain := range baseCname {
|
||||
if domain != "" {
|
||||
cdnResult, found := db.Data[domain]
|
||||
cdnResult, found := db.Map[domain]
|
||||
if found {
|
||||
return cdnResult, nil
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(domain, "kunlun") {
|
||||
return CDNResult{
|
||||
Name: "阿里云 CDN",
|
||||
}, nil
|
||||
for _, entry := range db.ReMap {
|
||||
if entry.Regexp.MatchString(domain) {
|
||||
return entry.CDNResult, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,10 +7,9 @@ import (
|
||||
)
|
||||
|
||||
var DownloadUrls = []string{
|
||||
"https://cdn.jsdelivr.net/gh/SukkaLab/cdn/src/cdn.yml",
|
||||
"https://raw.githubusercontent.com/SukkaLab/cdn/master/src/cdn.yml",
|
||||
"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",
|
||||
}
|
||||
|
||||
// Deprecated: This will be removed from 0.5.0, use package download instead
|
||||
|
@ -1,6 +1,9 @@
|
||||
package re
|
||||
|
||||
import "regexp"
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
DomainRe = regexp.MustCompile(`([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+([a-zA-Z][-a-zA-Z]{0,62})`)
|
||||
@ -8,3 +11,7 @@ var (
|
||||
IPv4Re = regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
|
||||
IPv6Re = regexp.MustCompile(`fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::[fF]{4}:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?`)
|
||||
)
|
||||
|
||||
func MaybeRegexp(s string) bool {
|
||||
return strings.ContainsAny(s, "[]{}()?")
|
||||
}
|
||||
|
@ -35,3 +35,21 @@ func TestIPv6Re(t *testing.T) {
|
||||
fmt.Println(IPv6Re.FindAllString(ip, -1))
|
||||
}
|
||||
}
|
||||
|
||||
var maybeRegexList = []string{
|
||||
"[a-z]*\\.example.com",
|
||||
"kunlun[^.]+.com",
|
||||
"gtm-a[1-7]b[1-9].com",
|
||||
}
|
||||
|
||||
func TestMaybeRegexp(t *testing.T) {
|
||||
if MaybeRegexp("abc.com") {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
for _, str := range maybeRegexList {
|
||||
if !MaybeRegexp(str) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user