mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	增加防盗链功能
This commit is contained in:
		@@ -457,6 +457,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
 | 
				
			|||||||
		config.UAM = uamConfig
 | 
							config.UAM = uamConfig
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Referers
 | 
				
			||||||
 | 
						if IsNotNull(web.Referers) {
 | 
				
			||||||
 | 
							var referersConfig = &serverconfigs.ReferersConfig{}
 | 
				
			||||||
 | 
							err = json.Unmarshal(web.Referers, referersConfig)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							config.Referers = referersConfig
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if cacheMap != nil {
 | 
						if cacheMap != nil {
 | 
				
			||||||
		cacheMap.Put(cacheKey, config)
 | 
							cacheMap.Put(cacheKey, config)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -1212,6 +1222,35 @@ func (this *HTTPWebDAO) FindWebUAM(tx *dbs.Tx, webId int64) ([]byte, error) {
 | 
				
			|||||||
		FindJSONCol()
 | 
							FindJSONCol()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateWebReferers 修改防盗链设置
 | 
				
			||||||
 | 
					func (this *HTTPWebDAO) UpdateWebReferers(tx *dbs.Tx, webId int64, referersConfig *serverconfigs.ReferersConfig) error {
 | 
				
			||||||
 | 
						if referersConfig == nil {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						configJSON, err := json.Marshal(referersConfig)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = this.Query(tx).
 | 
				
			||||||
 | 
							Pk(webId).
 | 
				
			||||||
 | 
							Set("referers", configJSON).
 | 
				
			||||||
 | 
							UpdateQuickly()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return this.NotifyUpdate(tx, webId)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FindWebReferers 查找服务的防盗链配置
 | 
				
			||||||
 | 
					func (this *HTTPWebDAO) FindWebReferers(tx *dbs.Tx, webId int64) ([]byte, error) {
 | 
				
			||||||
 | 
						return this.Query(tx).
 | 
				
			||||||
 | 
							Pk(webId).
 | 
				
			||||||
 | 
							Result("referers").
 | 
				
			||||||
 | 
							FindJSONCol()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NotifyUpdate 通知更新
 | 
					// NotifyUpdate 通知更新
 | 
				
			||||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
 | 
					func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
 | 
				
			||||||
	// server
 | 
						// server
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,43 +38,45 @@ type HTTPWeb struct {
 | 
				
			|||||||
	RequestLimit       dbs.JSON `field:"requestLimit"`       // 请求限制
 | 
						RequestLimit       dbs.JSON `field:"requestLimit"`       // 请求限制
 | 
				
			||||||
	RequestScripts     dbs.JSON `field:"requestScripts"`     // 请求脚本
 | 
						RequestScripts     dbs.JSON `field:"requestScripts"`     // 请求脚本
 | 
				
			||||||
	Uam                dbs.JSON `field:"uam"`                // UAM设置
 | 
						Uam                dbs.JSON `field:"uam"`                // UAM设置
 | 
				
			||||||
 | 
						Referers           dbs.JSON `field:"referers"`           // 防盗链设置
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HTTPWebOperator struct {
 | 
					type HTTPWebOperator struct {
 | 
				
			||||||
	Id                 interface{} // ID
 | 
						Id                 any // ID
 | 
				
			||||||
	IsOn               interface{} // 是否启用
 | 
						IsOn               any // 是否启用
 | 
				
			||||||
	TemplateId         interface{} // 模版ID
 | 
						TemplateId         any // 模版ID
 | 
				
			||||||
	AdminId            interface{} // 管理员ID
 | 
						AdminId            any // 管理员ID
 | 
				
			||||||
	UserId             interface{} // 用户ID
 | 
						UserId             any // 用户ID
 | 
				
			||||||
	State              interface{} // 状态
 | 
						State              any // 状态
 | 
				
			||||||
	CreatedAt          interface{} // 创建时间
 | 
						CreatedAt          any // 创建时间
 | 
				
			||||||
	Root               interface{} // 根目录
 | 
						Root               any // 根目录
 | 
				
			||||||
	Charset            interface{} // 字符集
 | 
						Charset            any // 字符集
 | 
				
			||||||
	Shutdown           interface{} // 临时关闭页面配置
 | 
						Shutdown           any // 临时关闭页面配置
 | 
				
			||||||
	Pages              interface{} // 特殊页面
 | 
						Pages              any // 特殊页面
 | 
				
			||||||
	RedirectToHttps    interface{} // 跳转到HTTPS设置
 | 
						RedirectToHttps    any // 跳转到HTTPS设置
 | 
				
			||||||
	Indexes            interface{} // 首页文件列表
 | 
						Indexes            any // 首页文件列表
 | 
				
			||||||
	MaxRequestBodySize interface{} // 最大允许的请求内容尺寸
 | 
						MaxRequestBodySize any // 最大允许的请求内容尺寸
 | 
				
			||||||
	RequestHeader      interface{} // 请求Header配置
 | 
						RequestHeader      any // 请求Header配置
 | 
				
			||||||
	ResponseHeader     interface{} // 响应Header配置
 | 
						ResponseHeader     any // 响应Header配置
 | 
				
			||||||
	AccessLog          interface{} // 访问日志配置
 | 
						AccessLog          any // 访问日志配置
 | 
				
			||||||
	Stat               interface{} // 统计配置
 | 
						Stat               any // 统计配置
 | 
				
			||||||
	Gzip               interface{} // Gzip配置(v0.3.2弃用)
 | 
						Gzip               any // Gzip配置(v0.3.2弃用)
 | 
				
			||||||
	Compression        interface{} // 压缩配置
 | 
						Compression        any // 压缩配置
 | 
				
			||||||
	Cache              interface{} // 缓存配置
 | 
						Cache              any // 缓存配置
 | 
				
			||||||
	Firewall           interface{} // 防火墙设置
 | 
						Firewall           any // 防火墙设置
 | 
				
			||||||
	Locations          interface{} // 路由规则配置
 | 
						Locations          any // 路由规则配置
 | 
				
			||||||
	Websocket          interface{} // Websocket设置
 | 
						Websocket          any // Websocket设置
 | 
				
			||||||
	RewriteRules       interface{} // 重写规则配置
 | 
						RewriteRules       any // 重写规则配置
 | 
				
			||||||
	HostRedirects      interface{} // 域名跳转
 | 
						HostRedirects      any // 域名跳转
 | 
				
			||||||
	Fastcgi            interface{} // Fastcgi配置
 | 
						Fastcgi            any // Fastcgi配置
 | 
				
			||||||
	Auth               interface{} // 认证策略配置
 | 
						Auth               any // 认证策略配置
 | 
				
			||||||
	Webp               interface{} // WebP配置
 | 
						Webp               any // WebP配置
 | 
				
			||||||
	RemoteAddr         interface{} // 客户端IP配置
 | 
						RemoteAddr         any // 客户端IP配置
 | 
				
			||||||
	MergeSlashes       interface{} // 是否合并路径中的斜杠
 | 
						MergeSlashes       any // 是否合并路径中的斜杠
 | 
				
			||||||
	RequestLimit       interface{} // 请求限制
 | 
						RequestLimit       any // 请求限制
 | 
				
			||||||
	RequestScripts     interface{} // 请求脚本
 | 
						RequestScripts     any // 请求脚本
 | 
				
			||||||
	Uam                interface{} // UAM设置
 | 
						Uam                any // UAM设置
 | 
				
			||||||
 | 
						Referers           any // 防盗链设置
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewHTTPWebOperator() *HTTPWebOperator {
 | 
					func NewHTTPWebOperator() *HTTPWebOperator {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -754,3 +754,55 @@ func (this *HTTPWebService) FindHTTPWebRequestScripts(ctx context.Context, req *
 | 
				
			|||||||
		RequestScriptsJSON: configJSON,
 | 
							RequestScriptsJSON: configJSON,
 | 
				
			||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateHTTPWebReferers 修改防盗链设置
 | 
				
			||||||
 | 
					func (this *HTTPWebService) UpdateHTTPWebReferers(ctx context.Context, req *pb.UpdateHTTPWebReferersRequest) (*pb.RPCSuccess, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var config = &serverconfigs.ReferersConfig{}
 | 
				
			||||||
 | 
						if len(req.ReferersJSON) > 0 {
 | 
				
			||||||
 | 
							err = json.Unmarshal(req.ReferersJSON, config)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = config.Init()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, errors.New("validate referers config failed: " + err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						err = models.SharedHTTPWebDAO.UpdateWebReferers(tx, req.HttpWebId, config)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return this.Success()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FindHTTPWebReferers 查找防盗链设置
 | 
				
			||||||
 | 
					func (this *HTTPWebService) FindHTTPWebReferers(ctx context.Context, req *pb.FindHTTPWebReferersRequest) (*pb.FindHTTPWebReferersResponse, error) {
 | 
				
			||||||
 | 
						_, err := this.ValidateAdmin(ctx)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var tx = this.NullTx()
 | 
				
			||||||
 | 
						config, err := models.SharedHTTPWebDAO.FindWebReferers(tx, req.HttpWebId)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						configJSON, err := json.Marshal(config)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &pb.FindHTTPWebReferersResponse{
 | 
				
			||||||
 | 
							ReferersJSON: configJSON,
 | 
				
			||||||
 | 
						}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user