Compare commits

...

54 Commits

Author SHA1 Message Date
刘祥超
71681b1c5d 提供下载最新边缘节点API 2021-06-10 19:21:45 +08:00
刘祥超
6b2a015015 创建缓存策略时默认加入缓存条件 2021-06-10 10:50:56 +08:00
刘祥超
9e74baf37b 路径规则、重写规则、URL跳转规则均支持匹配条件 2021-06-09 21:44:38 +08:00
刘祥超
b651d94bf5 请求统计增加即时、按天 2021-06-08 15:10:08 +08:00
刘祥超
b61433c5f8 更新SQL 2021-06-08 11:23:35 +08:00
刘祥超
c4d2191110 增加服务流量统计 2021-06-08 11:18:27 +08:00
刘祥超
f6159c646c 修改反向代理回源主机名默认值 2021-06-07 17:23:10 +08:00
刘祥超
9cc7d54b64 健康检查支持IPv6 2021-06-07 10:06:16 +08:00
刘祥超
05ce1d327b 边缘节点IP支持IPV6 2021-06-07 10:02:07 +08:00
刘祥超
111df18204 WAF策略支持搜索 2021-06-07 08:58:26 +08:00
刘祥超
bbcdf167b1 缓存策略列表支持搜索 2021-06-07 08:44:19 +08:00
刘祥超
d62a44b1ac 聚合服务相关日志 2021-06-06 13:40:33 +08:00
刘祥超
969bb2bcda 日志级别增加success 2021-06-06 13:40:25 +08:00
刘祥超
6dcd3f2a98 域名服务集群创建时可以选择开启访问日志 2021-06-05 15:39:02 +08:00
刘祥超
7936dc85a9 API认证超时时增加检查系统时钟提示 2021-06-04 15:34:58 +08:00
刘祥超
94c4098de5 兼容部分API老命名 2021-06-04 11:41:50 +08:00
刘祥超
5b0aef8d4a 修复用户创建服务时提示resource not found的问题 2021-06-04 11:23:01 +08:00
刘祥超
4571b95e4a 服务访问日志增加关键词搜索 2021-06-04 10:15:41 +08:00
刘祥超
b9cba4c7df DNS访问日志可以使用关键词搜索 2021-06-04 09:09:14 +08:00
刘祥超
b46b5dc05f 修复域名服务集群检测新版本节点的Bug 2021-06-03 22:08:38 +08:00
刘祥超
ef5630ba4a 实现在域名解析中使用EdgeDNS 2021-06-02 18:13:48 +08:00
刘祥超
fe6c610d56 更新SQL 2021-06-02 11:54:05 +08:00
刘祥超
442dd195ca 域名服务增加访问日志 2021-06-02 11:53:24 +08:00
刘祥超
e9e4abff03 实现域名、记录同步等API 2021-06-01 16:43:00 +08:00
刘祥超
2a3fbd080e [域名服务]实现基本的线路配置 2021-05-30 16:31:43 +08:00
刘祥超
9d7f1a2702 更新SQL 2021-05-27 17:29:31 +08:00
刘祥超
57dcbce775 实现基本的域名、记录管理 2021-05-27 17:09:07 +08:00
刘祥超
a71bf9ed79 实现最基本的域名服务节点管理 2021-05-26 14:40:05 +08:00
刘祥超
725692ce94 优化API命名 2021-05-25 17:48:55 +08:00
刘祥超
ea15096dc3 优化API命名 2021-05-25 17:08:58 +08:00
刘祥超
a07dbb718f 实现域名服务器集群管理 2021-05-25 15:49:13 +08:00
刘祥超
18381b76fb 变更版本为0.2.0 2021-05-25 15:49:04 +08:00
刘祥超
1d31245b4a 上传新SQL 2021-05-24 09:53:17 +08:00
刘祥超
f26bc337c9 缓存策略可以设置全局的缓存条件 2021-05-24 09:24:49 +08:00
刘祥超
05c2955211 SSH认证列表可以使用关键词搜索,返回内容增加用户名 2021-05-23 21:13:20 +08:00
刘祥超
f0ffa48390 节点日志增加服务ID、是否修复等字段 2021-05-23 20:46:51 +08:00
刘祥超
a472b8a3bd 修改版本 2021-05-20 19:41:33 +08:00
刘祥超
cfa2717838 实现日志消息聚合 2021-05-19 20:47:23 +08:00
刘祥超
aece12854e 节点日志可以通过日期、关键词、级别等筛选 2021-05-19 19:03:46 +08:00
刘祥超
02692029ee 增加darwin/arm64编译脚本 2021-05-13 14:57:04 +08:00
刘祥超
03da451d17 更新版本号为v0.1 2021-05-13 14:37:32 +08:00
刘祥超
946a9f373c 节点可以单独设置缓存的磁盘、内存容量 2021-05-12 21:39:08 +08:00
刘祥超
ff31d98f78 移除一处日志 2021-05-12 16:10:41 +08:00
刘祥超
c49d64eaee 更新SQL 2021-05-12 15:08:18 +08:00
刘祥超
68f390a73b 数据看板中增加升级信息 2021-05-11 22:48:04 +08:00
刘祥超
66a748cc26 服务支持fastcgi 2021-05-10 21:13:32 +08:00
刘祥超
b0339266e1 默认集群自动生成uniqueId和secret 2021-05-06 11:04:30 +08:00
刘祥超
ebdb181361 实现阈值通知消息 2021-05-05 19:50:55 +08:00
刘祥超
63f011c6c8 实现基础的阈值设置 2021-05-04 21:02:31 +08:00
刘祥超
43fbd7a404 记录和显示最近常用的服务 2021-05-03 15:16:02 +08:00
刘祥超
75192b6a6e 记录和显示最近常用的集群 2021-05-03 11:32:43 +08:00
刘祥超
d88ad9b3a1 实现基本的监控图表 2021-04-29 16:48:09 +08:00
刘祥超
b94fdf11cc 变更版本号 2021-04-18 21:25:05 +08:00
刘祥超
2eb249ad0f 增加SSH认证测试相关API 2021-04-18 21:19:34 +08:00
154 changed files with 7748 additions and 1236 deletions

View File

@@ -6,3 +6,4 @@
./build.sh linux mips64
./build.sh linux mips64le
./build.sh darwin amd64
./build.sh darwin arm64

6
go.sum
View File

@@ -175,12 +175,6 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20210125103732-4d79fc3c3d0b h1:niQL2t//041GUaqDPM5D+ldyr0Ng2WKwZJHPRLQhQtk=
github.com/iwind/TeaGo v0.0.0-20210125103732-4d79fc3c3d0b/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20210302120856-7588e79bdbe3 h1:k9K3HHMmkF7HYyIHz21AtmYH4Zdk/8OI98a8P0O8o1I=
github.com/iwind/TeaGo v0.0.0-20210302120856-7588e79bdbe3/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20210325033016-3279bdaa087d h1:FQTYJmZeCMdwM0Bz+C4h31SDBt04ap6A4JOjm+FfYwk=
github.com/iwind/TeaGo v0.0.0-20210325033016-3279bdaa087d/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f h1:r2O8PONj/KiuZjJHVHn7KlCePUIjNtgAmvLfgRafQ8o=
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=

View File

@@ -2,6 +2,7 @@ package acme
import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/go-acme/lego/v4/challenge/dns01"
"strings"
@@ -24,15 +25,15 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
return errors.New("invalid fqdn value")
}
recordName := fqdn[:index]
record, err := this.raw.QueryRecord(domain, recordName, dnsclients.RecordTypeTXT)
record, err := this.raw.QueryRecord(domain, recordName, dnstypes.RecordTypeTXT)
if err != nil {
return errors.New("query DNS record failed: " + err.Error())
}
if record == nil {
err = this.raw.AddRecord(domain, &dnsclients.Record{
err = this.raw.AddRecord(domain, &dnstypes.Record{
Id: "",
Name: recordName,
Type: dnsclients.RecordTypeTXT,
Type: dnstypes.RecordTypeTXT,
Value: value,
Route: this.raw.DefaultRoute(),
})
@@ -40,9 +41,9 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
return errors.New("create DNS record failed: " + err.Error())
}
} else {
err = this.raw.UpdateRecord(domain, record, &dnsclients.Record{
err = this.raw.UpdateRecord(domain, record, &dnstypes.Record{
Name: recordName,
Type: dnsclients.RecordTypeTXT,
Type: dnstypes.RecordTypeTXT,
Value: value,
Route: this.raw.DefaultRoute(),
})

View File

@@ -1,7 +1,7 @@
package teaconst
const (
Version = "0.0.13"
Version = "0.2.0"
ProductName = "Edge API"
ProcessName = "edge-api"
@@ -15,4 +15,12 @@ const (
ErrServer = "服务器出了点小问题,请稍后重试"
SystemdServiceName = "edge-api"
// 其他节点版本号,用来检测是否有需要升级的节点
NodeVersion = "0.2.0"
UserNodeVersion = "0.0.7"
AuthorityNodeVersion = "0.0.1"
MonitorNodeVersion = "0.0.1"
DNSNodeVersion = "0.0.1"
)

7
internal/const/vars.go Normal file
View File

@@ -0,0 +1,7 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package teaconst
var (
IsPlus = false
)

View File

@@ -3,6 +3,8 @@ package models
import (
"encoding/json"
"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"
@@ -38,7 +40,7 @@ func init() {
})
}
// 启用条目
// EnableAPINode 启用条目
func (this *APINodeDAO) EnableAPINode(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -47,7 +49,7 @@ func (this *APINodeDAO) EnableAPINode(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableAPINode 禁用条目
func (this *APINodeDAO) DisableAPINode(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -56,7 +58,7 @@ func (this *APINodeDAO) DisableAPINode(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledAPINode 查找启用中的条目
func (this *APINodeDAO) FindEnabledAPINode(tx *dbs.Tx, id int64) (*APINode, error) {
result, err := this.Query(tx).
Pk(id).
@@ -68,7 +70,7 @@ func (this *APINodeDAO) FindEnabledAPINode(tx *dbs.Tx, id int64) (*APINode, erro
return result.(*APINode), err
}
// 根据ID和Secret查找节点
// FindEnabledAPINodeWithUniqueIdAndSecret 根据ID和Secret查找节点
func (this *APINodeDAO) FindEnabledAPINodeWithUniqueIdAndSecret(tx *dbs.Tx, uniqueId string, secret string) (*APINode, error) {
one, err := this.Query(tx).
State(APINodeStateEnabled).
@@ -81,7 +83,7 @@ func (this *APINodeDAO) FindEnabledAPINodeWithUniqueIdAndSecret(tx *dbs.Tx, uniq
return one.(*APINode), nil
}
// 根据主键查找名称
// FindAPINodeName 根据主键查找名称
func (this *APINodeDAO) FindAPINodeName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -89,14 +91,14 @@ func (this *APINodeDAO) FindAPINodeName(tx *dbs.Tx, id int64) (string, error) {
FindStringCol("")
}
// 创建API节点
// CreateAPINode 创建API节点
func (this *APINodeDAO) CreateAPINode(tx *dbs.Tx, name string, description string, httpJSON []byte, httpsJSON []byte, restIsOn bool, restHTTPJSON []byte, restHTTPSJSON []byte, accessAddrsJSON []byte, isOn bool) (nodeId int64, err error) {
uniqueId, err := this.genUniqueId(tx)
if err != nil {
return 0, err
}
secret := rands.String(32)
err = NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, NodeRoleAPI)
err = NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleAPI)
if err != nil {
return
}
@@ -134,7 +136,7 @@ func (this *APINodeDAO) CreateAPINode(tx *dbs.Tx, name string, description strin
return types.Int64(op.Id), nil
}
// 修改API节点
// UpdateAPINode 修改API节点
func (this *APINodeDAO) UpdateAPINode(tx *dbs.Tx, nodeId int64, name string, description string, httpJSON []byte, httpsJSON []byte, restIsOn bool, restHTTPJSON []byte, restHTTPSJSON []byte, accessAddrsJSON []byte, isOn bool) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
@@ -177,7 +179,7 @@ func (this *APINodeDAO) UpdateAPINode(tx *dbs.Tx, nodeId int64, name string, des
return err
}
// 列出所有可用API节点
// FindAllEnabledAPINodes 列出所有可用API节点
func (this *APINodeDAO) FindAllEnabledAPINodes(tx *dbs.Tx) (result []*APINode, err error) {
_, err = this.Query(tx).
Attr("clusterId", 0). // 非集群专用
@@ -189,7 +191,7 @@ func (this *APINodeDAO) FindAllEnabledAPINodes(tx *dbs.Tx) (result []*APINode, e
return
}
// 列出所有可用而且启用的API节点
// FindAllEnabledAndOnAPINodes 列出所有可用而且启用的API节点
func (this *APINodeDAO) FindAllEnabledAndOnAPINodes(tx *dbs.Tx) (result []*APINode, err error) {
_, err = this.Query(tx).
Attr("clusterId", 0). // 非集群专用
@@ -202,14 +204,14 @@ func (this *APINodeDAO) FindAllEnabledAndOnAPINodes(tx *dbs.Tx) (result []*APINo
return
}
// 计算API节点数量
// CountAllEnabledAPINodes 计算API节点数量
func (this *APINodeDAO) CountAllEnabledAPINodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(APINodeStateEnabled).
Count()
}
// 列出单页的API节点
// ListEnabledAPINodes 列出单页的API节点
func (this *APINodeDAO) ListEnabledAPINodes(tx *dbs.Tx, offset int64, size int64) (result []*APINode, err error) {
_, err = this.Query(tx).
Attr("clusterId", 0). // 非集群专用
@@ -223,7 +225,7 @@ func (this *APINodeDAO) ListEnabledAPINodes(tx *dbs.Tx, offset int64, size int64
return
}
// 根据主机名和端口获取ID
// FindEnabledAPINodeIdWithAddr 根据主机名和端口获取ID
func (this *APINodeDAO) FindEnabledAPINodeIdWithAddr(tx *dbs.Tx, protocol string, host string, port int) (int64, error) {
addr := maps.Map{
"protocol": protocol,
@@ -250,7 +252,7 @@ func (this *APINodeDAO) FindEnabledAPINodeIdWithAddr(tx *dbs.Tx, protocol string
return int64(one.(*APINode).Id), nil
}
// 设置API节点状态
// UpdateAPINodeStatus 设置API节点状态
func (this *APINodeDAO) UpdateAPINodeStatus(tx *dbs.Tx, apiNodeId int64, statusJSON []byte) error {
_, err := this.Query(tx).
Pk(apiNodeId).
@@ -275,3 +277,13 @@ func (this *APINodeDAO) genUniqueId(tx *dbs.Tx) (string, error) {
return uniqueId, nil
}
}
// CountAllLowerVersionNodes 计算所有节点中低于某个版本的节点数量
func (this *APINodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
return this.Query(tx).
State(APINodeStateEnabled).
Where("status IS NOT NULL").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("version", utils.VersionToLong(version)).
Count()
}

View File

@@ -1,6 +1,7 @@
package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -34,7 +35,7 @@ func init() {
})
}
// 启用条目
// EnableApiToken 启用条目
func (this *ApiTokenDAO) EnableApiToken(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -42,7 +43,7 @@ func (this *ApiTokenDAO) EnableApiToken(tx *dbs.Tx, id uint32) (rowsAffected int
Update()
}
// 禁用条目
// DisableApiToken 禁用条目
func (this *ApiTokenDAO) DisableApiToken(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -50,7 +51,7 @@ func (this *ApiTokenDAO) DisableApiToken(tx *dbs.Tx, id uint32) (rowsAffected in
Update()
}
// 查找启用中的条目
// FindEnabledApiToken 查找启用中的条目
func (this *ApiTokenDAO) FindEnabledApiToken(tx *dbs.Tx, id uint32) (*ApiToken, error) {
result, err := this.Query(tx).
Pk(id).
@@ -62,7 +63,7 @@ func (this *ApiTokenDAO) FindEnabledApiToken(tx *dbs.Tx, id uint32) (*ApiToken,
return result.(*ApiToken), err
}
// 获取可缓存的节点Token信息
// FindEnabledTokenWithNodeCacheable 获取可缓存的节点Token信息
func (this *ApiTokenDAO) FindEnabledTokenWithNodeCacheable(tx *dbs.Tx, nodeId string) (*ApiToken, error) {
SharedCacheLocker.RLock()
token, ok := apiTokenCacheMap[nodeId]
@@ -85,7 +86,7 @@ func (this *ApiTokenDAO) FindEnabledTokenWithNodeCacheable(tx *dbs.Tx, nodeId st
return nil, err
}
// 获取节点Token信息并可以缓存
// FindEnabledTokenWithNode 获取节点Token信息并可以缓存
func (this *ApiTokenDAO) FindEnabledTokenWithNode(tx *dbs.Tx, nodeId string) (*ApiToken, error) {
one, err := this.Query(tx).
Attr("nodeId", nodeId).
@@ -97,7 +98,7 @@ func (this *ApiTokenDAO) FindEnabledTokenWithNode(tx *dbs.Tx, nodeId string) (*A
return nil, err
}
// 根据角色获取节点
// FindEnabledTokenWithRole 根据角色获取节点
func (this *ApiTokenDAO) FindEnabledTokenWithRole(tx *dbs.Tx, role string) (*ApiToken, error) {
one, err := this.Query(tx).
Attr("role", role).
@@ -109,8 +110,8 @@ func (this *ApiTokenDAO) FindEnabledTokenWithRole(tx *dbs.Tx, role string) (*Api
return nil, err
}
// 保存API Token
func (this *ApiTokenDAO) CreateAPIToken(tx *dbs.Tx, nodeId string, secret string, role NodeRole) error {
// CreateAPIToken 保存API Token
func (this *ApiTokenDAO) CreateAPIToken(tx *dbs.Tx, nodeId string, secret string, role nodeconfigs.NodeRole) error {
op := NewApiTokenOperator()
op.NodeId = nodeId
op.Secret = secret

View File

@@ -2,9 +2,11 @@ package authority
import (
"encoding/json"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
@@ -26,6 +28,9 @@ var SharedAuthorityKeyDAO *AuthorityKeyDAO
func init() {
dbs.OnReady(func() {
SharedAuthorityKeyDAO = NewAuthorityKeyDAO()
// 初始化IsPlus值
_, _ = SharedAuthorityKeyDAO.IsPlus(nil)
})
}
@@ -72,7 +77,14 @@ func (this *AuthorityKeyDAO) ReadKey(tx *dbs.Tx) (key *AuthorityKey, err error)
if one == nil {
return nil, nil
}
return one.(*AuthorityKey), nil
key = one.(*AuthorityKey)
// 顺便更新相关变量
if key.DayTo >= timeutil.Format("Y-m-d") {
teaconst.IsPlus = true
}
return
}
// ResetKey 重置Key
@@ -81,3 +93,16 @@ 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

@@ -7,17 +7,17 @@ import (
)
func TestAuthorityKeyDAO_UpdateValue(t *testing.T) {
err := NewAuthorityKeyDAO().UpdateValue(nil, "12345678")
err := NewAuthorityKeyDAO().UpdateKey(nil, "12345678", "", "", "", []string{}, "")
if err != nil {
t.Fatal(err)
}
t.Logf("ok")
t.Log("ok")
}
func TestAuthorityKeyDAO_ReadValue(t *testing.T) {
value, err := NewAuthorityKeyDAO().ReadValue(nil)
value, err := NewAuthorityKeyDAO().ReadKey(nil)
if err != nil {
t.Fatal(err)
}
t.Logf(value)
t.Log(value)
}

View File

@@ -3,6 +3,8 @@ package authority
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"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"
@@ -112,7 +114,7 @@ func (this *AuthorityNodeDAO) CreateAuthorityNode(tx *dbs.Tx, name string, descr
return 0, err
}
secret := rands.String(32)
err = models.NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, models.NodeRoleAuthority)
err = models.NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleAuthority)
if err != nil {
return
}
@@ -196,3 +198,13 @@ func (this *AuthorityNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJ
Update()
return err
}
// CountAllLowerVersionNodes 计算所有节点中低于某个版本的节点数量
func (this *AuthorityNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
return this.Query(tx).
State(AuthorityNodeStateEnabled).
Where("status IS NOT NULL").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("version", utils.VersionToLong(version)).
Count()
}

View File

@@ -3,6 +3,7 @@ package models
import (
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
@@ -15,16 +16,29 @@ import (
"time"
)
var accessLogDBMapping = map[int64]*dbs.DB{} // dbNodeId => DB
var accessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO
var accessLogDBMapping = map[int64]*dbs.DB{} // dbNodeId => DB
var accessLogLocker = &sync.RWMutex{}
var accessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true
// HTTP服务访问
var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO
var httpAccessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true
// DNS服务访问
var nsAccessLogDAOMapping = map[int64]*NSAccessLogDAOWrapper{} // dbNodeId => DAO
var nsAccessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
type HTTPAccessLogDAOWrapper struct {
DAO *HTTPAccessLogDAO
NodeId int64
}
// NSAccessLogDAOWrapper NS访问日志DAO
type NSAccessLogDAOWrapper struct {
DAO *NSAccessLogDAO
NodeId int64
}
func init() {
initializer := NewDBNodeInitializer()
dbs.OnReadyDone(func() {
@@ -33,12 +47,26 @@ func init() {
}
// 获取获取DAO
func randomAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
accessLogLocker.RLock()
if len(accessLogDAOMapping) == 0 {
if len(httpAccessLogDAOMapping) == 0 {
dao = nil
} else {
for _, d := range accessLogDAOMapping {
for _, d := range httpAccessLogDAOMapping {
dao = d
break
}
}
accessLogLocker.RUnlock()
return
}
func randomNSAccessLogDAO() (dao *NSAccessLogDAOWrapper) {
accessLogLocker.RLock()
if len(nsAccessLogDAOMapping) == 0 {
dao = nil
} else {
for _, d := range nsAccessLogDAOMapping {
dao = d
break
}
@@ -48,7 +76,7 @@ func randomAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
}
// 检查表格是否存在
func findAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
func findHTTPAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
err = errors.New("invalid day '" + day + "', should be YYYYMMDD")
return
@@ -63,7 +91,36 @@ func findAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool,
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
accessLogLocker.RLock()
_, ok = accessLogTableMapping[cacheKey]
_, ok = httpAccessLogTableMapping[cacheKey]
accessLogLocker.RUnlock()
if ok {
return tableName, true, nil
}
tableNames, err := db.TableNames()
if err != nil {
return tableName, false, err
}
return tableName, lists.ContainsString(tableNames, tableName), nil
}
func findNSAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
err = errors.New("invalid day '" + day + "', should be YYYYMMDD")
return
}
config, err := db.Config()
if err != nil {
return "", false, err
}
tableName = "edgeNSAccessLogs_" + day
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
accessLogLocker.RLock()
_, ok = nsAccessLogTableMapping[cacheKey]
accessLogLocker.RUnlock()
if ok {
return tableName, true, nil
@@ -78,7 +135,7 @@ func findAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool,
}
// 根据日期获取表名
func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
func findHTTPAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
config, err := db.Config()
if err != nil {
return "", err
@@ -89,7 +146,7 @@ func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
if !force {
accessLogLocker.RLock()
_, ok := accessLogTableMapping[cacheKey]
_, ok := httpAccessLogTableMapping[cacheKey]
accessLogLocker.RUnlock()
if ok {
return tableName, nil
@@ -103,7 +160,7 @@ func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
if lists.ContainsString(tableNames, tableName) {
accessLogLocker.Lock()
accessLogTableMapping[cacheKey] = true
httpAccessLogTableMapping[cacheKey] = true
accessLogLocker.Unlock()
return tableName, nil
}
@@ -115,13 +172,56 @@ func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
}
accessLogLocker.Lock()
accessLogTableMapping[cacheKey] = true
httpAccessLogTableMapping[cacheKey] = true
accessLogLocker.Unlock()
return tableName, nil
}
// 初始化数据库连接
func findNSAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
config, err := db.Config()
if err != nil {
return "", err
}
tableName := "edgeNSAccessLogs_" + day
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
if !force {
accessLogLocker.RLock()
_, ok := nsAccessLogTableMapping[cacheKey]
accessLogLocker.RUnlock()
if ok {
return tableName, nil
}
}
tableNames, err := db.TableNames()
if err != nil {
return tableName, err
}
if lists.ContainsString(tableNames, tableName) {
accessLogLocker.Lock()
nsAccessLogTableMapping[cacheKey] = true
accessLogLocker.Unlock()
return tableName, nil
}
// 创建表格
_, err = db.Exec("CREATE TABLE `" + tableName + "` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordId` int(11) unsigned DEFAULT '0' COMMENT '记录ID',\n `content` json DEFAULT NULL COMMENT '访问数据',\n `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n PRIMARY KEY (`id`),\n KEY `nodeId` (`nodeId`),\n KEY `domainId` (`domainId`),\n KEY `recordId` (`recordId`),\n KEY `requestId` (`requestId`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='域名服务访问日志';")
if err != nil {
return tableName, err
}
accessLogLocker.Lock()
nsAccessLogTableMapping[cacheKey] = true
accessLogLocker.Unlock()
return tableName, nil
}
// DBNodeInitializer 初始化数据库连接
type DBNodeInitializer struct {
}
@@ -129,7 +229,7 @@ func NewDBNodeInitializer() *DBNodeInitializer {
return &DBNodeInitializer{}
}
// 启动
// Start 启动
func (this *DBNodeInitializer) Start() {
// 初始运行
err := this.loop()
@@ -166,7 +266,8 @@ func (this *DBNodeInitializer) loop() error {
if !lists.ContainsInt64(nodeIds, nodeId) {
closingDbs = append(closingDbs, db)
delete(accessLogDBMapping, nodeId)
delete(accessLogDAOMapping, nodeId)
delete(httpAccessLogDAOMapping, nodeId)
delete(nsAccessLogDAOMapping, nodeId)
logs.Println("[DB_NODE]close db node '" + strconv.FormatInt(nodeId, 10) + "'")
}
}
@@ -212,46 +313,94 @@ func (this *DBNodeInitializer) loop() error {
}
// 检查表是否存在
tableName, err := findAccessLogTable(db, timeutil.Format("Ymd"), false)
if err != nil {
if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
// httpAccessLog
{
tableName, err := findHTTPAccessLogTable(db, timeutil.Format("Ymd"), false)
if err != nil {
if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, NodeRoleDatabase, nodeId, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
if createLogErr != nil {
logs.Println("[NODE_LOG]" + createLogErr.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
if createLogErr != nil {
logs.Println("[NODE_LOG]" + createLogErr.Error())
}
continue
} else {
err = nil
}
continue
} else {
err = nil
}
daoObject := dbs.DAOObject{
Instance: db,
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
Table: tableName,
PkName: "id",
Model: new(HTTPAccessLog),
}
err = daoObject.Init()
if err != nil {
logs.Println("[DB_NODE]initialize dao failed: " + err.Error())
continue
}
accessLogLocker.Lock()
accessLogDBMapping[nodeId] = db
dao := &HTTPAccessLogDAO{
DAOObject: daoObject,
}
httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
DAO: dao,
NodeId: nodeId,
}
accessLogLocker.Unlock()
}
daoObject := dbs.DAOObject{
Instance: db,
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
Table: tableName,
PkName: "id",
Model: new(HTTPAccessLog),
}
err = daoObject.Init()
if err != nil {
logs.Println("[DB_NODE]initialize dao failed: " + err.Error())
continue
}
accessLogLocker.Lock()
accessLogDBMapping[nodeId] = db
dao := &HTTPAccessLogDAO{
DAOObject: daoObject,
// nsAccessLog
{
tableName, err := findNSAccessLogTable(db, timeutil.Format("Ymd"), false)
if err != nil {
if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
if createLogErr != nil {
logs.Println("[NODE_LOG]" + createLogErr.Error())
}
continue
} else {
err = nil
}
}
daoObject := dbs.DAOObject{
Instance: db,
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
Table: tableName,
PkName: "id",
Model: new(NSAccessLog),
}
err = daoObject.Init()
if err != nil {
logs.Println("[DB_NODE]initialize dao failed: " + err.Error())
continue
}
accessLogLocker.Lock()
accessLogDBMapping[nodeId] = db
dao := &NSAccessLogDAO{
DAOObject: daoObject,
}
nsAccessLogDAOMapping[nodeId] = &NSAccessLogDAOWrapper{
DAO: dao,
NodeId: nodeId,
}
accessLogLocker.Unlock()
}
accessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
DAO: dao,
NodeId: nodeId,
}
accessLogLocker.Unlock()
}
}

View File

@@ -12,13 +12,13 @@ func TestDBNodeInitializer_loop(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Log(len(accessLogDBMapping), len(accessLogDAOMapping))
t.Log(len(accessLogDBMapping), len(httpAccessLogDAOMapping))
}
func TestFindAccessLogTable(t *testing.T) {
before := time.Now()
db := SharedHTTPAccessLogDAO.Instance
tableName, err := findAccessLogTable(db, "20201010", false)
tableName, err := findHTTPAccessLogTable(db, "20201010", false)
if err != nil {
t.Fatal(err)
}
@@ -26,7 +26,7 @@ func TestFindAccessLogTable(t *testing.T) {
t.Log(time.Since(before).Seconds()*1000, "ms")
before = time.Now()
tableName, err = findAccessLogTable(db, "20201010", false)
tableName, err = findHTTPAccessLogTable(db, "20201010", false)
if err != nil {
t.Fatal(err)
@@ -40,6 +40,6 @@ func BenchmarkFindAccessLogTable(b *testing.B) {
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
_, _ = findAccessLogTable(db, "20201010", false)
_, _ = findHTTPAccessLogTable(db, "20201010", false)
}
}

View File

@@ -2,7 +2,7 @@ package dns
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -39,7 +39,7 @@ func init() {
})
}
// 启用条目
// EnableDNSDomain 启用条目
func (this *DNSDomainDAO) EnableDNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -48,7 +48,7 @@ func (this *DNSDomainDAO) EnableDNSDomain(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableDNSDomain 禁用条目
func (this *DNSDomainDAO) DisableDNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -57,7 +57,7 @@ func (this *DNSDomainDAO) DisableDNSDomain(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledDNSDomain 查找启用中的条目
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, id int64) (*DNSDomain, error) {
result, err := this.Query(tx).
Pk(id).
@@ -69,7 +69,7 @@ func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, id int64) (*DNSDomain
return result.(*DNSDomain), err
}
// 根据主键查找名称
// FindDNSDomainName 根据主键查找名称
func (this *DNSDomainDAO) FindDNSDomainName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -77,7 +77,7 @@ func (this *DNSDomainDAO) FindDNSDomainName(tx *dbs.Tx, id int64) (string, error
FindStringCol("")
}
// 创建域名
// CreateDomain 创建域名
func (this *DNSDomainDAO) CreateDomain(tx *dbs.Tx, adminId int64, userId int64, providerId int64, name string) (int64, error) {
op := NewDNSDomainOperator()
op.ProviderId = providerId
@@ -93,7 +93,7 @@ func (this *DNSDomainDAO) CreateDomain(tx *dbs.Tx, adminId int64, userId int64,
return types.Int64(op.Id), nil
}
// 修改域名
// UpdateDomain 修改域名
func (this *DNSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, name string, isOn bool) error {
if domainId <= 0 {
return errors.New("invalid domainId")
@@ -109,7 +109,7 @@ func (this *DNSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, name string,
return nil
}
// 查询一个服务商下面的所有域名
// FindAllEnabledDomainsWithProviderId 查询一个服务商下面的所有域名
func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (result []*DNSDomain, err error) {
_, err = this.Query(tx).
State(DNSDomainStateEnabled).
@@ -120,7 +120,7 @@ func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(tx *dbs.Tx, provid
return
}
// 计算某个服务商下的域名数量
// CountAllEnabledDomainsWithProviderId 计算某个服务商下的域名数量
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (int64, error) {
return this.Query(tx).
State(DNSDomainStateEnabled).
@@ -128,7 +128,7 @@ func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, provi
Count()
}
// 更新域名数据
// UpdateDomainData 更新域名数据
func (this *DNSDomainDAO) UpdateDomainData(tx *dbs.Tx, domainId int64, data string) error {
if domainId <= 0 {
return errors.New("invalid domainId")
@@ -140,7 +140,7 @@ func (this *DNSDomainDAO) UpdateDomainData(tx *dbs.Tx, domainId int64, data stri
return err
}
// 更新域名解析记录
// UpdateDomainRecords 更新域名解析记录
func (this *DNSDomainDAO) UpdateDomainRecords(tx *dbs.Tx, domainId int64, recordsJSON []byte) error {
if domainId <= 0 {
return errors.New("invalid domainId")
@@ -153,7 +153,7 @@ func (this *DNSDomainDAO) UpdateDomainRecords(tx *dbs.Tx, domainId int64, record
return err
}
// 更新线路
// UpdateDomainRoutes 更新线路
func (this *DNSDomainDAO) UpdateDomainRoutes(tx *dbs.Tx, domainId int64, routesJSON []byte) error {
if domainId <= 0 {
return errors.New("invalid domainId")
@@ -166,8 +166,8 @@ func (this *DNSDomainDAO) UpdateDomainRoutes(tx *dbs.Tx, domainId int64, routesJ
return err
}
// 查找域名线路
func (this *DNSDomainDAO) FindDomainRoutes(tx *dbs.Tx, domainId int64) ([]*dnsclients.Route, error) {
// FindDomainRoutes 查找域名线路
func (this *DNSDomainDAO) FindDomainRoutes(tx *dbs.Tx, domainId int64) ([]*dnstypes.Route, error) {
routes, err := this.Query(tx).
Pk(domainId).
Result("routes").
@@ -178,7 +178,7 @@ func (this *DNSDomainDAO) FindDomainRoutes(tx *dbs.Tx, domainId int64) ([]*dnscl
if len(routes) == 0 || routes == "null" {
return nil, nil
}
result := []*dnsclients.Route{}
result := []*dnstypes.Route{}
err = json.Unmarshal([]byte(routes), &result)
if err != nil {
return nil, err
@@ -186,7 +186,7 @@ func (this *DNSDomainDAO) FindDomainRoutes(tx *dbs.Tx, domainId int64) ([]*dnscl
return result, nil
}
// 查找线路名称
// FindDomainRouteName 查找线路名称
func (this *DNSDomainDAO) FindDomainRouteName(tx *dbs.Tx, domainId int64, routeCode string) (string, error) {
routes, err := this.FindDomainRoutes(tx, domainId)
if err != nil {
@@ -200,7 +200,7 @@ func (this *DNSDomainDAO) FindDomainRouteName(tx *dbs.Tx, domainId int64, routeC
return "", nil
}
// 判断是否有域名可选
// ExistAvailableDomains 判断是否有域名可选
func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
subQuery, err := SharedDNSProviderDAO.Query(tx).
Where("state=1"). // 这里要使用非变量
@@ -216,7 +216,7 @@ func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
Exist()
}
// 检查域名解析记录是否存在
// ExistDomainRecord 检查域名解析记录是否存在
func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordName string, recordType string, recordRoute string, recordValue string) (bool, error) {
query := maps.Map{
"name": recordName,

View File

@@ -2,15 +2,15 @@ package dns
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
)
// 获取所有的线路
func (this *DNSDomain) DecodeRoutes() ([]*dnsclients.Route, error) {
// DecodeRoutes 获取所有的线路
func (this *DNSDomain) DecodeRoutes() ([]*dnstypes.Route, error) {
if len(this.Routes) == 0 || this.Routes == "null" {
return nil, nil
}
result := []*dnsclients.Route{}
result := []*dnstypes.Route{}
err := json.Unmarshal([]byte(this.Routes), &result)
if err != nil {
return nil, err
@@ -18,7 +18,7 @@ func (this *DNSDomain) DecodeRoutes() ([]*dnsclients.Route, error) {
return result, nil
}
// 检查是否包含某个线路
// ContainsRouteCode 检查是否包含某个线路
func (this *DNSDomain) ContainsRouteCode(route string) (bool, error) {
routes, err := this.DecodeRoutes()
if err != nil {
@@ -32,13 +32,13 @@ func (this *DNSDomain) ContainsRouteCode(route string) (bool, error) {
return false, nil
}
// 获取所有的记录
func (this *DNSDomain) DecodeRecords() ([]*dnsclients.Record, error) {
// DecodeRecords 获取所有的记录
func (this *DNSDomain) DecodeRecords() ([]*dnstypes.Record, error) {
records := this.Records
if len(records) == 0 || records == "null" {
return nil, nil
}
result := []*dnsclients.Record{}
result := []*dnstypes.Record{}
err := json.Unmarshal([]byte(records), &result)
if err != nil {
return nil, err

View File

@@ -12,6 +12,7 @@ import (
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"net/http"
"regexp"
"sort"
"strconv"
@@ -41,9 +42,9 @@ func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
}).(*HTTPAccessLogDAO)
}
// 创建访问日志
// CreateHTTPAccessLogs 创建访问日志
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.HTTPAccessLog) error {
dao := randomAccessLogDAO()
dao := randomHTTPAccessLogDAO()
if dao == nil {
dao = &HTTPAccessLogDAOWrapper{
DAO: SharedHTTPAccessLogDAO,
@@ -53,7 +54,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.
return this.CreateHTTPAccessLogsWithDAO(tx, dao, accessLogs)
}
// 使用特定的DAO创建访问日志
// CreateHTTPAccessLogsWithDAO 使用特定的DAO创建访问日志
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *HTTPAccessLogDAOWrapper, accessLogs []*pb.HTTPAccessLog) error {
if daoWrapper == nil {
return errors.New("dao should not be nil")
@@ -68,7 +69,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper
for _, accessLog := range accessLogs {
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
table, err := findAccessLogTable(dao.Instance, day, false)
table, err := findHTTPAccessLogTable(dao.Instance, day, false)
if err != nil {
return err
}
@@ -97,7 +98,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper
if err != nil {
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
if strings.Contains(err.Error(), "1146") {
table, err = findAccessLogTable(dao.Instance, day, true)
table, err = findHTTPAccessLogTable(dao.Instance, day, true)
if err != nil {
return err
}
@@ -115,8 +116,8 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper
return nil
}
// 读取往前的 单页访问日志
func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64) (result []*HTTPAccessLog, nextLastRequestId string, hasMore bool, err error) {
// ListAccessLogs 读取往前的 单页访问日志
func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64, keyword string) (result []*HTTPAccessLog, nextLastRequestId string, hasMore bool, err error) {
if len(day) != 8 {
return
}
@@ -126,18 +127,18 @@ func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, s
size = 1000
}
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId)
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword)
if err != nil || int64(len(result)) < size {
return
}
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId)
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword)
hasMore = len(moreResult) > 0
return
}
// 读取往前的单页访问日志
func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64) (result []*HTTPAccessLog, nextLastRequestId string, err error) {
func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64, keyword string) (result []*HTTPAccessLog, nextLastRequestId string, err error) {
if size <= 0 {
return nil, lastRequestId, nil
}
@@ -155,7 +156,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
accessLogLocker.RLock()
daoList := []*HTTPAccessLogDAOWrapper{}
for _, daoWrapper := range accessLogDAOMapping {
for _, daoWrapper := range httpAccessLogDAOMapping {
daoList = append(daoList, daoWrapper)
}
accessLogLocker.RUnlock()
@@ -178,7 +179,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
dao := daoWrapper.DAO
tableName, exists, err := findAccessLogTableName(dao.Instance, day)
tableName, exists, err := findHTTPAccessLogTableName(dao.Instance, day)
if !exists {
// 表格不存在则跳过
return
@@ -213,6 +214,46 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
query.Where("firewallPolicyId>0")
}
// keyword
if len(keyword) > 0 {
useOriginKeyword := false
where := "JSON_EXTRACT(content, '$.remoteAddr') LIKE :keyword OR JSON_EXTRACT(content, '$.requestURI') LIKE :keyword OR JSON_EXTRACT(content, '$.host') LIKE :keyword"
// 请求方法
if keyword == http.MethodGet ||
keyword == http.MethodPost ||
keyword == http.MethodHead ||
keyword == http.MethodConnect ||
keyword == http.MethodPut ||
keyword == http.MethodTrace ||
keyword == http.MethodOptions ||
keyword == http.MethodDelete ||
keyword == http.MethodPatch {
where += " OR JSON_EXTRACT(content, '$.requestMethod')=:originKeyword"
useOriginKeyword = true
}
// 响应状态码
if regexp.MustCompile(`^\d{3}$`).MatchString(keyword) {
where += " OR JSON_EXTRACT(content, '$.status')=:intKeyword"
query.Param("intKeyword", types.Int(keyword))
}
if regexp.MustCompile(`^\d{3}-\d{3}$`).MatchString(keyword) {
pieces := strings.Split(keyword, "-")
where += " OR JSON_EXTRACT(content, '$.status') BETWEEN :intKeyword1 AND :intKeyword2"
query.Param("intKeyword1", types.Int(pieces[0]))
query.Param("intKeyword2", types.Int(pieces[1]))
}
query.Where("("+where+")").
Param("keyword", "%"+keyword+"%")
if useOriginKeyword {
query.Param("originKeyword", keyword)
}
}
// offset
if len(lastRequestId) > 0 {
if !reverse {
@@ -278,7 +319,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
}
}
// 根据请求ID获取访问日志
// FindAccessLogWithRequestId 根据请求ID获取访问日志
func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*HTTPAccessLog, error) {
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
return nil, errors.New("invalid requestId")
@@ -286,7 +327,7 @@ func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId s
accessLogLocker.RLock()
daoList := []*HTTPAccessLogDAOWrapper{}
for _, daoWrapper := range accessLogDAOMapping {
for _, daoWrapper := range httpAccessLogDAOMapping {
daoList = append(daoList, daoWrapper)
}
accessLogLocker.RUnlock()
@@ -309,7 +350,7 @@ func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId s
dao := daoWrapper.DAO
tableName, exists, err := findAccessLogTableName(dao.Instance, day)
tableName, exists, err := findHTTPAccessLogTableName(dao.Instance, day)
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return

View File

@@ -24,7 +24,7 @@ func TestCreateHTTPAccessLogs(t *testing.T) {
Status: 200,
Timestamp: time.Now().Unix(),
}
dao := randomAccessLogDAO()
dao := randomHTTPAccessLogDAO()
t.Log("dao:", dao)
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLogsWithDAO(tx, dao, []*pb.HTTPAccessLog{accessLog})
if err != nil {
@@ -41,7 +41,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs(t *testing.T) {
t.Fatal(err)
}
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "", 10, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "", 10, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0, "")
if err != nil {
t.Fatal(err)
}
@@ -68,7 +68,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Page(t *testing.T) {
times := 0 // 防止循环次数太多
for {
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0, "")
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
@@ -99,7 +99,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Reverse(t *testing.T) {
}
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), 0, true, false, 0, 0, 0, false, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), 0, true, false, 0, 0, 0, false, 0, "")
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
@@ -124,7 +124,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Page_NotExists(t *testing.T) {
times := 0 // 防止循环次数太多
for {
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), 0, false, false, 0, 0, 0, false, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), 0, false, false, 0, 0, 0, false, 0, "")
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)

View File

@@ -5,7 +5,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// 转换成PB对象
// ToPB 转换成PB对象
func (this *HTTPAccessLog) ToPB() (*pb.HTTPAccessLog, error) {
p := &pb.HTTPAccessLog{}
err := json.Unmarshal([]byte(this.Content), p)

View File

@@ -37,12 +37,12 @@ func init() {
})
}
// 初始化
// Init 初始化
func (this *HTTPCachePolicyDAO) Init() {
_ = this.DAOObject.Init()
}
// 启用条目
// EnableHTTPCachePolicy 启用条目
func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -51,7 +51,7 @@ func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) erro
return err
}
// 禁用条目
// DisableHTTPCachePolicy 禁用条目
func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int64) error {
_, err := this.Query(tx).
Pk(policyId).
@@ -63,7 +63,7 @@ func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int6
return this.NotifyUpdate(tx, policyId)
}
// 查找启用中的条目
// FindEnabledHTTPCachePolicy 查找启用中的条目
func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64) (*HTTPCachePolicy, error) {
result, err := this.Query(tx).
Pk(id).
@@ -75,7 +75,7 @@ func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64)
return result.(*HTTPCachePolicy), err
}
// 根据主键查找名称
// FindHTTPCachePolicyName 根据主键查找名称
func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -83,7 +83,7 @@ func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(tx *dbs.Tx, id int64) (s
FindStringCol("")
}
// 查找所有可用的缓存策略
// FindAllEnabledCachePolicies 查找所有可用的缓存策略
func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result []*HTTPCachePolicy, err error) {
_, err = this.Query(tx).
State(HTTPCachePolicyStateEnabled).
@@ -93,7 +93,7 @@ func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result
return
}
// 创建缓存策略
// CreateCachePolicy 创建缓存策略
func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) (int64, error) {
op := NewHTTPCachePolicyOperator()
op.State = HTTPCachePolicyStateEnabled
@@ -111,14 +111,51 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st
if len(storageOptionsJSON) > 0 {
op.Options = storageOptionsJSON
}
err := this.Save(tx, op)
// 默认的缓存条件
cacheRef := &serverconfigs.HTTPCacheRef{
IsOn: true,
Key: "${scheme}://${host}${requestURI}",
Life: &shared.TimeDuration{Count: 2, Unit: shared.TimeDurationUnitHour},
Status: []int{200},
MaxSize: &shared.SizeCapacity{Count: 32, Unit: shared.SizeCapacityUnitMB},
SkipResponseSetCookie: true,
AllowChunkedEncoding: true,
Conds: &shared.HTTPRequestCondsConfig{
IsOn: true,
Connector: "or",
Groups: []*shared.HTTPRequestCondGroup{
{
IsOn: true,
Connector: "or",
Conds: []*shared.HTTPRequestCond{
{
Type: "url-extension",
IsRequest: true,
Param: "${requestPathExtension}",
Operator: shared.RequestCondOperatorIn,
Value: `[".html", ".js", ".css", ".gif", ".png", ".bmp", ".jpeg", ".jpg", ".webp", ".ico", ".pdf", ".ttf", ".eot", ".tiff", ".svg", ".svgz", ".eps", ".woff", ".otf", ".woff2", ".tif", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".wav", ".mp3", ".mp4", ".ogg", ".mid", ".midi"]`,
},
},
Description: "初始化规则",
},
},
},
}
refsJSON, err := json.Marshal([]*serverconfigs.HTTPCacheRef{cacheRef})
if err != nil {
return 0, err
}
op.Refs = refsJSON
err = this.Save(tx, op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
// 修改缓存策略
// UpdateCachePolicy 修改缓存策略
func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) error {
if policyId <= 0 {
return errors.New("invalid policyId")
@@ -147,7 +184,7 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
return this.NotifyUpdate(tx, policyId)
}
// 组合配置
// ComposeCachePolicy 组合配置
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (*serverconfigs.HTTPCachePolicy, error) {
policy, err := this.FindEnabledHTTPCachePolicy(tx, policyId)
if err != nil {
@@ -196,20 +233,39 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (
config.Options = m
}
// refs
if IsNotNull(policy.Refs) {
refs := []*serverconfigs.HTTPCacheRef{}
err = json.Unmarshal([]byte(policy.Refs), &refs)
if err != nil {
return nil, err
}
config.CacheRefs = refs
}
return config, nil
}
// 计算可用缓存策略数量
func (this *HTTPCachePolicyDAO) CountAllEnabledHTTPCachePolicies(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(HTTPCachePolicyStateEnabled).
Count()
// CountAllEnabledHTTPCachePolicies 计算可用缓存策略数量
func (this *HTTPCachePolicyDAO) CountAllEnabledHTTPCachePolicies(tx *dbs.Tx, keyword string) (int64, error) {
query := this.Query(tx).
State(HTTPCachePolicyStateEnabled)
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.Count()
}
// 列出单页的缓存策略
func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset int64, size int64) ([]*serverconfigs.HTTPCachePolicy, error) {
ones, err := this.Query(tx).
State(HTTPCachePolicyStateEnabled).
// ListEnabledHTTPCachePolicies 列出单页的缓存策略
func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, keyword string, offset int64, size int64) ([]*serverconfigs.HTTPCachePolicy, error) {
query := this.Query(tx).
State(HTTPCachePolicyStateEnabled)
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
ones, err := query.
ResultPk().
Offset(offset).
Limit(size).
@@ -237,7 +293,22 @@ func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset
return cachePolicies, nil
}
// 通知更新
// UpdatePolicyRefs 设置默认的缓存条件
func (this *HTTPCachePolicyDAO) UpdatePolicyRefs(tx *dbs.Tx, policyId int64, refsJSON []byte) error {
if len(refsJSON) == 0 {
return nil
}
err := this.Query(tx).
Pk(policyId).
Set("refs", refsJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, policyId)
}
// NotifyUpdate 通知更新
func (this *HTTPCachePolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithCachePolicyId(tx, policyId)
if err != nil {

View File

@@ -1,6 +1,6 @@
package models
// HTTP缓存策略
// HTTPCachePolicy HTTP缓存策略
type HTTPCachePolicy struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
@@ -16,6 +16,7 @@ type HTTPCachePolicy struct {
CreatedAt uint64 `field:"createdAt"` // 创建时间
State uint8 `field:"state"` // 状态
Description string `field:"description"` // 描述
Refs string `field:"refs"` // 默认的缓存设置
}
type HTTPCachePolicyOperator struct {
@@ -33,6 +34,7 @@ type HTTPCachePolicyOperator struct {
CreatedAt interface{} // 创建时间
State interface{} // 状态
Description interface{} // 描述
Refs interface{} // 默认的缓存设置
}
func NewHTTPCachePolicyOperator() *HTTPCachePolicyOperator {

View File

@@ -0,0 +1,201 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"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/types"
)
const (
HTTPFastcgiStateEnabled = 1 // 已启用
HTTPFastcgiStateDisabled = 0 // 已禁用
)
type HTTPFastcgiDAO dbs.DAO
func NewHTTPFastcgiDAO() *HTTPFastcgiDAO {
return dbs.NewDAO(&HTTPFastcgiDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeHTTPFastcgis",
Model: new(HTTPFastcgi),
PkName: "id",
},
}).(*HTTPFastcgiDAO)
}
var SharedHTTPFastcgiDAO *HTTPFastcgiDAO
func init() {
dbs.OnReady(func() {
SharedHTTPFastcgiDAO = NewHTTPFastcgiDAO()
})
}
// EnableHTTPFastcgi 启用条目
func (this *HTTPFastcgiDAO) EnableHTTPFastcgi(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", HTTPFastcgiStateEnabled).
Update()
return err
}
// DisableHTTPFastcgi 禁用条目
func (this *HTTPFastcgiDAO) DisableHTTPFastcgi(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", HTTPFastcgiStateDisabled).
Update()
return err
}
// FindEnabledHTTPFastcgi 查找启用中的条目
func (this *HTTPFastcgiDAO) FindEnabledHTTPFastcgi(tx *dbs.Tx, id int64) (*HTTPFastcgi, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", HTTPFastcgiStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*HTTPFastcgi), err
}
// ComposeFastcgiConfig 组合配置
func (this *HTTPFastcgiDAO) ComposeFastcgiConfig(tx *dbs.Tx, fastcgiId int64) (*serverconfigs.HTTPFastcgiConfig, error) {
if fastcgiId <= 0 {
return nil, nil
}
fastcgi, err := this.FindEnabledHTTPFastcgi(tx, fastcgiId)
if err != nil {
return nil, err
}
if fastcgi == nil {
return nil, nil
}
config := &serverconfigs.HTTPFastcgiConfig{}
config.Id = int64(fastcgi.Id)
config.IsOn = fastcgi.IsOn == 1
config.Address = fastcgi.Address
if IsNotNull(fastcgi.Params) {
params := []*serverconfigs.HTTPFastcgiParam{}
err = json.Unmarshal([]byte(fastcgi.Params), &params)
if err != nil {
return nil, err
}
config.Params = params
}
if IsNotNull(fastcgi.ReadTimeout) {
duration := &shared.TimeDuration{}
err = json.Unmarshal([]byte(fastcgi.ReadTimeout), duration)
if err != nil {
return nil, err
}
config.ReadTimeout = duration
}
if IsNotNull(fastcgi.ConnTimeout) {
duration := &shared.TimeDuration{}
err = json.Unmarshal([]byte(fastcgi.ConnTimeout), duration)
if err != nil {
return nil, err
}
config.ConnTimeout = duration
}
if fastcgi.PoolSize > 0 {
config.PoolSize = types.Int(fastcgi.PoolSize)
}
config.PathInfoPattern = fastcgi.PathInfoPattern
return config, nil
}
// CreateFastcgi 创建Fastcgi
func (this *HTTPFastcgiDAO) CreateFastcgi(tx *dbs.Tx, adminId int64, userId int64, isOn bool, address string, paramsJSON []byte, readTimeoutJSON []byte, connTimeoutJSON []byte, poolSize int32, pathInfoPattern string) (int64, error) {
op := NewHTTPFastcgiOperator()
op.AdminId = adminId
op.UserId = userId
op.IsOn = isOn
op.Address = address
if len(paramsJSON) > 0 {
op.Params = paramsJSON
}
if len(readTimeoutJSON) > 0 {
op.ReadTimeout = readTimeoutJSON
}
if len(connTimeoutJSON) > 0 {
op.ConnTimeout = connTimeoutJSON
}
op.PoolSize = poolSize
op.PathInfoPattern = pathInfoPattern
op.State = HTTPFastcgiStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateFastcgi 修改Fastcgi
func (this *HTTPFastcgiDAO) UpdateFastcgi(tx *dbs.Tx, fastcgiId int64, isOn bool, address string, paramsJSON []byte, readTimeoutJSON []byte, connTimeoutJSON []byte, poolSize int32, pathInfoPattern string) error {
if fastcgiId <= 0 {
return errors.New("invalid 'fastcgiId'")
}
op := NewHTTPFastcgiOperator()
op.Id = fastcgiId
op.IsOn = isOn
op.Address = address
if len(paramsJSON) > 0 {
op.Params = paramsJSON
}
if len(readTimeoutJSON) > 0 {
op.ReadTimeout = readTimeoutJSON
}
if len(connTimeoutJSON) > 0 {
op.ConnTimeout = connTimeoutJSON
}
op.PoolSize = poolSize
op.PathInfoPattern = pathInfoPattern
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, fastcgiId)
}
// CheckUserFastcgi 检查用户Fastcgi权限
func (this *HTTPFastcgiDAO) CheckUserFastcgi(tx *dbs.Tx, userId int64, fastcgiId int64) error {
if userId <= 0 || fastcgiId <= 0 {
return errors.New("permission error")
}
exists, err := this.Query(tx).
Pk(fastcgiId).
Attr("userId", userId).
State(HTTPFastcgiStateEnabled).
Exist()
if err != nil {
return err
}
if !exists {
return errors.New("permission error")
}
return nil
}
// NotifyUpdate 通知更新
func (this *HTTPFastcgiDAO) NotifyUpdate(tx *dbs.Tx, fastcgiId int64) error {
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithFastcgiId(tx, fastcgiId)
if err != nil {
return err
}
if webId > 0 {
return SharedHTTPWebDAO.NotifyUpdate(tx, webId)
}
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,34 @@
package models
// HTTPFastcgi Fastcgi设置
type HTTPFastcgi struct {
Id uint64 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用
Address string `field:"address"` // 地址
Params string `field:"params"` // 参数
ReadTimeout string `field:"readTimeout"` // 读取超时
ConnTimeout string `field:"connTimeout"` // 连接超时
PoolSize uint32 `field:"poolSize"` // 连接池尺寸
PathInfoPattern string `field:"pathInfoPattern"` // PATH_INFO匹配
State uint8 `field:"state"` // 状态
}
type HTTPFastcgiOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
Address interface{} // 地址
Params interface{} // 参数
ReadTimeout interface{} // 读取超时
ConnTimeout interface{} // 连接超时
PoolSize interface{} // 连接池尺寸
PathInfoPattern interface{} // PATH_INFO匹配
State interface{} // 状态
}
func NewHTTPFastcgiOperator() *HTTPFastcgiOperator {
return &HTTPFastcgiOperator{}
}

View File

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

View File

@@ -37,12 +37,12 @@ func init() {
})
}
// 初始化
// Init 初始化
func (this *HTTPFirewallPolicyDAO) Init() {
_ = this.DAOObject.Init()
}
// 启用条目
// EnableHTTPFirewallPolicy 启用条目
func (this *HTTPFirewallPolicyDAO) EnableHTTPFirewallPolicy(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -51,7 +51,7 @@ func (this *HTTPFirewallPolicyDAO) EnableHTTPFirewallPolicy(tx *dbs.Tx, id int64
return err
}
// 禁用条目
// DisableHTTPFirewallPolicy 禁用条目
func (this *HTTPFirewallPolicyDAO) DisableHTTPFirewallPolicy(tx *dbs.Tx, policyId int64) error {
_, err := this.Query(tx).
Pk(policyId).
@@ -64,7 +64,7 @@ func (this *HTTPFirewallPolicyDAO) DisableHTTPFirewallPolicy(tx *dbs.Tx, policyI
return this.NotifyUpdate(tx, policyId)
}
// 查找启用中的条目
// FindEnabledHTTPFirewallPolicy 查找启用中的条目
func (this *HTTPFirewallPolicyDAO) FindEnabledHTTPFirewallPolicy(tx *dbs.Tx, id int64) (*HTTPFirewallPolicy, error) {
result, err := this.Query(tx).
Pk(id).
@@ -76,7 +76,7 @@ func (this *HTTPFirewallPolicyDAO) FindEnabledHTTPFirewallPolicy(tx *dbs.Tx, id
return result.(*HTTPFirewallPolicy), err
}
// 根据主键查找名称
// FindHTTPFirewallPolicyName 根据主键查找名称
func (this *HTTPFirewallPolicyDAO) FindHTTPFirewallPolicyName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -84,7 +84,7 @@ func (this *HTTPFirewallPolicyDAO) FindHTTPFirewallPolicyName(tx *dbs.Tx, id int
FindStringCol("")
}
// 查找所有可用策略
// FindAllEnabledFirewallPolicies 查找所有可用策略
func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (result []*HTTPFirewallPolicy, err error) {
_, err = this.Query(tx).
State(HTTPFirewallPolicyStateEnabled).
@@ -94,7 +94,7 @@ func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (r
return
}
// 创建策略
// CreateFirewallPolicy 创建策略
func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64, serverId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) (int64, error) {
op := NewHTTPFirewallPolicyOperator()
op.UserId = userId
@@ -113,7 +113,7 @@ func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64
return types.Int64(op.Id), err
}
// 修改策略的Inbound和Outbound
// UpdateFirewallPolicyInboundAndOutbound 修改策略的Inbound和Outbound
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInboundAndOutbound(tx *dbs.Tx, policyId int64, inboundJSON []byte, outboundJSON []byte) error {
if policyId <= 0 {
return errors.New("invalid policyId")
@@ -138,7 +138,7 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInboundAndOutbound(tx *db
return this.NotifyUpdate(tx, policyId)
}
// 修改策略的Inbound
// UpdateFirewallPolicyInbound 修改策略的Inbound
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInbound(tx *dbs.Tx, policyId int64, inboundJSON []byte) error {
if policyId <= 0 {
return errors.New("invalid policyId")
@@ -158,7 +158,7 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInbound(tx *dbs.Tx, polic
return this.NotifyUpdate(tx, policyId)
}
// 修改策略
// UpdateFirewallPolicy 修改策略
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte, blockOptionsJSON []byte) error {
if policyId <= 0 {
return errors.New("invalid policyId")
@@ -189,18 +189,28 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx, policyId int
return this.NotifyUpdate(tx, policyId)
}
// 计算所有可用的策略数量
func (this *HTTPFirewallPolicyDAO) CountAllEnabledFirewallPolicies(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
// CountAllEnabledFirewallPolicies 计算所有可用的策略数量
func (this *HTTPFirewallPolicyDAO) CountAllEnabledFirewallPolicies(tx *dbs.Tx, keyword string) (int64, error) {
query := this.Query(tx)
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.
State(HTTPFirewallPolicyStateEnabled).
Attr("userId", 0).
Attr("serverId", 0).
Count()
}
// 列出单页的策略
func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPFirewallPolicy, err error) {
_, err = this.Query(tx).
// ListEnabledFirewallPolicies 列出单页的策略
func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*HTTPFirewallPolicy, err error) {
query := this.Query(tx)
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
_, err = query.
State(HTTPFirewallPolicyStateEnabled).
Attr("userId", 0).
Attr("serverId", 0).
@@ -212,7 +222,7 @@ func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, offse
return
}
// 组合策略配置
// ComposeFirewallPolicy 组合策略配置
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64) (*firewallconfigs.HTTPFirewallPolicy, error) {
policy, err := this.FindEnabledHTTPFirewallPolicy(tx, policyId)
if err != nil {
@@ -297,7 +307,7 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
return config, nil
}
// 检查用户防火墙策略
// CheckUserFirewallPolicy 检查用户防火墙策略
func (this *HTTPFirewallPolicyDAO) CheckUserFirewallPolicy(tx *dbs.Tx, userId int64, firewallPolicyId int64) error {
ok, err := this.Query(tx).
Pk(firewallPolicyId).
@@ -315,7 +325,7 @@ func (this *HTTPFirewallPolicyDAO) CheckUserFirewallPolicy(tx *dbs.Tx, userId in
return ErrNotFound
}
// 查找包含某个IPList的所有策略
// FindEnabledFirewallPolicyIdsWithIPListId 查找包含某个IPList的所有策略
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdsWithIPListId(tx *dbs.Tx, ipListId int64) ([]int64, error) {
ones, err := this.Query(tx).
ResultPk().
@@ -333,7 +343,7 @@ func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdsWithIPListId(tx *
return result, nil
}
// 查找包含某个规则分组的策略ID
// FindEnabledFirewallPolicyIdWithRuleGroupId 查找包含某个规则分组的策略ID
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdWithRuleGroupId(tx *dbs.Tx, ruleGroupId int64) (int64, error) {
return this.Query(tx).
ResultPk().
@@ -343,7 +353,7 @@ func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdWithRuleGroupId(tx
FindInt64Col(0)
}
// 设置某个策略所属的服务ID
// UpdateFirewallPolicyServerId 设置某个策略所属的服务ID
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyServerId(tx *dbs.Tx, policyId int64, serverId int64) error {
_, err := this.Query(tx).
Pk(policyId).
@@ -352,7 +362,7 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyServerId(tx *dbs.Tx, poli
return err
}
// 通知更新
// NotifyUpdate 通知更新
func (this *HTTPFirewallPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithHTTPFirewallPolicyId(tx, policyId)
if err != nil {

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"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"
@@ -36,12 +37,12 @@ func init() {
})
}
// 初始化
// Init 初始化
func (this *HTTPLocationDAO) Init() {
_ = this.DAOObject.Init()
}
// 启用条目
// EnableHTTPLocation 启用条目
func (this *HTTPLocationDAO) EnableHTTPLocation(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -50,7 +51,7 @@ func (this *HTTPLocationDAO) EnableHTTPLocation(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableHTTPLocation 禁用条目
func (this *HTTPLocationDAO) DisableHTTPLocation(tx *dbs.Tx, locationId int64) error {
_, err := this.Query(tx).
Pk(locationId).
@@ -62,7 +63,7 @@ func (this *HTTPLocationDAO) DisableHTTPLocation(tx *dbs.Tx, locationId int64) e
return this.NotifyUpdate(tx, locationId)
}
// 查找启用中的条目
// FindEnabledHTTPLocation 查找启用中的条目
func (this *HTTPLocationDAO) FindEnabledHTTPLocation(tx *dbs.Tx, id int64) (*HTTPLocation, error) {
result, err := this.Query(tx).
Pk(id).
@@ -74,7 +75,7 @@ func (this *HTTPLocationDAO) FindEnabledHTTPLocation(tx *dbs.Tx, id int64) (*HTT
return result.(*HTTPLocation), err
}
// 根据主键查找名称
// FindHTTPLocationName 根据主键查找名称
func (this *HTTPLocationDAO) FindHTTPLocationName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -82,8 +83,8 @@ func (this *HTTPLocationDAO) FindHTTPLocationName(tx *dbs.Tx, id int64) (string,
FindStringCol("")
}
// 创建路径规则
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool) (int64, error) {
// CreateLocation 创建路径规则
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte) (int64, error) {
op := NewHTTPLocationOperator()
op.IsOn = true
op.State = HTTPLocationStateEnabled
@@ -92,6 +93,11 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str
op.Pattern = pattern
op.Description = description
op.IsBreak = isBreak
if len(condsJSON) > 0 {
op.Conds = condsJSON
}
err := this.Save(tx, op)
if err != nil {
return 0, err
@@ -99,8 +105,8 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str
return types.Int64(op.Id), nil
}
// 修改路径规则
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool) error {
// UpdateLocation 修改路径规则
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte) error {
if locationId <= 0 {
return errors.New("invalid locationId")
}
@@ -111,6 +117,11 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
op.Description = description
op.IsOn = isOn
op.IsBreak = isBreak
if len(condsJSON) > 0 {
op.Conds = condsJSON
}
err := this.Save(tx, op)
if err != nil {
return err
@@ -118,7 +129,7 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
return this.NotifyUpdate(tx, locationId)
}
// 组合配置
// ComposeLocationConfig 组合配置
func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64) (*serverconfigs.HTTPLocationConfig, error) {
location, err := this.FindEnabledHTTPLocation(tx, locationId)
if err != nil {
@@ -163,10 +174,20 @@ func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64)
}
}
// conds
if len(location.Conds) > 0 {
conds := &shared.HTTPRequestCondsConfig{}
err = json.Unmarshal([]byte(location.Conds), conds)
if err != nil {
return nil, err
}
config.Conds = conds
}
return config, nil
}
// 查找反向代理设置
// FindLocationReverseProxy 查找反向代理设置
func (this *HTTPLocationDAO) FindLocationReverseProxy(tx *dbs.Tx, locationId int64) (*serverconfigs.ReverseProxyRef, error) {
refString, err := this.Query(tx).
Pk(locationId).
@@ -186,7 +207,7 @@ func (this *HTTPLocationDAO) FindLocationReverseProxy(tx *dbs.Tx, locationId int
return nil, nil
}
// 更改反向代理设置
// UpdateLocationReverseProxy 更改反向代理设置
func (this *HTTPLocationDAO) UpdateLocationReverseProxy(tx *dbs.Tx, locationId int64, reverseProxyJSON []byte) error {
if locationId <= 0 {
return errors.New("invalid locationId")
@@ -201,7 +222,7 @@ func (this *HTTPLocationDAO) UpdateLocationReverseProxy(tx *dbs.Tx, locationId i
return this.NotifyUpdate(tx, locationId)
}
// 查找WebId
// FindLocationWebId 查找WebId
func (this *HTTPLocationDAO) FindLocationWebId(tx *dbs.Tx, locationId int64) (int64, error) {
webId, err := this.Query(tx).
Pk(locationId).
@@ -210,7 +231,7 @@ func (this *HTTPLocationDAO) FindLocationWebId(tx *dbs.Tx, locationId int64) (in
return int64(webId), err
}
// 更改Web设置
// UpdateLocationWeb 更改Web设置
func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, webId int64) error {
if locationId <= 0 {
return errors.New("invalid locationId")
@@ -225,7 +246,7 @@ func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, web
return this.NotifyUpdate(tx, locationId)
}
// 转换引用为配置
// ConvertLocationRefs 转换引用为配置
func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef) (locations []*serverconfigs.HTTPLocationConfig, err error) {
for _, ref := range refs {
config, err := this.ComposeLocationConfig(tx, ref.LocationId)
@@ -243,7 +264,7 @@ func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfi
return
}
// 根据WebId查找LocationId
// FindEnabledLocationIdWithWebId 根据WebId查找LocationId
func (this *HTTPLocationDAO) FindEnabledLocationIdWithWebId(tx *dbs.Tx, webId int64) (locationId int64, err error) {
if webId <= 0 {
return
@@ -254,7 +275,7 @@ func (this *HTTPLocationDAO) FindEnabledLocationIdWithWebId(tx *dbs.Tx, webId in
FindInt64Col(0)
}
// 通知更新
// NotifyUpdate 通知更新
func (this *HTTPLocationDAO) NotifyUpdate(tx *dbs.Tx, locationId int64) error {
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithLocationId(tx, locationId)
if err != nil {

View File

@@ -1,8 +1,10 @@
package models
import (
"encoding/json"
"errors"
"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"
@@ -35,12 +37,12 @@ func init() {
})
}
// 初始化
// Init 初始化
func (this *HTTPRewriteRuleDAO) Init() {
_ = this.DAOObject.Init()
}
// 启用条目
// EnableHTTPRewriteRule 启用条目
func (this *HTTPRewriteRuleDAO) EnableHTTPRewriteRule(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -49,7 +51,7 @@ func (this *HTTPRewriteRuleDAO) EnableHTTPRewriteRule(tx *dbs.Tx, id int64) erro
return err
}
// 禁用条目
// DisableHTTPRewriteRule 禁用条目
func (this *HTTPRewriteRuleDAO) DisableHTTPRewriteRule(tx *dbs.Tx, rewriteRuleId int64) error {
_, err := this.Query(tx).
Pk(rewriteRuleId).
@@ -61,7 +63,7 @@ func (this *HTTPRewriteRuleDAO) DisableHTTPRewriteRule(tx *dbs.Tx, rewriteRuleId
return this.NotifyUpdate(tx, rewriteRuleId)
}
// 查找启用中的条目
// FindEnabledHTTPRewriteRule 查找启用中的条目
func (this *HTTPRewriteRuleDAO) FindEnabledHTTPRewriteRule(tx *dbs.Tx, id int64) (*HTTPRewriteRule, error) {
result, err := this.Query(tx).
Pk(id).
@@ -73,7 +75,7 @@ func (this *HTTPRewriteRuleDAO) FindEnabledHTTPRewriteRule(tx *dbs.Tx, id int64)
return result.(*HTTPRewriteRule), err
}
// 构造配置
// ComposeRewriteRule 构造配置
func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64) (*serverconfigs.HTTPRewriteRule, error) {
rule, err := this.FindEnabledHTTPRewriteRule(tx, rewriteRuleId)
if err != nil {
@@ -93,11 +95,21 @@ func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int
config.ProxyHost = rule.ProxyHost
config.IsBreak = rule.IsBreak == 1
config.WithQuery = rule.WithQuery == 1
// conds
if len(rule.Conds) > 0 {
conds := &shared.HTTPRequestCondsConfig{}
err = json.Unmarshal([]byte(rule.Conds), conds)
if err != nil {
return nil, err
}
config.Conds = conds
}
return config, nil
}
// 创建规则
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool) (int64, error) {
// CreateRewriteRule 创建规则
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool, condsJSON []byte) (int64, error) {
op := NewHTTPRewriteRuleOperator()
op.State = HTTPRewriteRuleStateEnabled
op.IsOn = isOn
@@ -109,12 +121,17 @@ func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, re
op.IsBreak = isBreak
op.WithQuery = withQuery
op.ProxyHost = proxyHost
if len(condsJSON) > 0 {
op.Conds = condsJSON
}
err := this.Save(tx, op)
return types.Int64(op.Id), err
}
// 修改规则
func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int64, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool) error {
// UpdateRewriteRule 修改规则
func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int64, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool, condsJSON []byte) error {
if rewriteRuleId <= 0 {
return errors.New("invalid rewriteRuleId")
}
@@ -128,6 +145,11 @@ func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int6
op.IsBreak = isBreak
op.WithQuery = withQuery
op.ProxyHost = proxyHost
if len(condsJSON) > 0 {
op.Conds = condsJSON
}
err := this.Save(tx, op)
if err != nil {
return err
@@ -135,7 +157,7 @@ func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int6
return this.NotifyUpdate(tx, rewriteRuleId)
}
// 通知更新
// NotifyUpdate 通知更新
func (this *HTTPRewriteRuleDAO) NotifyUpdate(tx *dbs.Tx, rewriteRuleId int64) error {
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithRewriteRuleId(tx, rewriteRuleId)
if err != nil {

View File

@@ -44,7 +44,7 @@ func (this *HTTPWebDAO) Init() {
_ = this.DAOObject.Init()
}
// 启用条目
// EnableHTTPWeb 启用条目
func (this *HTTPWebDAO) EnableHTTPWeb(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -53,7 +53,7 @@ func (this *HTTPWebDAO) EnableHTTPWeb(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableHTTPWeb 禁用条目
func (this *HTTPWebDAO) DisableHTTPWeb(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -62,7 +62,7 @@ func (this *HTTPWebDAO) DisableHTTPWeb(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledHTTPWeb 查找启用中的条目
func (this *HTTPWebDAO) FindEnabledHTTPWeb(tx *dbs.Tx, id int64) (*HTTPWeb, error) {
result, err := this.Query(tx).
Pk(id).
@@ -74,7 +74,7 @@ func (this *HTTPWebDAO) FindEnabledHTTPWeb(tx *dbs.Tx, id int64) (*HTTPWeb, erro
return result.(*HTTPWeb), err
}
// 组合配置
// ComposeWebConfig 组合配置
func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64) (*serverconfigs.HTTPWebConfig, error) {
web, err := SharedHTTPWebDAO.FindEnabledHTTPWeb(tx, webId)
if err != nil {
@@ -323,10 +323,34 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64) (*serverconfig
config.HostRedirects = redirects
}
// Fastcgi
if IsNotNull(web.Fastcgi) {
ref := &serverconfigs.HTTPFastcgiRef{}
err = json.Unmarshal([]byte(web.Fastcgi), ref)
if err != nil {
return nil, err
}
config.FastcgiRef = ref
if len(ref.FastcgiIds) > 0 {
list := []*serverconfigs.HTTPFastcgiConfig{}
for _, fastcgiId := range ref.FastcgiIds {
fastcgiConfig, err := SharedHTTPFastcgiDAO.ComposeFastcgiConfig(tx, fastcgiId)
if err != nil {
return nil, err
}
if fastcgiConfig != nil {
list = append(list, fastcgiConfig)
}
}
config.FastcgiList = list
}
}
return config, nil
}
// 创建Web配置
// CreateWeb 创建Web配置
func (this *HTTPWebDAO) CreateWeb(tx *dbs.Tx, adminId int64, userId int64, rootJSON []byte) (int64, error) {
op := NewHTTPWebOperator()
op.State = HTTPWebStateEnabled
@@ -342,7 +366,7 @@ func (this *HTTPWebDAO) CreateWeb(tx *dbs.Tx, adminId int64, userId int64, rootJ
return types.Int64(op.Id), nil
}
// 修改Web配置
// UpdateWeb 修改Web配置
func (this *HTTPWebDAO) UpdateWeb(tx *dbs.Tx, webId int64, rootJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -358,7 +382,7 @@ func (this *HTTPWebDAO) UpdateWeb(tx *dbs.Tx, webId int64, rootJSON []byte) erro
return this.NotifyUpdate(tx, webId)
}
// 修改Gzip配置
// UpdateWebGzip 修改Gzip配置
func (this *HTTPWebDAO) UpdateWebGzip(tx *dbs.Tx, webId int64, gzipJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -374,7 +398,7 @@ func (this *HTTPWebDAO) UpdateWebGzip(tx *dbs.Tx, webId int64, gzipJSON []byte)
return this.NotifyUpdate(tx, webId)
}
// 修改字符编码
// UpdateWebCharset 修改字符编码
func (this *HTTPWebDAO) UpdateWebCharset(tx *dbs.Tx, webId int64, charsetJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -390,7 +414,7 @@ func (this *HTTPWebDAO) UpdateWebCharset(tx *dbs.Tx, webId int64, charsetJSON []
return this.NotifyUpdate(tx, webId)
}
// 更改请求Header策略
// UpdateWebRequestHeaderPolicy 更改请求Header策略
func (this *HTTPWebDAO) UpdateWebRequestHeaderPolicy(tx *dbs.Tx, webId int64, headerPolicyJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -406,7 +430,7 @@ func (this *HTTPWebDAO) UpdateWebRequestHeaderPolicy(tx *dbs.Tx, webId int64, he
return this.NotifyUpdate(tx, webId)
}
// 更改响应Header策略
// UpdateWebResponseHeaderPolicy 更改响应Header策略
func (this *HTTPWebDAO) UpdateWebResponseHeaderPolicy(tx *dbs.Tx, webId int64, headerPolicyJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -422,7 +446,7 @@ func (this *HTTPWebDAO) UpdateWebResponseHeaderPolicy(tx *dbs.Tx, webId int64, h
return this.NotifyUpdate(tx, webId)
}
// 更改特殊页面配置
// UpdateWebPages 更改特殊页面配置
func (this *HTTPWebDAO) UpdateWebPages(tx *dbs.Tx, webId int64, pagesJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -438,7 +462,7 @@ func (this *HTTPWebDAO) UpdateWebPages(tx *dbs.Tx, webId int64, pagesJSON []byte
return this.NotifyUpdate(tx, webId)
}
// 更改Shutdown配置
// UpdateWebShutdown 更改Shutdown配置
func (this *HTTPWebDAO) UpdateWebShutdown(tx *dbs.Tx, webId int64, shutdownJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -454,7 +478,7 @@ func (this *HTTPWebDAO) UpdateWebShutdown(tx *dbs.Tx, webId int64, shutdownJSON
return this.NotifyUpdate(tx, webId)
}
// 更改访问日志策略
// UpdateWebAccessLogConfig 更改访问日志策略
func (this *HTTPWebDAO) UpdateWebAccessLogConfig(tx *dbs.Tx, webId int64, accessLogJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -470,7 +494,7 @@ func (this *HTTPWebDAO) UpdateWebAccessLogConfig(tx *dbs.Tx, webId int64, access
return this.NotifyUpdate(tx, webId)
}
// 更改统计配置
// UpdateWebStat 更改统计配置
func (this *HTTPWebDAO) UpdateWebStat(tx *dbs.Tx, webId int64, statJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -486,7 +510,7 @@ func (this *HTTPWebDAO) UpdateWebStat(tx *dbs.Tx, webId int64, statJSON []byte)
return this.NotifyUpdate(tx, webId)
}
// 更改缓存配置
// UpdateWebCache 更改缓存配置
func (this *HTTPWebDAO) UpdateWebCache(tx *dbs.Tx, webId int64, cacheJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -502,7 +526,7 @@ func (this *HTTPWebDAO) UpdateWebCache(tx *dbs.Tx, webId int64, cacheJSON []byte
return this.NotifyUpdate(tx, webId)
}
// 更改防火墙配置
// UpdateWebFirewall 更改防火墙配置
func (this *HTTPWebDAO) UpdateWebFirewall(tx *dbs.Tx, webId int64, firewallJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -518,7 +542,7 @@ func (this *HTTPWebDAO) UpdateWebFirewall(tx *dbs.Tx, webId int64, firewallJSON
return this.NotifyUpdate(tx, webId)
}
// 更改路径规则配置
// UpdateWebLocations 更改路径规则配置
func (this *HTTPWebDAO) UpdateWebLocations(tx *dbs.Tx, webId int64, locationsJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -534,7 +558,7 @@ func (this *HTTPWebDAO) UpdateWebLocations(tx *dbs.Tx, webId int64, locationsJSO
return this.NotifyUpdate(tx, webId)
}
// 更改跳转到HTTPS设置
// UpdateWebRedirectToHTTPS 更改跳转到HTTPS设置
func (this *HTTPWebDAO) UpdateWebRedirectToHTTPS(tx *dbs.Tx, webId int64, redirectToHTTPSJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -550,7 +574,7 @@ func (this *HTTPWebDAO) UpdateWebRedirectToHTTPS(tx *dbs.Tx, webId int64, redire
return this.NotifyUpdate(tx, webId)
}
// 修改Websocket设置
// UpdateWebsocket 修改Websocket设置
func (this *HTTPWebDAO) UpdateWebsocket(tx *dbs.Tx, webId int64, websocketJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -566,7 +590,23 @@ func (this *HTTPWebDAO) UpdateWebsocket(tx *dbs.Tx, webId int64, websocketJSON [
return this.NotifyUpdate(tx, webId)
}
// 修改重写规则设置
// UpdateWebFastcgi 修改Fastcgi设置
func (this *HTTPWebDAO) UpdateWebFastcgi(tx *dbs.Tx, webId int64, fastcgiJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
}
op := NewHTTPWebOperator()
op.Id = webId
op.Fastcgi = JSONBytes(fastcgiJSON)
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, webId)
}
// UpdateWebRewriteRules 修改重写规则设置
func (this *HTTPWebDAO) UpdateWebRewriteRules(tx *dbs.Tx, webId int64, rewriteRulesJSON []byte) error {
if webId <= 0 {
return errors.New("invalid webId")
@@ -582,7 +622,7 @@ func (this *HTTPWebDAO) UpdateWebRewriteRules(tx *dbs.Tx, webId int64, rewriteRu
return this.NotifyUpdate(tx, webId)
}
// 根据缓存策略ID查找所有的WebId
// FindAllWebIdsWithCachePolicyId 根据缓存策略ID查找所有的WebId
func (this *HTTPWebDAO) FindAllWebIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId int64) ([]int64, error) {
ones, err := this.Query(tx).
State(HTTPWebStateEnabled).
@@ -626,7 +666,7 @@ func (this *HTTPWebDAO) FindAllWebIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId
return result, nil
}
// 根据防火墙策略ID查找所有的WebId
// FindAllWebIdsWithHTTPFirewallPolicyId 根据防火墙策略ID查找所有的WebId
func (this *HTTPWebDAO) FindAllWebIdsWithHTTPFirewallPolicyId(tx *dbs.Tx, firewallPolicyId int64) ([]int64, error) {
ones, err := this.Query(tx).
State(HTTPWebStateEnabled).
@@ -673,7 +713,7 @@ func (this *HTTPWebDAO) FindAllWebIdsWithHTTPFirewallPolicyId(tx *dbs.Tx, firewa
return result, nil
}
// 查找包含某个Location的Web
// FindEnabledWebIdWithLocationId 查找包含某个Location的Web
func (this *HTTPWebDAO) FindEnabledWebIdWithLocationId(tx *dbs.Tx, locationId int64) (webId int64, err error) {
return this.Query(tx).
State(HTTPWebStateEnabled).
@@ -683,7 +723,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithLocationId(tx *dbs.Tx, locationId in
FindInt64Col(0)
}
// 查找包含某个重写规则的Web
// FindEnabledWebIdWithRewriteRuleId 查找包含某个重写规则的Web
func (this *HTTPWebDAO) FindEnabledWebIdWithRewriteRuleId(tx *dbs.Tx, rewriteRuleId int64) (webId int64, err error) {
return this.Query(tx).
State(HTTPWebStateEnabled).
@@ -693,7 +733,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithRewriteRuleId(tx *dbs.Tx, rewriteRul
FindInt64Col(0)
}
// 查找包含某个页面的Web
// FindEnabledWebIdWithPageId 查找包含某个页面的Web
func (this *HTTPWebDAO) FindEnabledWebIdWithPageId(tx *dbs.Tx, pageId int64) (webId int64, err error) {
return this.Query(tx).
State(HTTPWebStateEnabled).
@@ -703,7 +743,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithPageId(tx *dbs.Tx, pageId int64) (we
FindInt64Col(0)
}
// 查找包含某个Header的Web
// FindEnabledWebIdWithHeaderPolicyId 查找包含某个Header的Web
func (this *HTTPWebDAO) FindEnabledWebIdWithHeaderPolicyId(tx *dbs.Tx, headerPolicyId int64) (webId int64, err error) {
return this.Query(tx).
State(HTTPWebStateEnabled).
@@ -713,7 +753,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithHeaderPolicyId(tx *dbs.Tx, headerPol
FindInt64Col(0)
}
// 查找包含某个Gzip配置的Web
// FindEnabledWebIdWithGzipId 查找包含某个Gzip配置的Web
func (this *HTTPWebDAO) FindEnabledWebIdWithGzipId(tx *dbs.Tx, gzipId int64) (webId int64, err error) {
return this.Query(tx).
State(HTTPWebStateEnabled).
@@ -723,7 +763,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithGzipId(tx *dbs.Tx, gzipId int64) (we
FindInt64Col(0)
}
// 查找包含某个Websocket配置的Web
// FindEnabledWebIdWithWebsocketId 查找包含某个Websocket配置的Web
func (this *HTTPWebDAO) FindEnabledWebIdWithWebsocketId(tx *dbs.Tx, websocketId int64) (webId int64, err error) {
return this.Query(tx).
State(HTTPWebStateEnabled).
@@ -733,7 +773,17 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithWebsocketId(tx *dbs.Tx, websocketId
FindInt64Col(0)
}
// 查找使用此Web的Server
// FindEnabledWebIdWithFastcgiId 查找包含某个Fastcgi配置的Web
func (this *HTTPWebDAO) FindEnabledWebIdWithFastcgiId(tx *dbs.Tx, fastcgiId int64) (webId int64, err error) {
return this.Query(tx).
State(HTTPWebStateEnabled).
ResultPk().
Where("JSON_CONTAINS(fastcgi, :jsonQuery)").
Param("jsonQuery", maps.Map{"fastcgiIds": fastcgiId}.AsJSON()).
FindInt64Col(0)
}
// FindWebServerId 查找使用此Web的Server
func (this *HTTPWebDAO) FindWebServerId(tx *dbs.Tx, webId int64) (serverId int64, err error) {
if webId <= 0 {
return 0, nil
@@ -766,7 +816,7 @@ func (this *HTTPWebDAO) FindWebServerId(tx *dbs.Tx, webId int64) (serverId int64
return this.FindWebServerId(tx, webId)
}
// 检查用户权限
// CheckUserWeb 检查用户权限
func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) error {
serverId, err := this.FindWebServerId(tx, webId)
if err != nil {
@@ -778,7 +828,7 @@ func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) erro
return SharedServerDAO.CheckUserServer(tx, userId, serverId)
}
// 设置主机跳转
// UpdateWebHostRedirects 设置主机跳转
func (this *HTTPWebDAO) UpdateWebHostRedirects(tx *dbs.Tx, webId int64, hostRedirects []*serverconfigs.HTTPHostRedirectConfig) error {
if webId <= 0 {
return errors.New("invalid ")
@@ -801,7 +851,7 @@ func (this *HTTPWebDAO) UpdateWebHostRedirects(tx *dbs.Tx, webId int64, hostRedi
return this.NotifyUpdate(tx, webId)
}
// 查找主机跳转
// FindWebHostRedirects 查找主机跳转
func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, error) {
col, err := this.Query(tx).
Pk(webId).
@@ -813,7 +863,7 @@ func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, e
return []byte(col), nil
}
// 通知更新
// NotifyUpdate 通知更新
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
serverId, err := this.FindWebServerId(tx, webId)
if err != nil {

View File

@@ -1,6 +1,6 @@
package models
// HTTP Web
// HTTPWeb HTTP Web
type HTTPWeb struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
@@ -27,6 +27,7 @@ type HTTPWeb struct {
Websocket string `field:"websocket"` // Websocket设置
RewriteRules string `field:"rewriteRules"` // 重写规则配置
HostRedirects string `field:"hostRedirects"` // 域名跳转
Fastcgi string `field:"fastcgi"` // Fastcgi配置
}
type HTTPWebOperator struct {
@@ -55,6 +56,7 @@ type HTTPWebOperator struct {
Websocket interface{} // Websocket设置
RewriteRules interface{} // 重写规则配置
HostRedirects interface{} // 域名跳转
Fastcgi interface{} // Fastcgi配置
}
func NewHTTPWebOperator() *HTTPWebOperator {

View File

@@ -0,0 +1,51 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"time"
)
type LatestItemType = string
const (
LatestItemTypeCluster LatestItemType = "cluster"
LatestItemTypeServer LatestItemType = "server"
)
type LatestItemDAO dbs.DAO
func NewLatestItemDAO() *LatestItemDAO {
return dbs.NewDAO(&LatestItemDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeLatestItems",
Model: new(LatestItem),
PkName: "id",
},
}).(*LatestItemDAO)
}
var SharedLatestItemDAO *LatestItemDAO
func init() {
dbs.OnReady(func() {
SharedLatestItemDAO = NewLatestItemDAO()
})
}
// IncreaseItemCount 增加数量
func (this *LatestItemDAO) IncreaseItemCount(tx *dbs.Tx, itemType LatestItemType, itemId int64) error {
return this.Query(tx).
InsertOrUpdateQuickly(maps.Map{
"itemType": itemType,
"itemId": itemId,
"count": 1,
"updatedAt": time.Now().Unix(),
}, maps.Map{
"count": dbs.SQL("count+1"),
"updatedAt": time.Now().Unix(),
})
}

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,22 @@
package models
// LatestItem 最近的条目统计
type LatestItem struct {
Id uint64 `field:"id"` // ID
ItemType string `field:"itemType"` // Item类型
ItemId uint64 `field:"itemId"` // itemID
Count uint64 `field:"count"` // 数量
UpdatedAt uint64 `field:"updatedAt"` // 更新时间
}
type LatestItemOperator struct {
Id interface{} // ID
ItemType interface{} // Item类型
ItemId interface{} // itemID
Count interface{} // 数量
UpdatedAt interface{} // 更新时间
}
func NewLatestItemOperator() *LatestItemOperator {
return &LatestItemOperator{}
}

View File

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

View File

@@ -37,6 +37,7 @@ const (
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
MessageTypeServerNamesAuditingSuccess MessageType = "ServerNamesAuditingSuccess" // 服务域名审核成功
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败
MessageTypeThresholdSatisfied MessageType = "ThresholdSatisfied" // 满足阈值
)
type MessageDAO dbs.DAO
@@ -112,7 +113,20 @@ func (this *MessageDAO) CreateClusterMessage(tx *dbs.Tx, clusterId int64, messag
// CreateNodeMessage 创建节点消息
func (this *MessageDAO) CreateNodeMessage(tx *dbs.Tx, clusterId int64, nodeId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
_, err := this.createMessage(tx, clusterId, nodeId, messageType, level, subject, body, paramsJSON)
// 检查N分钟内是否已经发送过
hash := this.calHash(subject, body, paramsJSON)
exists, err := this.Query(tx).
Attr("hash", hash).
Gt("createdAt", time.Now().Unix()-10*60). // 10分钟
Exist()
if err != nil {
return err
}
if exists {
return nil
}
_, err = this.createMessage(tx, clusterId, nodeId, messageType, level, subject, body, paramsJSON)
if err != nil {
return err
}
@@ -144,11 +158,6 @@ func (this *MessageDAO) CreateNodeMessage(tx *dbs.Tx, clusterId int64, nodeId in
// CreateMessage 创建普通消息
func (this *MessageDAO) CreateMessage(tx *dbs.Tx, adminId int64, userId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
h := md5.New()
h.Write([]byte(body))
h.Write(paramsJSON)
hash := fmt.Sprintf("%x", h.Sum(nil))
op := NewMessageOperator()
op.AdminId = adminId
op.UserId = userId
@@ -169,7 +178,7 @@ func (this *MessageDAO) CreateMessage(tx *dbs.Tx, adminId int64, userId int64, m
op.State = MessageStateEnabled
op.IsRead = false
op.Day = timeutil.Format("Ymd")
op.Hash = hash
op.Hash = this.calHash(subject, body, paramsJSON)
err := this.Save(tx, op)
if err != nil {
return err
@@ -278,11 +287,6 @@ func (this *MessageDAO) CheckMessageUser(tx *dbs.Tx, messageId int64, adminId in
// 创建消息
func (this *MessageDAO) createMessage(tx *dbs.Tx, clusterId int64, nodeId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) (int64, error) {
h := md5.New()
h.Write([]byte(body))
h.Write(paramsJSON)
hash := fmt.Sprintf("%x", h.Sum(nil))
// TODO 检查同样的消息最近是否发送过
// 创建新消息
@@ -309,7 +313,7 @@ func (this *MessageDAO) createMessage(tx *dbs.Tx, clusterId int64, nodeId int64,
op.State = MessageStateEnabled
op.CreatedAt = time.Now().Unix()
op.Day = timeutil.Format("Ymd")
op.Hash = hash
op.Hash = this.calHash(subject, body, paramsJSON)
err := this.Save(tx, op)
if err != nil {
@@ -317,3 +321,12 @@ func (this *MessageDAO) createMessage(tx *dbs.Tx, clusterId int64, nodeId int64,
}
return types.Int64(op.Id), nil
}
// 计算Hash
func (this *MessageDAO) calHash(subject string, body string, paramsJSON []byte) string {
h := md5.New()
h.Write([]byte(subject))
h.Write([]byte(body))
h.Write(paramsJSON)
return fmt.Sprintf("%x", h.Sum(nil))
}

View File

@@ -97,8 +97,8 @@ func (this *MessageReceiverDAO) CreateReceiver(tx *dbs.Tx, target MessageTaskTar
return this.SaveInt64(tx, op)
}
// FindAllReceivers 查询接收人
func (this *MessageReceiverDAO) FindAllReceivers(tx *dbs.Tx, target MessageTaskTarget, messageType string) (result []*MessageReceiver, err error) {
// FindAllEnabledReceivers 查询接收人
func (this *MessageReceiverDAO) FindAllEnabledReceivers(tx *dbs.Tx, target MessageTaskTarget, messageType string) (result []*MessageReceiver, err error) {
query := this.Query(tx)
if len(messageType) > 0 {
query.Attr("type", []string{"*", messageType}) // *表示所有的
@@ -113,3 +113,17 @@ func (this *MessageReceiverDAO) FindAllReceivers(tx *dbs.Tx, target MessageTaskT
FindAll()
return
}
// CountAllEnabledReceivers 计算接收人数量
func (this *MessageReceiverDAO) CountAllEnabledReceivers(tx *dbs.Tx, target MessageTaskTarget, messageType string) (int64, error) {
query := this.Query(tx)
if len(messageType) > 0 {
query.Attr("type", []string{"*", messageType}) // *表示所有的
}
return query.
Attr("clusterId", target.ClusterId).
Attr("nodeId", target.NodeId).
Attr("serverId", target.ServerId).
State(MessageReceiverStateEnabled).
Count()
}

View File

@@ -118,7 +118,7 @@ func (this *MessageTaskDAO) UpdateMessageTaskStatus(tx *dbs.Tx, taskId int64, st
// CreateMessageTasks 从集群、节点或者服务中创建任务
func (this *MessageTaskDAO) CreateMessageTasks(tx *dbs.Tx, target MessageTaskTarget, messageType MessageType, subject string, body string) error {
receivers, err := SharedMessageReceiverDAO.FindAllReceivers(tx, target, messageType)
receivers, err := SharedMessageReceiverDAO.FindAllEnabledReceivers(tx, target, messageType)
if err != nil {
return err
}

View File

@@ -2,6 +2,8 @@ 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"
@@ -111,7 +113,7 @@ func (this *MonitorNodeDAO) CreateMonitorNode(tx *dbs.Tx, name string, descripti
return 0, err
}
secret := rands.String(32)
err = NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, NodeRoleMonitor)
err = NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleMonitor)
if err != nil {
return
}
@@ -195,3 +197,13 @@ func (this *MonitorNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSO
Update()
return err
}
// CountAllLowerVersionNodes 计算所有节点中低于某个版本的节点数量
func (this *MonitorNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
return this.Query(tx).
State(MonitorNodeStateEnabled).
Where("status IS NOT NULL").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("version", utils.VersionToLong(version)).
Count()
}

View File

@@ -0,0 +1,145 @@
package nameservers
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
NSClusterStateEnabled = 1 // 已启用
NSClusterStateDisabled = 0 // 已禁用
)
type NSClusterDAO dbs.DAO
func NewNSClusterDAO() *NSClusterDAO {
return dbs.NewDAO(&NSClusterDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNSClusters",
Model: new(NSCluster),
PkName: "id",
},
}).(*NSClusterDAO)
}
var SharedNSClusterDAO *NSClusterDAO
func init() {
dbs.OnReady(func() {
SharedNSClusterDAO = NewNSClusterDAO()
})
}
// EnableNSCluster 启用条目
func (this *NSClusterDAO) EnableNSCluster(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSClusterStateEnabled).
Update()
return err
}
// DisableNSCluster 禁用条目
func (this *NSClusterDAO) DisableNSCluster(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSClusterStateDisabled).
Update()
return err
}
// FindEnabledNSCluster 查找启用中的条目
func (this *NSClusterDAO) FindEnabledNSCluster(tx *dbs.Tx, id int64) (*NSCluster, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSClusterStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NSCluster), err
}
// FindEnabledNSClusterName 查找启用中的条目名称
func (this *NSClusterDAO) FindEnabledNSClusterName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
State(NSClusterStateEnabled).
Result("name").
FindStringCol("")
}
// CreateCluster 创建集群
func (this *NSClusterDAO) CreateCluster(tx *dbs.Tx, name string, accessLogRefJSON []byte) (int64, error) {
op := NewNSClusterOperator()
op.Name = name
if len(accessLogRefJSON) > 0 {
op.AccessLog = accessLogRefJSON
}
op.IsOn = true
op.State = NSClusterStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateCluster 修改集群
func (this *NSClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, isOn bool) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
}
op := NewNSClusterOperator()
op.Id = clusterId
op.Name = name
op.IsOn = isOn
return this.Save(tx, op)
}
// CountAllEnabledClusters 计算可用集群数量
func (this *NSClusterDAO) CountAllEnabledClusters(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(NSClusterStateEnabled).
Count()
}
// ListEnabledClusters 列出单页集群
func (this *NSClusterDAO) ListEnabledClusters(tx *dbs.Tx, offset int64, size int64) (result []*NSCluster, err error) {
_, err = this.Query(tx).
State(NSClusterStateEnabled).
Offset(offset).
Limit(size).
DescPk().
Slice(&result).
FindAll()
return
}
// FindAllEnabledClusters 列出所有集群
func (this *NSClusterDAO) FindAllEnabledClusters(tx *dbs.Tx) (result []*NSCluster, err error) {
_, err = this.Query(tx).
State(NSClusterStateEnabled).
DescPk().
Slice(&result).
FindAll()
return
}
// UpdateClusterAccessLog 设置访问日志
func (this *NSClusterDAO) UpdateClusterAccessLog(tx *dbs.Tx, clusterId int64, accessLogJSON []byte) error {
return this.Query(tx).
Pk(clusterId).
Set("accessLog", accessLogJSON).
UpdateQuickly()
}
// FindClusterAccessLog 读取访问日志配置
func (this *NSClusterDAO) FindClusterAccessLog(tx *dbs.Tx, clusterId int64) ([]byte, error) {
accessLog, err := this.Query(tx).
Pk(clusterId).
Result("accessLog").
FindStringCol("")
return []byte(accessLog), err
}

View File

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

View File

@@ -0,0 +1,24 @@
package nameservers
// NSCluster 域名服务器集群
type NSCluster struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
Name string `field:"name"` // 集群名
InstallDir string `field:"installDir"` // 安装目录
State uint8 `field:"state"` // 状态
AccessLog string `field:"accessLog"` // 访问日志配置
}
type NSClusterOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
Name interface{} // 集群名
InstallDir interface{} // 安装目录
State interface{} // 状态
AccessLog interface{} // 访问日志配置
}
func NewNSClusterOperator() *NSClusterOperator {
return &NSClusterOperator{}
}

View File

@@ -0,0 +1 @@
package nameservers

View File

@@ -0,0 +1,196 @@
package nameservers
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
NSDomainStateEnabled = 1 // 已启用
NSDomainStateDisabled = 0 // 已禁用
)
type NSDomainDAO dbs.DAO
func NewNSDomainDAO() *NSDomainDAO {
return dbs.NewDAO(&NSDomainDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNSDomains",
Model: new(NSDomain),
PkName: "id",
},
}).(*NSDomainDAO)
}
var SharedNSDomainDAO *NSDomainDAO
func init() {
dbs.OnReady(func() {
SharedNSDomainDAO = NewNSDomainDAO()
})
}
// EnableNSDomain 启用条目
func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSDomainStateEnabled).
Update()
return err
}
// DisableNSDomain 禁用条目
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, domainId int64) error {
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(domainId).
Set("state", NSDomainStateDisabled).
Set("version", version).
Update()
return err
}
// FindEnabledNSDomain 查找启用中的条目
func (this *NSDomainDAO) FindEnabledNSDomain(tx *dbs.Tx, id int64) (*NSDomain, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSDomainStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NSDomain), err
}
// FindNSDomainName 根据主键查找名称
func (this *NSDomainDAO) FindNSDomainName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreateDomain 创建域名
func (this *NSDomainDAO) CreateDomain(tx *dbs.Tx, clusterId int64, userId int64, name string) (int64, error) {
version, err := this.IncreaseVersion(tx)
if err != nil {
return 0, err
}
op := NewNSDomainOperator()
op.ClusterId = clusterId
op.UserId = userId
op.Name = name
op.Version = version
op.IsOn = true
op.State = NSDomainStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateDomain 修改域名
func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int64, userId int64, isOn bool) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
op := NewNSDomainOperator()
op.Id = domainId
op.ClusterId = clusterId
op.UserId = userId
op.IsOn = isOn
op.Version = version
return this.Save(tx, op)
}
// CountAllEnabledDomains 计算域名数量
func (this *NSDomainDAO) CountAllEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string) (int64, error) {
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
} else {
query.Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)")
}
if userId > 0 {
query.Attr("userId", userId)
} else {
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.
State(NSDomainStateEnabled).
Count()
}
// ListEnabledDomains 列出单页域名
func (this *NSDomainDAO) ListEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string, offset int64, size int64) (result []*NSDomain, err error) {
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
} else {
query.Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)")
}
if userId > 0 {
query.Attr("userId", userId)
} else {
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
_, err = query.
State(NSDomainStateEnabled).
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// IncreaseVersion 增加版本
func (this *NSDomainDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
return models.SharedSysLockerDAO.Increase(tx, "NS_DOMAIN_VERSION", 1)
}
// ListDomainsAfterVersion 列出某个版本后的域名
func (this *NSDomainDAO) ListDomainsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSDomain, err error) {
if size <= 0 {
size = 10000
}
_, err = this.Query(tx).
Gte("version", version).
Limit(size).
Asc("version").
Slice(&result).
FindAll()
return
}
// FindDomainIdWithName 根据名称查找域名
func (this *NSDomainDAO) FindDomainIdWithName(tx *dbs.Tx, clusterId int64, name string) (int64, error) {
return this.Query(tx).
Attr("clusterId", clusterId).
Attr("name", name).
State(NSDomainStateEnabled).
ResultPk().
FindInt64Col(0)
}

View File

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

View File

@@ -0,0 +1,28 @@
package nameservers
// NSDomain DNS域名
type NSDomain struct {
Id uint32 `field:"id"` // ID
ClusterId uint32 `field:"clusterId"` // 集群ID
UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用
Name string `field:"name"` // 域名
CreatedAt uint64 `field:"createdAt"` // 创建时间
Version uint64 `field:"version"` // 版本
State uint8 `field:"state"` // 状态
}
type NSDomainOperator struct {
Id interface{} // ID
ClusterId interface{} // 集群ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
Name interface{} // 域名
CreatedAt interface{} // 创建时间
Version interface{} // 版本
State interface{} // 状态
}
func NewNSDomainOperator() *NSDomainOperator {
return &NSDomainOperator{}
}

View File

@@ -0,0 +1 @@
package nameservers

View File

@@ -0,0 +1,392 @@
package nameservers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"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/rands"
"github.com/iwind/TeaGo/types"
)
const (
NSNodeStateEnabled = 1 // 已启用
NSNodeStateDisabled = 0 // 已禁用
)
type NSNodeDAO dbs.DAO
func NewNSNodeDAO() *NSNodeDAO {
return dbs.NewDAO(&NSNodeDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNSNodes",
Model: new(NSNode),
PkName: "id",
},
}).(*NSNodeDAO)
}
var SharedNSNodeDAO *NSNodeDAO
func init() {
dbs.OnReady(func() {
SharedNSNodeDAO = NewNSNodeDAO()
})
}
// EnableNSNode 启用条目
func (this *NSNodeDAO) EnableNSNode(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSNodeStateEnabled).
Update()
return err
}
// DisableNSNode 禁用条目
func (this *NSNodeDAO) DisableNSNode(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSNodeStateDisabled).
Update()
if err != nil {
return err
}
return this.NotifyUpdate(tx, id)
}
// FindEnabledNSNode 查找启用中的条目
func (this *NSNodeDAO) FindEnabledNSNode(tx *dbs.Tx, id int64) (*NSNode, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSNodeStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NSNode), err
}
// FindAllEnabledNodesWithClusterId 查找一个集群下的所有节点
func (this *NSNodeDAO) FindAllEnabledNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NSNode, err error) {
_, err = this.Query(tx).
Attr("clusterId", clusterId).
State(NSNodeStateEnabled).
DescPk().
Slice(&result).
FindAll()
return
}
// CountAllEnabledNodes 所有集群的可用的节点数量
func (this *NSNodeDAO) CountAllEnabledNodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(NSNodeStateEnabled).
Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)").
Count()
}
// CountAllEnabledNodesMatch 计算满足条件的节点数量
func (this *NSNodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string) (int64, error) {
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
// 安装状态
switch installState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Attr("isInstalled", 1)
case configutils.BoolStateNo:
query.Attr("isInstalled", 0)
}
// 在线状态
switch activeState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Where("JSON_EXTRACT(status, '$.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)")
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.
State(NSNodeStateEnabled).
Count()
}
// ListAllEnabledNodesMatch 列出单页匹配的节点
func (this *NSNodeDAO) ListAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string, offset int64, size int64) (result []*NSNode, err error) {
query := this.Query(tx)
// 安装状态
switch installState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Attr("isInstalled", 1)
case configutils.BoolStateNo:
query.Attr("isInstalled", 0)
}
// 在线状态
switch activeState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Where("JSON_EXTRACT(status, '$.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)")
}
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
_, err = query.
State(NSNodeStateEnabled).
Offset(offset).
Limit(size).
Slice(&result).
DescPk().
FindAll()
return
}
// CountAllLowerVersionNodesWithClusterId 计算单个集群中所有低于某个版本的节点数量
func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId int64, os string, arch string, version string) (int64, error) {
return this.Query(tx).
State(NSNodeStateEnabled).
Attr("clusterId", clusterId).
Where("status IS NOT NULL").
Where("JSON_EXTRACT(status, '$.os')=:os").
Where("JSON_EXTRACT(status, '$.arch')=:arch").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("os", os).
Param("arch", arch).
Param("version", utils.VersionToLong(version)).
Count()
}
// CreateNode 创建节点
func (this *NSNodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterId int64) (nodeId int64, err error) {
uniqueId, err := this.GenUniqueId(tx)
if err != nil {
return 0, err
}
secret := rands.String(32)
// 保存API Token
err = models.SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleDNS)
if err != nil {
return
}
op := NewNSNodeOperator()
op.AdminId = adminId
op.Name = name
op.UniqueId = uniqueId
op.Secret = secret
op.ClusterId = clusterId
op.IsOn = 1
op.State = NSNodeStateEnabled
err = this.Save(tx, op)
if err != nil {
return 0, err
}
// 通知节点更新
nodeId = types.Int64(op.Id)
err = this.NotifyUpdate(tx, nodeId)
if err != nil {
return 0, err
}
// 通知DNS更新
err = this.NotifyDNSUpdate(tx, nodeId)
if err != nil {
return 0, err
}
return nodeId, nil
}
// UpdateNode 修改节点
func (this *NSNodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, isOn bool) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
op := NewNSNodeOperator()
op.Id = nodeId
op.Name = name
op.ClusterId = clusterId
op.IsOn = isOn
err := this.Save(tx, op)
if err != nil {
return err
}
err = this.NotifyUpdate(tx, nodeId)
if err != nil {
return err
}
return this.NotifyDNSUpdate(tx, nodeId)
}
// FindEnabledNodeIdWithUniqueId 根据唯一ID获取节点ID
func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
return this.Query(tx).
Attr("uniqueId", uniqueId).
Attr("state", NSNodeStateEnabled).
ResultPk().
FindInt64Col(0)
}
// FindNodeInstallStatus 查询节点的安装状态
func (this *NSNodeDAO) FindNodeInstallStatus(tx *dbs.Tx, nodeId int64) (*models.NodeInstallStatus, error) {
node, err := this.Query(tx).
Pk(nodeId).
Result("installStatus", "isInstalled").
Find()
if err != nil {
return nil, err
}
if node == nil {
return nil, errors.New("not found")
}
installStatus := node.(*NSNode).InstallStatus
isInstalled := node.(*NSNode).IsInstalled == 1
if len(installStatus) == 0 {
return models.NewNodeInstallStatus(), nil
}
status := &models.NodeInstallStatus{}
err = json.Unmarshal([]byte(installStatus), status)
if err != nil {
return nil, err
}
if isInstalled {
status.IsFinished = true
status.IsOk = true
}
return status, nil
}
// GenUniqueId 生成唯一ID
func (this *NSNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
for {
uniqueId := rands.HexString(32)
ok, err := this.Query(tx).
Attr("uniqueId", uniqueId).
Exist()
if err != nil {
return "", err
}
if ok {
continue
}
return uniqueId, nil
}
}
// UpdateNodeIsInstalled 设置节点安装状态
func (this *NSNodeDAO) UpdateNodeIsInstalled(tx *dbs.Tx, nodeId int64, isInstalled bool) error {
_, err := this.Query(tx).
Pk(nodeId).
Set("isInstalled", isInstalled).
Set("installStatus", "null"). // 重置安装状态
Update()
return err
}
// UpdateNodeStatus 更改节点状态
func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
if statusJSON == nil {
return nil
}
_, err := this.Query(tx).
Pk(nodeId).
Set("status", string(statusJSON)).
Update()
return err
}
// CountAllLowerVersionNodes 计算所有节点中低于某个版本的节点数量
func (this *NSNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
return this.Query(tx).
State(NSNodeStateEnabled).
Where("status IS NOT NULL").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("version", utils.VersionToLong(version)).
Count()
}
// ComposeNodeConfig 组合节点配置
func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.NSNodeConfig, error) {
if nodeId <= 0 {
return nil, nil
}
node, err := this.FindEnabledNSNode(tx, nodeId)
if err != nil {
return nil, err
}
if node == nil {
return nil, nil
}
cluster, err := SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(node.ClusterId))
if err != nil {
return nil, err
}
if cluster == nil {
return nil, nil
}
config := &dnsconfigs.NSNodeConfig{
Id: int64(node.Id),
ClusterId: int64(node.ClusterId),
}
if len(cluster.AccessLog) > 0 {
ref := &dnsconfigs.AccessLogRef{}
err = json.Unmarshal([]byte(cluster.AccessLog), ref)
if err != nil {
return nil, err
}
config.AccessLogRef = ref
}
return config, nil
}
// NotifyUpdate 通知更新
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
// TODO 先什么都不做
return nil
}
// NotifyDNSUpdate 通知DNS更新
func (this *NSNodeDAO) NotifyDNSUpdate(tx *dbs.Tx, nodeId int64) error {
// TODO 先什么都不做
return nil
}

View File

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

View File

@@ -0,0 +1,38 @@
package nameservers
// NSNode 域名服务器节点
type NSNode struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
ClusterId uint32 `field:"clusterId"` // 集群ID
Name string `field:"name"` // 节点名称
IsOn uint8 `field:"isOn"` // 是否启用
Status string `field:"status"` // 运行状态
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
IsUp uint8 `field:"isUp"` // 是否运行
IsInstalled uint8 `field:"isInstalled"` // 是否已安装
InstallStatus string `field:"installStatus"` // 安装状态
InstallDir string `field:"installDir"` // 安装目录
State uint8 `field:"state"` // 状态
}
type NSNodeOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
ClusterId interface{} // 集群ID
Name interface{} // 节点名称
IsOn interface{} // 是否启用
Status interface{} // 运行状态
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
IsUp interface{} // 是否运行
IsInstalled interface{} // 是否已安装
InstallStatus interface{} // 安装状态
InstallDir interface{} // 安装目录
State interface{} // 状态
}
func NewNSNodeOperator() *NSNodeOperator {
return &NSNodeOperator{}
}

View File

@@ -0,0 +1,42 @@
package nameservers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"time"
)
// DecodeInstallStatus 安装状态
func (this *NSNode) DecodeInstallStatus() (*models.NodeInstallStatus, error) {
if len(this.InstallStatus) == 0 || this.InstallStatus == "null" {
return models.NewNodeInstallStatus(), nil
}
status := &models.NodeInstallStatus{}
err := json.Unmarshal([]byte(this.InstallStatus), status)
if err != nil {
return models.NewNodeInstallStatus(), err
}
// 如果N秒钟没有更新状态则认为不在运行
if status.IsRunning && status.UpdatedAt < time.Now().Unix()-10 {
status.IsRunning = false
status.IsFinished = true
status.Error = "timeout"
}
return status, nil
}
// DecodeStatus 节点状态
func (this *NSNode) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
if len(this.Status) == 0 || this.Status == "null" {
return nil, nil
}
status := &nodeconfigs.NodeStatus{}
err := json.Unmarshal([]byte(this.Status), status)
if err != nil {
return nil, err
}
return status, nil
}

View File

@@ -0,0 +1,220 @@
package nameservers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"strconv"
)
const (
NSRecordStateEnabled = 1 // 已启用
NSRecordStateDisabled = 0 // 已禁用
)
type NSRecordDAO dbs.DAO
func NewNSRecordDAO() *NSRecordDAO {
return dbs.NewDAO(&NSRecordDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNSRecords",
Model: new(NSRecord),
PkName: "id",
},
}).(*NSRecordDAO)
}
var SharedNSRecordDAO *NSRecordDAO
func init() {
dbs.OnReady(func() {
SharedNSRecordDAO = NewNSRecordDAO()
})
}
// EnableNSRecord 启用条目
func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, id uint64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSRecordStateEnabled).
Update()
return err
}
// DisableNSRecord 禁用条目
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id int64) error {
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(id).
Set("state", NSRecordStateDisabled).
Set("version", version).
Update()
return err
}
// FindEnabledNSRecord 查找启用中的条目
func (this *NSRecordDAO) FindEnabledNSRecord(tx *dbs.Tx, id int64) (*NSRecord, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSRecordStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NSRecord), err
}
// FindNSRecordName 根据主键查找名称
func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreateRecord 创建记录
func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64) (int64, error) {
version, err := this.IncreaseVersion(tx)
if err != nil {
return 0, err
}
op := NewNSRecordOperator()
op.DomainId = domainId
op.Description = description
op.Name = name
op.Type = dnsType
op.Value = value
op.Ttl = ttl
if len(routeIds) == 0 {
op.RouteIds = "[]"
} else {
routeIds, err := json.Marshal(routeIds)
if err != nil {
return 0, err
}
op.RouteIds = routeIds
}
op.IsOn = true
op.State = NSRecordStateEnabled
op.Version = version
return this.SaveInt64(tx, op)
}
func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64) error {
if recordId <= 0 {
return errors.New("invalid recordId")
}
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
op := NewNSRecordOperator()
op.Id = recordId
op.Description = description
op.Name = name
op.Type = dnsType
op.Value = value
op.Ttl = ttl
if len(routeIds) == 0 {
op.RouteIds = "[]"
} else {
routeIds, err := json.Marshal(routeIds)
if err != nil {
return err
}
op.RouteIds = routeIds
}
op.Version = version
return this.Save(tx, op)
}
func (this *NSRecordDAO) CountAllEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64) (int64, error) {
query := this.Query(tx).
Attr("domainId", domainId).
State(NSRecordStateEnabled)
if len(dnsType) > 0 {
query.Attr("type", dnsType)
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if routeId > 0 {
query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
}
return query.Count()
}
func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64, offset int64, size int64) (result []*NSRecord, err error) {
query := this.Query(tx).
Attr("domainId", domainId).
State(NSRecordStateEnabled)
if len(dnsType) > 0 {
query.Attr("type", dnsType)
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if routeId > 0 {
query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// IncreaseVersion 增加版本
func (this *NSRecordDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
return models.SharedSysLockerDAO.Increase(tx, "NS_RECORD_VERSION", 1)
}
// ListRecordsAfterVersion 列出某个版本后的记录
func (this *NSRecordDAO) ListRecordsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRecord, err error) {
if size <= 0 {
size = 10000
}
_, err = this.Query(tx).
Gte("version", version).
Limit(size).
Asc("version").
Slice(&result).
FindAll()
return
}
// FindEnabledRecordWithName 查询单条记录
func (this *NSRecordDAO) FindEnabledRecordWithName(tx *dbs.Tx, domainId int64, recordName string, recordType dnsconfigs.RecordType) (*NSRecord, error) {
record, err := this.Query(tx).
State(NSRecordStateEnabled).
Attr("domainId", domainId).
Attr("name", recordName).
Attr("type", recordType).
Find()
if record == nil {
return nil, err
}
return record.(*NSRecord), nil
}

View File

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

View File

@@ -0,0 +1,38 @@
package nameservers
// NSRecord DNS记录
type NSRecord struct {
Id uint64 `field:"id"` // ID
DomainId uint32 `field:"domainId"` // 域名ID
IsOn uint8 `field:"isOn"` // 是否启用
Description string `field:"description"` // 备注
Name string `field:"name"` // 记录名
Type string `field:"type"` // 类型
Value string `field:"value"` // 值
Ttl uint32 `field:"ttl"` // TTL
Weight uint32 `field:"weight"` // 权重
RouteIds string `field:"routeIds"` // 线路
CreatedAt uint64 `field:"createdAt"` // 创建时间
Version uint64 `field:"version"` //
State uint8 `field:"state"` // 状态
}
type NSRecordOperator struct {
Id interface{} // ID
DomainId interface{} // 域名ID
IsOn interface{} // 是否启用
Description interface{} // 备注
Name interface{} // 记录名
Type interface{} // 类型
Value interface{} // 值
Ttl interface{} // TTL
Weight interface{} // 权重
RouteIds interface{} // 线路
CreatedAt interface{} // 创建时间
Version interface{} //
State interface{} // 状态
}
func NewNSRecordOperator() *NSRecordOperator {
return &NSRecordOperator{}
}

View File

@@ -0,0 +1,11 @@
package nameservers
import "encoding/json"
func (this *NSRecord) DecodeRouteIds() []int64 {
routeIds := []int64{}
if len(this.RouteIds) > 0 {
_ = json.Unmarshal([]byte(this.RouteIds), &routeIds)
}
return routeIds
}

View File

@@ -0,0 +1,192 @@
package nameservers
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
NSRouteStateEnabled = 1 // 已启用
NSRouteStateDisabled = 0 // 已禁用
)
type NSRouteDAO dbs.DAO
func NewNSRouteDAO() *NSRouteDAO {
return dbs.NewDAO(&NSRouteDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNSRoutes",
Model: new(NSRoute),
PkName: "id",
},
}).(*NSRouteDAO)
}
var SharedNSRouteDAO *NSRouteDAO
func init() {
dbs.OnReady(func() {
SharedNSRouteDAO = NewNSRouteDAO()
})
}
// EnableNSRoute 启用条目
func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSRouteStateEnabled).
Update()
return err
}
// DisableNSRoute 禁用条目
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, routeId int64) error {
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(routeId).
Set("state", NSRouteStateDisabled).
Set("version", version).
Update()
return err
}
// FindEnabledNSRoute 查找启用中的条目
func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id int64) (*NSRoute, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSRouteStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NSRoute), err
}
// FindNSRouteName 根据主键查找名称
func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreateRoute 创建线路
func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64, userId int64, name string, rangesJSON []byte) (int64, error) {
version, err := this.IncreaseVersion(tx)
if err != nil {
return 0, err
}
op := NewNSRouteOperator()
op.ClusterId = clusterId
op.DomainId = domainId
op.UserId = userId
op.Name = name
if len(rangesJSON) > 0 {
op.Ranges = rangesJSON
} else {
op.Ranges = "[]"
}
op.IsOn = true
op.State = NSRouteStateEnabled
op.Version = version
return this.SaveInt64(tx, op)
}
// UpdateRoute 修改线路
func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rangesJSON []byte) error {
if routeId <= 0 {
return errors.New("invalid routeId")
}
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
op := NewNSRouteOperator()
op.Id = routeId
op.Name = name
if len(rangesJSON) > 0 {
op.Ranges = rangesJSON
} else {
op.Ranges = "[]"
}
op.Version = version
return this.Save(tx, op)
}
// UpdateRouteOrders 修改线路排序
func (this *NSRouteDAO) UpdateRouteOrders(tx *dbs.Tx, routeIds []int64) error {
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
order := len(routeIds)
for _, routeId := range routeIds {
_, err = this.Query(tx).
Pk(routeId).
Set("order", order).
Set("version", version).
Update()
if err != nil {
return err
}
order--
}
return nil
}
// FindAllEnabledRoutes 列出所有线路
func (this *NSRouteDAO) FindAllEnabledRoutes(tx *dbs.Tx, clusterId int64, domainId int64, userId int64) (result []*NSRoute, err error) {
query := this.Query(tx).
State(NSRouteStateEnabled).
Slice(&result).
Desc("order").
DescPk()
if clusterId > 0 {
query.Attr("clusterId", clusterId)
} else {
// 不查询所有集群的线路
query.Attr("clusterId", 0)
}
if domainId > 0 {
query.Attr("domainId", domainId)
}
if userId > 0 {
query.Attr("userId", userId)
}
_, err = query.FindAll()
return
}
// IncreaseVersion 增加版本
func (this *NSRouteDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
return models.SharedSysLockerDAO.Increase(tx, "NS_ROUTE_VERSION", 1)
}
// ListRoutesAfterVersion 列出某个版本后的域名
func (this *NSRouteDAO) ListRoutesAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRoute, err error) {
if size <= 0 {
size = 10000
}
_, err = this.Query(tx).
Gte("version", version).
Limit(size).
Asc("version").
Slice(&result).
FindAll()
return
}

View File

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

View File

@@ -0,0 +1,32 @@
package nameservers
// NSRoute DNS线路
type NSRoute struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
ClusterId uint32 `field:"clusterId"` // 集群ID
DomainId uint32 `field:"domainId"` // 域名ID
UserId uint32 `field:"userId"` // 用户ID
Name string `field:"name"` // 名称
Ranges string `field:"ranges"` // 范围
Order uint32 `field:"order"` // 排序
Version uint64 `field:"version"` // 版本号
State uint8 `field:"state"` // 状态
}
type NSRouteOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
ClusterId interface{} // 集群ID
DomainId interface{} // 域名ID
UserId interface{} // 用户ID
Name interface{} // 名称
Ranges interface{} // 范围
Order interface{} // 排序
Version interface{} // 版本号
State interface{} // 状态
}
func NewNSRouteOperator() *NSRouteOperator {
return &NSRouteOperator{}
}

View File

@@ -0,0 +1 @@
package nameservers

View File

@@ -44,7 +44,7 @@ func init() {
})
}
// 启用条目
// EnableNodeCluster 启用条目
func (this *NodeClusterDAO) EnableNodeCluster(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -53,7 +53,7 @@ func (this *NodeClusterDAO) EnableNodeCluster(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableNodeCluster 禁用条目
func (this *NodeClusterDAO) DisableNodeCluster(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -62,7 +62,7 @@ func (this *NodeClusterDAO) DisableNodeCluster(tx *dbs.Tx, id int64) error {
return err
}
// 查找集群
// FindEnabledNodeCluster 查找集群
func (this *NodeClusterDAO) FindEnabledNodeCluster(tx *dbs.Tx, id int64) (*NodeCluster, error) {
result, err := this.Query(tx).
Pk(id).
@@ -74,7 +74,7 @@ func (this *NodeClusterDAO) FindEnabledNodeCluster(tx *dbs.Tx, id int64) (*NodeC
return result.(*NodeCluster), err
}
// 根据UniqueId获取ID
// FindEnabledClusterIdWithUniqueId 根据UniqueId获取ID
// TODO 增加缓存
func (this *NodeClusterDAO) FindEnabledClusterIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
return this.Query(tx).
@@ -84,7 +84,7 @@ func (this *NodeClusterDAO) FindEnabledClusterIdWithUniqueId(tx *dbs.Tx, uniqueI
FindInt64Col(0)
}
// 根据主键查找名称
// FindNodeClusterName 根据主键查找名称
func (this *NodeClusterDAO) FindNodeClusterName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -92,7 +92,7 @@ func (this *NodeClusterDAO) FindNodeClusterName(tx *dbs.Tx, id int64) (string, e
FindStringCol("")
}
// 查找所有可用的集群
// FindAllEnableClusters 查找所有可用的集群
func (this *NodeClusterDAO) FindAllEnableClusters(tx *dbs.Tx) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
@@ -103,7 +103,7 @@ func (this *NodeClusterDAO) FindAllEnableClusters(tx *dbs.Tx) (result []*NodeClu
return
}
// 查找所有可用的集群Ids
// FindAllEnableClusterIds 查找所有可用的集群Ids
func (this *NodeClusterDAO) FindAllEnableClusterIds(tx *dbs.Tx) (result []int64, err error) {
ones, err := this.Query(tx).
State(NodeClusterStateEnabled).
@@ -118,7 +118,7 @@ func (this *NodeClusterDAO) FindAllEnableClusterIds(tx *dbs.Tx) (result []int64,
return
}
// 创建集群
// CreateCluster 创建集群
func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string, grantId int64, installDir string, dnsDomainId int64, dnsName string, cachePolicyId int64, httpFirewallPolicyId int64, systemServices map[string]maps.Map) (clusterId int64, err error) {
uniqueId, err := this.GenUniqueId(tx)
if err != nil {
@@ -126,7 +126,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
}
secret := rands.String(32)
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, NodeRoleCluster)
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleCluster)
if err != nil {
return 0, err
}
@@ -176,7 +176,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
return types.Int64(op.Id), nil
}
// 修改集群
// UpdateCluster 修改集群
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
@@ -190,7 +190,7 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
return err
}
// 计算所有集群数量
// CountAllEnabledClusters 计算所有集群数量
func (this *NodeClusterDAO) CountAllEnabledClusters(tx *dbs.Tx, keyword string) (int64, error) {
query := this.Query(tx).
State(NodeClusterStateEnabled)
@@ -201,7 +201,7 @@ func (this *NodeClusterDAO) CountAllEnabledClusters(tx *dbs.Tx, keyword string)
return query.Count()
}
// 列出单页集群
// ListEnabledClusters 列出单页集群
func (this *NodeClusterDAO) ListEnabledClusters(tx *dbs.Tx, keyword string, offset, size int64) (result []*NodeCluster, err error) {
query := this.Query(tx).
State(NodeClusterStateEnabled)
@@ -218,7 +218,7 @@ func (this *NodeClusterDAO) ListEnabledClusters(tx *dbs.Tx, keyword string, offs
return
}
// 查找所有API节点地址
// FindAllAPINodeAddrsWithCluster 查找所有API节点地址
func (this *NodeClusterDAO) FindAllAPINodeAddrsWithCluster(tx *dbs.Tx, clusterId int64) (result []string, err error) {
one, err := this.Query(tx).
Pk(clusterId).
@@ -274,7 +274,7 @@ func (this *NodeClusterDAO) FindAllAPINodeAddrsWithCluster(tx *dbs.Tx, clusterId
return result, nil
}
// 查找健康检查设置
// FindClusterHealthCheckConfig 查找健康检查设置
func (this *NodeClusterDAO) FindClusterHealthCheckConfig(tx *dbs.Tx, clusterId int64) (*serverconfigs.HealthCheckConfig, error) {
col, err := this.Query(tx).
Pk(clusterId).
@@ -295,7 +295,7 @@ func (this *NodeClusterDAO) FindClusterHealthCheckConfig(tx *dbs.Tx, clusterId i
return config, nil
}
// 修改健康检查设置
// UpdateClusterHealthCheck 修改健康检查设置
func (this *NodeClusterDAO) UpdateClusterHealthCheck(tx *dbs.Tx, clusterId int64, healthCheckJSON []byte) error {
if clusterId <= 0 {
return errors.New("invalid clusterId '" + strconv.FormatInt(clusterId, 10) + "'")
@@ -310,7 +310,7 @@ func (this *NodeClusterDAO) UpdateClusterHealthCheck(tx *dbs.Tx, clusterId int64
return this.NotifyUpdate(tx, clusterId)
}
// 计算使用某个认证的集群数量
// CountAllEnabledClustersWithGrantId 计算使用某个认证的集群数量
func (this *NodeClusterDAO) CountAllEnabledClustersWithGrantId(tx *dbs.Tx, grantId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterStateEnabled).
@@ -318,7 +318,7 @@ func (this *NodeClusterDAO) CountAllEnabledClustersWithGrantId(tx *dbs.Tx, grant
Count()
}
// 获取使用某个认证的所有集群
// FindAllEnabledClustersWithGrantId 获取使用某个认证的所有集群
func (this *NodeClusterDAO) FindAllEnabledClustersWithGrantId(tx *dbs.Tx, grantId int64) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
@@ -329,7 +329,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersWithGrantId(tx *dbs.Tx, grantI
return
}
// 计算使用某个DNS服务商的集群数量
// CountAllEnabledClustersWithDNSProviderId 计算使用某个DNS服务商的集群数量
func (this *NodeClusterDAO) CountAllEnabledClustersWithDNSProviderId(tx *dbs.Tx, dnsProviderId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterStateEnabled).
@@ -338,7 +338,7 @@ func (this *NodeClusterDAO) CountAllEnabledClustersWithDNSProviderId(tx *dbs.Tx,
Count()
}
// 获取所有使用某个DNS服务商的集群
// FindAllEnabledClustersWithDNSProviderId 获取所有使用某个DNS服务商的集群
func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSProviderId(tx *dbs.Tx, dnsProviderId int64) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
@@ -350,7 +350,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSProviderId(tx *dbs.Tx,
return
}
// 计算使用某个DNS域名的集群数量
// CountAllEnabledClustersWithDNSDomainId 计算使用某个DNS域名的集群数量
func (this *NodeClusterDAO) CountAllEnabledClustersWithDNSDomainId(tx *dbs.Tx, dnsDomainId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterStateEnabled).
@@ -358,7 +358,7 @@ func (this *NodeClusterDAO) CountAllEnabledClustersWithDNSDomainId(tx *dbs.Tx, d
Count()
}
// 查询使用某个DNS域名的集群ID列表
// FindAllEnabledClusterIdsWithDNSDomainId 查询使用某个DNS域名的集群ID列表
func (this *NodeClusterDAO) FindAllEnabledClusterIdsWithDNSDomainId(tx *dbs.Tx, dnsDomainId int64) ([]int64, error) {
ones, err := this.Query(tx).
State(NodeClusterStateEnabled).
@@ -375,7 +375,7 @@ func (this *NodeClusterDAO) FindAllEnabledClusterIdsWithDNSDomainId(tx *dbs.Tx,
return result, nil
}
// 查询使用某个DNS域名的所有集群域名
// FindAllEnabledClustersWithDNSDomainId 查询使用某个DNS域名的所有集群域名
func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSDomainId(tx *dbs.Tx, dnsDomainId int64) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
@@ -386,7 +386,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSDomainId(tx *dbs.Tx, dn
return
}
// 查询已经设置了域名的集群
// FindAllEnabledClustersHaveDNSDomain 查询已经设置了域名的集群
func (this *NodeClusterDAO) FindAllEnabledClustersHaveDNSDomain(tx *dbs.Tx) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
@@ -397,7 +397,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersHaveDNSDomain(tx *dbs.Tx) (res
return
}
// 查找集群的认证ID
// FindClusterGrantId 查找集群的认证ID
func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
Pk(clusterId).
@@ -405,7 +405,7 @@ func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int
FindInt64Col(0)
}
// 查找DNS信息
// FindClusterDNSInfo 查找DNS信息
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64) (*NodeCluster, error) {
one, err := this.Query(tx).
Pk(clusterId).
@@ -420,7 +420,7 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64) (*No
return one.(*NodeCluster), nil
}
// 检查某个子域名是否可用
// ExistClusterDNSName 检查某个子域名是否可用
func (this *NodeClusterDAO) ExistClusterDNSName(tx *dbs.Tx, dnsName string, excludeClusterId int64) (bool, error) {
return this.Query(tx).
Attr("dnsName", dnsName).
@@ -430,7 +430,7 @@ func (this *NodeClusterDAO) ExistClusterDNSName(tx *dbs.Tx, dnsName string, excl
Exist()
}
// 修改集群DNS相关信息
// UpdateClusterDNS 修改集群DNS相关信息
func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
@@ -461,7 +461,7 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
return this.NotifyDNSUpdate(tx, clusterId)
}
// 检查集群的DNS问题
// CheckClusterDNS 检查集群的DNS问题
func (this *NodeClusterDAO) CheckClusterDNS(tx *dbs.Tx, cluster *NodeCluster) (issues []*pb.DNSIssue, err error) {
clusterId := int64(cluster.Id)
domainId := int64(cluster.DnsDomainId)
@@ -549,7 +549,7 @@ func (this *NodeClusterDAO) CheckClusterDNS(tx *dbs.Tx, cluster *NodeCluster) (i
}
// 检查IP地址
ipAddr, err := SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId)
ipAddr, err := SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, nodeconfigs.NodeRoleNode)
if err != nil {
return nil, err
}
@@ -573,7 +573,7 @@ func (this *NodeClusterDAO) CheckClusterDNS(tx *dbs.Tx, cluster *NodeCluster) (i
return
}
// 查找集群所属管理员
// FindClusterAdminId 查找集群所属管理员
func (this *NodeClusterDAO) FindClusterAdminId(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
Pk(clusterId).
@@ -581,7 +581,7 @@ func (this *NodeClusterDAO) FindClusterAdminId(tx *dbs.Tx, clusterId int64) (int
FindInt64Col(0)
}
// 查找集群的TOA设置
// FindClusterTOAConfig 查找集群的TOA设置
func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*nodeconfigs.TOAConfig, error) {
toa, err := this.Query(tx).
Pk(clusterId).
@@ -602,7 +602,7 @@ func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*
return config, nil
}
// 修改集群的TOA设置
// UpdateClusterTOA 修改集群的TOA设置
func (this *NodeClusterDAO) UpdateClusterTOA(tx *dbs.Tx, clusterId int64, toaJSON []byte) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
@@ -617,7 +617,7 @@ func (this *NodeClusterDAO) UpdateClusterTOA(tx *dbs.Tx, clusterId int64, toaJSO
return this.NotifyUpdate(tx, clusterId)
}
// 计算使用某个缓存策略的集群数量
// CountAllEnabledNodeClustersWithHTTPCachePolicyId 计算使用某个缓存策略的集群数量
func (this *NodeClusterDAO) CountAllEnabledNodeClustersWithHTTPCachePolicyId(tx *dbs.Tx, httpCachePolicyId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterStateEnabled).
@@ -625,7 +625,7 @@ func (this *NodeClusterDAO) CountAllEnabledNodeClustersWithHTTPCachePolicyId(tx
Count()
}
// 查找使用缓存策略的所有集群
// FindAllEnabledNodeClustersWithHTTPCachePolicyId 查找使用缓存策略的所有集群
func (this *NodeClusterDAO) FindAllEnabledNodeClustersWithHTTPCachePolicyId(tx *dbs.Tx, httpCachePolicyId int64) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
@@ -636,7 +636,7 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClustersWithHTTPCachePolicyId(tx *
return
}
// 计算使用某个WAF策略的集群数量
// CountAllEnabledNodeClustersWithHTTPFirewallPolicyId 计算使用某个WAF策略的集群数量
func (this *NodeClusterDAO) CountAllEnabledNodeClustersWithHTTPFirewallPolicyId(tx *dbs.Tx, httpFirewallPolicyId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterStateEnabled).
@@ -644,7 +644,7 @@ func (this *NodeClusterDAO) CountAllEnabledNodeClustersWithHTTPFirewallPolicyId(
Count()
}
// 查找使用WAF策略的所有集群
// FindAllEnabledNodeClustersWithHTTPFirewallPolicyId 查找使用WAF策略的所有集群
func (this *NodeClusterDAO) FindAllEnabledNodeClustersWithHTTPFirewallPolicyId(tx *dbs.Tx, httpFirewallPolicyId int64) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
@@ -655,7 +655,7 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClustersWithHTTPFirewallPolicyId(t
return
}
// 查找使用WAF策略的所有集群Ids
// FindAllEnabledNodeClusterIdsWithHTTPFirewallPolicyId 查找使用WAF策略的所有集群Ids
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithHTTPFirewallPolicyId(tx *dbs.Tx, httpFirewallPolicyId int64) (result []int64, err error) {
ones, err := this.Query(tx).
State(NodeClusterStateEnabled).
@@ -668,7 +668,7 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithHTTPFirewallPolicyId
return
}
// 查找使用缓存策略的所有集群Ids
// FindAllEnabledNodeClusterIdsWithCachePolicyId 查找使用缓存策略的所有集群Ids
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId int64) (result []int64, err error) {
ones, err := this.Query(tx).
State(NodeClusterStateEnabled).
@@ -681,7 +681,7 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *db
return
}
// 获取集群的WAF策略ID
// FindClusterHTTPFirewallPolicyId 获取集群的WAF策略ID
func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
Pk(clusterId).
@@ -689,7 +689,7 @@ func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterI
FindInt64Col(0)
}
// 设置集群的缓存策略
// UpdateNodeClusterHTTPCachePolicyId 设置集群的缓存策略
func (this *NodeClusterDAO) UpdateNodeClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64, httpCachePolicyId int64) error {
_, err := this.Query(tx).
Pk(clusterId).
@@ -701,7 +701,7 @@ func (this *NodeClusterDAO) UpdateNodeClusterHTTPCachePolicyId(tx *dbs.Tx, clust
return this.NotifyUpdate(tx, clusterId)
}
// 获取集群的缓存策略ID
// FindClusterHTTPCachePolicyId 获取集群的缓存策略ID
func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
Pk(clusterId).
@@ -709,7 +709,7 @@ func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId i
FindInt64Col(0)
}
// 设置集群的WAF策略
// UpdateNodeClusterHTTPFirewallPolicyId 设置集群的WAF策略
func (this *NodeClusterDAO) UpdateNodeClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64, httpFirewallPolicyId int64) error {
_, err := this.Query(tx).
Pk(clusterId).
@@ -721,7 +721,7 @@ func (this *NodeClusterDAO) UpdateNodeClusterHTTPFirewallPolicyId(tx *dbs.Tx, cl
return this.NotifyUpdate(tx, clusterId)
}
// 修改集群的系统服务设置
// UpdateNodeClusterSystemService 修改集群的系统服务设置
func (this *NodeClusterDAO) UpdateNodeClusterSystemService(tx *dbs.Tx, clusterId int64, serviceType nodeconfigs.SystemServiceType, params maps.Map) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
@@ -760,7 +760,7 @@ func (this *NodeClusterDAO) UpdateNodeClusterSystemService(tx *dbs.Tx, clusterId
return this.NotifyUpdate(tx, clusterId)
}
// 查找集群的系统服务设置
// FindNodeClusterSystemServiceParams 查找集群的系统服务设置
func (this *NodeClusterDAO) FindNodeClusterSystemServiceParams(tx *dbs.Tx, clusterId int64, serviceType nodeconfigs.SystemServiceType) (params maps.Map, err error) {
if clusterId <= 0 {
return nil, errors.New("invalid clusterId")
@@ -782,7 +782,7 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServiceParams(tx *dbs.Tx, clust
return servicesMap[serviceType], nil
}
// 查找集群的所有服务设置
// FindNodeClusterSystemServices 查找集群的所有服务设置
func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64) (services map[string]maps.Map, err error) {
if clusterId <= 0 {
return nil, errors.New("invalid clusterId")
@@ -804,7 +804,7 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId
return servicesMap, nil
}
// 生成唯一ID
// GenUniqueId 生成唯一ID
func (this *NodeClusterDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
for {
uniqueId := rands.HexString(32)
@@ -821,12 +821,28 @@ func (this *NodeClusterDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
}
}
// 通知更新
// FindLatestNodeClusters 查询最近访问的集群
func (this *NodeClusterDAO) FindLatestNodeClusters(tx *dbs.Tx, size int64) (result []*NodeCluster, err error) {
itemTable := SharedLatestItemDAO.Table
itemType := LatestItemTypeCluster
_, err = this.Query(tx).
Result(this.Table+".id", this.Table+".name").
Join(SharedLatestItemDAO, dbs.QueryJoinRight, this.Table+".id="+itemTable+".itemId AND "+itemTable+".itemType='"+itemType+"'").
Asc("CEIL((UNIX_TIMESTAMP() - " + itemTable + ".updatedAt) / (7 * 86400))"). // 优先一个星期以内的
Desc(itemTable + ".count").
State(NodeClusterStateEnabled).
Limit(size).
Slice(&result).
FindAll()
return
}
// NotifyUpdate 通知更新
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
return SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
}
// 通知DNS更新
// NotifyDNSUpdate 通知DNS更新
// TODO 更新新的DNS解析记录的同时需要删除老的DNS解析记录
func (this *NodeClusterDAO) NotifyDNSUpdate(tx *dbs.Tx, clusterId int64) error {
err := dns.SharedDNSTaskDAO.CreateClusterTask(tx, clusterId, dns.DNSTaskTypeClusterChange)

View File

@@ -9,6 +9,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -124,7 +125,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
secret := rands.String(32)
// 保存API Token
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, NodeRoleNode)
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleNode)
if err != nil {
return
}
@@ -161,7 +162,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
}
// UpdateNode 修改节点
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, groupId int64, regionId int64, maxCPU int32, isOn bool) error {
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, groupId int64, regionId int64, maxCPU int32, isOn bool, maxCacheDiskCapacityJSON []byte, maxCacheMemoryCapacityJSON []byte) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
@@ -174,6 +175,12 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId
op.LatestVersion = dbs.SQL("latestVersion+1")
op.MaxCPU = maxCPU
op.IsOn = isOn
if len(maxCacheDiskCapacityJSON) > 0 {
op.MaxCacheDiskCapacity = maxCacheDiskCapacityJSON
}
if len(maxCacheMemoryCapacityJSON) > 0 {
op.MaxCacheMemoryCapacity = maxCacheMemoryCapacityJSON
}
err := this.Save(tx, op)
if err != nil {
return err
@@ -395,6 +402,27 @@ func (this *NodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byt
return err
}
// FindNodeStatus 获取节点状态
func (this *NodeDAO) FindNodeStatus(tx *dbs.Tx, nodeId int64) (*nodeconfigs.NodeStatus, error) {
statusJSONString, err := this.Query(tx).
Pk(nodeId).
Result("status").
FindStringCol("")
if err != nil {
return nil, err
}
if len(statusJSONString) == 0 {
return nil, nil
}
status := &nodeconfigs.NodeStatus{}
err = json.Unmarshal([]byte(statusJSONString), status)
if err != nil {
return nil, err
}
return status, nil
}
// UpdateNodeIsActive 更改节点在线状态
func (this *NodeDAO) UpdateNodeIsActive(tx *dbs.Tx, nodeId int64, isActive bool) error {
b := "true"
@@ -558,6 +586,29 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N
}
}
// 缓存最大容量设置
if len(node.MaxCacheDiskCapacity) > 0 {
capacity := &shared.SizeCapacity{}
err = json.Unmarshal([]byte(node.MaxCacheDiskCapacity), capacity)
if err != nil {
return nil, err
}
if capacity.Count > 0 {
config.MaxCacheDiskCapacity = capacity
}
}
if len(node.MaxCacheMemoryCapacity) > 0 {
capacity := &shared.SizeCapacity{}
err = json.Unmarshal([]byte(node.MaxCacheMemoryCapacity), capacity)
if err != nil {
return nil, err
}
if capacity.Count > 0 {
config.MaxCacheMemoryCapacity = capacity
}
}
// TOA
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, clusterId)
if err != nil {
@@ -692,7 +743,7 @@ func (this *NodeDAO) FindAllNotInstalledNodesWithClusterId(tx *dbs.Tx, clusterId
return
}
// CountAllLowerVersionNodesWithClusterId 计算所有低于某个版本的节点数量
// CountAllLowerVersionNodesWithClusterId 计算单个集群中所有低于某个版本的节点数量
func (this *NodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId int64, os string, arch string, version string) (int64, error) {
return this.Query(tx).
State(NodeStateEnabled).
@@ -707,7 +758,7 @@ func (this *NodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterI
Count()
}
// FindAllLowerVersionNodesWithClusterId 查找所有低于某个版本的节点
// FindAllLowerVersionNodesWithClusterId 查找单个集群中所有低于某个版本的节点
func (this *NodeDAO) FindAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId int64, os string, arch string, version string) (result []*Node, err error) {
_, err = this.Query(tx).
State(NodeStateEnabled).
@@ -725,6 +776,17 @@ func (this *NodeDAO) FindAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId
return
}
// CountAllLowerVersionNodes 计算所有集群中低于某个版本的节点数量
func (this *NodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
return this.Query(tx).
State(NodeStateEnabled).
Where("clusterId IN (SELECT id FROM "+SharedNodeClusterDAO.Table+" WHERE state=1)").
Where("status IS NOT NULL").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("version", utils.VersionToLong(version)).
Count()
}
// CountAllEnabledNodesWithGroupId 查找某个节点分组下的所有节点数量
func (this *NodeDAO) CountAllEnabledNodesWithGroupId(tx *dbs.Tx, groupId int64) (int64, error) {
return this.Query(tx).

View File

@@ -34,7 +34,7 @@ func init() {
})
}
// 启用条目
// EnableNodeGrant 启用条目
func (this *NodeGrantDAO) EnableNodeGrant(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -42,7 +42,7 @@ func (this *NodeGrantDAO) EnableNodeGrant(tx *dbs.Tx, id uint32) (rowsAffected i
Update()
}
// 禁用条目
// DisableNodeGrant 禁用条目
func (this *NodeGrantDAO) DisableNodeGrant(tx *dbs.Tx, id int64) (err error) {
_, err = this.Query(tx).
Pk(id).
@@ -51,7 +51,7 @@ func (this *NodeGrantDAO) DisableNodeGrant(tx *dbs.Tx, id int64) (err error) {
return err
}
// 查找启用中的条目
// FindEnabledNodeGrant 查找启用中的条目
func (this *NodeGrantDAO) FindEnabledNodeGrant(tx *dbs.Tx, id int64) (*NodeGrant, error) {
result, err := this.Query(tx).
Pk(id).
@@ -63,7 +63,7 @@ func (this *NodeGrantDAO) FindEnabledNodeGrant(tx *dbs.Tx, id int64) (*NodeGrant
return result.(*NodeGrant), err
}
// 根据主键查找名称
// FindNodeGrantName 根据主键查找名称
func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, error) {
name, err := this.Query(tx).
Pk(id).
@@ -72,7 +72,7 @@ func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, erro
return name.(string), err
}
// 创建认证信息
// 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) {
op := NewNodeGrantOperator()
op.AdminId = adminId
@@ -94,7 +94,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
return types.Int64(op.Id), err
}
// 修改认证信息
// 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 {
if grantId <= 0 {
return errors.New("invalid grantId")
@@ -119,17 +119,26 @@ func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, me
return err
}
// 计算所有认证信息数量
func (this *NodeGrantDAO) CountAllEnabledGrants(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(NodeGrantStateEnabled).
Count()
// CountAllEnabledGrants 计算所有认证信息数量
func (this *NodeGrantDAO) CountAllEnabledGrants(tx *dbs.Tx, keyword string) (int64, error) {
query := this.Query(tx).
State(NodeGrantStateEnabled)
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR username LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.Count()
}
// 列出单页的认证信息
func (this *NodeGrantDAO) ListEnabledGrants(tx *dbs.Tx, offset int64, size int64) (result []*NodeGrant, err error) {
_, err = this.Query(tx).
State(NodeGrantStateEnabled).
// ListEnabledGrants 列出单页的认证信息
func (this *NodeGrantDAO) ListEnabledGrants(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*NodeGrant, err error) {
query := this.Query(tx).
State(NodeGrantStateEnabled)
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR username LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
_, err = query.
Offset(offset).
Size(size).
DescPk().
@@ -138,7 +147,7 @@ func (this *NodeGrantDAO) ListEnabledGrants(tx *dbs.Tx, offset int64, size int64
return
}
// 列出所有的认证信息
// FindAllEnabledGrants 列出所有的认证信息
func (this *NodeGrantDAO) FindAllEnabledGrants(tx *dbs.Tx) (result []*NodeGrant, err error) {
_, err = this.Query(tx).
State(NodeGrantStateEnabled).

View File

@@ -2,6 +2,7 @@ package models
import (
"errors"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -34,7 +35,7 @@ func init() {
})
}
// 启用条目
// EnableAddress 启用条目
func (this *NodeIPAddressDAO) EnableAddress(tx *dbs.Tx, id int64) (err error) {
_, err = this.Query(tx).
Pk(id).
@@ -43,7 +44,7 @@ func (this *NodeIPAddressDAO) EnableAddress(tx *dbs.Tx, id int64) (err error) {
return err
}
// 禁用IP地址
// DisableAddress 禁用IP地址
func (this *NodeIPAddressDAO) DisableAddress(tx *dbs.Tx, id int64) (err error) {
_, err = this.Query(tx).
Pk(id).
@@ -52,11 +53,14 @@ func (this *NodeIPAddressDAO) DisableAddress(tx *dbs.Tx, id int64) (err error) {
return err
}
// 禁用节点的所有的IP地址
func (this *NodeIPAddressDAO) DisableAllAddressesWithNodeId(tx *dbs.Tx, nodeId int64) error {
// DisableAllAddressesWithNodeId 禁用节点的所有的IP地址
func (this *NodeIPAddressDAO) DisableAllAddressesWithNodeId(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
_, err := this.Query(tx).
Attr("nodeId", nodeId).
Set("state", NodeIPAddressStateDisabled).
@@ -64,7 +68,7 @@ func (this *NodeIPAddressDAO) DisableAllAddressesWithNodeId(tx *dbs.Tx, nodeId i
return err
}
// 查找启用中的IP地址
// FindEnabledAddress 查找启用中的IP地址
func (this *NodeIPAddressDAO) FindEnabledAddress(tx *dbs.Tx, id int64) (*NodeIPAddress, error) {
result, err := this.Query(tx).
Pk(id).
@@ -76,7 +80,7 @@ func (this *NodeIPAddressDAO) FindEnabledAddress(tx *dbs.Tx, id int64) (*NodeIPA
return result.(*NodeIPAddress), err
}
// 根据主键查找名称
// FindAddressName 根据主键查找名称
func (this *NodeIPAddressDAO) FindAddressName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -84,10 +88,15 @@ func (this *NodeIPAddressDAO) FindAddressName(tx *dbs.Tx, id int64) (string, err
FindStringCol("")
}
// 创建IP地址
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, nodeId int64, name string, ip string, canAccess bool) (addressId int64, err error) {
// CreateAddress 创建IP地址
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool) (addressId int64, err error) {
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
op := NewNodeIPAddressOperator()
op.NodeId = nodeId
op.Role = role
op.Name = name
op.Ip = ip
op.CanAccess = canAccess
@@ -105,7 +114,7 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, nodeId int64, name strin
return types.Int64(op.Id), nil
}
// 修改IP地址
// UpdateAddress 修改IP地址
func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, addressId int64, name string, ip string, canAccess bool) (err error) {
if addressId <= 0 {
return errors.New("invalid addressId")
@@ -121,7 +130,7 @@ func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, addressId int64, name st
return err
}
// 修改IP地址中的IP
// UpdateAddressIP 修改IP地址中的IP
func (this *NodeIPAddressDAO) UpdateAddressIP(tx *dbs.Tx, addressId int64, ip string) error {
if addressId <= 0 {
return errors.New("invalid addressId")
@@ -133,7 +142,7 @@ func (this *NodeIPAddressDAO) UpdateAddressIP(tx *dbs.Tx, addressId int64, ip st
return err
}
// 修改IP地址所属节点
// UpdateAddressNodeId 修改IP地址所属节点
func (this *NodeIPAddressDAO) UpdateAddressNodeId(tx *dbs.Tx, addressId int64, nodeId int64) error {
_, err := this.Query(tx).
Pk(addressId).
@@ -151,10 +160,14 @@ func (this *NodeIPAddressDAO) UpdateAddressNodeId(tx *dbs.Tx, addressId int64, n
return nil
}
// 查找节点的所有的IP地址
func (this *NodeIPAddressDAO) FindAllEnabledAddressesWithNode(tx *dbs.Tx, nodeId int64) (result []*NodeIPAddress, err error) {
// FindAllEnabledAddressesWithNode 查找节点的所有的IP地址
func (this *NodeIPAddressDAO) FindAllEnabledAddressesWithNode(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (result []*NodeIPAddress, err error) {
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
_, err = this.Query(tx).
Attr("nodeId", nodeId).
Attr("role", role).
State(NodeIPAddressStateEnabled).
Desc("order").
AscPk().
@@ -163,10 +176,14 @@ func (this *NodeIPAddressDAO) FindAllEnabledAddressesWithNode(tx *dbs.Tx, nodeId
return
}
// 查找节点的第一个可访问的IP地址
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId int64) (string, error) {
// FindFirstNodeAccessIPAddress 查找节点的第一个可访问的IP地址
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (string, error) {
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
return this.Query(tx).
Attr("nodeId", nodeId).
Attr("role", role).
State(NodeIPAddressStateEnabled).
Attr("canAccess", true).
Desc("order").
@@ -175,10 +192,14 @@ func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddress(tx *dbs.Tx, nodeId in
FindStringCol("")
}
// 查找节点的第一个可访问的IP地址ID
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddressId(tx *dbs.Tx, nodeId int64) (int64, error) {
// FindFirstNodeAccessIPAddressId 查找节点的第一个可访问的IP地址ID
func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddressId(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (int64, error) {
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
return this.Query(tx).
Attr("nodeId", nodeId).
Attr("role", role).
State(NodeIPAddressStateEnabled).
Attr("canAccess", true).
Desc("order").
@@ -187,8 +208,11 @@ func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddressId(tx *dbs.Tx, nodeId
FindInt64Col(0)
}
// 查找节点所有的可访问的IP地址
func (this *NodeIPAddressDAO) FindNodeAccessIPAddresses(tx *dbs.Tx, nodeId int64) (result []*NodeIPAddress, err error) {
// FindNodeAccessIPAddresses 查找节点所有的可访问的IP地址
func (this *NodeIPAddressDAO) FindNodeAccessIPAddresses(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (result []*NodeIPAddress, err error) {
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
_, err = this.Query(tx).
Attr("nodeId", nodeId).
State(NodeIPAddressStateEnabled).

View File

@@ -1,9 +1,10 @@
package models
// 节点IP地址
// NodeIPAddress 节点IP地址
type NodeIPAddress struct {
Id uint32 `field:"id"` // ID
NodeId uint32 `field:"nodeId"` // 节点ID
Role string `field:"role"` // 节点角色
Name string `field:"name"` // 名称
Ip string `field:"ip"` // IP地址
Description string `field:"description"` // 描述
@@ -15,6 +16,7 @@ type NodeIPAddress struct {
type NodeIPAddressOperator struct {
Id interface{} // ID
NodeId interface{} // 节点ID
Role interface{} // 节点角色
Name interface{} // 名称
Ip interface{} // IP地址
Description interface{} // 描述

View File

@@ -2,18 +2,20 @@ package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
stringutil "github.com/iwind/TeaGo/utils/string"
timeutil "github.com/iwind/TeaGo/utils/time"
"strconv"
"strings"
"time"
)
type NodeLogDAO dbs.DAO
const ()
func NewNodeLogDAO() *NodeLogDAO {
return dbs.NewDAO(&NodeLogDAO{
DAOObject: dbs.DAOObject{
@@ -33,21 +35,44 @@ func init() {
})
}
// 创建日志
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, level string, tag string, description string, createdAt int64) error {
// CreateLog 创建日志
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, level string, tag string, description string, createdAt int64) error {
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + level + "@" + tag + "@" + description)
// 检查是否在重复最后一条,避免重复创建
lastLog, err := this.Query(tx).
Result("id", "hash", "createdAt").
DescPk().
Find()
if err != nil {
return err
}
if lastLog != nil {
nodeLog := lastLog.(*NodeLog)
if nodeLog.Hash == hash && time.Now().Unix()-int64(nodeLog.CreatedAt) < 1800 {
err = this.Query(tx).
Pk(nodeLog.Id).
Set("count", dbs.SQL("count+1")).
UpdateQuickly()
return err
}
}
op := NewNodeLogOperator()
op.Role = nodeRole
op.NodeId = nodeId
op.ServerId = serverId
op.Level = level
op.Tag = tag
op.Description = description
op.CreatedAt = createdAt
op.Day = timeutil.FormatTime("Ymd", createdAt)
err := this.Save(tx, op)
op.Hash = hash
err = this.Save(tx, op)
return err
}
// 清除超出一定日期的日志
// DeleteExpiredLogs 清除超出一定日期的日志
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
if days <= 0 {
return errors.New("invalid days '" + strconv.Itoa(days) + "'")
@@ -61,19 +86,93 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
return err
}
// 计算节点数量
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64) (int64, error) {
return this.Query(tx).
Attr("nodeId", nodeId).
Attr("role", role).
Count()
// CountNodeLogs 计算节点日志数量
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
query := this.Query(tx).
Attr("role", role)
if nodeId > 0 {
query.Attr("nodeId", nodeId)
} else {
switch role {
case nodeconfigs.NodeRoleNode:
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
case nodeconfigs.NodeRoleDNS:
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
}
}
if serverId > 0 {
query.Attr("serverId", serverId)
}
if len(dayFrom) > 0 {
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
query.Gte("day", dayFrom)
}
if len(dayTo) > 0 {
dayTo = strings.ReplaceAll(dayTo, "-", "")
query.Lte("day", dayTo)
}
if len(keyword) > 0 {
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if len(level) > 0 {
query.Attr("level", level)
}
return query.Count()
}
// 列出单页日志
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, offset int64, size int64) (result []*NodeLog, err error) {
_, err = this.Query(tx).
Attr("nodeId", nodeId).
Attr("role", role).
// ListNodeLogs 列出单页日志
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
role string,
nodeId int64,
serverId int64,
allServers bool,
dayFrom string,
dayTo string,
keyword string,
level string,
fixedState configutils.BoolState,
offset int64,
size int64) (result []*NodeLog, err error) {
query := this.Query(tx).
Attr("role", role)
if nodeId > 0 {
query.Attr("nodeId", nodeId)
} else {
switch role {
case nodeconfigs.NodeRoleNode:
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
case nodeconfigs.NodeRoleDNS:
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
}
}
if serverId > 0 {
query.Attr("serverId", serverId)
} else if allServers {
query.Where("serverId>0")
}
if fixedState == configutils.BoolStateYes {
query.Attr("isFixed", 1)
} else if fixedState == configutils.BoolStateNo {
query.Attr("isFixed", 0)
}
if len(dayFrom) > 0 {
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
query.Gte("day", dayFrom)
}
if len(dayTo) > 0 {
dayTo = strings.ReplaceAll(dayTo, "-", "")
query.Lte("day", dayTo)
}
if len(keyword) > 0 {
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if len(level) > 0 {
query.Attr("level", level)
}
_, err = query.
Offset(offset).
Limit(size).
Slice(&result).
@@ -81,3 +180,33 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, offs
FindAll()
return
}
// UpdateNodeLogFixed 设置节点日志为已修复
func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
if logId <= 0 {
return errors.New("invalid logId")
}
// 我们把相同内容的日志都置为已修复
hash, err := this.Query(tx).
Pk(logId).
Result("hash").
FindStringCol("")
if err != nil {
return err
}
if len(hash) == 0 {
return nil
}
err = this.Query(tx).
Attr("hash", hash).
Attr("isFixed", false).
Set("isFixed", true).
UpdateQuickly()
if err != nil {
return err
}
return nil
}

View File

@@ -1,6 +1,6 @@
package models
// 节点日志
// NodeLog 节点日志
type NodeLog struct {
Id uint64 `field:"id"` // ID
Role string `field:"role"` // 节点角色
@@ -10,6 +10,10 @@ type NodeLog struct {
Level string `field:"level"` // 级别
NodeId uint32 `field:"nodeId"` // 节点ID
Day string `field:"day"` // 日期
ServerId uint32 `field:"serverId"` // 服务ID
Hash string `field:"hash"` // 信息内容Hash
Count uint32 `field:"count"` // 重复次数
IsFixed uint8 `field:"isFixed"` // 是否已处理
}
type NodeLogOperator struct {
@@ -21,6 +25,10 @@ type NodeLogOperator struct {
Level interface{} // 级别
NodeId interface{} // 节点ID
Day interface{} // 日期
ServerId interface{} // 服务ID
Hash interface{} // 信息内容Hash
Count interface{} // 重复次数
IsFixed interface{} // 是否已处理
}
func NewNodeLogOperator() *NodeLogOperator {

View File

@@ -1,62 +1,66 @@
package models
// 节点
// Node 节点
type Node struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用
IsUp uint8 `field:"isUp"` // 是否在线
CountUp uint32 `field:"countUp"` // 连续在线次数
CountDown uint32 `field:"countDown"` // 连续下线次数
IsActive uint8 `field:"isActive"` // 是否活跃
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
Name string `field:"name"` // 节点名
Code string `field:"code"` // 代号
ClusterId uint32 `field:"clusterId"` // 集群ID
RegionId uint32 `field:"regionId"` // 区域ID
GroupId uint32 `field:"groupId"` // 分组ID
CreatedAt uint64 `field:"createdAt"` // 创建时间
Status string `field:"status"` // 最新的状态
Version uint32 `field:"version"` // 当前版本号
LatestVersion uint32 `field:"latestVersion"` // 最后版本号
InstallDir string `field:"installDir"` // 安装目录
IsInstalled uint8 `field:"isInstalled"` // 是否已安装
InstallStatus string `field:"installStatus"` // 安装状态
State uint8 `field:"state"` // 状态
ConnectedAPINodes string `field:"connectedAPINodes"` // 当前连接的API节点
MaxCPU uint32 `field:"maxCPU"` // 可以使用的最多CPU
DnsRoutes string `field:"dnsRoutes"` // DNS线路设置
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用
IsUp uint8 `field:"isUp"` // 是否在线
CountUp uint32 `field:"countUp"` // 连续在线次数
CountDown uint32 `field:"countDown"` // 连续下线次数
IsActive uint8 `field:"isActive"` // 是否活跃
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
Name string `field:"name"` // 节点名
Code string `field:"code"` // 代号
ClusterId uint32 `field:"clusterId"` // 集群ID
RegionId uint32 `field:"regionId"` // 区域ID
GroupId uint32 `field:"groupId"` // 分组ID
CreatedAt uint64 `field:"createdAt"` // 创建时间
Status string `field:"status"` // 最新的状态
Version uint32 `field:"version"` // 当前版本号
LatestVersion uint32 `field:"latestVersion"` // 最后版本号
InstallDir string `field:"installDir"` // 安装目录
IsInstalled uint8 `field:"isInstalled"` // 是否已安装
InstallStatus string `field:"installStatus"` // 安装状态
State uint8 `field:"state"` // 状态
ConnectedAPINodes string `field:"connectedAPINodes"` // 当前连接的API节点
MaxCPU uint32 `field:"maxCPU"` // 可以使用的最多CPU
DnsRoutes string `field:"dnsRoutes"` // DNS线路设置
MaxCacheDiskCapacity string `field:"maxCacheDiskCapacity"` // 硬盘缓存容量
MaxCacheMemoryCapacity string `field:"maxCacheMemoryCapacity"` // 内存缓存容量
}
type NodeOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
IsUp interface{} // 是否在线
CountUp interface{} // 连续在线次数
CountDown interface{} // 连续下线次数
IsActive interface{} // 是否活跃
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
Name interface{} // 节点名
Code interface{} // 代号
ClusterId interface{} // 集群ID
RegionId interface{} // 区域ID
GroupId interface{} // 分组ID
CreatedAt interface{} // 创建时间
Status interface{} // 最新的状态
Version interface{} // 当前版本号
LatestVersion interface{} // 最后版本号
InstallDir interface{} // 安装目录
IsInstalled interface{} // 是否已安装
InstallStatus interface{} // 安装状态
State interface{} // 状态
ConnectedAPINodes interface{} // 当前连接的API节点
MaxCPU interface{} // 可以使用的最多CPU
DnsRoutes interface{} // DNS线路设置
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
IsUp interface{} // 是否在线
CountUp interface{} // 连续在线次数
CountDown interface{} // 连续下线次数
IsActive interface{} // 是否活跃
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
Name interface{} // 节点名
Code interface{} // 代号
ClusterId interface{} // 集群ID
RegionId interface{} // 区域ID
GroupId interface{} // 分组ID
CreatedAt interface{} // 创建时间
Status interface{} // 最新的状态
Version interface{} // 当前版本号
LatestVersion interface{} // 最后版本号
InstallDir interface{} // 安装目录
IsInstalled interface{} // 是否已安装
InstallStatus interface{} // 安装状态
State interface{} // 状态
ConnectedAPINodes interface{} // 当前连接的API节点
MaxCPU interface{} // 可以使用的最多CPU
DnsRoutes interface{} // DNS线路设置
MaxCacheDiskCapacity interface{} // 硬盘缓存容量
MaxCacheMemoryCapacity interface{} // 内存缓存容量
}
func NewNodeOperator() *NodeOperator {

View File

@@ -1,17 +0,0 @@
package models
type NodeRole = string
const (
NodeRoleAdmin NodeRole = "admin"
NodeRoleUser NodeRole = "user"
NodeRoleProvider NodeRole = "provider"
NodeRoleAPI NodeRole = "api"
NodeRoleDatabase NodeRole = "database"
NodeRoleLog NodeRole = "log"
NodeRoleDNS NodeRole = "dns"
NodeRoleMonitor NodeRole = "monitor"
NodeRoleNode NodeRole = "node"
NodeRoleCluster NodeRole = "cluster"
NodeRoleAuthority NodeRole = "authority"
)

View File

@@ -13,8 +13,9 @@ import (
type NodeTaskType = string
const (
NodeTaskTypeConfigChanged NodeTaskType = "configChanged"
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
NodeTaskTypeConfigChanged NodeTaskType = "configChanged"
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
)
type NodeTaskDAO dbs.DAO

View File

@@ -0,0 +1,271 @@
package models
import (
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"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/maps"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"strings"
"time"
)
const (
NodeThresholdStateEnabled = 1 // 已启用
NodeThresholdStateDisabled = 0 // 已禁用
)
type NodeThresholdDAO dbs.DAO
func NewNodeThresholdDAO() *NodeThresholdDAO {
return dbs.NewDAO(&NodeThresholdDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNodeThresholds",
Model: new(NodeThreshold),
PkName: "id",
},
}).(*NodeThresholdDAO)
}
var SharedNodeThresholdDAO *NodeThresholdDAO
func init() {
dbs.OnReady(func() {
SharedNodeThresholdDAO = NewNodeThresholdDAO()
})
}
// EnableNodeThreshold 启用条目
func (this *NodeThresholdDAO) EnableNodeThreshold(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NodeThresholdStateEnabled).
Update()
return err
}
// DisableNodeThreshold 禁用条目
func (this *NodeThresholdDAO) DisableNodeThreshold(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NodeThresholdStateDisabled).
Update()
return err
}
// FindEnabledNodeThreshold 查找启用中的条目
func (this *NodeThresholdDAO) FindEnabledNodeThreshold(tx *dbs.Tx, id int64) (*NodeThreshold, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NodeThresholdStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NodeThreshold), err
}
// CreateThreshold 创建阈值
func (this *NodeThresholdDAO) CreateThreshold(tx *dbs.Tx, role string, clusterId int64, nodeId int64, item nodeconfigs.NodeValueItem, param string, operator nodeconfigs.NodeValueOperator, valueJSON []byte, message string, sumMethod nodeconfigs.NodeValueSumMethod, duration int32, durationUnit nodeconfigs.NodeValueDurationUnit, notifyDuration int32) (int64, error) {
op := NewNodeThresholdOperator()
op.Role = role
op.ClusterId = clusterId
op.NodeId = nodeId
op.Item = item
op.Param = param
op.Operator = operator
op.Value = valueJSON
op.Message = message
op.SumMethod = sumMethod
op.Duration = duration
op.DurationUnit = durationUnit
op.NotifyDuration = notifyDuration
op.IsOn = true
op.State = NodeThresholdStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateThreshold 修改阈值
func (this *NodeThresholdDAO) UpdateThreshold(tx *dbs.Tx, thresholdId int64, item nodeconfigs.NodeValueItem, param string, operator nodeconfigs.NodeValueOperator, valueJSON []byte, message string, sumMethod nodeconfigs.NodeValueSumMethod, duration int32, durationUnit nodeconfigs.NodeValueDurationUnit, notifyDuration int32, isOn bool) error {
if thresholdId <= 0 {
return errors.New("invalid thresholdId")
}
op := NewNodeThresholdOperator()
op.Id = thresholdId
op.Item = item
op.Param = param
op.Operator = operator
op.Value = valueJSON
op.Message = message
op.SumMethod = sumMethod
op.Duration = duration
op.DurationUnit = durationUnit
op.NotifyDuration = notifyDuration
op.IsOn = isOn
return this.Save(tx, op)
}
// FindAllEnabledThresholds 列出所有阈值
func (this *NodeThresholdDAO) FindAllEnabledThresholds(tx *dbs.Tx, role string, clusterId int64, nodeId int64) (result []*NodeThreshold, err error) {
if clusterId <= 0 && nodeId <= 0 {
return
}
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
if nodeId > 0 {
query.Attr("nodeId", nodeId)
}
query.State(NodeThresholdStateEnabled)
query.Slice(&result)
_, err = query.
Attr("role", role).
Asc("IF(nodeId>0, 1, 0)").
Desc("order").
AscPk().
FindAll()
return
}
// FindAllEnabledAndOnClusterThresholds 查询集群专属的阈值设置
func (this *NodeThresholdDAO) FindAllEnabledAndOnClusterThresholds(tx *dbs.Tx, role string, clusterId int64, item string) (result []*NodeThreshold, err error) {
if clusterId <= 0 {
return
}
_, err = this.Query(tx).
Attr("role", role).
Attr("clusterId", clusterId).
Attr("nodeId", 0).
Attr("item", item).
Attr("isOn", true).
State(NodeThresholdStateEnabled).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// FindAllEnabledAndOnNodeThresholds 查询节点专属的阈值设置
func (this *NodeThresholdDAO) FindAllEnabledAndOnNodeThresholds(tx *dbs.Tx, role string, nodeId int64, item string) (result []*NodeThreshold, err error) {
if nodeId <= 0 {
return
}
_, err = this.Query(tx).
Attr("role", role).
Attr("nodeId", nodeId).
Attr("item", item).
Attr("isOn", true).
State(NodeThresholdStateEnabled).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// CountAllEnabledThresholds 计算阈值的数量
func (this *NodeThresholdDAO) CountAllEnabledThresholds(tx *dbs.Tx, clusterId int64, nodeId int64) (int64, error) {
if clusterId <= 0 && nodeId <= 0 {
return 0, nil
}
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
if nodeId > 0 {
query.Attr("nodeId", nodeId)
}
query.State(NodeThresholdStateEnabled)
return query.Count()
}
// FireNodeThreshold 触发相关阈值设置
func (this *NodeThresholdDAO) FireNodeThreshold(tx *dbs.Tx, role string, nodeId int64, item string) error {
clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
if err != nil {
return err
}
if clusterId == 0 {
return nil
}
// 集群相关阈值
var thresholds []*NodeThreshold
{
clusterThresholds, err := this.FindAllEnabledAndOnClusterThresholds(tx, role, clusterId, item)
if err != nil {
return err
}
thresholds = append(thresholds, clusterThresholds...)
}
// 节点相关阈值
{
nodeThresholds, err := this.FindAllEnabledAndOnNodeThresholds(tx, role, nodeId, item)
if err != nil {
return err
}
thresholds = append(thresholds, nodeThresholds...)
}
if len(thresholds) > 0 {
for _, threshold := range thresholds {
if len(threshold.Param) == 0 || threshold.Duration <= 0 {
continue
}
paramValue, err := SharedNodeValueDAO.SumValues(tx, role, nodeId, item, threshold.Param, threshold.SumMethod, types.Int32(threshold.Duration), threshold.DurationUnit)
if err != nil {
return err
}
originValue := nodeconfigs.UnmarshalNodeValue([]byte(threshold.Value))
thresholdValue := types.Float64(originValue)
isMatched := nodeconfigs.CompareNodeValue(threshold.Operator, paramValue, thresholdValue)
if isMatched {
// TODO 执行其他动作
// 是否已经通知过
if threshold.NotifyDuration > 0 && threshold.NotifiedAt > 0 && time.Now().Unix()-int64(threshold.NotifiedAt) < int64(threshold.NotifyDuration*60) {
continue
}
// 创建消息
nodeName, err := SharedNodeDAO.FindNodeName(tx, nodeId)
if err != nil {
return err
}
itemName := nodeconfigs.FindNodeValueItemName(threshold.Item)
paramName := nodeconfigs.FindNodeValueItemParamName(threshold.Item, threshold.Param)
operatorName := nodeconfigs.FindNodeValueOperatorName(threshold.Operator)
subject := "节点 \"" + nodeName + "\" " + itemName + " 达到阈值"
body := "节点 \"" + nodeName + "\" " + itemName + " 达到阈值\n阈值设置" + paramName + " " + operatorName + " " + originValue + "\n当前值" + fmt.Sprintf("%.2f", paramValue) + "\n触发时间" + timeutil.Format("Y-m-d H:i:s")
if len(threshold.Message) > 0 {
body = threshold.Message
body = strings.Replace(body, "${item.name}", itemName, -1)
body = strings.Replace(body, "${value}", fmt.Sprintf("%.2f", paramValue), -1)
}
err = SharedMessageDAO.CreateNodeMessage(tx, clusterId, nodeId, MessageTypeThresholdSatisfied, MessageLevelWarning, subject, body, maps.Map{}.AsJSON())
if err != nil {
return err
}
// 设置通知时间
_, err = this.Query(tx).
Pk(threshold.Id).
Set("notifiedAt", time.Now().Unix()).
Update()
if err != nil {
return err
}
}
}
}
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,46 @@
package models
// NodeThreshold 集群阈值设置
type NodeThreshold struct {
Id uint64 `field:"id"` // ID
Role string `field:"role"` // 节点角色
ClusterId uint32 `field:"clusterId"` // 集群ID
NodeId uint32 `field:"nodeId"` // 节点ID
IsOn uint8 `field:"isOn"` // 是否启用
Item string `field:"item"` // 监控项
Param string `field:"param"` // 参数
Operator string `field:"operator"` // 操作符
Value string `field:"value"` // 对比值
Message string `field:"message"` // 消息内容
NotifyDuration uint32 `field:"notifyDuration"` // 通知间隔
NotifiedAt uint32 `field:"notifiedAt"` // 上次通知时间
Duration uint32 `field:"duration"` // 时间段
DurationUnit string `field:"durationUnit"` // 时间段单位
SumMethod string `field:"sumMethod"` // 聚合方法
Order uint32 `field:"order"` // 排序
State uint8 `field:"state"` // 状态
}
type NodeThresholdOperator struct {
Id interface{} // ID
Role interface{} // 节点角色
ClusterId interface{} // 集群ID
NodeId interface{} // 节点ID
IsOn interface{} // 是否启用
Item interface{} // 监控项
Param interface{} // 参数
Operator interface{} // 操作符
Value interface{} // 对比值
Message interface{} // 消息内容
NotifyDuration interface{} // 通知间隔
NotifiedAt interface{} // 上次通知时间
Duration interface{} // 时间段
DurationUnit interface{} // 时间段单位
SumMethod interface{} // 聚合方法
Order interface{} // 排序
State interface{} // 状态
}
func NewNodeThresholdOperator() *NodeThresholdOperator {
return &NodeThresholdOperator{}
}

View File

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

View File

@@ -0,0 +1,120 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"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/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
type NodeValueDAO dbs.DAO
func NewNodeValueDAO() *NodeValueDAO {
return dbs.NewDAO(&NodeValueDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNodeValues",
Model: new(NodeValue),
PkName: "id",
},
}).(*NodeValueDAO)
}
var SharedNodeValueDAO *NodeValueDAO
func init() {
dbs.OnReady(func() {
SharedNodeValueDAO = NewNodeValueDAO()
})
}
// CreateValue 创建值
func (this *NodeValueDAO) CreateValue(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64, item string, valueJSON []byte, createdAt int64) error {
day := timeutil.FormatTime("Ymd", createdAt)
hour := timeutil.FormatTime("YmdH", createdAt)
minute := timeutil.FormatTime("YmdHi", createdAt)
return this.Query(tx).
InsertOrUpdateQuickly(maps.Map{
"role": role,
"nodeId": nodeId,
"item": item,
"value": valueJSON,
"createdAt": createdAt,
"day": day,
"hour": hour,
"minute": minute,
}, maps.Map{
"value": valueJSON,
})
}
// DeleteExpiredValues 清除数据
func (this *NodeValueDAO) DeleteExpiredValues(tx *dbs.Tx) error {
// 删除N天之前的所有数据
expiredDays := 100
day := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -expiredDays))
_, err := this.Query(tx).
Where("day<:day").
Param("day", day).
Delete()
if err != nil {
return err
}
return nil
}
// ListValues 列出最近的的数据
func (this *NodeValueDAO) ListValues(tx *dbs.Tx, role string, nodeId int64, item string, timeRange nodeconfigs.NodeValueRange) (result []*NodeValue, err error) {
query := this.Query(tx).
Attr("role", role).
Attr("nodeId", nodeId).
Attr("item", item)
switch timeRange {
// TODO 支持更多的时间范围
case nodeconfigs.NodeValueRangeMinute:
fromMinute := timeutil.FormatTime("YmdHi", time.Now().Unix()-3600) // 一个小时之前的
query.Gte("minute", fromMinute)
default:
err = errors.New("invalid 'range' value: '" + timeRange + "'")
return
}
_, err = query.Slice(&result).
FindAll()
return
}
// SumValues 计算某项参数值
func (this *NodeValueDAO) SumValues(tx *dbs.Tx, role string, nodeId int64, item string, param string, method nodeconfigs.NodeValueSumMethod, duration int32, durationUnit nodeconfigs.NodeValueDurationUnit) (float64, error) {
if duration <= 0 {
return 0, nil
}
query := this.Query(tx).
Attr("role", role).
Attr("nodeId", nodeId).
Attr("item", item)
switch method {
case nodeconfigs.NodeValueSumMethodAvg:
query.Result("AVG(JSON_EXTRACT(value, '$." + param + "'))")
case nodeconfigs.NodeValueSumMethodSum:
query.Result("SUM(JSON_EXTRACT(value, '$." + param + "'))")
default:
query.Result("AVG(JSON_EXTRACT(value, '$." + param + "'))")
}
switch durationUnit {
case nodeconfigs.NodeValueDurationUnitMinute:
fromMinute := timeutil.FormatTime("YmdHi", time.Now().Unix()-int64(duration * 60))
query.Gte("minute", fromMinute)
default:
fromMinute := timeutil.FormatTime("YmdHi", time.Now().Unix()-int64(duration * 60))
query.Gte("minute", fromMinute)
}
return query.FindFloat64Col(0)
}

View File

@@ -0,0 +1,22 @@
package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/maps"
"testing"
"time"
)
func TestNodeValueDAO_CreateValue(t *testing.T) {
dao := NewNodeValueDAO()
m := maps.Map{
"hello": "world12344",
}
err := dao.CreateValue(nil, nodeconfigs.NodeRoleNode, 1, "test", m.AsJSON(), time.Now().Unix())
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -0,0 +1,30 @@
package models
// NodeValue 节点监控数据
type NodeValue struct {
Id uint64 `field:"id"` // ID
NodeId uint32 `field:"nodeId"` // 节点ID
Role string `field:"role"` // 节点角色
Item string `field:"item"` // 监控项
Value string `field:"value"` // 数据
CreatedAt uint64 `field:"createdAt"` // 创建时间
Day string `field:"day"` // 日期
Hour string `field:"hour"` // 小时
Minute string `field:"minute"` // 分钟
}
type NodeValueOperator struct {
Id interface{} // ID
NodeId interface{} // 节点ID
Role interface{} // 节点角色
Item interface{} // 监控项
Value interface{} // 数据
CreatedAt interface{} // 创建时间
Day interface{} // 日期
Hour interface{} // 小时
Minute interface{} // 分钟
}
func NewNodeValueOperator() *NodeValueOperator {
return &NodeValueOperator{}
}

View File

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

View File

@@ -5,6 +5,7 @@ const (
LevelInfo = "info"
LevelWarning = "warning"
LevelError = "error"
LevelSuccess = "success"
LogTypeAdmin = "admin"
LogTypeUser = "user"

View File

@@ -0,0 +1,315 @@
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"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
)
type NSAccessLogDAO dbs.DAO
func NewNSAccessLogDAO() *NSAccessLogDAO {
return dbs.NewDAO(&NSAccessLogDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNSAccessLogs",
Model: new(NSAccessLog),
PkName: "id",
},
}).(*NSAccessLogDAO)
}
var SharedNSAccessLogDAO *NSAccessLogDAO
func init() {
dbs.OnReady(func() {
SharedNSAccessLogDAO = NewNSAccessLogDAO()
})
}
// CreateNSAccessLogs 创建访问日志
func (this *NSAccessLogDAO) CreateNSAccessLogs(tx *dbs.Tx, accessLogs []*pb.NSAccessLog) error {
dao := randomNSAccessLogDAO()
if dao == nil {
dao = &NSAccessLogDAOWrapper{
DAO: SharedNSAccessLogDAO,
NodeId: 0,
}
}
return this.CreateNSAccessLogsWithDAO(tx, dao, accessLogs)
}
// CreateNSAccessLogsWithDAO 使用特定的DAO创建访问日志
func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NSAccessLogDAOWrapper, accessLogs []*pb.NSAccessLog) error {
if daoWrapper == nil {
return errors.New("dao should not be nil")
}
if len(accessLogs) == 0 {
return nil
}
dao := daoWrapper.DAO
// TODO 改成事务批量提交,以加快速度
for _, accessLog := range accessLogs {
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
table, err := findNSAccessLogTable(dao.Instance, day, false)
if err != nil {
return err
}
fields := map[string]interface{}{}
fields["nodeId"] = accessLog.NsNodeId
fields["domainId"] = accessLog.NsDomainId
fields["recordId"] = accessLog.NsRecordId
fields["createdAt"] = accessLog.Timestamp
fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId
content, err := json.Marshal(accessLog)
if err != nil {
return err
}
fields["content"] = content
_, err = dao.Query(tx).
Table(table).
Sets(fields).
Insert()
if err != nil {
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
if strings.Contains(err.Error(), "1146") {
table, err = findNSAccessLogTable(dao.Instance, day, true)
if err != nil {
return err
}
_, err = dao.Query(tx).
Table(table).
Sets(fields).
Insert()
if err != nil {
return err
}
}
}
}
return nil
}
// ListAccessLogs 读取往前的 单页访问日志
func (this *NSAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, nodeId int64, domainId int64, recordId int64, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, hasMore bool, err error) {
if len(day) != 8 {
return
}
// 限制能查询的最大条数,防止占用内存过多
if size > 1000 {
size = 1000
}
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, nodeId, domainId, recordId, keyword, reverse)
if err != nil || int64(len(result)) < size {
return
}
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, nodeId, domainId, recordId, keyword, reverse)
hasMore = len(moreResult) > 0
return
}
// 读取往前的单页访问日志
func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, nodeId int64, domainId int64, recordId int64, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, err error) {
if size <= 0 {
return nil, lastRequestId, nil
}
accessLogLocker.RLock()
daoList := []*NSAccessLogDAOWrapper{}
for _, daoWrapper := range nsAccessLogDAOMapping {
daoList = append(daoList, daoWrapper)
}
accessLogLocker.RUnlock()
if len(daoList) == 0 {
daoList = []*NSAccessLogDAOWrapper{{
DAO: SharedNSAccessLogDAO,
NodeId: 0,
}}
}
locker := sync.Mutex{}
count := len(daoList)
wg := &sync.WaitGroup{}
wg.Add(count)
for _, daoWrapper := range daoList {
go func(daoWrapper *NSAccessLogDAOWrapper) {
defer wg.Done()
dao := daoWrapper.DAO
tableName, exists, err := findNSAccessLogTableName(dao.Instance, day)
if !exists {
// 表格不存在则跳过
return
}
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
query := dao.Query(tx)
// 条件
if nodeId > 0 {
query.Attr("nodeId", nodeId)
}
if domainId > 0 {
query.Attr("domainId", domainId)
}
if recordId > 0 {
query.Attr("recordId", recordId)
}
// offset
if len(lastRequestId) > 0 {
if !reverse {
query.Where("requestId<:requestId").
Param("requestId", lastRequestId)
} else {
query.Where("requestId>:requestId").
Param("requestId", lastRequestId)
}
}
// keyword
if len(keyword) > 0 {
query.Where("(JSON_EXTRACT(content, '$.remoteAddr') LIKE :keyword OR JSON_EXTRACT(content, '$.questionName') LIKE :keyword OR JSON_EXTRACT(content, '$.recordValue') LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if !reverse {
query.Desc("requestId")
} else {
query.Asc("requestId")
}
// 开始查询
ones, err := query.
Table(tableName).
Limit(size).
FindAll()
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
locker.Lock()
for _, one := range ones {
accessLog := one.(*NSAccessLog)
result = append(result, accessLog)
}
locker.Unlock()
}(daoWrapper)
}
wg.Wait()
if len(result) == 0 {
return nil, lastRequestId, nil
}
// 按照requestId排序
sort.Slice(result, func(i, j int) bool {
if !reverse {
return result[i].RequestId > result[j].RequestId
} else {
return result[i].RequestId < result[j].RequestId
}
})
if int64(len(result)) > size {
result = result[:size]
}
requestId := result[len(result)-1].RequestId
if reverse {
lists.Reverse(result)
}
if !reverse {
return result, requestId, nil
} else {
return result, requestId, nil
}
}
// FindAccessLogWithRequestId 根据请求ID获取访问日志
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
return nil, errors.New("invalid requestId")
}
accessLogLocker.RLock()
daoList := []*NSAccessLogDAOWrapper{}
for _, daoWrapper := range nsAccessLogDAOMapping {
daoList = append(daoList, daoWrapper)
}
accessLogLocker.RUnlock()
if len(daoList) == 0 {
daoList = []*NSAccessLogDAOWrapper{{
DAO: SharedNSAccessLogDAO,
NodeId: 0,
}}
}
count := len(daoList)
wg := &sync.WaitGroup{}
wg.Add(count)
var result *NSAccessLog = nil
day := timeutil.FormatTime("Ymd", types.Int64(requestId[:10]))
for _, daoWrapper := range daoList {
go func(daoWrapper *NSAccessLogDAOWrapper) {
defer wg.Done()
dao := daoWrapper.DAO
tableName, exists, err := findNSAccessLogTableName(dao.Instance, day)
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
if !exists {
return
}
one, err := dao.Query(tx).
Table(tableName).
Attr("requestId", requestId).
Find()
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
if one != nil {
result = one.(*NSAccessLog)
}
}(daoWrapper)
}
wg.Wait()
return result, 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,26 @@
package models
// NSAccessLog 域名服务访问日志
type NSAccessLog struct {
Id uint64 `field:"id"` // ID
NodeId uint32 `field:"nodeId"` // 节点ID
DomainId uint32 `field:"domainId"` // 域名ID
RecordId uint32 `field:"recordId"` // 记录ID
Content string `field:"content"` // 访问数据
RequestId string `field:"requestId"` // 请求ID
CreatedAt uint64 `field:"createdAt"` // 创建时间
}
type NSAccessLogOperator struct {
Id interface{} // ID
NodeId interface{} // 节点ID
DomainId interface{} // 域名ID
RecordId interface{} // 记录ID
Content interface{} // 访问数据
RequestId interface{} // 请求ID
CreatedAt interface{} // 创建时间
}
func NewNSAccessLogOperator() *NSAccessLogOperator {
return &NSAccessLogOperator{}
}

View File

@@ -0,0 +1,17 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// ToPB 转换成PB对象
func (this *NSAccessLog) ToPB() (*pb.NSAccessLog, error) {
p := &pb.NSAccessLog{}
err := json.Unmarshal([]byte(this.Content), p)
if err != nil {
return nil, err
}
p.RequestId = this.RequestId
return p, nil
}

View File

@@ -38,12 +38,12 @@ func init() {
})
}
// 初始化
// Init 初始化
func (this *ReverseProxyDAO) Init() {
_ = this.DAOObject.Init()
}
// 启用条目
// EnableReverseProxy 启用条目
func (this *ReverseProxyDAO) EnableReverseProxy(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -55,7 +55,7 @@ func (this *ReverseProxyDAO) EnableReverseProxy(tx *dbs.Tx, id int64) error {
return this.NotifyUpdate(tx, id)
}
// 禁用条目
// DisableReverseProxy 禁用条目
func (this *ReverseProxyDAO) DisableReverseProxy(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -67,7 +67,7 @@ func (this *ReverseProxyDAO) DisableReverseProxy(tx *dbs.Tx, id int64) error {
return this.NotifyUpdate(tx, id)
}
// 查找启用中的条目
// FindEnabledReverseProxy 查找启用中的条目
func (this *ReverseProxyDAO) FindEnabledReverseProxy(tx *dbs.Tx, id int64) (*ReverseProxy, error) {
result, err := this.Query(tx).
Pk(id).
@@ -79,7 +79,7 @@ func (this *ReverseProxyDAO) FindEnabledReverseProxy(tx *dbs.Tx, id int64) (*Rev
return result.(*ReverseProxy), err
}
// 根据iD组合配置
// ComposeReverseProxyConfig 根据ID组合配置
func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyId int64) (*serverconfigs.ReverseProxyConfig, error) {
reverseProxy, err := this.FindEnabledReverseProxy(tx, reverseProxyId)
if err != nil {
@@ -184,13 +184,14 @@ func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyI
return config, nil
}
// 创建反向代理
// CreateReverseProxy 创建反向代理
func (this *ReverseProxyDAO) CreateReverseProxy(tx *dbs.Tx, adminId int64, userId int64, schedulingJSON []byte, primaryOriginsJSON []byte, backupOriginsJSON []byte) (int64, error) {
op := NewReverseProxyOperator()
op.IsOn = true
op.State = ReverseProxyStateEnabled
op.AdminId = adminId
op.UserId = userId
op.RequestHostType = serverconfigs.RequestHostTypeProxyServer
defaultHeaders := []string{"X-Real-IP", "X-Forwarded-For", "X-Forwarded-By", "X-Forwarded-Host", "X-Forwarded-Proto"}
defaultHeadersJSON, err := json.Marshal(defaultHeaders)
@@ -216,7 +217,7 @@ func (this *ReverseProxyDAO) CreateReverseProxy(tx *dbs.Tx, adminId int64, userI
return types.Int64(op.Id), nil
}
// 修改反向代理调度算法
// UpdateReverseProxyScheduling 修改反向代理调度算法
func (this *ReverseProxyDAO) UpdateReverseProxyScheduling(tx *dbs.Tx, reverseProxyId int64, schedulingJSON []byte) error {
if reverseProxyId <= 0 {
return errors.New("invalid reverseProxyId")
@@ -235,7 +236,7 @@ func (this *ReverseProxyDAO) UpdateReverseProxyScheduling(tx *dbs.Tx, reversePro
return this.NotifyUpdate(tx, reverseProxyId)
}
// 修改主要源站
// UpdateReverseProxyPrimaryOrigins 修改主要源站
func (this *ReverseProxyDAO) UpdateReverseProxyPrimaryOrigins(tx *dbs.Tx, reverseProxyId int64, origins []byte) error {
if reverseProxyId <= 0 {
return errors.New("invalid reverseProxyId")
@@ -254,7 +255,7 @@ func (this *ReverseProxyDAO) UpdateReverseProxyPrimaryOrigins(tx *dbs.Tx, revers
return this.NotifyUpdate(tx, reverseProxyId)
}
// 修改备用源站
// UpdateReverseProxyBackupOrigins 修改备用源站
func (this *ReverseProxyDAO) UpdateReverseProxyBackupOrigins(tx *dbs.Tx, reverseProxyId int64, origins []byte) error {
if reverseProxyId <= 0 {
return errors.New("invalid reverseProxyId")
@@ -273,7 +274,7 @@ func (this *ReverseProxyDAO) UpdateReverseProxyBackupOrigins(tx *dbs.Tx, reverse
return this.NotifyUpdate(tx, reverseProxyId)
}
// 修改是否启用
// 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 {
if reverseProxyId <= 0 {
return errors.New("invalid reverseProxyId")
@@ -340,7 +341,7 @@ func (this *ReverseProxyDAO) UpdateReverseProxy(tx *dbs.Tx, reverseProxyId int64
return this.NotifyUpdate(tx, reverseProxyId)
}
// 查找包含某个源站的反向代理ID
// FindReverseProxyContainsOriginId 查找包含某个源站的反向代理ID
func (this *ReverseProxyDAO) FindReverseProxyContainsOriginId(tx *dbs.Tx, originId int64) (int64, error) {
return this.Query(tx).
ResultPk().
@@ -351,7 +352,31 @@ func (this *ReverseProxyDAO) FindReverseProxyContainsOriginId(tx *dbs.Tx, origin
FindInt64Col(0)
}
// 通知更新
// CheckUserReverseProxy 检查用户权限
func (this *ReverseProxyDAO) CheckUserReverseProxy(tx *dbs.Tx, userId int64, reverseProxyId int64) error {
exists, err := this.Query(tx).
Pk(reverseProxyId).
Attr("userId", userId).
Exist()
if err != nil {
return err
}
if exists {
return nil
}
// 检查server是否为用户的
serverId, err := SharedServerDAO.FindEnabledServerIdWithReverseProxyId(tx, reverseProxyId)
if err != nil {
return err
}
if serverId == 0 {
return ErrNotFound
}
return SharedServerDAO.CheckUserServer(tx, userId, serverId)
}
// NotifyUpdate 通知更新
func (this *ReverseProxyDAO) NotifyUpdate(tx *dbs.Tx, reverseProxyId int64) error {
serverId, err := SharedServerDAO.FindEnabledServerIdWithReverseProxyId(tx, reverseProxyId)
if err != nil {

View File

@@ -7,6 +7,7 @@ import (
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
)
type ServerDailyStatDAO dbs.DAO
@@ -30,24 +31,33 @@ func init() {
})
}
// 提交数据
// SaveStats 提交数据
func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailyStat) error {
for _, stat := range stats {
day := timeutil.FormatTime("Ymd", stat.CreatedAt)
timeFrom := timeutil.FormatTime("His", stat.CreatedAt)
timeTo := timeutil.FormatTime("His", stat.CreatedAt+5*60) // 5分钟
timeTo := timeutil.FormatTime("His", stat.CreatedAt+5*60-1) // 5分钟
_, _, err := this.Query(tx).
Param("bytes", stat.Bytes).
Param("cachedBytes", stat.CachedBytes).
Param("countRequests", stat.CountRequests).
Param("countCachedRequests", stat.CountCachedRequests).
InsertOrUpdate(maps.Map{
"serverId": stat.ServerId,
"regionId": stat.RegionId,
"bytes": dbs.SQL("bytes+:bytes"),
"day": day,
"timeFrom": timeFrom,
"timeTo": timeTo,
"serverId": stat.ServerId,
"regionId": stat.RegionId,
"bytes": dbs.SQL("bytes+:bytes"),
"cachedBytes": dbs.SQL("cachedBytes+:cachedBytes"),
"countRequests": dbs.SQL("countRequests+:countRequests"),
"countCachedRequests": dbs.SQL("countCachedRequests+:countCachedRequests"),
"day": day,
"timeFrom": timeFrom,
"timeTo": timeTo,
}, maps.Map{
"bytes": dbs.SQL("bytes+:bytes"),
"bytes": dbs.SQL("bytes+:bytes"),
"cachedBytes": dbs.SQL("cachedBytes+:cachedBytes"),
"countRequests": dbs.SQL("countRequests+:countRequests"),
"countCachedRequests": dbs.SQL("countCachedRequests+:countCachedRequests"),
})
if err != nil {
return err
@@ -56,7 +66,7 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
return nil
}
// 根据用户计算某月合计
// SumUserMonthly 根据用户计算某月合计
// month 格式为YYYYMM
func (this *ServerDailyStatDAO) SumUserMonthly(tx *dbs.Tx, userId int64, regionId int64, month string) (int64, error) {
query := this.Query(tx)
@@ -69,7 +79,7 @@ func (this *ServerDailyStatDAO) SumUserMonthly(tx *dbs.Tx, userId int64, regionI
SumInt64("bytes", 0)
}
// 获取某月带宽峰值
// SumUserMonthlyPeek 获取某月带宽峰值
// month 格式为YYYYMM
func (this *ServerDailyStatDAO) SumUserMonthlyPeek(tx *dbs.Tx, userId int64, regionId int64, month string) (int64, error) {
query := this.Query(tx)
@@ -86,7 +96,7 @@ func (this *ServerDailyStatDAO) SumUserMonthlyPeek(tx *dbs.Tx, userId int64, reg
return int64(max), nil
}
// 获取某天流量总和
// SumUserDaily 获取某天流量总和
// day 格式为YYYYMMDD
func (this *ServerDailyStatDAO) SumUserDaily(tx *dbs.Tx, userId int64, regionId int64, day string) (int64, error) {
query := this.Query(tx)
@@ -100,7 +110,7 @@ func (this *ServerDailyStatDAO) SumUserDaily(tx *dbs.Tx, userId int64, regionId
SumInt64("bytes", 0)
}
// 获取某天带宽峰值
// SumUserDailyPeek 获取某天带宽峰值
// day 格式为YYYYMMDD
func (this *ServerDailyStatDAO) SumUserDailyPeek(tx *dbs.Tx, userId int64, regionId int64, day string) (int64, error) {
query := this.Query(tx)
@@ -117,3 +127,93 @@ func (this *ServerDailyStatDAO) SumUserDailyPeek(tx *dbs.Tx, userId int64, regio
}
return int64(max), nil
}
// SumMinutelyStat 获取某个分钟内的流量
// minute 格式为YYYYMMDDHHMM并且已经格式化成每5分钟一个值
func (this *ServerDailyStatDAO) SumMinutelyStat(tx *dbs.Tx, serverId int64, minute string) (stat *pb.ServerDailyStat, err error) {
stat = &pb.ServerDailyStat{}
if !regexp.MustCompile(`^\d{12}$`).MatchString(minute) {
return
}
one, _, err := this.Query(tx).
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests").
Attr("serverId", serverId).
Attr("day", minute[:8]).
Attr("timeFrom", minute[8:]+"00").
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")
return
}
// SumHourlyStat 获取某个小时内的流量
// hour 格式为YYYYMMDDHH
func (this *ServerDailyStatDAO) SumHourlyStat(tx *dbs.Tx, serverId int64, hour string) (stat *pb.ServerDailyStat, err error) {
stat = &pb.ServerDailyStat{}
if !regexp.MustCompile(`^\d{10}$`).MatchString(hour) {
return
}
one, _, err := this.Query(tx).
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests").
Attr("serverId", serverId).
Attr("day", hour[:8]).
Gte("timeFrom", hour[8:]+"0000").
Lte("timeTo", hour[8:]+"5959").
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")
return
}
// SumDailyStat 获取某天内的流量
// day 格式为YYYYMMDD
func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day string) (stat *pb.ServerDailyStat, err error) {
stat = &pb.ServerDailyStat{}
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
return
}
one, _, err := this.Query(tx).
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests").
Attr("serverId", serverId).
Attr("day", day).
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")
return
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
timeutil "github.com/iwind/TeaGo/utils/time"
"testing"
)
@@ -51,3 +52,25 @@ func TestServerDailyStatDAO_SumUserMonthly(t *testing.T) {
}
t.Log("bytes:", bytes)
}
func TestServerDailyStatDAO_SumHourlyRequests(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
stat, err := NewServerDailyStatDAO().SumHourlyStat(tx, 23, timeutil.Format("YmdH"))
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(stat, t)
}
func TestServerDailyStatDAO_SumMinutelyRequests(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
stat, err := NewServerDailyStatDAO().SumMinutelyStat(tx, 23, timeutil.Format("Ymd") + "1435")
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(stat, t)
}

View File

@@ -1,26 +1,32 @@
package models
// 计费流量统计
// ServerDailyStat 计费流量统计
type ServerDailyStat struct {
Id uint64 `field:"id"` // ID
ServerId uint32 `field:"serverId"` // 服务ID
RegionId uint32 `field:"regionId"` // 区域ID
Bytes uint64 `field:"bytes"` // 流量
Day string `field:"day"` // 日期YYYYMMDD
TimeFrom string `field:"timeFrom"` // 开始时间HHMMSS
TimeTo string `field:"timeTo"` // 结束时间
IsCharged uint8 `field:"isCharged"` // 是否已计算费用
Id uint64 `field:"id"` // 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"` // 缓存的请求数
Day string `field:"day"` // 日期YYYYMMDD
TimeFrom string `field:"timeFrom"` // 开始时间HHMMSS
TimeTo string `field:"timeTo"` // 结束时间
IsCharged uint8 `field:"isCharged"` // 是否已计算费用
}
type ServerDailyStatOperator struct {
Id interface{} // ID
ServerId interface{} // 服务ID
RegionId interface{} // 区域ID
Bytes interface{} // 流量
Day interface{} // 日期YYYYMMDD
TimeFrom interface{} // 开始时间HHMMSS
TimeTo interface{} // 结束时间
IsCharged interface{} // 是否已计算费用
Id interface{} // ID
ServerId interface{} // 服务ID
RegionId interface{} // 区域ID
Bytes interface{} // 流量
CachedBytes interface{} // 缓存的流量
CountRequests interface{} // 请求数
CountCachedRequests interface{} // 缓存的请求数
Day interface{} // 日期YYYYMMDD
TimeFrom interface{} // 开始时间HHMMSS
TimeTo interface{} // 结束时间
IsCharged interface{} // 是否已计算费用
}
func NewServerDailyStatOperator() *ServerDailyStatOperator {

View File

@@ -49,14 +49,14 @@ func init() {
})
}
// 初始化
// Init 初始化
func (this *ServerDAO) Init() {
this.DAOObject.Init()
// 这里不处理增删改事件是为了避免Server修改本身的时候也要触发别的Server变更
}
// 启用条目
// EnableServer 启用条目
func (this *ServerDAO) EnableServer(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -64,7 +64,7 @@ func (this *ServerDAO) EnableServer(tx *dbs.Tx, id uint32) (rowsAffected int64,
Update()
}
// 禁用条目
// DisableServer 禁用条目
func (this *ServerDAO) DisableServer(tx *dbs.Tx, serverId int64) (err error) {
_, err = this.Query(tx).
Pk(serverId).
@@ -85,7 +85,7 @@ func (this *ServerDAO) DisableServer(tx *dbs.Tx, serverId int64) (err error) {
return nil
}
// 查找启用中的服务
// FindEnabledServer 查找启用中的服务
func (this *ServerDAO) FindEnabledServer(tx *dbs.Tx, serverId int64) (*Server, error) {
result, err := this.Query(tx).
Pk(serverId).
@@ -97,7 +97,7 @@ func (this *ServerDAO) FindEnabledServer(tx *dbs.Tx, serverId int64) (*Server, e
return result.(*Server), err
}
// 查找服务名称
// FindEnabledServerName 查找服务名称
func (this *ServerDAO) FindEnabledServerName(tx *dbs.Tx, serverId int64) (string, error) {
return this.Query(tx).
Pk(serverId).
@@ -106,7 +106,7 @@ func (this *ServerDAO) FindEnabledServerName(tx *dbs.Tx, serverId int64) (string
FindStringCol("")
}
// 查找服务基本信息
// FindEnabledServerBasic 查找服务基本信息
func (this *ServerDAO) FindEnabledServerBasic(tx *dbs.Tx, serverId int64) (*Server, error) {
result, err := this.Query(tx).
Pk(serverId).
@@ -119,7 +119,7 @@ func (this *ServerDAO) FindEnabledServerBasic(tx *dbs.Tx, serverId int64) (*Serv
return result.(*Server), err
}
// 查找服务类型
// FindEnabledServerType 查找服务类型
func (this *ServerDAO) FindEnabledServerType(tx *dbs.Tx, serverId int64) (string, error) {
return this.Query(tx).
Pk(serverId).
@@ -127,7 +127,7 @@ func (this *ServerDAO) FindEnabledServerType(tx *dbs.Tx, serverId int64) (string
FindStringCol("")
}
// 创建服务
// CreateServer 创建服务
func (this *ServerDAO) CreateServer(tx *dbs.Tx,
adminId int64,
userId int64,
@@ -236,7 +236,7 @@ func (this *ServerDAO) CreateServer(tx *dbs.Tx,
return serverId, nil
}
// 修改服务基本信息
// UpdateServerBasic 修改服务基本信息
func (this *ServerDAO) UpdateServerBasic(tx *dbs.Tx, serverId int64, name string, description string, clusterId int64, isOn bool, groupIds []int64) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -273,7 +273,7 @@ func (this *ServerDAO) UpdateServerBasic(tx *dbs.Tx, serverId int64, name string
return this.NotifyDNSUpdate(tx, serverId)
}
// 设置用户相关的基本信息
// UpdateUserServerBasic 设置用户相关的基本信息
func (this *ServerDAO) UpdateUserServerBasic(tx *dbs.Tx, serverId int64, name string) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -290,7 +290,7 @@ func (this *ServerDAO) UpdateUserServerBasic(tx *dbs.Tx, serverId int64, name st
return this.NotifyUpdate(tx, serverId)
}
// 修复服务是否启用
// UpdateServerIsOn 修复服务是否启用
func (this *ServerDAO) UpdateServerIsOn(tx *dbs.Tx, serverId int64, isOn bool) error {
_, err := this.Query(tx).
Pk(serverId).
@@ -308,7 +308,7 @@ func (this *ServerDAO) UpdateServerIsOn(tx *dbs.Tx, serverId int64, isOn bool) e
return nil
}
// 修改服务配置
// UpdateServerConfig 修改服务配置
func (this *ServerDAO) UpdateServerConfig(tx *dbs.Tx, serverId int64, configJSON []byte, updateMd5 bool) (isChanged bool, err error) {
if serverId <= 0 {
return false, errors.New("serverId should not be smaller than 0")
@@ -351,7 +351,7 @@ func (this *ServerDAO) UpdateServerConfig(tx *dbs.Tx, serverId int64, configJSON
return true, err
}
// 修改HTTP配置
// UpdateServerHTTP 修改HTTP配置
func (this *ServerDAO) UpdateServerHTTP(tx *dbs.Tx, serverId int64, config []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -370,7 +370,7 @@ func (this *ServerDAO) UpdateServerHTTP(tx *dbs.Tx, serverId int64, config []byt
return this.NotifyUpdate(tx, serverId)
}
// 修改HTTPS配置
// UpdateServerHTTPS 修改HTTPS配置
func (this *ServerDAO) UpdateServerHTTPS(tx *dbs.Tx, serverId int64, httpsJSON []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -389,7 +389,7 @@ func (this *ServerDAO) UpdateServerHTTPS(tx *dbs.Tx, serverId int64, httpsJSON [
return this.NotifyUpdate(tx, serverId)
}
// 修改TCP配置
// UpdateServerTCP 修改TCP配置
func (this *ServerDAO) UpdateServerTCP(tx *dbs.Tx, serverId int64, config []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -408,7 +408,7 @@ func (this *ServerDAO) UpdateServerTCP(tx *dbs.Tx, serverId int64, config []byte
return this.NotifyUpdate(tx, serverId)
}
// 修改TLS配置
// UpdateServerTLS 修改TLS配置
func (this *ServerDAO) UpdateServerTLS(tx *dbs.Tx, serverId int64, config []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -427,7 +427,7 @@ func (this *ServerDAO) UpdateServerTLS(tx *dbs.Tx, serverId int64, config []byte
return this.NotifyUpdate(tx, serverId)
}
// 修改Unix配置
// UpdateServerUnix 修改Unix配置
func (this *ServerDAO) UpdateServerUnix(tx *dbs.Tx, serverId int64, config []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -446,7 +446,7 @@ func (this *ServerDAO) UpdateServerUnix(tx *dbs.Tx, serverId int64, config []byt
return this.NotifyUpdate(tx, serverId)
}
// 修改UDP配置
// UpdateServerUDP 修改UDP配置
func (this *ServerDAO) UpdateServerUDP(tx *dbs.Tx, serverId int64, config []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -465,7 +465,7 @@ func (this *ServerDAO) UpdateServerUDP(tx *dbs.Tx, serverId int64, config []byte
return this.NotifyUpdate(tx, serverId)
}
// 修改Web配置
// UpdateServerWeb 修改Web配置
func (this *ServerDAO) UpdateServerWeb(tx *dbs.Tx, serverId int64, webId int64) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -480,7 +480,7 @@ func (this *ServerDAO) UpdateServerWeb(tx *dbs.Tx, serverId int64, webId int64)
return this.NotifyUpdate(tx, serverId)
}
// 初始化Web配置
// 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")
@@ -512,7 +512,7 @@ func (this *ServerDAO) InitServerWeb(tx *dbs.Tx, serverId int64) (int64, error)
return webId, nil
}
// 查找ServerNames配置
// FindServerServerNames 查找ServerNames配置
func (this *ServerDAO) FindServerServerNames(tx *dbs.Tx, serverId int64) (serverNamesJSON []byte, isAuditing bool, auditingServerNamesJSON []byte, auditingResultJSON []byte, err error) {
if serverId <= 0 {
return
@@ -531,7 +531,7 @@ func (this *ServerDAO) FindServerServerNames(tx *dbs.Tx, serverId int64) (server
return []byte(server.ServerNames), server.IsAuditing == 1, []byte(server.AuditingServerNames), []byte(server.AuditingResult), nil
}
// 修改ServerNames配置
// UpdateServerNames 修改ServerNames配置
func (this *ServerDAO) UpdateServerNames(tx *dbs.Tx, serverId int64, serverNames []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -551,7 +551,7 @@ func (this *ServerDAO) UpdateServerNames(tx *dbs.Tx, serverId int64, serverNames
return this.NotifyUpdate(tx, serverId)
}
// 修改域名审核
// UpdateAuditingServerNames 修改域名审核
func (this *ServerDAO) UpdateAuditingServerNames(tx *dbs.Tx, serverId int64, isAuditing bool, auditingServerNamesJSON []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -573,7 +573,7 @@ func (this *ServerDAO) UpdateAuditingServerNames(tx *dbs.Tx, serverId int64, isA
return this.NotifyUpdate(tx, serverId)
}
// 修改域名审核结果
// UpdateServerAuditing 修改域名审核结果
func (this *ServerDAO) UpdateServerAuditing(tx *dbs.Tx, serverId int64, result *pb.ServerNameAuditingResult) error {
if serverId <= 0 {
return errors.New("invalid serverId")
@@ -608,7 +608,7 @@ func (this *ServerDAO) UpdateServerAuditing(tx *dbs.Tx, serverId int64, result *
return this.NotifyDNSUpdate(tx, serverId)
}
// 修改反向代理配置
// UpdateServerReverseProxy 修改反向代理配置
func (this *ServerDAO) UpdateServerReverseProxy(tx *dbs.Tx, serverId int64, config []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
@@ -624,14 +624,14 @@ func (this *ServerDAO) UpdateServerReverseProxy(tx *dbs.Tx, serverId int64, conf
return this.NotifyUpdate(tx, serverId)
}
// 计算所有可用服务数量
// CountAllEnabledServers 计算所有可用服务数量
func (this *ServerDAO) CountAllEnabledServers(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(ServerStateEnabled).
Count()
}
// 计算所有可用服务数量
// CountAllEnabledServersMatch 计算所有可用服务数量
func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag configutils.BoolState, protocolFamily string) (int64, error) {
query := this.Query(tx).
State(ServerStateEnabled)
@@ -660,7 +660,7 @@ func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, ke
return query.Count()
}
// 列出单页的服务
// ListEnabledServersMatch 列出单页的服务
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamily string) (result []*Server, err error) {
query := this.Query(tx).
State(ServerStateEnabled).
@@ -696,7 +696,7 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
return
}
// 获取节点中的所有服务
// FindAllEnabledServersWithNode 获取节点中的所有服务
func (this *ServerDAO) FindAllEnabledServersWithNode(tx *dbs.Tx, nodeId int64) (result []*Server, err error) {
// 节点所在集群
clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
@@ -716,7 +716,7 @@ func (this *ServerDAO) FindAllEnabledServersWithNode(tx *dbs.Tx, nodeId int64) (
return
}
// 获取所有的服务ID
// FindAllEnabledServerIds 获取所有的服务ID
func (this *ServerDAO) FindAllEnabledServerIds(tx *dbs.Tx) (serverIds []int64, err error) {
ones, err := this.Query(tx).
State(ServerStateEnabled).
@@ -729,7 +729,7 @@ func (this *ServerDAO) FindAllEnabledServerIds(tx *dbs.Tx) (serverIds []int64, e
return
}
// 获取某个用户的所有的服务ID
// FindAllEnabledServerIdsWithUserId 获取某个用户的所有的服务ID
func (this *ServerDAO) FindAllEnabledServerIdsWithUserId(tx *dbs.Tx, userId int64) (serverIds []int64, err error) {
ones, err := this.Query(tx).
State(ServerStateEnabled).
@@ -743,7 +743,7 @@ func (this *ServerDAO) FindAllEnabledServerIdsWithUserId(tx *dbs.Tx, userId int6
return
}
// 查找服务的搜索条件
// FindServerNodeFilters 查找服务的搜索条件
func (this *ServerDAO) FindServerNodeFilters(tx *dbs.Tx, serverId int64) (isOk bool, clusterId int64, err error) {
one, err := this.Query(tx).
Pk(serverId).
@@ -760,7 +760,7 @@ func (this *ServerDAO) FindServerNodeFilters(tx *dbs.Tx, serverId int64) (isOk b
return true, int64(server.ClusterId), nil
}
// 构造服务的Config
// ComposeServerConfig 构造服务的Config
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverconfigs.ServerConfig, error) {
server, err := this.FindEnabledServer(tx, serverId)
if err != nil {
@@ -921,7 +921,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
return config, nil
}
// 更新服务的Config配置
// RenewServerConfig 更新服务的Config配置
func (this *ServerDAO) RenewServerConfig(tx *dbs.Tx, serverId int64, updateMd5 bool) (isChanged bool, err error) {
serverConfig, err := this.ComposeServerConfig(tx, serverId)
if err != nil {
@@ -934,7 +934,7 @@ func (this *ServerDAO) RenewServerConfig(tx *dbs.Tx, serverId int64, updateMd5 b
return this.UpdateServerConfig(tx, serverId, data, updateMd5)
}
// 根据条件获取反向代理配置
// FindReverseProxyRef 根据条件获取反向代理配置
func (this *ServerDAO) FindReverseProxyRef(tx *dbs.Tx, serverId int64) (*serverconfigs.ReverseProxyRef, error) {
reverseProxy, err := this.Query(tx).
Pk(serverId).
@@ -951,7 +951,7 @@ func (this *ServerDAO) FindReverseProxyRef(tx *dbs.Tx, serverId int64) (*serverc
return config, err
}
// 查找Server对应的WebId
// FindServerWebId 查找Server对应的WebId
func (this *ServerDAO) FindServerWebId(tx *dbs.Tx, serverId int64) (int64, error) {
webId, err := this.Query(tx).
Pk(serverId).
@@ -963,7 +963,7 @@ func (this *ServerDAO) FindServerWebId(tx *dbs.Tx, serverId int64) (int64, error
return int64(webId), nil
}
// 计算使用SSL策略的所有服务数量
// CountAllEnabledServersWithSSLPolicyIds 计算使用SSL策略的所有服务数量
func (this *ServerDAO) CountAllEnabledServersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
if len(sslPolicyIds) == 0 {
return
@@ -979,7 +979,7 @@ func (this *ServerDAO) CountAllEnabledServersWithSSLPolicyIds(tx *dbs.Tx, sslPol
Count()
}
// 查找使用某个SSL策略的所有服务
// FindAllEnabledServersWithSSLPolicyIds 查找使用某个SSL策略的所有服务
func (this *ServerDAO) FindAllEnabledServersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (result []*Server, err error) {
if len(sslPolicyIds) == 0 {
return
@@ -999,7 +999,7 @@ func (this *ServerDAO) FindAllEnabledServersWithSSLPolicyIds(tx *dbs.Tx, sslPoli
return
}
// 查找使用某个SSL策略的所有服务Id
// FindAllEnabledServerIdsWithSSLPolicyIds 查找使用某个SSL策略的所有服务Id
func (this *ServerDAO) FindAllEnabledServerIdsWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (result []int64, err error) {
if len(sslPolicyIds) == 0 {
return
@@ -1025,7 +1025,7 @@ func (this *ServerDAO) FindAllEnabledServerIdsWithSSLPolicyIds(tx *dbs.Tx, sslPo
return
}
// 计算使用某个缓存策略的所有服务数量
// CountEnabledServersWithWebIds 计算使用某个缓存策略的所有服务数量
func (this *ServerDAO) CountEnabledServersWithWebIds(tx *dbs.Tx, webIds []int64) (count int64, err error) {
if len(webIds) == 0 {
return
@@ -1037,7 +1037,7 @@ func (this *ServerDAO) CountEnabledServersWithWebIds(tx *dbs.Tx, webIds []int64)
Count()
}
// 查找使用某个缓存策略的所有服务
// FindAllEnabledServersWithWebIds 查找使用某个缓存策略的所有服务
func (this *ServerDAO) FindAllEnabledServersWithWebIds(tx *dbs.Tx, webIds []int64) (result []*Server, err error) {
if len(webIds) == 0 {
return
@@ -1052,7 +1052,7 @@ func (this *ServerDAO) FindAllEnabledServersWithWebIds(tx *dbs.Tx, webIds []int6
return
}
// 计算使用某个集群的所有服务数量
// CountAllEnabledServersWithNodeClusterId 计算使用某个集群的所有服务数量
func (this *ServerDAO) CountAllEnabledServersWithNodeClusterId(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
State(ServerStateEnabled).
@@ -1060,7 +1060,7 @@ func (this *ServerDAO) CountAllEnabledServersWithNodeClusterId(tx *dbs.Tx, clust
Count()
}
// 计算使用某个分组的服务数量
// CountAllEnabledServersWithGroupId 计算使用某个分组的服务数量
func (this *ServerDAO) CountAllEnabledServersWithGroupId(tx *dbs.Tx, groupId int64) (int64, error) {
return this.Query(tx).
State(ServerStateEnabled).
@@ -1069,7 +1069,7 @@ func (this *ServerDAO) CountAllEnabledServersWithGroupId(tx *dbs.Tx, groupId int
Count()
}
// 查询使用某个DNS域名的所有服务域名
// FindAllServerDNSNamesWithDNSDomainId 查询使用某个DNS域名的所有服务域名
func (this *ServerDAO) FindAllServerDNSNamesWithDNSDomainId(tx *dbs.Tx, dnsDomainId int64) ([]string, error) {
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledClusterIdsWithDNSDomainId(tx, dnsDomainId)
if err != nil {
@@ -1099,7 +1099,7 @@ func (this *ServerDAO) FindAllServerDNSNamesWithDNSDomainId(tx *dbs.Tx, dnsDomai
return result, nil
}
// 获取某个集群下的服务DNS信息
// FindAllServersDNSWithClusterId 获取某个集群下的服务DNS信息
func (this *ServerDAO) FindAllServersDNSWithClusterId(tx *dbs.Tx, clusterId int64) (result []*Server, err error) {
_, err = this.Query(tx).
State(ServerStateEnabled).
@@ -1113,7 +1113,7 @@ func (this *ServerDAO) FindAllServersDNSWithClusterId(tx *dbs.Tx, clusterId int6
return
}
// 重新生成子域名
// GenerateServerDNSName 重新生成子域名
func (this *ServerDAO) GenerateServerDNSName(tx *dbs.Tx, serverId int64) (string, error) {
if serverId <= 0 {
return "", errors.New("invalid serverId")
@@ -1143,7 +1143,7 @@ func (this *ServerDAO) GenerateServerDNSName(tx *dbs.Tx, serverId int64) (string
return dnsName, nil
}
// 查询当前服务的集群ID
// FindServerClusterId 查询当前服务的集群ID
func (this *ServerDAO) FindServerClusterId(tx *dbs.Tx, serverId int64) (int64, error) {
return this.Query(tx).
Pk(serverId).
@@ -1151,7 +1151,7 @@ func (this *ServerDAO) FindServerClusterId(tx *dbs.Tx, serverId int64) (int64, e
FindInt64Col(0)
}
// 查询服务的DNS名称
// FindServerDNSName 查询服务的DNS名称
func (this *ServerDAO) FindServerDNSName(tx *dbs.Tx, serverId int64) (string, error) {
return this.Query(tx).
Pk(serverId).
@@ -1159,7 +1159,7 @@ func (this *ServerDAO) FindServerDNSName(tx *dbs.Tx, serverId int64) (string, er
FindStringCol("")
}
// 查询服务的DNS相关信息并且不关注状态
// FindStatelessServerDNS 查询服务的DNS相关信息并且不关注状态
func (this *ServerDAO) FindStatelessServerDNS(tx *dbs.Tx, serverId int64) (*Server, error) {
one, err := this.Query(tx).
Pk(serverId).
@@ -1171,7 +1171,7 @@ func (this *ServerDAO) FindStatelessServerDNS(tx *dbs.Tx, serverId int64) (*Serv
return one.(*Server), nil
}
// 获取当前服务的管理员ID和用户ID
// FindServerAdminIdAndUserId 获取当前服务的管理员ID和用户ID
func (this *ServerDAO) FindServerAdminIdAndUserId(tx *dbs.Tx, serverId int64) (adminId int64, userId int64, err error) {
one, err := this.Query(tx).
Pk(serverId).
@@ -1186,7 +1186,7 @@ func (this *ServerDAO) FindServerAdminIdAndUserId(tx *dbs.Tx, serverId int64) (a
return int64(one.(*Server).AdminId), int64(one.(*Server).UserId), nil
}
// 检查用户服务
// CheckUserServer 检查用户服务
func (this *ServerDAO) CheckUserServer(tx *dbs.Tx, userId int64, serverId int64) error {
if serverId <= 0 || userId <= 0 {
return ErrNotFound
@@ -1204,7 +1204,7 @@ func (this *ServerDAO) CheckUserServer(tx *dbs.Tx, userId int64, serverId int64)
return nil
}
// 设置一个用户下的所有服务的所属集群
// UpdateUserServersClusterId 设置一个用户下的所有服务的所属集群
func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, clusterId int64) error {
// 之前的cluster
oldClusterId, err := SharedUserDAO.FindUserClusterId(tx, userId)
@@ -1248,7 +1248,7 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, clus
return err
}
// 查找用户的所有的服务
// FindAllEnabledServersWithUserId 查找用户的所有的服务
func (this *ServerDAO) FindAllEnabledServersWithUserId(tx *dbs.Tx, userId int64) (result []*Server, err error) {
_, err = this.Query(tx).
State(ServerStateEnabled).
@@ -1259,7 +1259,7 @@ func (this *ServerDAO) FindAllEnabledServersWithUserId(tx *dbs.Tx, userId int64)
return
}
// 根据WebId查找ServerId
// FindEnabledServerIdWithWebId 根据WebId查找ServerId
func (this *ServerDAO) FindEnabledServerIdWithWebId(tx *dbs.Tx, webId int64) (serverId int64, err error) {
if webId <= 0 {
return 0, nil
@@ -1271,7 +1271,7 @@ func (this *ServerDAO) FindEnabledServerIdWithWebId(tx *dbs.Tx, webId int64) (se
FindInt64Col(0)
}
// 查找包含某个反向代理的Server
// FindEnabledServerIdWithReverseProxyId 查找包含某个反向代理的Server
func (this *ServerDAO) FindEnabledServerIdWithReverseProxyId(tx *dbs.Tx, reverseProxyId int64) (serverId int64, err error) {
return this.Query(tx).
State(ServerStateEnabled).
@@ -1281,7 +1281,7 @@ func (this *ServerDAO) FindEnabledServerIdWithReverseProxyId(tx *dbs.Tx, reverse
FindInt64Col(0)
}
// 检查端口是否被使用
// CheckPortIsUsing 检查端口是否被使用
func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, port int, excludeServerId int64, excludeProtocol string) (bool, error) {
listen := maps.Map{
"portRange": strconv.Itoa(port),
@@ -1321,7 +1321,7 @@ func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, port int, e
Exist()
}
// 检查ServerName是否已存在
// ExistServerNameInCluster 检查ServerName是否已存在
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64) (bool, error) {
query := this.Query(tx).
Attr("clusterId", clusterId).
@@ -1335,7 +1335,7 @@ func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, ser
return query.Exist()
}
// 生成DNS Name
// GenDNSName 生成DNS Name
func (this *ServerDAO) GenDNSName(tx *dbs.Tx) (string, error) {
for {
dnsName := rands.HexString(8)
@@ -1351,7 +1351,23 @@ func (this *ServerDAO) GenDNSName(tx *dbs.Tx) (string, error) {
}
}
// 同步集群
// FindLatestServers 查询最近访问的服务
func (this *ServerDAO) FindLatestServers(tx *dbs.Tx, size int64) (result []*Server, err error) {
itemTable := SharedLatestItemDAO.Table
itemType := LatestItemTypeServer
_, err = this.Query(tx).
Result(this.Table+".id", this.Table+".name").
Join(SharedLatestItemDAO, dbs.QueryJoinRight, this.Table+".id="+itemTable+".itemId AND "+itemTable+".itemType='"+itemType+"'").
Asc("CEIL((UNIX_TIMESTAMP() - " + itemTable + ".updatedAt) / (7 * 86400))"). // 优先一个星期以内的
Desc(itemTable + ".count").
State(NodeClusterStateEnabled).
Limit(size).
Slice(&result).
FindAll()
return
}
// NotifyUpdate 同步集群
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
// 更新配置
_, err := this.RenewServerConfig(tx, serverId, true)
@@ -1370,7 +1386,7 @@ func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
return SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
}
// 通知DNS更新
// NotifyDNSUpdate 通知DNS更新
func (this *ServerDAO) NotifyDNSUpdate(tx *dbs.Tx, serverId int64) error {
clusterId, err := this.Query(tx).
Pk(serverId).

View File

@@ -36,7 +36,7 @@ func init() {
})
}
// 启用条目
// EnableUser 启用条目
func (this *UserDAO) EnableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -44,7 +44,7 @@ func (this *UserDAO) EnableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err e
Update()
}
// 禁用条目
// DisableUser 禁用条目
func (this *UserDAO) DisableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -52,7 +52,7 @@ 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) {
result, err := this.Query(tx).
Pk(id).
@@ -64,7 +64,7 @@ func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, id int64) (*User, error) {
return result.(*User), err
}
// 查找用户基本信息
// FindEnabledBasicUser 查找用户基本信息
func (this *UserDAO) FindEnabledBasicUser(tx *dbs.Tx, id int64) (*User, error) {
result, err := this.Query(tx).
Pk(id).
@@ -77,7 +77,7 @@ func (this *UserDAO) FindEnabledBasicUser(tx *dbs.Tx, id int64) (*User, error) {
return result.(*User), err
}
// 获取管理员名称
// FindUserFullname 获取管理员名称
func (this *UserDAO) FindUserFullname(tx *dbs.Tx, userId int64) (string, error) {
return this.Query(tx).
Pk(userId).
@@ -85,7 +85,7 @@ func (this *UserDAO) FindUserFullname(tx *dbs.Tx, userId int64) (string, error)
FindStringCol("")
}
// 创建用户
// CreateUser 创建用户
func (this *UserDAO) CreateUser(tx *dbs.Tx, username string, password string, fullname string, mobile string, tel string, email string, remark string, source string, clusterId int64) (int64, error) {
op := NewUserOperator()
op.Username = username
@@ -107,7 +107,7 @@ func (this *UserDAO) CreateUser(tx *dbs.Tx, username string, password string, fu
return types.Int64(op.Id), nil
}
// 修改用户
// UpdateUser 修改用户
func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, password string, fullname string, mobile string, tel string, email string, remark string, isOn bool, clusterId int64) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -129,7 +129,7 @@ func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, passw
return err
}
// 修改用户基本信息
// UpdateUserInfo 修改用户基本信息
func (this *UserDAO) UpdateUserInfo(tx *dbs.Tx, userId int64, fullname string) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -140,7 +140,7 @@ func (this *UserDAO) UpdateUserInfo(tx *dbs.Tx, userId int64, fullname string) e
return this.Save(tx, op)
}
// 修改用户登录信息
// UpdateUserLogin 修改用户登录信息
func (this *UserDAO) UpdateUserLogin(tx *dbs.Tx, userId int64, username string, password string) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -155,7 +155,7 @@ func (this *UserDAO) UpdateUserLogin(tx *dbs.Tx, userId int64, username string,
return err
}
// 计算用户数量
// CountAllEnabledUsers 计算用户数量
func (this *UserDAO) CountAllEnabledUsers(tx *dbs.Tx, keyword string) (int64, error) {
query := this.Query(tx)
query.State(UserStateEnabled)
@@ -166,8 +166,8 @@ func (this *UserDAO) CountAllEnabledUsers(tx *dbs.Tx, keyword string) (int64, er
return query.Count()
}
// 列出单页用户
func (this *UserDAO) ListEnabledUsers(tx *dbs.Tx, keyword string) (result []*User, err error) {
// ListEnabledUsers 列出单页用户
func (this *UserDAO) ListEnabledUsers(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*User, err error) {
query := this.Query(tx)
query.State(UserStateEnabled)
if len(keyword) > 0 {
@@ -176,12 +176,14 @@ func (this *UserDAO) ListEnabledUsers(tx *dbs.Tx, keyword string) (result []*Use
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// 检查用户名是否存在
// ExistUser 检查用户名是否存在
func (this *UserDAO) ExistUser(tx *dbs.Tx, userId int64, username string) (bool, error) {
return this.Query(tx).
State(UserStateEnabled).
@@ -190,7 +192,7 @@ func (this *UserDAO) ExistUser(tx *dbs.Tx, userId int64, username string) (bool,
Exist()
}
// 列出单页的用户ID
// ListEnabledUserIds 列出单页的用户ID
func (this *UserDAO) ListEnabledUserIds(tx *dbs.Tx, offset, size int64) ([]int64, error) {
ones, _, err := this.Query(tx).
ResultPk().
@@ -209,7 +211,7 @@ func (this *UserDAO) ListEnabledUserIds(tx *dbs.Tx, offset, size int64) ([]int64
return result, nil
}
// 检查用户名、密码
// CheckUserPassword 检查用户名、密码
func (this *UserDAO) CheckUserPassword(tx *dbs.Tx, username string, encryptedPassword string) (int64, error) {
if len(username) == 0 || len(encryptedPassword) == 0 {
return 0, nil
@@ -223,7 +225,7 @@ func (this *UserDAO) CheckUserPassword(tx *dbs.Tx, username string, encryptedPas
FindInt64Col(0)
}
// 查找用户所在集群
// FindUserClusterId 查找用户所在集群
func (this *UserDAO) FindUserClusterId(tx *dbs.Tx, userId int64) (int64, error) {
return this.Query(tx).
Pk(userId).
@@ -231,7 +233,7 @@ func (this *UserDAO) FindUserClusterId(tx *dbs.Tx, userId int64) (int64, error)
FindInt64Col(0)
}
// 更新用户Features
// UpdateUserFeatures 更新用户Features
func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON []byte) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -249,7 +251,7 @@ func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON [
return nil
}
// 查找用户Features
// FindUserFeatures 查找用户Features
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*UserFeature, error) {
featuresJSON, err := this.Query(tx).
Pk(userId).

View File

@@ -3,6 +3,8 @@ 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/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -38,7 +40,7 @@ func init() {
})
}
// 启用条目
// EnableUserNode 启用条目
func (this *UserNodeDAO) EnableUserNode(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
@@ -47,7 +49,7 @@ func (this *UserNodeDAO) EnableUserNode(tx *dbs.Tx, id uint32) error {
return err
}
// 禁用条目
// DisableUserNode 禁用条目
func (this *UserNodeDAO) DisableUserNode(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -56,7 +58,7 @@ func (this *UserNodeDAO) DisableUserNode(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledUserNode 查找启用中的条目
func (this *UserNodeDAO) FindEnabledUserNode(tx *dbs.Tx, id int64) (*UserNode, error) {
result, err := this.Query(tx).
Pk(id).
@@ -68,7 +70,7 @@ func (this *UserNodeDAO) FindEnabledUserNode(tx *dbs.Tx, id int64) (*UserNode, e
return result.(*UserNode), err
}
// 根据主键查找名称
// FindUserNodeName 根据主键查找名称
func (this *UserNodeDAO) FindUserNodeName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -76,7 +78,7 @@ func (this *UserNodeDAO) FindUserNodeName(tx *dbs.Tx, id int64) (string, error)
FindStringCol("")
}
// 列出所有可用用户节点
// FindAllEnabledUserNodes 列出所有可用用户节点
func (this *UserNodeDAO) FindAllEnabledUserNodes(tx *dbs.Tx) (result []*UserNode, err error) {
_, err = this.Query(tx).
State(UserNodeStateEnabled).
@@ -87,14 +89,14 @@ func (this *UserNodeDAO) FindAllEnabledUserNodes(tx *dbs.Tx) (result []*UserNode
return
}
// 计算用户节点数量
// CountAllEnabledUserNodes 计算用户节点数量
func (this *UserNodeDAO) CountAllEnabledUserNodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(UserNodeStateEnabled).
Count()
}
// 列出单页的用户节点
// ListEnabledUserNodes 列出单页的用户节点
func (this *UserNodeDAO) ListEnabledUserNodes(tx *dbs.Tx, offset int64, size int64) (result []*UserNode, err error) {
_, err = this.Query(tx).
State(UserNodeStateEnabled).
@@ -107,7 +109,7 @@ func (this *UserNodeDAO) ListEnabledUserNodes(tx *dbs.Tx, offset int64, size int
return
}
// 根据主机名和端口获取ID
// FindEnabledUserNodeIdWithAddr 根据主机名和端口获取ID
func (this *UserNodeDAO) FindEnabledUserNodeIdWithAddr(tx *dbs.Tx, protocol string, host string, port int) (int64, error) {
addr := maps.Map{
"protocol": protocol,
@@ -134,14 +136,14 @@ func (this *UserNodeDAO) FindEnabledUserNodeIdWithAddr(tx *dbs.Tx, protocol stri
return int64(one.(*UserNode).Id), nil
}
// 创建用户节点
// CreateUserNode 创建用户节点
func (this *UserNodeDAO) CreateUserNode(tx *dbs.Tx, name string, description string, httpJSON []byte, httpsJSON []byte, accessAddrsJSON []byte, isOn bool) (nodeId int64, err error) {
uniqueId, err := this.GenUniqueId(tx)
if err != nil {
return 0, err
}
secret := rands.String(32)
err = NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, NodeRoleUser)
err = NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleUser)
if err != nil {
return
}
@@ -172,7 +174,7 @@ func (this *UserNodeDAO) CreateUserNode(tx *dbs.Tx, name string, description str
return types.Int64(op.Id), nil
}
// 修改用户节点
// UpdateUserNode 修改用户节点
func (this *UserNodeDAO) UpdateUserNode(tx *dbs.Tx, nodeId int64, name string, description string, httpJSON []byte, httpsJSON []byte, accessAddrsJSON []byte, isOn bool) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
@@ -204,7 +206,7 @@ func (this *UserNodeDAO) UpdateUserNode(tx *dbs.Tx, nodeId int64, name string, d
return err
}
// 根据唯一ID获取节点信息
// FindEnabledUserNodeWithUniqueId 根据唯一ID获取节点信息
func (this *UserNodeDAO) FindEnabledUserNodeWithUniqueId(tx *dbs.Tx, uniqueId string) (*UserNode, error) {
result, err := this.Query(tx).
Attr("uniqueId", uniqueId).
@@ -216,7 +218,7 @@ func (this *UserNodeDAO) FindEnabledUserNodeWithUniqueId(tx *dbs.Tx, uniqueId st
return result.(*UserNode), err
}
// 根据唯一ID获取节点ID
// FindEnabledUserNodeIdWithUniqueId 根据唯一ID获取节点ID
func (this *UserNodeDAO) FindEnabledUserNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
return this.Query(tx).
Attr("uniqueId", uniqueId).
@@ -225,7 +227,7 @@ func (this *UserNodeDAO) FindEnabledUserNodeIdWithUniqueId(tx *dbs.Tx, uniqueId
FindInt64Col(0)
}
// 生成唯一ID
// GenUniqueId 生成唯一ID
func (this *UserNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
for {
uniqueId := rands.HexString(32)
@@ -242,7 +244,7 @@ func (this *UserNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
}
}
// 更改节点状态
// UpdateNodeStatus 更改节点状态
func (this *UserNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
if len(statusJSON) == 0 {
return nil
@@ -253,3 +255,13 @@ func (this *UserNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON [
Update()
return err
}
// CountAllLowerVersionNodes 计算所有节点中低于某个版本的节点数量
func (this *UserNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
return this.Query(tx).
State(UserNodeStateEnabled).
Where("status IS NOT NULL").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("version", utils.VersionToLong(version)).
Count()
}

View File

@@ -1,10 +1,11 @@
package dnsclients
package dnstypes
type RecordType = string
const (
RecordTypeA RecordType = "A"
RecordTypeCName RecordType = "CNAME"
RecordTypeAAAA RecordType = "AAAA"
RecordTypeCNAME RecordType = "CNAME"
RecordTypeTXT RecordType = "TXT"
)

View File

@@ -1,4 +1,4 @@
package dnsclients
package dnstypes
// Route 线路描述
type Route struct {

View File

@@ -2,6 +2,7 @@ package dnsclients
import (
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
@@ -31,7 +32,7 @@ func (this *AliDNSProvider) Auth(params maps.Map) error {
}
// GetRecords 获取域名列表
func (this *AliDNSProvider) GetRecords(domain string) (records []*Record, err error) {
func (this *AliDNSProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
pageNumber := 1
size := 100
@@ -48,11 +49,11 @@ func (this *AliDNSProvider) GetRecords(domain string) (records []*Record, err er
}
for _, record := range resp.DomainRecords.Record {
// 修正Record
if record.Type == RecordTypeCName && !strings.HasSuffix(record.Value, ".") {
if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Value, ".") {
record.Value += "."
}
records = append(records, &Record{
records = append(records, &dnstypes.Record{
Id: record.RecordId,
Name: record.RR,
Type: record.Type,
@@ -71,7 +72,7 @@ func (this *AliDNSProvider) GetRecords(domain string) (records []*Record, err er
}
// GetRoutes 读取域名支持的线路数据
func (this *AliDNSProvider) GetRoutes(domain string) (routes []*Route, err error) {
func (this *AliDNSProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
req := alidns.CreateDescribeSupportLinesRequest()
req.DomainName = domain
@@ -81,7 +82,7 @@ func (this *AliDNSProvider) GetRoutes(domain string) (routes []*Route, err error
return nil, err
}
for _, line := range resp.RecordLines.RecordLine {
routes = append(routes, &Route{
routes = append(routes, &dnstypes.Route{
Name: line.LineName,
Code: line.LineCode,
})
@@ -90,7 +91,7 @@ func (this *AliDNSProvider) GetRoutes(domain string) (routes []*Route, err error
}
// QueryRecord 查询单个记录
func (this *AliDNSProvider) QueryRecord(domain string, name string, recordType RecordType) (*Record, error) {
func (this *AliDNSProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
records, err := this.GetRecords(domain)
if err != nil {
return nil, err
@@ -104,7 +105,7 @@ func (this *AliDNSProvider) QueryRecord(domain string, name string, recordType R
}
// AddRecord 设置记录
func (this *AliDNSProvider) AddRecord(domain string, newRecord *Record) error {
func (this *AliDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
req := alidns.CreateAddDomainRecordRequest()
req.RR = newRecord.Name
req.Type = newRecord.Type
@@ -125,7 +126,7 @@ func (this *AliDNSProvider) AddRecord(domain string, newRecord *Record) error {
}
// UpdateRecord 修改记录
func (this *AliDNSProvider) UpdateRecord(domain string, record *Record, newRecord *Record) error {
func (this *AliDNSProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
req := alidns.CreateUpdateDomainRecordRequest()
req.RecordId = record.Id
req.RR = newRecord.Name
@@ -139,7 +140,7 @@ func (this *AliDNSProvider) UpdateRecord(domain string, record *Record, newRecor
}
// DeleteRecord 删除记录
func (this *AliDNSProvider) DeleteRecord(domain string, record *Record) error {
func (this *AliDNSProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
req := alidns.CreateDeleteDomainRecordRequest()
req.RecordId = record.Id

View File

@@ -2,6 +2,7 @@ package dnsclients
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
@@ -29,7 +30,7 @@ func TestAliDNSProvider_DeleteRecord(t *testing.T) {
t.Fatal(err)
}
err = provider.DeleteRecord("meloy.cn", &Record{
err = provider.DeleteRecord("meloy.cn", &dnstypes.Record{
Id: "20746603318032384",
})
if err != nil {
@@ -57,10 +58,10 @@ func TestAliDNSProvider_AddRecord(t *testing.T) {
t.Fatal(err)
}
err = provider.AddRecord("meloy.cn", &Record{
err = provider.AddRecord("meloy.cn", &dnstypes.Record{
Id: "",
Name: "test",
Type: RecordTypeA,
Type: dnstypes.RecordTypeA,
Value: "192.168.1.100",
Route: "unicom",
})
@@ -76,10 +77,10 @@ func TestAliDNSProvider_UpdateRecord(t *testing.T) {
t.Fatal(err)
}
err = provider.UpdateRecord("meloy.cn", &Record{Id: "20746664455255040"}, &Record{
err = provider.UpdateRecord("meloy.cn", &dnstypes.Record{Id: "20746664455255040"}, &dnstypes.Record{
Id: "",
Name: "test",
Type: RecordTypeA,
Type: dnstypes.RecordTypeA,
Value: "192.168.1.101",
Route: "unicom",
})

View File

@@ -7,6 +7,7 @@ import (
"crypto/tls"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/cloudflare"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/maps"
"io"
@@ -59,7 +60,7 @@ func (this *CloudFlareProvider) Auth(params maps.Map) error {
}
// GetRecords 获取域名解析记录列表
func (this *CloudFlareProvider) GetRecords(domain string) (records []*Record, err error) {
func (this *CloudFlareProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
zoneId, err := this.findZoneIdWithDomain(domain)
if err != nil {
return nil, err
@@ -81,13 +82,13 @@ func (this *CloudFlareProvider) GetRecords(domain string) (records []*Record, er
for _, record := range resp.Result {
// 修正Record
if record.Type == RecordTypeCName && !strings.HasSuffix(record.Content, ".") {
if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Content, ".") {
record.Content += "."
}
record.Name = strings.TrimSuffix(record.Name, "."+domain)
records = append(records, &Record{
records = append(records, &dnstypes.Record{
Id: record.Id,
Name: record.Name,
Type: record.Type,
@@ -101,15 +102,15 @@ func (this *CloudFlareProvider) GetRecords(domain string) (records []*Record, er
}
// GetRoutes 读取域名支持的线路数据
func (this *CloudFlareProvider) GetRoutes(domain string) (routes []*Route, err error) {
routes = []*Route{
func (this *CloudFlareProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
routes = []*dnstypes.Route{
{Name: "默认", Code: CloudFlareDefaultRoute},
}
return
}
// QueryRecord 查询单个记录
func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordType RecordType) (*Record, error) {
func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
zoneId, err := this.findZoneIdWithDomain(domain)
if err != nil {
return nil, err
@@ -131,13 +132,13 @@ func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordTy
record := resp.Result[0]
// 修正Record
if record.Type == RecordTypeCName && !strings.HasSuffix(record.Content, ".") {
if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Content, ".") {
record.Content += "."
}
record.Name = strings.TrimSuffix(record.Name, "."+domain)
return &Record{
return &dnstypes.Record{
Id: record.Id,
Name: record.Name,
Type: record.Type,
@@ -147,7 +148,7 @@ func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordTy
}
// AddRecord 设置记录
func (this *CloudFlareProvider) AddRecord(domain string, newRecord *Record) error {
func (this *CloudFlareProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
zoneId, err := this.findZoneIdWithDomain(domain)
if err != nil {
return err
@@ -167,7 +168,7 @@ func (this *CloudFlareProvider) AddRecord(domain string, newRecord *Record) erro
}
// UpdateRecord 修改记录
func (this *CloudFlareProvider) UpdateRecord(domain string, record *Record, newRecord *Record) error {
func (this *CloudFlareProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
zoneId, err := this.findZoneIdWithDomain(domain)
if err != nil {
return err
@@ -183,7 +184,7 @@ func (this *CloudFlareProvider) UpdateRecord(domain string, record *Record, newR
}
// DeleteRecord 删除记录
func (this *CloudFlareProvider) DeleteRecord(domain string, record *Record) error {
func (this *CloudFlareProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
zoneId, err := this.findZoneIdWithDomain(domain)
if err != nil {
return err

View File

@@ -4,6 +4,7 @@ package dnsclients
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
@@ -45,7 +46,7 @@ func TestCloudFlareProvider_QueryRecord(t *testing.T) {
}
{
t.Log("== www.meloy.cn/A ==")
record, err := provider.QueryRecord("meloy.cn", "www", RecordTypeA)
record, err := provider.QueryRecord("meloy.cn", "www", dnstypes.RecordTypeA)
if err != nil {
t.Fatal(err)
}
@@ -53,7 +54,7 @@ func TestCloudFlareProvider_QueryRecord(t *testing.T) {
}
{
t.Log("== www.meloy.cn/CNAME ==")
record, err := provider.QueryRecord("meloy.cn", "www", RecordTypeCName)
record, err := provider.QueryRecord("meloy.cn", "www", dnstypes.RecordTypeCNAME)
if err != nil {
t.Fatal(err)
}
@@ -61,7 +62,7 @@ func TestCloudFlareProvider_QueryRecord(t *testing.T) {
}
{
t.Log("== hello.meloy.cn ==")
record, err := provider.QueryRecord("meloy.cn", "hello", RecordTypeA)
record, err := provider.QueryRecord("meloy.cn", "hello", dnstypes.RecordTypeA)
if err != nil {
t.Fatal(err)
}
@@ -69,7 +70,7 @@ func TestCloudFlareProvider_QueryRecord(t *testing.T) {
}
{
t.Log("== test.meloy.cn ==")
record, err := provider.QueryRecord("meloy.cn", "test", RecordTypeCName)
record, err := provider.QueryRecord("meloy.cn", "test", dnstypes.RecordTypeCNAME)
if err != nil {
t.Fatal(err)
}
@@ -83,10 +84,10 @@ func TestCloudFlareProvider_AddRecord(t *testing.T) {
t.Fatal(err)
}
{
err = provider.AddRecord("meloy.cn", &Record{
err = provider.AddRecord("meloy.cn", &dnstypes.Record{
Id: "",
Name: "test",
Type: RecordTypeA,
Type: dnstypes.RecordTypeA,
Value: "182.92.212.46",
Route: "",
})
@@ -95,10 +96,10 @@ func TestCloudFlareProvider_AddRecord(t *testing.T) {
}
}
{
err = provider.AddRecord("meloy.cn", &Record{
err = provider.AddRecord("meloy.cn", &dnstypes.Record{
Id: "",
Name: "test1",
Type: RecordTypeCName,
Type: dnstypes.RecordTypeCNAME,
Value: "cdn.meloy.cn.",
Route: "",
})
@@ -114,10 +115,10 @@ func TestCloudFlareProvider_UpdateRecord(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = provider.UpdateRecord("meloy.cn", &Record{Id: "b4da7ad9f90173ec37c80ba6bb70641a"}, &Record{
err = provider.UpdateRecord("meloy.cn", &dnstypes.Record{Id: "b4da7ad9f90173ec37c80ba6bb70641a"}, &dnstypes.Record{
Id: "",
Name: "test1",
Type: RecordTypeCName,
Type: dnstypes.RecordTypeCNAME,
Value: "cdn123.meloy.cn.",
Route: "",
})
@@ -132,7 +133,7 @@ func TestCloudFlareProvider_DeleteRecord(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = provider.DeleteRecord("meloy.cn", &Record{
err = provider.DeleteRecord("meloy.cn", &dnstypes.Record{
Id: "86282d89bbd1f66a69ca409da84f34b1",
})
if err != nil {

View File

@@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/maps"
"io/ioutil"
@@ -49,7 +50,7 @@ func (this *CustomHTTPProvider) Auth(params maps.Map) error {
}
// GetRecords 获取域名解析记录列表
func (this *CustomHTTPProvider) GetRecords(domain string) (records []*Record, err error) {
func (this *CustomHTTPProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
resp, err := this.post(maps.Map{
"action": "GetRecords",
"domain": domain,
@@ -62,7 +63,7 @@ func (this *CustomHTTPProvider) GetRecords(domain string) (records []*Record, er
}
// GetRoutes 读取域名支持的线路数据
func (this *CustomHTTPProvider) GetRoutes(domain string) (routes []*Route, err error) {
func (this *CustomHTTPProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
resp, err := this.post(maps.Map{
"action": "GetRoutes",
"domain": domain,
@@ -75,7 +76,7 @@ func (this *CustomHTTPProvider) GetRoutes(domain string) (routes []*Route, err e
}
// QueryRecord 查询单个记录
func (this *CustomHTTPProvider) QueryRecord(domain string, name string, recordType RecordType) (*Record, error) {
func (this *CustomHTTPProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
resp, err := this.post(maps.Map{
"action": "QueryRecord",
"domain": domain,
@@ -88,7 +89,7 @@ func (this *CustomHTTPProvider) QueryRecord(domain string, name string, recordTy
if len(resp) == 0 {
return nil, nil
}
record := &Record{}
record := &dnstypes.Record{}
err = json.Unmarshal(resp, record)
if err != nil {
return nil, err
@@ -100,7 +101,7 @@ func (this *CustomHTTPProvider) QueryRecord(domain string, name string, recordTy
}
// AddRecord 设置记录
func (this *CustomHTTPProvider) AddRecord(domain string, newRecord *Record) error {
func (this *CustomHTTPProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
_, err := this.post(maps.Map{
"action": "AddRecord",
"domain": domain,
@@ -110,7 +111,7 @@ func (this *CustomHTTPProvider) AddRecord(domain string, newRecord *Record) erro
}
// UpdateRecord 修改记录
func (this *CustomHTTPProvider) UpdateRecord(domain string, record *Record, newRecord *Record) error {
func (this *CustomHTTPProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
_, err := this.post(maps.Map{
"action": "UpdateRecord",
"domain": domain,
@@ -121,7 +122,7 @@ func (this *CustomHTTPProvider) UpdateRecord(domain string, record *Record, newR
}
// DeleteRecord 删除记录
func (this *CustomHTTPProvider) DeleteRecord(domain string, record *Record) error {
func (this *CustomHTTPProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
_, err := this.post(maps.Map{
"action": "DeleteRecord",
"domain": domain,

View File

@@ -1,6 +1,7 @@
package dnsclients
import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"testing"
@@ -15,10 +16,10 @@ func TestCustomHTTPProvider_AddRecord(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = provider.AddRecord("hello.com", &Record{
err = provider.AddRecord("hello.com", &dnstypes.Record{
Id: "",
Name: "world",
Type: RecordTypeA,
Type: dnstypes.RecordTypeA,
Value: "127.0.0.1",
Route: "default",
})

View File

@@ -3,6 +3,7 @@ package dnsclients
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
@@ -35,7 +36,7 @@ func (this *DNSPodProvider) Auth(params maps.Map) error {
}
// GetRecords 获取域名列表
func (this *DNSPodProvider) GetRecords(domain string) (records []*Record, err error) {
func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
offset := 0
size := 100
for {
@@ -53,7 +54,7 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*Record, err er
recordSlice := recordsResp.GetSlice("records")
for _, record := range recordSlice {
recordMap := maps.NewMap(record)
records = append(records, &Record{
records = append(records, &dnstypes.Record{
Id: recordMap.GetString("id"),
Name: recordMap.GetString("name"),
Type: recordMap.GetString("type"),
@@ -73,7 +74,7 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*Record, err er
}
// GetRoutes 读取线路数据
func (this *DNSPodProvider) GetRoutes(domain string) (routes []*Route, err error) {
func (this *DNSPodProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
infoResp, err := this.post("/Domain.info", map[string]string{
"domain": domain,
})
@@ -97,7 +98,7 @@ func (this *DNSPodProvider) GetRoutes(domain string) (routes []*Route, err error
}
for _, line := range lines {
lineString := types.String(line)
routes = append(routes, &Route{
routes = append(routes, &dnstypes.Route{
Name: lineString,
Code: lineString,
})
@@ -107,7 +108,7 @@ func (this *DNSPodProvider) GetRoutes(domain string) (routes []*Route, err error
}
// QueryRecord 查询单个记录
func (this *DNSPodProvider) QueryRecord(domain string, name string, recordType RecordType) (*Record, error) {
func (this *DNSPodProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
records, err := this.GetRecords(domain)
if err != nil {
return nil, err
@@ -121,13 +122,13 @@ func (this *DNSPodProvider) QueryRecord(domain string, name string, recordType R
}
// AddRecord 设置记录
func (this *DNSPodProvider) AddRecord(domain string, newRecord *Record) error {
func (this *DNSPodProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
if newRecord == nil {
return errors.New("invalid new record")
}
// 在CHANGE记录后面加入点
if newRecord.Type == RecordTypeCName && !strings.HasSuffix(newRecord.Value, ".") {
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
newRecord.Value += "."
}
_, err := this.post("/Record.Create", map[string]string{
@@ -141,7 +142,7 @@ func (this *DNSPodProvider) AddRecord(domain string, newRecord *Record) error {
}
// UpdateRecord 修改记录
func (this *DNSPodProvider) UpdateRecord(domain string, record *Record, newRecord *Record) error {
func (this *DNSPodProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
if record == nil {
return errors.New("invalid record")
}
@@ -150,7 +151,7 @@ func (this *DNSPodProvider) UpdateRecord(domain string, record *Record, newRecor
}
// 在CHANGE记录后面加入点
if newRecord.Type == RecordTypeCName && !strings.HasSuffix(newRecord.Value, ".") {
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
newRecord.Value += "."
}
_, err := this.post("/Record.Modify", map[string]string{
@@ -165,7 +166,7 @@ func (this *DNSPodProvider) UpdateRecord(domain string, record *Record, newRecor
}
// DeleteRecord 删除记录
func (this *DNSPodProvider) DeleteRecord(domain string, record *Record) error {
func (this *DNSPodProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
if record == nil {
return errors.New("invalid record to delete")
}

View File

@@ -2,6 +2,7 @@ package dnsclients
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
@@ -40,8 +41,8 @@ func TestDNSPodProvider_AddRecord(t *testing.T) {
t.Fatal(err)
}
err = provider.AddRecord("yun4s.cn", &Record{
Type: RecordTypeCName,
err = provider.AddRecord("yun4s.cn", &dnstypes.Record{
Type: dnstypes.RecordTypeCNAME,
Name: "hello-forward",
Value: "hello.yun4s.cn",
Route: "联通",
@@ -58,10 +59,10 @@ func TestDNSPodProvider_UpdateRecord(t *testing.T) {
t.Fatal(err)
}
err = provider.UpdateRecord("yun4s.cn", &Record{
err = provider.UpdateRecord("yun4s.cn", &dnstypes.Record{
Id: "697036856",
}, &Record{
Type: RecordTypeA,
}, &dnstypes.Record{
Type: dnstypes.RecordTypeA,
Name: "hello",
Value: "192.168.1.102",
Route: "联通",
@@ -78,7 +79,7 @@ func TestDNSPodProvider_DeleteRecord(t *testing.T) {
t.Fatal(err)
}
err = provider.DeleteRecord("yun4s.cn", &Record{
err = provider.DeleteRecord("yun4s.cn", &dnstypes.Record{
Id: "697040986",
})
if err != nil {

View File

@@ -1,6 +1,9 @@
package dnsclients
import "github.com/iwind/TeaGo/maps"
import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/maps"
)
// ProviderInterface DNS操作接口
type ProviderInterface interface {
@@ -8,22 +11,22 @@ type ProviderInterface interface {
Auth(params maps.Map) error
// GetRecords 获取域名解析记录列表
GetRecords(domain string) (records []*Record, err error)
GetRecords(domain string) (records []*dnstypes.Record, err error)
// GetRoutes 读取域名支持的线路数据
GetRoutes(domain string) (routes []*Route, err error)
GetRoutes(domain string) (routes []*dnstypes.Route, err error)
// QueryRecord 查询单个记录
QueryRecord(domain string, name string, recordType RecordType) (*Record, error)
QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error)
// AddRecord 设置记录
AddRecord(domain string, newRecord *Record) error
AddRecord(domain string, newRecord *dnstypes.Record) error
// UpdateRecord 修改记录
UpdateRecord(domain string, record *Record, newRecord *Record) error
UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error
// DeleteRecord 删除记录
DeleteRecord(domain string, record *Record) error
DeleteRecord(domain string, record *dnstypes.Record) error
// DefaultRoute 默认线路
DefaultRoute() string

View File

@@ -0,0 +1,246 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dnsclients
import (
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"regexp"
"strings"
)
type LocalEdgeDNSProvider struct {
clusterId int64 // 集群ID
ttl int32 // TTL
}
// Auth 认证
func (this *LocalEdgeDNSProvider) Auth(params maps.Map) error {
this.clusterId = params.GetInt64("clusterId")
if this.clusterId <= 0 {
return errors.New("'clusterId' should be greater than 0")
}
this.ttl = params.GetInt32("ttl")
if this.ttl <= 0 {
this.ttl = 3600
}
return nil
}
// GetRecords 获取域名解析记录列表
func (this *LocalEdgeDNSProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
var tx *dbs.Tx
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
if err != nil {
return nil, err
}
if domainId == 0 {
return nil, errors.New("can not find domain '" + domain + "'")
}
offset := int64(0)
size := int64(1000)
for {
result, err := nameservers.SharedNSRecordDAO.ListEnabledRecords(tx, domainId, "", "", 0, offset, size)
if err != nil {
return nil, err
}
if len(result) == 0 {
break
}
for _, record := range result {
if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Value, ".") {
record.Value += "."
}
routeIds := record.DecodeRouteIds()
var routeIdString = ""
if len(routeIds) > 0 {
routeIdString = fmt.Sprintf("%d", routeIds[0])
}
records = append(records, &dnstypes.Record{
Id: fmt.Sprintf("%d", record.Id),
Name: record.Name,
Type: record.Type,
Value: record.Value,
Route: routeIdString,
})
}
offset += size
}
return
}
// GetRoutes 读取域名支持的线路数据
func (this *LocalEdgeDNSProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
var tx *dbs.Tx
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
if err != nil {
return nil, err
}
if domainId == 0 {
return nil, errors.New("can not find domain '" + domain + "'")
}
// TODO 将来支持集群、域名、用户自定义线路
result, err := nameservers.SharedNSRouteDAO.FindAllEnabledRoutes(tx, 0, 0, 0)
if err != nil {
return nil, err
}
for _, route := range result {
routes = append(routes, &dnstypes.Route{
Name: route.Name,
Code: fmt.Sprintf("%d", route.Id),
})
}
return
}
// QueryRecord 查询单个记录
func (this *LocalEdgeDNSProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
var tx *dbs.Tx
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
if err != nil {
return nil, err
}
if domainId == 0 {
return nil, nil
}
record, err := nameservers.SharedNSRecordDAO.FindEnabledRecordWithName(tx, domainId, name, recordType)
if err != nil {
return nil, err
}
if record == nil {
return nil, nil
}
routeIds := record.DecodeRouteIds()
var routeIdString = ""
if len(routeIds) > 0 {
routeIdString = fmt.Sprintf("%d", routeIds[0])
}
return &dnstypes.Record{
Id: fmt.Sprintf("%d", record.Id),
Name: record.Name,
Type: record.Type,
Value: record.Value,
Route: routeIdString,
}, nil
}
// AddRecord 设置记录
func (this *LocalEdgeDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
var tx *dbs.Tx
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
if err != nil {
return err
}
if domainId == 0 {
return errors.New("can not find domain '" + domain + "'")
}
var routeIds []int64
if len(newRecord.Route) > 0 && regexp.MustCompile(`^\d+$`).MatchString(newRecord.Route) {
routeId := types.Int64(newRecord.Route)
if routeId > 0 {
routeIds = append(routeIds, routeId)
}
}
_, err = nameservers.SharedNSRecordDAO.CreateRecord(tx, domainId, "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds)
if err != nil {
return err
}
return nil
}
// UpdateRecord 修改记录
func (this *LocalEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
var tx *dbs.Tx
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
if err != nil {
return err
}
if domainId == 0 {
return errors.New("can not find domain '" + domain + "'")
}
var routeIds []int64
if len(newRecord.Route) > 0 && regexp.MustCompile(`^\d+$`).MatchString(newRecord.Route) {
routeId := types.Int64(newRecord.Route)
if routeId > 0 {
routeIds = append(routeIds, routeId)
}
}
if len(record.Id) > 0 {
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(record.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds)
if err != nil {
return err
}
} else {
realRecord, err := nameservers.SharedNSRecordDAO.FindEnabledRecordWithName(tx, domainId, record.Name, record.Type)
if err != nil {
return err
}
if realRecord != nil {
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(realRecord.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds)
if err != nil {
return err
}
}
}
return nil
}
// DeleteRecord 删除记录
func (this *LocalEdgeDNSProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
var tx *dbs.Tx
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
if err != nil {
return err
}
if domainId == 0 {
return errors.New("can not find domain '" + domain + "'")
}
if len(record.Id) > 0 {
err = nameservers.SharedNSRecordDAO.DisableNSRecord(tx, types.Int64(record.Id))
if err != nil {
return err
}
} else {
realRecord, err := nameservers.SharedNSRecordDAO.FindEnabledRecordWithName(tx, domainId, record.Name, record.Type)
if err != nil {
return err
}
if realRecord != nil {
err = nameservers.SharedNSRecordDAO.DisableNSRecord(tx, types.Int64(realRecord.Id))
if err != nil {
return err
}
}
}
return nil
}
// DefaultRoute 默认线路
func (this *LocalEdgeDNSProvider) DefaultRoute() string {
return ""
}

View File

@@ -0,0 +1,160 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dnsclients
import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"testing"
)
func TestLocalEdgeDNSProvider_GetRecords(t *testing.T) {
dbs.NotifyReady()
provider := &LocalEdgeDNSProvider{}
err := provider.Auth(maps.Map{
"clusterId": 1,
})
if err != nil {
t.Fatal(err)
}
records, err := provider.GetRecords("teaos.cn")
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(records, t)
}
func TestLocalEdgeDNSProvider_GetRoutes(t *testing.T) {
dbs.NotifyReady()
provider := &LocalEdgeDNSProvider{}
err := provider.Auth(maps.Map{
"clusterId": 1,
})
if err != nil {
t.Fatal(err)
}
routes, err := provider.GetRoutes("teaos.cn")
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(routes, t)
}
func TestLocalEdgeDNSProvider_QueryRecord(t *testing.T) {
dbs.NotifyReady()
provider := &LocalEdgeDNSProvider{}
err := provider.Auth(maps.Map{
"clusterId": 1,
})
if err != nil {
t.Fatal(err)
}
record, err := provider.QueryRecord("teaos.cn", "cdn", dnstypes.RecordTypeA)
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(record)
}
func TestLocalEdgeDNSProvider_AddRecord(t *testing.T) {
dbs.NotifyReady()
provider := &LocalEdgeDNSProvider{}
err := provider.Auth(maps.Map{
"clusterId": 1,
})
if err != nil {
t.Fatal(err)
}
err = provider.AddRecord("teaos.cn", &dnstypes.Record{
Id: "",
Name: "example",
Type: dnstypes.RecordTypeA,
Value: "10.0.0.1",
Route: "7",
})
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestLocalEdgeDNSProvider_UpdateRecord(t *testing.T) {
dbs.NotifyReady()
provider := &LocalEdgeDNSProvider{}
err := provider.Auth(maps.Map{
"clusterId": 1,
})
if err != nil {
t.Fatal(err)
}
record, err := provider.QueryRecord("teaos.cn", "cdn", dnstypes.RecordTypeA)
if err != nil {
t.Fatal(err)
}
if record == nil {
t.Log("not found record")
return
}
//record.Id = ""
err = provider.UpdateRecord("teaos.cn", record, &dnstypes.Record{
Id: "",
Name: record.Name,
Type: record.Type,
Value: "127.0.0.3",
Route: record.Route,
})
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestLocalEdgeDNSProvider_DeleteRecord(t *testing.T) {
dbs.NotifyReady()
provider := &LocalEdgeDNSProvider{}
err := provider.Auth(maps.Map{
"clusterId": 1,
})
if err != nil {
t.Fatal(err)
}
err = provider.DeleteRecord("teaos.cn", &dnstypes.Record{
Id: "",
Name: "example",
Type: "A",
Value: "",
Route: "",
})
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestLocalEdgeDNSProvider_DefaultRoute(t *testing.T) {
dbs.NotifyReady()
provider := &LocalEdgeDNSProvider{}
err := provider.Auth(maps.Map{
"clusterId": 1,
})
if err != nil {
t.Fatal(err)
}
t.Log(provider.DefaultRoute())
}

View File

@@ -0,0 +1,59 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dnsclients
import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/maps"
)
type UserEdgeDNSProvider struct {
}
// Auth 认证
func (this *UserEdgeDNSProvider) Auth(params maps.Map) error {
// TODO
return nil
}
// GetRecords 获取域名解析记录列表
func (this *UserEdgeDNSProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
// TODO
return
}
// GetRoutes 读取域名支持的线路数据
func (this *UserEdgeDNSProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
// TODO
return
}
// QueryRecord 查询单个记录
func (this *UserEdgeDNSProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
// TODO
return nil, nil
}
// AddRecord 设置记录
func (this *UserEdgeDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
// TODO
return nil
}
// UpdateRecord 修改记录
func (this *UserEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
// TODO
return nil
}
// DeleteRecord 删除记录
func (this *UserEdgeDNSProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
// TODO
return nil
}
// DefaultRoute 默认线路
func (this *UserEdgeDNSProvider) DefaultRoute() string {
// TODO
return ""
}

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