mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	实现UA名单功能
This commit is contained in:
		@@ -21,5 +21,5 @@ const (
 | 
			
		||||
	NodeVersion = "0.6.0"
 | 
			
		||||
 | 
			
		||||
	// SQLVersion SQL版本号
 | 
			
		||||
	SQLVersion = "7"
 | 
			
		||||
	SQLVersion = "8"
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -468,6 +468,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
 | 
			
		||||
		config.Referers = referersConfig
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// User-Agent
 | 
			
		||||
	if IsNotNull(web.UserAgent) {
 | 
			
		||||
		var userAgentConfig = serverconfigs.NewUserAgentConfig()
 | 
			
		||||
		err = json.Unmarshal(web.UserAgent, userAgentConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.UserAgent = userAgentConfig
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if cacheMap != nil {
 | 
			
		||||
		cacheMap.Put(cacheKey, config)
 | 
			
		||||
	}
 | 
			
		||||
@@ -1252,6 +1262,35 @@ func (this *HTTPWebDAO) FindWebReferers(tx *dbs.Tx, webId int64) ([]byte, error)
 | 
			
		||||
		FindJSONCol()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateWebUserAgent 修改User-Agent设置
 | 
			
		||||
func (this *HTTPWebDAO) UpdateWebUserAgent(tx *dbs.Tx, webId int64, userAgentConfig *serverconfigs.UserAgentConfig) error {
 | 
			
		||||
	if userAgentConfig == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	configJSON, err := json.Marshal(userAgentConfig)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = this.Query(tx).
 | 
			
		||||
		Pk(webId).
 | 
			
		||||
		Set("userAgent", configJSON).
 | 
			
		||||
		UpdateQuickly()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.NotifyUpdate(tx, webId)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindWebUserAgent 查找服务User-Agent配置
 | 
			
		||||
func (this *HTTPWebDAO) FindWebUserAgent(tx *dbs.Tx, webId int64) ([]byte, error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Pk(webId).
 | 
			
		||||
		Result("userAgent").
 | 
			
		||||
		FindJSONCol()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotifyUpdate 通知更新
 | 
			
		||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
 | 
			
		||||
	// server
 | 
			
		||||
 
 | 
			
		||||
@@ -39,6 +39,7 @@ type HTTPWeb struct {
 | 
			
		||||
	RequestScripts     dbs.JSON `field:"requestScripts"`     // 请求脚本
 | 
			
		||||
	Uam                dbs.JSON `field:"uam"`                // UAM设置
 | 
			
		||||
	Referers           dbs.JSON `field:"referers"`           // 防盗链设置
 | 
			
		||||
	UserAgent          dbs.JSON `field:"userAgent"`          // UserAgent设置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPWebOperator struct {
 | 
			
		||||
@@ -77,6 +78,7 @@ type HTTPWebOperator struct {
 | 
			
		||||
	RequestScripts     any // 请求脚本
 | 
			
		||||
	Uam                any // UAM设置
 | 
			
		||||
	Referers           any // 防盗链设置
 | 
			
		||||
	UserAgent          any // UserAgent设置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHTTPWebOperator() *HTTPWebOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -822,3 +822,71 @@ func (this *HTTPWebService) FindHTTPWebReferers(ctx context.Context, req *pb.Fin
 | 
			
		||||
		ReferersJSON: configJSON,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateHTTPWebUserAgent 修改UserAgent设置
 | 
			
		||||
func (this *HTTPWebService) UpdateHTTPWebUserAgent(ctx context.Context, req *pb.UpdateHTTPWebUserAgentRequest) (*pb.RPCSuccess, error) {
 | 
			
		||||
	_, userId, err := this.ValidateAdminAndUser(ctx, true)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	if userId > 0 {
 | 
			
		||||
		err = models.SharedHTTPWebDAO.CheckUserWeb(tx, userId, req.HttpWebId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var config = &serverconfigs.UserAgentConfig{}
 | 
			
		||||
	if len(req.UserAgentJSON) > 0 {
 | 
			
		||||
		err = json.Unmarshal(req.UserAgentJSON, config)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = config.Init()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, errors.New("validate user-agent config failed: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedHTTPWebDAO.UpdateWebUserAgent(tx, req.HttpWebId, config)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindHTTPWebUserAgent 查找UserAgent设置
 | 
			
		||||
func (this *HTTPWebService) FindHTTPWebUserAgent(ctx context.Context, req *pb.FindHTTPWebUserAgentRequest) (*pb.FindHTTPWebUserAgentResponse, error) {
 | 
			
		||||
	_, userId, err := this.ValidateAdminAndUser(ctx, true)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	if userId > 0 {
 | 
			
		||||
		err = models.SharedHTTPWebDAO.CheckUserWeb(tx, userId, req.HttpWebId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config, err := models.SharedHTTPWebDAO.FindWebUserAgent(tx, req.HttpWebId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	configJSON, err := json.Marshal(config)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindHTTPWebUserAgentResponse{
 | 
			
		||||
		UserAgentJSON: configJSON,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user