Skip to content

Commit

Permalink
fix: CDN support regex
Browse files Browse the repository at this point in the history
Signed-off-by: zu1k <[email protected]>
  • Loading branch information
zu1k committed Jun 29, 2022
1 parent 0b4f50d commit 21cc821
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 23 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/db/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func GetDefaultDBList() List {

&DB{
Name: "cdn",
Format: FormatCDNSkkYml,
Format: FormatCDNYml,
File: "cdn.yml",
Languages: LanguagesZH,
Types: TypesCDN,
Expand Down
4 changes: 2 additions & 2 deletions internal/db/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand All @@ -70,7 +70,7 @@ const (
FormatIP2Region = "ip2region"
FormatIP2Location = "ip2location"

FormatCDNSkkYml = "cdn-skk-yml"
FormatCDNYml = "cdn-yml"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 34 additions & 14 deletions pkg/cdn/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
Expand All @@ -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
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/cdn/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion pkg/re/re.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
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})`)

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, "[]{}()?")
}
18 changes: 18 additions & 0 deletions pkg/re/re_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}

0 comments on commit 21cc821

Please sign in to comment.