mirror of
				https://github.com/TeaOSLab/EdgeCommon.git
				synced 2025-11-04 05:00:24 +08:00 
			
		
		
		
	例外和限制URL中增加常见图片、常见音频、常见视频等规则
This commit is contained in:
		@@ -4,6 +4,7 @@ package shared
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -13,8 +14,15 @@ type URLPatternType = string
 | 
				
			|||||||
const (
 | 
					const (
 | 
				
			||||||
	URLPatternTypeWildcard URLPatternType = "wildcard" // 通配符
 | 
						URLPatternTypeWildcard URLPatternType = "wildcard" // 通配符
 | 
				
			||||||
	URLPatternTypeRegexp   URLPatternType = "regexp"   // 正则表达式
 | 
						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 URLPattern struct {
 | 
				
			||||||
	Type    URLPatternType `yaml:"type" json:"type"`
 | 
						Type    URLPatternType `yaml:"type" json:"type"`
 | 
				
			||||||
	Pattern string         `yaml:"pattern" json:"pattern"`
 | 
						Pattern string         `yaml:"pattern" json:"pattern"`
 | 
				
			||||||
@@ -23,36 +31,36 @@ type URLPattern struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *URLPattern) Init() error {
 | 
					func (this *URLPattern) Init() error {
 | 
				
			||||||
	if len(this.Pattern) == 0 {
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	switch this.Type {
 | 
						switch this.Type {
 | 
				
			||||||
	case URLPatternTypeWildcard:
 | 
						case URLPatternTypeWildcard:
 | 
				
			||||||
		// 只支持星号
 | 
							if len(this.Pattern) > 0 {
 | 
				
			||||||
		var pieces = strings.Split(this.Pattern, "*")
 | 
								// 只支持星号
 | 
				
			||||||
		for index, piece := range pieces {
 | 
								var pieces = strings.Split(this.Pattern, "*")
 | 
				
			||||||
			pieces[index] = regexp.QuoteMeta(piece)
 | 
								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:
 | 
						case URLPatternTypeRegexp:
 | 
				
			||||||
		var pattern = this.Pattern
 | 
							if len(this.Pattern) > 0 {
 | 
				
			||||||
		if !strings.HasPrefix(pattern, "(?i)") { // 大小写不敏感
 | 
								var pattern = this.Pattern
 | 
				
			||||||
			pattern = "(?i)" + 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
 | 
						return nil
 | 
				
			||||||
@@ -63,8 +71,39 @@ func (this *URLPattern) Match(url string) bool {
 | 
				
			|||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if this.reg != nil {
 | 
						switch this.Type {
 | 
				
			||||||
		return this.reg.MatchString(url)
 | 
						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
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -118,6 +118,46 @@ func TestURLPattern_Match(t *testing.T) {
 | 
				
			|||||||
			url:         "https://example.com/123456/789",
 | 
								url:         "https://example.com/123456/789",
 | 
				
			||||||
			result:      false,
 | 
								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{
 | 
							var p = &shared.URLPattern{
 | 
				
			||||||
			Type:    ut.patternType,
 | 
								Type:    ut.patternType,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user