Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71681b1c5d | ||
|
|
6b2a015015 | ||
|
|
9e74baf37b | ||
|
|
b651d94bf5 | ||
|
|
b61433c5f8 | ||
|
|
c4d2191110 | ||
|
|
f6159c646c | ||
|
|
9cc7d54b64 | ||
|
|
05ce1d327b | ||
|
|
111df18204 | ||
|
|
bbcdf167b1 | ||
|
|
d62a44b1ac | ||
|
|
969bb2bcda | ||
|
|
6dcd3f2a98 | ||
|
|
7936dc85a9 | ||
|
|
94c4098de5 | ||
|
|
5b0aef8d4a | ||
|
|
4571b95e4a | ||
|
|
b9cba4c7df | ||
|
|
b46b5dc05f | ||
|
|
ef5630ba4a | ||
|
|
fe6c610d56 | ||
|
|
442dd195ca | ||
|
|
e9e4abff03 | ||
|
|
2a3fbd080e | ||
|
|
9d7f1a2702 | ||
|
|
57dcbce775 | ||
|
|
a71bf9ed79 | ||
|
|
725692ce94 | ||
|
|
ea15096dc3 | ||
|
|
a07dbb718f | ||
|
|
18381b76fb |
6
go.sum
6
go.sum
@@ -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=
|
||||
|
||||
@@ -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(),
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package teaconst
|
||||
|
||||
const (
|
||||
Version = "0.1.1"
|
||||
Version = "0.2.0"
|
||||
|
||||
ProductName = "Edge API"
|
||||
ProcessName = "edge-api"
|
||||
@@ -18,7 +18,7 @@ const (
|
||||
|
||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||
|
||||
NodeVersion = "0.1.1"
|
||||
NodeVersion = "0.2.0"
|
||||
UserNodeVersion = "0.0.7"
|
||||
AuthorityNodeVersion = "0.0.1"
|
||||
MonitorNodeVersion = "0.0.1"
|
||||
|
||||
7
internal/const/vars.go
Normal file
7
internal/const/vars.go
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package teaconst
|
||||
|
||||
var (
|
||||
IsPlus = false
|
||||
)
|
||||
@@ -4,6 +4,7 @@ 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"
|
||||
@@ -97,7 +98,7 @@ func (this *APINodeDAO) CreateAPINode(tx *dbs.Tx, name string, description strin
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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"
|
||||
@@ -113,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
|
||||
}
|
||||
|
||||
@@ -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, 0, "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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -111,7 +111,44 @@ 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
|
||||
}
|
||||
@@ -210,16 +247,25 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (
|
||||
}
|
||||
|
||||
// CountAllEnabledHTTPCachePolicies 计算可用缓存策略数量
|
||||
func (this *HTTPCachePolicyDAO) CountAllEnabledHTTPCachePolicies(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(HTTPCachePolicyStateEnabled).
|
||||
Count()
|
||||
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()
|
||||
}
|
||||
|
||||
// ListEnabledHTTPCachePolicies 列出单页的缓存策略
|
||||
func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset int64, size int64) ([]*serverconfigs.HTTPCachePolicy, error) {
|
||||
ones, err := this.Query(tx).
|
||||
State(HTTPCachePolicyStateEnabled).
|
||||
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).
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -3,6 +3,7 @@ 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"
|
||||
@@ -112,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
|
||||
}
|
||||
|
||||
145
internal/db/models/nameservers/ns_cluster_dao.go
Normal file
145
internal/db/models/nameservers/ns_cluster_dao.go
Normal 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
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_cluster_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_cluster_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
24
internal/db/models/nameservers/ns_cluster_model.go
Normal file
24
internal/db/models/nameservers/ns_cluster_model.go
Normal 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{}
|
||||
}
|
||||
1
internal/db/models/nameservers/ns_cluster_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_cluster_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
196
internal/db/models/nameservers/ns_domain_dao.go
Normal file
196
internal/db/models/nameservers/ns_domain_dao.go
Normal 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)
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_domain_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_domain_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
28
internal/db/models/nameservers/ns_domain_model.go
Normal file
28
internal/db/models/nameservers/ns_domain_model.go
Normal 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{}
|
||||
}
|
||||
1
internal/db/models/nameservers/ns_domain_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_domain_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
392
internal/db/models/nameservers/ns_node_dao.go
Normal file
392
internal/db/models/nameservers/ns_node_dao.go
Normal 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
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_node_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_node_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
38
internal/db/models/nameservers/ns_node_model.go
Normal file
38
internal/db/models/nameservers/ns_node_model.go
Normal 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{}
|
||||
}
|
||||
42
internal/db/models/nameservers/ns_node_model_ext.go
Normal file
42
internal/db/models/nameservers/ns_node_model_ext.go
Normal 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
|
||||
}
|
||||
220
internal/db/models/nameservers/ns_record_dao.go
Normal file
220
internal/db/models/nameservers/ns_record_dao.go
Normal 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
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_record_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_record_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
38
internal/db/models/nameservers/ns_record_model.go
Normal file
38
internal/db/models/nameservers/ns_record_model.go
Normal 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{}
|
||||
}
|
||||
11
internal/db/models/nameservers/ns_record_model_ext.go
Normal file
11
internal/db/models/nameservers/ns_record_model_ext.go
Normal 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
|
||||
}
|
||||
192
internal/db/models/nameservers/ns_route_dao.go
Normal file
192
internal/db/models/nameservers/ns_route_dao.go
Normal 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
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_route_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_route_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
32
internal/db/models/nameservers/ns_route_model.go
Normal file
32
internal/db/models/nameservers/ns_route_model.go
Normal 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{}
|
||||
}
|
||||
1
internal/db/models/nameservers/ns_route_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_route_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -125,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
|
||||
}
|
||||
@@ -402,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"
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -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{} // 描述
|
||||
|
||||
@@ -3,6 +3,7 @@ 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"
|
||||
@@ -35,7 +36,7 @@ func init() {
|
||||
}
|
||||
|
||||
// CreateLog 创建日志
|
||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, serverId int64, level string, tag string, description string, createdAt int64) error {
|
||||
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)
|
||||
|
||||
// 检查是否在重复最后一条,避免重复创建
|
||||
@@ -93,8 +94,10 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, ser
|
||||
query.Attr("nodeId", nodeId)
|
||||
} else {
|
||||
switch role {
|
||||
case NodeRoleNode:
|
||||
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 {
|
||||
@@ -138,8 +141,10 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
||||
query.Attr("nodeId", nodeId)
|
||||
} else {
|
||||
switch role {
|
||||
case NodeRoleNode:
|
||||
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 {
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
@@ -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
|
||||
|
||||
@@ -33,7 +33,7 @@ func init() {
|
||||
}
|
||||
|
||||
// CreateValue 创建值
|
||||
func (this *NodeValueDAO) CreateValue(tx *dbs.Tx, role NodeRole, nodeId int64, item string, valueJSON []byte, createdAt int64) error {
|
||||
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)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
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"
|
||||
@@ -13,7 +14,7 @@ func TestNodeValueDAO_CreateValue(t *testing.T) {
|
||||
m := maps.Map{
|
||||
"hello": "world12344",
|
||||
}
|
||||
err := dao.CreateValue(nil, NodeRoleNode, 1, "test", m.AsJSON(), time.Now().Unix())
|
||||
err := dao.CreateValue(nil, nodeconfigs.NodeRoleNode, 1, "test", m.AsJSON(), time.Now().Unix())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ const (
|
||||
LevelInfo = "info"
|
||||
LevelWarning = "warning"
|
||||
LevelError = "error"
|
||||
LevelSuccess = "success"
|
||||
|
||||
LogTypeAdmin = "admin"
|
||||
LogTypeUser = "user"
|
||||
|
||||
315
internal/db/models/ns_access_log_dao.go
Normal file
315
internal/db/models/ns_access_log_dao.go
Normal 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
|
||||
}
|
||||
6
internal/db/models/ns_access_log_dao_test.go
Normal file
6
internal/db/models/ns_access_log_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
26
internal/db/models/ns_access_log_model.go
Normal file
26
internal/db/models/ns_access_log_model.go
Normal 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{}
|
||||
}
|
||||
17
internal/db/models/ns_access_log_model_ext.go
Normal file
17
internal/db/models/ns_access_log_model_ext.go
Normal 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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -4,6 +4,7 @@ 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"
|
||||
@@ -142,7 +143,7 @@ func (this *UserNodeDAO) CreateUserNode(tx *dbs.Tx, name string, description str
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsclients
|
||||
package dnstypes
|
||||
|
||||
// Route 线路描述
|
||||
type Route struct {
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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",
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
})
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
246
internal/dnsclients/provider_local_edge_dns.go
Normal file
246
internal/dnsclients/provider_local_edge_dns.go
Normal 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 ""
|
||||
}
|
||||
160
internal/dnsclients/provider_local_edge_dns_test.go
Normal file
160
internal/dnsclients/provider_local_edge_dns_test.go
Normal 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())
|
||||
}
|
||||
59
internal/dnsclients/provider_user_edge_dns.go
Normal file
59
internal/dnsclients/provider_user_edge_dns.go
Normal 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 ""
|
||||
}
|
||||
@@ -1,40 +1,70 @@
|
||||
package dnsclients
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type ProviderType = string
|
||||
|
||||
// 服务商代号
|
||||
const (
|
||||
ProviderTypeDNSPod ProviderType = "dnspod"
|
||||
ProviderTypeAliDNS ProviderType = "alidns"
|
||||
ProviderTypeDNSCom ProviderType = "dnscom"
|
||||
ProviderTypeCloudFlare ProviderType = "cloudFlare"
|
||||
ProviderTypeCustomHTTP ProviderType = "customHTTP"
|
||||
ProviderTypeDNSPod ProviderType = "dnspod" // DNSPod
|
||||
ProviderTypeAliDNS ProviderType = "alidns" // 阿里云DNS
|
||||
ProviderTypeDNSCom ProviderType = "dnscom" // dns.com
|
||||
ProviderTypeCloudFlare ProviderType = "cloudFlare" // CloudFlare DNS
|
||||
ProviderTypeLocalEdgeDNS ProviderType = "localEdgeDNS" // 和当前系统集成的EdgeDNS
|
||||
ProviderTypeUserEdgeDNS ProviderType = "userEdgeDNS" // 通过API连接的EdgeDNS
|
||||
ProviderTypeCustomHTTP ProviderType = "customHTTP" // 自定义HTTP接口
|
||||
)
|
||||
|
||||
// AllProviderTypes 所有的服务商类型
|
||||
var AllProviderTypes = []maps.Map{
|
||||
{
|
||||
"name": "阿里云DNS",
|
||||
"code": ProviderTypeAliDNS,
|
||||
},
|
||||
{
|
||||
"name": "DNSPod",
|
||||
"code": ProviderTypeDNSPod,
|
||||
},
|
||||
/**{
|
||||
"name": "帝恩思DNS.COM",
|
||||
"code": ProviderTypeDNSCom,
|
||||
},**/
|
||||
{
|
||||
"name": "CloudFlare DNS",
|
||||
"code": ProviderTypeCloudFlare,
|
||||
},
|
||||
{
|
||||
"name": "自定义HTTP DNS",
|
||||
"code": ProviderTypeCustomHTTP,
|
||||
},
|
||||
// FindAllProviderTypes 所有的服务商类型
|
||||
func FindAllProviderTypes() []maps.Map {
|
||||
typeMaps := []maps.Map{
|
||||
{
|
||||
"name": "阿里云DNS",
|
||||
"code": ProviderTypeAliDNS,
|
||||
"description": "阿里云提供的DNS服务。",
|
||||
},
|
||||
{
|
||||
"name": "DNSPod",
|
||||
"code": ProviderTypeDNSPod,
|
||||
"description": "DNSPod提供的DNS服务。",
|
||||
},
|
||||
/**{
|
||||
"name": "帝恩思DNS.COM",
|
||||
"code": ProviderTypeDNSCom,
|
||||
"description": "DNS.com提供的DNS服务。",
|
||||
},**/
|
||||
{
|
||||
"name": "CloudFlare DNS",
|
||||
"code": ProviderTypeCloudFlare,
|
||||
"description": "CloudFlare提供的DNS服务。",
|
||||
},
|
||||
}
|
||||
|
||||
if teaconst.IsPlus {
|
||||
typeMaps = append(typeMaps, []maps.Map{
|
||||
{
|
||||
"name": "集成EdgeDNS",
|
||||
"code": ProviderTypeLocalEdgeDNS,
|
||||
"description": "当前企业版提供的DNS服务。",
|
||||
},
|
||||
// TODO 需要实现用户使用AccessId/AccessKey来连接DNS服务
|
||||
/**{
|
||||
"name": "用户EdgeDNS",
|
||||
"code": ProviderTypeUserEdgeDNS,
|
||||
"description": "通过API连接企业版提供的DNS服务。",
|
||||
},**/
|
||||
}...)
|
||||
}
|
||||
|
||||
typeMaps = append(typeMaps, maps.Map{
|
||||
"name": "自定义HTTP DNS",
|
||||
"code": ProviderTypeCustomHTTP,
|
||||
"description": "通过自定义的HTTP接口提供DNS服务。",
|
||||
})
|
||||
return typeMaps
|
||||
}
|
||||
|
||||
// FindProvider 查找服务商实例
|
||||
@@ -46,6 +76,10 @@ func FindProvider(providerType ProviderType) ProviderInterface {
|
||||
return &AliDNSProvider{}
|
||||
case ProviderTypeCloudFlare:
|
||||
return &CloudFlareProvider{}
|
||||
case ProviderTypeLocalEdgeDNS:
|
||||
return &LocalEdgeDNSProvider{}
|
||||
case ProviderTypeUserEdgeDNS:
|
||||
return &UserEdgeDNSProvider{}
|
||||
case ProviderTypeCustomHTTP:
|
||||
return &CustomHTTPProvider{}
|
||||
}
|
||||
@@ -54,7 +88,7 @@ func FindProvider(providerType ProviderType) ProviderInterface {
|
||||
|
||||
// FindProviderTypeName 查找服务商名称
|
||||
func FindProviderTypeName(providerType ProviderType) string {
|
||||
for _, t := range AllProviderTypes {
|
||||
for _, t := range FindAllProviderTypes() {
|
||||
if t.GetString("code") == providerType {
|
||||
return t.GetString("name")
|
||||
}
|
||||
|
||||
79
internal/installers/deploy_file.go
Normal file
79
internal/installers/deploy_file.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package installers
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// DeployFile 部署文件描述
|
||||
type DeployFile struct {
|
||||
OS string
|
||||
Arch string
|
||||
Version string
|
||||
Path string
|
||||
}
|
||||
|
||||
// Sum 计算概要
|
||||
func (this *DeployFile) Sum() (string, error) {
|
||||
fp, err := os.Open(this.Path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() {
|
||||
_ = fp.Close()
|
||||
}()
|
||||
|
||||
m := md5.New()
|
||||
buffer := make([]byte, 128*1024)
|
||||
for {
|
||||
n, err := fp.Read(buffer)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
_, err = m.Write(buffer[:n])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
sum := m.Sum(nil)
|
||||
return fmt.Sprintf("%x", sum), nil
|
||||
}
|
||||
|
||||
// Read 读取一个片段数据
|
||||
func (this *DeployFile) Read(offset int64) (data []byte, newOffset int64, err error) {
|
||||
fp, err := os.Open(this.Path)
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
defer func() {
|
||||
_ = fp.Close()
|
||||
}()
|
||||
|
||||
stat, err := fp.Stat()
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
if offset >= stat.Size() {
|
||||
return nil, offset, io.EOF
|
||||
}
|
||||
|
||||
_, err = fp.Seek(offset, io.SeekStart)
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
|
||||
buffer := make([]byte, 128*1024)
|
||||
n, err := fp.Read(buffer)
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
|
||||
return buffer[:n], offset + int64(n), nil
|
||||
}
|
||||
36
internal/installers/deploy_file_test.go
Normal file
36
internal/installers/deploy_file_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package installers
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeployFile_Sum(t *testing.T) {
|
||||
d := &DeployFile{Path: "deploy_test.txt"}
|
||||
sum, err := d.Sum()
|
||||
if err != nil {
|
||||
t.Log("err:", err)
|
||||
return
|
||||
}
|
||||
t.Log("sum:", sum)
|
||||
}
|
||||
|
||||
func TestDeployFile_Read(t *testing.T) {
|
||||
d := &DeployFile{Path: "deploy_test.txt"}
|
||||
|
||||
var offset int64
|
||||
for i := 0; i < 3; i++ {
|
||||
data, newOffset, err := d.Read(offset)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
t.Log("err: ", err)
|
||||
return
|
||||
}
|
||||
t.Log("offset:", newOffset, "data:", string(data))
|
||||
offset = newOffset
|
||||
}
|
||||
}
|
||||
@@ -9,28 +9,67 @@ import (
|
||||
|
||||
var SharedDeployManager = NewDeployManager()
|
||||
|
||||
type DeployFile struct {
|
||||
OS string
|
||||
Arch string
|
||||
Version string
|
||||
Path string
|
||||
}
|
||||
|
||||
type DeployManager struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
// NewDeployManager 节点部署文件管理器
|
||||
func NewDeployManager() *DeployManager {
|
||||
return &DeployManager{
|
||||
dir: Tea.Root + "/deploy",
|
||||
}
|
||||
}
|
||||
|
||||
// 加载所有文件
|
||||
func (this *DeployManager) LoadFiles() []*DeployFile {
|
||||
// LoadNodeFiles 加载所有边缘节点文件
|
||||
func (this *DeployManager) LoadNodeFiles() []*DeployFile {
|
||||
keyMap := map[string]*DeployFile{} // key => File
|
||||
|
||||
reg := regexp.MustCompile(`(\w+)-(\w+)-v([0-9.]+)\.zip`)
|
||||
reg := regexp.MustCompile(`^edge-node-(\w+)-(\w+)-v([0-9.]+)\.zip$`)
|
||||
for _, file := range files.NewFile(this.dir).List() {
|
||||
name := file.Name()
|
||||
if !reg.MatchString(name) {
|
||||
continue
|
||||
}
|
||||
matches := reg.FindStringSubmatch(name)
|
||||
osName := matches[1]
|
||||
arch := matches[2]
|
||||
version := matches[3]
|
||||
|
||||
key := osName + "_" + arch
|
||||
oldFile, ok := keyMap[key]
|
||||
if ok && stringutil.VersionCompare(oldFile.Version, version) > 0 {
|
||||
continue
|
||||
}
|
||||
keyMap[key] = &DeployFile{
|
||||
OS: osName,
|
||||
Arch: arch,
|
||||
Version: version,
|
||||
Path: file.Path(),
|
||||
}
|
||||
}
|
||||
|
||||
result := []*DeployFile{}
|
||||
for _, v := range keyMap {
|
||||
result = append(result, v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// FindNodeFile 查找特别平台的节点文件
|
||||
func (this *DeployManager) FindNodeFile(os string, arch string) *DeployFile {
|
||||
for _, file := range this.LoadNodeFiles() {
|
||||
if file.OS == os && file.Arch == arch {
|
||||
return file
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadNSNodeFiles 加载所有文件
|
||||
func (this *DeployManager) LoadNSNodeFiles() []*DeployFile {
|
||||
keyMap := map[string]*DeployFile{} // key => File
|
||||
|
||||
reg := regexp.MustCompile(`^edge-dns-(\w+)-(\w+)-v([0-9.]+)\.zip$`)
|
||||
for _, file := range files.NewFile(this.dir).List() {
|
||||
name := file.Name()
|
||||
if !reg.MatchString(name) {
|
||||
|
||||
@@ -2,8 +2,16 @@ package installers
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDeployManager_LoadFiles(t *testing.T) {
|
||||
files := NewDeployManager().LoadFiles()
|
||||
func TestDeployManager_LoadNodeFiles(t *testing.T) {
|
||||
files := NewDeployManager().LoadNodeFiles()
|
||||
for _, file := range files {
|
||||
t.Logf("%#v", file)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestDeployManager_LoadNSNodeFiles(t *testing.T) {
|
||||
files := NewDeployManager().LoadNSNodeFiles()
|
||||
for _, file := range files {
|
||||
t.Logf("%#v", file)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func TestQueue_InstallNode(t *testing.T) {
|
||||
queue := NewQueue()
|
||||
err := queue.InstallNodeProcess(16)
|
||||
err := queue.InstallNodeProcess(16, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -9,10 +9,8 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/events"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/go-yaml/yaml"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -188,85 +186,7 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
|
||||
logs.Println("[API_NODE]listening GRPC https://" + listener.Addr().String() + " ...")
|
||||
rpcServer = grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))
|
||||
}
|
||||
pb.RegisterAdminServiceServer(rpcServer, &services.AdminService{})
|
||||
pb.RegisterNodeGrantServiceServer(rpcServer, &services.NodeGrantService{})
|
||||
pb.RegisterServerServiceServer(rpcServer, &services.ServerService{})
|
||||
pb.RegisterNodeServiceServer(rpcServer, &services.NodeService{})
|
||||
pb.RegisterNodeClusterServiceServer(rpcServer, &services.NodeClusterService{})
|
||||
pb.RegisterNodeIPAddressServiceServer(rpcServer, &services.NodeIPAddressService{})
|
||||
pb.RegisterAPINodeServiceServer(rpcServer, &services.APINodeService{})
|
||||
pb.RegisterOriginServiceServer(rpcServer, &services.OriginService{})
|
||||
pb.RegisterHTTPWebServiceServer(rpcServer, &services.HTTPWebService{})
|
||||
pb.RegisterReverseProxyServiceServer(rpcServer, &services.ReverseProxyService{})
|
||||
pb.RegisterHTTPGzipServiceServer(rpcServer, &services.HTTPGzipService{})
|
||||
pb.RegisterHTTPHeaderPolicyServiceServer(rpcServer, &services.HTTPHeaderPolicyService{})
|
||||
pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{})
|
||||
pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{})
|
||||
pb.RegisterHTTPAccessLogPolicyServiceServer(rpcServer, &services.HTTPAccessLogPolicyService{})
|
||||
pb.RegisterHTTPCachePolicyServiceServer(rpcServer, &services.HTTPCachePolicyService{})
|
||||
pb.RegisterHTTPFirewallPolicyServiceServer(rpcServer, &services.HTTPFirewallPolicyService{})
|
||||
pb.RegisterHTTPLocationServiceServer(rpcServer, &services.HTTPLocationService{})
|
||||
pb.RegisterHTTPWebsocketServiceServer(rpcServer, &services.HTTPWebsocketService{})
|
||||
pb.RegisterHTTPRewriteRuleServiceServer(rpcServer, &services.HTTPRewriteRuleService{})
|
||||
pb.RegisterSSLCertServiceServer(rpcServer, &services.SSLCertService{})
|
||||
pb.RegisterSSLPolicyServiceServer(rpcServer, &services.SSLPolicyService{})
|
||||
pb.RegisterSysSettingServiceServer(rpcServer, &services.SysSettingService{})
|
||||
pb.RegisterHTTPFirewallRuleGroupServiceServer(rpcServer, &services.HTTPFirewallRuleGroupService{})
|
||||
pb.RegisterHTTPFirewallRuleSetServiceServer(rpcServer, &services.HTTPFirewallRuleSetService{})
|
||||
pb.RegisterDBNodeServiceServer(rpcServer, &services.DBNodeService{})
|
||||
pb.RegisterNodeLogServiceServer(rpcServer, &services.NodeLogService{})
|
||||
pb.RegisterHTTPAccessLogServiceServer(rpcServer, &services.HTTPAccessLogService{})
|
||||
pb.RegisterMessageServiceServer(rpcServer, &services.MessageService{})
|
||||
pb.RegisterMessageRecipientServiceServer(rpcServer, &services.MessageRecipientService{})
|
||||
pb.RegisterMessageReceiverServiceServer(rpcServer, &services.MessageReceiverService{})
|
||||
pb.RegisterMessageMediaServiceServer(rpcServer, &services.MessageMediaService{})
|
||||
pb.RegisterMessageRecipientGroupServiceServer(rpcServer, &services.MessageRecipientGroupService{})
|
||||
pb.RegisterMessageMediaInstanceServiceServer(rpcServer, &services.MessageMediaInstanceService{})
|
||||
pb.RegisterMessageTaskServiceServer(rpcServer, &services.MessageTaskService{})
|
||||
pb.RegisterMessageTaskLogServiceServer(rpcServer, &services.MessageTaskLogService{})
|
||||
pb.RegisterNodeGroupServiceServer(rpcServer, &services.NodeGroupService{})
|
||||
pb.RegisterNodeRegionServiceServer(rpcServer, &services.NodeRegionService{})
|
||||
pb.RegisterNodePriceItemServiceServer(rpcServer, &services.NodePriceItemService{})
|
||||
pb.RegisterServerGroupServiceServer(rpcServer, &services.ServerGroupService{})
|
||||
pb.RegisterIPLibraryServiceServer(rpcServer, &services.IPLibraryService{})
|
||||
pb.RegisterFileChunkServiceServer(rpcServer, &services.FileChunkService{})
|
||||
pb.RegisterFileServiceServer(rpcServer, &services.FileService{})
|
||||
pb.RegisterRegionCountryServiceServer(rpcServer, &services.RegionCountryService{})
|
||||
pb.RegisterRegionProvinceServiceServer(rpcServer, &services.RegionProvinceService{})
|
||||
pb.RegisterIPListServiceServer(rpcServer, &services.IPListService{})
|
||||
pb.RegisterIPItemServiceServer(rpcServer, &services.IPItemService{})
|
||||
pb.RegisterLogServiceServer(rpcServer, &services.LogService{})
|
||||
pb.RegisterDNSProviderServiceServer(rpcServer, &services.DNSProviderService{})
|
||||
pb.RegisterDNSDomainServiceServer(rpcServer, &services.DNSDomainService{})
|
||||
pb.RegisterDNSServiceServer(rpcServer, &services.DNSService{})
|
||||
pb.RegisterACMEUserServiceServer(rpcServer, &services.ACMEUserService{})
|
||||
pb.RegisterACMETaskServiceServer(rpcServer, &services.ACMETaskService{})
|
||||
pb.RegisterACMEAuthenticationServiceServer(rpcServer, &services.ACMEAuthenticationService{})
|
||||
pb.RegisterUserServiceServer(rpcServer, &services.UserService{})
|
||||
pb.RegisterServerDailyStatServiceServer(rpcServer, &services.ServerDailyStatService{})
|
||||
pb.RegisterUserBillServiceServer(rpcServer, &services.UserBillService{})
|
||||
pb.RegisterUserNodeServiceServer(rpcServer, &services.UserNodeService{})
|
||||
pb.RegisterLoginServiceServer(rpcServer, &services.LoginService{})
|
||||
pb.RegisterUserAccessKeyServiceServer(rpcServer, &services.UserAccessKeyService{})
|
||||
pb.RegisterSysLockerServiceServer(rpcServer, &services.SysLockerService{})
|
||||
pb.RegisterNodeTaskServiceServer(rpcServer, &services.NodeTaskService{})
|
||||
pb.RegisterNodeValueServiceServer(rpcServer, &services.NodeValueService{})
|
||||
pb.RegisterDBServiceServer(rpcServer, &services.DBService{})
|
||||
pb.RegisterServerRegionCityMonthlyStatServiceServer(rpcServer, &services.ServerRegionCityMonthlyStatService{})
|
||||
pb.RegisterServerRegionCountryMonthlyStatServiceServer(rpcServer, &services.ServerRegionCountryMonthlyStatService{})
|
||||
pb.RegisterServerRegionProvinceMonthlyStatServiceServer(rpcServer, &services.ServerRegionProvinceMonthlyStatService{})
|
||||
pb.RegisterServerRegionProviderMonthlyStatServiceServer(rpcServer, &services.ServerRegionProviderMonthlyStatService{})
|
||||
pb.RegisterServerClientSystemMonthlyStatServiceServer(rpcServer, &services.ServerClientSystemMonthlyStatService{})
|
||||
pb.RegisterServerClientBrowserMonthlyStatServiceServer(rpcServer, &services.ServerClientBrowserMonthlyStatService{})
|
||||
pb.RegisterServerHTTPFirewallDailyStatServiceServer(rpcServer, &services.ServerHTTPFirewallDailyStatService{})
|
||||
pb.RegisterDNSTaskServiceServer(rpcServer, &services.DNSTaskService{})
|
||||
pb.RegisterNodeClusterFirewallActionServiceServer(rpcServer, &services.NodeClusterFirewallActionService{})
|
||||
pb.RegisterMonitorNodeServiceServer(rpcServer, &services.MonitorNodeService{})
|
||||
pb.RegisterAuthorityKeyServiceServer(rpcServer, &services.AuthorityKeyService{})
|
||||
pb.RegisterAuthorityNodeServiceServer(rpcServer, &services.AuthorityNodeService{})
|
||||
pb.RegisterLatestItemServiceServer(rpcServer, &services.LatestItemService{})
|
||||
pb.RegisterNodeThresholdServiceServer(rpcServer, &services.NodeThresholdService{})
|
||||
pb.RegisterHTTPFastcgiServiceServer(rpcServer, &services.HTTPFastcgiService{})
|
||||
this.registerServices(rpcServer)
|
||||
err := rpcServer.Serve(listener)
|
||||
if err != nil {
|
||||
return errors.New("[API_NODE]start rpc failed: " + err.Error())
|
||||
|
||||
99
internal/nodes/api_node_services.go
Normal file
99
internal/nodes/api_node_services.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services/nameservers"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// 注册服务
|
||||
func (this *APINode) registerServices(server *grpc.Server) {
|
||||
pb.RegisterAdminServiceServer(server, &services.AdminService{})
|
||||
pb.RegisterNodeGrantServiceServer(server, &services.NodeGrantService{})
|
||||
pb.RegisterServerServiceServer(server, &services.ServerService{})
|
||||
pb.RegisterNodeServiceServer(server, &services.NodeService{})
|
||||
pb.RegisterNodeClusterServiceServer(server, &services.NodeClusterService{})
|
||||
pb.RegisterNodeIPAddressServiceServer(server, &services.NodeIPAddressService{})
|
||||
pb.RegisterAPINodeServiceServer(server, &services.APINodeService{})
|
||||
pb.RegisterOriginServiceServer(server, &services.OriginService{})
|
||||
pb.RegisterHTTPWebServiceServer(server, &services.HTTPWebService{})
|
||||
pb.RegisterReverseProxyServiceServer(server, &services.ReverseProxyService{})
|
||||
pb.RegisterHTTPGzipServiceServer(server, &services.HTTPGzipService{})
|
||||
pb.RegisterHTTPHeaderPolicyServiceServer(server, &services.HTTPHeaderPolicyService{})
|
||||
pb.RegisterHTTPHeaderServiceServer(server, &services.HTTPHeaderService{})
|
||||
pb.RegisterHTTPPageServiceServer(server, &services.HTTPPageService{})
|
||||
pb.RegisterHTTPAccessLogPolicyServiceServer(server, &services.HTTPAccessLogPolicyService{})
|
||||
pb.RegisterHTTPCachePolicyServiceServer(server, &services.HTTPCachePolicyService{})
|
||||
pb.RegisterHTTPFirewallPolicyServiceServer(server, &services.HTTPFirewallPolicyService{})
|
||||
pb.RegisterHTTPLocationServiceServer(server, &services.HTTPLocationService{})
|
||||
pb.RegisterHTTPWebsocketServiceServer(server, &services.HTTPWebsocketService{})
|
||||
pb.RegisterHTTPRewriteRuleServiceServer(server, &services.HTTPRewriteRuleService{})
|
||||
pb.RegisterSSLCertServiceServer(server, &services.SSLCertService{})
|
||||
pb.RegisterSSLPolicyServiceServer(server, &services.SSLPolicyService{})
|
||||
pb.RegisterSysSettingServiceServer(server, &services.SysSettingService{})
|
||||
pb.RegisterHTTPFirewallRuleGroupServiceServer(server, &services.HTTPFirewallRuleGroupService{})
|
||||
pb.RegisterHTTPFirewallRuleSetServiceServer(server, &services.HTTPFirewallRuleSetService{})
|
||||
pb.RegisterDBNodeServiceServer(server, &services.DBNodeService{})
|
||||
pb.RegisterNodeLogServiceServer(server, &services.NodeLogService{})
|
||||
pb.RegisterHTTPAccessLogServiceServer(server, &services.HTTPAccessLogService{})
|
||||
pb.RegisterMessageServiceServer(server, &services.MessageService{})
|
||||
pb.RegisterMessageRecipientServiceServer(server, &services.MessageRecipientService{})
|
||||
pb.RegisterMessageReceiverServiceServer(server, &services.MessageReceiverService{})
|
||||
pb.RegisterMessageMediaServiceServer(server, &services.MessageMediaService{})
|
||||
pb.RegisterMessageRecipientGroupServiceServer(server, &services.MessageRecipientGroupService{})
|
||||
pb.RegisterMessageMediaInstanceServiceServer(server, &services.MessageMediaInstanceService{})
|
||||
pb.RegisterMessageTaskServiceServer(server, &services.MessageTaskService{})
|
||||
pb.RegisterMessageTaskLogServiceServer(server, &services.MessageTaskLogService{})
|
||||
pb.RegisterNodeGroupServiceServer(server, &services.NodeGroupService{})
|
||||
pb.RegisterNodeRegionServiceServer(server, &services.NodeRegionService{})
|
||||
pb.RegisterNodePriceItemServiceServer(server, &services.NodePriceItemService{})
|
||||
pb.RegisterServerGroupServiceServer(server, &services.ServerGroupService{})
|
||||
pb.RegisterIPLibraryServiceServer(server, &services.IPLibraryService{})
|
||||
pb.RegisterFileChunkServiceServer(server, &services.FileChunkService{})
|
||||
pb.RegisterFileServiceServer(server, &services.FileService{})
|
||||
pb.RegisterRegionCountryServiceServer(server, &services.RegionCountryService{})
|
||||
pb.RegisterRegionProvinceServiceServer(server, &services.RegionProvinceService{})
|
||||
pb.RegisterIPListServiceServer(server, &services.IPListService{})
|
||||
pb.RegisterIPItemServiceServer(server, &services.IPItemService{})
|
||||
pb.RegisterLogServiceServer(server, &services.LogService{})
|
||||
pb.RegisterDNSProviderServiceServer(server, &services.DNSProviderService{})
|
||||
pb.RegisterDNSDomainServiceServer(server, &services.DNSDomainService{})
|
||||
pb.RegisterDNSServiceServer(server, &services.DNSService{})
|
||||
pb.RegisterACMEUserServiceServer(server, &services.ACMEUserService{})
|
||||
pb.RegisterACMETaskServiceServer(server, &services.ACMETaskService{})
|
||||
pb.RegisterACMEAuthenticationServiceServer(server, &services.ACMEAuthenticationService{})
|
||||
pb.RegisterUserServiceServer(server, &services.UserService{})
|
||||
pb.RegisterServerDailyStatServiceServer(server, &services.ServerDailyStatService{})
|
||||
pb.RegisterUserBillServiceServer(server, &services.UserBillService{})
|
||||
pb.RegisterUserNodeServiceServer(server, &services.UserNodeService{})
|
||||
pb.RegisterLoginServiceServer(server, &services.LoginService{})
|
||||
pb.RegisterUserAccessKeyServiceServer(server, &services.UserAccessKeyService{})
|
||||
pb.RegisterSysLockerServiceServer(server, &services.SysLockerService{})
|
||||
pb.RegisterNodeTaskServiceServer(server, &services.NodeTaskService{})
|
||||
pb.RegisterNodeValueServiceServer(server, &services.NodeValueService{})
|
||||
pb.RegisterDBServiceServer(server, &services.DBService{})
|
||||
pb.RegisterServerRegionCityMonthlyStatServiceServer(server, &services.ServerRegionCityMonthlyStatService{})
|
||||
pb.RegisterServerRegionCountryMonthlyStatServiceServer(server, &services.ServerRegionCountryMonthlyStatService{})
|
||||
pb.RegisterServerRegionProvinceMonthlyStatServiceServer(server, &services.ServerRegionProvinceMonthlyStatService{})
|
||||
pb.RegisterServerRegionProviderMonthlyStatServiceServer(server, &services.ServerRegionProviderMonthlyStatService{})
|
||||
pb.RegisterServerClientSystemMonthlyStatServiceServer(server, &services.ServerClientSystemMonthlyStatService{})
|
||||
pb.RegisterServerClientBrowserMonthlyStatServiceServer(server, &services.ServerClientBrowserMonthlyStatService{})
|
||||
pb.RegisterServerHTTPFirewallDailyStatServiceServer(server, &services.ServerHTTPFirewallDailyStatService{})
|
||||
pb.RegisterDNSTaskServiceServer(server, &services.DNSTaskService{})
|
||||
pb.RegisterNodeClusterFirewallActionServiceServer(server, &services.NodeClusterFirewallActionService{})
|
||||
pb.RegisterMonitorNodeServiceServer(server, &services.MonitorNodeService{})
|
||||
pb.RegisterAuthorityKeyServiceServer(server, &services.AuthorityKeyService{})
|
||||
pb.RegisterAuthorityNodeServiceServer(server, &services.AuthorityNodeService{})
|
||||
pb.RegisterLatestItemServiceServer(server, &services.LatestItemService{})
|
||||
pb.RegisterNodeThresholdServiceServer(server, &services.NodeThresholdService{})
|
||||
pb.RegisterHTTPFastcgiServiceServer(server, &services.HTTPFastcgiService{})
|
||||
pb.RegisterNSClusterServiceServer(server, &nameservers.NSClusterService{})
|
||||
pb.RegisterNSNodeServiceServer(server, &nameservers.NSNodeService{})
|
||||
pb.RegisterNSDomainServiceServer(server, &nameservers.NSDomainService{})
|
||||
pb.RegisterNSRecordServiceServer(server, &nameservers.NSRecordService{})
|
||||
pb.RegisterNSRouteServiceServer(server, &nameservers.NSRouteService{})
|
||||
pb.RegisterNSAccessLogServiceServer(server, &nameservers.NSAccessLogService{})
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"time"
|
||||
@@ -99,7 +100,7 @@ Loop:
|
||||
for {
|
||||
select {
|
||||
case log := <-logChan:
|
||||
err := models.SharedNodeLogDAO.CreateLog(nil, models.NodeRoleAPI, log.NodeId, 0, log.Level, log.Tag, log.Description, log.CreatedAt)
|
||||
err := models.SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleAPI, log.NodeId, 0, log.Level, log.Tag, log.Description, log.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
102
internal/rpc/services/nameservers/service_ns_access_log.go
Normal file
102
internal/rpc/services/nameservers/service_ns_access_log.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// NSAccessLogService 访问日志相关服务
|
||||
type NSAccessLogService struct {
|
||||
services.BaseService
|
||||
}
|
||||
|
||||
// CreateNSAccessLogs 创建访问日志
|
||||
func (this *NSAccessLogService) CreateNSAccessLogs(ctx context.Context, req *pb.CreateNSAccessLogsRequest) (*pb.CreateNSAccessLogsResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeDNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(req.NsAccessLogs) == 0 {
|
||||
return &pb.CreateNSAccessLogsResponse{}, nil
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = models.SharedNSAccessLogDAO.CreateNSAccessLogs(tx, req.NsAccessLogs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.CreateNSAccessLogsResponse{}, nil
|
||||
}
|
||||
|
||||
// ListNSAccessLogs 列出单页访问日志
|
||||
func (this *NSAccessLogService) ListNSAccessLogs(ctx context.Context, req *pb.ListNSAccessLogsRequest) (*pb.ListNSAccessLogsResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
// 检查服务ID
|
||||
if userId > 0 {
|
||||
// TODO
|
||||
}
|
||||
|
||||
accessLogs, requestId, hasMore, err := models.SharedNSAccessLogDAO.ListAccessLogs(tx, req.RequestId, req.Size, req.Day, req.NsNodeId, req.NsDomainId, req.NsRecordId, req.Keyword, req.Reverse)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []*pb.NSAccessLog{}
|
||||
for _, accessLog := range accessLogs {
|
||||
a, err := accessLog.ToPB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, a)
|
||||
}
|
||||
|
||||
return &pb.ListNSAccessLogsResponse{
|
||||
NsAccessLogs: result,
|
||||
HasMore: hasMore,
|
||||
RequestId: requestId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FindNSAccessLog 查找单个日志
|
||||
func (this *NSAccessLogService) FindNSAccessLog(ctx context.Context, req *pb.FindNSAccessLogRequest) (*pb.FindNSAccessLogResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
accessLog, err := models.SharedNSAccessLogDAO.FindAccessLogWithRequestId(tx, req.RequestId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if accessLog == nil {
|
||||
return &pb.FindNSAccessLogResponse{NsAccessLog: nil}, nil
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
if userId > 0 {
|
||||
// TODO
|
||||
}
|
||||
|
||||
a, err := accessLog.ToPB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.FindNSAccessLogResponse{NsAccessLog: a}, nil
|
||||
}
|
||||
169
internal/rpc/services/nameservers/service_ns_cluster.go
Normal file
169
internal/rpc/services/nameservers/service_ns_cluster.go
Normal file
@@ -0,0 +1,169 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// NSClusterService 域名服务集群相关服务
|
||||
type NSClusterService struct {
|
||||
services.BaseService
|
||||
}
|
||||
|
||||
// CreateNSCluster 创建集群
|
||||
func (this *NSClusterService) CreateNSCluster(ctx context.Context, req *pb.CreateNSClusterRequest) (*pb.CreateNSClusterResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
clusterId, err := nameservers.SharedNSClusterDAO.CreateCluster(tx, req.Name, req.AccessLogJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateNSClusterResponse{NsClusterId: clusterId}, nil
|
||||
}
|
||||
|
||||
// UpdateNSCluster 修改集群
|
||||
func (this *NSClusterService) UpdateNSCluster(ctx context.Context, req *pb.UpdateNSClusterRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSClusterDAO.UpdateCluster(tx, req.NsClusterId, req.Name, req.IsOn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindNSClusterAccessLog 查找集群访问日志配置
|
||||
func (this *NSClusterService) FindNSClusterAccessLog(ctx context.Context, req *pb.FindNSClusterAccessLogRequest) (*pb.FindNSClusterAccessLogResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
accessLogJSON, err := nameservers.SharedNSClusterDAO.FindClusterAccessLog(tx, req.NsClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.FindNSClusterAccessLogResponse{AccessLogJSON: accessLogJSON}, nil
|
||||
}
|
||||
|
||||
// UpdateNSClusterAccessLog 修改集群访问日志配置
|
||||
func (this *NSClusterService) UpdateNSClusterAccessLog(ctx context.Context, req *pb.UpdateNSClusterAccessLogRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSClusterDAO.UpdateClusterAccessLog(tx, req.NsClusterId, req.AccessLogJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// DeleteNSCluster 删除集群
|
||||
func (this *NSClusterService) DeleteNSCluster(ctx context.Context, req *pb.DeleteNSCluster) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSClusterDAO.DisableNSCluster(tx, req.NsClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindEnabledNSCluster 查找单个可用集群信息
|
||||
func (this *NSClusterService) FindEnabledNSCluster(ctx context.Context, req *pb.FindEnabledNSClusterRequest) (*pb.FindEnabledNSClusterResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, req.NsClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cluster == nil {
|
||||
return &pb.FindEnabledNSClusterResponse{NsCluster: nil}, nil
|
||||
}
|
||||
return &pb.FindEnabledNSClusterResponse{NsCluster: &pb.NSCluster{
|
||||
Id: int64(cluster.Id),
|
||||
IsOn: cluster.IsOn == 1,
|
||||
Name: cluster.Name,
|
||||
InstallDir: cluster.InstallDir,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// CountAllEnabledNSClusters 计算所有可用集群的数量
|
||||
func (this *NSClusterService) CountAllEnabledNSClusters(ctx context.Context, req *pb.CountAllEnabledNSClustersRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
count, err := nameservers.SharedNSClusterDAO.CountAllEnabledClusters(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListEnabledNSClusters 列出单页可用集群
|
||||
func (this *NSClusterService) ListEnabledNSClusters(ctx context.Context, req *pb.ListEnabledNSClustersRequest) (*pb.ListEnabledNSClustersResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
clusters, err := nameservers.SharedNSClusterDAO.ListEnabledClusters(tx, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbClusters = []*pb.NSCluster{}
|
||||
for _, cluster := range clusters {
|
||||
pbClusters = append(pbClusters, &pb.NSCluster{
|
||||
Id: int64(cluster.Id),
|
||||
IsOn: cluster.IsOn == 1,
|
||||
Name: cluster.Name,
|
||||
InstallDir: cluster.InstallDir,
|
||||
})
|
||||
}
|
||||
return &pb.ListEnabledNSClustersResponse{NsClusters: pbClusters}, nil
|
||||
}
|
||||
|
||||
// FindAllEnabledNSClusters 查找所有可用集群
|
||||
func (this *NSClusterService) FindAllEnabledNSClusters(ctx context.Context, req *pb.FindAllEnabledNSClustersRequest) (*pb.FindAllEnabledNSClustersResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
clusters, err := nameservers.SharedNSClusterDAO.FindAllEnabledClusters(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbClusters = []*pb.NSCluster{}
|
||||
for _, cluster := range clusters {
|
||||
pbClusters = append(pbClusters, &pb.NSCluster{
|
||||
Id: int64(cluster.Id),
|
||||
IsOn: cluster.IsOn == 1,
|
||||
Name: cluster.Name,
|
||||
InstallDir: cluster.InstallDir,
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledNSClustersResponse{NsClusters: pbClusters}, nil
|
||||
}
|
||||
221
internal/rpc/services/nameservers/service_ns_domain.go
Normal file
221
internal/rpc/services/nameservers/service_ns_domain.go
Normal file
@@ -0,0 +1,221 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// NSDomainService 域名相关服务
|
||||
type NSDomainService struct {
|
||||
services.BaseService
|
||||
}
|
||||
|
||||
// CreateNSDomain 创建域名
|
||||
func (this *NSDomainService) CreateNSDomain(ctx context.Context, req *pb.CreateNSDomainRequest) (*pb.CreateNSDomainResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
domainId, err := nameservers.SharedNSDomainDAO.CreateDomain(tx, req.NsClusterId, req.UserId, req.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateNSDomainResponse{NsDomainId: domainId}, nil
|
||||
}
|
||||
|
||||
// UpdateNSDomain 修改域名
|
||||
func (this *NSDomainService) UpdateNSDomain(ctx context.Context, req *pb.UpdateNSDomainRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSDomainDAO.UpdateDomain(tx, req.NsDomainId, req.NsClusterId, req.UserId, req.IsOn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// DeleteNSDomain 删除域名
|
||||
func (this *NSDomainService) DeleteNSDomain(ctx context.Context, req *pb.DeleteNSDomainRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSDomainDAO.DisableNSDomain(tx, req.NsDomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindEnabledNSDomain 查找单个域名
|
||||
func (this *NSDomainService) FindEnabledNSDomain(ctx context.Context, req *pb.FindEnabledNSDomainRequest) (*pb.FindEnabledNSDomainResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, req.NsDomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domain == nil {
|
||||
return &pb.FindEnabledNSDomainResponse{NsDomain: nil}, nil
|
||||
}
|
||||
|
||||
// 集群
|
||||
cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(domain.ClusterId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cluster == nil {
|
||||
return &pb.FindEnabledNSDomainResponse{NsDomain: nil}, nil
|
||||
}
|
||||
|
||||
// 用户
|
||||
var pbUser *pb.User
|
||||
if domain.UserId > 0 {
|
||||
user, err := models.SharedUserDAO.FindEnabledUser(tx, int64(domain.UserId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user == nil {
|
||||
return &pb.FindEnabledNSDomainResponse{NsDomain: nil}, nil
|
||||
}
|
||||
pbUser = &pb.User{
|
||||
Id: int64(user.Id),
|
||||
Username: user.Username,
|
||||
Fullname: user.Fullname,
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.FindEnabledNSDomainResponse{
|
||||
NsDomain: &pb.NSDomain{
|
||||
Id: int64(domain.Id),
|
||||
Name: domain.Name,
|
||||
IsOn: domain.IsOn == 1,
|
||||
CreatedAt: int64(domain.CreatedAt),
|
||||
NsCluster: &pb.NSCluster{
|
||||
Id: int64(cluster.Id),
|
||||
IsOn: cluster.IsOn == 1,
|
||||
Name: cluster.Name,
|
||||
},
|
||||
User: pbUser,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CountAllEnabledNSDomains 计算域名数量
|
||||
func (this *NSDomainService) CountAllEnabledNSDomains(ctx context.Context, req *pb.CountAllEnabledNSDomainsRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
count, err := nameservers.SharedNSDomainDAO.CountAllEnabledDomains(tx, req.NsClusterId, req.UserId, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListEnabledNSDomains 列出单页域名
|
||||
func (this *NSDomainService) ListEnabledNSDomains(ctx context.Context, req *pb.ListEnabledNSDomainsRequest) (*pb.ListEnabledNSDomainsResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
domains, err := nameservers.SharedNSDomainDAO.ListEnabledDomains(tx, req.NsClusterId, req.UserId, req.Keyword, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbDomains := []*pb.NSDomain{}
|
||||
for _, domain := range domains {
|
||||
// 集群
|
||||
cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(domain.ClusterId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cluster == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 用户
|
||||
var pbUser *pb.User
|
||||
if domain.UserId > 0 {
|
||||
user, err := models.SharedUserDAO.FindEnabledUser(tx, int64(domain.UserId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user == nil {
|
||||
continue
|
||||
}
|
||||
pbUser = &pb.User{
|
||||
Id: int64(user.Id),
|
||||
Username: user.Username,
|
||||
Fullname: user.Fullname,
|
||||
}
|
||||
}
|
||||
|
||||
pbDomains = append(pbDomains, &pb.NSDomain{
|
||||
Id: int64(domain.Id),
|
||||
Name: domain.Name,
|
||||
IsOn: domain.IsOn == 1,
|
||||
CreatedAt: int64(domain.CreatedAt),
|
||||
NsCluster: &pb.NSCluster{
|
||||
Id: int64(cluster.Id),
|
||||
IsOn: cluster.IsOn == 1,
|
||||
Name: cluster.Name,
|
||||
},
|
||||
User: pbUser,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.ListEnabledNSDomainsResponse{NsDomains: pbDomains}, nil
|
||||
}
|
||||
|
||||
// ListNSDomainsAfterVersion 根据版本列出一组域名
|
||||
func (this *NSDomainService) ListNSDomainsAfterVersion(ctx context.Context, req *pb.ListNSDomainsAfterVersionRequest) (*pb.ListNSDomainsAfterVersionResponse, error) {
|
||||
_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 集群ID
|
||||
var tx = this.NullTx()
|
||||
domains, err := nameservers.SharedNSDomainDAO.ListDomainsAfterVersion(tx, req.Version, 2000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pbDomains []*pb.NSDomain
|
||||
for _, domain := range domains {
|
||||
pbDomains = append(pbDomains, &pb.NSDomain{
|
||||
Id: int64(domain.Id),
|
||||
Name: domain.Name,
|
||||
IsOn: domain.IsOn == 1,
|
||||
IsDeleted: domain.State == nameservers.NSDomainStateDisabled,
|
||||
Version: int64(domain.Version),
|
||||
NsCluster: &pb.NSCluster{Id: int64(domain.ClusterId)},
|
||||
User: nil,
|
||||
})
|
||||
}
|
||||
return &pb.ListNSDomainsAfterVersionResponse{NsDomains: pbDomains}, nil
|
||||
}
|
||||
387
internal/rpc/services/nameservers/service_ns_node.go
Normal file
387
internal/rpc/services/nameservers/service_ns_node.go
Normal file
@@ -0,0 +1,387 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/installers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
)
|
||||
|
||||
// NSNodeService 域名服务器节点服务
|
||||
type NSNodeService struct {
|
||||
services.BaseService
|
||||
}
|
||||
|
||||
// FindAllEnabledNSNodesWithNSClusterId 根据集群查找所有节点
|
||||
func (this *NSNodeService) FindAllEnabledNSNodesWithNSClusterId(ctx context.Context, req *pb.FindAllEnabledNSNodesWithNSClusterIdRequest) (*pb.FindAllEnabledNSNodesWithNSClusterIdResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
nodes, err := nameservers.SharedNSNodeDAO.FindAllEnabledNodesWithClusterId(tx, req.NsClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pbNodes := []*pb.NSNode{}
|
||||
for _, node := range nodes {
|
||||
pbNodes = append(pbNodes, &pb.NSNode{
|
||||
Id: int64(node.Id),
|
||||
Name: node.Name,
|
||||
IsOn: node.IsOn == 1,
|
||||
UniqueId: node.UniqueId,
|
||||
Secret: node.Secret,
|
||||
IsInstalled: node.IsInstalled == 1,
|
||||
InstallDir: node.InstallDir,
|
||||
IsUp: node.IsUp == 1,
|
||||
NsCluster: nil,
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledNSNodesWithNSClusterIdResponse{NsNodes: pbNodes}, nil
|
||||
}
|
||||
|
||||
// CountAllEnabledNSNodes 所有可用的节点数量
|
||||
func (this *NSNodeService) CountAllEnabledNSNodes(ctx context.Context, req *pb.CountAllEnabledNSNodesRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
count, err := nameservers.SharedNSNodeDAO.CountAllEnabledNodes(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// CountAllEnabledNSNodesMatch 计算匹配的节点数量
|
||||
func (this *NSNodeService) CountAllEnabledNSNodesMatch(ctx context.Context, req *pb.CountAllEnabledNSNodesMatchRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
count, err := nameservers.SharedNSNodeDAO.CountAllEnabledNodesMatch(tx, req.NsClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListEnabledNSNodesMatch 列出单页节点
|
||||
func (this *NSNodeService) ListEnabledNSNodesMatch(ctx context.Context, req *pb.ListEnabledNSNodesMatchRequest) (*pb.ListEnabledNSNodesMatchResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
nodes, err := nameservers.SharedNSNodeDAO.ListAllEnabledNodesMatch(tx, req.NsClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword, req.Offset, req.Size)
|
||||
pbNodes := []*pb.NSNode{}
|
||||
for _, node := range nodes {
|
||||
// 安装信息
|
||||
installStatus, err := node.DecodeInstallStatus()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
installStatusResult := &pb.NodeInstallStatus{}
|
||||
if installStatus != nil {
|
||||
installStatusResult = &pb.NodeInstallStatus{
|
||||
IsRunning: installStatus.IsRunning,
|
||||
IsFinished: installStatus.IsFinished,
|
||||
IsOk: installStatus.IsOk,
|
||||
Error: installStatus.Error,
|
||||
ErrorCode: installStatus.ErrorCode,
|
||||
UpdatedAt: installStatus.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
pbNodes = append(pbNodes, &pb.NSNode{
|
||||
Id: int64(node.Id),
|
||||
Name: node.Name,
|
||||
IsOn: node.IsOn == 1,
|
||||
UniqueId: node.UniqueId,
|
||||
Secret: node.Secret,
|
||||
IsInstalled: node.IsInstalled == 1,
|
||||
InstallDir: node.InstallDir,
|
||||
IsUp: node.IsUp == 1,
|
||||
StatusJSON: []byte(node.Status),
|
||||
InstallStatus: installStatusResult,
|
||||
NsCluster: nil,
|
||||
})
|
||||
}
|
||||
return &pb.ListEnabledNSNodesMatchResponse{NsNodes: pbNodes}, nil
|
||||
}
|
||||
|
||||
// CountAllUpgradeNSNodesWithNSClusterId 计算需要升级的节点数量
|
||||
func (this *NSNodeService) CountAllUpgradeNSNodesWithNSClusterId(ctx context.Context, req *pb.CountAllUpgradeNSNodesWithNSClusterIdRequest) (*pb.RPCCountResponse, error) {
|
||||
// 校验请求
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
deployFiles := installers.SharedDeployManager.LoadNSNodeFiles()
|
||||
total := int64(0)
|
||||
for _, deployFile := range deployFiles {
|
||||
count, err := nameservers.SharedNSNodeDAO.CountAllLowerVersionNodesWithClusterId(tx, req.NsClusterId, deployFile.OS, deployFile.Arch, deployFile.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
total += count
|
||||
}
|
||||
|
||||
return this.SuccessCount(total)
|
||||
}
|
||||
|
||||
// CreateNSNode 创建节点
|
||||
func (this *NSNodeService) CreateNSNode(ctx context.Context, req *pb.CreateNSNodeRequest) (*pb.CreateNSNodeResponse, error) {
|
||||
adminId, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
nodeId, err := nameservers.SharedNSNodeDAO.CreateNode(tx, adminId, req.Name, req.NodeClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.CreateNSNodeResponse{
|
||||
NsNodeId: nodeId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteNSNode 删除节点
|
||||
func (this *NSNodeService) DeleteNSNode(ctx context.Context, req *pb.DeleteNSNodeRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = nameservers.SharedNSNodeDAO.DisableNSNode(tx, req.NsNodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindEnabledNSNode 查询单个节点信息
|
||||
func (this *NSNodeService) FindEnabledNSNode(ctx context.Context, req *pb.FindEnabledNSNodeRequest) (*pb.FindEnabledNSNodeResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
node, err := nameservers.SharedNSNodeDAO.FindEnabledNSNode(tx, req.NsNodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node == nil {
|
||||
return &pb.FindEnabledNSNodeResponse{NsNode: nil}, nil
|
||||
}
|
||||
|
||||
// 集群信息
|
||||
clusterName, err := nameservers.SharedNSClusterDAO.FindEnabledNSClusterName(tx, int64(node.ClusterId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 安装信息
|
||||
installStatus, err := node.DecodeInstallStatus()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
installStatusResult := &pb.NodeInstallStatus{}
|
||||
if installStatus != nil {
|
||||
installStatusResult = &pb.NodeInstallStatus{
|
||||
IsRunning: installStatus.IsRunning,
|
||||
IsFinished: installStatus.IsFinished,
|
||||
IsOk: installStatus.IsOk,
|
||||
Error: installStatus.Error,
|
||||
ErrorCode: installStatus.ErrorCode,
|
||||
UpdatedAt: installStatus.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.FindEnabledNSNodeResponse{NsNode: &pb.NSNode{
|
||||
Id: int64(node.Id),
|
||||
Name: node.Name,
|
||||
StatusJSON: []byte(node.Status),
|
||||
UniqueId: node.UniqueId,
|
||||
Secret: node.Secret,
|
||||
IsInstalled: node.IsInstalled == 1,
|
||||
InstallDir: node.InstallDir,
|
||||
NsCluster: &pb.NSCluster{
|
||||
Id: int64(node.ClusterId),
|
||||
Name: clusterName,
|
||||
},
|
||||
InstallStatus: installStatusResult,
|
||||
IsOn: node.IsOn == 1,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// UpdateNSNode 修改节点
|
||||
func (this *NSNodeService) UpdateNSNode(ctx context.Context, req *pb.UpdateNSNodeRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = nameservers.SharedNSNodeDAO.UpdateNode(tx, req.NsNodeId, req.Name, req.NsClusterId, req.IsOn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// InstallNSNode 安装节点
|
||||
func (this *NSNodeService) InstallNSNode(ctx context.Context, req *pb.InstallNSNodeRequest) (*pb.InstallNSNodeResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 需要实现
|
||||
return nil, errors.New("尚未实现此功能")
|
||||
|
||||
return &pb.InstallNSNodeResponse{}, nil
|
||||
}
|
||||
|
||||
// FindNSNodeInstallStatus 读取节点安装状态
|
||||
func (this *NSNodeService) FindNSNodeInstallStatus(ctx context.Context, req *pb.FindNSNodeInstallStatusRequest) (*pb.FindNSNodeInstallStatusResponse, error) {
|
||||
// 校验请求
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
installStatus, err := nameservers.SharedNSNodeDAO.FindNodeInstallStatus(tx, req.NsNodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if installStatus == nil {
|
||||
return &pb.FindNSNodeInstallStatusResponse{InstallStatus: nil}, nil
|
||||
}
|
||||
|
||||
pbInstallStatus := &pb.NodeInstallStatus{
|
||||
IsRunning: installStatus.IsRunning,
|
||||
IsFinished: installStatus.IsFinished,
|
||||
IsOk: installStatus.IsOk,
|
||||
Error: installStatus.Error,
|
||||
ErrorCode: installStatus.ErrorCode,
|
||||
UpdatedAt: installStatus.UpdatedAt,
|
||||
}
|
||||
return &pb.FindNSNodeInstallStatusResponse{InstallStatus: pbInstallStatus}, nil
|
||||
}
|
||||
|
||||
// UpdateNSNodeIsInstalled 修改节点安装状态
|
||||
func (this *NSNodeService) UpdateNSNodeIsInstalled(ctx context.Context, req *pb.UpdateNSNodeIsInstalledRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = nameservers.SharedNSNodeDAO.UpdateNodeIsInstalled(tx, req.NsNodeId, req.IsInstalled)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// UpdateNSNodeStatus 更新节点状态
|
||||
func (this *NSNodeService) UpdateNSNodeStatus(ctx context.Context, req *pb.UpdateNSNodeStatusRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验节点
|
||||
_, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.NodeId > 0 {
|
||||
nodeId = req.NodeId
|
||||
}
|
||||
|
||||
if nodeId <= 0 {
|
||||
return nil, errors.New("'nodeId' should be greater than 0")
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = nameservers.SharedNSNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindCurrentNSNodeConfig 获取当前节点信息
|
||||
func (this *NSNodeService) FindCurrentNSNodeConfig(ctx context.Context, req *pb.FindCurrentNSNodeConfigRequest) (*pb.FindCurrentNSNodeConfigResponse, error) {
|
||||
// 校验节点
|
||||
_, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
config, err := nameservers.SharedNSNodeDAO.ComposeNodeConfig(tx, nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if config == nil {
|
||||
return &pb.FindCurrentNSNodeConfigResponse{NsNodeJSON: nil}, nil
|
||||
}
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.FindCurrentNSNodeConfigResponse{NsNodeJSON: configJSON}, nil
|
||||
}
|
||||
|
||||
// CheckNSNodeLatestVersion 检查新版本
|
||||
func (this *NSNodeService) CheckNSNodeLatestVersion(ctx context.Context, req *pb.CheckNSNodeLatestVersionRequest) (*pb.CheckNSNodeLatestVersionResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deployFiles := installers.SharedDeployManager.LoadNSNodeFiles()
|
||||
for _, file := range deployFiles {
|
||||
if file.OS == req.Os && file.Arch == req.Arch && stringutil.VersionCompare(file.Version, req.CurrentVersion) > 0 {
|
||||
return &pb.CheckNSNodeLatestVersionResponse{
|
||||
HasNewVersion: true,
|
||||
NewVersion: file.Version,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
return &pb.CheckNSNodeLatestVersionResponse{HasNewVersion: false}, nil
|
||||
}
|
||||
226
internal/rpc/services/nameservers/service_ns_record.go
Normal file
226
internal/rpc/services/nameservers/service_ns_record.go
Normal file
@@ -0,0 +1,226 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
// NSRecordService 域名记录相关服务
|
||||
type NSRecordService struct {
|
||||
services.BaseService
|
||||
}
|
||||
|
||||
// CreateNSRecord 创建记录
|
||||
func (this *NSRecordService) CreateNSRecord(ctx context.Context, req *pb.CreateNSRecordRequest) (*pb.CreateNSRecordResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
recordId, err := nameservers.SharedNSRecordDAO.CreateRecord(tx, req.NsDomainId, req.Description, req.Name, req.Type, req.Value, req.Ttl, req.NsRouteIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateNSRecordResponse{NsRecordId: recordId}, nil
|
||||
}
|
||||
|
||||
// UpdateNSRecord 修改记录
|
||||
func (this *NSRecordService) UpdateNSRecord(ctx context.Context, req *pb.UpdateNSRecordRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, req.NsRecordId, req.Description, req.Name, req.Type, req.Value, req.Ttl, req.NsRouteIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// DeleteNSRecord 删除记录
|
||||
func (this *NSRecordService) DeleteNSRecord(ctx context.Context, req *pb.DeleteNSRecordRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSRecordDAO.DisableNSRecord(tx, req.NsRecordId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// CountAllEnabledNSRecords 计算记录数量
|
||||
func (this *NSRecordService) CountAllEnabledNSRecords(ctx context.Context, req *pb.CountAllEnabledNSRecordsRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
count, err := nameservers.SharedNSRecordDAO.CountAllEnabledRecords(tx, req.NsDomainId, req.Type, req.Keyword, req.NsRouteId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListEnabledNSRecords 读取单页记录
|
||||
func (this *NSRecordService) ListEnabledNSRecords(ctx context.Context, req *pb.ListEnabledNSRecordsRequest) (*pb.ListEnabledNSRecordsResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
records, err := nameservers.SharedNSRecordDAO.ListEnabledRecords(tx, req.NsDomainId, req.Type, req.Keyword, req.NsRouteId, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbRecords = []*pb.NSRecord{}
|
||||
for _, record := range records {
|
||||
// 线路
|
||||
var pbRoutes = []*pb.NSRoute{}
|
||||
for _, recordId := range record.DecodeRouteIds() {
|
||||
route, err := nameservers.SharedNSRouteDAO.FindEnabledNSRoute(tx, recordId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if route == nil {
|
||||
continue
|
||||
}
|
||||
pbRoutes = append(pbRoutes, &pb.NSRoute{
|
||||
Id: int64(route.Id),
|
||||
Name: route.Name,
|
||||
})
|
||||
}
|
||||
|
||||
pbRecords = append(pbRecords, &pb.NSRecord{
|
||||
Id: int64(record.Id),
|
||||
Description: record.Description,
|
||||
Name: record.Name,
|
||||
Type: record.Type,
|
||||
Value: record.Value,
|
||||
Ttl: types.Int32(record.Ttl),
|
||||
Weight: types.Int32(record.Weight),
|
||||
CreatedAt: int64(record.CreatedAt),
|
||||
IsOn: record.IsOn == 1,
|
||||
NsDomain: nil,
|
||||
NsRoutes: pbRoutes,
|
||||
})
|
||||
}
|
||||
return &pb.ListEnabledNSRecordsResponse{NsRecords: pbRecords}, nil
|
||||
}
|
||||
|
||||
// FindEnabledNSRecord 查询单个记录信息
|
||||
func (this *NSRecordService) FindEnabledNSRecord(ctx context.Context, req *pb.FindEnabledNSRecordRequest) (*pb.FindEnabledNSRecordResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
record, err := nameservers.SharedNSRecordDAO.FindEnabledNSRecord(tx, req.NsRecordId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if record == nil {
|
||||
return &pb.FindEnabledNSRecordResponse{NsRecord: nil}, nil
|
||||
}
|
||||
|
||||
// 域名
|
||||
domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, int64(record.DomainId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domain == nil {
|
||||
return &pb.FindEnabledNSRecordResponse{NsRecord: nil}, nil
|
||||
}
|
||||
var pbDomain = &pb.NSDomain{
|
||||
Id: int64(domain.Id),
|
||||
Name: domain.Name,
|
||||
IsOn: domain.IsOn == 1,
|
||||
}
|
||||
|
||||
// 线路
|
||||
var pbRoutes = []*pb.NSRoute{}
|
||||
for _, recordId := range record.DecodeRouteIds() {
|
||||
route, err := nameservers.SharedNSRouteDAO.FindEnabledNSRoute(tx, recordId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if route == nil {
|
||||
continue
|
||||
}
|
||||
pbRoutes = append(pbRoutes, &pb.NSRoute{
|
||||
Id: int64(route.Id),
|
||||
Name: route.Name,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindEnabledNSRecordResponse{NsRecord: &pb.NSRecord{
|
||||
Id: int64(record.Id),
|
||||
Description: record.Description,
|
||||
Name: record.Name,
|
||||
Type: record.Type,
|
||||
Value: record.Value,
|
||||
Ttl: types.Int32(record.Ttl),
|
||||
Weight: types.Int32(record.Weight),
|
||||
CreatedAt: int64(record.CreatedAt),
|
||||
IsOn: record.IsOn == 1,
|
||||
NsDomain: pbDomain,
|
||||
NsRoutes: pbRoutes,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// ListNSRecordsAfterVersion 根据版本列出一组记录
|
||||
func (this *NSRecordService) ListNSRecordsAfterVersion(ctx context.Context, req *pb.ListNSRecordsAfterVersionRequest) (*pb.ListNSRecordsAfterVersionResponse, error) {
|
||||
_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 集群ID
|
||||
var tx = this.NullTx()
|
||||
records, err := nameservers.SharedNSRecordDAO.ListRecordsAfterVersion(tx, req.Version, 2000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pbRecords []*pb.NSRecord
|
||||
for _, record := range records {
|
||||
// 线路
|
||||
pbRoutes := []*pb.NSRoute{}
|
||||
routeIds := record.DecodeRouteIds()
|
||||
for _, routeId := range routeIds {
|
||||
pbRoutes = append(pbRoutes, &pb.NSRoute{Id: routeId})
|
||||
}
|
||||
|
||||
pbRecords = append(pbRecords, &pb.NSRecord{
|
||||
Id: int64(record.Id),
|
||||
Description: "",
|
||||
Name: record.Name,
|
||||
Type: record.Type,
|
||||
Value: record.Value,
|
||||
Ttl: types.Int32(record.Ttl),
|
||||
Weight: types.Int32(record.Weight),
|
||||
IsDeleted: record.State == nameservers.NSRecordStateDisabled,
|
||||
IsOn: record.IsOn == 1,
|
||||
Version: int64(record.Version),
|
||||
NsDomain: &pb.NSDomain{Id: int64(record.DomainId)},
|
||||
NsRoutes: pbRoutes,
|
||||
})
|
||||
}
|
||||
return &pb.ListNSRecordsAfterVersionResponse{NsRecords: pbRecords}, nil
|
||||
}
|
||||
229
internal/rpc/services/nameservers/service_ns_route.go
Normal file
229
internal/rpc/services/nameservers/service_ns_route.go
Normal file
@@ -0,0 +1,229 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// NSRouteService 线路相关服务
|
||||
type NSRouteService struct {
|
||||
services.BaseService
|
||||
}
|
||||
|
||||
// CreateNSRoute 创建线路
|
||||
func (this *NSRouteService) CreateNSRoute(ctx context.Context, req *pb.CreateNSRouteRequest) (*pb.CreateNSRouteResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
routeId, err := nameservers.SharedNSRouteDAO.CreateRoute(tx, req.NsClusterId, req.NsDomainId, req.UserId, req.Name, req.RangesJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateNSRouteResponse{NsRouteId: routeId}, nil
|
||||
}
|
||||
|
||||
// UpdateNSRoute 修改线路
|
||||
func (this *NSRouteService) UpdateNSRoute(ctx context.Context, req *pb.UpdateNSRouteRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSRouteDAO.UpdateRoute(tx, req.NsRouteId, req.Name, req.RangesJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// DeleteNSRoute 删除线路
|
||||
func (this *NSRouteService) DeleteNSRoute(ctx context.Context, req *pb.DeleteNSRouteRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSRouteDAO.DisableNSRoute(tx, req.NsRouteId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindEnabledNSRoute 获取单个路线信息
|
||||
func (this *NSRouteService) FindEnabledNSRoute(ctx context.Context, req *pb.FindEnabledNSRouteRequest) (*pb.FindEnabledNSRouteResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
route, err := nameservers.SharedNSRouteDAO.FindEnabledNSRoute(tx, req.NsRouteId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if route == nil {
|
||||
return &pb.FindEnabledNSRouteResponse{NsRoute: nil}, nil
|
||||
}
|
||||
|
||||
// 集群
|
||||
var pbCluster *pb.NSCluster
|
||||
if route.ClusterId > 0 {
|
||||
cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(route.ClusterId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cluster != nil {
|
||||
pbCluster = &pb.NSCluster{
|
||||
Id: int64(cluster.Id),
|
||||
IsOn: cluster.IsOn == 1,
|
||||
Name: cluster.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 域名
|
||||
var pbDomain *pb.NSDomain
|
||||
if route.DomainId > 0 {
|
||||
domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, int64(route.DomainId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domain != nil {
|
||||
pbDomain = &pb.NSDomain{
|
||||
Id: int64(domain.Id),
|
||||
Name: domain.Name,
|
||||
IsOn: domain.IsOn == 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.FindEnabledNSRouteResponse{NsRoute: &pb.NSRoute{
|
||||
Id: int64(route.Id),
|
||||
IsOn: route.IsOn == 1,
|
||||
Name: route.Name,
|
||||
RangesJSON: []byte(route.Ranges),
|
||||
NsCluster: pbCluster,
|
||||
NsDomain: pbDomain,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// FindAllEnabledNSRoutes 读取所有线路
|
||||
func (this *NSRouteService) FindAllEnabledNSRoutes(ctx context.Context, req *pb.FindAllEnabledNSRoutesRequest) (*pb.FindAllEnabledNSRoutesResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
routes, err := nameservers.SharedNSRouteDAO.FindAllEnabledRoutes(tx, req.NsClusterId, req.NsDomainId, req.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbRoutes = []*pb.NSRoute{}
|
||||
for _, route := range routes {
|
||||
// 集群
|
||||
var pbCluster *pb.NSCluster
|
||||
if route.ClusterId > 0 {
|
||||
cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(route.ClusterId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cluster != nil {
|
||||
pbCluster = &pb.NSCluster{
|
||||
Id: int64(cluster.Id),
|
||||
IsOn: cluster.IsOn == 1,
|
||||
Name: cluster.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 域名
|
||||
var pbDomain *pb.NSDomain
|
||||
if route.DomainId > 0 {
|
||||
domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, int64(route.DomainId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domain != nil {
|
||||
pbDomain = &pb.NSDomain{
|
||||
Id: int64(domain.Id),
|
||||
Name: domain.Name,
|
||||
IsOn: domain.IsOn == 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pbRoutes = append(pbRoutes, &pb.NSRoute{
|
||||
Id: int64(route.Id),
|
||||
IsOn: route.IsOn == 1,
|
||||
Name: route.Name,
|
||||
RangesJSON: []byte(route.Ranges),
|
||||
NsCluster: pbCluster,
|
||||
NsDomain: pbDomain,
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledNSRoutesResponse{NsRoutes: pbRoutes}, nil
|
||||
}
|
||||
|
||||
// UpdateNSRouteOrders 设置线路排序
|
||||
func (this *NSRouteService) UpdateNSRouteOrders(ctx context.Context, req *pb.UpdateNSRouteOrdersRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
err = nameservers.SharedNSRouteDAO.UpdateRouteOrders(tx, req.NsRouteIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// ListNSRoutesAfterVersion 根据版本列出一组线路
|
||||
func (this *NSRouteService) ListNSRoutesAfterVersion(ctx context.Context, req *pb.ListNSRoutesAfterVersionRequest) (*pb.ListNSRoutesAfterVersionResponse, error) {
|
||||
_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 集群ID
|
||||
var tx = this.NullTx()
|
||||
routes, err := nameservers.SharedNSRouteDAO.ListRoutesAfterVersion(tx, req.Version, 2000)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pbRoutes []*pb.NSRoute
|
||||
for _, route := range routes {
|
||||
// 集群
|
||||
var pbCluster *pb.NSCluster
|
||||
if route.ClusterId > 0 {
|
||||
pbCluster = &pb.NSCluster{Id: int64(route.ClusterId)}
|
||||
}
|
||||
|
||||
// 域名
|
||||
var pbDomain *pb.NSDomain
|
||||
if route.DomainId > 0 {
|
||||
pbDomain = &pb.NSDomain{Id: int64(route.DomainId)}
|
||||
}
|
||||
|
||||
pbRoutes = append(pbRoutes, &pb.NSRoute{
|
||||
Id: int64(route.Id),
|
||||
IsOn: route.IsOn == 1,
|
||||
Name: "",
|
||||
RangesJSON: []byte(route.Ranges),
|
||||
IsDeleted: route.State == nameservers.NSRouteStateDisabled,
|
||||
Order: int64(route.Order),
|
||||
Version: int64(route.Version),
|
||||
NsCluster: pbCluster,
|
||||
NsDomain: pbDomain,
|
||||
})
|
||||
}
|
||||
return &pb.ListNSRoutesAfterVersionResponse{NsRoutes: pbRoutes}, nil
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/authority"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/stats"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
@@ -546,6 +547,12 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com
|
||||
})
|
||||
}
|
||||
|
||||
// 是否是企业版
|
||||
isPlus, err := authority.SharedAuthorityKeyDAO.IsPlus(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 边缘节点升级信息
|
||||
{
|
||||
upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{
|
||||
@@ -560,7 +567,7 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com
|
||||
}
|
||||
|
||||
// 监控节点升级信息
|
||||
{
|
||||
if isPlus {
|
||||
upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{
|
||||
NewVersion: teaconst.MonitorNodeVersion,
|
||||
}
|
||||
@@ -573,7 +580,7 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com
|
||||
}
|
||||
|
||||
// 认证节点升级信息
|
||||
{
|
||||
if isPlus {
|
||||
upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{
|
||||
NewVersion: teaconst.AuthorityNodeVersion,
|
||||
}
|
||||
@@ -586,7 +593,7 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com
|
||||
}
|
||||
|
||||
// 用户节点升级信息
|
||||
{
|
||||
if isPlus {
|
||||
upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{
|
||||
NewVersion: teaconst.UserNodeVersion,
|
||||
}
|
||||
@@ -611,5 +618,18 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com
|
||||
resp.ApiNodeUpgradeInfo = upgradeInfo
|
||||
}
|
||||
|
||||
// DNS节点升级信息
|
||||
if isPlus {
|
||||
upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{
|
||||
NewVersion: teaconst.DNSNodeVersion,
|
||||
}
|
||||
countNodes, err := nameservers.SharedNSNodeDAO.CountAllLowerVersionNodes(tx, upgradeInfo.NewVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
upgradeInfo.CountNodes = countNodes
|
||||
resp.NsNodeUpgradeInfo = upgradeInfo
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/authority"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/encrypt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
@@ -159,7 +160,7 @@ func (this *BaseService) ValidateNodeId(ctx context.Context, roles ...rpcutils.U
|
||||
timestamp := m.GetInt64("timestamp")
|
||||
if time.Now().Unix()-timestamp > 600 {
|
||||
// 请求超过10分钟认为超时
|
||||
return rpcutils.UserTypeNone, 0, errors.New("authenticate timeout")
|
||||
return rpcutils.UserTypeNone, 0, errors.New("authenticate timeout, please check your system clock")
|
||||
}
|
||||
|
||||
switch apiToken.Role {
|
||||
@@ -185,6 +186,8 @@ func (this *BaseService) ValidateNodeId(ctx context.Context, roles ...rpcutils.U
|
||||
nodeIntId = 0
|
||||
case rpcutils.UserTypeMonitor:
|
||||
nodeIntId, err = models.SharedMonitorNodeDAO.FindEnabledMonitorNodeIdWithUniqueId(nil, nodeId)
|
||||
case rpcutils.UserTypeDNS:
|
||||
nodeIntId, err = nameservers.SharedNSNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
|
||||
case rpcutils.UserTypeAuthority:
|
||||
nodeIntId, err = authority.SharedAuthorityNodeDAO.FindEnabledAuthorityNodeIdWithUniqueId(nil, nodeId)
|
||||
default:
|
||||
|
||||
@@ -7,12 +7,12 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// DNS相关服务
|
||||
// DNSService DNS相关服务
|
||||
type DNSService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 查找问题
|
||||
// FindAllDNSIssues 查找问题
|
||||
func (this *DNSService) FindAllDNSIssues(ctx context.Context, req *pb.FindAllDNSIssuesRequest) (*pb.FindAllDNSIssuesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -20,13 +20,24 @@ func (this *DNSService) FindAllDNSIssues(ctx context.Context, req *pb.FindAllDNS
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []*pb.DNSIssue{}
|
||||
var result = []*pb.DNSIssue{}
|
||||
var tx = this.NullTx()
|
||||
var clusters []*models.NodeCluster
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
clusters, err := models.SharedNodeClusterDAO.FindAllEnabledClustersHaveDNSDomain(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if req.NodeClusterId <= 0 {
|
||||
clusters, err = models.SharedNodeClusterDAO.FindAllEnabledClustersHaveDNSDomain(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(tx, req.NodeClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cluster == nil {
|
||||
return &pb.FindAllDNSIssuesResponse{Issues: nil}, nil
|
||||
}
|
||||
clusters = []*models.NodeCluster{cluster}
|
||||
}
|
||||
for _, cluster := range clusters {
|
||||
issues, err := models.SharedNodeClusterDAO.CheckClusterDNS(tx, cluster)
|
||||
|
||||
@@ -6,21 +6,24 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"net"
|
||||
)
|
||||
|
||||
// DNS域名相关服务
|
||||
// DNSDomainService DNS域名相关服务
|
||||
type DNSDomainService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建域名
|
||||
// CreateDNSDomain 创建域名
|
||||
func (this *DNSDomainService) CreateDNSDomain(ctx context.Context, req *pb.CreateDNSDomainRequest) (*pb.CreateDNSDomainResponse, error) {
|
||||
// 校验请求
|
||||
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -91,7 +94,7 @@ func (this *DNSDomainService) CreateDNSDomain(ctx context.Context, req *pb.Creat
|
||||
return &pb.CreateDNSDomainResponse{DnsDomainId: domainId}, nil
|
||||
}
|
||||
|
||||
// 修改域名
|
||||
// UpdateDNSDomain 修改域名
|
||||
func (this *DNSDomainService) UpdateDNSDomain(ctx context.Context, req *pb.UpdateDNSDomainRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -108,7 +111,7 @@ func (this *DNSDomainService) UpdateDNSDomain(ctx context.Context, req *pb.Updat
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 删除域名
|
||||
// DeleteDNSDomain 删除域名
|
||||
func (this *DNSDomainService) DeleteDNSDomain(ctx context.Context, req *pb.DeleteDNSDomainRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -125,7 +128,7 @@ func (this *DNSDomainService) DeleteDNSDomain(ctx context.Context, req *pb.Delet
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 查询单个域名完整信息
|
||||
// FindEnabledDNSDomain 查询单个域名完整信息
|
||||
func (this *DNSDomainService) FindEnabledDNSDomain(ctx context.Context, req *pb.FindEnabledDNSDomainRequest) (*pb.FindEnabledDNSDomainResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -147,7 +150,7 @@ func (this *DNSDomainService) FindEnabledDNSDomain(ctx context.Context, req *pb.
|
||||
return &pb.FindEnabledDNSDomainResponse{DnsDomain: pbDomain}, nil
|
||||
}
|
||||
|
||||
// 查询单个域名基础信息
|
||||
// FindEnabledBasicDNSDomain 查询单个域名基础信息
|
||||
func (this *DNSDomainService) FindEnabledBasicDNSDomain(ctx context.Context, req *pb.FindEnabledBasicDNSDomainRequest) (*pb.FindEnabledBasicDNSDomainResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -173,7 +176,7 @@ func (this *DNSDomainService) FindEnabledBasicDNSDomain(ctx context.Context, req
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// 计算服务商下的域名数量
|
||||
// CountAllEnabledDNSDomainsWithDNSProviderId 计算服务商下的域名数量
|
||||
func (this *DNSDomainService) CountAllEnabledDNSDomainsWithDNSProviderId(ctx context.Context, req *pb.CountAllEnabledDNSDomainsWithDNSProviderIdRequest) (*pb.RPCCountResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -190,7 +193,7 @@ func (this *DNSDomainService) CountAllEnabledDNSDomainsWithDNSProviderId(ctx con
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// 列出服务商下的所有域名
|
||||
// FindAllEnabledDNSDomainsWithDNSProviderId 列出服务商下的所有域名
|
||||
func (this *DNSDomainService) FindAllEnabledDNSDomainsWithDNSProviderId(ctx context.Context, req *pb.FindAllEnabledDNSDomainsWithDNSProviderIdRequest) (*pb.FindAllEnabledDNSDomainsWithDNSProviderIdResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -217,7 +220,7 @@ func (this *DNSDomainService) FindAllEnabledDNSDomainsWithDNSProviderId(ctx cont
|
||||
return &pb.FindAllEnabledDNSDomainsWithDNSProviderIdResponse{DnsDomains: result}, nil
|
||||
}
|
||||
|
||||
// 列出服务商下的所有域名基本信息
|
||||
// FindAllEnabledBasicDNSDomainsWithDNSProviderId 列出服务商下的所有域名基本信息
|
||||
func (this *DNSDomainService) FindAllEnabledBasicDNSDomainsWithDNSProviderId(ctx context.Context, req *pb.FindAllEnabledBasicDNSDomainsWithDNSProviderIdRequest) (*pb.FindAllEnabledBasicDNSDomainsWithDNSProviderIdResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -244,7 +247,7 @@ func (this *DNSDomainService) FindAllEnabledBasicDNSDomainsWithDNSProviderId(ctx
|
||||
return &pb.FindAllEnabledBasicDNSDomainsWithDNSProviderIdResponse{DnsDomains: result}, nil
|
||||
}
|
||||
|
||||
// 同步域名数据
|
||||
// SyncDNSDomainData 同步域名数据
|
||||
func (this *DNSDomainService) SyncDNSDomainData(ctx context.Context, req *pb.SyncDNSDomainDataRequest) (*pb.SyncDNSDomainDataResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -254,7 +257,7 @@ func (this *DNSDomainService) SyncDNSDomainData(ctx context.Context, req *pb.Syn
|
||||
return this.syncClusterDNS(req)
|
||||
}
|
||||
|
||||
// 查看支持的线路
|
||||
// FindAllDNSDomainRoutes 查看支持的线路
|
||||
func (this *DNSDomainService) FindAllDNSDomainRoutes(ctx context.Context, req *pb.FindAllDNSDomainRoutesRequest) (*pb.FindAllDNSDomainRoutesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -280,7 +283,7 @@ func (this *DNSDomainService) FindAllDNSDomainRoutes(ctx context.Context, req *p
|
||||
return &pb.FindAllDNSDomainRoutesResponse{Routes: pbRoutes}, nil
|
||||
}
|
||||
|
||||
// 判断是否有域名可选
|
||||
// ExistAvailableDomains 判断是否有域名可选
|
||||
func (this *DNSDomainService) ExistAvailableDomains(ctx context.Context, req *pb.ExistAvailableDomainsRequest) (*pb.ExistAvailableDomainsResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -301,7 +304,7 @@ func (this *DNSDomainService) ExistAvailableDomains(ctx context.Context, req *pb
|
||||
func (this *DNSDomainService) convertDomainToPB(domain *dns.DNSDomain) (*pb.DNSDomain, error) {
|
||||
domainId := int64(domain.Id)
|
||||
|
||||
records := []*dnsclients.Record{}
|
||||
records := []*dnstypes.Record{}
|
||||
if len(domain.Records) > 0 && domain.Records != "null" {
|
||||
err := json.Unmarshal([]byte(domain.Records), &records)
|
||||
if err != nil {
|
||||
@@ -375,7 +378,7 @@ func (this *DNSDomainService) convertDomainToPB(domain *dns.DNSDomain) (*pb.DNSD
|
||||
}
|
||||
|
||||
// 转换域名记录信息
|
||||
func (this *DNSDomainService) convertRecordToPB(record *dnsclients.Record) *pb.DNSRecord {
|
||||
func (this *DNSDomainService) convertRecordToPB(record *dnstypes.Record) *pb.DNSRecord {
|
||||
return &pb.DNSRecord{
|
||||
Id: record.Id,
|
||||
Name: record.Name,
|
||||
@@ -386,7 +389,7 @@ func (this *DNSDomainService) convertRecordToPB(record *dnsclients.Record) *pb.D
|
||||
}
|
||||
|
||||
// 检查集群节点变化
|
||||
func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster, records []*dnsclients.Record, domainName string) (result []maps.Map, doneNodeRecords []*dnsclients.Record, doneServerRecords []*dnsclients.Record, countAllNodes int64, countAllServers int64, nodesChanged bool, serversChanged bool, err error) {
|
||||
func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster, records []*dnstypes.Record, domainName string) (result []maps.Map, doneNodeRecords []*dnstypes.Record, doneServerRecords []*dnstypes.Record, countAllNodes int64, countAllServers int64, nodesChanged bool, serversChanged bool, err error) {
|
||||
clusterId := int64(cluster.Id)
|
||||
clusterDnsName := cluster.DnsName
|
||||
clusterDomain := clusterDnsName + "." + domainName
|
||||
@@ -399,10 +402,10 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
|
||||
return nil, nil, nil, 0, 0, false, false, err
|
||||
}
|
||||
countAllNodes = int64(len(nodes))
|
||||
nodeRecords := []*dnsclients.Record{} // 之所以用数组再存一遍,是因为dnsName可能会重复
|
||||
nodeRecordMapping := map[string]*dnsclients.Record{} // value_route => *Record
|
||||
nodeRecords := []*dnstypes.Record{} // 之所以用数组再存一遍,是因为dnsName可能会重复
|
||||
nodeRecordMapping := map[string]*dnstypes.Record{} // value_route => *Record
|
||||
for _, record := range records {
|
||||
if record.Type == dnsclients.RecordTypeA && record.Name == clusterDnsName {
|
||||
if (record.Type == dnstypes.RecordTypeA || record.Type == dnstypes.RecordTypeAAAA) && record.Name == clusterDnsName {
|
||||
nodeRecords = append(nodeRecords, record)
|
||||
nodeRecordMapping[record.Value+"_"+record.Route] = record
|
||||
}
|
||||
@@ -411,7 +414,7 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
|
||||
// 新增的节点域名
|
||||
nodeKeys := []string{}
|
||||
for _, node := range nodes {
|
||||
ipAddresses, err := models.SharedNodeIPAddressDAO.FindNodeAccessIPAddresses(tx, int64(node.Id))
|
||||
ipAddresses, err := models.SharedNodeIPAddressDAO.FindNodeAccessIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
|
||||
if err != nil {
|
||||
return nil, nil, nil, 0, 0, false, false, err
|
||||
}
|
||||
@@ -438,12 +441,16 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
|
||||
nodeKeys = append(nodeKeys, key)
|
||||
record, ok := nodeRecordMapping[key]
|
||||
if !ok {
|
||||
recordType := dnstypes.RecordTypeA
|
||||
if utils.IsIPv6(ip) {
|
||||
recordType = dnstypes.RecordTypeAAAA
|
||||
}
|
||||
result = append(result, maps.Map{
|
||||
"action": "create",
|
||||
"record": &dnsclients.Record{
|
||||
"record": &dnstypes.Record{
|
||||
Id: "",
|
||||
Name: clusterDnsName,
|
||||
Type: dnsclients.RecordTypeA,
|
||||
Type: recordType,
|
||||
Value: ip,
|
||||
Route: route,
|
||||
},
|
||||
@@ -474,10 +481,10 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
|
||||
return nil, nil, nil, 0, 0, false, false, err
|
||||
}
|
||||
countAllServers = int64(len(servers))
|
||||
serverRecords := []*dnsclients.Record{} // 之所以用数组再存一遍,是因为dnsName可能会重复
|
||||
serverRecordsMap := map[string]*dnsclients.Record{} // dnsName => *Record
|
||||
serverRecords := []*dnstypes.Record{} // 之所以用数组再存一遍,是因为dnsName可能会重复
|
||||
serverRecordsMap := map[string]*dnstypes.Record{} // dnsName => *Record
|
||||
for _, record := range records {
|
||||
if record.Type == dnsclients.RecordTypeCName && record.Value == clusterDomain+"." {
|
||||
if record.Type == dnstypes.RecordTypeCNAME && record.Value == clusterDomain+"." {
|
||||
serverRecords = append(serverRecords, record)
|
||||
serverRecordsMap[record.Name] = record
|
||||
}
|
||||
@@ -496,10 +503,10 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
|
||||
serversChanged = true
|
||||
result = append(result, maps.Map{
|
||||
"action": "create",
|
||||
"record": &dnsclients.Record{
|
||||
"record": &dnstypes.Record{
|
||||
Id: "",
|
||||
Name: dnsName,
|
||||
Type: dnsclients.RecordTypeCName,
|
||||
Type: dnstypes.RecordTypeCNAME,
|
||||
Value: clusterDomain + ".",
|
||||
Route: "", // 注意这里为空,需要在执行过程中获取默认值
|
||||
},
|
||||
@@ -644,7 +651,7 @@ func (this *DNSDomainService) syncClusterDNS(req *pb.SyncDNSDomainDataRequest) (
|
||||
}
|
||||
for _, change := range allChanges {
|
||||
action := change.GetString("action")
|
||||
record := change.Get("record").(*dnsclients.Record)
|
||||
record := change.Get("record").(*dnstypes.Record)
|
||||
|
||||
if len(record.Route) == 0 {
|
||||
record.Route = manager.DefaultRoute()
|
||||
|
||||
@@ -8,12 +8,12 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// DNS服务商相关服务
|
||||
// DNSProviderService DNS服务商相关服务
|
||||
type DNSProviderService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建服务商
|
||||
// CreateDNSProvider 创建服务商
|
||||
func (this *DNSProviderService) CreateDNSProvider(ctx context.Context, req *pb.CreateDNSProviderRequest) (*pb.CreateDNSProviderResponse, error) {
|
||||
// 校验请求
|
||||
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -31,7 +31,7 @@ func (this *DNSProviderService) CreateDNSProvider(ctx context.Context, req *pb.C
|
||||
return &pb.CreateDNSProviderResponse{DnsProviderId: providerId}, nil
|
||||
}
|
||||
|
||||
// 修改服务商
|
||||
// UpdateDNSProvider 修改服务商
|
||||
func (this *DNSProviderService) UpdateDNSProvider(ctx context.Context, req *pb.UpdateDNSProviderRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -50,7 +50,7 @@ func (this *DNSProviderService) UpdateDNSProvider(ctx context.Context, req *pb.U
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 计算服务商数量
|
||||
// CountAllEnabledDNSProviders 计算服务商数量
|
||||
func (this *DNSProviderService) CountAllEnabledDNSProviders(ctx context.Context, req *pb.CountAllEnabledDNSProvidersRequest) (*pb.RPCCountResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, req.UserId)
|
||||
@@ -67,7 +67,7 @@ func (this *DNSProviderService) CountAllEnabledDNSProviders(ctx context.Context,
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// 列出单页服务商信息
|
||||
// ListEnabledDNSProviders 列出单页服务商信息
|
||||
func (this *DNSProviderService) ListEnabledDNSProviders(ctx context.Context, req *pb.ListEnabledDNSProvidersRequest) (*pb.ListEnabledDNSProvidersResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, req.UserId)
|
||||
@@ -97,7 +97,7 @@ func (this *DNSProviderService) ListEnabledDNSProviders(ctx context.Context, req
|
||||
return &pb.ListEnabledDNSProvidersResponse{DnsProviders: result}, nil
|
||||
}
|
||||
|
||||
// 查找所有的DNS服务商
|
||||
// FindAllEnabledDNSProviders 查找所有的DNS服务商
|
||||
func (this *DNSProviderService) FindAllEnabledDNSProviders(ctx context.Context, req *pb.FindAllEnabledDNSProvidersRequest) (*pb.FindAllEnabledDNSProvidersResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, req.UserId)
|
||||
@@ -127,7 +127,7 @@ func (this *DNSProviderService) FindAllEnabledDNSProviders(ctx context.Context,
|
||||
return &pb.FindAllEnabledDNSProvidersResponse{DnsProviders: result}, nil
|
||||
}
|
||||
|
||||
// 删除服务商
|
||||
// DeleteDNSProvider 删除服务商
|
||||
func (this *DNSProviderService) DeleteDNSProvider(ctx context.Context, req *pb.DeleteDNSProviderRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -146,7 +146,7 @@ func (this *DNSProviderService) DeleteDNSProvider(ctx context.Context, req *pb.D
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 查找单个服务商
|
||||
// FindEnabledDNSProvider 查找单个服务商
|
||||
func (this *DNSProviderService) FindEnabledDNSProvider(ctx context.Context, req *pb.FindEnabledDNSProviderRequest) (*pb.FindEnabledDNSProviderResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -174,7 +174,7 @@ func (this *DNSProviderService) FindEnabledDNSProvider(ctx context.Context, req
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// 取得所有服务商类型
|
||||
// FindAllDNSProviderTypes 取得所有服务商类型
|
||||
func (this *DNSProviderService) FindAllDNSProviderTypes(ctx context.Context, req *pb.FindAllDNSProviderTypesRequest) (*pb.FindAllDNSProviderTypesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -183,16 +183,17 @@ func (this *DNSProviderService) FindAllDNSProviderTypes(ctx context.Context, req
|
||||
}
|
||||
|
||||
result := []*pb.DNSProviderType{}
|
||||
for _, t := range dnsclients.AllProviderTypes {
|
||||
for _, t := range dnsclients.FindAllProviderTypes() {
|
||||
result = append(result, &pb.DNSProviderType{
|
||||
Name: t.GetString("name"),
|
||||
Code: t.GetString("code"),
|
||||
Name: t.GetString("name"),
|
||||
Code: t.GetString("code"),
|
||||
Description: t.GetString("description"),
|
||||
})
|
||||
}
|
||||
return &pb.FindAllDNSProviderTypesResponse{ProviderTypes: result}, nil
|
||||
}
|
||||
|
||||
// 取得某个类型的所有服务商
|
||||
// FindAllEnabledDNSProvidersWithType 取得某个类型的所有服务商
|
||||
func (this *DNSProviderService) FindAllEnabledDNSProvidersWithType(ctx context.Context, req *pb.FindAllEnabledDNSProvidersWithTypeRequest) (*pb.FindAllEnabledDNSProvidersWithTypeResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
|
||||
@@ -7,12 +7,12 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// 访问日志相关服务
|
||||
// HTTPAccessLogService 访问日志相关服务
|
||||
type HTTPAccessLogService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建访问日志
|
||||
// CreateHTTPAccessLogs 创建访问日志
|
||||
func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req *pb.CreateHTTPAccessLogsRequest) (*pb.CreateHTTPAccessLogsResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode)
|
||||
@@ -20,13 +20,13 @@ func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(req.AccessLogs) == 0 {
|
||||
if len(req.HttpAccessLogs) == 0 {
|
||||
return &pb.CreateHTTPAccessLogsResponse{}, nil
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = models.SharedHTTPAccessLogDAO.CreateHTTPAccessLogs(tx, req.AccessLogs)
|
||||
err = models.SharedHTTPAccessLogDAO.CreateHTTPAccessLogs(tx, req.HttpAccessLogs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req
|
||||
return &pb.CreateHTTPAccessLogsResponse{}, nil
|
||||
}
|
||||
|
||||
// 列出单页访问日志
|
||||
// ListHTTPAccessLogs 列出单页访问日志
|
||||
func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *pb.ListHTTPAccessLogsRequest) (*pb.ListHTTPAccessLogsResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -59,7 +59,7 @@ func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *p
|
||||
}
|
||||
}
|
||||
|
||||
accessLogs, requestId, hasMore, err := models.SharedHTTPAccessLogDAO.ListAccessLogs(tx, req.RequestId, req.Size, req.Day, req.ServerId, req.Reverse, req.HasError, req.FirewallPolicyId, req.FirewallRuleGroupId, req.FirewallRuleSetId, req.HasFirewallPolicy, req.UserId)
|
||||
accessLogs, requestId, hasMore, err := models.SharedHTTPAccessLogDAO.ListAccessLogs(tx, req.RequestId, req.Size, req.Day, req.ServerId, req.Reverse, req.HasError, req.FirewallPolicyId, req.FirewallRuleGroupId, req.FirewallRuleSetId, req.HasFirewallPolicy, req.UserId, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -74,13 +74,14 @@ func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *p
|
||||
}
|
||||
|
||||
return &pb.ListHTTPAccessLogsResponse{
|
||||
AccessLogs: result,
|
||||
HasMore: hasMore,
|
||||
RequestId: requestId,
|
||||
HttpAccessLogs: result,
|
||||
AccessLogs: result, // TODO 仅仅为了兼容,当用户节点版本大于0.0.8时可以删除
|
||||
HasMore: hasMore,
|
||||
RequestId: requestId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 查找单个日志
|
||||
// FindHTTPAccessLog 查找单个日志
|
||||
func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb.FindHTTPAccessLogRequest) (*pb.FindHTTPAccessLogResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -95,7 +96,7 @@ func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb
|
||||
return nil, err
|
||||
}
|
||||
if accessLog == nil {
|
||||
return &pb.FindHTTPAccessLogResponse{AccessLog: nil}, nil
|
||||
return &pb.FindHTTPAccessLogResponse{HttpAccessLog: nil}, nil
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
@@ -110,5 +111,5 @@ func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.FindHTTPAccessLogResponse{AccessLog: a}, nil
|
||||
return &pb.FindHTTPAccessLogResponse{HttpAccessLog: a}, nil
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func (this *HTTPCachePolicyService) CountAllEnabledHTTPCachePolicies(ctx context
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
count, err := models.SharedHTTPCachePolicyDAO.CountAllEnabledHTTPCachePolicies(tx)
|
||||
count, err := models.SharedHTTPCachePolicyDAO.CountAllEnabledHTTPCachePolicies(tx, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -117,7 +117,7 @@ func (this *HTTPCachePolicyService) ListEnabledHTTPCachePolicies(ctx context.Con
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
cachePolicies, err := models.SharedHTTPCachePolicyDAO.ListEnabledHTTPCachePolicies(tx, req.Offset, req.Size)
|
||||
cachePolicies, err := models.SharedHTTPCachePolicyDAO.ListEnabledHTTPCachePolicies(tx, req.Keyword, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ import (
|
||||
"net"
|
||||
)
|
||||
|
||||
// HTTP防火墙(WAF)相关服务
|
||||
// HTTPFirewallPolicyService HTTP防火墙(WAF)相关服务
|
||||
type HTTPFirewallPolicyService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 获取所有可用策略
|
||||
// FindAllEnabledHTTPFirewallPolicies 获取所有可用策略
|
||||
func (this *HTTPFirewallPolicyService) FindAllEnabledHTTPFirewallPolicies(ctx context.Context, req *pb.FindAllEnabledHTTPFirewallPoliciesRequest) (*pb.FindAllEnabledHTTPFirewallPoliciesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -50,7 +50,7 @@ func (this *HTTPFirewallPolicyService) FindAllEnabledHTTPFirewallPolicies(ctx co
|
||||
return &pb.FindAllEnabledHTTPFirewallPoliciesResponse{FirewallPolicies: result}, nil
|
||||
}
|
||||
|
||||
// 创建防火墙策略
|
||||
// CreateHTTPFirewallPolicy 创建防火墙策略
|
||||
func (this *HTTPFirewallPolicyService) CreateHTTPFirewallPolicy(ctx context.Context, req *pb.CreateHTTPFirewallPolicyRequest) (*pb.CreateHTTPFirewallPolicyResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -118,7 +118,7 @@ func (this *HTTPFirewallPolicyService) CreateHTTPFirewallPolicy(ctx context.Cont
|
||||
return &pb.CreateHTTPFirewallPolicyResponse{HttpFirewallPolicyId: policyId}, nil
|
||||
}
|
||||
|
||||
// 创建空防火墙策略
|
||||
// CreateEmptyHTTPFirewallPolicy 创建空防火墙策略
|
||||
func (this *HTTPFirewallPolicyService) CreateEmptyHTTPFirewallPolicy(ctx context.Context, req *pb.CreateEmptyHTTPFirewallPolicyRequest) (*pb.CreateEmptyHTTPFirewallPolicyResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -164,7 +164,7 @@ func (this *HTTPFirewallPolicyService) CreateEmptyHTTPFirewallPolicy(ctx context
|
||||
return &pb.CreateEmptyHTTPFirewallPolicyResponse{HttpFirewallPolicyId: policyId}, nil
|
||||
}
|
||||
|
||||
// 修改防火墙策略
|
||||
// UpdateHTTPFirewallPolicy 修改防火墙策略
|
||||
func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallPolicy(ctx context.Context, req *pb.UpdateHTTPFirewallPolicyRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -292,7 +292,7 @@ func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallPolicy(ctx context.Cont
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 修改分组信息
|
||||
// UpdateHTTPFirewallPolicyGroups 修改分组信息
|
||||
func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallPolicyGroups(ctx context.Context, req *pb.UpdateHTTPFirewallPolicyGroupsRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -317,7 +317,7 @@ func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallPolicyGroups(ctx contex
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 修改inbound信息
|
||||
// UpdateHTTPFirewallInboundConfig 修改inbound信息
|
||||
func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallInboundConfig(ctx context.Context, req *pb.UpdateHTTPFirewallInboundConfigRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -342,7 +342,7 @@ func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallInboundConfig(ctx conte
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 计算可用的防火墙策略数量
|
||||
// CountAllEnabledHTTPFirewallPolicies 计算可用的防火墙策略数量
|
||||
func (this *HTTPFirewallPolicyService) CountAllEnabledHTTPFirewallPolicies(ctx context.Context, req *pb.CountAllEnabledHTTPFirewallPoliciesRequest) (*pb.RPCCountResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -352,14 +352,14 @@ func (this *HTTPFirewallPolicyService) CountAllEnabledHTTPFirewallPolicies(ctx c
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
count, err := models.SharedHTTPFirewallPolicyDAO.CountAllEnabledFirewallPolicies(tx)
|
||||
count, err := models.SharedHTTPFirewallPolicyDAO.CountAllEnabledFirewallPolicies(tx, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// 列出单页的防火墙策略
|
||||
// ListEnabledHTTPFirewallPolicies 列出单页的防火墙策略
|
||||
func (this *HTTPFirewallPolicyService) ListEnabledHTTPFirewallPolicies(ctx context.Context, req *pb.ListEnabledHTTPFirewallPoliciesRequest) (*pb.ListEnabledHTTPFirewallPoliciesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -369,7 +369,7 @@ func (this *HTTPFirewallPolicyService) ListEnabledHTTPFirewallPolicies(ctx conte
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
policies, err := models.SharedHTTPFirewallPolicyDAO.ListEnabledFirewallPolicies(tx, req.Offset, req.Size)
|
||||
policies, err := models.SharedHTTPFirewallPolicyDAO.ListEnabledFirewallPolicies(tx, req.Keyword, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -389,7 +389,7 @@ func (this *HTTPFirewallPolicyService) ListEnabledHTTPFirewallPolicies(ctx conte
|
||||
return &pb.ListEnabledHTTPFirewallPoliciesResponse{HttpFirewallPolicies: result}, nil
|
||||
}
|
||||
|
||||
// 删除某个防火墙策略
|
||||
// DeleteHTTPFirewallPolicy 删除某个防火墙策略
|
||||
func (this *HTTPFirewallPolicyService) DeleteHTTPFirewallPolicy(ctx context.Context, req *pb.DeleteHTTPFirewallPolicyRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -407,7 +407,7 @@ func (this *HTTPFirewallPolicyService) DeleteHTTPFirewallPolicy(ctx context.Cont
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 查找单个防火墙配置
|
||||
// FindEnabledHTTPFirewallPolicyConfig 查找单个防火墙配置
|
||||
func (this *HTTPFirewallPolicyService) FindEnabledHTTPFirewallPolicyConfig(ctx context.Context, req *pb.FindEnabledHTTPFirewallPolicyConfigRequest) (*pb.FindEnabledHTTPFirewallPolicyConfigResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -441,7 +441,7 @@ func (this *HTTPFirewallPolicyService) FindEnabledHTTPFirewallPolicyConfig(ctx c
|
||||
return &pb.FindEnabledHTTPFirewallPolicyConfigResponse{HttpFirewallPolicyJSON: configJSON}, nil
|
||||
}
|
||||
|
||||
// 获取防火墙的基本信息
|
||||
// FindEnabledHTTPFirewallPolicy 获取防火墙的基本信息
|
||||
func (this *HTTPFirewallPolicyService) FindEnabledHTTPFirewallPolicy(ctx context.Context, req *pb.FindEnabledHTTPFirewallPolicyRequest) (*pb.FindEnabledHTTPFirewallPolicyResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -475,7 +475,7 @@ func (this *HTTPFirewallPolicyService) FindEnabledHTTPFirewallPolicy(ctx context
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// 导入策略数据
|
||||
// ImportHTTPFirewallPolicy 导入策略数据
|
||||
func (this *HTTPFirewallPolicyService) ImportHTTPFirewallPolicy(ctx context.Context, req *pb.ImportHTTPFirewallPolicyRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
@@ -633,7 +633,7 @@ func (this *HTTPFirewallPolicyService) ImportHTTPFirewallPolicy(ctx context.Cont
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 检查IP状态
|
||||
// CheckHTTPFirewallPolicyIPStatus 检查IP状态
|
||||
func (this *HTTPFirewallPolicyService) CheckHTTPFirewallPolicyIPStatus(ctx context.Context, req *pb.CheckHTTPFirewallPolicyIPStatusRequest) (*pb.CheckHTTPFirewallPolicyIPStatusResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
)
|
||||
|
||||
// HTTPLocationService 路径规则相关服务
|
||||
type HTTPLocationService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建路径规则
|
||||
// CreateHTTPLocation 创建路径规则
|
||||
func (this *HTTPLocationService) CreateHTTPLocation(ctx context.Context, req *pb.CreateHTTPLocationRequest) (*pb.CreateHTTPLocationResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -23,7 +24,7 @@ func (this *HTTPLocationService) CreateHTTPLocation(ctx context.Context, req *pb
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
locationId, err := models.SharedHTTPLocationDAO.CreateLocation(tx, req.ParentId, req.Name, req.Pattern, req.Description, req.IsBreak)
|
||||
locationId, err := models.SharedHTTPLocationDAO.CreateLocation(tx, req.ParentId, req.Name, req.Pattern, req.Description, req.IsBreak, req.CondsJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -31,7 +32,7 @@ func (this *HTTPLocationService) CreateHTTPLocation(ctx context.Context, req *pb
|
||||
return &pb.CreateHTTPLocationResponse{LocationId: locationId}, nil
|
||||
}
|
||||
|
||||
// 修改路径规则
|
||||
// UpdateHTTPLocation 修改路径规则
|
||||
func (this *HTTPLocationService) UpdateHTTPLocation(ctx context.Context, req *pb.UpdateHTTPLocationRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -41,7 +42,7 @@ func (this *HTTPLocationService) UpdateHTTPLocation(ctx context.Context, req *pb
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = models.SharedHTTPLocationDAO.UpdateLocation(tx, req.LocationId, req.Name, req.Pattern, req.Description, req.IsOn, req.IsBreak)
|
||||
err = models.SharedHTTPLocationDAO.UpdateLocation(tx, req.LocationId, req.Name, req.Pattern, req.Description, req.IsOn, req.IsBreak, req.CondsJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -49,7 +50,7 @@ func (this *HTTPLocationService) UpdateHTTPLocation(ctx context.Context, req *pb
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 查找路径规则配置
|
||||
// FindEnabledHTTPLocationConfig 查找路径规则配置
|
||||
func (this *HTTPLocationService) FindEnabledHTTPLocationConfig(ctx context.Context, req *pb.FindEnabledHTTPLocationConfigRequest) (*pb.FindEnabledHTTPLocationConfigResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -70,7 +71,7 @@ func (this *HTTPLocationService) FindEnabledHTTPLocationConfig(ctx context.Conte
|
||||
return &pb.FindEnabledHTTPLocationConfigResponse{LocationJSON: configJSON}, nil
|
||||
}
|
||||
|
||||
// 删除路径规则
|
||||
// DeleteHTTPLocation 删除路径规则
|
||||
func (this *HTTPLocationService) DeleteHTTPLocation(ctx context.Context, req *pb.DeleteHTTPLocationRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -87,7 +88,7 @@ func (this *HTTPLocationService) DeleteHTTPLocation(ctx context.Context, req *pb
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 查找反向代理设置
|
||||
// FindAndInitHTTPLocationReverseProxyConfig 查找反向代理设置
|
||||
func (this *HTTPLocationService) FindAndInitHTTPLocationReverseProxyConfig(ctx context.Context, req *pb.FindAndInitHTTPLocationReverseProxyConfigRequest) (*pb.FindAndInitHTTPLocationReverseProxyConfigResponse, error) {
|
||||
// 校验请求
|
||||
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -140,7 +141,7 @@ func (this *HTTPLocationService) FindAndInitHTTPLocationReverseProxyConfig(ctx c
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 初始化Web设置
|
||||
// FindAndInitHTTPLocationWebConfig 初始化Web设置
|
||||
func (this *HTTPLocationService) FindAndInitHTTPLocationWebConfig(ctx context.Context, req *pb.FindAndInitHTTPLocationWebConfigRequest) (*pb.FindAndInitHTTPLocationWebConfigResponse, error) {
|
||||
// 校验请求
|
||||
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -179,7 +180,7 @@ func (this *HTTPLocationService) FindAndInitHTTPLocationWebConfig(ctx context.Co
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 修改反向代理设置
|
||||
// UpdateHTTPLocationReverseProxy 修改反向代理设置
|
||||
func (this *HTTPLocationService) UpdateHTTPLocationReverseProxy(ctx context.Context, req *pb.UpdateHTTPLocationReverseProxyRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
|
||||
@@ -8,11 +8,12 @@ import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
// HTTPRewriteRuleService 重写规则相关服务
|
||||
type HTTPRewriteRuleService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建重写规则
|
||||
// CreateHTTPRewriteRule 创建重写规则
|
||||
func (this *HTTPRewriteRuleService) CreateHTTPRewriteRule(ctx context.Context, req *pb.CreateHTTPRewriteRuleRequest) (*pb.CreateHTTPRewriteRuleResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -22,7 +23,7 @@ func (this *HTTPRewriteRuleService) CreateHTTPRewriteRule(ctx context.Context, r
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
rewriteRuleId, err := models.SharedHTTPRewriteRuleDAO.CreateRewriteRule(tx, req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.WithQuery, req.IsOn)
|
||||
rewriteRuleId, err := models.SharedHTTPRewriteRuleDAO.CreateRewriteRule(tx, req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.WithQuery, req.IsOn, req.CondsJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -30,7 +31,7 @@ func (this *HTTPRewriteRuleService) CreateHTTPRewriteRule(ctx context.Context, r
|
||||
return &pb.CreateHTTPRewriteRuleResponse{RewriteRuleId: rewriteRuleId}, nil
|
||||
}
|
||||
|
||||
// 修改重写规则
|
||||
// UpdateHTTPRewriteRule 修改重写规则
|
||||
func (this *HTTPRewriteRuleService) UpdateHTTPRewriteRule(ctx context.Context, req *pb.UpdateHTTPRewriteRuleRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -40,7 +41,7 @@ func (this *HTTPRewriteRuleService) UpdateHTTPRewriteRule(ctx context.Context, r
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = models.SharedHTTPRewriteRuleDAO.UpdateRewriteRule(tx, req.RewriteRuleId, req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.WithQuery, req.IsOn)
|
||||
err = models.SharedHTTPRewriteRuleDAO.UpdateRewriteRule(tx, req.RewriteRuleId, req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.WithQuery, req.IsOn, req.CondsJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user