mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		@@ -130,6 +130,9 @@ func TestGenerate_EAB(t *testing.T) {
 | 
				
			|||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		reg, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
 | 
							reg, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	myUser.Registration = reg
 | 
						myUser.Registration = reg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	request := certificate.ObtainRequest{
 | 
						request := certificate.ObtainRequest{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -92,13 +92,13 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
 | 
				
			|||||||
	// 注册用户
 | 
						// 注册用户
 | 
				
			||||||
	var resource = this.task.User.GetRegistration()
 | 
						var resource = this.task.User.GetRegistration()
 | 
				
			||||||
	if resource != nil {
 | 
						if resource != nil {
 | 
				
			||||||
		resource, err = client.Registration.QueryRegistration()
 | 
							_, err = client.Registration.QueryRegistration()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, nil, err
 | 
								return nil, nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		if this.task.Provider.RequireEAB {
 | 
							if this.task.Provider.RequireEAB {
 | 
				
			||||||
			resource, err := client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
 | 
								resource, err = client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
 | 
				
			||||||
				TermsOfServiceAgreed: true,
 | 
									TermsOfServiceAgreed: true,
 | 
				
			||||||
				Kid:                  this.task.Account.EABKid,
 | 
									Kid:                  this.task.Account.EABKid,
 | 
				
			||||||
				HmacEncoded:          this.task.Account.EABKey,
 | 
									HmacEncoded:          this.task.Account.EABKey,
 | 
				
			||||||
@@ -111,7 +111,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
 | 
				
			|||||||
				return nil, nil, err
 | 
									return nil, nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			resource, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
 | 
								resource, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return nil, nil, err
 | 
									return nil, nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -165,13 +165,13 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
 | 
				
			|||||||
	// 注册用户
 | 
						// 注册用户
 | 
				
			||||||
	var resource = this.task.User.GetRegistration()
 | 
						var resource = this.task.User.GetRegistration()
 | 
				
			||||||
	if resource != nil {
 | 
						if resource != nil {
 | 
				
			||||||
		resource, err = client.Registration.QueryRegistration()
 | 
							_, err = client.Registration.QueryRegistration()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, nil, err
 | 
								return nil, nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		if this.task.Provider.RequireEAB {
 | 
							if this.task.Provider.RequireEAB {
 | 
				
			||||||
			resource, err := client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
 | 
								resource, err = client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
 | 
				
			||||||
				TermsOfServiceAgreed: true,
 | 
									TermsOfServiceAgreed: true,
 | 
				
			||||||
				Kid:                  this.task.Account.EABKid,
 | 
									Kid:                  this.task.Account.EABKid,
 | 
				
			||||||
				HmacEncoded:          this.task.Account.EABKey,
 | 
									HmacEncoded:          this.task.Account.EABKey,
 | 
				
			||||||
@@ -184,7 +184,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
 | 
				
			|||||||
				return nil, nil, err
 | 
									return nil, nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			resource, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
 | 
								resource, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return nil, nil, err
 | 
									return nil, nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
				
			||||||
@@ -37,7 +38,7 @@ func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverc
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = config.Init(nil)
 | 
						err = config.Init(context.TODO())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -55,7 +56,7 @@ func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverc
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = config.Init(nil)
 | 
						err = config.Init(context.TODO())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -135,7 +136,7 @@ func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*ser
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = config.Init(nil)
 | 
						err = config.Init(context.TODO())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -153,7 +154,7 @@ func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*ser
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = config.Init(nil)
 | 
						err = config.Init(context.TODO())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -232,7 +232,7 @@ Loop:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// CreateHTTPAccessLog 写入单条访问日志
 | 
					// CreateHTTPAccessLog 写入单条访问日志
 | 
				
			||||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
 | 
					func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
 | 
				
			||||||
	var day = ""
 | 
						var day string
 | 
				
			||||||
	// 注意:如果你修改了 TimeISO8601 的逻辑,这里也需要同步修改
 | 
						// 注意:如果你修改了 TimeISO8601 的逻辑,这里也需要同步修改
 | 
				
			||||||
	if len(accessLog.TimeISO8601) > 10 {
 | 
						if len(accessLog.TimeISO8601) > 10 {
 | 
				
			||||||
		day = strings.ReplaceAll(accessLog.TimeISO8601[:10], "-", "")
 | 
							day = strings.ReplaceAll(accessLog.TimeISO8601[:10], "-", "")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -373,7 +373,7 @@ func (this *HTTPAccessLogManager) findTableWithoutCache(db *dbs.DB, day string,
 | 
				
			|||||||
		var lastInt64Id = types.Int64(lastId)
 | 
							var lastInt64Id = types.Int64(lastId)
 | 
				
			||||||
		if accessLogRowsPerTable > 0 && lastInt64Id >= accessLogRowsPerTable {
 | 
							if accessLogRowsPerTable > 0 && lastInt64Id >= accessLogRowsPerTable {
 | 
				
			||||||
			// create next partial table
 | 
								// create next partial table
 | 
				
			||||||
			var nextTableName = ""
 | 
								var nextTableName string
 | 
				
			||||||
			if accessLogTableMainReg.MatchString(lastTableName) {
 | 
								if accessLogTableMainReg.MatchString(lastTableName) {
 | 
				
			||||||
				nextTableName = prefix + "_0001"
 | 
									nextTableName = prefix + "_0001"
 | 
				
			||||||
			} else if accessLogTablePartialReg.MatchString(lastTableName) {
 | 
								} else if accessLogTablePartialReg.MatchString(lastTableName) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -674,6 +674,9 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
 | 
								clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			for _, clusterId := range clusterIds {
 | 
								for _, clusterId := range clusterIds {
 | 
				
			||||||
				err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeIPItemChanged)
 | 
									err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeIPItemChanged)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1039,9 +1039,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, dataMap *shared
 | 
				
			|||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		for _, clusterServer := range clusterServers {
 | 
							servers = append(servers, clusterServers...)
 | 
				
			||||||
			servers = append(servers, clusterServer)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, server := range servers {
 | 
						for _, server := range servers {
 | 
				
			||||||
@@ -1063,7 +1061,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, dataMap *shared
 | 
				
			|||||||
	// TODO 根据用户的不同读取不同的全局设置
 | 
						// TODO 根据用户的不同读取不同的全局设置
 | 
				
			||||||
	var settingCacheKey = "SharedSysSettingDAO:" + systemconfigs.SettingCodeServerGlobalConfig
 | 
						var settingCacheKey = "SharedSysSettingDAO:" + systemconfigs.SettingCodeServerGlobalConfig
 | 
				
			||||||
	settingJSONCache, ok := cacheMap.Get(settingCacheKey)
 | 
						settingJSONCache, ok := cacheMap.Get(settingCacheKey)
 | 
				
			||||||
	var settingJSON = []byte{}
 | 
						var settingJSON []byte
 | 
				
			||||||
	if ok {
 | 
						if ok {
 | 
				
			||||||
		settingJSON = settingJSONCache.([]byte)
 | 
							settingJSON = settingJSONCache.([]byte)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
@@ -2124,7 +2122,7 @@ func (this *NodeDAO) FindParentNodeConfigs(tx *dbs.Tx, nodeId int64, groupId int
 | 
				
			|||||||
			var secretHash = fmt.Sprintf("%x", sha256.Sum256([]byte(node.UniqueId+"@"+node.Secret)))
 | 
								var secretHash = fmt.Sprintf("%x", sha256.Sum256([]byte(node.UniqueId+"@"+node.Secret)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			for _, clusterId := range node.AllClusterIds() {
 | 
								for _, clusterId := range node.AllClusterIds() {
 | 
				
			||||||
				parentNodeConfigs, _ := result[clusterId]
 | 
									var parentNodeConfigs = result[clusterId]
 | 
				
			||||||
				parentNodeConfigs = append(parentNodeConfigs, &nodeconfigs.ParentNodeConfig{
 | 
									parentNodeConfigs = append(parentNodeConfigs, &nodeconfigs.ParentNodeConfig{
 | 
				
			||||||
					Id:         int64(node.Id),
 | 
										Id:         int64(node.Id),
 | 
				
			||||||
					Addrs:      addrStrings,
 | 
										Addrs:      addrStrings,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -70,8 +70,7 @@ func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error)
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	domainRoutes, _ := routes[dnsDomainId]
 | 
						var domainRoutes = routes[dnsDomainId]
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(domainRoutes) > 0 {
 | 
						if len(domainRoutes) > 0 {
 | 
				
			||||||
		sort.Strings(domainRoutes)
 | 
							sort.Strings(domainRoutes)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -127,6 +127,9 @@ func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId stri
 | 
				
			|||||||
		pinyinResult = append(pinyinResult, strings.Join(piece, " "))
 | 
							pinyinResult = append(pinyinResult, strings.Join(piece, " "))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	pinyinJSON, err := json.Marshal([]string{strings.Join(pinyinResult, " ")})
 | 
						pinyinJSON, err := json.Marshal([]string{strings.Join(pinyinResult, " ")})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	op.Pinyin = pinyinJSON
 | 
						op.Pinyin = pinyinJSON
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	codes := []string{name}
 | 
						codes := []string{name}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
				
			||||||
@@ -78,7 +79,7 @@ func (this *Server) DecodeHTTPSPorts() (ports []int) {
 | 
				
			|||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		err = config.Init(nil)
 | 
							err = config.Init(context.TODO())
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -120,7 +121,7 @@ func (this *Server) DecodeTLSPorts() (ports []int) {
 | 
				
			|||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		err = config.Init(nil)
 | 
							err = config.Init(context.TODO())
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -701,7 +701,7 @@ func (this *SSLCertDAO) buildDomainSearchingQuery(query *dbs.Query, domains []st
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 检测 JSON_OVERLAPS() 函数是否可用
 | 
						// 检测 JSON_OVERLAPS() 函数是否可用
 | 
				
			||||||
	var canJSONOverlaps = false
 | 
						var canJSONOverlaps bool
 | 
				
			||||||
	_, funcErr := this.Instance.FindCol(0, "SELECT JSON_OVERLAPS('[1]', '[1]')")
 | 
						_, funcErr := this.Instance.FindCol(0, "SELECT JSON_OVERLAPS('[1]', '[1]')")
 | 
				
			||||||
	canJSONOverlaps = funcErr == nil
 | 
						canJSONOverlaps = funcErr == nil
 | 
				
			||||||
	if canJSONOverlaps {
 | 
						if canJSONOverlaps {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -522,16 +522,6 @@ func (this *UserBandwidthStatDAO) sumBytesField(useAvg bool) string {
 | 
				
			|||||||
	return "SUM(bytes) AS bytes"
 | 
						return "SUM(bytes) AS bytes"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *UserBandwidthStatDAO) fixUserStat(stat *UserBandwidthStat, useAvg bool) *UserBandwidthStat {
 | 
					 | 
				
			||||||
	if stat == nil {
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if useAvg {
 | 
					 | 
				
			||||||
		stat.Bytes = stat.AvgBytes
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return stat
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// HasFullData 检查一个月是否完整数据
 | 
					// HasFullData 检查一个月是否完整数据
 | 
				
			||||||
// 是为了兼容以前数据,以前的表中没有缓存流量、请求数等字段
 | 
					// 是为了兼容以前数据,以前的表中没有缓存流量、请求数等字段
 | 
				
			||||||
func (this *UserBandwidthStatDAO) HasFullData(tx *dbs.Tx, userId int64, month string) (bool, error) {
 | 
					func (this *UserBandwidthStatDAO) HasFullData(tx *dbs.Tx, userId int64, month string) (bool, error) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
				
			||||||
@@ -36,7 +37,7 @@ func (this *UserNode) DecodeHTTPS(cacheMap *utils.CacheMap) (*serverconfigs.HTTP
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = config.Init(nil)
 | 
						err = config.Init(context.TODO())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -54,7 +55,7 @@ func (this *UserNode) DecodeHTTPS(cacheMap *utils.CacheMap) (*serverconfigs.HTTP
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = config.Init(nil)
 | 
						err = config.Init(context.TODO())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,10 +61,7 @@ func (this *DomainRecordsCache) WriteDomainRecords(providerId int64, domain stri
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var clonedRecords = []*dnstypes.Record{}
 | 
						var clonedRecords = append([]*dnstypes.Record{}, records...)
 | 
				
			||||||
	for _, record := range records {
 | 
					 | 
				
			||||||
		clonedRecords = append(clonedRecords, record)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	this.domainRecordsMap[domain] = &recordList{
 | 
						this.domainRecordsMap[domain] = &recordList{
 | 
				
			||||||
		version:   version,
 | 
							version:   version,
 | 
				
			||||||
		updatedAt: time.Now().Unix(),
 | 
							updatedAt: time.Now().Unix(),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,7 +18,7 @@ func (this *BaseProvider) WrapError(err error, domain string, record *dnstypes.R
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var fullname = ""
 | 
						var fullname string
 | 
				
			||||||
	if len(record.Name) == 0 {
 | 
						if len(record.Name) == 0 {
 | 
				
			||||||
		fullname = domain
 | 
							fullname = domain
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,7 +19,6 @@ func TestAES128CFBMethod_Encrypt(t *testing.T) {
 | 
				
			|||||||
	dst = dst[:len(src)]
 | 
						dst = dst[:len(src)]
 | 
				
			||||||
	t.Log("dst:", string(dst))
 | 
						t.Log("dst:", string(dst))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	src = make([]byte, len(src))
 | 
					 | 
				
			||||||
	src, err = method.Decrypt(dst)
 | 
						src, err = method.Decrypt(dst)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
@@ -64,7 +63,6 @@ func TestAES128CFBMethod_Encrypt2(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	for _, dst := range sources {
 | 
						for _, dst := range sources {
 | 
				
			||||||
		dst2 := append([]byte{}, dst...)
 | 
							dst2 := append([]byte{}, dst...)
 | 
				
			||||||
		src2 := make([]byte, len(dst2))
 | 
					 | 
				
			||||||
		src2, err := method.Decrypt(dst2)
 | 
							src2, err := method.Decrypt(dst2)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			t.Fatal(err)
 | 
								t.Fatal(err)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,7 @@ func On(event string, callback func()) {
 | 
				
			|||||||
	locker.Lock()
 | 
						locker.Lock()
 | 
				
			||||||
	defer locker.Unlock()
 | 
						defer locker.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	callbacks, _ := eventsMap[event]
 | 
						var callbacks = eventsMap[event]
 | 
				
			||||||
	callbacks = append(callbacks, callback)
 | 
						callbacks = append(callbacks, callback)
 | 
				
			||||||
	eventsMap[event] = callbacks
 | 
						eventsMap[event] = callbacks
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -18,7 +18,7 @@ func On(event string, callback func()) {
 | 
				
			|||||||
// Notify 通知事件
 | 
					// Notify 通知事件
 | 
				
			||||||
func Notify(event string) {
 | 
					func Notify(event string) {
 | 
				
			||||||
	locker.Lock()
 | 
						locker.Lock()
 | 
				
			||||||
	callbacks, _ := eventsMap[event]
 | 
						var callbacks = eventsMap[event]
 | 
				
			||||||
	locker.Unlock()
 | 
						locker.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, callback := range callbacks {
 | 
						for _, callback := range callbacks {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -149,8 +149,8 @@ func (this *BaseInstaller) LookupLatestInstaller(filePrefix string) (string, err
 | 
				
			|||||||
func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.NodeRole) (env *Env, err error) {
 | 
					func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.NodeRole) (env *Env, err error) {
 | 
				
			||||||
	var uname = this.uname()
 | 
						var uname = this.uname()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var osName = ""
 | 
						var osName string
 | 
				
			||||||
	var archName = ""
 | 
						var archName string
 | 
				
			||||||
	if strings.Contains(uname, "Darwin") {
 | 
						if strings.Contains(uname, "Darwin") {
 | 
				
			||||||
		osName = "darwin"
 | 
							osName = "darwin"
 | 
				
			||||||
	} else if strings.Contains(uname, "Linux") {
 | 
						} else if strings.Contains(uname, "Linux") {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -97,10 +97,7 @@ func (this *HTTPCacheTaskKeyService) ValidateHTTPCacheTaskKeys(ctx context.Conte
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var serverClusterId = int64(server.ClusterId)
 | 
							var serverClusterId = int64(server.ClusterId)
 | 
				
			||||||
		if serverClusterId == 0 {
 | 
							if serverClusterId == 0 && clusterId <= 0 {
 | 
				
			||||||
			if clusterId > 0 {
 | 
					 | 
				
			||||||
				serverClusterId = clusterId
 | 
					 | 
				
			||||||
			} else {
 | 
					 | 
				
			||||||
			pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
 | 
								pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
 | 
				
			||||||
				Key:        key,
 | 
									Key:        key,
 | 
				
			||||||
				ReasonCode: "requireClusterId",
 | 
									ReasonCode: "requireClusterId",
 | 
				
			||||||
@@ -108,7 +105,6 @@ func (this *HTTPCacheTaskKeyService) ValidateHTTPCacheTaskKeys(ctx context.Conte
 | 
				
			|||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &pb.ValidateHTTPCacheTaskKeysResponse{FailKeys: pbFailResults}, nil
 | 
						return &pb.ValidateHTTPCacheTaskKeysResponse{FailKeys: pbFailResults}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,7 +34,7 @@ type CommandRequestWaiting struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (this *CommandRequestWaiting) Close() {
 | 
					func (this *CommandRequestWaiting) Close() {
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		recover()
 | 
							_ = recover()
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	close(this.Chan)
 | 
						close(this.Chan)
 | 
				
			||||||
@@ -207,7 +207,7 @@ func (this *NodeService) NodeStream(server pb.NodeService_NodeStreamServer) erro
 | 
				
			|||||||
		func(req *pb.NodeStreamMessage) {
 | 
							func(req *pb.NodeStreamMessage) {
 | 
				
			||||||
			// 因为 responseChan.Chan 有被关闭的风险,所以我们使用recover防止panic
 | 
								// 因为 responseChan.Chan 有被关闭的风险,所以我们使用recover防止panic
 | 
				
			||||||
			defer func() {
 | 
								defer func() {
 | 
				
			||||||
				recover()
 | 
									_ = recover()
 | 
				
			||||||
			}()
 | 
								}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			nodeLocker.Lock()
 | 
								nodeLocker.Lock()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2917,8 +2917,14 @@ func (this *ServerService) CopyServerConfig(ctx context.Context, req *pb.CopySer
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return nil, err
 | 
									return nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								if req.TargetUserId <= 0 {
 | 
				
			||||||
 | 
									req.TargetUserId = userId
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// 此时如果用户为0,则同步到未分配用户的服务
 | 
								// 此时如果用户为0,则同步到未分配用户的网站
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								// 只能同步到自己的网站
 | 
				
			||||||
 | 
								req.TargetUserId = userId
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		err = models.SharedServerDAO.CopyServerConfigToUser(tx, req.ServerId, req.TargetUserId, req.ConfigCode)
 | 
							err = models.SharedServerDAO.CopyServerConfigToUser(tx, req.ServerId, req.TargetUserId, req.ConfigCode)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -177,7 +177,7 @@ func (this *ServerBandwidthStatService) FindServerBandwidthStats(ctx context.Con
 | 
				
			|||||||
		req.Algo = bandwidthAlgo
 | 
							req.Algo = bandwidthAlgo
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var stats = []*models.ServerBandwidthStat{}
 | 
						var stats []*models.ServerBandwidthStat
 | 
				
			||||||
	if len(req.Day) > 0 {
 | 
						if len(req.Day) > 0 {
 | 
				
			||||||
		stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day, req.Algo == systemconfigs.BandwidthAlgoAvg)
 | 
							stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day, req.Algo == systemconfigs.BandwidthAlgoAvg)
 | 
				
			||||||
	} else if len(req.Month) > 0 {
 | 
						} else if len(req.Month) > 0 {
 | 
				
			||||||
@@ -398,7 +398,7 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
 | 
				
			|||||||
		return nil, errors.New("invalid dayTo '" + req.DayTo + "'")
 | 
							return nil, errors.New("invalid dayTo '" + req.DayTo + "'")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var pbStats = []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{}
 | 
						var pbStats []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
 | 
				
			||||||
	var pbNthStat *pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
 | 
						var pbNthStat *pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
 | 
				
			||||||
	if req.ServerId > 0 { // 服务统计
 | 
						if req.ServerId > 0 { // 服务统计
 | 
				
			||||||
		pbStats, err = models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
 | 
							pbStats, err = models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -424,10 +424,8 @@ func upgradeV0_3_2(db *dbs.DB) error {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if sizeCapacity != nil {
 | 
					 | 
				
			||||||
			compressionConfig.MinLength = sizeCapacity
 | 
								compressionConfig.MinLength = sizeCapacity
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var maxLengthBytes = []byte(gzipOne.GetString("maxLength"))
 | 
							var maxLengthBytes = []byte(gzipOne.GetString("maxLength"))
 | 
				
			||||||
		if len(maxLengthBytes) > 0 {
 | 
							if len(maxLengthBytes) > 0 {
 | 
				
			||||||
@@ -436,10 +434,8 @@ func upgradeV0_3_2(db *dbs.DB) error {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if sizeCapacity != nil {
 | 
					 | 
				
			||||||
			compressionConfig.MaxLength = sizeCapacity
 | 
								compressionConfig.MaxLength = sizeCapacity
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var condsBytes = []byte(gzipOne.GetString("conds"))
 | 
							var condsBytes = []byte(gzipOne.GetString("conds"))
 | 
				
			||||||
		if len(condsBytes) > 0 {
 | 
							if len(condsBytes) > 0 {
 | 
				
			||||||
@@ -448,10 +444,8 @@ func upgradeV0_3_2(db *dbs.DB) error {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if conds != nil {
 | 
					 | 
				
			||||||
			compressionConfig.Conds = conds
 | 
								compressionConfig.Conds = conds
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		configJSON, err := json.Marshal(compressionConfig)
 | 
							configJSON, err := json.Marshal(compressionConfig)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -177,7 +177,7 @@ func (this *HealthCheckExecutor) runNode(healthCheckConfig *serverconfigs.Health
 | 
				
			|||||||
			// 在线状态发生变化
 | 
								// 在线状态发生变化
 | 
				
			||||||
			if healthCheckConfig.AutoDown {
 | 
								if healthCheckConfig.AutoDown {
 | 
				
			||||||
				// 发送消息
 | 
									// 发送消息
 | 
				
			||||||
				var message = ""
 | 
									var message string
 | 
				
			||||||
				var messageType string
 | 
									var messageType string
 | 
				
			||||||
				var messageLevel string
 | 
									var messageLevel string
 | 
				
			||||||
				if result.IsOk {
 | 
									if result.IsOk {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -122,7 +122,7 @@ func TestList_ManyItems(t *testing.T) {
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
	list.GC(time.Now().Unix() + 1)
 | 
						list.GC(time.Now().Unix() + 1)
 | 
				
			||||||
	t.Log("gc", count, "items")
 | 
						t.Log("gc", count, "items")
 | 
				
			||||||
	t.Log(time.Now().Sub(now))
 | 
						t.Log(time.Since(now))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestList_Map_Performance(t *testing.T) {
 | 
					func TestList_Map_Performance(t *testing.T) {
 | 
				
			||||||
@@ -140,7 +140,7 @@ func TestList_Map_Performance(t *testing.T) {
 | 
				
			|||||||
		for i := 0; i < 100_000; i++ {
 | 
							for i := 0; i < 100_000; i++ {
 | 
				
			||||||
			delete(m, int64(i))
 | 
								delete(m, int64(i))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		t.Log(time.Now().Sub(now))
 | 
							t.Log(time.Since(now))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -153,7 +153,7 @@ func TestList_Map_Performance(t *testing.T) {
 | 
				
			|||||||
		for i := 0; i < 100_000; i++ {
 | 
							for i := 0; i < 100_000; i++ {
 | 
				
			||||||
			delete(m, uint64(i))
 | 
								delete(m, uint64(i))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		t.Log(time.Now().Sub(now))
 | 
							t.Log(time.Since(now))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -166,7 +166,7 @@ func TestList_Map_Performance(t *testing.T) {
 | 
				
			|||||||
		for i := 0; i < 100_000; i++ {
 | 
							for i := 0; i < 100_000; i++ {
 | 
				
			||||||
			delete(m, uint32(i))
 | 
								delete(m, uint32(i))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		t.Log(time.Now().Sub(now))
 | 
							t.Log(time.Since(now))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,5 +25,5 @@ func SetRLimit(limit uint64) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// set best resource limit value
 | 
					// set best resource limit value
 | 
				
			||||||
func SetSuitableRLimit() {
 | 
					func SetSuitableRLimit() {
 | 
				
			||||||
	SetRLimit(4096 * 100) // 1M=100Files
 | 
						_ = SetRLimit(4096 * 100) // 1M=100Files
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,7 +39,7 @@ func (this *ServiceManager) setup() {
 | 
				
			|||||||
	this.onceLocker.Do(func() {
 | 
						this.onceLocker.Do(func() {
 | 
				
			||||||
		logFile := files.NewFile(Tea.Root + "/logs/service.log")
 | 
							logFile := files.NewFile(Tea.Root + "/logs/service.log")
 | 
				
			||||||
		if logFile.Exists() {
 | 
							if logFile.Exists() {
 | 
				
			||||||
			logFile.Delete()
 | 
								_ = logFile.Delete()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		//logger
 | 
							//logger
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,8 +10,10 @@ var SharedCache = NewCache()
 | 
				
			|||||||
// Cache TTL缓存
 | 
					// Cache TTL缓存
 | 
				
			||||||
// 最大的缓存时间为30 * 86400
 | 
					// 最大的缓存时间为30 * 86400
 | 
				
			||||||
// Piece数据结构:
 | 
					// Piece数据结构:
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
//	    Piece1            |  Piece2 | Piece3 | ...
 | 
					//	    Piece1            |  Piece2 | Piece3 | ...
 | 
				
			||||||
//	[ Item1, Item2, ... ] |   ...
 | 
					//	[ Item1, Item2, ... ] |   ...
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
// KeyMap列表数据结构
 | 
					// KeyMap列表数据结构
 | 
				
			||||||
// { timestamp1 => [key1, key2, ...] }, ...
 | 
					// { timestamp1 => [key1, key2, ...] }, ...
 | 
				
			||||||
type Cache struct {
 | 
					type Cache struct {
 | 
				
			||||||
@@ -115,19 +117,11 @@ func (this *Cache) Read(key string) (item *Item) {
 | 
				
			|||||||
	return this.pieces[uint64Key%this.countPieces].Read(uint64Key)
 | 
						return this.pieces[uint64Key%this.countPieces].Read(uint64Key)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Cache) readIntKey(key uint64) (value *Item) {
 | 
					 | 
				
			||||||
	return this.pieces[key%this.countPieces].Read(key)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (this *Cache) Delete(key string) {
 | 
					func (this *Cache) Delete(key string) {
 | 
				
			||||||
	uint64Key := HashKey([]byte(key))
 | 
						uint64Key := HashKey([]byte(key))
 | 
				
			||||||
	this.pieces[uint64Key%this.countPieces].Delete(uint64Key)
 | 
						this.pieces[uint64Key%this.countPieces].Delete(uint64Key)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Cache) deleteIntKey(key uint64) {
 | 
					 | 
				
			||||||
	this.pieces[key%this.countPieces].Delete(key)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (this *Cache) Count() (count int) {
 | 
					func (this *Cache) Count() (count int) {
 | 
				
			||||||
	for _, piece := range this.pieces {
 | 
						for _, piece := range this.pieces {
 | 
				
			||||||
		count += piece.Count()
 | 
							count += piece.Count()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user