mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 04:10:25 +08:00
例外和限制URL中增加常见图片、常见音频、常见视频等规则
This commit is contained in:
@@ -4,6 +4,7 @@ package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
@@ -13,8 +14,15 @@ type URLPatternType = string
|
||||
const (
|
||||
URLPatternTypeWildcard URLPatternType = "wildcard" // 通配符
|
||||
URLPatternTypeRegexp URLPatternType = "regexp" // 正则表达式
|
||||
URLPatternTypeImages URLPatternType = "images" // 常见图片
|
||||
URLPatternTypeAudios URLPatternType = "audios" // 常见音频
|
||||
URLPatternTypeVideos URLPatternType = "videos" // 常见视频
|
||||
)
|
||||
|
||||
var commonImageExtensions = []string{".apng", ".avif", ".gif", ".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", ".png", ".svg", ".webp", ".bmp", ".ico", ".cur", ".tif", ".tiff"}
|
||||
var commonAudioExtensions = []string{".mp3", ".flac", ".wav", ".aac", ".ogg", ".m4a", ".wma", ".m3u8"} // m3u8 is special
|
||||
var commonVideoExtensions = []string{".mp4", ".avi", ".mkv", ".mov", ".wmv", ".mpeg", ".3gp", ".webm", ".ts", ".m3u8"}
|
||||
|
||||
type URLPattern struct {
|
||||
Type URLPatternType `yaml:"type" json:"type"`
|
||||
Pattern string `yaml:"pattern" json:"pattern"`
|
||||
@@ -23,36 +31,36 @@ type URLPattern struct {
|
||||
}
|
||||
|
||||
func (this *URLPattern) Init() error {
|
||||
if len(this.Pattern) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch this.Type {
|
||||
case URLPatternTypeWildcard:
|
||||
// 只支持星号
|
||||
var pieces = strings.Split(this.Pattern, "*")
|
||||
for index, piece := range pieces {
|
||||
pieces[index] = regexp.QuoteMeta(piece)
|
||||
if len(this.Pattern) > 0 {
|
||||
// 只支持星号
|
||||
var pieces = strings.Split(this.Pattern, "*")
|
||||
for index, piece := range pieces {
|
||||
pieces[index] = regexp.QuoteMeta(piece)
|
||||
}
|
||||
var pattern = strings.Join(pieces, "(.*)")
|
||||
if len(pattern) > 0 && pattern[0] == '/' {
|
||||
pattern = "(http|https)://[\\w.-]+" + pattern
|
||||
}
|
||||
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + pattern + "$")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.reg = reg
|
||||
}
|
||||
var pattern = strings.Join(pieces, "(.*)")
|
||||
if len(pattern) > 0 && pattern[0] == '/' {
|
||||
pattern = "(http|https)://[\\w.-]+" + pattern
|
||||
}
|
||||
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + pattern + "$")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.reg = reg
|
||||
case URLPatternTypeRegexp:
|
||||
var pattern = this.Pattern
|
||||
if !strings.HasPrefix(pattern, "(?i)") { // 大小写不敏感
|
||||
pattern = "(?i)" + pattern
|
||||
if len(this.Pattern) > 0 {
|
||||
var pattern = this.Pattern
|
||||
if !strings.HasPrefix(pattern, "(?i)") { // 大小写不敏感
|
||||
pattern = "(?i)" + pattern
|
||||
}
|
||||
reg, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("compile '%s' failed: %w", pattern, err)
|
||||
}
|
||||
this.reg = reg
|
||||
}
|
||||
reg, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("compile '%s' failed: %w", pattern, err)
|
||||
}
|
||||
this.reg = reg
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -63,8 +71,39 @@ func (this *URLPattern) Match(url string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
if this.reg != nil {
|
||||
return this.reg.MatchString(url)
|
||||
switch this.Type {
|
||||
case URLPatternTypeImages:
|
||||
var urlExt = strings.ToLower(filepath.Ext(url))
|
||||
if len(urlExt) > 0 {
|
||||
for _, ext := range commonImageExtensions {
|
||||
if ext == urlExt {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
case URLPatternTypeAudios:
|
||||
var urlExt = strings.ToLower(filepath.Ext(url))
|
||||
if len(urlExt) > 0 {
|
||||
for _, ext := range commonAudioExtensions {
|
||||
if ext == urlExt {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
case URLPatternTypeVideos:
|
||||
var urlExt = strings.ToLower(filepath.Ext(url))
|
||||
if len(urlExt) > 0 {
|
||||
for _, ext := range commonVideoExtensions {
|
||||
if ext == urlExt {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
if this.reg != nil {
|
||||
return this.reg.MatchString(url)
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -118,6 +118,46 @@ func TestURLPattern_Match(t *testing.T) {
|
||||
url: "https://example.com/123456/789",
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
patternType: "images",
|
||||
url: "https://example.com/images/logo.png",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "images",
|
||||
url: "https://example.com/images/logo.webp",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "images",
|
||||
url: "https://example.com/images/logo.mp3",
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
patternType: "audios",
|
||||
url: "https://example.com/audios/music.mp3",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "audios",
|
||||
url: "https://example.com/audios/music.mm",
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
patternType: "videos",
|
||||
url: "https://example.com/images/movie.mp4",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "videos",
|
||||
url: "https://example.com/images/movie.ts",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "videos",
|
||||
url: "https://example.com/images/movie.mp5",
|
||||
result: false,
|
||||
},
|
||||
} {
|
||||
var p = &shared.URLPattern{
|
||||
Type: ut.patternType,
|
||||
|
||||
Reference in New Issue
Block a user