Compare commits

...

24 Commits

Author SHA1 Message Date
刘祥超
1eb9cca793 将WAF策略中的默认省份封禁提示内容长度从255修改为65535 2023-08-14 12:54:11 +08:00
刘祥超
8766f5b1a9 修改版本号为1.2.8 2023-08-14 12:24:29 +08:00
刘祥超
823e42626d DNS任务增加失败重试 2023-08-13 15:26:59 +08:00
刘祥超
c5308cf41c 生成节点时去除停用的WAF规则集 2023-08-13 10:51:52 +08:00
刘祥超
3053157c6e 将节点的api.yaml改为api_node.yaml 2023-08-12 15:27:09 +08:00
刘祥超
d1ba141c65 优化错误处理相关代码 2023-08-11 16:13:33 +08:00
刘祥超
034ababead 静态分发增加例外URL、限制URL、排除隐藏文件等选项 2023-08-10 11:27:05 +08:00
刘祥超
f5450e37be WAF策略可以自定义默认的区域/省份封禁提示 2023-08-10 10:30:50 +08:00
刘祥超
549fca93e6 将版本号修改为1.2.7 2023-08-09 14:24:16 +08:00
刘祥超
efa0f33256 Update .golangci.yaml 2023-08-09 08:11:53 +08:00
刘祥超
977a12843c 添加golangci-lint配置 2023-08-08 18:36:24 +08:00
刘祥超
6de2834a8c 优化代码 2023-08-08 16:46:17 +08:00
刘祥超
51f91e1603 优化代码 2023-08-08 12:09:20 +08:00
刘祥超
d27b7c8fa1 允许用户调用获取缓存策略信息API 2023-08-07 19:55:57 +08:00
刘祥超
c5098c66af 缓存策略增加预热超时时间设置(默认20分钟) 2023-08-06 17:07:48 +08:00
刘祥超
c2635b0d04 修复默认WAF策略模板中分组不能默认关闭的问题 2023-08-02 17:15:26 +08:00
刘祥超
41a1a6a2e5 更新SQL 2023-08-02 17:02:39 +08:00
刘祥超
e437117e69 WAF策略增加“最多检查内容尺寸“选项 2023-08-02 16:59:38 +08:00
刘祥超
fdc8f78229 优化CC配置 2023-08-01 19:50:01 +08:00
刘祥超
2f78d76a1a 修复系统服务相关代码可能不执行的问题 2023-08-01 16:19:05 +08:00
刘祥超
742f2f0216 启动时自动创建相关软链接 2023-08-01 10:47:13 +08:00
刘祥超
89a606329f 修复自定义页面无法保存的问题 2023-07-31 09:46:00 +08:00
刘祥超
3bba79d14c 优化统计 2023-07-31 09:45:48 +08:00
刘祥超
9f9787e30f 版本号更改为1.2.6 2023-07-28 09:27:08 +08:00
68 changed files with 676 additions and 278 deletions

75
.golangci.yaml Normal file
View File

@@ -0,0 +1,75 @@
# https://golangci-lint.run/usage/configuration/
linters:
enable-all: true
disable:
- ifshort
- exhaustivestruct
- golint
- nosnakecase
- scopelint
- varcheck
- structcheck
- interfacer
- maligned
- deadcode
- dogsled
- wrapcheck
- wastedassign
- varnamelen
- testpackage
- thelper
- nilerr
- sqlclosecheck
- paralleltest
- nonamedreturns
- nlreturn
- nakedret
- ireturn
- interfacebloat
- gosmopolitan
- gomnd
- goerr113
- gochecknoglobals
- exhaustruct
- errorlint
- depguard
- exhaustive
- containedctx
- wsl
- cyclop
- dupword
- errchkjson
- contextcheck
- tagalign
- dupl
- forbidigo
- funlen
- goconst
- godox
- gosec
- lll
- nestif
- revive
- unparam
- stylecheck
- gocritic
- gofumpt
- gomoddirectives
- godot
- gofmt
- gocognit
- mirror
- gocyclo
- gochecknoinits
- gci
- maintidx
- prealloc
- goimports
- errname
- musttag
- forcetypeassert
- whitespace
- noctx
- tagliatelle
- nilnil

View File

@@ -130,6 +130,9 @@ func TestGenerate_EAB(t *testing.T) {
} else {
reg, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
}
if err != nil {
t.Fatal(err)
}
myUser.Registration = reg
request := certificate.ObtainRequest{

View File

@@ -1,6 +1,7 @@
package acme
import (
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
@@ -45,7 +46,7 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
if !wasDeleted {
records, err := this.raw.QueryRecords(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
if err != nil {
return errors.New("query DNS record failed: " + err.Error())
return fmt.Errorf("query DNS record failed: %w", err)
}
for _, record := range records {
err = this.raw.DeleteRecord(this.dnsDomain, record)
@@ -67,7 +68,7 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
Route: this.raw.DefaultRoute(),
})
if err != nil {
return errors.New("create DNS record failed: " + err.Error())
return fmt.Errorf("create DNS record failed: %w", err)
}
return nil

View File

@@ -1,6 +1,7 @@
package acme
import (
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/go-acme/lego/v4/certcrypto"
@@ -92,26 +93,26 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
// 注册用户
var resource = this.task.User.GetRegistration()
if resource != nil {
resource, err = client.Registration.QueryRegistration()
_, err = client.Registration.QueryRegistration()
if err != nil {
return nil, nil, err
}
} else {
if this.task.Provider.RequireEAB {
resource, err := client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
resource, err = client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
TermsOfServiceAgreed: true,
Kid: this.task.Account.EABKid,
HmacEncoded: this.task.Account.EABKey,
})
if err != nil {
return nil, nil, errors.New("register user failed: " + err.Error())
return nil, nil, fmt.Errorf("register user failed: %w", err)
}
err = this.task.User.Register(resource)
if err != nil {
return nil, nil, err
}
} else {
resource, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
resource, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return nil, nil, err
}
@@ -134,7 +135,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
}
certResource, err := client.Certificate.Obtain(request)
if err != nil {
return nil, nil, errors.New("obtain cert failed: " + err.Error())
return nil, nil, fmt.Errorf("obtain cert failed: %w", err)
}
return certResource.Certificate, certResource.PrivateKey, nil
@@ -165,26 +166,26 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
// 注册用户
var resource = this.task.User.GetRegistration()
if resource != nil {
resource, err = client.Registration.QueryRegistration()
_, err = client.Registration.QueryRegistration()
if err != nil {
return nil, nil, err
}
} else {
if this.task.Provider.RequireEAB {
resource, err := client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
resource, err = client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
TermsOfServiceAgreed: true,
Kid: this.task.Account.EABKid,
HmacEncoded: this.task.Account.EABKey,
})
if err != nil {
return nil, nil, errors.New("register user failed: " + err.Error())
return nil, nil, fmt.Errorf("register user failed: %w", err)
}
err = this.task.User.Register(resource)
if err != nil {
return nil, nil, err
}
} else {
resource, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
resource, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return nil, nil, err
}

View File

@@ -1,6 +1,7 @@
package apps
import (
"errors"
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/iwind/TeaGo/logs"
@@ -9,8 +10,10 @@ import (
"github.com/iwind/gosock/pkg/gosock"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
@@ -184,13 +187,16 @@ func (this *AppCmd) runStart() {
return
}
cmd := exec.Command(os.Args[0])
var cmd = exec.Command(this.exe())
err := cmd.Start()
if err != nil {
fmt.Println(this.product+" start failed:", err.Error())
return
}
// create symbolic links
_ = this.createSymLinks()
fmt.Println(this.product+" started ok, pid:", cmd.Process.Pid)
}
@@ -237,3 +243,58 @@ func (this *AppCmd) getPID() int {
}
return maps.NewMap(reply.Params).GetInt("pid")
}
func (this *AppCmd) exe() string {
var exe, _ = os.Executable()
if len(exe) == 0 {
exe = os.Args[0]
}
return exe
}
// 创建软链接
func (this *AppCmd) createSymLinks() error {
if runtime.GOOS != "linux" {
return nil
}
var exe, _ = os.Executable()
if len(exe) == 0 {
return nil
}
var errorList = []string{}
// bin
{
var target = "/usr/bin/" + teaconst.ProcessName
old, _ := filepath.EvalSymlinks(target)
if old != exe {
_ = os.Remove(target)
err := os.Symlink(exe, target)
if err != nil {
errorList = append(errorList, err.Error())
}
}
}
// log
{
var realPath = filepath.Dir(filepath.Dir(exe)) + "/logs/run.log"
var target = "/var/log/" + teaconst.ProcessName + ".log"
old, _ := filepath.EvalSymlinks(target)
if old != realPath {
_ = os.Remove(target)
err := os.Symlink(realPath, target)
if err != nil {
errorList = append(errorList, err.Error())
}
}
}
if len(errorList) > 0 {
return errors.New(strings.Join(errorList, "\n"))
}
return nil
}

View File

@@ -1,7 +1,7 @@
package teaconst
const (
Version = "1.2.5"
Version = "1.2.8"
ProductName = "Edge API"
ProcessName = "edge-api"
@@ -18,7 +18,7 @@ const (
// 其他节点版本号,用来检测是否有需要升级的节点
NodeVersion = "1.2.5"
NodeVersion = "1.2.8"
// SQLVersion SQL版本号
SQLVersion = "11"

View File

@@ -2,6 +2,7 @@ package acme
import (
"bytes"
"context"
"encoding/json"
acmeutils "github.com/TeaOSLab/EdgeAPI/internal/acme"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
@@ -434,7 +435,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
CertData: certData,
KeyData: keyData,
}
err = sslConfig.Init(nil)
err = sslConfig.Init(context.Background())
if err != nil {
errMsg = "证书生成成功,但是分析证书信息时发生错误:" + err.Error()
return

View File

@@ -1,6 +1,7 @@
package models
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -37,7 +38,7 @@ func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverc
return nil, err
}
err = config.Init(nil)
err = config.Init(context.TODO())
if err != nil {
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 {
return nil, err
}
@@ -135,7 +136,7 @@ func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*ser
return nil, err
}
err = config.Init(nil)
err = config.Init(context.TODO())
if err != nil {
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 {
return nil, err
}

View File

@@ -61,11 +61,12 @@ func (this *DNSTaskDAO) CreateDNSTask(tx *dbs.Tx, clusterId int64, serverId int6
"error": "",
"version": time.Now().UnixNano(),
}, maps.Map{
"updatedAt": time.Now().Unix(),
"isDone": false,
"isOk": false,
"error": "",
"version": time.Now().UnixNano(),
"updatedAt": time.Now().Unix(),
"isDone": false,
"isOk": false,
"error": "",
"version": time.Now().UnixNano(),
"countFails": 0,
})
if err != nil {
return err
@@ -108,7 +109,7 @@ func (this *DNSTaskDAO) CreateDomainTask(tx *dbs.Tx, domainId int64, taskType DN
// FindAllDoingTasks 查找所有正在执行的任务
func (this *DNSTaskDAO) FindAllDoingTasks(tx *dbs.Tx) (result []*DNSTask, err error) {
_, err = this.Query(tx).
Attr("isDone", 0).
Where("(isDone=0 OR (isDone=1 AND isOk=0 AND countFails<3))"). // 3 = retry times
Asc("version").
AscPk().
Slice(&result).
@@ -171,6 +172,7 @@ func (this *DNSTaskDAO) UpdateDNSTaskError(tx *dbs.Tx, taskId int64, err string)
op.IsDone = true
op.Error = err
op.IsOk = false
op.CountFails = dbs.SQL("countFails+1")
return this.Save(tx, op)
}
@@ -197,6 +199,7 @@ func (this *DNSTaskDAO) UpdateDNSTaskDone(tx *dbs.Tx, taskId int64, taskVersion
op.Id = taskId
op.IsDone = true
op.IsOk = true
op.CountFails = 0
op.Error = ""
return this.Save(tx, op)
}
@@ -219,6 +222,7 @@ func (this *DNSTaskDAO) UpdateClusterDNSTasksDone(tx *dbs.Tx, clusterId int64, m
Set("isDone", true).
Set("isOk", true).
Set("error", "").
Set("countFails", 0).
UpdateQuickly()
}

View File

@@ -1,5 +1,23 @@
package dns
import "github.com/iwind/TeaGo/dbs"
const (
DNSTaskField_Id dbs.FieldName = "id" // ID
DNSTaskField_ClusterId dbs.FieldName = "clusterId" // 集群ID
DNSTaskField_ServerId dbs.FieldName = "serverId" // 服务ID
DNSTaskField_NodeId dbs.FieldName = "nodeId" // 节点ID
DNSTaskField_DomainId dbs.FieldName = "domainId" // 域名ID
DNSTaskField_RecordName dbs.FieldName = "recordName" // 记录名
DNSTaskField_Type dbs.FieldName = "type" // 任务类型
DNSTaskField_UpdatedAt dbs.FieldName = "updatedAt" // 更新时间
DNSTaskField_IsDone dbs.FieldName = "isDone" // 是否已完成
DNSTaskField_IsOk dbs.FieldName = "isOk" // 是否成功
DNSTaskField_Error dbs.FieldName = "error" // 错误信息
DNSTaskField_Version dbs.FieldName = "version" // 版本
DNSTaskField_CountFails dbs.FieldName = "countFails" // 尝试失败次数
)
// DNSTask DNS更新任务
type DNSTask struct {
Id uint64 `field:"id"` // ID
@@ -14,6 +32,7 @@ type DNSTask struct {
IsOk bool `field:"isOk"` // 是否成功
Error string `field:"error"` // 错误信息
Version uint64 `field:"version"` // 版本
CountFails uint32 `field:"countFails"` // 尝试失败次数
}
type DNSTaskOperator struct {
@@ -29,6 +48,7 @@ type DNSTaskOperator struct {
IsOk any // 是否成功
Error any // 错误信息
Version any // 版本
CountFails any // 尝试失败次数
}
func NewDNSTaskOperator() *DNSTaskOperator {

View File

@@ -3,6 +3,7 @@
package dnsutils
import (
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
@@ -217,7 +218,7 @@ func FindDefaultDomainRoute(tx *dbs.Tx, domain *dns.DNSDomain) (string, error) {
}
paramsMap, err := provider.DecodeAPIParams()
if err != nil {
return "", errors.New("decode provider params failed: " + err.Error())
return "", fmt.Errorf("decode provider params failed: %w", err)
}
var dnsProvider = dnsclients.FindProvider(provider.Type, int64(provider.Id))
if dnsProvider == nil {

View File

@@ -232,7 +232,7 @@ Loop:
// CreateHTTPAccessLog 写入单条访问日志
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
var day = ""
var day string
// 注意:如果你修改了 TimeISO8601 的逻辑,这里也需要同步修改
if len(accessLog.TimeISO8601) > 10 {
day = strings.ReplaceAll(accessLog.TimeISO8601[:10], "-", "")

View File

@@ -41,7 +41,7 @@ func (this *HTTPAccessLogManager) FindTableNames(db *dbs.DB, day string) ([]stri
for _, prefix := range []string{"edgeHTTPAccessLogs_" + day + "%", "edgehttpaccesslogs_" + day + "%"} {
ones, columnNames, err := db.FindPreparedOnes(`SHOW TABLES LIKE '` + prefix + `'`)
if err != nil {
return nil, errors.New("query table names error: " + err.Error())
return nil, fmt.Errorf("query table names error: %w", err)
}
var columnName = columnNames[0]
@@ -88,7 +88,7 @@ func (this *HTTPAccessLogManager) FindTables(db *dbs.DB, day string) ([]*httpAcc
for _, prefix := range []string{"edgeHTTPAccessLogs_" + day + "%", "edgehttpaccesslogs_" + day + "%"} {
ones, columnNames, err := db.FindPreparedOnes(`SHOW TABLES LIKE '` + prefix + `'`)
if err != nil {
return nil, errors.New("query table names error: " + err.Error())
return nil, fmt.Errorf("query table names error: %w", err)
}
var columnName = columnNames[0]
@@ -373,7 +373,7 @@ func (this *HTTPAccessLogManager) findTableWithoutCache(db *dbs.DB, day string,
var lastInt64Id = types.Int64(lastId)
if accessLogRowsPerTable > 0 && lastInt64Id >= accessLogRowsPerTable {
// create next partial table
var nextTableName = ""
var nextTableName string
if accessLogTableMainReg.MatchString(lastTableName) {
nextTableName = prefix + "_0001"
} else if accessLogTablePartialReg.MatchString(lastTableName) {

View File

@@ -96,7 +96,7 @@ func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result
}
// CreateCachePolicy 创建缓存策略
func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte, syncCompressionCache bool) (int64, error) {
func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte, syncCompressionCache bool, fetchTimeoutJSON []byte) (int64, error) {
var op = NewHTTPCachePolicyOperator()
op.State = HTTPCachePolicyStateEnabled
op.IsOn = isOn
@@ -114,6 +114,10 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st
}
op.SyncCompressionCache = syncCompressionCache
if len(fetchTimeoutJSON) > 0 {
op.FetchTimeout = fetchTimeoutJSON
}
// 默认的缓存条件
cacheRef := &serverconfigs.HTTPCacheRef{
IsOn: true,
@@ -183,7 +187,7 @@ func (this *HTTPCachePolicyDAO) CreateDefaultCachePolicy(tx *dbs.Tx, name string
return 0, err
}
policyId, err := this.CreateCachePolicy(tx, true, "\""+name+"\"缓存策略", "默认创建的缓存策略", capacityJSON, maxSizeJSON, serverconfigs.CachePolicyStorageFile, storageOptionsJSON, false)
policyId, err := this.CreateCachePolicy(tx, true, "\""+name+"\"缓存策略", "默认创建的缓存策略", capacityJSON, maxSizeJSON, serverconfigs.CachePolicyStorageFile, storageOptionsJSON, false, nil)
if err != nil {
return 0, err
}
@@ -191,7 +195,7 @@ func (this *HTTPCachePolicyDAO) CreateDefaultCachePolicy(tx *dbs.Tx, name string
}
// UpdateCachePolicy 修改缓存策略
func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, capacityJSON []byte, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte, syncCompressionCache bool) error {
func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, capacityJSON []byte, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte, syncCompressionCache bool, fetchTimeoutJSON []byte) error {
if policyId <= 0 {
return errors.New("invalid policyId")
}
@@ -212,6 +216,9 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
op.Options = storageOptionsJSON
}
op.SyncCompressionCache = syncCompressionCache
if len(fetchTimeoutJSON) > 0 {
op.FetchTimeout = fetchTimeoutJSON
}
err := this.Save(tx, op)
if err != nil {
return err
@@ -237,7 +244,7 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
if policy == nil {
return nil, nil
}
config := &serverconfigs.HTTPCachePolicy{}
var config = &serverconfigs.HTTPCachePolicy{}
config.Id = int64(policy.Id)
config.IsOn = policy.IsOn
config.Name = policy.Name
@@ -246,7 +253,7 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
// capacity
if IsNotNull(policy.Capacity) {
capacityConfig := &shared.SizeCapacity{}
var capacityConfig = &shared.SizeCapacity{}
err = json.Unmarshal(policy.Capacity, capacityConfig)
if err != nil {
return nil, err
@@ -256,7 +263,7 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
// max size
if IsNotNull(policy.MaxSize) {
maxSizeConfig := &shared.SizeCapacity{}
var maxSizeConfig = &shared.SizeCapacity{}
err = json.Unmarshal(policy.MaxSize, maxSizeConfig)
if err != nil {
return nil, err
@@ -268,7 +275,7 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
// options
if IsNotNull(policy.Options) {
m := map[string]interface{}{}
var m = map[string]any{}
err = json.Unmarshal(policy.Options, &m)
if err != nil {
return nil, errors.Wrap(err)
@@ -278,7 +285,7 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
// refs
if IsNotNull(policy.Refs) {
refs := []*serverconfigs.HTTPCacheRef{}
var refs = []*serverconfigs.HTTPCacheRef{}
err = json.Unmarshal(policy.Refs, &refs)
if err != nil {
return nil, err
@@ -286,6 +293,16 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
config.CacheRefs = refs
}
// fetch timeout
if IsNotNull(policy.FetchTimeout) {
var timeoutDuration = &shared.TimeDuration{}
err = json.Unmarshal(policy.FetchTimeout, timeoutDuration)
if err != nil {
return nil, err
}
config.FetchTimeout = timeoutDuration
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}

View File

@@ -2,6 +2,26 @@ package models
import "github.com/iwind/TeaGo/dbs"
const (
HTTPCachePolicyField_Id dbs.FieldName = "id" // ID
HTTPCachePolicyField_AdminId dbs.FieldName = "adminId" // 管理员ID
HTTPCachePolicyField_UserId dbs.FieldName = "userId" // 用户ID
HTTPCachePolicyField_TemplateId dbs.FieldName = "templateId" // 模版ID
HTTPCachePolicyField_IsOn dbs.FieldName = "isOn" // 是否启用
HTTPCachePolicyField_Name dbs.FieldName = "name" // 名称
HTTPCachePolicyField_Capacity dbs.FieldName = "capacity" // 容量数据
HTTPCachePolicyField_MaxKeys dbs.FieldName = "maxKeys" // 最多Key值
HTTPCachePolicyField_MaxSize dbs.FieldName = "maxSize" // 最大缓存内容尺寸
HTTPCachePolicyField_Type dbs.FieldName = "type" // 存储类型
HTTPCachePolicyField_Options dbs.FieldName = "options" // 存储选项
HTTPCachePolicyField_CreatedAt dbs.FieldName = "createdAt" // 创建时间
HTTPCachePolicyField_State dbs.FieldName = "state" // 状态
HTTPCachePolicyField_Description dbs.FieldName = "description" // 描述
HTTPCachePolicyField_Refs dbs.FieldName = "refs" // 默认的缓存设置
HTTPCachePolicyField_SyncCompressionCache dbs.FieldName = "syncCompressionCache" // 是否同步写入压缩缓存
HTTPCachePolicyField_FetchTimeout dbs.FieldName = "fetchTimeout" // 预热超时时间
)
// HTTPCachePolicy HTTP缓存策略
type HTTPCachePolicy struct {
Id uint32 `field:"id"` // ID
@@ -20,25 +40,27 @@ type HTTPCachePolicy struct {
Description string `field:"description"` // 描述
Refs dbs.JSON `field:"refs"` // 默认的缓存设置
SyncCompressionCache uint8 `field:"syncCompressionCache"` // 是否同步写入压缩缓存
FetchTimeout dbs.JSON `field:"fetchTimeout"` // 预热超时时间
}
type HTTPCachePolicyOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
TemplateId interface{} // 模版ID
IsOn interface{} // 是否启用
Name interface{} // 名称
Capacity interface{} // 容量数据
MaxKeys interface{} // 最多Key值
MaxSize interface{} // 最大缓存内容尺寸
Type interface{} // 存储类型
Options interface{} // 存储选项
CreatedAt interface{} // 创建时间
State interface{} // 状态
Description interface{} // 描述
Refs interface{} // 默认的缓存设置
SyncCompressionCache interface{} // 是否同步写入压缩缓存
Id any // ID
AdminId any // 管理员ID
UserId any // 用户ID
TemplateId any // 模版ID
IsOn any // 是否启用
Name any // 名称
Capacity any // 容量数据
MaxKeys any // 最多Key值
MaxSize any // 最大缓存内容尺寸
Type any // 存储类型
Options any // 存储选项
CreatedAt any // 创建时间
State any // 状态
Description any // 描述
Refs any // 默认的缓存设置
SyncCompressionCache any // 是否同步写入压缩缓存
FetchTimeout any // 预热超时时间
}
func NewHTTPCachePolicyOperator() *HTTPCachePolicyOperator {

View File

@@ -172,16 +172,18 @@ func (this *HTTPFirewallPolicyDAO) CreateDefaultFirewallPolicy(tx *dbs.Tx, name
// 初始化
var groupCodes = []string{}
templatePolicy := firewallconfigs.HTTPFirewallTemplate()
var templatePolicy = firewallconfigs.HTTPFirewallTemplate()
for _, group := range templatePolicy.AllRuleGroups() {
groupCodes = append(groupCodes, group.Code)
if group.IsOn {
groupCodes = append(groupCodes, group.Code)
}
}
var inboundConfig = &firewallconfigs.HTTPFirewallInboundConfig{IsOn: true}
var outboundConfig = &firewallconfigs.HTTPFirewallOutboundConfig{IsOn: true}
if templatePolicy.Inbound != nil {
for _, group := range templatePolicy.Inbound.Groups {
isOn := lists.ContainsString(groupCodes, group.Code)
var isOn = lists.ContainsString(groupCodes, group.Code)
group.IsOn = isOn
groupId, err := SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, group)
@@ -196,7 +198,7 @@ func (this *HTTPFirewallPolicyDAO) CreateDefaultFirewallPolicy(tx *dbs.Tx, name
}
if templatePolicy.Outbound != nil {
for _, group := range templatePolicy.Outbound.Groups {
isOn := lists.ContainsString(groupCodes, group.Code)
var isOn = lists.ContainsString(groupCodes, group.Code)
group.IsOn = isOn
groupId, err := SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, group)
@@ -290,7 +292,10 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx,
mode firewallconfigs.FirewallMode,
useLocalFirewall bool,
synFloodConfig *firewallconfigs.SYNFloodConfig,
logConfig *firewallconfigs.HTTPFirewallPolicyLogConfig) error {
logConfig *firewallconfigs.HTTPFirewallPolicyLogConfig,
maxRequestBodySize int64,
denyCountryHTML string,
denyProvinceHTML string) error {
if policyId <= 0 {
return errors.New("invalid policyId")
}
@@ -338,6 +343,10 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx,
}
op.UseLocalFirewall = useLocalFirewall
op.MaxRequestBodySize = maxRequestBodySize
op.DenyCountryHTML = denyCountryHTML
op.DenyProvinceHTML = denyProvinceHTML
err := this.Save(tx, op)
if err != nil {
return err
@@ -390,7 +399,7 @@ func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, clust
}
// ComposeFirewallPolicy 组合策略配置
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*firewallconfigs.HTTPFirewallPolicy, error) {
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, forNode bool, cacheMap *utils.CacheMap) (*firewallconfigs.HTTPFirewallPolicy, error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
@@ -414,6 +423,9 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
config.Name = policy.Name
config.Description = policy.Description
config.UseLocalFirewall = policy.UseLocalFirewall == 1
config.MaxRequestBodySize = int64(policy.MaxRequestBodySize)
config.DenyCountryHTML = policy.DenyCountryHTML
config.DenyProvinceHTML = policy.DenyProvinceHTML
if len(policy.Mode) == 0 {
policy.Mode = firewallconfigs.FirewallModeDefend
@@ -421,18 +433,18 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
config.Mode = policy.Mode
// Inbound
inbound := &firewallconfigs.HTTPFirewallInboundConfig{}
var inbound = &firewallconfigs.HTTPFirewallInboundConfig{}
if IsNotNull(policy.Inbound) {
err = json.Unmarshal(policy.Inbound, inbound)
if err != nil {
return nil, err
}
if len(inbound.GroupRefs) > 0 {
resultGroupRefs := []*firewallconfigs.HTTPFirewallRuleGroupRef{}
resultGroups := []*firewallconfigs.HTTPFirewallRuleGroup{}
var resultGroupRefs = []*firewallconfigs.HTTPFirewallRuleGroupRef{}
var resultGroups = []*firewallconfigs.HTTPFirewallRuleGroup{}
for _, groupRef := range inbound.GroupRefs {
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, groupRef.GroupId)
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, groupRef.GroupId, forNode)
if err != nil {
return nil, err
}
@@ -449,18 +461,18 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
config.Inbound = inbound
// Outbound
outbound := &firewallconfigs.HTTPFirewallOutboundConfig{}
var outbound = &firewallconfigs.HTTPFirewallOutboundConfig{}
if IsNotNull(policy.Outbound) {
err = json.Unmarshal(policy.Outbound, outbound)
if err != nil {
return nil, err
}
if len(outbound.GroupRefs) > 0 {
resultGroupRefs := []*firewallconfigs.HTTPFirewallRuleGroupRef{}
resultGroups := []*firewallconfigs.HTTPFirewallRuleGroup{}
var resultGroupRefs = []*firewallconfigs.HTTPFirewallRuleGroupRef{}
var resultGroups = []*firewallconfigs.HTTPFirewallRuleGroup{}
for _, groupRef := range outbound.GroupRefs {
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, groupRef.GroupId)
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, groupRef.GroupId, forNode)
if err != nil {
return nil, err
}

View File

@@ -2,49 +2,80 @@ package models
import "github.com/iwind/TeaGo/dbs"
const (
HTTPFirewallPolicyField_Id dbs.FieldName = "id" // ID
HTTPFirewallPolicyField_TemplateId dbs.FieldName = "templateId" // 模版ID
HTTPFirewallPolicyField_AdminId dbs.FieldName = "adminId" // 管理员ID
HTTPFirewallPolicyField_UserId dbs.FieldName = "userId" // 用户ID
HTTPFirewallPolicyField_ServerId dbs.FieldName = "serverId" // 服务ID
HTTPFirewallPolicyField_GroupId dbs.FieldName = "groupId" // 服务分组ID
HTTPFirewallPolicyField_State dbs.FieldName = "state" // 状态
HTTPFirewallPolicyField_CreatedAt dbs.FieldName = "createdAt" // 创建时间
HTTPFirewallPolicyField_IsOn dbs.FieldName = "isOn" // 是否启用
HTTPFirewallPolicyField_Name dbs.FieldName = "name" // 名称
HTTPFirewallPolicyField_Description dbs.FieldName = "description" // 描述
HTTPFirewallPolicyField_Inbound dbs.FieldName = "inbound" // 入站规则
HTTPFirewallPolicyField_Outbound dbs.FieldName = "outbound" // 出站规则
HTTPFirewallPolicyField_BlockOptions dbs.FieldName = "blockOptions" // BLOCK选项
HTTPFirewallPolicyField_CaptchaOptions dbs.FieldName = "captchaOptions" // 验证码选项
HTTPFirewallPolicyField_Mode dbs.FieldName = "mode" // 模式
HTTPFirewallPolicyField_UseLocalFirewall dbs.FieldName = "useLocalFirewall" // 是否自动使用本地防火墙
HTTPFirewallPolicyField_SynFlood dbs.FieldName = "synFlood" // SynFlood防御设置
HTTPFirewallPolicyField_Log dbs.FieldName = "log" // 日志配置
HTTPFirewallPolicyField_MaxRequestBodySize dbs.FieldName = "maxRequestBodySize" // 可以检查的最大请求内容尺寸
HTTPFirewallPolicyField_DenyCountryHTML dbs.FieldName = "denyCountryHTML" // 区域封禁提示
HTTPFirewallPolicyField_DenyProvinceHTML dbs.FieldName = "denyProvinceHTML" // 省份封禁提示
)
// HTTPFirewallPolicy HTTP防火墙
type HTTPFirewallPolicy struct {
Id uint32 `field:"id"` // ID
TemplateId uint32 `field:"templateId"` // 模版ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
ServerId uint32 `field:"serverId"` // 服务ID
GroupId uint32 `field:"groupId"` // 服务分组ID
State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
Inbound dbs.JSON `field:"inbound"` // 入站规则
Outbound dbs.JSON `field:"outbound"` // 出站规则
BlockOptions dbs.JSON `field:"blockOptions"` // BLOCK选项
CaptchaOptions dbs.JSON `field:"captchaOptions"` // 验证码选项
Mode string `field:"mode"` // 模式
UseLocalFirewall uint8 `field:"useLocalFirewall"` // 是否自动使用本地防火墙
SynFlood dbs.JSON `field:"synFlood"` // SynFlood防御设置
Log dbs.JSON `field:"log"` // 日志配置
Id uint32 `field:"id"` // ID
TemplateId uint32 `field:"templateId"` // 模版ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
ServerId uint32 `field:"serverId"` // 服务ID
GroupId uint32 `field:"groupId"` // 服务分组ID
State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
Inbound dbs.JSON `field:"inbound"` // 入站规则
Outbound dbs.JSON `field:"outbound"` // 出站规则
BlockOptions dbs.JSON `field:"blockOptions"` // BLOCK选项
CaptchaOptions dbs.JSON `field:"captchaOptions"` // 验证码选项
Mode string `field:"mode"` // 模式
UseLocalFirewall uint8 `field:"useLocalFirewall"` // 是否自动使用本地防火墙
SynFlood dbs.JSON `field:"synFlood"` // SynFlood防御设置
Log dbs.JSON `field:"log"` // 日志配置
MaxRequestBodySize uint32 `field:"maxRequestBodySize"` // 可以检查的最大请求内容尺寸
DenyCountryHTML string `field:"denyCountryHTML"` // 区域封禁提示
DenyProvinceHTML string `field:"denyProvinceHTML"` // 省份封禁提示
}
type HTTPFirewallPolicyOperator struct {
Id interface{} // ID
TemplateId interface{} // 模版ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
ServerId interface{} // 服务ID
GroupId interface{} // 服务分组ID
State interface{} // 状态
CreatedAt interface{} // 创建时间
IsOn interface{} // 是否启用
Name interface{} // 名称
Description interface{} // 描述
Inbound interface{} // 入站规则
Outbound interface{} // 出站规则
BlockOptions interface{} // BLOCK选项
CaptchaOptions interface{} // 验证码选项
Mode interface{} // 模式
UseLocalFirewall interface{} // 是否自动使用本地防火墙
SynFlood interface{} // SynFlood防御设置
Log interface{} // 日志配置
Id any // ID
TemplateId any // 模版ID
AdminId any // 管理员ID
UserId any // 用户ID
ServerId any // 服务ID
GroupId any // 服务分组ID
State any // 状态
CreatedAt any // 创建时间
IsOn any // 是否启用
Name any // 名称
Description any // 描述
Inbound any // 入站规则
Outbound any // 出站规则
BlockOptions any // BLOCK选项
CaptchaOptions any // 验证码选项
Mode any // 模式
UseLocalFirewall any // 是否自动使用本地防火墙
SynFlood any // SynFlood防御设置
Log any // 日志配置
MaxRequestBodySize any // 可以检查的最大请求内容尺寸
DenyCountryHTML any // 区域封禁提示
DenyProvinceHTML any // 省份封禁提示
}
func NewHTTPFirewallPolicyOperator() *HTTPFirewallPolicyOperator {

View File

@@ -81,7 +81,7 @@ func (this *HTTPFirewallRuleGroupDAO) FindHTTPFirewallRuleGroupName(tx *dbs.Tx,
}
// ComposeFirewallRuleGroup 组合配置
func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, groupId int64) (*firewallconfigs.HTTPFirewallRuleGroup, error) {
func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, groupId int64, forNode bool) (*firewallconfigs.HTTPFirewallRuleGroup, error) {
group, err := this.FindEnabledHTTPFirewallRuleGroup(tx, groupId)
if err != nil {
return nil, err
@@ -89,7 +89,7 @@ func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, group
if group == nil {
return nil, nil
}
config := &firewallconfigs.HTTPFirewallRuleGroup{}
var config = &firewallconfigs.HTTPFirewallRuleGroup{}
config.Id = int64(group.Id)
config.IsOn = group.IsOn
config.Name = group.Name
@@ -98,7 +98,7 @@ func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, group
config.IsTemplate = group.IsTemplate
if IsNotNull(group.Sets) {
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
var setRefs = []*firewallconfigs.HTTPFirewallRuleSetRef{}
err = json.Unmarshal(group.Sets, &setRefs)
if err != nil {
return nil, err
@@ -108,7 +108,7 @@ func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, group
if err != nil {
return nil, err
}
if setConfig != nil {
if setConfig != nil && (!forNode || setConfig.IsOn) {
config.SetRefs = append(config.SetRefs, setRef)
config.Sets = append(config.Sets, setConfig)
}

View File

@@ -101,7 +101,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, isLocationOrGr
// root
if IsNotNull(web.Root) {
var rootConfig = &serverconfigs.HTTPRootConfig{}
var rootConfig = serverconfigs.NewHTTPRootConfig()
err = json.Unmarshal(web.Root, rootConfig)
if err != nil {
return nil, err
@@ -301,7 +301,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, isLocationOrGr
// 自定义防火墙设置
if firewallRef.FirewallPolicyId > 0 {
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, firewallRef.FirewallPolicyId, cacheMap)
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, firewallRef.FirewallPolicyId, forNode, cacheMap)
if err != nil {
return nil, err
}
@@ -519,6 +519,14 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, isLocationOrGr
}
if this.shouldCompose(isLocationOrGroup, forNode, ccConfig.IsPrior, ccConfig.IsOn) {
config.CC = ccConfig
if forNode {
for index, threshold := range ccConfig.Thresholds {
if index < len(serverconfigs.DefaultHTTPCCThresholds) {
threshold.MergeIfEmpty(serverconfigs.DefaultHTTPCCThresholds[index])
}
}
}
}
}

View File

@@ -674,6 +674,9 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
}
} else {
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
if err != nil {
return err
}
for _, clusterId := range clusterIds {
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeIPItemChanged)
if err != nil {

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"errors"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
@@ -299,7 +300,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
var libraryFile = one.(*IPLibraryFile)
template, err := iplibrary.NewTemplate(libraryFile.Template)
if err != nil {
return errors.New("create template from '" + libraryFile.Template + "' failed: " + err.Error())
return fmt.Errorf("create template from '%s' failed: %w", libraryFile.Template, err)
}
var fileId = int64(libraryFile.FileId)
@@ -314,17 +315,17 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
if os.IsNotExist(err) {
err = os.Mkdir(dir, 0777)
if err != nil {
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
return fmt.Errorf("can not open dir '%s' to write: %w", dir, err)
}
} else {
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
return fmt.Errorf("can not open dir '%s' to write: %w", dir, err)
}
} else if !stat.IsDir() {
_ = os.Remove(dir)
err = os.Mkdir(dir, 0777)
if err != nil {
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
return fmt.Errorf("can not open dir '%s' to write: %w", dir, err)
}
}
@@ -428,7 +429,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
err = writer.WriteMeta()
if err != nil {
return errors.New("write meta failed: " + err.Error())
return fmt.Errorf("write meta failed: %w", err)
}
chunkIds, err := SharedFileChunkDAO.FindAllFileChunkIds(tx, fileId)
@@ -503,7 +504,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
err = writer.Write(ipFrom, ipTo, countryId, provinceId, cityId, townId, providerId)
if err != nil {
return errors.New("write failed: " + err.Error())
return fmt.Errorf("write failed: %w", err)
}
return nil
@@ -536,7 +537,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
// 将生成的内容写入到文件
stat, err = os.Stat(filePath)
if err != nil {
return errors.New("stat generated file failed: " + err.Error())
return fmt.Errorf("stat generated file failed: %w", err)
}
generatedFileId, err := SharedFileDAO.CreateFile(tx, 0, 0, "ipLibraryFile", "", libraryCode+".db", stat.Size(), "", false)
if err != nil {
@@ -545,7 +546,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
fp, err := os.Open(filePath)
if err != nil {
return errors.New("open generated file failed: " + err.Error())
return fmt.Errorf("open generated file failed: %w", err)
}
var buf = make([]byte, 256*1024)
for {

View File

@@ -1039,9 +1039,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, dataMap *shared
if err != nil {
return nil, err
}
for _, clusterServer := range clusterServers {
servers = append(servers, clusterServer)
}
servers = append(servers, clusterServers...)
}
for _, server := range servers {
@@ -1063,7 +1061,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, dataMap *shared
// TODO 根据用户的不同读取不同的全局设置
var settingCacheKey = "SharedSysSettingDAO:" + systemconfigs.SettingCodeServerGlobalConfig
settingJSONCache, ok := cacheMap.Get(settingCacheKey)
var settingJSON = []byte{}
var settingJSON []byte
if ok {
settingJSON = settingJSONCache.([]byte)
} else {
@@ -1119,7 +1117,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, dataMap *shared
// 防火墙
var httpFirewallPolicyId = int64(nodeCluster.HttpFirewallPolicyId)
if httpFirewallPolicyId > 0 {
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId, cacheMap)
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId, true, cacheMap)
if err != nil {
return nil, err
}
@@ -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)))
for _, clusterId := range node.AllClusterIds() {
parentNodeConfigs, _ := result[clusterId]
var parentNodeConfigs = result[clusterId]
parentNodeConfigs = append(parentNodeConfigs, &nodeconfigs.ParentNodeConfig{
Id: int64(node.Id),
Addrs: addrStrings,

View File

@@ -70,8 +70,7 @@ func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error)
if err != nil {
return nil, err
}
domainRoutes, _ := routes[dnsDomainId]
var domainRoutes = routes[dnsDomainId]
if len(domainRoutes) > 0 {
sort.Strings(domainRoutes)
}

View File

@@ -127,6 +127,9 @@ func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId stri
pinyinResult = append(pinyinResult, strings.Join(piece, " "))
}
pinyinJSON, err := json.Marshal([]string{strings.Join(pinyinResult, " ")})
if err != nil {
return 0, err
}
op.Pinyin = pinyinJSON
codes := []string{name}

View File

@@ -844,6 +844,11 @@ func (this *ServerBandwidthStatDAO) fixServerStats(stats []*ServerBandwidthStat,
// HasFullData 检查一个月是否完整数据
// 是为了兼容以前数据,以前的表中没有缓存流量、请求数等字段
func (this *ServerBandwidthStatDAO) HasFullData(tx *dbs.Tx, serverId int64, month string) (bool, error) {
// 最迟在2024年完成过渡
if time.Now().Year() >= 2024 {
return true, nil
}
var monthKey = month + "@" + types.String(serverId)
if !regexputils.YYYYMM.MatchString(month) {

View File

@@ -1,6 +1,7 @@
package models
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -78,7 +79,7 @@ func (this *Server) DecodeHTTPSPorts() (ports []int) {
if err != nil {
return nil
}
err = config.Init(nil)
err = config.Init(context.TODO())
if err != nil {
return nil
}
@@ -120,7 +121,7 @@ func (this *Server) DecodeTLSPorts() (ports []int) {
if err != nil {
return nil
}
err = config.Init(nil)
err = config.Init(context.TODO())
if err != nil {
return nil
}

View File

@@ -701,7 +701,7 @@ func (this *SSLCertDAO) buildDomainSearchingQuery(query *dbs.Query, domains []st
}
// 检测 JSON_OVERLAPS() 函数是否可用
var canJSONOverlaps = false
var canJSONOverlaps bool
_, funcErr := this.Instance.FindCol(0, "SELECT JSON_OVERLAPS('[1]', '[1]')")
canJSONOverlaps = funcErr == nil
if canJSONOverlaps {

View File

@@ -70,7 +70,7 @@ func TestServerDomainHourlyStatDAO_FindTopDomainStats(t *testing.T) {
func TestServerDomainHourlyStatDAO_Clean(t *testing.T) {
var dao = NewServerDomainHourlyStatDAO()
err := dao.Clean(nil, 10)
err := dao.CleanDays(nil, 10)
if err != nil {
t.Fatal(err)
}

View File

@@ -160,7 +160,7 @@ func (this *SysLockerDAO) Increase(tx *dbs.Tx, key string, defaultValue int64) (
colValue, err := tx.FindCol(0, "INSERT INTO `"+this.Table+"` (`key`, `version`) VALUES ('"+key+"', "+types.String(defaultValue+sysLockerStep)+") ON DUPLICATE KEY UPDATE `version`=`version`+"+types.String(sysLockerStep)+"; SELECT `version` FROM `"+this.Table+"` WHERE `key`='"+key+"'")
if err != nil {
if CheckSQLErrCode(err, 1064 /** syntax error **/) {
// continue to use seperated query
// continue to use separated query
err = nil
} else {
return 0, err

View File

@@ -522,16 +522,6 @@ func (this *UserBandwidthStatDAO) sumBytesField(useAvg bool) string {
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 检查一个月是否完整数据
// 是为了兼容以前数据,以前的表中没有缓存流量、请求数等字段
func (this *UserBandwidthStatDAO) HasFullData(tx *dbs.Tx, userId int64, month string) (bool, error) {

View File

@@ -1,6 +1,7 @@
package models
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -36,7 +37,7 @@ func (this *UserNode) DecodeHTTPS(cacheMap *utils.CacheMap) (*serverconfigs.HTTP
return nil, err
}
err = config.Init(nil)
err = config.Init(context.TODO())
if err != nil {
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 {
return nil, err
}

View File

@@ -61,10 +61,7 @@ func (this *DomainRecordsCache) WriteDomainRecords(providerId int64, domain stri
return
}
var clonedRecords = []*dnstypes.Record{}
for _, record := range records {
clonedRecords = append(clonedRecords, record)
}
var clonedRecords = append([]*dnstypes.Record{}, records...)
this.domainRecordsMap[domain] = &recordList{
version: version,
updatedAt: time.Now().Unix(),

View File

@@ -1,9 +1,8 @@
package dnsclients
import (
"errors"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/types"
)
type BaseProvider struct{}
@@ -18,11 +17,11 @@ func (this *BaseProvider) WrapError(err error, domain string, record *dnstypes.R
return err
}
var fullname = ""
var fullname string
if len(record.Name) == 0 {
fullname = domain
} else {
fullname = record.Name + "." + domain
}
return errors.New("record operation failed: '" + fullname + " " + record.Type + " " + record.Value + " " + types.String(record.TTL) + "': " + err.Error())
return fmt.Errorf("record operation failed: '%s %s %s %d': %w", fullname, record.Type, record.Value, record.TTL, err)
}

View File

@@ -6,6 +6,7 @@ import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/cloudflare"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
@@ -337,7 +338,7 @@ func (this *CloudFlareProvider) doAPI(method string, apiPath string, args map[st
err = json.Unmarshal(data, respPtr)
if err != nil {
return errors.New("decode json failed: " + err.Error() + ", response text: " + string(data))
return fmt.Errorf("decode json failed: %w, response text: %s", err, string(data))
}
return nil

View File

@@ -201,7 +201,7 @@ func (this *CustomHTTPProvider) post(params maps.Map) (respData []byte, err erro
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
return nil, errors.New("status should be 200, but got '" + strconv.Itoa(resp.StatusCode) + "'")
}
return io.ReadAll(resp.Body)

View File

@@ -4,6 +4,7 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnspod"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
@@ -380,7 +381,7 @@ func (this *DNSPodProvider) doAPI(path string, params map[string]string, respPtr
req, err := http.NewRequest(http.MethodPost, apiHost+path, strings.NewReader(query.Encode()))
if err != nil {
return errors.New("create request failed: " + err.Error())
return fmt.Errorf("create request failed: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "GoEdge-Client/1.0.0 (iwind.liu@gmail.com)")

View File

@@ -7,6 +7,7 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/edgeapi"
@@ -435,6 +436,11 @@ func (this *EdgeDNSAPIProvider) doAPI(path string, params map[string]any, respPt
if err != nil {
return err
}
defer func() {
if resp.Body != nil {
_ = resp.Body.Close()
}
}()
if resp.StatusCode != http.StatusOK {
return errors.New("invalid response status code '" + types.String(resp.StatusCode) + "'")
@@ -447,7 +453,7 @@ func (this *EdgeDNSAPIProvider) doAPI(path string, params map[string]any, respPt
err = json.Unmarshal(data, respPtr)
if err != nil {
return errors.New("decode response failed: " + err.Error() + ", JSON: " + string(data))
return fmt.Errorf("decode response failed: %w, JSON: %s", err, string(data))
}
if !respPtr.IsValid() {

View File

@@ -19,7 +19,6 @@ func TestAES128CFBMethod_Encrypt(t *testing.T) {
dst = dst[:len(src)]
t.Log("dst:", string(dst))
src = make([]byte, len(src))
src, err = method.Decrypt(dst)
if err != nil {
t.Fatal(err)
@@ -64,7 +63,6 @@ func TestAES128CFBMethod_Encrypt2(t *testing.T) {
for _, dst := range sources {
dst2 := append([]byte{}, dst...)
src2 := make([]byte, len(dst2))
src2, err := method.Decrypt(dst2)
if err != nil {
t.Fatal(err)

View File

@@ -13,9 +13,9 @@ func (this *DetailedError) Code() string {
return this.code
}
func NewDetailedError(code string, error string) *DetailedError {
func NewDetailedError(code string, errString string) *DetailedError {
return &DetailedError{
msg: error,
msg: errString,
code: code,
}
}

View File

@@ -10,7 +10,7 @@ func On(event string, callback func()) {
locker.Lock()
defer locker.Unlock()
callbacks, _ := eventsMap[event]
var callbacks = eventsMap[event]
callbacks = append(callbacks, callback)
eventsMap[event] = callbacks
}
@@ -18,9 +18,9 @@ func On(event string, callback func()) {
// Notify 通知事件
func Notify(event string) {
locker.Lock()
callbacks, _ := eventsMap[event]
var callbacks = eventsMap[event]
locker.Unlock()
for _, callback := range callbacks {
callback()
}

View File

@@ -2,6 +2,7 @@ package installers
import (
"errors"
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/Tea"
@@ -67,7 +68,7 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
signer, err = ssh.ParsePrivateKey([]byte(credentials.PrivateKey))
}
if err != nil {
return errors.New("parse private key: " + err.Error())
return fmt.Errorf("parse private key: %w", err)
}
authMethod := ssh.PublicKeys(signer)
methods = append(methods, authMethod)
@@ -149,8 +150,8 @@ func (this *BaseInstaller) LookupLatestInstaller(filePrefix string) (string, err
func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.NodeRole) (env *Env, err error) {
var uname = this.uname()
var osName = ""
var archName = ""
var osName string
var archName string
if strings.Contains(uname, "Darwin") {
osName = "darwin"
} else if strings.Contains(uname, "Linux") {

View File

@@ -3,6 +3,7 @@ package installers
import (
"bytes"
"errors"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"os"
@@ -24,7 +25,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
}
err := nodeParams.Validate()
if err != nil {
return errors.New("params validation: " + err.Error())
return fmt.Errorf("params validation: %w", err)
}
// 检查目标目录是否存在
@@ -33,7 +34,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
err = this.client.MkdirAll(dir)
if err != nil {
installStatus.ErrorCode = "CREATE_ROOT_DIRECTORY_FAILED"
return errors.New("create directory '" + dir + "' failed: " + err.Error())
return fmt.Errorf("create directory '%s' failed: %w", dir, err)
}
}
@@ -74,7 +75,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
}
}
if firstCopyErr != nil {
return errors.New("upload node file failed: " + firstCopyErr.Error())
return fmt.Errorf("upload node file failed: %w", firstCopyErr)
}
// 测试运行环境
@@ -82,7 +83,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
if !nodeParams.IsUpgrading {
_, stderr, err := this.client.Exec(env.HelperPath + " -cmd=test")
if err != nil {
return errors.New("test failed: " + err.Error())
return fmt.Errorf("test failed: %w", err)
}
if len(stderr) > 0 {
return errors.New("test failed: " + stderr)
@@ -99,7 +100,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
// 删除可执行文件防止冲突
err = this.client.Remove(exePath)
if err != nil && err != os.ErrNotExist {
return errors.New("remove old file failed: " + err.Error())
return fmt.Errorf("remove old file failed: %w", err)
}
}
}
@@ -115,15 +116,14 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
// 修改配置文件
{
configFile := dir + "/edge-node/configs/api.yaml"
var configFile = dir + "/edge-node/configs/api_node.yaml"
// sudo之后我们需要修改配置目录才能写入文件
if this.client.sudo {
_, _, _ = this.client.Exec("chown " + this.client.User() + " " + filepath.Dir(configFile))
}
var data = []byte(`rpc:
endpoints: [ ${endpoints} ]
var data = []byte(`rpc.endpoints: [ ${endpoints} ]
nodeId: "${nodeId}"
secret: "${nodeSecret}"`)
@@ -133,7 +133,7 @@ secret: "${nodeSecret}"`)
_, err = this.client.WriteFile(configFile, data)
if err != nil {
return errors.New("write '" + configFile + "': " + err.Error())
return fmt.Errorf("write '%s': %w", configFile, err)
}
}
@@ -141,7 +141,7 @@ secret: "${nodeSecret}"`)
_, stderr, err = this.client.Exec(dir + "/edge-node/bin/edge-node test")
if err != nil {
installStatus.ErrorCode = "TEST_FAILED"
return errors.New("test edge node failed: " + err.Error() + ", stderr: " + stderr)
return fmt.Errorf("test edge node failed: %w, stderr: %s", err, stderr)
}
if len(stderr) > 0 {
if regexp.MustCompile(`(?i)rpc`).MatchString(stderr) {
@@ -154,7 +154,7 @@ secret: "${nodeSecret}"`)
// 启动
_, stderr, err = this.client.Exec(dir + "/edge-node/bin/edge-node start")
if err != nil {
return errors.New("start edge node failed: " + err.Error())
return fmt.Errorf("start edge node failed: %w", err)
}
if len(stderr) > 0 {

View File

@@ -167,7 +167,7 @@ func (this *NodeQueue) InstallNode(nodeId int64, installStatus *models.NodeInsta
for _, apiNode := range apiNodes {
addrConfigs, err := apiNode.DecodeAccessAddrs()
if err != nil {
return errors.New("decode api node access addresses failed: " + err.Error())
return fmt.Errorf("decode api node access addresses failed: %w", err)
}
for _, addrConfig := range addrConfigs {
apiEndpoints = append(apiEndpoints, addrConfig.FullAddresses()...)
@@ -320,7 +320,7 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
// 执行start
_, stderr, err := installer.client.Exec("sudo " + exe + " start")
if err != nil {
return errors.New("start failed: " + err.Error())
return fmt.Errorf("start failed: %w", err)
}
if len(stderr) > 0 {
return errors.New("start failed: " + stderr)
@@ -427,7 +427,7 @@ func (this *NodeQueue) StopNode(nodeId int64) error {
// 执行stop
_, stderr, err := installer.client.Exec(exe + " stop")
if err != nil {
return errors.New("stop failed: " + err.Error())
return fmt.Errorf("stop failed: %w", err)
}
if len(stderr) > 0 {
return errors.New("stop failed: " + stderr)

View File

@@ -5,6 +5,7 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/configs"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
@@ -285,7 +286,7 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
this.registerServices(rpcServer)
err := rpcServer.Serve(listener)
if err != nil {
return errors.New("[API_NODE]start rpc failed: " + err.Error())
return fmt.Errorf("[API_NODE]start rpc failed: %w", err)
}
return nil
@@ -354,23 +355,23 @@ func (this *APINode) autoUpgrade() error {
var config = &dbs.Config{}
configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
if err != nil {
return errors.New("read database config file failed: " + err.Error())
return fmt.Errorf("read database config file failed: %w", err)
}
err = yaml.Unmarshal(configData, config)
if err != nil {
return errors.New("decode database config failed: " + err.Error())
return fmt.Errorf("decode database config failed: %w", err)
}
var dbConfig = config.DBs[Tea.Env]
db, err := dbs.NewInstanceFromConfig(dbConfig)
if err != nil {
return errors.New("load database failed: " + err.Error())
return fmt.Errorf("load database failed: %w", err)
}
defer func() {
_ = db.Close()
}()
one, err := db.FindOne("SELECT version FROM edgeVersions LIMIT 1")
if err != nil {
return errors.New("query version failed: " + err.Error())
return fmt.Errorf("query version failed: %w", err)
}
if one != nil {
// 如果是同样的版本,则直接认为是最新版本
@@ -384,7 +385,7 @@ func (this *APINode) autoUpgrade() error {
logs.Println("[API_NODE]upgrade database starting ...")
err = setup.NewSQLExecutor(dbConfig).Run(false)
if err != nil {
return errors.New("execute sql failed: " + err.Error())
return fmt.Errorf("execute sql failed: %w", err)
}
// 不使用remotelog
logs.Println("[API_NODE]upgrade database done")

View File

@@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
)
type HTTPCachePolicyService struct {
@@ -46,7 +48,26 @@ func (this *HTTPCachePolicyService) CreateHTTPCachePolicy(ctx context.Context, r
var tx = this.NullTx()
policyId, err := models.SharedHTTPCachePolicyDAO.CreateCachePolicy(tx, req.IsOn, req.Name, req.Description, req.CapacityJSON, req.MaxSizeJSON, req.Type, req.OptionsJSON, req.SyncCompressionCache)
if req.CapacityJSON != nil {
req.CapacityJSON, err = utils.JSONDecodeConfig(req.CapacityJSON, &shared.SizeCapacity{})
if err != nil {
return nil, err
}
}
if req.MaxSizeJSON != nil {
req.MaxSizeJSON, err = utils.JSONDecodeConfig(req.MaxSizeJSON, &shared.SizeCapacity{})
if err != nil {
return nil, err
}
}
if req.FetchTimeoutJSON != nil {
req.FetchTimeoutJSON, err = utils.JSONDecodeConfig(req.FetchTimeoutJSON, &shared.TimeDuration{})
if err != nil {
return nil, err
}
}
policyId, err := models.SharedHTTPCachePolicyDAO.CreateCachePolicy(tx, req.IsOn, req.Name, req.Description, req.CapacityJSON, req.MaxSizeJSON, req.Type, req.OptionsJSON, req.SyncCompressionCache, req.FetchTimeoutJSON)
if err != nil {
return nil, err
}
@@ -63,7 +84,26 @@ func (this *HTTPCachePolicyService) UpdateHTTPCachePolicy(ctx context.Context, r
var tx = this.NullTx()
err = models.SharedHTTPCachePolicyDAO.UpdateCachePolicy(tx, req.HttpCachePolicyId, req.IsOn, req.Name, req.Description, req.CapacityJSON, req.MaxSizeJSON, req.Type, req.OptionsJSON, req.SyncCompressionCache)
if req.CapacityJSON != nil {
req.CapacityJSON, err = utils.JSONDecodeConfig(req.CapacityJSON, &shared.SizeCapacity{})
if err != nil {
return nil, err
}
}
if req.MaxSizeJSON != nil {
req.MaxSizeJSON, err = utils.JSONDecodeConfig(req.MaxSizeJSON, &shared.SizeCapacity{})
if err != nil {
return nil, err
}
}
if req.FetchTimeoutJSON != nil {
req.FetchTimeoutJSON, err = utils.JSONDecodeConfig(req.FetchTimeoutJSON, &shared.TimeDuration{})
if err != nil {
return nil, err
}
}
err = models.SharedHTTPCachePolicyDAO.UpdateCachePolicy(tx, req.HttpCachePolicyId, req.IsOn, req.Name, req.Description, req.CapacityJSON, req.MaxSizeJSON, req.Type, req.OptionsJSON, req.SyncCompressionCache, req.FetchTimeoutJSON)
if err != nil {
return nil, err
}
@@ -147,7 +187,7 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicyConfig(ctx context
// FindEnabledHTTPCachePolicy 查找单个缓存策略信息
func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Context, req *pb.FindEnabledHTTPCachePolicyRequest) (*pb.FindEnabledHTTPCachePolicyResponse, error) {
_, err := this.ValidateAdmin(ctx)
_, _, err := this.ValidateAdminAndUser(ctx, false)
if err != nil {
return nil, err
}
@@ -162,9 +202,10 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Conte
return &pb.FindEnabledHTTPCachePolicyResponse{HttpCachePolicy: nil}, nil
}
return &pb.FindEnabledHTTPCachePolicyResponse{HttpCachePolicy: &pb.HTTPCachePolicy{
Id: int64(policy.Id),
Name: policy.Name,
IsOn: policy.IsOn,
Id: int64(policy.Id),
Name: policy.Name,
IsOn: policy.IsOn,
MaxBytesJSON: policy.MaxSize,
}}, nil
}

View File

@@ -97,16 +97,12 @@ func (this *HTTPCacheTaskKeyService) ValidateHTTPCacheTaskKeys(ctx context.Conte
}
var serverClusterId = int64(server.ClusterId)
if serverClusterId == 0 {
if clusterId > 0 {
serverClusterId = clusterId
} else {
pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
Key: key,
ReasonCode: "requireClusterId",
})
continue
}
if serverClusterId == 0 && clusterId <= 0 {
pbFailResults = append(pbFailResults, &pb.ValidateHTTPCacheTaskKeysResponse_FailKey{
Key: key,
ReasonCode: "requireClusterId",
})
continue
}
}

View File

@@ -177,7 +177,7 @@ func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallPolicy(ctx context.Cont
var tx = this.NullTx()
// 已经有的数据
firewallPolicy, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, nil)
firewallPolicy, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, false, nil)
if err != nil {
return nil, err
}
@@ -300,7 +300,12 @@ func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallPolicy(ctx context.Cont
}
}
err = models.SharedHTTPFirewallPolicyDAO.UpdateFirewallPolicy(tx, req.HttpFirewallPolicyId, req.IsOn, req.Name, req.Description, inboundConfigJSON, outboundConfigJSON, req.BlockOptionsJSON, req.CaptchaOptionsJSON, req.Mode, req.UseLocalFirewall, synFloodConfig, logConfig)
// MaxRequestBodySize
if req.MaxRequestBodySize < 0 {
req.MaxRequestBodySize = 0
}
err = models.SharedHTTPFirewallPolicyDAO.UpdateFirewallPolicy(tx, req.HttpFirewallPolicyId, req.IsOn, req.Name, req.Description, inboundConfigJSON, outboundConfigJSON, req.BlockOptionsJSON, req.CaptchaOptionsJSON, req.Mode, req.UseLocalFirewall, synFloodConfig, logConfig, req.MaxRequestBodySize, req.DenyCountryHTML, req.DenyProvinceHTML)
if err != nil {
return nil, err
}
@@ -443,7 +448,7 @@ func (this *HTTPFirewallPolicyService) FindEnabledHTTPFirewallPolicyConfig(ctx c
var tx = this.NullTx()
config, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, nil)
config, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, false, nil)
if err != nil {
return nil, err
}
@@ -507,7 +512,7 @@ func (this *HTTPFirewallPolicyService) ImportHTTPFirewallPolicy(ctx context.Cont
var tx = this.NullTx()
oldConfig, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, nil)
oldConfig, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, false, nil)
if err != nil {
return nil, err
}
@@ -670,7 +675,7 @@ func (this *HTTPFirewallPolicyService) CheckHTTPFirewallPolicyIPStatus(ctx conte
ipLong := utils.IP2Long(req.Ip)
var tx = this.NullTx()
firewallPolicy, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, nil)
firewallPolicy, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, false, nil)
if err != nil {
return nil, err
}

View File

@@ -101,7 +101,7 @@ func (this *HTTPFirewallRuleGroupService) FindEnabledHTTPFirewallRuleGroupConfig
var tx = this.NullTx()
groupConfig, err := models.SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, req.FirewallRuleGroupId)
groupConfig, err := models.SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, req.FirewallRuleGroupId, false)
if err != nil {
return nil, err
}
@@ -198,7 +198,7 @@ func (this *HTTPFirewallRuleGroupService) AddHTTPFirewallRuleGroupSet(ctx contex
var tx = this.NullTx()
// 已经有的规则
config, err := models.SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, req.FirewallRuleGroupId)
config, err := models.SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, req.FirewallRuleGroupId, false)
if err != nil {
return nil, err
}

View File

@@ -369,7 +369,7 @@ func (this *HTTPWebService) UpdateHTTPWebShutdown(ctx context.Context, req *pb.U
if len(shutdownConfig.URL) > maxURLLength {
return nil, errors.New("'url' too long")
}
if !regexputils.HTTPProtocol.MatchString(shutdownConfig.URL) {
if shutdownConfig.IsOn /** validate when it's on **/ && !regexputils.HTTPProtocol.MatchString(shutdownConfig.URL) {
return nil, errors.New("invalid 'url' format")
}
@@ -425,6 +425,9 @@ func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.Upda
if len(req.PagesJSON) > 0 {
var pages = []*serverconfigs.HTTPPageConfig{}
err = json.Unmarshal(req.PagesJSON, &pages)
if err != nil {
return nil, err
}
for _, page := range pages {
err = page.Init()

View File

@@ -34,7 +34,7 @@ type CommandRequestWaiting struct {
func (this *CommandRequestWaiting) Close() {
defer func() {
recover()
_ = recover()
}()
close(this.Chan)
@@ -207,7 +207,7 @@ func (this *NodeService) NodeStream(server pb.NodeService_NodeStreamServer) erro
func(req *pb.NodeStreamMessage) {
// 因为 responseChan.Chan 有被关闭的风险所以我们使用recover防止panic
defer func() {
recover()
_ = recover()
}()
nodeLocker.Lock()

View File

@@ -346,6 +346,9 @@ func (this *ServerService) CreateBasicHTTPServer(ctx context.Context, req *pb.Cr
Options: nil,
}
reverseProxyScheduleJSON, err := json.Marshal(reverseProxyScheduleConfig)
if err != nil {
return nil, err
}
var primaryOrigins = []*serverconfigs.OriginRef{}
for _, originAddr := range req.OriginAddrs {
@@ -615,6 +618,9 @@ func (this *ServerService) CreateBasicTCPServer(ctx context.Context, req *pb.Cre
Options: nil,
}
reverseProxyScheduleJSON, err := json.Marshal(reverseProxyScheduleConfig)
if err != nil {
return nil, err
}
var primaryOrigins = []*serverconfigs.OriginRef{}
for _, originAddr := range req.OriginAddrs {
@@ -2911,8 +2917,14 @@ func (this *ServerService) CopyServerConfig(ctx context.Context, req *pb.CopySer
if err != nil {
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)
if err != nil {

View File

@@ -177,7 +177,7 @@ func (this *ServerBandwidthStatService) FindServerBandwidthStats(ctx context.Con
req.Algo = bandwidthAlgo
}
var stats = []*models.ServerBandwidthStat{}
var stats []*models.ServerBandwidthStat
if len(req.Day) > 0 {
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day, req.Algo == systemconfigs.BandwidthAlgoAvg)
} else if len(req.Month) > 0 {
@@ -398,7 +398,7 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
return nil, errors.New("invalid dayTo '" + req.DayTo + "'")
}
var pbStats = []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{}
var pbStats []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
var pbNthStat *pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
if req.ServerId > 0 { // 服务统计
pbStats, err = models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)

View File

@@ -2,6 +2,7 @@ package rpcutils
import (
"errors"
"fmt"
)
type UserType = string
@@ -27,5 +28,5 @@ func Wrap(description string, err error) error {
if err == nil {
return errors.New(description)
}
return errors.New(description + ": " + err.Error())
return fmt.Errorf("%s: %w", description, err)
}

View File

@@ -2,6 +2,7 @@ package setup
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/configs"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
@@ -150,7 +151,7 @@ func (this *Setup) Run() error {
}
addrsJSON, err := json.Marshal([]*serverconfigs.NetworkAddressConfig{addr})
if err != nil {
return errors.New("json encode api node addr failed: " + err.Error())
return fmt.Errorf("json encode api node addr failed: %w", err)
}
var httpJSON []byte = nil
@@ -166,7 +167,7 @@ func (this *Setup) Run() error {
}
httpJSON, err = json.Marshal(httpConfig)
if err != nil {
return errors.New("json encode api node http config failed: " + err.Error())
return fmt.Errorf("json encode api node http config failed: %w", err)
}
}
if this.config.APINodeProtocol == "https" {
@@ -181,14 +182,14 @@ func (this *Setup) Run() error {
}
httpsJSON, err = json.Marshal(httpsConfig)
if err != nil {
return errors.New("json encode api node https config failed: " + err.Error())
return fmt.Errorf("json encode api node https config failed: %w", err)
}
}
// 创建API节点
nodeId, err := dao.CreateAPINode(nil, "默认API节点", "这是默认创建的第一个API节点", httpJSON, httpsJSON, false, nil, nil, addrsJSON, true)
if err != nil {
return errors.New("create api node in database failed: " + err.Error())
return fmt.Errorf("create api node in database failed: %w", err)
}
apiNodeId = nodeId
}
@@ -208,7 +209,7 @@ func (this *Setup) Run() error {
}
err = apiConfig.WriteFile(Tea.ConfigFile("api.yaml"))
if err != nil {
return errors.New("save config failed: " + err.Error())
return fmt.Errorf("save config failed: %w", err)
}
return nil

View File

@@ -87506,7 +87506,7 @@
"name": "edgeDNSTasks",
"engine": "InnoDB",
"charset": "utf8mb4_general_ci",
"definition": "CREATE TABLE `edgeDNSTasks` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `clusterId` int(11) unsigned DEFAULT '0' COMMENT '集群ID',\n `serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordName` varchar(255) DEFAULT NULL COMMENT '记录名',\n `type` varchar(255) DEFAULT NULL COMMENT '任务类型',\n `updatedAt` bigint(11) unsigned DEFAULT '0' COMMENT '更新时间',\n `isDone` tinyint(1) unsigned DEFAULT '0' COMMENT '是否已完成',\n `isOk` tinyint(1) unsigned DEFAULT '0' COMMENT '是否成功',\n `error` varchar(1024) DEFAULT NULL COMMENT '错误信息',\n `version` bigint(20) unsigned DEFAULT '0' COMMENT '版本',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniqueId` (`clusterId`,`serverId`,`nodeId`,`domainId`,`recordName`,`type`) USING BTREE,\n KEY `isDone` (`isDone`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='DNS更新任务'",
"definition": "CREATE TABLE `edgeDNSTasks` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `clusterId` int(11) unsigned DEFAULT '0' COMMENT '集群ID',\n `serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordName` varchar(255) DEFAULT NULL COMMENT '记录名',\n `type` varchar(255) DEFAULT NULL COMMENT '任务类型',\n `updatedAt` bigint(11) unsigned DEFAULT '0' COMMENT '更新时间',\n `isDone` tinyint(1) unsigned DEFAULT '0' COMMENT '是否已完成',\n `isOk` tinyint(1) unsigned DEFAULT '0' COMMENT '是否成功',\n `error` varchar(1024) DEFAULT NULL COMMENT '错误信息',\n `version` bigint(20) unsigned DEFAULT '0' COMMENT '版本',\n `countFails` int(11) unsigned DEFAULT '0' COMMENT '尝试失败次数',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniqueId` (`clusterId`,`serverId`,`nodeId`,`domainId`,`recordName`,`type`) USING BTREE,\n KEY `isDone` (`isDone`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='DNS更新任务'",
"fields": [
{
"name": "id",
@@ -87555,6 +87555,10 @@
{
"name": "version",
"definition": "bigint(20) unsigned DEFAULT '0' COMMENT '版本'"
},
{
"name": "countFails",
"definition": "int(11) unsigned DEFAULT '0' COMMENT '尝试失败次数'"
}
],
"indexes": [
@@ -91371,7 +91375,7 @@
"name": "edgeHTTPCachePolicies",
"engine": "InnoDB",
"charset": "utf8mb4_general_ci",
"definition": "CREATE TABLE `edgeHTTPCachePolicies` (\n `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `adminId` int(11) unsigned DEFAULT '0' COMMENT '管理员ID',\n `userId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n `templateId` int(11) unsigned DEFAULT '0' COMMENT '模版ID',\n `isOn` tinyint(1) unsigned DEFAULT '1' COMMENT '是否启用',\n `name` varchar(255) DEFAULT NULL COMMENT '名称',\n `capacity` json DEFAULT NULL COMMENT '容量数据',\n `maxKeys` bigint(20) unsigned DEFAULT '0' COMMENT '最多Key值',\n `maxSize` json DEFAULT NULL COMMENT '最大缓存内容尺寸',\n `type` varchar(255) DEFAULT NULL COMMENT '存储类型',\n `options` json DEFAULT NULL COMMENT '存储选项',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `state` tinyint(1) unsigned DEFAULT '1' COMMENT '状态',\n `description` varchar(1024) DEFAULT NULL COMMENT '描述',\n `refs` json DEFAULT NULL COMMENT '默认的缓存设置',\n `syncCompressionCache` tinyint(1) unsigned DEFAULT '0' COMMENT '是否同步写入压缩缓存',\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='HTTP缓存策略'",
"definition": "CREATE TABLE `edgeHTTPCachePolicies` (\n `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `adminId` int(11) unsigned DEFAULT '0' COMMENT '管理员ID',\n `userId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n `templateId` int(11) unsigned DEFAULT '0' COMMENT '模版ID',\n `isOn` tinyint(1) unsigned DEFAULT '1' COMMENT '是否启用',\n `name` varchar(255) DEFAULT NULL COMMENT '名称',\n `capacity` json DEFAULT NULL COMMENT '容量数据',\n `maxKeys` bigint(20) unsigned DEFAULT '0' COMMENT '最多Key值',\n `maxSize` json DEFAULT NULL COMMENT '最大缓存内容尺寸',\n `type` varchar(255) DEFAULT NULL COMMENT '存储类型',\n `options` json DEFAULT NULL COMMENT '存储选项',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `state` tinyint(1) unsigned DEFAULT '1' COMMENT '状态',\n `description` varchar(1024) DEFAULT NULL COMMENT '描述',\n `refs` json DEFAULT NULL COMMENT '默认的缓存设置',\n `syncCompressionCache` tinyint(1) unsigned DEFAULT '0' COMMENT '是否同步写入压缩缓存',\n `fetchTimeout` json DEFAULT NULL COMMENT '预热超时时间',\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='HTTP缓存策略'",
"fields": [
{
"name": "id",
@@ -91436,6 +91440,10 @@
{
"name": "syncCompressionCache",
"definition": "tinyint(1) unsigned DEFAULT '0' COMMENT '是否同步写入压缩缓存'"
},
{
"name": "fetchTimeout",
"definition": "json COMMENT '预热超时时间'"
}
],
"indexes": [
@@ -91702,7 +91710,7 @@
"name": "edgeHTTPFirewallPolicies",
"engine": "InnoDB",
"charset": "utf8mb4_general_ci",
"definition": "CREATE TABLE `edgeHTTPFirewallPolicies` (\n `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `templateId` int(11) unsigned DEFAULT '0' COMMENT '模版ID',\n `adminId` int(11) unsigned DEFAULT '0' COMMENT '管理员ID',\n `userId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n `serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',\n `groupId` int(11) unsigned DEFAULT '0' COMMENT '服务分组ID',\n `state` tinyint(1) unsigned DEFAULT '1' COMMENT '状态',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `isOn` tinyint(1) unsigned DEFAULT '1' COMMENT '是否启用',\n `name` varchar(255) DEFAULT NULL COMMENT '名称',\n `description` varchar(1024) DEFAULT NULL COMMENT '描述',\n `inbound` json DEFAULT NULL COMMENT '入站规则',\n `outbound` json DEFAULT NULL COMMENT '出站规则',\n `blockOptions` json DEFAULT NULL COMMENT 'BLOCK选项',\n `captchaOptions` json DEFAULT NULL COMMENT '验证码选项',\n `mode` varchar(32) DEFAULT 'defend' COMMENT '模式',\n `useLocalFirewall` tinyint(1) unsigned DEFAULT '1' COMMENT '是否自动使用本地防火墙',\n `synFlood` json DEFAULT NULL COMMENT 'SynFlood防御设置',\n `log` json DEFAULT NULL COMMENT '日志配置',\n PRIMARY KEY (`id`),\n KEY `userId` (`userId`),\n KEY `serverId` (`serverId`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='HTTP防火墙'",
"definition": "CREATE TABLE `edgeHTTPFirewallPolicies` (\n `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `templateId` int(11) unsigned DEFAULT '0' COMMENT '模版ID',\n `adminId` int(11) unsigned DEFAULT '0' COMMENT '管理员ID',\n `userId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n `serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',\n `groupId` int(11) unsigned DEFAULT '0' COMMENT '服务分组ID',\n `state` tinyint(1) unsigned DEFAULT '1' COMMENT '状态',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `isOn` tinyint(1) unsigned DEFAULT '1' COMMENT '是否启用',\n `name` varchar(255) DEFAULT NULL COMMENT '名称',\n `description` varchar(1024) DEFAULT NULL COMMENT '描述',\n `inbound` json DEFAULT NULL COMMENT '入站规则',\n `outbound` json DEFAULT NULL COMMENT '出站规则',\n `blockOptions` json DEFAULT NULL COMMENT 'BLOCK选项',\n `captchaOptions` json DEFAULT NULL COMMENT '验证码选项',\n `mode` varchar(32) DEFAULT 'defend' COMMENT '模式',\n `useLocalFirewall` tinyint(1) unsigned DEFAULT '1' COMMENT '是否自动使用本地防火墙',\n `synFlood` json DEFAULT NULL COMMENT 'SynFlood防御设置',\n `log` json DEFAULT NULL COMMENT '日志配置',\n `maxRequestBodySize` int(11) unsigned DEFAULT '0' COMMENT '可以检查的最大请求内容尺寸',\n `denyCountryHTML` text COMMENT '区域封禁提示',\n `denyProvinceHTML` text COMMENT '省份封禁提示',\n PRIMARY KEY (`id`),\n KEY `userId` (`userId`),\n KEY `serverId` (`serverId`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='HTTP防火墙'",
"fields": [
{
"name": "id",
@@ -91779,6 +91787,18 @@
{
"name": "log",
"definition": "json COMMENT '日志配置'"
},
{
"name": "maxRequestBodySize",
"definition": "int(11) unsigned DEFAULT '0' COMMENT '可以检查的最大请求内容尺寸'"
},
{
"name": "denyCountryHTML",
"definition": "text COMMENT '区域封禁提示'"
},
{
"name": "denyProvinceHTML",
"definition": "text COMMENT '省份封禁提示'"
}
],
"indexes": [

View File

@@ -2,8 +2,8 @@ package setup
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
@@ -68,7 +68,7 @@ func (this *SQLExecutor) Run(showLog bool) error {
var sqlResult = &SQLDumpResult{}
err = json.Unmarshal(sqlData, sqlResult)
if err != nil {
return errors.New("decode sql data failed: " + err.Error())
return fmt.Errorf("decode sql data failed: %w", err)
}
_, err = sqlDump.Apply(db, sqlResult, showLog)
@@ -227,7 +227,7 @@ func (this *SQLExecutor) checkCluster(db *dbs.DB) error {
/// 检查是否有集群数字
stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeNodeClusters")
if err != nil {
return errors.New("query clusters failed: " + err.Error())
return fmt.Errorf("query clusters failed: %w", err)
}
defer func() {
_ = stmt.Close()
@@ -235,7 +235,7 @@ func (this *SQLExecutor) checkCluster(db *dbs.DB) error {
col, err := stmt.FindCol(0)
if err != nil {
return errors.New("query clusters failed: " + err.Error())
return fmt.Errorf("query clusters failed: %w", err)
}
count := types.Int(col)
if count > 0 {
@@ -311,7 +311,7 @@ func (this *SQLExecutor) checkCluster(db *dbs.DB) error {
func (this *SQLExecutor) checkIPList(db *dbs.DB) error {
stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeIPLists")
if err != nil {
return errors.New("query ip lists failed: " + err.Error())
return fmt.Errorf("query ip lists failed: %w", err)
}
defer func() {
_ = stmt.Close()
@@ -319,7 +319,7 @@ func (this *SQLExecutor) checkIPList(db *dbs.DB) error {
col, err := stmt.FindCol(0)
if err != nil {
return errors.New("query ip lists failed: " + err.Error())
return fmt.Errorf("query ip lists failed: %w", err)
}
count := types.Int(col)
if count > 0 {
@@ -508,7 +508,7 @@ func (this *SQLExecutor) checkClientAgents(db *dbs.DB) error {
func (this *SQLExecutor) updateVersion(db *dbs.DB, version string) error {
stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeVersions")
if err != nil {
return errors.New("query version failed: " + err.Error())
return fmt.Errorf("query version failed: %w", err)
}
defer func() {
_ = stmt.Close()
@@ -516,20 +516,20 @@ func (this *SQLExecutor) updateVersion(db *dbs.DB, version string) error {
col, err := stmt.FindCol(0)
if err != nil {
return errors.New("query version failed: " + err.Error())
return fmt.Errorf("query version failed: %w", err)
}
count := types.Int(col)
if count > 0 {
_, err = db.Exec("UPDATE edgeVersions SET version=?", version)
if err != nil {
return errors.New("update version failed: " + err.Error())
return fmt.Errorf("update version failed: %w", err)
}
return nil
}
_, err = db.Exec("INSERT edgeVersions (version) VALUES (?)", version)
if err != nil {
return errors.New("create version failed: " + err.Error())
return fmt.Errorf("create version failed: %w", err)
}
return nil

View File

@@ -424,9 +424,7 @@ func upgradeV0_3_2(db *dbs.DB) error {
if err != nil {
continue
}
if sizeCapacity != nil {
compressionConfig.MinLength = sizeCapacity
}
compressionConfig.MinLength = sizeCapacity
}
var maxLengthBytes = []byte(gzipOne.GetString("maxLength"))
@@ -436,9 +434,7 @@ func upgradeV0_3_2(db *dbs.DB) error {
if err != nil {
continue
}
if sizeCapacity != nil {
compressionConfig.MaxLength = sizeCapacity
}
compressionConfig.MaxLength = sizeCapacity
}
var condsBytes = []byte(gzipOne.GetString("conds"))
@@ -448,9 +444,7 @@ func upgradeV0_3_2(db *dbs.DB) error {
if err != nil {
continue
}
if conds != nil {
compressionConfig.Conds = conds
}
compressionConfig.Conds = conds
}
configJSON, err := json.Marshal(compressionConfig)

View File

@@ -5,7 +5,7 @@ package setup
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
@@ -79,7 +79,7 @@ func upgradeV0_4_9(db *dbs.DB) error {
config.DenySpiders = true
configJSON, err := json.Marshal(config)
if err != nil {
return errors.New("encode SecurityConfig failed: " + err.Error())
return fmt.Errorf("encode SecurityConfig failed: %w", err)
} else {
_, err := db.Exec("UPDATE edgeSysSettings SET value=? WHERE code=?", configJSON, systemconfigs.SettingCodeAdminSecurityConfig)
if err != nil {

View File

@@ -177,7 +177,7 @@ func (this *HealthCheckExecutor) runNode(healthCheckConfig *serverconfigs.Health
// 在线状态发生变化
if healthCheckConfig.AutoDown {
// 发送消息
var message = ""
var message string
var messageType string
var messageLevel string
if result.IsOk {

View File

@@ -8,6 +8,7 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
@@ -63,7 +64,7 @@ func (this *SSLCertUpdateOCSPTask) Loop() error {
var maxTries = 5
certs, err := models.SharedSSLCertDAO.ListCertsToUpdateOCSP(tx, maxTries, size)
if err != nil {
return errors.New("list certs failed: " + err.Error())
return fmt.Errorf("list certs failed: %w", err)
}
if len(certs) == 0 {
@@ -74,7 +75,7 @@ func (this *SSLCertUpdateOCSPTask) Loop() error {
for _, cert := range certs {
err := models.SharedSSLCertDAO.PrepareCertOCSPUpdating(tx, int64(cert.Id))
if err != nil {
return errors.New("prepare cert ocsp updating failed: " + err.Error())
return fmt.Errorf("prepare cert ocsp updating failed: %w", err)
}
}
@@ -90,7 +91,7 @@ func (this *SSLCertUpdateOCSPTask) Loop() error {
}
err = models.SharedSSLCertDAO.UpdateCertOCSP(tx, int64(cert.Id), ocspData, expiresAt, hasErr, errString)
if err != nil {
return errors.New("update ocsp failed: " + err.Error())
return fmt.Errorf("update ocsp failed: %w", err)
}
}

View File

@@ -122,7 +122,7 @@ func TestList_ManyItems(t *testing.T) {
})
list.GC(time.Now().Unix() + 1)
t.Log("gc", count, "items")
t.Log(time.Now().Sub(now))
t.Log(time.Since(now))
}
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++ {
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++ {
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++ {
delete(m, uint32(i))
}
t.Log(time.Now().Sub(now))
t.Log(time.Since(now))
}
}

View File

@@ -43,3 +43,32 @@ func JSONClone[T any](ptr T) (newPtr T, err error) {
return newValue.(T), nil
}
// JSONDecodeConfig 解码并重新编码
// 是为了去除原有JSON中不需要的数据
func JSONDecodeConfig(data []byte, ptr any) (encodeJSON []byte, err error) {
err = json.Unmarshal(data, ptr)
if err != nil {
return
}
encodeJSON, err = json.Marshal(ptr)
if err != nil {
return
}
// validate config
if ptr != nil {
config, ok := ptr.(interface {
Init() error
})
if ok {
initErr := config.Init()
if initErr != nil {
err = errors.New("validate config failed: " + initErr.Error())
}
}
}
return
}

View File

@@ -3,6 +3,7 @@
package utils_test
import (
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/logs"
"testing"
@@ -49,3 +50,38 @@ func TestJSONClone_Slice(t *testing.T) {
}
logs.PrintAsJSON(newU, t)
}
type jsonUserType struct {
Name string `json:"name"`
Age int `json:"age"`
}
func (this *jsonUserType) Init() error {
if len(this.Name) < 10 {
return errors.New("'name' too short")
}
return nil
}
func TestJSONDecodeConfig(t *testing.T) {
var data = []byte(`{ "name":"Lily", "age":20, "description": "Nice" }`)
var u = &jsonUserType{}
newJSON, err := utils.JSONDecodeConfig(data, u)
if err != nil {
t.Fatal(err)
}
t.Logf("%+v, %s", u, string(newJSON))
}
func TestJSONDecodeConfig_Validate(t *testing.T) {
var data = []byte(`{ "name":"Lily", "age":20, "description": "Nice" }`)
var u = &jsonUserType{}
newJSON, err := utils.JSONDecodeConfig(data, u)
if err != nil {
t.Log("ignore error:", err) // error expected
}
t.Logf("%+v, %s", u, string(newJSON))
}

View File

@@ -25,5 +25,5 @@ func SetRLimit(limit uint64) error {
// set best resource limit value
func SetSuitableRLimit() {
SetRLimit(4096 * 100) // 1M=100Files
_ = SetRLimit(4096 * 100) // 1M=100Files
}

View File

@@ -39,7 +39,7 @@ func (this *ServiceManager) setup() {
this.onceLocker.Do(func() {
logFile := files.NewFile(Tea.Root + "/logs/service.log")
if logFile.Exists() {
logFile.Delete()
_ = logFile.Delete()
}
//logger

View File

@@ -1,3 +1,4 @@
//go:build linux
// +build linux
package utils
@@ -47,7 +48,7 @@ func (this *ServiceManager) Start() error {
return exec.Command("service", teaconst.ProcessName, "start").Start()
}
// 删除服务
// Uninstall 删除服务
func (this *ServiceManager) Uninstall() error {
if os.Getgid() != 0 {
return errors.New("only root users can uninstall the service")
@@ -60,10 +61,10 @@ func (this *ServiceManager) Uninstall() error {
}
// disable service
exec.Command(systemd, "disable", teaconst.SystemdServiceName+".service").Start()
_ = exec.Command(systemd, "disable", teaconst.SystemdServiceName+".service").Start()
// reload
exec.Command(systemd, "daemon-reload")
_ = exec.Command(systemd, "daemon-reload").Start()
return files.NewFile(systemdServiceFile).Delete()
}
@@ -144,10 +145,10 @@ WantedBy=multi-user.target`
}
// stop current systemd service if running
exec.Command(systemd, "stop", shortName+".service")
_ = exec.Command(systemd, "stop", shortName+".service").Start()
// reload
exec.Command(systemd, "daemon-reload")
_ = exec.Command(systemd, "daemon-reload").Start()
// enable
cmd := exec.Command(systemd, "enable", shortName+".service")

View File

@@ -1,3 +1,4 @@
//go:build windows
// +build windows
package utils
@@ -16,7 +17,7 @@ import (
func (this *ServiceManager) Install(exePath string, args []string) error {
m, err := mgr.Connect()
if err != nil {
return fmt.Errorf("connecting: %s please 'Run as administrator' again", err.Error())
return fmt.Errorf("connecting: %w please 'Run as administrator' again", err)
}
defer m.Disconnect()
s, err := m.OpenService(this.Name)
@@ -31,7 +32,7 @@ func (this *ServiceManager) Install(exePath string, args []string) error {
StartType: windows.SERVICE_AUTO_START,
}, args...)
if err != nil {
return fmt.Errorf("creating: %s", err.Error())
return fmt.Errorf("creating: %w", err)
}
defer s.Close()
@@ -47,12 +48,12 @@ func (this *ServiceManager) Start() error {
defer m.Disconnect()
s, err := m.OpenService(this.Name)
if err != nil {
return fmt.Errorf("could not access service: %v", err)
return fmt.Errorf("could not access service: %w", err)
}
defer s.Close()
err = s.Start("service")
if err != nil {
return fmt.Errorf("could not start service: %v", err)
return fmt.Errorf("could not start service: %w", err)
}
return nil
@@ -62,12 +63,12 @@ func (this *ServiceManager) Start() error {
func (this *ServiceManager) Uninstall() error {
m, err := mgr.Connect()
if err != nil {
return fmt.Errorf("connecting: %s please 'Run as administrator' again", err.Error())
return fmt.Errorf("connecting: %w please 'Run as administrator' again", err)
}
defer m.Disconnect()
s, err := m.OpenService(this.Name)
if err != nil {
return fmt.Errorf("open service: %s", err.Error())
return fmt.Errorf("open service: %w", err)
}
// shutdown service
@@ -79,7 +80,7 @@ func (this *ServiceManager) Uninstall() error {
defer s.Close()
err = s.Delete()
if err != nil {
return fmt.Errorf("deleting: %s", err.Error())
return fmt.Errorf("deleting: %w", err)
}
return nil
}

View File

@@ -10,8 +10,10 @@ var SharedCache = NewCache()
// Cache TTL缓存
// 最大的缓存时间为30 * 86400
// Piece数据结构
// Piece1 | Piece2 | Piece3 | ...
// [ Item1, Item2, ... ] | ...
//
// Piece1 | Piece2 | Piece3 | ...
// [ Item1, Item2, ... ] | ...
//
// KeyMap列表数据结构
// { timestamp1 => [key1, key2, ...] }, ...
type Cache struct {
@@ -115,19 +117,11 @@ func (this *Cache) Read(key string) (item *Item) {
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) {
uint64Key := HashKey([]byte(key))
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) {
for _, piece := range this.pieces {
count += piece.Count()