Compare commits

...

68 Commits

Author SHA1 Message Date
刘祥超
f63c2d867d 修复流量控制API可能产生的JSON解析错误 2021-11-22 10:39:29 +08:00
刘祥超
eefb497c20 修复一个拼写错误:默认安装的节点从https改成http 2021-11-21 19:28:05 +08:00
刘祥超
1c8564d817 启动时增加查询是否正在启动指令 2021-11-21 19:27:27 +08:00
刘祥超
1359997f48 优化访问日志的requestId 2021-11-21 15:56:13 +08:00
刘祥超
4a52d43273 增加批量删除IP名单中IP的API 2021-11-21 09:42:57 +08:00
刘祥超
fb68de23c1 IP名单查看所有IP时不显示过期IP/节点同步IP名单时将过期Item设置为已删除 2021-11-21 08:43:26 +08:00
刘祥超
c1b7cf11c3 优化边缘节点在线状态管理 2021-11-20 18:59:35 +08:00
刘祥超
311751596c 监控数据只保留7天(先前是100天) 2021-11-20 14:35:34 +08:00
刘祥超
4132dee4bc 修复用户查询证书时返回其他证书的Bug 2021-11-18 16:44:41 +08:00
刘祥超
8e886cd410 更新SQL 2021-11-18 16:43:43 +08:00
刘祥超
d6f896298d 节点IP阈值增加节点健康检查失败 2021-11-18 14:30:53 +08:00
刘祥超
2ca8aa4b44 开源版本不运行sql.sh 2021-11-18 08:56:13 +08:00
刘祥超
783052a0b4 IP名单增加是否只显示自动拦截名单选项 2021-11-17 20:25:36 +08:00
刘祥超
5bda9e4b51 IP名单默认改为非全局 2021-11-17 20:00:34 +08:00
刘祥超
f7cbf051bd 增加全局查看、检索IP功能 2021-11-17 19:51:00 +08:00
刘祥超
4d7e82d0a2 IP名单增加是否全局 2021-11-17 16:14:55 +08:00
刘祥超
bf2ffeb15c 删除WAF策略和删除服务时同时也删除关联的IP名单 2021-11-16 17:50:52 +08:00
刘祥超
7b3290800d 更新SQL 2021-11-16 16:11:55 +08:00
刘祥超
eaebb6df5a IP名单中IP创建时保存相关节点、服务、WAF策略信息 2021-11-16 16:10:48 +08:00
刘祥超
acdddf5e12 API取消对节点时钟的检查 2021-11-16 09:02:55 +08:00
刘祥超
1224758b5b IP名单API增加IP添加时间 2021-11-15 11:31:27 +08:00
刘祥超
5b32343a2c 优化节点配置生成速度 2021-11-11 14:16:42 +08:00
刘祥超
8afd21f2f4 节点获取配置时进行压缩传输,至少减少80%的传输带宽 2021-11-11 09:06:44 +08:00
刘祥超
3e76bf6e5a 自动生成账单,自动支付账单 2021-11-11 08:30:53 +08:00
刘祥超
ed74adebac 改进流量限制 2021-11-10 14:54:27 +08:00
刘祥超
1400dff676 将带宽限制改为流量限制 2021-11-09 17:36:54 +08:00
刘祥超
8e3360b368 支持套餐相关操作 2021-11-09 15:36:25 +08:00
刘祥超
e80441639c 支持购买套餐/续费套餐/用户账户操作等 2021-11-08 20:52:15 +08:00
刘祥超
07bf994b68 增加删除/恢复DNS域名API 2021-11-06 16:23:12 +08:00
刘祥超
344ef4d597 SSH登录支持Passphrase 2021-11-06 15:31:01 +08:00
刘祥超
144b9b9519 增加多个API/规范命名 2021-11-05 17:56:35 +08:00
刘祥超
38841d6d75 规范命名 2021-11-05 15:36:45 +08:00
刘祥超
6d1603e253 增加APINodeService.CountAllEnabledAndOnAPINodes() 2021-11-05 15:35:22 +08:00
刘祥超
6ce23c9913 修复服务列表无法使用数字搜索的Bug 2021-11-04 18:48:07 +08:00
刘祥超
fd8c9adbf7 修改版本号为0.3.5 2021-11-04 18:47:53 +08:00
刘祥超
ea40dc7534 支持info指令查询PID、版本号等信息 2021-11-04 11:15:22 +08:00
刘祥超
37c8a8c955 升级到v0.3.3时删除7天以前的节点运行日志(info级别) 2021-11-01 16:03:40 +08:00
刘祥超
2a0e4bba4b 优化节点运行日志清理时间 2021-11-01 15:58:58 +08:00
刘祥超
dd252b7b87 修复db.yaml自动被还原的Bug 2021-11-01 15:31:59 +08:00
刘祥超
2de63863a7 修改版本为0.3.4 2021-11-01 10:46:00 +08:00
刘祥超
5d00d53ea5 增加ACME错误信息字段长度 2021-10-30 15:47:59 +08:00
刘祥超
dcf04d64bd 更新SQL 2021-10-29 14:06:12 +08:00
刘祥超
d36f7d01df 增加套餐相关代码 2021-10-29 14:02:40 +08:00
刘祥超
a0ff24adb6 增加在IP名单中使用ipFrom和ipTo查找IP的API 2021-10-26 09:17:33 +08:00
刘祥超
5597a0af6c 创建WAF分组时也记录代号 2021-10-25 19:02:20 +08:00
刘祥超
c196a85a59 增加为WAF分组添加规则集的API 2021-10-25 12:01:16 +08:00
刘祥超
ba638d4e1d 优化代码 2021-10-22 13:40:04 +08:00
刘祥超
7f8abccd2a 优化代码 2021-10-22 13:38:18 +08:00
刘祥超
c0f540cc2c 更新SQL 2021-10-22 12:40:30 +08:00
刘祥超
bd905ff1a9 可以在IP名单中搜索IP 2021-10-22 12:18:53 +08:00
刘祥超
3b8d1b4cd8 实现单个服务的带宽限制(商业版) 2021-10-21 17:10:53 +08:00
刘祥超
f86180b93c 将HTTP Header中Edge-改成X-Edge- 2021-10-19 19:49:06 +08:00
刘祥超
cfb1864fd2 健康检查支持UserAgent和是否基础请求设置 2021-10-19 16:31:05 +08:00
刘祥超
1d0a66a156 上传SQL 2021-10-18 09:09:32 +08:00
刘祥超
b4d4f6460e 增加PURGE某个URL缓存功能 2021-10-17 20:22:14 +08:00
刘祥超
e2de9799c0 增加清除服务缓存API 2021-10-17 17:12:30 +08:00
刘祥超
9b9c6471f7 增加支持服务CNAME选项/提供重新生成服务CNAME API 2021-10-16 12:02:42 +08:00
刘祥超
d30b10baee 只讲错误级别的节点运行日志设置为未读 2021-10-16 10:26:47 +08:00
刘祥超
55f7189a1c 运行日志显示未读的日志数量 2021-10-15 12:54:31 +08:00
刘祥超
0ff6fb002d 域名小时统计只保留7天 2021-10-15 10:16:14 +08:00
刘祥超
a5d34565c5 节点日志增加是否已读标记 2021-10-14 17:29:54 +08:00
刘祥超
3c17ba0a8b 修复华为云DNS TXT记录值不加引号无法添加的问题 2021-10-13 17:53:14 +08:00
刘祥超
a8c0c64071 更新SQL 2021-10-13 10:12:10 +08:00
刘祥超
82ed22a464 支持PROXY Protocol 2021-10-12 20:18:35 +08:00
刘祥超
b190479d44 集群API中增加时区信息 2021-10-12 14:40:24 +08:00
刘祥超
ed7b586137 可以在集群中指定节点时区 2021-10-12 11:44:24 +08:00
刘祥超
ea19635fe5 修复同属多集群下的节点无法删除线路的Bug 2021-10-12 08:40:39 +08:00
刘祥超
29cd7da6e4 版本号改为0.3.3 2021-10-12 08:38:00 +08:00
150 changed files with 4736 additions and 644 deletions

View File

@@ -76,8 +76,10 @@ function build() {
fi
# build sql
echo "building sql ..."
${ROOT}/sql.sh
if [ $TAG = "plus" ]; then
echo "building sql ..."
${ROOT}/sql.sh
fi
# copy files
echo "copying ..."

6
go.mod
View File

@@ -4,20 +4,20 @@ go 1.15
replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/TeaOSLab/EdgeCommon v0.0.0-00010101000000-000000000000
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183
github.com/andybalholm/brotli v1.0.4
github.com/cespare/xxhash/v2 v2.1.1
github.com/go-acme/lego/v4 v4.5.2
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/golang/protobuf v1.5.2
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3
github.com/json-iterator/go v1.1.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
github.com/mozillazg/go-pinyin v0.18.0
github.com/pkg/sftp v1.12.0

8
go.sum
View File

@@ -50,6 +50,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183 h1:dkj8/dxOQ4L1XpwCzRLqukvUBbxuNdz3FeyvHFnRjmo=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1183/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
@@ -232,6 +234,8 @@ github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7Q
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13 h1:HuEJ5xJfujW1Q6rNDhOu5LQXEBB2qLPah3jYslT8Gz4=
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24 h1:1cGulkD2SNJJRok5OKwyhP/Ddm+PgSWKOupn0cR36/A=
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
@@ -248,6 +252,8 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
@@ -318,6 +324,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mozillazg/go-pinyin v0.18.0 h1:hQompXO23/0ohH8YNjvfsAITnCQImCiR/Fny8EhIeW0=
github.com/mozillazg/go-pinyin v0.18.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

View File

@@ -13,9 +13,7 @@ import (
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
)
@@ -57,14 +55,12 @@ func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
return nil
}
var requestId int64 = 1_0000_0000_0000_0000
bulk := &strings.Builder{}
indexName := this.FormatVariables(this.config.Index)
typeName := this.FormatVariables(this.config.MappingType)
for _, accessLog := range accessLogs {
if len(accessLog.RequestId) == 0 {
accessLog.RequestId = strconv.FormatInt(time.Now().UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + fmt.Sprintf("%08d", 1)
continue
}
opData, err := json.Marshal(map[string]interface{}{

View File

@@ -1,3 +1,5 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package accesslogs
import (
@@ -5,7 +7,6 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/lists"
@@ -49,23 +50,6 @@ func (this *StorageManager) Start() {
}
}
// 写入日志
func (this *StorageManager) Write(policyId int64, accessLogs []*pb.HTTPAccessLog) error {
this.locker.Lock()
storage, ok := this.storageMap[policyId]
this.locker.Unlock()
if !ok {
return nil
}
if !storage.IsOk() {
return nil
}
return storage.Write(accessLogs)
}
// Loop 更新
func (this *StorageManager) Loop() error {
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAndOnPolicies(nil)

View File

@@ -0,0 +1,15 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build community
// +build community
package accesslogs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// 写入日志
func (this *StorageManager) Write(policyId int64, accessLogs []*pb.HTTPAccessLog) error {
return nil
}

View File

@@ -1,7 +1,6 @@
package configs
import (
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/go-yaml/yaml"
"github.com/iwind/TeaGo/Tea"
@@ -11,9 +10,8 @@ import (
)
var sharedAPIConfig *APIConfig = nil
var PaddingId string
// API节点配置
// APIConfig API节点配置
type APIConfig struct {
NodeId string `yaml:"nodeId" json:"nodeId"`
Secret string `yaml:"secret" json:"secret"`
@@ -21,7 +19,7 @@ type APIConfig struct {
numberId int64 // 数字ID
}
// 获取共享配置
// SharedAPIConfig 获取共享配置
func SharedAPIConfig() (*APIConfig, error) {
sharedLocker.Lock()
defer sharedLocker.Unlock()
@@ -72,7 +70,7 @@ func SharedAPIConfig() (*APIConfig, error) {
{
dbConfigFile := Tea.ConfigFile("db.yaml")
_, err := os.Stat(dbConfigFile)
if err == nil {
if err != nil {
paths := []string{}
homeDir, homeErr := os.UserHomeDir()
if homeErr == nil {
@@ -96,18 +94,18 @@ func SharedAPIConfig() (*APIConfig, error) {
return config, nil
}
// 设置数字ID
// SetNumberId 设置数字ID
func (this *APIConfig) SetNumberId(numberId int64) {
this.numberId = numberId
PaddingId = fmt.Sprintf("%08d", numberId)
teaconst.NodeId = numberId
}
// 获取数字ID
// NumberId 获取数字ID
func (this *APIConfig) NumberId() int64 {
return this.numberId
}
// 保存到文件
// WriteFile 保存到文件
func (this *APIConfig) WriteFile(path string) error {
data, err := yaml.Marshal(this)
if err != nil {

View File

@@ -1,7 +1,7 @@
package teaconst
const (
Version = "0.3.2"
Version = "0.3.5"
ProductName = "Edge API"
ProcessName = "edge-api"
@@ -18,7 +18,7 @@ const (
// 其他节点版本号,用来检测是否有需要升级的节点
NodeVersion = "0.3.2"
NodeVersion = "0.3.5"
UserNodeVersion = "0.0.10"
AuthorityNodeVersion = "0.0.2"
MonitorNodeVersion = "0.0.3"

View File

@@ -5,4 +5,5 @@ package teaconst
var (
IsPlus = false
MaxNodes int32 = 0
NodeId int64 = 0
)

View File

@@ -0,0 +1,80 @@
package accounts
import (
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type UserAccountDailyStatDAO dbs.DAO
func NewUserAccountDailyStatDAO() *UserAccountDailyStatDAO {
return dbs.NewDAO(&UserAccountDailyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeUserAccountDailyStats",
Model: new(UserAccountDailyStat),
PkName: "id",
},
}).(*UserAccountDailyStatDAO)
}
var SharedUserAccountDailyStatDAO *UserAccountDailyStatDAO
func init() {
dbs.OnReady(func() {
SharedUserAccountDailyStatDAO = NewUserAccountDailyStatDAO()
})
}
// UpdateDailyStat 更新当天统计数据
func (this *UserAccountDailyStatDAO) UpdateDailyStat(tx *dbs.Tx) error {
var day = timeutil.Format("Ymd")
var month = timeutil.Format("Ym")
income, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountIncomeEventTypes)
if err != nil {
return err
}
expense, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountExpenseEventTypes)
if err != nil {
return err
}
if expense < 0 {
expense = -expense
}
return this.Query(tx).
InsertOrUpdateQuickly(maps.Map{
"day": day,
"month": month,
"income": income,
"expense": expense,
}, maps.Map{
"income": income,
"expense": expense,
})
}
// FindDailyStats 查看按天统计
func (this *UserAccountDailyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
_, err = this.Query(tx).
Between("day", dayFrom, dayTo).
Slice(&result).
FindAll()
return
}
// FindMonthlyStats 查看某月统计
func (this *UserAccountDailyStatDAO) FindMonthlyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
_, err = this.Query(tx).
Result("SUM(income) AS income", "SUM(expense) AS expense", "month").
Between("day", dayFrom, dayTo).
Group("month").
Slice(&result).
FindAll()
return
}

View File

@@ -0,0 +1,6 @@
package accounts
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,22 @@
package accounts
// UserAccountDailyStat 账户每日统计
type UserAccountDailyStat struct {
Id uint32 `field:"id"` // ID
Day string `field:"day"` // YYYYMMDD
Month string `field:"month"` // YYYYMM
Income float64 `field:"income"` // 收入
Expense float64 `field:"expense"` // 支出
}
type UserAccountDailyStatOperator struct {
Id interface{} // ID
Day interface{} // YYYYMMDD
Month interface{} // YYYYMM
Income interface{} // 收入
Expense interface{} // 支出
}
func NewUserAccountDailyStatOperator() *UserAccountDailyStatOperator {
return &UserAccountDailyStatOperator{}
}

View File

@@ -0,0 +1 @@
package accounts

View File

@@ -0,0 +1,237 @@
package accounts
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"time"
)
func init() {
dbs.OnReadyDone(func() {
go func() {
// 自动支付账单任务
var ticker = time.NewTicker(12 * time.Hour)
for range ticker.C {
if SharedUserAccountDAO.Instance != nil {
err := SharedUserAccountDAO.Instance.RunTx(func(tx *dbs.Tx) error {
return SharedUserAccountDAO.PayBills(tx)
})
if err != nil {
remotelogs.Error("USER_ACCOUNT_DAO", "pay bills task failed: "+err.Error())
}
}
}
}()
})
}
type UserAccountDAO dbs.DAO
func NewUserAccountDAO() *UserAccountDAO {
return dbs.NewDAO(&UserAccountDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeUserAccounts",
Model: new(UserAccount),
PkName: "id",
},
}).(*UserAccountDAO)
}
var SharedUserAccountDAO *UserAccountDAO
func init() {
dbs.OnReady(func() {
SharedUserAccountDAO = NewUserAccountDAO()
})
}
// FindUserAccountWithUserId 根据用户ID查找用户账户
func (this *UserAccountDAO) FindUserAccountWithUserId(tx *dbs.Tx, userId int64) (*UserAccount, error) {
if userId <= 0 {
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
}
// 用户是否存在
user, err := models.SharedUserDAO.FindEnabledUser(tx, userId, nil)
if err != nil {
return nil, err
}
if user == nil {
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
}
account, err := this.Query(tx).
Attr("userId", userId).
Find()
if err != nil {
return nil, err
}
if account != nil {
return account.(*UserAccount), nil
}
var op = NewUserAccountOperator()
op.UserId = userId
_, err = this.SaveInt64(tx, op)
if err != nil {
return nil, err
}
return this.FindUserAccountWithUserId(tx, userId)
}
// FindUserAccountWithAccountId 根据ID查找用户账户
func (this *UserAccountDAO) FindUserAccountWithAccountId(tx *dbs.Tx, accountId int64) (*UserAccount, error) {
one, err := this.Query(tx).
Pk(accountId).
Find()
if one != nil {
return one.(*UserAccount), nil
}
return nil, err
}
// UpdateUserAccount 操作用户账户
func (this *UserAccountDAO) UpdateUserAccount(tx *dbs.Tx, accountId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
account, err := this.FindUserAccountWithAccountId(tx, accountId)
if err != nil {
return err
}
if account == nil {
return errors.New("invalid account id '" + types.String(accountId) + "'")
}
var userId = int64(account.Id)
var deltaFloat64 = float64(delta)
if deltaFloat64 < 0 && account.Total < -deltaFloat64 {
return errors.New("not enough account quota to decrease")
}
// 操作账户
err = this.Query(tx).
Pk(account.Id).
Set("total", dbs.SQL("total+:delta")).
Param("delta", delta).
UpdateQuickly()
if err != nil {
return err
}
// 生成日志
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, accountId, delta, 0, eventType, description, params)
if err != nil {
return err
}
return nil
}
// UpdateUserAccountFrozen 操作用户账户冻结余额
func (this *UserAccountDAO) UpdateUserAccountFrozen(tx *dbs.Tx, userId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
account, err := this.FindUserAccountWithUserId(tx, userId)
if err != nil {
return err
}
var deltaFloat64 = float64(delta)
if deltaFloat64 < 0 && account.TotalFrozen < -deltaFloat64 {
return errors.New("not enough account frozen quota to decrease")
}
// 操作账户
err = this.Query(tx).
Pk(account.Id).
Set("totalFrozen", dbs.SQL("total+:delta")).
Param("delta", delta).
UpdateQuickly()
if err != nil {
return err
}
// 生成日志
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, int64(account.Id), 0, delta, eventType, description, params)
if err != nil {
return err
}
return nil
}
// CountAllAccounts 计算所有账户数量
func (this *UserAccountDAO) CountAllAccounts(tx *dbs.Tx, keyword string) (int64, error) {
var query = this.Query(tx)
if len(keyword) > 0 {
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
query.Param("keyword", keyword)
} else {
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
}
return query.Count()
}
// ListAccounts 列出单页账户
func (this *UserAccountDAO) ListAccounts(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*UserAccount, err error) {
var query = this.Query(tx)
if len(keyword) > 0 {
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
query.Param("keyword", keyword)
} else {
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// PayBills 尝试自动支付账单
func (this *UserAccountDAO) PayBills(tx *dbs.Tx) error {
bills, err := models.SharedUserBillDAO.FindUnpaidBills(tx, 10000)
if err != nil {
return err
}
// 先支付久远的
lists.Reverse(bills)
for _, bill := range bills {
if bill.Amount <= 0 {
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
if err != nil {
return err
}
continue
}
account, err := SharedUserAccountDAO.FindUserAccountWithUserId(tx, int64(bill.UserId))
if err != nil {
return err
}
if account == nil || account.Total < bill.Amount {
continue
}
// 扣款
err = SharedUserAccountDAO.UpdateUserAccount(tx, int64(account.Id), -float32(bill.Amount), userconfigs.AccountEventTypePayBill, "支付账单"+bill.Code, maps.Map{"billId": bill.Id})
if err != nil {
return err
}
// 改为已支付
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
if err != nil {
return err
}
}
return nil
}

View File

@@ -0,0 +1,18 @@
package accounts
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
)
func TestUserAccountDAO_PayBills(t *testing.T) {
dbs.NotifyReady()
err := NewUserAccountDAO().PayBills(nil)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -0,0 +1,128 @@
package accounts
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type UserAccountLogDAO dbs.DAO
func NewUserAccountLogDAO() *UserAccountLogDAO {
return dbs.NewDAO(&UserAccountLogDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeUserAccountLogs",
Model: new(UserAccountLog),
PkName: "id",
},
}).(*UserAccountLogDAO)
}
var SharedUserAccountLogDAO *UserAccountLogDAO
func init() {
dbs.OnReady(func() {
SharedUserAccountLogDAO = NewUserAccountLogDAO()
})
}
// CreateAccountLog 生成用户账户日志
func (this *UserAccountLogDAO) CreateAccountLog(tx *dbs.Tx, userId int64, accountId int64, delta float32, deltaFrozen float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
var op = NewUserAccountLogOperator()
op.UserId = userId
op.AccountId = accountId
op.Delta = delta
op.DeltaFrozen = deltaFrozen
account, err := SharedUserAccountDAO.FindUserAccountWithAccountId(tx, accountId)
if err != nil {
return err
}
if account == nil {
return errors.New("invalid account id '" + types.String(accountId) + "'")
}
op.Total = account.Total
op.TotalFrozen = account.TotalFrozen
op.EventType = eventType
op.Description = description
if params == nil {
params = maps.Map{}
}
op.Params = params.AsJSON()
op.Day = timeutil.Format("Ymd")
err = this.Save(tx, op)
if err != nil {
return err
}
return SharedUserAccountDailyStatDAO.UpdateDailyStat(tx)
}
// CountAccountLogs 计算日志数量
func (this *UserAccountLogDAO) CountAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string) (int64, error) {
var query = this.Query(tx)
if userId > 0 {
query.Attr("userId", userId)
}
if accountId > 0 {
query.Attr("accountId", accountId)
}
if len(keyword) > 0 {
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
query.Param("keyword", "%"+keyword+"%")
}
if len(eventType) > 0 {
query.Attr("eventType", eventType)
}
return query.Count()
}
// ListAccountLogs 列出单页日志
func (this *UserAccountLogDAO) ListAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string, offset int64, size int64) (result []*UserAccountLog, err error) {
var query = this.Query(tx)
if userId > 0 {
query.Attr("userId", userId)
}
if accountId > 0 {
query.Attr("accountId", accountId)
}
if len(keyword) > 0 {
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
query.Param("keyword", "%"+keyword+"%")
}
if len(eventType) > 0 {
query.Attr("eventType", eventType)
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// SumDailyEventTypes 统计某天数据总和
func (this *UserAccountLogDAO) SumDailyEventTypes(tx *dbs.Tx, day string, eventTypes []userconfigs.AccountEventType) (float32, error) {
if len(eventTypes) == 0 {
return 0, nil
}
result, err := this.Query(tx).
Attr("day", day).
Attr("eventType", eventTypes).
Sum("delta", 0)
if err != nil {
return 0, err
}
return types.Float32(result), nil
}

View File

@@ -0,0 +1,6 @@
package accounts
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,36 @@
package accounts
// UserAccountLog 用户账户日志
type UserAccountLog struct {
Id uint64 `field:"id"` // ID
UserId uint64 `field:"userId"` // 用户ID
AccountId uint64 `field:"accountId"` // 账户ID
Delta float64 `field:"delta"` // 操作余额的数量(可为负)
DeltaFrozen float64 `field:"deltaFrozen"` // 操作冻结的数量(可为负)
Total float64 `field:"total"` // 操作后余额
TotalFrozen float64 `field:"totalFrozen"` // 操作后冻结余额
EventType string `field:"eventType"` // 类型
Description string `field:"description"` // 描述文字
Day string `field:"day"` // YYYYMMDD
CreatedAt uint64 `field:"createdAt"` // 时间
Params string `field:"params"` // 参数
}
type UserAccountLogOperator struct {
Id interface{} // ID
UserId interface{} // 用户ID
AccountId interface{} // 账户ID
Delta interface{} // 操作余额的数量(可为负)
DeltaFrozen interface{} // 操作冻结的数量(可为负)
Total interface{} // 操作后余额
TotalFrozen interface{} // 操作后冻结余额
EventType interface{} // 类型
Description interface{} // 描述文字
Day interface{} // YYYYMMDD
CreatedAt interface{} // 时间
Params interface{} // 参数
}
func NewUserAccountLogOperator() *UserAccountLogOperator {
return &UserAccountLogOperator{}
}

View File

@@ -0,0 +1 @@
package accounts

View File

@@ -0,0 +1,20 @@
package accounts
// UserAccount 用户账号
type UserAccount struct {
Id uint64 `field:"id"` // ID
UserId uint64 `field:"userId"` // 用户ID
Total float64 `field:"total"` // 可用总余额
TotalFrozen float64 `field:"totalFrozen"` // 冻结余额
}
type UserAccountOperator struct {
Id interface{} // ID
UserId interface{} // 用户ID
Total interface{} // 可用总余额
TotalFrozen interface{} // 冻结余额
}
func NewUserAccountOperator() *UserAccountOperator {
return &UserAccountOperator{}
}

View File

@@ -0,0 +1 @@
package accounts

View File

@@ -12,6 +12,7 @@ import (
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
"strconv"
"strings"
)
const (
@@ -219,7 +220,6 @@ func (this *APINodeDAO) CountAllEnabledAndOnAPINodes(tx *dbs.Tx) (int64, error)
Count()
}
// CountAllEnabledAndOnOfflineAPINodes 计算API节点数量
func (this *APINodeDAO) CountAllEnabledAndOnOfflineAPINodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
@@ -305,3 +305,19 @@ func (this *APINodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (i
Param("version", utils.VersionToLong(version)).
Count()
}
// CountAllEnabledAPINodesWithSSLPolicyIds 计算使用SSL策略的所有API节点数量
func (this *APINodeDAO) CountAllEnabledAPINodesWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
if len(sslPolicyIds) == 0 {
return
}
policyStringIds := []string{}
for _, policyId := range sslPolicyIds {
policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10))
}
return this.Query(tx).
State(APINodeStateEnabled).
Where("(FIND_IN_SET(JSON_EXTRACT(https, '$.sslPolicyRef.sslPolicyId'), :policyIds) OR FIND_IN_SET(JSON_EXTRACT(restHTTPS, '$.sslPolicyRef.sslPolicyId'), :policyIds))").
Param("policyIds", strings.Join(policyStringIds, ",")).
Count()
}

View File

@@ -2,9 +2,9 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
// DecodeHTTP 解析HTTP配置
@@ -27,11 +27,7 @@ func (this *APINode) DecodeHTTP() (*serverconfigs.HTTPProtocolConfig, error) {
}
// DecodeHTTPS 解析HTTPS配置
func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
if !IsNotNull(this.Https) {
return nil, nil
}
@@ -123,9 +119,9 @@ func (this *APINode) DecodeRestHTTP() (*serverconfigs.HTTPProtocolConfig, error)
}
// DecodeRestHTTPS 解析HTTPS配置
func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) {
func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
if this.RestIsOn != 1 {
return nil, nil

View File

@@ -93,16 +93,3 @@ func (this *AuthorityKeyDAO) ResetKey(tx *dbs.Tx) error {
Delete()
return err
}
// IsPlus 判断是否为企业版
func (this *AuthorityKeyDAO) IsPlus(tx *dbs.Tx) (bool, error) {
key, err := this.ReadKey(tx)
if err != nil {
return false, err
}
if key == nil {
return false, nil
}
teaconst.IsPlus = key.DayTo >= timeutil.Format("Y-m-d")
return teaconst.IsPlus, nil
}

View File

@@ -0,0 +1,14 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build community
// +build community
package authority
import (
"github.com/iwind/TeaGo/dbs"
)
// IsPlus 判断是否为企业版
func (this *AuthorityKeyDAO) IsPlus(tx *dbs.Tx) (bool, error) {
return false, nil
}

View File

@@ -5,6 +5,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"strconv"
)
const (
@@ -85,7 +86,7 @@ func (this *ClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browser
browserId, err := this.Query(tx).
Where("JSON_CONTAINS(codes, :browserName)").
Param("browserName", "\""+browserName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
Param("browserName", strconv.Quote(browserName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {

View File

@@ -5,6 +5,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"strconv"
)
const (
@@ -85,7 +86,7 @@ func (this *ClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemNam
systemId, err := this.Query(tx).
Where("JSON_CONTAINS(codes, :systemName)").
Param("systemName", "\""+systemName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
Param("systemName", strconv.Quote(systemName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -58,14 +59,13 @@ func (this *DNSDomainDAO) DisableDNSDomain(tx *dbs.Tx, id int64) error {
}
// FindEnabledDNSDomain 查找启用中的条目
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cacheMap maps.Map) (*DNSDomain, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cacheMap *utils.CacheMap) (*DNSDomain, error) {
var cacheKey = this.Table + ":record:" + types.String(domainId)
var cache = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*DNSDomain), nil
if cacheMap != nil {
cache, _ := cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*DNSDomain), nil
}
}
result, err := this.Query(tx).
@@ -75,7 +75,9 @@ func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cache
if result == nil {
return nil, err
}
cacheMap[cacheKey] = result
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*DNSDomain), err
}
@@ -214,7 +216,7 @@ func (this *DNSDomainDAO) FindDomainRouteName(tx *dbs.Tx, domainId int64, routeC
// ExistAvailableDomains 判断是否有域名可选
func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
subQuery, err := SharedDNSProviderDAO.Query(tx).
Where("state=1"). // 这里要使用非变量
Where("state=1"). // 这里要使用非变量
ResultPk().
AsSQL()
if err != nil {
@@ -263,7 +265,6 @@ func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordNa
func (this *DNSDomainDAO) FindEnabledDomainWithName(tx *dbs.Tx, providerId int64, domainName string) (*DNSDomain, error) {
one, err := this.Query(tx).
State(DNSDomainStateEnabled).
Attr("isOn", true).
Attr("providerId", providerId).
Attr("name", domainName).
Find()
@@ -280,3 +281,11 @@ func (this *DNSDomainDAO) UpdateDomainIsUp(tx *dbs.Tx, domainId int64, isUp bool
Set("isUp", isUp).
UpdateQuickly()
}
// UpdateDomainIsDeleted 设置域名为删除
func (this *DNSDomainDAO) UpdateDomainIsDeleted(tx *dbs.Tx, domainId int64, isDeleted bool) error {
return this.Query(tx).
Pk(domainId).
Set("isDeleted", isDeleted).
UpdateQuickly()
}

View File

@@ -16,6 +16,7 @@ type DNSDomain struct {
Routes string `field:"routes"` // 线路数据
IsUp uint8 `field:"isUp"` // 是否在线
State uint8 `field:"state"` // 状态
IsDeleted uint8 `field:"isDeleted"` // 是否已删除
}
type DNSDomainOperator struct {
@@ -33,6 +34,7 @@ type DNSDomainOperator struct {
Routes interface{} // 线路数据
IsUp interface{} // 是否在线
State interface{} // 状态
IsDeleted interface{} // 是否已删除
}
func NewDNSDomainOperator() *DNSDomainOperator {

View File

@@ -153,7 +153,7 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
}
// 检查IP地址
ipAddr, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, nodeconfigs.NodeRoleNode)
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, nodeconfigs.NodeRoleNode)
if err != nil {
return nil, err
}

View File

@@ -2,7 +2,6 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/configs"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
@@ -18,7 +17,6 @@ import (
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
@@ -82,7 +80,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper
fields["nodeId"] = accessLog.NodeId
fields["status"] = accessLog.Status
fields["createdAt"] = accessLog.Timestamp
fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId
fields["requestId"] = accessLog.RequestId
fields["firewallPolicyId"] = accessLog.FirewallPolicyId
fields["firewallRuleGroupId"] = accessLog.FirewallRuleGroupId
fields["firewallRuleSetId"] = accessLog.FirewallRuleSetId
@@ -407,7 +405,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
// FindAccessLogWithRequestId 根据请求ID获取访问日志
func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*HTTPAccessLog, error) {
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
return nil, errors.New("invalid requestId")
}

View File

@@ -3,11 +3,11 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
@@ -96,12 +96,12 @@ func (this *HTTPAuthPolicyDAO) UpdateHTTPAuthPolicy(tx *dbs.Tx, policyId int64,
}
// ComposePolicyConfig 组合配置
func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*serverconfigs.HTTPAuthPolicy, error) {
func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPAuthPolicy, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(policyId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.HTTPAuthPolicy), nil
}
@@ -130,7 +130,9 @@ func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, c
}
config.Params = params
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -3,12 +3,12 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
@@ -225,12 +225,12 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
}
// ComposeCachePolicy 组合配置
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*serverconfigs.HTTPCachePolicy, error) {
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPCachePolicy, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(policyId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.HTTPCachePolicy), nil
}
@@ -292,7 +292,9 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
config.CacheRefs = refs
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -62,6 +63,11 @@ func (this *HTTPFirewallPolicyDAO) DisableHTTPFirewallPolicy(tx *dbs.Tx, policyI
return err
}
err = this.NotifyDisable(tx, policyId)
if err != nil {
return err
}
return this.NotifyUpdate(tx, policyId)
}
@@ -85,6 +91,19 @@ func (this *HTTPFirewallPolicyDAO) FindHTTPFirewallPolicyName(tx *dbs.Tx, id int
FindStringCol("")
}
// FindEnabledHTTPFirewallPolicyBasic 获取WAF策略基本信息
func (this *HTTPFirewallPolicyDAO) FindEnabledHTTPFirewallPolicyBasic(tx *dbs.Tx, policyId int64) (*HTTPFirewallPolicy, error) {
result, err := this.Query(tx).
Pk(policyId).
Result("id", "name", "serverId", "isOn").
Attr("state", HTTPFirewallPolicyStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*HTTPFirewallPolicy), err
}
// FindAllEnabledFirewallPolicies 查找所有可用策略
func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (result []*HTTPFirewallPolicy, err error) {
_, err = this.Query(tx).
@@ -297,12 +316,12 @@ func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, keywo
}
// ComposeFirewallPolicy 组合策略配置
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*firewallconfigs.HTTPFirewallPolicy, error) {
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*firewallconfigs.HTTPFirewallPolicy, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(policyId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*firewallconfigs.HTTPFirewallPolicy), nil
}
@@ -392,7 +411,9 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
config.BlockOptions = blockAction
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
@@ -447,6 +468,19 @@ func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdsWithIPListId(tx *
return result, nil
}
// FindEnabledFirewallPolicyWithIPListId 查找使用某个IPList的策略
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyWithIPListId(tx *dbs.Tx, ipListId int64) (*HTTPFirewallPolicy, error) {
one, err := this.Query(tx).
State(HTTPFirewallPolicyStateEnabled).
Where("(JSON_CONTAINS(inbound, :listQuery, '$.whiteListRef') OR JSON_CONTAINS(inbound, :listQuery, '$.blackListRef'))").
Param("listQuery", maps.Map{"isOn": true, "listId": ipListId}.AsJSON()).
Find()
if err != nil || one == nil {
return nil, err
}
return one.(*HTTPFirewallPolicy), err
}
// FindEnabledFirewallPolicyIdWithRuleGroupId 查找包含某个规则分组的策略ID
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdWithRuleGroupId(tx *dbs.Tx, ruleGroupId int64) (int64, error) {
return this.Query(tx).
@@ -466,6 +500,23 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyServerId(tx *dbs.Tx, poli
return err
}
// FindFirewallPolicyIdsWithServerId 查找服务独立关联的策略IDs
func (this *HTTPFirewallPolicyDAO) FindFirewallPolicyIdsWithServerId(tx *dbs.Tx, serverId int64) ([]int64, error) {
var result = []int64{}
ones, err := this.Query(tx).
Attr("serverId", serverId).
State(HTTPFirewallPolicyStateEnabled).
Result("id").
FindAll()
if err != nil {
return nil, err
}
for _, one := range ones {
result = append(result, int64(one.(*HTTPFirewallPolicy).Id))
}
return result, nil
}
// NotifyUpdate 通知更新
func (this *HTTPFirewallPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithHTTPFirewallPolicyId(tx, policyId)
@@ -492,3 +543,65 @@ func (this *HTTPFirewallPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) erro
return nil
}
// NotifyDisable 通知禁用
func (this *HTTPFirewallPolicyDAO) NotifyDisable(tx *dbs.Tx, policyId int64) error {
if policyId <= 0 {
return nil
}
// 禁用IP名单
inboundString, err := this.Query(tx).
Pk(policyId).
Result("inbound").
FindStringCol("")
if err != nil {
return err
}
if len(inboundString) > 0 {
var inboundConfig = &firewallconfigs.HTTPFirewallInboundConfig{}
err = json.Unmarshal([]byte(inboundString), inboundConfig)
if err != nil {
// 不处理错误
return nil
}
if inboundConfig.AllowListRef != nil && inboundConfig.AllowListRef.ListId > 0 {
err = SharedIPListDAO.DisableIPList(tx, inboundConfig.AllowListRef.ListId)
if err != nil {
return err
}
err = SharedIPItemDAO.DisableIPItemsWithListId(tx, inboundConfig.AllowListRef.ListId)
if err != nil {
return err
}
}
if inboundConfig.DenyListRef != nil && inboundConfig.DenyListRef.ListId > 0 {
err = SharedIPListDAO.DisableIPList(tx, inboundConfig.DenyListRef.ListId)
if err != nil {
return err
}
err = SharedIPItemDAO.DisableIPItemsWithListId(tx, inboundConfig.DenyListRef.ListId)
if err != nil {
return err
}
}
if inboundConfig.GreyListRef != nil && inboundConfig.GreyListRef.ListId > 0 {
err = SharedIPListDAO.DisableIPList(tx, inboundConfig.GreyListRef.ListId)
if err != nil {
return err
}
err = SharedIPItemDAO.DisableIPItemsWithListId(tx, inboundConfig.GreyListRef.ListId)
if err != nil {
return err
}
}
}
return nil
}

View File

@@ -163,11 +163,12 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroupIsOn(tx *dbs.Tx, groupId int64,
}
// CreateGroup 创建分组
func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name string, description string) (int64, error) {
func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name string, code string, description string) (int64, error) {
op := NewHTTPFirewallRuleGroupOperator()
op.State = HTTPFirewallRuleStateEnabled
op.IsOn = isOn
op.Name = name
op.Code = code
op.Description = description
err := this.Save(tx, op)
if err != nil {
@@ -194,13 +195,13 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isO
}
// UpdateGroupSets 修改分组中的规则集
func (this *HTTPFirewallRuleGroupDAO) UpdateGroupSets(tx *dbs.Tx, groupId int64, setsJSON []byte) error {
func (this *HTTPFirewallRuleGroupDAO) UpdateGroupSets(tx *dbs.Tx, groupId int64, setRefsJSON []byte) error {
if groupId <= 0 {
return errors.New("invalid groupId")
}
op := NewHTTPFirewallRuleGroupOperator()
op.Id = groupId
op.Sets = setsJSON
op.Sets = setRefsJSON
err := this.Save(tx, op)
if err != nil {
return err

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql"
@@ -131,12 +132,12 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
}
// ComposeLocationConfig 组合配置
func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64, cacheMap maps.Map) (*serverconfigs.HTTPLocationConfig, error) {
func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPLocationConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(locationId)
var cacheConfig = cacheMap.Get(cacheKey)
var cacheConfig, _ = cacheMap.Get(cacheKey)
if cacheConfig != nil {
return cacheConfig.(*serverconfigs.HTTPLocationConfig), nil
}
@@ -194,7 +195,9 @@ func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64,
config.Conds = conds
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
@@ -259,7 +262,7 @@ func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, web
}
// ConvertLocationRefs 转换引用为配置
func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef, cacheMap maps.Map) (locations []*serverconfigs.HTTPLocationConfig, err error) {
func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef, cacheMap *utils.CacheMap) (locations []*serverconfigs.HTTPLocationConfig, err error) {
for _, ref := range refs {
config, err := this.ComposeLocationConfig(tx, ref.LocationId, cacheMap)
if err != nil {

View File

@@ -3,12 +3,12 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
@@ -133,12 +133,12 @@ func (this *HTTPPageDAO) UpdatePage(tx *dbs.Tx, pageId int64, statusList []strin
}
// ComposePageConfig 组合配置
func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap maps.Map) (*serverconfigs.HTTPPageConfig, error) {
func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPPageConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(pageId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.HTTPPageConfig), nil
}
@@ -175,7 +175,9 @@ func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap ma
}
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -3,12 +3,12 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
@@ -77,12 +77,12 @@ func (this *HTTPRewriteRuleDAO) FindEnabledHTTPRewriteRule(tx *dbs.Tx, id int64)
}
// ComposeRewriteRule 构造配置
func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64, cacheMap maps.Map) (*serverconfigs.HTTPRewriteRule, error) {
func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPRewriteRule, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(rewriteRuleId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.HTTPRewriteRule), nil
}
@@ -116,7 +116,9 @@ func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int
config.Conds = conds
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
@@ -75,12 +76,12 @@ func (this *HTTPWebDAO) FindEnabledHTTPWeb(tx *dbs.Tx, id int64) (*HTTPWeb, erro
}
// ComposeWebConfig 组合配置
func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps.Map) (*serverconfigs.HTTPWebConfig, error) {
func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPWebConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(webId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.HTTPWebConfig), nil
}
@@ -418,7 +419,9 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps.
config.RemoteAddr = remoteAddrConfig
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -75,6 +76,41 @@ func (this *IPItemDAO) DisableIPItem(tx *dbs.Tx, id int64) error {
return this.NotifyUpdate(tx, id)
}
// DisableIPItemsWithListId 禁用某个IP名单内的所有IP
func (this *IPItemDAO) DisableIPItemsWithListId(tx *dbs.Tx, listId int64) error {
for {
ones, err := this.Query(tx).
ResultPk().
Attr("listId", listId).
State(IPItemStateEnabled).
Limit(1000).
FindAll()
if err != nil {
return err
}
if len(ones) == 0 {
break
}
for _, one := range ones {
var itemId = one.(*IPItem).Id
version, err := SharedIPListDAO.IncreaseVersion(tx)
if err != nil {
return err
}
err = this.Query(tx).
Pk(itemId).
State(IPItemStateEnabled).
Set("version", version).
Set("state", IPItemStateDisabled).
UpdateQuickly()
if err != nil {
return err
}
}
}
return nil
}
// FindEnabledIPItem 查找启用中的条目
func (this *IPItemDAO) FindEnabledIPItem(tx *dbs.Tx, id int64) (*IPItem, error) {
result, err := this.Query(tx).
@@ -98,7 +134,21 @@ func (this *IPItemDAO) DisableOldIPItem(tx *dbs.Tx, listId int64, ipFrom string,
}
// CreateIPItem 创建IP
func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string, expiredAt int64, reason string, itemType IPItemType, eventLevel string) (int64, error) {
func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
listId int64,
ipFrom string,
ipTo string,
expiredAt int64,
reason string,
itemType IPItemType,
eventLevel string,
nodeId int64,
serverId int64,
sourceNodeId int64,
sourceServerId int64,
sourceHTTPFirewallPolicyId int64,
sourceHTTPFirewallRuleGroupId int64,
sourceHTTPFirewallRuleSetId int64) (int64, error) {
version, err := SharedIPListDAO.IncreaseVersion(tx)
if err != nil {
return 0, err
@@ -118,6 +168,15 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipT
expiredAt = 0
}
op.ExpiredAt = expiredAt
op.NodeId = nodeId
op.ServerId = serverId
op.SourceNodeId = sourceNodeId
op.SourceServerId = sourceServerId
op.SourceHTTPFirewallPolicyId = sourceHTTPFirewallPolicyId
op.SourceHTTPFirewallRuleGroupId = sourceHTTPFirewallRuleGroupId
op.SourceHTTPFirewallRuleSetId = sourceHTTPFirewallRuleSetId
op.State = IPItemStateEnabled
err = this.Save(tx, op)
if err != nil {
@@ -177,18 +236,39 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
}
// CountIPItemsWithListId 计算IP数量
func (this *IPItemDAO) CountIPItemsWithListId(tx *dbs.Tx, listId int64) (int64, error) {
return this.Query(tx).
func (this *IPItemDAO) CountIPItemsWithListId(tx *dbs.Tx, listId int64, ipFrom string, ipTo string, keyword string) (int64, error) {
var query = this.Query(tx).
State(IPItemStateEnabled).
Attr("listId", listId).
Count()
Attr("listId", listId)
if len(keyword) > 0 {
query.Where("(ipFrom LIKE :keyword OR ipTo LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if len(ipFrom) > 0 {
query.Attr("ipFrom", ipFrom)
}
if len(ipTo) > 0 {
query.Attr("ipTo", ipTo)
}
return query.Count()
}
// ListIPItemsWithListId 查找IP列表
func (this *IPItemDAO) ListIPItemsWithListId(tx *dbs.Tx, listId int64, offset int64, size int64) (result []*IPItem, err error) {
_, err = this.Query(tx).
func (this *IPItemDAO) ListIPItemsWithListId(tx *dbs.Tx, listId int64, keyword string, ipFrom string, ipTo string, offset int64, size int64) (result []*IPItem, err error) {
var query = this.Query(tx).
State(IPItemStateEnabled).
Attr("listId", listId).
Attr("listId", listId)
if len(keyword) > 0 {
query.Where("(ipFrom LIKE :keyword OR ipTo LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if len(ipFrom) > 0 {
query.Attr("ipFrom", ipFrom)
}
if len(ipTo) > 0 {
query.Attr("ipTo", ipTo)
}
_, err = query.
DescPk().
Slice(&result).
Offset(offset).
@@ -202,9 +282,8 @@ func (this *IPItemDAO) ListIPItemsAfterVersion(tx *dbs.Tx, version int64, size i
_, err = this.Query(tx).
// 这里不要设置状态参数,因为我们要知道哪些是删除的
Gt("version", version).
Where("(expiredAt=0 OR expiredAt>:expiredAt)").
Param("expiredAt", time.Now().Unix()).
Asc("version").
Asc("id").
Limit(size).
Slice(&result).
FindAll()
@@ -262,6 +341,47 @@ func (this *IPItemDAO) ExistsEnabledItem(tx *dbs.Tx, itemId int64) (bool, error)
Exist()
}
// CountAllEnabledIPItems 计算数量
func (this *IPItemDAO) CountAllEnabledIPItems(tx *dbs.Tx, ip string, listId int64) (int64, error) {
var query = this.Query(tx)
if len(ip) > 0 {
query.Attr("ipFrom", ip)
}
if listId > 0 {
query.Attr("listId", listId)
} else {
query.Where("(listId=" + types.String(firewallconfigs.GlobalListId) + " OR listId IN (SELECT id FROM " + SharedIPListDAO.Table + " WHERE state=1))")
}
return query.
State(IPItemStateEnabled).
Where("(expiredAt=0 OR expiredAt>:expiredAt)").
Param("expiredAt", time.Now().Unix()).
Count()
}
// ListAllEnabledIPItems 搜索所有IP
func (this *IPItemDAO) ListAllEnabledIPItems(tx *dbs.Tx, ip string, listId int64, offset int64, size int64) (result []*IPItem, err error) {
var query = this.Query(tx)
if len(ip) > 0 {
query.Attr("ipFrom", ip)
}
if listId > 0 {
query.Attr("listId", listId)
} else {
query.Where("(listId=" + types.String(firewallconfigs.GlobalListId) + " OR listId IN (SELECT id FROM " + SharedIPListDAO.Table + " WHERE state=1))")
}
_, err = query.
State(IPItemStateEnabled).
Where("(expiredAt=0 OR expiredAt>:expiredAt)").
Param("expiredAt", time.Now().Unix()).
DescPk().
Offset(offset).
Size(size).
Slice(&result).
FindAll()
return
}
// NotifyUpdate 通知更新
func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
// 获取ListId

View File

@@ -16,3 +16,14 @@ func TestIPItemDAO_NotifyClustersUpdate(t *testing.T) {
}
t.Log("ok")
}
func TestIPItemDAO_DisableIPItemsWithListId(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
err := SharedIPItemDAO.DisableIPItemsWithListId(tx, 67)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -1,38 +1,52 @@
package models
// IP
// IPItem IP
type IPItem struct {
Id uint64 `field:"id"` // ID
ListId uint32 `field:"listId"` // 所属名单ID
Type string `field:"type"` // 类型
IpFrom string `field:"ipFrom"` // 开始IP
IpTo string `field:"ipTo"` // 结束IP
IpFromLong uint64 `field:"ipFromLong"` // 开始IP整型
IpToLong uint64 `field:"ipToLong"` // 结束IP整型
Version uint64 `field:"version"` // 版本
CreatedAt uint64 `field:"createdAt"` // 创建时间
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
Reason string `field:"reason"` // 加入说明
EventLevel string `field:"eventLevel"` // 事件级别
State uint8 `field:"state"` // 状态
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
Id uint64 `field:"id"` // ID
ListId uint32 `field:"listId"` // 所属名单ID
Type string `field:"type"` // 类型
IpFrom string `field:"ipFrom"` // 开始IP
IpTo string `field:"ipTo"` // 结束IP
IpFromLong uint64 `field:"ipFromLong"` // 开始IP整型
IpToLong uint64 `field:"ipToLong"` // 结束IP整型
Version uint64 `field:"version"` // 版本
CreatedAt uint64 `field:"createdAt"` // 创建时间
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
Reason string `field:"reason"` // 加入说明
EventLevel string `field:"eventLevel"` // 事件级别
State uint8 `field:"state"` // 状态
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
ServerId uint32 `field:"serverId"` // 有效范围服务ID
NodeId uint32 `field:"nodeId"` // 有效范围节点ID
SourceNodeId uint32 `field:"sourceNodeId"` // 来源节点ID
SourceServerId uint32 `field:"sourceServerId"` // 来源服务ID
SourceHTTPFirewallPolicyId uint32 `field:"sourceHTTPFirewallPolicyId"` // 来源策略ID
SourceHTTPFirewallRuleGroupId uint32 `field:"sourceHTTPFirewallRuleGroupId"` // 来源规则集分组ID
SourceHTTPFirewallRuleSetId uint32 `field:"sourceHTTPFirewallRuleSetId"` // 来源规则集ID
}
type IPItemOperator struct {
Id interface{} // ID
ListId interface{} // 所属名单ID
Type interface{} // 类型
IpFrom interface{} // 开始IP
IpTo interface{} // 结束IP
IpFromLong interface{} // 开始IP整型
IpToLong interface{} // 结束IP整型
Version interface{} // 版本
CreatedAt interface{} // 创建时间
UpdatedAt interface{} // 修改时间
Reason interface{} // 加入说明
EventLevel interface{} // 事件级别
State interface{} // 状态
ExpiredAt interface{} // 过期时间
Id interface{} // ID
ListId interface{} // 所属名单ID
Type interface{} // 类型
IpFrom interface{} // 开始IP
IpTo interface{} // 结束IP
IpFromLong interface{} // 开始IP整型
IpToLong interface{} // 结束IP整型
Version interface{} // 版本
CreatedAt interface{} // 创建时间
UpdatedAt interface{} // 修改时间
Reason interface{} // 加入说明
EventLevel interface{} // 事件级别
State interface{} // 状态
ExpiredAt interface{} // 过期时间
ServerId interface{} // 有效范围服务ID
NodeId interface{} // 有效范围节点ID
SourceNodeId interface{} // 来源节点ID
SourceServerId interface{} // 来源服务ID
SourceHTTPFirewallPolicyId interface{} // 来源策略ID
SourceHTTPFirewallRuleGroupId interface{} // 来源规则集分组ID
SourceHTTPFirewallRuleSetId interface{} // 来源规则集ID
}
func NewIPItemOperator() *IPItemOperator {

View File

@@ -2,7 +2,9 @@ package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -16,7 +18,16 @@ const (
IPListStateDisabled = 0 // 已禁用
)
var listTypeCacheMap = map[int64]string{} // listId => type
var listTypeCacheMap = map[int64]*IPList{} // listId => *IPList
var DefaultGlobalIPList = &IPList{
Id: uint32(firewallconfigs.GlobalListId),
Name: "全局封锁名单",
IsPublic: 1,
IsGlobal: 1,
Type: "black",
State: IPListStateEnabled,
IsOn: 1,
}
type IPListDAO dbs.DAO
@@ -58,7 +69,19 @@ func (this *IPListDAO) DisableIPList(tx *dbs.Tx, id int64) error {
}
// FindEnabledIPList 查找启用中的条目
func (this *IPListDAO) FindEnabledIPList(tx *dbs.Tx, id int64) (*IPList, error) {
func (this *IPListDAO) FindEnabledIPList(tx *dbs.Tx, id int64, cacheMap *utils.CacheMap) (*IPList, error) {
if id == firewallconfigs.GlobalListId {
return DefaultGlobalIPList, nil
}
var cacheKey = this.Table + ":FindEnabledIPList:" + types.String(id)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*IPList), nil
}
}
result, err := this.Query(tx).
Pk(id).
Attr("state", IPListStateEnabled).
@@ -66,6 +89,11 @@ func (this *IPListDAO) FindEnabledIPList(tx *dbs.Tx, id int64) (*IPList, error)
if result == nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*IPList), err
}
@@ -77,38 +105,39 @@ func (this *IPListDAO) FindIPListName(tx *dbs.Tx, id int64) (string, error) {
FindStringCol("")
}
// FindIPListTypeCacheable 获取名单类型
func (this *IPListDAO) FindIPListTypeCacheable(tx *dbs.Tx, listId int64) (string, error) {
// FindIPListCacheable 获取名单
func (this *IPListDAO) FindIPListCacheable(tx *dbs.Tx, listId int64) (*IPList, error) {
// 全局黑名单
if listId == firewallconfigs.GlobalListId {
return DefaultGlobalIPList, nil
}
// 检查缓存
SharedCacheLocker.RLock()
listType, ok := listTypeCacheMap[listId]
list, ok := listTypeCacheMap[listId]
SharedCacheLocker.RUnlock()
if ok {
return listType, nil
return list, nil
}
listType, err := this.Query(tx).
one, err := this.Query(tx).
Pk(listId).
Result("type").
FindStringCol("")
if err != nil {
return "", err
}
if len(listType) == 0 {
return "", nil
Result("isGlobal", "type", "state", "id", "isPublic", "isGlobal").
Find()
if err != nil || one == nil {
return nil, err
}
// 保存缓存
SharedCacheLocker.Lock()
listTypeCacheMap[listId] = listType
listTypeCacheMap[listId] = one.(*IPList)
SharedCacheLocker.Unlock()
return listType, nil
return one.(*IPList), nil
}
// CreateIPList 创建名单
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool) (int64, error) {
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool, isGlobal bool) (int64, error) {
op := NewIPListOperator()
op.IsOn = true
op.UserId = userId
@@ -121,6 +150,7 @@ func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs
}
op.Description = description
op.IsPublic = isPublic
op.IsGlobal = isGlobal
err := this.Save(tx, op)
if err != nil {
return 0, err

View File

@@ -15,6 +15,7 @@ type IPList struct {
Actions string `field:"actions"` // IP触发的动作
Description string `field:"description"` // 描述
IsPublic uint8 `field:"isPublic"` // 是否公用
IsGlobal uint8 `field:"isGlobal"` // 是否全局
}
type IPListOperator struct {
@@ -31,6 +32,7 @@ type IPListOperator struct {
Actions interface{} // IP触发的动作
Description interface{} // 描述
IsPublic interface{} // 是否公用
IsGlobal interface{} // 是否全局
}
func NewIPListOperator() *IPListOperator {

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -55,12 +56,12 @@ func (this *MessageMediaInstanceDAO) DisableMessageMediaInstance(tx *dbs.Tx, id
}
// FindEnabledMessageMediaInstance 查找启用中的条目
func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx, instanceId int64, cacheMap maps.Map) (*MessageMediaInstance, error) {
func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx, instanceId int64, cacheMap *utils.CacheMap) (*MessageMediaInstance, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":record:" + types.String(instanceId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*MessageMediaInstance), nil
}
@@ -73,7 +74,9 @@ func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx,
return nil, err
}
cacheMap[cacheKey] = result
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*MessageMediaInstance), err
}

View File

@@ -3,11 +3,11 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"regexp"
)
@@ -57,13 +57,13 @@ func (this *MessageRecipientDAO) DisableMessageRecipient(tx *dbs.Tx, id int64) e
}
// FindEnabledMessageRecipient 查找启用中的条目
func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipientId int64, cacheMap maps.Map,
func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipientId int64, cacheMap *utils.CacheMap,
) (*MessageRecipient, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":record:" + types.String(recipientId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*MessageRecipient), nil
}
@@ -76,7 +76,9 @@ func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipie
return nil, err
}
cacheMap[cacheKey] = result
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*MessageRecipient), err
}

View File

@@ -1,6 +1,7 @@
package models
import (
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -94,6 +95,10 @@ func (this *MessageTaskDAO) FindEnabledMessageTask(tx *dbs.Tx, id int64) (*Messa
// CreateMessageTask 创建任务
func (this *MessageTaskDAO) CreateMessageTask(tx *dbs.Tx, recipientId int64, instanceId int64, user string, subject string, body string, isPrimary bool) (int64, error) {
if !teaconst.IsPlus {
return 0, nil
}
var hash = stringutil.Md5(types.String(recipientId) + "@" + types.String(instanceId) + "@" + user + "@" + subject + "@" + types.String(isPrimary))
recipientInstanceId, err := SharedMessageRecipientDAO.FindRecipientInstanceId(tx, recipientId)
if err != nil {
@@ -196,6 +201,10 @@ func (this *MessageTaskDAO) UpdateMessageTaskStatus(tx *dbs.Tx, taskId int64, st
// CreateMessageTasks 从集群、节点或者服务中创建任务
func (this *MessageTaskDAO) CreateMessageTasks(tx *dbs.Tx, role nodeconfigs.NodeRole, clusterId int64, nodeId int64, serverId int64, messageType MessageType, subject string, body string) error {
if !teaconst.IsPlus {
return nil
}
receivers, err := SharedMessageReceiverDAO.FindEnabledBestFitReceivers(tx, role, clusterId, nodeId, serverId, messageType)
if err != nil {
return err

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
@@ -229,7 +230,16 @@ func (this *MetricItemDAO) ListEnabledItems(tx *dbs.Tx, category serverconfigs.M
}
// FindAllPublicItems 取得公用的指标
func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string) (result []*MetricItem, err error) {
func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string, cacheMap *utils.CacheMap) (result []*MetricItem, err error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindAllPublicItems:" + category
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.([]*MetricItem), nil
}
_, err = this.Query(tx).
State(MetricItemStateEnabled).
Attr("userId", 0).
@@ -238,6 +248,14 @@ func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string) (resu
DescPk().
Slice(&result).
FindAll()
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -177,7 +178,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
}
// UpdateCluster 修改集群
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string) error {
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
}
@@ -186,8 +187,12 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
op.Name = name
op.GrantId = grantId
op.InstallDir = installDir
op.TimeZone = timezone
err := this.Save(tx, op)
return err
if err != nil {
return err
}
return this.NotifyUpdate(tx, clusterId)
}
// CountAllEnabledClusters 计算所有集群数量
@@ -403,14 +408,13 @@ func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int
}
// FindClusterDNSInfo 查找DNS信息
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (*NodeCluster, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
var cacheKey = this.Table + ":record:" + types.String(clusterId)
var cache = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*NodeCluster), nil
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
var cacheKey = this.Table + ":FindClusterDNSInfo:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*NodeCluster), nil
}
}
one, err := this.Query(tx).
@@ -423,7 +427,9 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cach
if one == nil {
return nil, nil
}
cacheMap[cacheKey] = one
if cacheMap != nil {
cacheMap.Put(cacheKey, one)
}
return one.(*NodeCluster), nil
}
@@ -483,7 +489,15 @@ func (this *NodeClusterDAO) FindClusterAdminId(tx *dbs.Tx, clusterId int64) (int
}
// FindClusterTOAConfig 查找集群的TOA设置
func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*nodeconfigs.TOAConfig, error) {
func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*nodeconfigs.TOAConfig, error) {
var cacheKey = this.Table + ":FindClusterTOAConfig:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*nodeconfigs.TOAConfig), nil
}
}
toa, err := this.Query(tx).
Pk(clusterId).
Result("toa").
@@ -500,6 +514,11 @@ func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
@@ -583,12 +602,12 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *db
}
// FindClusterHTTPFirewallPolicyId 获取集群的WAF策略ID
func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (int64, error) {
func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (int64, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindClusterHTTPFirewallPolicyId:" + types.String(clusterId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(int64), nil
}
@@ -601,7 +620,9 @@ func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterI
return 0, err
}
cacheMap[cacheKey] = firewallPolicyId
if cacheMap != nil {
cacheMap.Put(cacheKey, firewallPolicyId)
}
return firewallPolicyId, nil
}
@@ -619,12 +640,12 @@ func (this *NodeClusterDAO) UpdateNodeClusterHTTPCachePolicyId(tx *dbs.Tx, clust
}
// FindClusterHTTPCachePolicyId 获取集群的缓存策略ID
func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (int64, error) {
func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (int64, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindClusterHTTPCachePolicyId:" + types.String(clusterId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(int64), nil
}
@@ -637,7 +658,9 @@ func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId i
return 0, err
}
cacheMap[cacheKey] = cachePolicyId
if cacheMap != nil {
cacheMap.Put(cacheKey, cachePolicyId)
}
return cachePolicyId, nil
}
@@ -716,10 +739,19 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServiceParams(tx *dbs.Tx, clust
}
// FindNodeClusterSystemServices 查找集群的所有服务设置
func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64) (services map[string]maps.Map, err error) {
func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (services map[string]maps.Map, err error) {
if clusterId <= 0 {
return nil, errors.New("invalid clusterId")
}
var cacheKey = this.Table + ":FindNodeClusterSystemServices:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(map[string]maps.Map), nil
}
}
service, err := this.Query(tx).
Pk(clusterId).
Result("systemServices").
@@ -734,6 +766,11 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId
return nil, err
}
}
if cacheMap != nil {
cacheMap.Put(cacheKey, servicesMap)
}
return servicesMap, nil
}
@@ -811,6 +848,29 @@ func (this *NodeClusterDAO) ExistsEnabledCluster(tx *dbs.Tx, clusterId int64) (b
Exist()
}
// FindClusterTimezone 查找时区
func (this *NodeClusterDAO) FindClusterTimezone(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (string, error) {
var cacheKey = this.Table + ":FindEnabledTimeZone:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(string), nil
}
}
timeZone, err := this.Query(tx).
Pk(clusterId).
Result("timeZone").
FindStringCol("")
if err != nil {
return "", err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, timeZone)
}
return timeZone, nil
}
// NotifyUpdate 通知更新
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)

View File

@@ -2,11 +2,13 @@ package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
const (
@@ -35,7 +37,7 @@ func init() {
})
}
// 启用条目
// EnableFirewallAction 启用条目
func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
@@ -44,7 +46,7 @@ func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id ui
return err
}
// 禁用条目
// DisableFirewallAction 禁用条目
func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, actionId int64) error {
_, err := this.Query(tx).
Pk(actionId).
@@ -56,7 +58,7 @@ func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, acti
return this.NotifyUpdate(tx, actionId)
}
// 查找启用中的条目
// FindEnabledFirewallAction 查找启用中的条目
func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx, actionId int64) (*NodeClusterFirewallAction, error) {
result, err := this.Query(tx).
Pk(actionId).
@@ -68,7 +70,7 @@ func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx,
return result.(*NodeClusterFirewallAction), err
}
// 根据主键查找名称
// FindFirewallActionName 根据主键查找名称
func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx).
Pk(id).
@@ -76,7 +78,7 @@ func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id
FindStringCol("")
}
// 创建动作
// CreateFirewallAction 创建动作
func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, adminId int64, clusterId int64, name string, eventLevel, actionType firewallconfigs.FirewallActionType, params maps.Map) (int64, error) {
if params == nil {
params = maps.Map{}
@@ -101,7 +103,7 @@ func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, admin
return actionId, nil
}
// 修改动作
// UpdateFirewallAction 修改动作
func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actionId int64, name string, eventLevel string, actionType firewallconfigs.FirewallActionType, params maps.Map) error {
if actionId <= 0 {
return errors.New("invalid actionId")
@@ -124,17 +126,33 @@ func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actio
return this.NotifyUpdate(tx, actionId)
}
// 查找所有集群的动作
func (this *NodeClusterFirewallActionDAO) FindAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (result []*NodeClusterFirewallAction, err error) {
// FindAllEnabledFirewallActions 查找所有集群的动作
func (this *NodeClusterFirewallActionDAO) FindAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (result []*NodeClusterFirewallAction, err error) {
var cacheKey = this.Table + ":FindAllEnabledFirewallActions:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.([]*NodeClusterFirewallAction), nil
}
}
_, err = this.Query(tx).
Attr("clusterId", clusterId).
State(NodeClusterFirewallActionStateEnabled).
Slice(&result).
FindAll()
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return
}
// 组合配置
// ComposeFirewallActionConfig 组合配置
func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx, action *NodeClusterFirewallAction) (*firewallconfigs.FirewallActionConfig, error) {
if action == nil {
return nil, nil
@@ -153,7 +171,7 @@ func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx
return config, nil
}
// 计算动作数量
// CountAllEnabledFirewallActions 计算动作数量
func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterFirewallActionStateEnabled).
@@ -161,7 +179,7 @@ func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs
Count()
}
// 通知更新
// NotifyUpdate 通知更新
func (this *NodeClusterFirewallActionDAO) NotifyUpdate(tx *dbs.Tx, actionId int64) error {
clusterId, err := this.Query(tx).
Pk(actionId).

View File

@@ -2,10 +2,12 @@ package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
@@ -112,16 +114,32 @@ func (this *NodeClusterMetricItemDAO) FindAllClusterItems(tx *dbs.Tx, clusterId
}
// FindAllClusterItemIds 查找某个集群的指标Ids
func (this *NodeClusterMetricItemDAO) FindAllClusterItemIds(tx *dbs.Tx, clusterId int64) (result []int64, err error) {
func (this *NodeClusterMetricItemDAO) FindAllClusterItemIds(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (result []int64, err error) {
var cacheKey = this.Table + ":FindAllClusterItemIds:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.([]int64), nil
}
}
ones, err := this.Query(tx).
Attr("clusterId", clusterId).
State(NodeClusterMetricItemStateEnabled).
Result("itemId").
DescPk().
FindAll()
if err != nil {
return nil, err
}
for _, one := range ones {
result = append(result, int64(one.(*NodeClusterMetricItem).ItemId))
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return
}

View File

@@ -26,6 +26,7 @@ type NodeCluster struct {
HttpFirewallPolicyId uint32 `field:"httpFirewallPolicyId"` // WAF策略ID
AccessLog string `field:"accessLog"` // 访问日志设置
SystemServices string `field:"systemServices"` // 系统服务设置
TimeZone string `field:"timeZone"` // 时区
}
type NodeClusterOperator struct {
@@ -53,6 +54,7 @@ type NodeClusterOperator struct {
HttpFirewallPolicyId interface{} // WAF策略ID
AccessLog interface{} // 访问日志设置
SystemServices interface{} // 系统服务设置
TimeZone interface{} // 时区
}
func NewNodeClusterOperator() *NodeClusterOperator {

View File

@@ -315,7 +315,7 @@ func (this *NodeDAO) ListEnabledNodesMatch(tx *dbs.Tx,
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Where("JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
query.Where("isActive AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
case configutils.BoolStateNo:
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
}
@@ -498,14 +498,30 @@ func (this *NodeDAO) FindAllEnabledNodesWithClusterId(tx *dbs.Tx, clusterId int6
return
}
// FindAllEnabledNodeIdsWithClusterId 获取一个集群的所有节点Ids
func (this *NodeDAO) FindAllEnabledNodeIdsWithClusterId(tx *dbs.Tx, clusterId int64) (result []int64, err error) {
ones, err := this.Query(tx).
ResultPk().
State(NodeStateEnabled).
Attr("clusterId", clusterId).
FindAll()
if err != nil {
return nil, err
}
for _, one := range ones {
result = append(result, int64(one.(*Node).Id))
}
return
}
// FindAllInactiveNodesWithClusterId 取得一个集群离线的节点
func (this *NodeDAO) FindAllInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*Node, err error) {
_, err = this.Query(tx).
State(NodeStateEnabled).
Attr("clusterId", clusterId).
Attr("isOn", true). // 只监控启用的节点
Attr("isOn", true). // 只监控启用的节点
Attr("isInstalled", true). // 只监控已经安装的节点
Attr("isActive", true). // 当前已经在线的
Attr("isActive", true). // 当前已经在线的
Where("(status IS NULL OR (JSON_EXTRACT(status, '$.isActive')=false AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>10) OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>120)").
Result("id", "name").
Slice(&result).
@@ -553,7 +569,7 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx,
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Where("JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
query.Where("isActive AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
case configutils.BoolStateNo:
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
}
@@ -581,6 +597,7 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx,
func (this *NodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
_, err := this.Query(tx).
Pk(nodeId).
Set("isActive", true).
Set("status", string(statusJSON)).
Update()
return err
@@ -685,7 +702,11 @@ func (this *NodeDAO) UpdateNodeInstallStatus(tx *dbs.Tx, nodeId int64, status *N
// ComposeNodeConfig 组合配置
// TODO 提升运行速度
func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.Map) (*nodeconfigs.NodeConfig, error) {
func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils.CacheMap) (*nodeconfigs.NodeConfig, error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
node, err := this.FindEnabledNode(tx, nodeId)
if err != nil {
return nil, err
@@ -713,7 +734,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
}
for _, server := range servers {
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap)
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true)
if err != nil {
return nil, err
}
@@ -721,14 +742,27 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
continue
}
config.Servers = append(config.Servers, serverConfig)
if server.IsOn == 1 && server.SupportCNAME == 1 {
config.SupportCNAME = true
}
}
// 全局设置
// TODO 根据用户的不同读取不同的全局设置
settingJSON, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeServerGlobalConfig)
if err != nil {
return nil, err
var settingCacheKey = "SharedSysSettingDAO:" + systemconfigs.SettingCodeServerGlobalConfig
settingJSONCache, ok := cacheMap.Get(settingCacheKey)
var settingJSON = []byte{}
if ok {
settingJSON = settingJSONCache.([]byte)
} else {
settingJSON, err = SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeServerGlobalConfig)
if err != nil {
return nil, err
}
cacheMap.Put(settingCacheKey, settingJSON)
}
if len(settingJSON) > 0 {
globalConfig := &serverconfigs.GlobalConfig{}
err = json.Unmarshal(settingJSON, globalConfig)
@@ -770,6 +804,15 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
config.HTTPCachePolicies = append(config.HTTPCachePolicies, cachePolicy)
}
}
// 时区
timeZone, err := SharedNodeClusterDAO.FindClusterTimezone(tx, clusterId, cacheMap)
if err != nil {
return nil, err
}
if len(timeZone) > 0 {
config.TimeZone = timeZone
}
}
// 缓存最大容量设置
@@ -796,14 +839,14 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
}
// TOA
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId)
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId, cacheMap)
if err != nil {
return nil, err
}
config.TOA = toaConfig
// 系统服务
services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, primaryClusterId)
services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, primaryClusterId, cacheMap)
if err != nil {
return nil, err
}
@@ -812,7 +855,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
}
// 防火墙动作
actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, primaryClusterId)
actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, primaryClusterId, cacheMap)
if err != nil {
return nil, err
}
@@ -827,7 +870,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
}
// 集群指标
metricItemIds, err := SharedNodeClusterMetricItemDAO.FindAllClusterItemIds(tx, int64(node.ClusterId))
metricItemIds, err := SharedNodeClusterMetricItemDAO.FindAllClusterItemIds(tx, int64(node.ClusterId), cacheMap)
if err != nil {
return nil, err
}
@@ -843,7 +886,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
}
// 公用指标
publicMetricItems, err := SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP)
publicMetricItems, err := SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP, cacheMap)
if err != nil {
return nil, err
}
@@ -1398,7 +1441,7 @@ func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
return err
}
if clusterId > 0 {
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, NodeTaskTypeConfigChanged)
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, NodeTaskTypeConfigChanged, 0)
}
return nil
}

View File

@@ -1,9 +1,9 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"testing"
"time"
)
@@ -46,13 +46,13 @@ func TestNodeDAO_ComposeNodeConfig(t *testing.T) {
}()
var tx *dbs.Tx
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
nodeConfig, err := SharedNodeDAO.ComposeNodeConfig(tx, 48, cacheMap)
if err != nil {
t.Fatal(err)
}
t.Log(len(nodeConfig.Servers), "servers")
t.Log(len(cacheMap), "items")
t.Log(cacheMap.Len(), "items")
// old: 77ms => new: 56ms
}

View File

@@ -73,7 +73,7 @@ func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, erro
}
// CreateGrant 创建认证信息
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) (grantId int64, err error) {
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64) (grantId int64, err error) {
op := NewNodeGrantOperator()
op.AdminId = adminId
op.Name = name
@@ -87,6 +87,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
case "privateKey":
op.Username = username
op.PrivateKey = privateKey
op.Passphrase = passphrase
}
op.Description = description
op.NodeId = nodeId
@@ -96,7 +97,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
}
// UpdateGrant 修改认证信息
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) error {
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64) error {
if grantId <= 0 {
return errors.New("invalid grantId")
}
@@ -114,6 +115,7 @@ func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, me
case "privateKey":
op.Username = username
op.PrivateKey = privateKey
op.Passphrase = passphrase
}
op.Description = description
op.NodeId = nodeId

View File

@@ -9,7 +9,8 @@ type NodeGrant struct {
Username string `field:"username"` // 用户名
Password string `field:"password"` // 密码
Su uint8 `field:"su"` // 是否需要su
PrivateKey string `field:"privateKey"` //
PrivateKey string `field:"privateKey"` //
Passphrase string `field:"passphrase"` // 私钥密码
Description string `field:"description"` // 备注
NodeId uint32 `field:"nodeId"` // 专有节点
Role string `field:"role"` // 角色
@@ -25,7 +26,8 @@ type NodeGrantOperator struct {
Username interface{} // 用户名
Password interface{} // 密码
Su interface{} // 是否需要su
PrivateKey interface{} //
PrivateKey interface{} //
Passphrase interface{} // 私钥密码
Description interface{} // 备注
NodeId interface{} // 专有节点
Role interface{} // 角色

View File

@@ -102,6 +102,22 @@ func (this *NodeIPAddressDAO) FindAddressName(tx *dbs.Tx, id int64) (string, err
FindStringCol("")
}
// FindAddressIsHealthy 判断IP地址是否健康
func (this *NodeIPAddressDAO) FindAddressIsHealthy(tx *dbs.Tx, addressId int64) (isHealthy bool, err error) {
if addressId <= 0 {
return false, nil
}
one, err := this.Query(tx).
Pk(addressId).
Result("isHealthy").
Find()
if err != nil || one == nil {
return false, err
}
var addr = one.(*NodeIPAddress)
return addr.IsHealthy == 1, nil
}
// CreateAddress 创建IP地址
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool) (addressId int64, err error) {
if len(role) == 0 {
@@ -216,11 +232,11 @@ func (this *NodeIPAddressDAO) FindAllEnabledAddressesWithNode(tx *dbs.Tx, nodeId
}
// FindFirstNodeAccessIPAddress 查找节点的第一个可访问的IP地址
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (string, error) {
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (ip string, addrId int64, err error) {
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
return this.Query(tx).
one, err := this.Query(tx).
Attr("nodeId", nodeId).
Attr("role", role).
State(NodeIPAddressStateEnabled).
@@ -229,8 +245,17 @@ func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId in
Attr("isUp", true).
Desc("order").
AscPk().
Result("ip").
FindStringCol("")
Result("id", "ip").
Find()
if err != nil {
return "", 0, err
}
if one == nil {
return
}
var addr = one.(*NodeIPAddress)
return addr.Ip, int64(addr.Id), nil
}
// FindFirstNodeAccessIPAddressId 查找节点的第一个可访问的IP地址ID
@@ -400,6 +425,10 @@ func (this *NodeIPAddressDAO) UpdateAddressIsUp(tx *dbs.Tx, addressId int64, isU
var err = this.Query(tx).
Pk(addressId).
Set("isUp", isUp).
Set("countUp", 0).
Set("countDown", 0).
Set("backupIP", "").
Set("backupThresholdId", 0).
UpdateQuickly()
if err != nil {
return err
@@ -423,6 +452,71 @@ func (this *NodeIPAddressDAO) UpdateAddressBackupIP(tx *dbs.Tx, addressId int64,
return this.NotifyUpdate(tx, addressId)
}
// UpdateAddressHealthCount 计算IP健康状态
func (this *NodeIPAddressDAO) UpdateAddressHealthCount(tx *dbs.Tx, addrId int64, isUp bool, maxUp int, maxDown int) (changed bool, err error) {
if addrId <= 0 {
return false, errors.New("invalid address id")
}
one, err := this.Query(tx).
Pk(addrId).
Result("isHealthy", "countUp", "countDown").
Find()
if err != nil {
return false, err
}
if one == nil {
return false, nil
}
oldIsHealthy := one.(*NodeIPAddress).IsHealthy == 1
// 如果新老状态一致,则不做任何事情
if oldIsHealthy == isUp {
return false, nil
}
countUp := int(one.(*NodeIPAddress).CountUp)
countDown := int(one.(*NodeIPAddress).CountDown)
op := NewNodeIPAddressOperator()
op.Id = addrId
if isUp {
countUp++
countDown = 0
if countUp >= maxUp {
changed = true
//op.IsUp = true
op.IsHealthy = true
}
} else {
countDown++
countUp = 0
if countDown >= maxDown {
changed = true
//op.IsUp = false
op.IsHealthy = false
}
}
op.CountUp = countUp
op.CountDown = countDown
err = this.Save(tx, op)
if err != nil {
return false, err
}
if changed {
err = this.NotifyUpdate(tx, addrId)
if err != nil {
return true, err
}
}
return
}
// NotifyUpdate 通知更新
func (this *NodeIPAddressDAO) NotifyUpdate(tx *dbs.Tx, addressId int64) error {
address, err := this.Query(tx).

View File

@@ -40,3 +40,25 @@ func TestNodeIPAddressDAO_LoopTasks(t *testing.T) {
}
t.Log("ok")
}
func TestNodeIPAddressDAO_FindAddressIsHealthy(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
isHealthy, err := SharedNodeIPAddressDAO.FindAddressIsHealthy(tx, 1)
if err != nil {
t.Fatal(err)
}
t.Log("isHealthy:", isHealthy)
}
func TestNodeIPAddressDAO_UpdateAddressHealthCount(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
isChanged, err := SharedNodeIPAddressDAO.UpdateAddressHealthCount(tx, 1, true, 3, 3)
if err != nil {
t.Fatal(err)
}
t.Log("isChanged:", isChanged)
}

View File

@@ -13,10 +13,13 @@ type NodeIPAddress struct {
CanAccess uint8 `field:"canAccess"` // 是否可以访问
IsOn uint8 `field:"isOn"` // 是否启用
IsUp uint8 `field:"isUp"` // 是否上线
IsHealthy uint8 `field:"isHealthy"` // 是否健康
Thresholds string `field:"thresholds"` // 上线阈值
Connectivity string `field:"connectivity"` // 连通性状态
BackupIP string `field:"backupIP"` // 备用IP
BackupThresholdId uint32 `field:"backupThresholdId"` // 触发备用IP的阈值
CountUp uint32 `field:"countUp"` // UP状态次数
CountDown uint32 `field:"countDown"` // DOWN状态次数
}
type NodeIPAddressOperator struct {
@@ -31,10 +34,13 @@ type NodeIPAddressOperator struct {
CanAccess interface{} // 是否可以访问
IsOn interface{} // 是否启用
IsUp interface{} // 是否上线
IsHealthy interface{} // 是否健康
Thresholds interface{} // 上线阈值
Connectivity interface{} // 连通性状态
BackupIP interface{} // 备用IP
BackupThresholdId interface{} // 触发备用IP的阈值
CountUp interface{} // UP状态次数
CountDown interface{} // DOWN状态次数
}
func NewNodeIPAddressOperator() *NodeIPAddressOperator {

View File

@@ -75,10 +75,26 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nod
op.Day = timeutil.FormatTime("Ymd", createdAt)
op.Hash = hash
op.Count = 1
op.IsRead = level != "error"
err = this.Save(tx, op)
return err
}
// DeleteExpiredLogsWithLevel 清除超出一定日期的某级别日志
func (this *NodeLogDAO) DeleteExpiredLogsWithLevel(tx *dbs.Tx, level string, days int) error {
if days <= 0 {
return errors.New("invalid days '" + strconv.Itoa(days) + "'")
}
date := time.Now().AddDate(0, 0, -days)
expireDay := timeutil.Format("Ymd", date)
_, err := this.Query(tx).
Attr("level", level).
Where("day<=:day").
Param("day", expireDay).
Delete()
return err
}
// DeleteExpiredLogs 清除超出一定日期的日志
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
if days <= 0 {
@@ -94,9 +110,20 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
}
// CountNodeLogs 计算节点日志数量
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, originId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
query := this.Query(tx).
Attr("role", role)
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx,
role string,
nodeId int64,
serverId int64,
originId int64,
dayFrom string,
dayTo string,
keyword string,
level string,
isUnread bool) (int64, error) {
query := this.Query(tx)
if len(role) > 0 {
query.Attr("role", role)
}
if nodeId > 0 {
query.Attr("nodeId", nodeId)
} else {
@@ -128,6 +155,9 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, ser
if len(level) > 0 {
query.Attr("level", level)
}
if isUnread {
query.Attr("isRead", 0)
}
return query.Count()
}
@@ -144,10 +174,13 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
keyword string,
level string,
fixedState configutils.BoolState,
isUnread bool,
offset int64,
size int64) (result []*NodeLog, err error) {
query := this.Query(tx).
Attr("role", role)
query := this.Query(tx)
if len(role) > 0 {
query.Attr("role", role)
}
if nodeId > 0 {
query.Attr("nodeId", nodeId)
} else {
@@ -186,6 +219,9 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
if len(level) > 0 {
query.Attr("level", level)
}
if isUnread {
query.Attr("isRead", 0)
}
_, err = query.
Offset(offset).
Limit(size).
@@ -224,3 +260,32 @@ func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
return nil
}
// CountAllUnreadNodeLogs 计算未读的日志数量
func (this *NodeLogDAO) CountAllUnreadNodeLogs(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
Attr("isRead", false).
Count()
}
// UpdateNodeLogsRead 设置日志为已读
func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, nodeLogIds []int64) error {
for _, logId := range nodeLogIds {
err := this.Query(tx).
Pk(logId).
Set("isRead", true).
UpdateQuickly()
if err != nil {
return err
}
}
return nil
}
// UpdateAllNodeLogsRead 设置所有日志为已读
func (this *NodeLogDAO) UpdateAllNodeLogsRead(tx *dbs.Tx) error {
return this.Query(tx).
Attr("isRead", false).
Set("isRead", true).
UpdateQuickly()
}

View File

@@ -15,6 +15,7 @@ type NodeLog struct {
Hash string `field:"hash"` // 信息内容Hash
Count uint32 `field:"count"` // 重复次数
IsFixed uint8 `field:"isFixed"` // 是否已处理
IsRead uint8 `field:"isRead"` // 是否已读
}
type NodeLogOperator struct {
@@ -31,6 +32,7 @@ type NodeLogOperator struct {
Hash interface{} // 信息内容Hash
Count interface{} // 重复次数
IsFixed interface{} // 是否已处理
IsRead interface{} // 是否已读
}
func NewNodeLogOperator() *NodeLogOperator {

View File

@@ -35,7 +35,7 @@ func init() {
})
}
// 启用条目
// EnableNodePriceItem 启用条目
func (this *NodePriceItemDAO) EnableNodePriceItem(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -44,7 +44,7 @@ func (this *NodePriceItemDAO) EnableNodePriceItem(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableNodePriceItem 禁用条目
func (this *NodePriceItemDAO) DisableNodePriceItem(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -53,7 +53,7 @@ func (this *NodePriceItemDAO) DisableNodePriceItem(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledNodePriceItem 查找启用中的条目
func (this *NodePriceItemDAO) FindEnabledNodePriceItem(tx *dbs.Tx, id int64) (*NodePriceItem, error) {
result, err := this.Query(tx).
Pk(id).
@@ -65,7 +65,7 @@ func (this *NodePriceItemDAO) FindEnabledNodePriceItem(tx *dbs.Tx, id int64) (*N
return result.(*NodePriceItem), err
}
// 根据主键查找名称
// FindNodePriceItemName 根据主键查找名称
func (this *NodePriceItemDAO) FindNodePriceItemName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -73,7 +73,7 @@ func (this *NodePriceItemDAO) FindNodePriceItemName(tx *dbs.Tx, id int64) (strin
FindStringCol("")
}
// 创建价格
// CreateItem 创建价格
func (this *NodePriceItemDAO) CreateItem(tx *dbs.Tx, name string, itemType string, bitsFrom, bitsTo int64) (int64, error) {
op := NewNodePriceItemOperator()
op.Name = name
@@ -85,7 +85,7 @@ func (this *NodePriceItemDAO) CreateItem(tx *dbs.Tx, name string, itemType strin
return this.SaveInt64(tx, op)
}
// 修改价格
// UpdateItem 修改价格
func (this *NodePriceItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, bitsFrom, bitsTo int64) error {
if itemId <= 0 {
return errors.New("invalid itemId")
@@ -98,7 +98,7 @@ func (this *NodePriceItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string,
return this.Save(tx, op)
}
// 列出某个区域的所有价格
// FindAllEnabledRegionPrices 列出某个区域的所有价格
func (this *NodePriceItemDAO) FindAllEnabledRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
_, err = this.Query(tx).
Attr("type", priceType).
@@ -109,7 +109,7 @@ func (this *NodePriceItemDAO) FindAllEnabledRegionPrices(tx *dbs.Tx, priceType s
return
}
// 列出某个区域的所有启用的价格
// FindAllEnabledAndOnRegionPrices 列出某个区域的所有启用的价格
func (this *NodePriceItemDAO) FindAllEnabledAndOnRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
_, err = this.Query(tx).
Attr("type", priceType).
@@ -121,7 +121,7 @@ func (this *NodePriceItemDAO) FindAllEnabledAndOnRegionPrices(tx *dbs.Tx, priceT
return
}
// 根据字节查找付费项目
// SearchItemsWithBytes 根据字节查找付费项目
func (this *NodePriceItemDAO) SearchItemsWithBytes(items []*NodePriceItem, bytes int64) int64 {
bytes *= 8

View File

@@ -35,7 +35,7 @@ func init() {
})
}
// 启用条目
// EnableNodeRegion 启用条目
func (this *NodeRegionDAO) EnableNodeRegion(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -44,7 +44,7 @@ func (this *NodeRegionDAO) EnableNodeRegion(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableNodeRegion 禁用条目
func (this *NodeRegionDAO) DisableNodeRegion(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -53,7 +53,7 @@ func (this *NodeRegionDAO) DisableNodeRegion(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledNodeRegion 查找启用中的条目
func (this *NodeRegionDAO) FindEnabledNodeRegion(tx *dbs.Tx, id int64) (*NodeRegion, error) {
result, err := this.Query(tx).
Pk(id).
@@ -65,7 +65,7 @@ func (this *NodeRegionDAO) FindEnabledNodeRegion(tx *dbs.Tx, id int64) (*NodeReg
return result.(*NodeRegion), err
}
// 根据主键查找名称
// FindNodeRegionName 根据主键查找名称
func (this *NodeRegionDAO) FindNodeRegionName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -73,7 +73,7 @@ func (this *NodeRegionDAO) FindNodeRegionName(tx *dbs.Tx, id int64) (string, err
FindStringCol("")
}
// 创建区域
// CreateRegion 创建区域
func (this *NodeRegionDAO) CreateRegion(tx *dbs.Tx, adminId int64, name string, description string) (int64, error) {
op := NewNodeRegionOperator()
op.AdminId = adminId
@@ -84,7 +84,7 @@ func (this *NodeRegionDAO) CreateRegion(tx *dbs.Tx, adminId int64, name string,
return this.SaveInt64(tx, op)
}
// 修改区域
// UpdateRegion 修改区域
func (this *NodeRegionDAO) UpdateRegion(tx *dbs.Tx, regionId int64, name string, description string, isOn bool) error {
if regionId <= 0 {
return errors.New("invalid regionId")
@@ -97,7 +97,7 @@ func (this *NodeRegionDAO) UpdateRegion(tx *dbs.Tx, regionId int64, name string,
return this.Save(tx, op)
}
// 列出所有区域
// FindAllEnabledRegions 列出所有区域
func (this *NodeRegionDAO) FindAllEnabledRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
_, err = this.Query(tx).
State(NodeRegionStateEnabled).
@@ -108,7 +108,7 @@ func (this *NodeRegionDAO) FindAllEnabledRegions(tx *dbs.Tx) (result []*NodeRegi
return
}
// 列出所有价格
// FindAllEnabledRegionPrices 列出所有价格
func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*NodeRegion, err error) {
_, err = this.Query(tx).
State(NodeRegionStateEnabled).
@@ -120,7 +120,7 @@ func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*Nod
return
}
// 列出所有启用的区域
// FindAllEnabledAndOnRegions 列出所有启用的区域
func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
_, err = this.Query(tx).
State(NodeRegionStateEnabled).
@@ -132,7 +132,7 @@ func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*Nod
return
}
// 排序
// UpdateRegionOrders 排序
func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) error {
order := len(regionIds)
for _, regionId := range regionIds {
@@ -148,7 +148,7 @@ func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) err
return nil
}
// 修改价格项价格
// UpdateRegionItemPrice 修改价格项价格
func (this *NodeRegionDAO) UpdateRegionItemPrice(tx *dbs.Tx, regionId int64, itemId int64, price float32) error {
one, err := this.Query(tx).
Pk(regionId).

View File

@@ -49,7 +49,7 @@ func init() {
}
// CreateNodeTask 创建单个节点任务
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, taskType NodeTaskType) error {
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, taskType NodeTaskType, version int64) error {
if clusterId <= 0 || nodeId <= 0 {
return nil
}
@@ -66,6 +66,7 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
"isDone": 0,
"isOk": 0,
"error": "",
"version": version,
}, maps.Map{
"clusterId": clusterId,
"updatedAt": updatedAt,
@@ -73,6 +74,7 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
"isOk": 0,
"error": "",
"isNotified": 0,
"version": version,
})
return err
}
@@ -97,12 +99,14 @@ func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId in
"isOk": 0,
"isNotified": 0,
"error": "",
"version": time.Now().UnixNano(),
}, maps.Map{
"updatedAt": updatedAt,
"isDone": 0,
"isOk": 0,
"isNotified": 0,
"error": "",
"version": time.Now().UnixNano(),
})
return err
}
@@ -125,8 +129,9 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, tas
return err
}
var version = time.Now().UnixNano()
for _, nodeId := range nodeIds {
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, taskType)
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, taskType, version)
if err != nil {
return err
}
@@ -156,15 +161,16 @@ func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskT
Attr("role", nodeconfigs.NodeRoleDNS).
Attr("clusterId", clusterId).
Param("clusterIdString", types.String(clusterId)).
Where("nodeId> 0").
Where("nodeId > 0").
Attr("type", taskType).
Delete()
if err != nil {
return err
}
var version = time.Now().UnixNano()
for _, nodeId := range nodeIds {
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, taskType)
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, taskType, version)
if err != nil {
return err
}

View File

@@ -11,7 +11,7 @@ func TestNodeTaskDAO_CreateNodeTask(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, NodeTaskTypeConfigChanged)
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, NodeTaskTypeConfigChanged, 0)
if err != nil {
t.Fatal(err)
}

View File

@@ -13,6 +13,7 @@ type NodeTask struct {
IsOk uint8 `field:"isOk"` // 是否已完成
Error string `field:"error"` // 错误信息
IsNotified uint8 `field:"isNotified"` // 是否已通知更新
Version uint64 `field:"version"` // 版本
}
type NodeTaskOperator struct {
@@ -27,6 +28,7 @@ type NodeTaskOperator struct {
IsOk interface{} // 是否已完成
Error interface{} // 错误信息
IsNotified interface{} // 是否已通知更新
Version interface{} // 版本
}
func NewNodeTaskOperator() *NodeTaskOperator {

View File

@@ -58,7 +58,7 @@ func (this *NodeValueDAO) CreateValue(tx *dbs.Tx, clusterId int64, role nodeconf
// DeleteExpiredValues 清除数据
func (this *NodeValueDAO) DeleteExpiredValues(tx *dbs.Tx) error {
// 删除N天之前的所有数据
expiredDays := 100
expiredDays := 7
day := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -expiredDays))
_, err := this.Query(tx).
Where("day<:day").

View File

@@ -2,7 +2,6 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/configs"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
@@ -14,7 +13,6 @@ import (
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
@@ -78,7 +76,7 @@ func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NS
fields["domainId"] = accessLog.NsDomainId
fields["recordId"] = accessLog.NsRecordId
fields["createdAt"] = accessLog.Timestamp
fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId
fields["requestId"] = accessLog.RequestId
content, err := json.Marshal(accessLog)
if err != nil {
@@ -259,7 +257,7 @@ func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, siz
// FindAccessLogWithRequestId 根据请求ID获取访问日志
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
return nil, errors.New("invalid requestId")
}

View File

@@ -3,13 +3,13 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
@@ -220,12 +220,12 @@ func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, add
}
// ComposeOriginConfig 将源站信息转换为配置
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap maps.Map) (*serverconfigs.OriginConfig, error) {
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap *utils.CacheMap) (*serverconfigs.OriginConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(originId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.OriginConfig), nil
}
@@ -358,7 +358,9 @@ func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap
// TODO
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -0,0 +1,273 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
PlanStateEnabled = 1 // 已启用
PlanStateDisabled = 0 // 已禁用
)
type PlanDAO dbs.DAO
func NewPlanDAO() *PlanDAO {
return dbs.NewDAO(&PlanDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgePlans",
Model: new(Plan),
PkName: "id",
},
}).(*PlanDAO)
}
var SharedPlanDAO *PlanDAO
func init() {
dbs.OnReady(func() {
SharedPlanDAO = NewPlanDAO()
})
}
// EnablePlan 启用条目
func (this *PlanDAO) EnablePlan(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", PlanStateEnabled).
Update()
return err
}
// DisablePlan 禁用条目
func (this *PlanDAO) DisablePlan(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", PlanStateDisabled).
Update()
if err != nil {
return err
}
return this.NotifyUpdate(tx, id)
}
// FindEnabledPlan 查找启用中的条目
func (this *PlanDAO) FindEnabledPlan(tx *dbs.Tx, id int64) (*Plan, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", PlanStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*Plan), err
}
// FindPlanName 根据主键查找名称
func (this *PlanDAO) FindPlanName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreatePlan 创建套餐
func (this *PlanDAO) CreatePlan(tx *dbs.Tx, name string, clusterId int64, trafficLimitJSON []byte, featuresJSON []byte, priceType serverconfigs.PlanPriceType, trafficPriceJSON []byte, monthlyPrice float32, seasonallyPrice float32, yearlyPrice float32) (int64, error) {
var op = NewPlanOperator()
op.Name = name
op.ClusterId = clusterId
if len(trafficLimitJSON) > 0 {
op.TrafficLimit = trafficLimitJSON
}
if len(featuresJSON) > 0 {
op.Features = featuresJSON
}
op.PriceType = priceType
if len(trafficPriceJSON) > 0 {
op.TrafficPrice = trafficPriceJSON
}
if monthlyPrice >= 0 {
op.MonthlyPrice = monthlyPrice
}
if seasonallyPrice >= 0 {
op.SeasonallyPrice = seasonallyPrice
}
if yearlyPrice >= 0 {
op.YearlyPrice = yearlyPrice
}
op.IsOn = true
op.State = PlanStateEnabled
return this.SaveInt64(tx, op)
}
// UpdatePlan 修改套餐
func (this *PlanDAO) UpdatePlan(tx *dbs.Tx, planId int64, name string, isOn bool, clusterId int64, trafficLimitJSON []byte, featuresJSON []byte, priceType serverconfigs.PlanPriceType, trafficPriceJSON []byte, monthlyPrice float32, seasonallyPrice float32, yearlyPrice float32) error {
if planId <= 0 {
return errors.New("invalid planId")
}
// 检查集群有无变化
oldClusterId, err := this.Query(tx).
Pk(planId).
Result("clusterId").
FindInt64Col(0)
if err != nil {
return err
}
var op = NewPlanOperator()
op.Id = planId
op.Name = name
op.IsOn = isOn
op.ClusterId = clusterId
if len(trafficLimitJSON) > 0 {
op.TrafficLimit = trafficLimitJSON
}
if len(featuresJSON) > 0 {
op.Features = featuresJSON
}
op.PriceType = priceType
if len(trafficPriceJSON) > 0 {
op.TrafficPrice = trafficPriceJSON
}
if monthlyPrice >= 0 {
op.MonthlyPrice = monthlyPrice
} else {
op.MonthlyPrice = 0
}
if seasonallyPrice >= 0 {
op.SeasonallyPrice = seasonallyPrice
} else {
op.SeasonallyPrice = 0
}
if yearlyPrice >= 0 {
op.YearlyPrice = yearlyPrice
} else {
op.YearlyPrice = 0
}
err = this.Save(tx, op)
if err != nil {
return err
}
if oldClusterId != clusterId {
// 修改服务所属集群
err = SharedServerDAO.UpdateServersClusterIdWithPlanId(tx, planId, clusterId)
if err != nil {
return err
}
err = SharedNodeClusterDAO.NotifyUpdate(tx, oldClusterId)
if err != nil {
return err
}
}
return this.NotifyUpdate(tx, planId)
}
// CountAllEnabledPlans 计算套餐的数量
func (this *PlanDAO) CountAllEnabledPlans(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(PlanStateEnabled).
Count()
}
// ListEnabledPlans 列出单页套餐
func (this *PlanDAO) ListEnabledPlans(tx *dbs.Tx, offset int64, size int64) (result []*Plan, err error) {
_, err = this.Query(tx).
State(PlanStateEnabled).
Offset(offset).
Limit(size).
Slice(&result).
Desc("order").
DescPk().
FindAll()
return
}
// FindAllEnabledPlans 查找所有可用套餐
func (this *PlanDAO) FindAllEnabledPlans(tx *dbs.Tx) (result []*Plan, err error) {
_, err = this.Query(tx).
State(PlanStateEnabled).
Slice(&result).
FindAll()
return
}
// SortPlans 增加排序
func (this *PlanDAO) SortPlans(tx *dbs.Tx, planIds []int64) error {
if len(planIds) == 0 {
return nil
}
var order = len(planIds)
for _, planId := range planIds {
err := this.Query(tx).
Pk(planId).
Set("order", order).
UpdateQuickly()
if err != nil {
return err
}
order--
}
return nil
}
// FindEnabledPlanTrafficLimit 获取套餐的流量限制
func (this *PlanDAO) FindEnabledPlanTrafficLimit(tx *dbs.Tx, planId int64, cacheMap *utils.CacheMap) (*serverconfigs.TrafficLimitConfig, error) {
var cacheKey = this.Table + ":FindEnabledPlanTrafficLimit:" + types.String(planId)
if cacheMap != nil {
cache, _ := cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.TrafficLimitConfig), nil
}
}
trafficLimit, err := this.Query(tx).
Pk(planId).
State(PlanStateEnabled).
Result("trafficLimit").
FindStringCol("")
if err != nil {
return nil, err
}
if len(trafficLimit) == 0 {
return nil, nil
}
var config = &serverconfigs.TrafficLimitConfig{}
err = json.Unmarshal([]byte(trafficLimit), config)
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
// NotifyUpdate 通知变更
func (this *PlanDAO) NotifyUpdate(tx *dbs.Tx, planId int64) error {
// 这里不要加入状态参数,因为需要适应删除后的更新
clusterId, err := this.Query(tx).
Pk(planId).
Result("clusterId").
FindInt64Col(0)
if err != nil {
return err
}
if clusterId > 0 {
return SharedNodeClusterDAO.NotifyUpdate(tx, clusterId)
}
return nil
}

View File

@@ -0,0 +1,6 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,38 @@
package models
// Plan 用户套餐
type Plan struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
Name string `field:"name"` // 套餐名
ClusterId uint32 `field:"clusterId"` // 集群ID
TrafficLimit string `field:"trafficLimit"` // 流量限制
Features string `field:"features"` // 允许的功能
TrafficPrice string `field:"trafficPrice"` // 流量价格设定
MonthlyPrice float64 `field:"monthlyPrice"` // 月付
SeasonallyPrice float64 `field:"seasonallyPrice"` // 季付
YearlyPrice float64 `field:"yearlyPrice"` // 年付
PriceType string `field:"priceType"` // 价格类型
Order uint32 `field:"order"` // 排序
State uint8 `field:"state"` // 状态
}
type PlanOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
Name interface{} // 套餐名
ClusterId interface{} // 集群ID
TrafficLimit interface{} // 流量限制
Features interface{} // 允许的功能
TrafficPrice interface{} // 流量价格设定
MonthlyPrice interface{} // 月付
SeasonallyPrice interface{} // 季付
YearlyPrice interface{} // 年付
PriceType interface{} // 价格类型
Order interface{} // 排序
State interface{} // 状态
}
func NewPlanOperator() *PlanOperator {
return &PlanOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -7,6 +7,7 @@ import (
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
"strconv"
)
const (
@@ -119,7 +120,7 @@ func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId in
cityId, err := this.Query(tx).
Attr("provinceId", provinceId).
Where("JSON_CONTAINS(codes, :cityName)").
Param("cityName", "\""+cityName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
Param("cityName", strconv.Quote(cityName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {

View File

@@ -7,6 +7,7 @@ import (
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
"github.com/mozillazg/go-pinyin"
"strconv"
"strings"
)
@@ -88,7 +89,7 @@ func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string)
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
return this.Query(tx).
Where("JSON_CONTAINS(codes, :countryName)").
Param("countryName", "\""+countryName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
Param("countryName", strconv.Quote(countryName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
}

View File

@@ -5,6 +5,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"strconv"
)
const (
@@ -85,7 +86,7 @@ func (this *RegionProviderDAO) FindProviderIdWithNameCacheable(tx *dbs.Tx, provi
providerId, err := this.Query(tx).
Where("JSON_CONTAINS(codes, :providerName)").
Param("providerName", "\""+providerName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
Param("providerName", strconv.Quote(providerName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {

View File

@@ -7,6 +7,7 @@ import (
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
"strconv"
)
const (
@@ -88,7 +89,7 @@ func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int6
return this.Query(tx).
Attr("countryId", countryId).
Where("JSON_CONTAINS(codes, :provinceName)").
Param("provinceName", "\""+provinceName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
Param("provinceName", strconv.Quote(provinceName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
}

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql"
@@ -80,12 +81,12 @@ func (this *ReverseProxyDAO) FindEnabledReverseProxy(tx *dbs.Tx, id int64) (*Rev
}
// ComposeReverseProxyConfig 根据ID组合配置
func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyId int64, cacheMap maps.Map) (*serverconfigs.ReverseProxyConfig, error) {
func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyId int64, cacheMap *utils.CacheMap) (*serverconfigs.ReverseProxyConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(reverseProxyId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*serverconfigs.ReverseProxyConfig), nil
}
@@ -190,7 +191,19 @@ func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyI
config.IdleTimeout = idleTimeout
}
cacheMap[cacheKey] = config
// PROXY Protocol
if IsNotNull(reverseProxy.ProxyProtocol) {
var proxyProtocolConfig = &serverconfigs.ProxyProtocolConfig{}
err = json.Unmarshal([]byte(reverseProxy.ProxyProtocol), proxyProtocolConfig)
if err != nil {
return nil, err
}
config.ProxyProtocol = proxyProtocolConfig
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
@@ -286,7 +299,20 @@ func (this *ReverseProxyDAO) UpdateReverseProxyBackupOrigins(tx *dbs.Tx, reverse
}
// UpdateReverseProxy 修改是否启用
func (this *ReverseProxyDAO) UpdateReverseProxy(tx *dbs.Tx, reverseProxyId int64, requestHostType int8, requestHost string, requestURI string, stripPrefix string, autoFlush bool, addHeaders []string, connTimeout *shared.TimeDuration, readTimeout *shared.TimeDuration, idleTimeout *shared.TimeDuration, maxConns int32, maxIdleConns int32) error {
func (this *ReverseProxyDAO) UpdateReverseProxy(tx *dbs.Tx,
reverseProxyId int64,
requestHostType int8,
requestHost string,
requestURI string,
stripPrefix string,
autoFlush bool,
addHeaders []string,
connTimeout *shared.TimeDuration,
readTimeout *shared.TimeDuration,
idleTimeout *shared.TimeDuration,
maxConns int32,
maxIdleConns int32,
proxyProtocolJSON []byte) error {
if reverseProxyId <= 0 {
return errors.New("invalid reverseProxyId")
}
@@ -345,6 +371,10 @@ func (this *ReverseProxyDAO) UpdateReverseProxy(tx *dbs.Tx, reverseProxyId int64
op.MaxIdleConns = 0
}
if len(proxyProtocolJSON) > 0 {
op.ProxyProtocol = proxyProtocolJSON
}
err = this.Save(tx, op)
if err != nil {
return err

View File

@@ -1,6 +1,6 @@
package models
// 反向代理配置
// ReverseProxy 反向代理配置
type ReverseProxy struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
@@ -23,6 +23,7 @@ type ReverseProxy struct {
IdleTimeout string `field:"idleTimeout"` // 空闲超时时间
MaxConns uint32 `field:"maxConns"` // 最大并发连接数
MaxIdleConns uint32 `field:"maxIdleConns"` // 最大空闲连接数
ProxyProtocol string `field:"proxyProtocol"` // Proxy Protocol配置
}
type ReverseProxyOperator struct {
@@ -47,6 +48,7 @@ type ReverseProxyOperator struct {
IdleTimeout interface{} // 空闲超时时间
MaxConns interface{} // 最大并发连接数
MaxIdleConns interface{} // 最大空闲连接数
ProxyProtocol interface{} // Proxy Protocol配置
}
func NewReverseProxyOperator() *ReverseProxyOperator {

View File

@@ -1,6 +1,7 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
@@ -53,12 +54,14 @@ func init() {
// SaveStats 提交数据
func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailyStat) error {
var serverUserMap = map[int64]int64{} // serverId => userId
var cacheMap = utils.NewCacheMap()
for _, stat := range stats {
day := timeutil.FormatTime("Ymd", stat.CreatedAt)
hour := timeutil.FormatTime("YmdH", stat.CreatedAt)
timeFrom := timeutil.FormatTime("His", stat.CreatedAt)
timeTo := timeutil.FormatTime("His", stat.CreatedAt+5*60-1) // 5分钟
// 所属用户
serverUserId, ok := serverUserMap[stat.ServerId]
if !ok {
userId, err := SharedServerDAO.FindServerUserId(tx, stat.ServerId)
@@ -85,6 +88,7 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
"countCachedRequests": stat.CountCachedRequests,
"countAttackRequests": stat.CountAttackRequests,
"attackBytes": stat.AttackBytes,
"planId": stat.PlanId,
"day": day,
"hour": hour,
"timeFrom": timeFrom,
@@ -96,11 +100,32 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
"countCachedRequests": dbs.SQL("countCachedRequests+:countCachedRequests"),
"countAttackRequests": dbs.SQL("countAttackRequests+:countAttackRequests"),
"attackBytes": dbs.SQL("attackBytes+:attackBytes"),
"planId": stat.PlanId,
})
if err != nil {
return err
}
// 更新流量限制状态
if stat.CheckTrafficLimiting {
trafficLimitConfig, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, stat.ServerId, cacheMap)
if err != nil {
return err
}
if trafficLimitConfig != nil && trafficLimitConfig.IsOn && !trafficLimitConfig.IsEmpty() {
err = SharedServerDAO.IncreaseServerTotalTraffic(tx, stat.ServerId, stat.Bytes)
if err != nil {
return err
}
err = SharedServerDAO.UpdateServerTrafficLimitStatus(tx, trafficLimitConfig, stat.ServerId, false)
if err != nil {
return err
}
}
}
}
return nil
}
@@ -116,6 +141,30 @@ func (this *ServerDailyStatDAO) SumUserMonthly(tx *dbs.Tx, userId int64, regionI
SumInt64("bytes", 0)
}
// SumUserMonthlyWithoutPlan 根据用户计算某月合计并排除套餐
// month 格式为YYYYMM
func (this *ServerDailyStatDAO) SumUserMonthlyWithoutPlan(tx *dbs.Tx, userId int64, regionId int64, month string) (int64, error) {
query := this.Query(tx)
if regionId > 0 {
query.Attr("regionId", regionId)
}
return query.
Attr("planId", 0).
Between("day", month+"01", month+"32").
Attr("userId", userId).
SumInt64("bytes", 0)
}
// SumUserMonthlyFee 计算用户某个月费用
// month 格式为YYYYMM
func (this *ServerDailyStatDAO) SumUserMonthlyFee(tx *dbs.Tx, userId int64, month string) (float64, error) {
return this.Query(tx).
Attr("userId", userId).
Between("day", month+"01", month+"32").
Gt("fee", 0).
Sum("fee", 0)
}
// SumUserMonthlyPeek 获取某月带宽峰值
// month 格式为YYYYMM
func (this *ServerDailyStatDAO) SumUserMonthlyPeek(tx *dbs.Tx, userId int64, regionId int64, month string) (int64, error) {
@@ -233,7 +282,7 @@ func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day str
stat = &pb.ServerDailyStat{}
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
return
return nil, errors.New("invalid day '" + day + "'")
}
one, _, err := this.Query(tx).
@@ -258,6 +307,37 @@ func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day str
return
}
// SumMonthlyStat 获取某月内的流量
// month 格式为YYYYMM
func (this *ServerDailyStatDAO) SumMonthlyStat(tx *dbs.Tx, serverId int64, month string) (stat *pb.ServerDailyStat, err error) {
stat = &pb.ServerDailyStat{}
if !regexp.MustCompile(`^\d{6}$`).MatchString(month) {
return
}
one, _, err := this.Query(tx).
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests, SUM(countAttackRequests) AS countAttackRequests, SUM(attackBytes) AS attackBytes").
Attr("serverId", serverId).
Between("day", month+"01", month+"31").
FindOne()
if err != nil {
return nil, err
}
if one == nil {
return
}
stat.Bytes = one.GetInt64("bytes")
stat.CachedBytes = one.GetInt64("cachedBytes")
stat.CountRequests = one.GetInt64("countRequests")
stat.CountCachedRequests = one.GetInt64("countCachedRequests")
stat.CountAttackRequests = one.GetInt64("countAttackRequests")
stat.AttackBytes = one.GetInt64("attackBytes")
return
}
// FindDailyStats 按天统计
func (this *ServerDailyStatDAO) FindDailyStats(tx *dbs.Tx, serverId int64, dayFrom string, dayTo string) (result []*ServerDailyStat, err error) {
ones, err := this.Query(tx).
@@ -291,6 +371,17 @@ func (this *ServerDailyStatDAO) FindDailyStats(tx *dbs.Tx, serverId int64, dayFr
return
}
// FindMonthlyStatsWithPlan 查找某月有套餐的流量
// month YYYYMM
func (this *ServerDailyStatDAO) FindMonthlyStatsWithPlan(tx *dbs.Tx, month string) (result []*ServerDailyStat, err error) {
_, err = this.Query(tx).
Between("day", month+"01", month+"32").
Gt("planId", 0).
Slice(&result).
FindAll()
return
}
// FindHourlyStats 按小时统计
func (this *ServerDailyStatDAO) FindHourlyStats(tx *dbs.Tx, serverId int64, hourFrom string, hourTo string) (result []*ServerDailyStat, err error) {
ones, err := this.Query(tx).
@@ -336,6 +427,14 @@ func (this *ServerDailyStatDAO) FindTopUserStats(tx *dbs.Tx, hourFrom string, ho
return
}
// UpdateStatFee 设置费用
func (this *ServerDailyStatDAO) UpdateStatFee(tx *dbs.Tx, statId int64, fee float32) error {
return this.Query(tx).
Pk(statId).
Set("fee", fee).
UpdateQuickly()
}
// Clean 清理历史数据
func (this *ServerDailyStatDAO) Clean(tx *dbs.Tx, days int) error {
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))

View File

@@ -2,21 +2,23 @@ package models
// ServerDailyStat 计费流量统计
type ServerDailyStat struct {
Id uint64 `field:"id"` // ID
UserId uint32 `field:"userId"` // 用户ID
ServerId uint32 `field:"serverId"` // 服务ID
RegionId uint32 `field:"regionId"` // 区域ID
Bytes uint64 `field:"bytes"` // 流量
CachedBytes uint64 `field:"cachedBytes"` // 缓存的流量
CountRequests uint64 `field:"countRequests"` // 请求数
CountCachedRequests uint64 `field:"countCachedRequests"` // 缓存的请求数
CountAttackRequests uint64 `field:"countAttackRequests"` // 攻击请求数
AttackBytes uint64 `field:"attackBytes"` // 攻击流量
Day string `field:"day"` // 日期YYYYMMDD
Hour string `field:"hour"` // YYYYMMDDHH
TimeFrom string `field:"timeFrom"` // 开始时间HHMMSS
TimeTo string `field:"timeTo"` // 结束时间
IsCharged uint8 `field:"isCharged"` // 是否已计算费用
Id uint64 `field:"id"` // ID
UserId uint32 `field:"userId"` // 用户ID
ServerId uint32 `field:"serverId"` // 服务ID
RegionId uint32 `field:"regionId"` // 区域ID
Bytes uint64 `field:"bytes"` // 流量
CachedBytes uint64 `field:"cachedBytes"` // 缓存的流量
CountRequests uint64 `field:"countRequests"` // 请求数
CountCachedRequests uint64 `field:"countCachedRequests"` // 缓存的请求数
CountAttackRequests uint64 `field:"countAttackRequests"` // 攻击请求数
AttackBytes uint64 `field:"attackBytes"` // 攻击流量
Day string `field:"day"` // 日期YYYYMMDD
Hour string `field:"hour"` // YYYYMMDDHH
TimeFrom string `field:"timeFrom"` // 开始时间HHMMSS
TimeTo string `field:"timeTo"` // 结束时间
IsCharged uint8 `field:"isCharged"` // 是否已计算费用
PlanId uint64 `field:"planId"` // 套餐ID
Fee float64 `field:"fee"` // 费用
}
type ServerDailyStatOperator struct {
@@ -35,6 +37,8 @@ type ServerDailyStatOperator struct {
TimeFrom interface{} // 开始时间HHMMSS
TimeTo interface{} // 结束时间
IsCharged interface{} // 是否已计算费用
PlanId interface{} // 套餐ID
Fee interface{} // 费用
}
func NewServerDailyStatOperator() *ServerDailyStatOperator {

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -16,6 +17,7 @@ import (
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"strconv"
"strings"
@@ -50,7 +52,7 @@ func init() {
// Init 初始化
func (this *ServerDAO) Init() {
this.DAOObject.Init()
_ = this.DAOObject.Init()
// 这里不处理增删改事件是为了避免Server修改本身的时候也要触发别的Server变更
}
@@ -72,6 +74,13 @@ func (this *ServerDAO) DisableServer(tx *dbs.Tx, serverId int64) (err error) {
if err != nil {
return err
}
// 删除对应的操作
err = this.NotifyDisable(tx, serverId)
if err != nil {
return err
}
err = this.NotifyUpdate(tx, serverId)
if err != nil {
return err
@@ -147,7 +156,8 @@ func (this *ServerDAO) CreateServer(tx *dbs.Tx,
clusterId int64,
includeNodesJSON string,
excludeNodesJSON string,
groupIds []int64) (serverId int64, err error) {
groupIds []int64,
userPlanId int64) (serverId int64, err error) {
op := NewServerOperator()
op.UserId = userId
op.AdminId = adminId
@@ -209,6 +219,8 @@ func (this *ServerDAO) CreateServer(tx *dbs.Tx,
}
op.DnsName = dnsName
op.UserPlanId = userPlanId
op.Version = 1
op.IsOn = 1
op.State = ServerStateEnabled
@@ -472,10 +484,26 @@ func (this *ServerDAO) UpdateServerWeb(tx *dbs.Tx, serverId int64, webId int64)
return this.NotifyUpdate(tx, serverId)
}
// UpdateServerDNS 修改DNS设置
func (this *ServerDAO) UpdateServerDNS(tx *dbs.Tx, serverId int64, supportCNAME bool) error {
if serverId <= 0 {
return errors.New("invalid serverId")
}
var op = NewServerOperator()
op.Id = serverId
op.SupportCNAME = supportCNAME
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, serverId)
}
// InitServerWeb 初始化Web配置
func (this *ServerDAO) InitServerWeb(tx *dbs.Tx, serverId int64) (int64, error) {
if serverId <= 0 {
return 0, errors.New("serverId should not be smaller than 0")
return 0, errors.New("invalid serverId")
}
adminId, userId, err := this.FindServerAdminIdAndUserId(tx, serverId)
@@ -674,7 +702,8 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
if len(keyword) > 0 {
if regexp.MustCompile(`^\d+$`).MatchString(keyword) {
query.Where("(name LIKE :keyword OR serverNames LIKE :keyword OR JSON_CONTAINS(http, :portRange, '$.listen') OR JSON_CONTAINS(https, :portRange, '$.listen') OR JSON_CONTAINS(tcp, :portRange, '$.listen') OR JSON_CONTAINS(tls, :portRange, '$.listen'))").
Param("portRange", string(maps.Map{"portRange": keyword}.AsJSON()))
Param("portRange", string(maps.Map{"portRange": keyword}.AsJSON())).
Param("keyword", "%"+keyword+"%")
} else {
query.Where("(name LIKE :keyword OR serverNames LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
@@ -805,7 +834,7 @@ func (this *ServerDAO) FindServerNodeFilters(tx *dbs.Tx, serverId int64) (isOk b
}
// ComposeServerConfigWithServerId 构造服务的Config
func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int64) (*serverconfigs.ServerConfig, error) {
func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int64, forNode bool) (*serverconfigs.ServerConfig, error) {
server, err := this.FindEnabledServer(tx, serverId)
if err != nil {
return nil, err
@@ -813,17 +842,23 @@ func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int6
if server == nil {
return nil, ErrNotFound
}
return this.ComposeServerConfig(tx, server, nil)
return this.ComposeServerConfig(tx, server, nil, forNode)
}
// ComposeServerConfig 构造服务的Config
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap maps.Map) (*serverconfigs.ServerConfig, error) {
// forNode 是否是节点请求
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap *utils.CacheMap, forNode bool) (*serverconfigs.ServerConfig, error) {
if server == nil {
return nil, ErrNotFound
}
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":ComposeServerConfig:" + types.String(server.Id)
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*serverconfigs.ServerConfig), nil
}
config := &serverconfigs.ServerConfig{}
@@ -859,6 +894,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
}
// CNAME
config.SupportCNAME = server.SupportCNAME == 1
if server.ClusterId > 0 && len(server.DnsName) > 0 {
clusterDNS, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, int64(server.ClusterId), cacheMap)
if err != nil {
@@ -1008,6 +1044,69 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
config.HTTPCachePolicyId = httpCachePolicyId
}
// traffic limit
if len(server.TrafficLimit) > 0 {
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
err = json.Unmarshal([]byte(server.TrafficLimit), trafficLimitConfig)
if err != nil {
return nil, err
}
config.TrafficLimit = trafficLimitConfig
}
// 用户套餐
if forNode && server.UserPlanId > 0 {
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, int64(server.UserPlanId), cacheMap)
if err != nil {
return nil, err
}
if userPlan != nil && userPlan.IsOn == 1 {
if len(userPlan.DayTo) == 0 {
userPlan.DayTo = DefaultUserPlanMaxDay
}
// 套餐是否依然有效
plan, err := SharedPlanDAO.FindEnabledPlan(tx, int64(userPlan.PlanId))
if err != nil {
return nil, err
}
if plan != nil {
config.UserPlan = &serverconfigs.UserPlanConfig{
DayTo: userPlan.DayTo,
Plan: &serverconfigs.PlanConfig{
Id: int64(plan.Id),
},
}
if len(plan.TrafficLimit) > 0 && (config.TrafficLimit == nil || !config.TrafficLimit.IsOn) {
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
err = json.Unmarshal([]byte(plan.TrafficLimit), trafficLimitConfig)
if err != nil {
return nil, err
}
config.TrafficLimit = trafficLimitConfig
}
}
}
}
if config.TrafficLimit != nil && config.TrafficLimit.IsOn && !config.TrafficLimit.IsEmpty() {
if len(server.TrafficLimitStatus) > 0 {
var status = &serverconfigs.TrafficLimitStatus{}
err = json.Unmarshal([]byte(server.TrafficLimitStatus), status)
if err != nil {
return nil, err
}
if status.IsValid() {
config.TrafficLimitStatus = status
}
}
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
@@ -1190,6 +1289,54 @@ func (this *ServerDAO) FindAllServersDNSWithClusterId(tx *dbs.Tx, clusterId int6
return
}
// FindAllEnabledServersWithDomain 根据域名查找服务
func (this *ServerDAO) FindAllEnabledServersWithDomain(tx *dbs.Tx, domain string) (result []*Server, err error) {
if len(domain) == 0 {
return
}
_, err = this.Query(tx).
State(ServerStateEnabled).
Where("(JSON_CONTAINS(serverNames, :domain1) OR JSON_CONTAINS(serverNames, :domain2))").
Param("domain1", maps.Map{"name": domain}.AsJSON()).
Param("domain2", maps.Map{"subNames": domain}.AsJSON()).
Slice(&result).
DescPk().
FindAll()
if err != nil {
return nil, err
}
// 支持泛解析
var countPieces = strings.Count(domain, ".")
for {
var index = strings.Index(domain, ".")
if index > 0 {
domain = domain[index+1:]
var search = strings.Repeat("*.", countPieces-strings.Count(domain, ".")) + domain
_, err = this.Query(tx).
State(ServerStateEnabled).
Where("(JSON_CONTAINS(serverNames, :domain1) OR JSON_CONTAINS(serverNames, :domain2))").
Param("domain1", maps.Map{"name": search}.AsJSON()).
Param("domain2", maps.Map{"subNames": search}.AsJSON()).
Slice(&result).
DescPk().
FindAll()
if err != nil {
return
}
if len(result) > 0 {
return
}
} else {
break
}
}
return
}
// GenerateServerDNSName 重新生成子域名
func (this *ServerDAO) GenerateServerDNSName(tx *dbs.Tx, serverId int64) (string, error) {
if serverId <= 0 {
@@ -1236,6 +1383,18 @@ func (this *ServerDAO) FindServerDNSName(tx *dbs.Tx, serverId int64) (string, er
FindStringCol("")
}
// FindServerSupportCNAME 查询服务是否支持CNAME
func (this *ServerDAO) FindServerSupportCNAME(tx *dbs.Tx, serverId int64) (bool, error) {
supportCNAME, err := this.Query(tx).
Pk(serverId).
Result("supportCNAME").
FindIntCol(0)
if err != nil {
return false, err
}
return supportCNAME == 1, nil
}
// FindStatelessServerDNS 查询服务的DNS相关信息并且不关注状态
func (this *ServerDAO) FindStatelessServerDNS(tx *dbs.Tx, serverId int64) (*Server, error) {
one, err := this.Query(tx).
@@ -1275,6 +1434,14 @@ func (this *ServerDAO) FindServerUserId(tx *dbs.Tx, serverId int64) (userId int6
return one.GetInt64("userId"), nil
}
// FindServerUserPlanId 查找服务的套餐ID
func (this *ServerDAO) FindServerUserPlanId(tx *dbs.Tx, serverId int64) (userPlanId int64, err error) {
return this.Query(tx).
Pk(serverId).
Result("userPlanId").
FindInt64Col(0)
}
// CheckUserServer 检查用户服务
func (this *ServerDAO) CheckUserServer(tx *dbs.Tx, userId int64, serverId int64) error {
if serverId <= 0 || userId <= 0 {
@@ -1661,6 +1828,300 @@ func (this *ServerDAO) NotifyServerPortsUpdate(tx *dbs.Tx, serverId int64) error
UpdateQuickly()
}
// FindServerTrafficLimitConfig 查找服务的流量限制
func (this *ServerDAO) FindServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, cacheMap *utils.CacheMap) (*serverconfigs.TrafficLimitConfig, error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindServerTrafficLimitConfig:" + types.String(serverId)
result, ok := cacheMap.Get(cacheKey)
if ok {
return result.(*serverconfigs.TrafficLimitConfig), nil
}
serverOne, err := this.Query(tx).
Pk(serverId).
Result("trafficLimit").
Find()
if err != nil {
return nil, err
}
var limit = &serverconfigs.TrafficLimitConfig{}
if serverOne == nil {
return limit, nil
}
var trafficLimit = serverOne.(*Server).TrafficLimit
if len(trafficLimit) > 0 {
err = json.Unmarshal([]byte(trafficLimit), limit)
if err != nil {
return nil, err
}
}
if cacheMap != nil {
cacheMap.Put(cacheKey, limit)
}
return limit, nil
}
// CalculateServerTrafficLimitConfig 计算服务的流量限制
// TODO 优化性能
func (this *ServerDAO) CalculateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, cacheMap *utils.CacheMap) (*serverconfigs.TrafficLimitConfig, error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindServerTrafficLimitConfig:" + types.String(serverId)
result, ok := cacheMap.Get(cacheKey)
if ok {
return result.(*serverconfigs.TrafficLimitConfig), nil
}
serverOne, err := this.Query(tx).
Pk(serverId).
Result("trafficLimit", "userPlanId").
Find()
if err != nil {
return nil, err
}
var limitConfig = &serverconfigs.TrafficLimitConfig{}
if serverOne == nil {
return limitConfig, nil
}
var trafficLimit = serverOne.(*Server).TrafficLimit
var userPlanId = int64(serverOne.(*Server).UserPlanId)
if len(trafficLimit) == 0 {
if userPlanId > 0 {
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, cacheMap)
if err != nil {
return nil, err
}
if userPlan != nil {
planLimit, err := SharedPlanDAO.FindEnabledPlanTrafficLimit(tx, int64(userPlan.PlanId), cacheMap)
if err != nil {
return nil, err
}
if planLimit != nil {
return planLimit, nil
}
}
}
return limitConfig, nil
}
err = json.Unmarshal([]byte(trafficLimit), limitConfig)
if err != nil {
return nil, err
}
if !limitConfig.IsOn {
if userPlanId > 0 {
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, cacheMap)
if err != nil {
return nil, err
}
if userPlan != nil {
planLimit, err := SharedPlanDAO.FindEnabledPlanTrafficLimit(tx, int64(userPlan.PlanId), cacheMap)
if err != nil {
return nil, err
}
if planLimit != nil {
return planLimit, nil
}
}
}
}
if cacheMap != nil {
cacheMap.Put(cacheKey, limitConfig)
}
return limitConfig, nil
}
// UpdateServerTrafficLimitConfig 修改服务的流量限制
func (this *ServerDAO) UpdateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, trafficLimitConfig *serverconfigs.TrafficLimitConfig) error {
if serverId <= 0 {
return errors.New("invalid serverId")
}
limitJSON, err := json.Marshal(trafficLimitConfig)
if err != nil {
return err
}
err = this.Query(tx).
Pk(serverId).
Set("trafficLimit", limitJSON).
UpdateQuickly()
if err != nil {
return err
}
// 更新状态
return this.UpdateServerTrafficLimitStatus(tx, trafficLimitConfig, serverId, true)
}
func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitConfig *serverconfigs.TrafficLimitConfig, serverId int64, isUpdatingConfig bool) error {
if !trafficLimitConfig.IsOn {
if isUpdatingConfig {
return this.NotifyUpdate(tx, serverId)
}
return nil
}
serverOne, err := this.Query(tx).
Pk(serverId).
Result("trafficLimitStatus", "totalTraffic", "totalDailyTraffic", "totalMonthlyTraffic", "trafficDay", "trafficMonth").
Find()
if err != nil {
return err
}
if serverOne == nil {
return nil
}
var server = serverOne.(*Server)
var oldStatus = &serverconfigs.TrafficLimitStatus{}
if len(server.TrafficLimitStatus) > 0 {
err = json.Unmarshal([]byte(server.TrafficLimitStatus), oldStatus)
if err != nil {
return err
}
// 如果已经达到限制了,而且还在有效期,那就没必要再更新
if !isUpdatingConfig && oldStatus.IsValid() {
return nil
}
}
var untilDay = ""
// daily
if trafficLimitConfig.DailyBytes() > 0 {
if server.TrafficDay == timeutil.Format("Ymd") && server.TotalDailyTraffic >= float64(trafficLimitConfig.DailyBytes())/1024/1024/1024 {
untilDay = timeutil.Format("Ymd")
}
}
// monthly
if server.TrafficMonth == timeutil.Format("Ym") && trafficLimitConfig.MonthlyBytes() > 0 {
if server.TotalMonthlyTraffic >= float64(trafficLimitConfig.MonthlyBytes())/1024/1024/1024 {
untilDay = timeutil.Format("Ym32")
}
}
// totally
if trafficLimitConfig.TotalBytes() > 0 {
if server.TotalTraffic >= float64(trafficLimitConfig.TotalBytes())/1024/1024/1024 {
untilDay = "30000101"
}
}
var isChanged = oldStatus.UntilDay != untilDay
if isChanged {
statusJSON, err := json.Marshal(&serverconfigs.TrafficLimitStatus{UntilDay: untilDay})
if err != nil {
return err
}
err = this.Query(tx).
Pk(serverId).
Set("trafficLimitStatus", statusJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, serverId)
}
if isUpdatingConfig {
return this.NotifyUpdate(tx, serverId)
}
return nil
}
// IncreaseServerTotalTraffic 增加服务的总流量
func (this *ServerDAO) IncreaseServerTotalTraffic(tx *dbs.Tx, serverId int64, bytes int64) error {
var gb = float64(bytes) / 1024 / 1024 / 1024
var day = timeutil.Format("Ymd")
var month = timeutil.Format("Ym")
return this.Query(tx).
Pk(serverId).
Set("totalDailyTraffic", dbs.SQL("IF(trafficDay=:day, totalDailyTraffic, 0)+:trafficGB")).
Set("totalMonthlyTraffic", dbs.SQL("IF(trafficMonth=:month, totalMonthlyTraffic, 0)+:trafficGB")).
Set("totalTraffic", dbs.SQL("totalTraffic+:trafficGB")).
Set("trafficDay", day).
Set("trafficMonth", month).
Param("day", day).
Param("month", month).
Param("trafficGB", gb).
UpdateQuickly()
}
// ResetServerTotalTraffic 重置服务总流量
func (this *ServerDAO) ResetServerTotalTraffic(tx *dbs.Tx, serverId int64) error {
return this.Query(tx).
Pk(serverId).
Set("totalDailyTraffic", 0).
Set("totalMonthlyTraffic", 0).
UpdateQuickly()
}
// FindEnabledServerIdWithUserPlanId 查找使用某个套餐的服务
func (this *ServerDAO) FindEnabledServerIdWithUserPlanId(tx *dbs.Tx, userPlanId int64) (int64, error) {
return this.Query(tx).
State(ServerStateEnabled).
Attr("userPlanId", userPlanId).
ResultPk().
FindInt64Col(0)
}
// UpdateServersClusterIdWithPlanId 修改套餐所在集群
func (this *ServerDAO) UpdateServersClusterIdWithPlanId(tx *dbs.Tx, planId int64, clusterId int64) error {
return this.Query(tx).
Where("userPlanId IN (SELECT id FROM "+SharedUserPlanDAO.Table+" WHERE planId=:planId AND state=1)").
Param("planId", planId).
Set("clusterId", clusterId).
UpdateQuickly()
}
// UpdateServerUserPlanId 设置服务所属套餐
func (this *ServerDAO) UpdateServerUserPlanId(tx *dbs.Tx, serverId int64, userPlanId int64) error {
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, nil)
if err != nil {
return err
}
if userPlan == nil {
return errors.New("can not find user plan with id '" + types.String(userPlanId) + "'")
}
plan, err := SharedPlanDAO.FindEnabledPlan(tx, int64(userPlan.PlanId))
if err != nil {
return err
}
if plan == nil {
return errors.New("can not find plan with id '" + types.String(userPlan.PlanId) + "'")
}
err = this.Query(tx).
Pk(serverId).
Set("userPlanId", userPlanId).
Set("clusterId", plan.ClusterId).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, serverId)
}
// NotifyUpdate 同步集群
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
// 创建任务
@@ -1698,3 +2159,25 @@ func (this *ServerDAO) NotifyDNSUpdate(tx *dbs.Tx, serverId int64) error {
}
return dns.SharedDNSTaskDAO.CreateServerTask(tx, serverId, dns.DNSTaskTypeServerChange)
}
// NotifyDisable 通知禁用
func (this *ServerDAO) NotifyDisable(tx *dbs.Tx, serverId int64) error {
// 禁用缓存策略相关的内容
policyIds, err := SharedHTTPFirewallPolicyDAO.FindFirewallPolicyIdsWithServerId(tx, serverId)
if err != nil {
return err
}
for _, policyId := range policyIds {
err = SharedHTTPFirewallPolicyDAO.DisableHTTPFirewallPolicy(tx, policyId)
if err != nil {
return err
}
err = SharedHTTPFirewallPolicyDAO.NotifyDisable(tx, policyId)
if err != nil {
return err
}
}
return nil
}

View File

@@ -3,16 +3,20 @@ package models
import (
"crypto/md5"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
"testing"
"time"
)
func TestServerDAO_ComposeServerConfig(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1, false)
if err != nil {
t.Fatal(err)
}
@@ -22,7 +26,7 @@ func TestServerDAO_ComposeServerConfig(t *testing.T) {
func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 14)
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 14, false)
if err != nil {
t.Fatal(err)
}
@@ -32,7 +36,7 @@ func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
func TestServerDAO_UpdateServerConfig(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1, false)
if err != nil {
t.Fatal(err)
}
@@ -136,6 +140,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
func TestServerDAO_FindAllEnabledServersWithNode(t *testing.T) {
dbs.NotifyReady()
var before = time.Now()
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48)
if err != nil {
t.Fatal(err)
@@ -143,6 +148,107 @@ func TestServerDAO_FindAllEnabledServersWithNode(t *testing.T) {
for _, server := range servers {
t.Log("serverId:", server.Id, "clusterId:", server.ClusterId)
}
t.Log(time.Since(before).Seconds()*1000, "ms")
}
func TestServerDAO_FindAllEnabledServersWithNode_Cache(t *testing.T) {
dbs.NotifyReady()
var cacheMap = utils.NewCacheMap()
{
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48)
if err != nil {
t.Fatal(err)
}
for _, server := range servers {
_, _ = SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true)
}
}
var before = time.Now()
{
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48)
if err != nil {
t.Fatal(err)
}
for _, server := range servers {
_, _ = SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true)
}
}
t.Log(time.Since(before).Seconds()*1000, "ms")
}
func TestServerDAO_FindAllEnabledServersWithDomain(t *testing.T) {
for _, domain := range []string{"yun4s.cn", "teaos.cn", "teaos2.cn", "cdn.teaos.cn", "cdn100.teaos.cn"} {
servers, err := NewServerDAO().FindAllEnabledServersWithDomain(nil, domain)
if err != nil {
t.Fatal(err)
}
if len(servers) > 0 {
for _, server := range servers {
t.Log(domain + ": " + server.ServerNames)
}
} else {
t.Log(domain + ": not found")
}
}
}
func TestServerDAO_UpdateServerTrafficLimitStatus(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
before := time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
err := NewServerDAO().UpdateServerTrafficLimitStatus(tx, &serverconfigs.TrafficLimitConfig{
IsOn: true,
DailySize: &shared.SizeCapacity{Count: 1, Unit: "mb"},
MonthlySize: &shared.SizeCapacity{Count: 10, Unit: "mb"},
TotalSize: &shared.SizeCapacity{Count: 100, Unit: "gb"},
NoticePageBody: "",
}, 23, false)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestServerDAO_CalculateServerTrafficLimitConfig(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
before := time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
var cacheMap = utils.NewCacheMap()
config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap)
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(config, t)
}
func TestServerDAO_CalculateServerTrafficLimitConfig_Cache(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
before := time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
var cacheMap = utils.NewCacheMap()
for i := 0; i < 10; i++ {
config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap)
if err != nil {
t.Fatal(err)
}
_ = config
}
}
func BenchmarkServerDAO_CountAllEnabledServers(b *testing.B) {

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -267,13 +268,13 @@ func (this *ServerGroupDAO) InitGroupWeb(tx *dbs.Tx, groupId int64) (int64, erro
}
// ComposeGroupConfig 组合配置
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap maps.Map) (*serverconfigs.ServerGroupConfig, error) {
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap *utils.CacheMap) (*serverconfigs.ServerGroupConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(groupId)
var cacheConfig = cacheMap.Get(cacheKey)
var cacheConfig, _ = cacheMap.Get(cacheKey)
if cacheConfig != nil {
// 克隆防止分解后的Server配置相互受到影响
configJSON, err := json.Marshal(cacheConfig)
@@ -365,7 +366,9 @@ func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheM
}
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -2,37 +2,46 @@ package models
// Server 服务
type Server struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
UserId uint32 `field:"userId"` // 用户ID
AdminId uint32 `field:"adminId"` // 管理员ID
Type string `field:"type"` // 服务类型
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
ServerNames string `field:"serverNames"` // 域名列表
AuditingServerNames string `field:"auditingServerNames"` // 审核中的域名
IsAuditing uint8 `field:"isAuditing"` // 是否正在审核
AuditingResult string `field:"auditingResult"` // 审核结果
Http string `field:"http"` // HTTP配置
Https string `field:"https"` // HTTPS配置
Tcp string `field:"tcp"` // TCP配置
Tls string `field:"tls"` // TLS配置
Unix string `field:"unix"` // Unix配置
Udp string `field:"udp"` // UDP配置
WebId uint32 `field:"webId"` // WEB配置
ReverseProxy string `field:"reverseProxy"` // 反向代理配置
GroupIds string `field:"groupIds"` // 分组ID列表
Config string `field:"config"` // 服务配置,自动生成
ConfigMd5 string `field:"configMd5"` // Md5
ClusterId uint32 `field:"clusterId"` // 集群ID
IncludeNodes string `field:"includeNodes"` // 部署条件
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
Version uint32 `field:"version"` // 版本号
CreatedAt uint64 `field:"createdAt"` // 创建时间
State uint8 `field:"state"` // 状态
DnsName string `field:"dnsName"` // DNS名称
TcpPorts string `field:"tcpPorts"` // 所包含TCP端口
UdpPorts string `field:"udpPorts"` // 所包含UDP端口
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
UserId uint32 `field:"userId"` // 用户ID
AdminId uint32 `field:"adminId"` // 管理员ID
Type string `field:"type"` // 服务类型
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
ServerNames string `field:"serverNames"` // 域名列表
AuditingServerNames string `field:"auditingServerNames"` // 审核中的域名
IsAuditing uint8 `field:"isAuditing"` // 是否正在审核
AuditingResult string `field:"auditingResult"` // 审核结果
Http string `field:"http"` // HTTP配置
Https string `field:"https"` // HTTPS配置
Tcp string `field:"tcp"` // TCP配置
Tls string `field:"tls"` // TLS配置
Unix string `field:"unix"` // Unix配置
Udp string `field:"udp"` // UDP配置
WebId uint32 `field:"webId"` // WEB配置
ReverseProxy string `field:"reverseProxy"` // 反向代理配置
GroupIds string `field:"groupIds"` // 分组ID列表
Config string `field:"config"` // 服务配置,自动生成
ConfigMd5 string `field:"configMd5"` // Md5
ClusterId uint32 `field:"clusterId"` // 集群ID
IncludeNodes string `field:"includeNodes"` // 部署条件
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
Version uint32 `field:"version"` // 版本号
CreatedAt uint64 `field:"createdAt"` // 创建时间
State uint8 `field:"state"` // 状态
DnsName string `field:"dnsName"` // DNS名称
TcpPorts string `field:"tcpPorts"` // 所包含TCP端口
UdpPorts string `field:"udpPorts"` // 所包含UDP端口
SupportCNAME uint8 `field:"supportCNAME"` // 允许CNAME不在域名名单
TrafficLimit string `field:"trafficLimit"` // 流量限制
TrafficDay string `field:"trafficDay"` // YYYYMMDD
TrafficMonth string `field:"trafficMonth"` // YYYYMM
TotalDailyTraffic float64 `field:"totalDailyTraffic"` // 日流量
TotalMonthlyTraffic float64 `field:"totalMonthlyTraffic"` // 月流量
TrafficLimitStatus string `field:"trafficLimitStatus"` // 流量限制状态
TotalTraffic float64 `field:"totalTraffic"` // 总流量
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
}
type ServerOperator struct {
@@ -67,6 +76,15 @@ type ServerOperator struct {
DnsName interface{} // DNS名称
TcpPorts interface{} // 所包含TCP端口
UdpPorts interface{} // 所包含UDP端口
SupportCNAME interface{} // 允许CNAME不在域名名单
TrafficLimit interface{} // 流量限制
TrafficDay interface{} // YYYYMMDD
TrafficMonth interface{} // YYYYMM
TotalDailyTraffic interface{} // 日流量
TotalMonthlyTraffic interface{} // 月流量
TrafficLimitStatus interface{} // 流量限制状态
TotalTraffic interface{} // 总流量
UserPlanId interface{} // 所属套餐ID
}
func NewServerOperator() *ServerOperator {

View File

@@ -3,11 +3,11 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
@@ -164,12 +164,12 @@ func (this *SSLCertDAO) UpdateCert(tx *dbs.Tx, certId int64, isOn bool, name str
}
// ComposeCertConfig 组合配置
func (this *SSLCertDAO) ComposeCertConfig(tx *dbs.Tx, certId int64, cacheMap maps.Map) (*sslconfigs.SSLCertConfig, error) {
func (this *SSLCertDAO) ComposeCertConfig(tx *dbs.Tx, certId int64, cacheMap *utils.CacheMap) (*sslconfigs.SSLCertConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(certId)
var cache = cacheMap.Get(cacheKey)
var cache, _ = cacheMap.Get(cacheKey)
if cache != nil {
return cache.(*sslconfigs.SSLCertConfig), nil
}
@@ -213,7 +213,9 @@ func (this *SSLCertDAO) ComposeCertConfig(tx *dbs.Tx, certId int64, cacheMap map
config.CommonNames = commonNames
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}

View File

@@ -3,13 +3,13 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"strconv"
)
const (
@@ -77,13 +77,13 @@ func (this *SSLPolicyDAO) FindEnabledSSLPolicy(tx *dbs.Tx, id int64) (*SSLPolicy
}
// ComposePolicyConfig 组合配置
func (this *SSLPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*sslconfigs.SSLPolicy, error) {
func (this *SSLPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*sslconfigs.SSLPolicy, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":config:" + types.String(policyId)
var cacheConfig = cacheMap.Get(cacheKey)
var cacheConfig, _ = cacheMap.Get(cacheKey)
if cacheConfig != nil {
return cacheConfig.(*sslconfigs.SSLPolicy), nil
}
@@ -167,7 +167,9 @@ func (this *SSLPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheM
config.HSTS = hstsConfig
}
cacheMap[cacheKey] = config
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
@@ -181,8 +183,8 @@ func (this *SSLPolicyDAO) FindAllEnabledPolicyIdsWithCertId(tx *dbs.Tx, certId i
ones, err := this.Query(tx).
State(SSLPolicyStateEnabled).
ResultPk().
Where(`JSON_CONTAINS(certs, '{"certId": ` + strconv.FormatInt(certId, 10) + ` }')`).
Reuse(false). // 由于我们在JSON_CONTAINS()直接使用了变量,所以不能重用
Where("JSON_CONTAINS(certs, :certJSON)").
Param("certJSON", maps.Map{"certId": certId}.AsJSON()).
FindAll()
if err != nil {
return nil, err

View File

@@ -24,7 +24,7 @@ func init() {
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
go func() {
for range ticker.C {
err := SharedServerDomainHourlyStatDAO.Clean(nil, 60) // 只保留60
err := SharedServerDomainHourlyStatDAO.Clean(nil, 7) // 只保留7
if err != nil {
remotelogs.Error("ServerDomainHourlyStatDAO", "clean expired data failed: "+err.Error())
}

View File

@@ -34,7 +34,8 @@ func TestSysLocker_Increase(t *testing.T) {
defer wg.Done()
v, err := NewSysLockerDAO().Increase(nil, "hello", 0)
if err != nil {
t.Fatal(err)
t.Log("err:", err)
return
}
t.Log("v:", v)
}()

View File

@@ -2,12 +2,45 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
func init() {
dbs.OnReadyDone(func() {
var generatedMonths = []string{}
go func() {
// 自动生成账单任务
var ticker = time.NewTicker(1 * time.Minute)
for range ticker.C {
// 是否已经生成了,如果已经生成了就跳过
var lastMonth = timeutil.Format("Ym", time.Now().AddDate(0, -1, 0))
if lists.ContainsString(generatedMonths, lastMonth) {
continue
}
err := SharedUserBillDAO.GenerateBills(nil, lastMonth)
if err != nil {
remotelogs.Error("UserBillDAO", "generate bills failed: "+err.Error())
} else {
generatedMonths = append(generatedMonths, lastMonth)
}
}
}()
})
}
type BillType = string
const (
@@ -35,7 +68,7 @@ func init() {
})
}
// 计算账单数量
// CountAllUserBills 计算账单数量
func (this *UserBillDAO) CountAllUserBills(tx *dbs.Tx, isPaid int32, userId int64, month string) (int64, error) {
query := this.Query(tx)
if isPaid == 0 {
@@ -52,7 +85,7 @@ func (this *UserBillDAO) CountAllUserBills(tx *dbs.Tx, isPaid int32, userId int6
return query.Count()
}
// 列出单页账单
// ListUserBills 列出单页账单
func (this *UserBillDAO) ListUserBills(tx *dbs.Tx, isPaid int32, userId int64, month string, offset, size int64) (result []*UserBill, err error) {
query := this.Query(tx)
if isPaid == 0 {
@@ -75,19 +108,42 @@ func (this *UserBillDAO) ListUserBills(tx *dbs.Tx, isPaid int32, userId int64, m
return
}
// 创建账
func (this *UserBillDAO) CreateBill(tx *dbs.Tx, userId int64, billType BillType, description string, amount float32, month string) (int64, error) {
op := NewUserBillOperator()
op.UserId = userId
op.Type = billType
op.Description = description
op.Amount = amount
op.Month = month
op.IsPaid = false
return this.SaveInt64(tx, op)
// FindUnpaidBills 查找未支付订
func (this *UserBillDAO) FindUnpaidBills(tx *dbs.Tx, size int64) (result []*UserBill, err error) {
if size <= 0 {
size = 10000
}
_, err = this.Query(tx).
Attr("isPaid", false).
Lt("month", timeutil.Format("Ym")). //当月的不能支付,因为当月还没过完
Limit(size).
DescPk().
Slice(&result).
FindAll()
return
}
// 检查是否有当月账单
// CreateBill 创建账单
func (this *UserBillDAO) CreateBill(tx *dbs.Tx, userId int64, billType BillType, description string, amount float32, month string) error {
code, err := this.GenerateBillCode(tx)
if err != nil {
return err
}
return this.Query(tx).
InsertOrUpdateQuickly(maps.Map{
"userId": userId,
"type": billType,
"description": description,
"amount": amount,
"month": month,
"code": code,
"isPaid": false,
}, maps.Map{
"amount": amount,
})
}
// ExistBill 检查是否有当月账单
func (this *UserBillDAO) ExistBill(tx *dbs.Tx, userId int64, billType BillType, month string) (bool, error) {
return this.Query(tx).
Attr("userId", userId).
@@ -96,47 +152,10 @@ func (this *UserBillDAO) ExistBill(tx *dbs.Tx, userId int64, billType BillType,
Exist()
}
// 生成账单
// GenerateBills 生成账单
// month 格式YYYYMM
func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
// 用户
offset := int64(0)
size := int64(100) // 每次只查询N次防止由于执行时间过长而锁表
for {
userIds, err := SharedUserDAO.ListEnabledUserIds(tx, offset, size)
if err != nil {
return err
}
offset += size
if len(userIds) == 0 {
break
}
for _, userId := range userIds {
// CDN流量账单
err := this.generateTrafficBill(tx, userId, month)
if err != nil {
return err
}
}
}
return nil
}
// 生成CDN流量账单
// month 格式YYYYMM
func (this *UserBillDAO) generateTrafficBill(tx *dbs.Tx, userId int64, month string) error {
// 检查是否已经有账单了
b, err := this.ExistBill(tx, userId, BillTypeTraffic, month)
if err != nil {
return err
}
if b {
return nil
}
// TODO 优化使用缓存
// 区域价格
regions, err := SharedNodeRegionDAO.FindAllEnabledRegionPrices(tx)
if err != nil {
return err
@@ -153,18 +172,104 @@ func (this *UserBillDAO) generateTrafficBill(tx *dbs.Tx, userId int64, month str
return nil
}
cost := float32(0)
// 计算套餐费用
plans, err := SharedPlanDAO.FindAllEnabledPlans(tx)
if err != nil {
return err
}
var planMap = map[int64]*Plan{}
for _, plan := range plans {
planMap[int64(plan.Id)] = plan
}
stats, err := SharedServerDailyStatDAO.FindMonthlyStatsWithPlan(tx, month)
if err != nil {
return err
}
for _, stat := range stats {
plan, ok := planMap[int64(stat.PlanId)]
if !ok {
continue
}
if plan.PriceType != serverconfigs.PlanPriceTypeTraffic {
continue
}
if len(plan.TrafficPrice) == 0 {
continue
}
var priceConfig = &serverconfigs.PlanTrafficPrice{}
err = json.Unmarshal([]byte(plan.TrafficPrice), priceConfig)
if err != nil {
return err
}
if priceConfig.Base > 0 {
var fee = priceConfig.Base * (float32(stat.Bytes) / 1024 / 1024 / 1024)
err = SharedServerDailyStatDAO.UpdateStatFee(tx, int64(stat.Id), fee)
if err != nil {
return err
}
}
}
// 用户
offset := int64(0)
size := int64(100) // 每次只查询N次防止由于执行时间过长而锁表
for {
userIds, err := SharedUserDAO.ListEnabledUserIds(tx, offset, size)
if err != nil {
return err
}
offset += size
if len(userIds) == 0 {
break
}
for _, userId := range userIds {
// CDN流量账单
err := this.generateTrafficBill(tx, userId, month, regions, priceItems)
if err != nil {
return err
}
}
}
return nil
}
// UpdateUserBillIsPaid 设置账单已支付
func (this *UserBillDAO) UpdateUserBillIsPaid(tx *dbs.Tx, billId int64, isPaid bool) error {
return this.Query(tx).
Pk(billId).
Set("isPaid", isPaid).
UpdateQuickly()
}
// 生成CDN流量账单
// month 格式YYYYMM
func (this *UserBillDAO) generateTrafficBill(tx *dbs.Tx, userId int64, month string, regions []*NodeRegion, priceItems []*NodePriceItem) error {
// 检查是否已经有账单了
if month < timeutil.Format("Ym") {
b, err := this.ExistBill(tx, userId, BillTypeTraffic, month)
if err != nil {
return err
}
if b {
return nil
}
}
var cost = float32(0)
for _, region := range regions {
if len(region.Prices) == 0 || region.Prices == "null" {
continue
}
priceMap := map[string]float32{}
err = json.Unmarshal([]byte(region.Prices), &priceMap)
err := json.Unmarshal([]byte(region.Prices), &priceMap)
if err != nil {
return err
}
trafficBytes, err := SharedServerDailyStatDAO.SumUserMonthly(tx, userId, int64(region.Id), month)
trafficBytes, err := SharedServerDailyStatDAO.SumUserMonthlyWithoutPlan(tx, userId, int64(region.Id), month)
if err != nil {
return err
}
@@ -187,16 +292,22 @@ func (this *UserBillDAO) generateTrafficBill(tx *dbs.Tx, userId int64, month str
cost += (float32(trafficBytes*8) / 1_000_000_000) * price
}
// 套餐费用
planFee, err := SharedServerDailyStatDAO.SumUserMonthlyFee(tx, userId, month)
if err != nil {
return err
}
cost += float32(planFee)
if cost == 0 {
return nil
}
// 创建账单
_, err = this.CreateBill(tx, userId, BillTypeTraffic, "按流量计费", cost, month)
return err
return this.CreateBill(tx, userId, BillTypeTraffic, "按流量计费", cost, month)
}
// 获取账单类型名称
// BillTypeName 获取账单类型名称
func (this *UserBillDAO) BillTypeName(billType BillType) string {
switch billType {
case BillTypeTraffic:
@@ -204,3 +315,18 @@ func (this *UserBillDAO) BillTypeName(billType BillType) string {
}
return ""
}
// GenerateBillCode 生成账单编号
func (this *UserBillDAO) GenerateBillCode(tx *dbs.Tx) (string, error) {
var code = timeutil.Format("YmdHis") + types.String(rands.Int(100000, 999999))
exists, err := this.Query(tx).
Attr("code", code).
Exist()
if err != nil {
return "", err
}
if !exists {
return code, nil
}
return this.GenerateBillCode(tx)
}

View File

@@ -1,6 +1,6 @@
package models
// 用户账单
// UserBill 用户账单
type UserBill struct {
Id uint64 `field:"id"` // ID
UserId uint32 `field:"userId"` // 用户ID
@@ -10,6 +10,7 @@ type UserBill struct {
Month string `field:"month"` // 帐期YYYYMM
IsPaid uint8 `field:"isPaid"` // 是否已支付
PaidAt uint64 `field:"paidAt"` // 支付时间
Code string `field:"code"` // 账单编号
CreatedAt uint64 `field:"createdAt"` // 创建时间
}
@@ -22,6 +23,7 @@ type UserBillOperator struct {
Month interface{} // 帐期YYYYMM
IsPaid interface{} // 是否已支付
PaidAt interface{} // 支付时间
Code interface{} // 账单编号
CreatedAt interface{} // 创建时间
}

View File

@@ -55,15 +55,29 @@ func (this *UserDAO) DisableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err
Update()
}
// FindEnabledUser 查找启用中的条目
func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, id int64) (*User, error) {
// FindEnabledUser 查找启用的用户
func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, userId int64, cacheMap *utils.CacheMap) (*User, error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindEnabledUser:" + types.String(userId)
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*User), nil
}
result, err := this.Query(tx).
Pk(id).
Pk(userId).
Attr("state", UserStateEnabled).
Find()
if result == nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*User), err
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
"strconv"
"strings"
)
const (
@@ -282,3 +283,19 @@ func (this *UserNodeDAO) CountAllEnabledAndOnOfflineNodes(tx *dbs.Tx) (int64, er
Where("(status IS NULL OR JSON_EXTRACT(status, '$.updatedAt')<UNIX_TIMESTAMP()-60)").
Count()
}
// CountAllEnabledUserNodesWithSSLPolicyIds 计算使用SSL策略的所有用户节点数量
func (this *UserNodeDAO) CountAllEnabledUserNodesWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
if len(sslPolicyIds) == 0 {
return
}
policyStringIds := []string{}
for _, policyId := range sslPolicyIds {
policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10))
}
return this.Query(tx).
State(UserNodeStateEnabled).
Where("(FIND_IN_SET(JSON_EXTRACT(https, '$.sslPolicyRef.sslPolicyId'), :policyIds)) ").
Param("policyIds", strings.Join(policyStringIds, ",")).
Count()
}

View File

@@ -2,8 +2,8 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/maps"
)
// DecodeHTTP 解析HTTP配置
@@ -26,7 +26,7 @@ func (this *UserNode) DecodeHTTP() (*serverconfigs.HTTPProtocolConfig, error) {
}
// DecodeHTTPS 解析HTTPS配置
func (this *UserNode) DecodeHTTPS(cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) {
func (this *UserNode) DecodeHTTPS(cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
if !IsNotNull(this.Https) {
return nil, nil
}

View File

@@ -0,0 +1,211 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
const (
UserPlanStateEnabled = 1 // 已启用
UserPlanStateDisabled = 0 // 已禁用
DefaultUserPlanMaxDay = "3000-01-01"
)
type UserPlanDAO dbs.DAO
func NewUserPlanDAO() *UserPlanDAO {
return dbs.NewDAO(&UserPlanDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeUserPlans",
Model: new(UserPlan),
PkName: "id",
},
}).(*UserPlanDAO)
}
var SharedUserPlanDAO *UserPlanDAO
func init() {
dbs.OnReady(func() {
SharedUserPlanDAO = NewUserPlanDAO()
})
}
// EnableUserPlan 启用条目
func (this *UserPlanDAO) EnableUserPlan(tx *dbs.Tx, id uint64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", UserPlanStateEnabled).
Update()
return err
}
// DisableUserPlan 禁用条目
func (this *UserPlanDAO) DisableUserPlan(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", UserPlanStateDisabled).
Update()
if err != nil {
return err
}
return this.NotifyUpdate(tx, id)
}
// FindEnabledUserPlan 查找启用中的条目
func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64, cacheMap *utils.CacheMap) (*UserPlan, error) {
var cacheKey = this.Table + ":FindEnabledUserPlan:" + types.String(userPlanId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*UserPlan), nil
}
}
result, err := this.Query(tx).
Pk(userPlanId).
Attr("state", UserPlanStateEnabled).
Find()
if result == nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*UserPlan), err
}
// CountAllEnabledUserPlans 计算套餐数量
func (this *UserPlanDAO) CountAllEnabledUserPlans(tx *dbs.Tx, isAvailable bool, isExpired bool, expiringDays int32) (int64, error) {
var query = this.Query(tx).
State(UserPlanStateEnabled).
Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)").
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
var today = timeutil.Format("Y-m-d")
if isAvailable {
query.Gte("dayTo", today)
}
if isExpired {
query.Lt("dayTo", today)
}
if expiringDays > 0 {
var expiringDay = timeutil.Format("Y-m-d", time.Now().AddDate(0, 0, int(expiringDays)))
query.Gte("dayTo", today)
query.Lte("dayTo", expiringDay)
}
return query.Count()
}
// ListEnabledUserPlans 列出单页套餐
func (this *UserPlanDAO) ListEnabledUserPlans(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int32, offset int64, size int64) (result []*UserPlan, err error) {
var query = this.Query(tx).
State(UserPlanStateEnabled).
Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)").
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
if userId > 0 {
query.Attr("userId", userId)
}
var today = timeutil.Format("Y-m-d")
if isAvailable {
query.Gte("dayTo", today)
}
if isExpired {
query.Lt("dayTo", today)
}
if expiringDays > 0 {
var expiringDay = timeutil.Format("Y-m-d", time.Now().AddDate(0, 0, int(expiringDays)))
query.Gte("dayTo", today)
query.Lte("dayTo", expiringDay)
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// CreateUserPlan 创建套餐
func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64, dayTo string) (int64, error) {
var op = NewUserPlanOperator()
op.UserId = userId
op.PlanId = planId
op.DayTo = dayTo
op.IsOn = true
op.State = UserStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateUserPlan 修改套餐
func (this *UserPlanDAO) UpdateUserPlan(tx *dbs.Tx, userPlanId int64, planId int64, dayTo string, isOn bool) error {
if userPlanId <= 0 {
return errors.New("invalid userPlanId")
}
var op = NewUserPlanOperator()
op.Id = userPlanId
op.PlanId = planId
op.DayTo = dayTo
op.IsOn = isOn
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, userPlanId)
}
// UpdateUserPlanDayTo 修改套餐日期
func (this *UserPlanDAO) UpdateUserPlanDayTo(tx *dbs.Tx, userPlanId int64, dayTo string) error {
if userPlanId <= 0 {
return errors.New("invalid userPlanId")
}
var op = NewUserPlanOperator()
op.Id = userPlanId
op.DayTo = dayTo
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, userPlanId)
}
// FindAllEnabledPlansForServer 列出服务可用的套餐
func (this *UserPlanDAO) FindAllEnabledPlansForServer(tx *dbs.Tx, userId int64, serverId int64) (result []*UserPlan, err error) {
var query = this.Query(tx).
State(UserPlanStateEnabled).
Attr("userId", userId).
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
if serverId > 0 {
query.Where("id NOT IN (SELECT userPlanId FROM " + SharedServerDAO.Table + " WHERE state=1 AND id!=:serverId)")
query.Param("serverId", serverId)
} else {
query.Where("id NOT IN (SELECT userPlanId FROM " + SharedServerDAO.Table + " WHERE state=1)")
}
_, err = query.
DescPk().
Slice(&result).
FindAll()
return
}
// NotifyUpdate 通知更新
func (this *UserPlanDAO) NotifyUpdate(tx *dbs.Tx, userPlanId int64) error {
serverId, err := SharedServerDAO.FindEnabledServerIdWithUserPlanId(tx, userPlanId)
if err != nil {
return err
}
if serverId > 0 {
return SharedServerDAO.NotifyUpdate(tx, serverId)
}
return nil
}

View File

@@ -0,0 +1,6 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,24 @@
package models
// UserPlan 用户的套餐
type UserPlan struct {
Id uint64 `field:"id"` // ID
UserId uint32 `field:"userId"` // 用户ID
PlanId uint32 `field:"planId"` // 套餐ID
IsOn uint8 `field:"isOn"` // 是否启用
DayTo string `field:"dayTo"` // 结束日期
State uint8 `field:"state"` // 状态
}
type UserPlanOperator struct {
Id interface{} // ID
UserId interface{} // 用户ID
PlanId interface{} // 套餐ID
IsOn interface{} // 是否启用
DayTo interface{} // 结束日期
State interface{} // 状态
}
func NewUserPlanOperator() *UserPlanOperator {
return &UserPlanOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -1339,6 +1339,12 @@ func (this *HuaweiDNSProvider) AddRecord(domain string, newRecord *dnstypes.Reco
if ttl <= 0 {
ttl = 300
}
// 华为云TXT需要加引号
if newRecord.Type == dnstypes.RecordTypeTXT {
newRecord.Value = "\"" + strings.Trim(newRecord.Value, "\"") + "\""
}
err = this.doAPI(http.MethodPost, "/v2.1/zones/"+zoneId+"/recordsets", map[string]string{}, maps.Map{
"name": newRecord.Name + "." + domain + ".",
"description": "CDN系统自动创建",
@@ -1376,6 +1382,11 @@ func (this *HuaweiDNSProvider) UpdateRecord(domain string, record *dnstypes.Reco
ttl = 300
}
// 华为云TXT需要加引号
if newRecord.Type == dnstypes.RecordTypeTXT {
newRecord.Value = "\"" + strings.Trim(newRecord.Value, "\"") + "\""
}
var resp = new(huaweidns.ZonesUpdateRecordSetResponse)
err = this.doAPI(http.MethodPut, "/v2.1/zones/"+zoneId+"/recordsets/"+recordId, map[string]string{}, maps.Map{
"name": newRecord.Name + "." + domain + ".",

View File

@@ -6,5 +6,6 @@ type Credentials struct {
Username string
Password string
PrivateKey string
Passphrase string
Method string
}

Some files were not shown because too many files have changed in this diff Show More