mirror of
				https://github.com/TeaOSLab/EdgeCommon.git
				synced 2025-11-04 13:10:24 +08:00 
			
		
		
		
	静态分发增加例外URL、限制URL、排除隐藏文件等选项
This commit is contained in:
		@@ -1,8 +1,17 @@
 | 
				
			|||||||
package serverconfigs
 | 
					package serverconfigs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Web文档目录配置
 | 
					func NewHTTPRootConfig() *HTTPRootConfig {
 | 
				
			||||||
 | 
						return &HTTPRootConfig{
 | 
				
			||||||
 | 
							ExceptHiddenFiles: true,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// HTTPRootConfig Web文档目录配置
 | 
				
			||||||
type HTTPRootConfig struct {
 | 
					type HTTPRootConfig struct {
 | 
				
			||||||
	IsPrior           bool                 `yaml:"isPrior" json:"isPrior"`                     // 是否优先
 | 
						IsPrior           bool                 `yaml:"isPrior" json:"isPrior"`                     // 是否优先
 | 
				
			||||||
	IsOn              bool                 `yaml:"isOn" json:"isOn"`                           // 是否启用
 | 
						IsOn              bool                 `yaml:"isOn" json:"isOn"`                           // 是否启用
 | 
				
			||||||
@@ -11,17 +20,57 @@ type HTTPRootConfig struct {
 | 
				
			|||||||
	StripPrefix       string               `yaml:"stripPrefix" json:"stripPrefix"`             // 去除URL前缀
 | 
						StripPrefix       string               `yaml:"stripPrefix" json:"stripPrefix"`             // 去除URL前缀
 | 
				
			||||||
	DecodePath        bool                 `yaml:"decodePath" json:"decodePath"`               // 是否对请求路径进行解码
 | 
						DecodePath        bool                 `yaml:"decodePath" json:"decodePath"`               // 是否对请求路径进行解码
 | 
				
			||||||
	IsBreak           bool                 `yaml:"isBreak" json:"isBreak"`                     // 找不到文件的情况下是否终止
 | 
						IsBreak           bool                 `yaml:"isBreak" json:"isBreak"`                     // 找不到文件的情况下是否终止
 | 
				
			||||||
 | 
						ExceptHiddenFiles bool                 `yaml:"exceptHiddenFiles" json:"exceptHiddenFiles"` // 排除隐藏文件
 | 
				
			||||||
 | 
						OnlyURLPatterns   []*shared.URLPattern `yaml:"onlyURLPatterns" json:"onlyURLPatterns"`     // 仅限的URL
 | 
				
			||||||
 | 
						ExceptURLPatterns []*shared.URLPattern `yaml:"exceptURLPatterns" json:"exceptURLPatterns"` // 排除的URL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hasVariables bool
 | 
						hasVariables bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 初始化
 | 
					// Init 初始化
 | 
				
			||||||
func (this *HTTPRootConfig) Init() error {
 | 
					func (this *HTTPRootConfig) Init() error {
 | 
				
			||||||
	this.hasVariables = configutils.HasVariables(this.Dir)
 | 
						this.hasVariables = configutils.HasVariables(this.Dir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, pattern := range this.OnlyURLPatterns {
 | 
				
			||||||
 | 
							err := pattern.Init()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, pattern := range this.ExceptURLPatterns {
 | 
				
			||||||
 | 
							err := pattern.Init()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 判断是否有变量
 | 
					// HasVariables 判断是否有变量
 | 
				
			||||||
func (this *HTTPRootConfig) HasVariables() bool {
 | 
					func (this *HTTPRootConfig) HasVariables() bool {
 | 
				
			||||||
	return this.hasVariables
 | 
						return this.hasVariables
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (this *HTTPRootConfig) MatchURL(url string) bool {
 | 
				
			||||||
 | 
						// except
 | 
				
			||||||
 | 
						if len(this.ExceptURLPatterns) > 0 {
 | 
				
			||||||
 | 
							for _, pattern := range this.ExceptURLPatterns {
 | 
				
			||||||
 | 
								if pattern.Match(url) {
 | 
				
			||||||
 | 
									return false
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(this.OnlyURLPatterns) > 0 {
 | 
				
			||||||
 | 
							for _, pattern := range this.OnlyURLPatterns {
 | 
				
			||||||
 | 
								if pattern.Match(url) {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -70,6 +70,12 @@ func TestURLPattern_Match(t *testing.T) {
 | 
				
			|||||||
			url:         "https://example-test.com/123/hello/world",
 | 
								url:         "https://example-test.com/123/hello/world",
 | 
				
			||||||
			result:      false,
 | 
								result:      false,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								patternType: "wildcard",
 | 
				
			||||||
 | 
								pattern:     "/hidden/*",
 | 
				
			||||||
 | 
								url:         "/hidden/index.html",
 | 
				
			||||||
 | 
								result:      false, // because don't have https://HOST in url
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			patternType: "regexp",
 | 
								patternType: "regexp",
 | 
				
			||||||
			pattern:     ".*",
 | 
								pattern:     ".*",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user