Compare commits
64 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d82c3abf6 | ||
|
|
55034efa87 | ||
|
|
3ff45d6f49 | ||
|
|
2b2c285c90 | ||
|
|
29ad1bc741 | ||
|
|
a263ab5938 | ||
|
|
6b34d32a8f | ||
|
|
3ff6ca5905 | ||
|
|
59ce71f72e | ||
|
|
49da653ed8 | ||
|
|
12db6910dc | ||
|
|
3ce1aa14b5 | ||
|
|
c67fbfa849 | ||
|
|
72b94c5035 | ||
|
|
01ea13d283 | ||
|
|
91378b26dd | ||
|
|
46dc74195b | ||
|
|
63316b6b23 | ||
|
|
1b8600e04d | ||
|
|
11141d022f | ||
|
|
8fd29bd81c | ||
|
|
820779e614 | ||
|
|
d45dc35ea6 | ||
|
|
b3280ee290 | ||
|
|
ff0e89aa5f | ||
|
|
6ff5ba67e8 | ||
|
|
e7474523ba | ||
|
|
0b928aed14 | ||
|
|
52dcd3da1f | ||
|
|
0260d6f735 | ||
|
|
364636ea61 | ||
|
|
12f816cb5e | ||
|
|
ef70d3465d | ||
|
|
a3a9e726cb | ||
|
|
0c65774c82 | ||
|
|
fa31e547a2 | ||
|
|
c406536511 | ||
|
|
35f48e4f5d | ||
|
|
851e982ab9 | ||
|
|
956186613f | ||
|
|
aeb2a0c4f9 | ||
|
|
4f05671af2 | ||
|
|
6b6170b183 | ||
|
|
29f2aa2654 | ||
|
|
ed234b58bc | ||
|
|
cfcad531f4 | ||
|
|
12ffdfd849 | ||
|
|
c2be7d70b8 | ||
|
|
efe723bc51 | ||
|
|
501559e3b8 | ||
|
|
de8b3c2195 | ||
|
|
31aeb21e09 | ||
|
|
f1cf89a78c | ||
|
|
d6df5e1c48 | ||
|
|
049b9b52dd | ||
|
|
c53259008e | ||
|
|
6e0a0a099d | ||
|
|
f4313de55f | ||
|
|
fd3823255c | ||
|
|
79823f4317 | ||
|
|
99fd015066 | ||
|
|
f63c2d867d | ||
|
|
eefb497c20 | ||
|
|
1c8564d817 |
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -68,6 +69,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Println("done")
|
fmt.Println("done")
|
||||||
})
|
})
|
||||||
|
app.On("goman", func() {
|
||||||
|
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
||||||
|
reply, err := sock.Send(&gosock.Command{Code: "goman"})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[ERROR]" + err.Error())
|
||||||
|
} else {
|
||||||
|
instancesJSON, err := json.MarshalIndent(reply.Params, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[ERROR]" + err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(string(instancesJSON))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
app.Run(func() {
|
app.Run(func() {
|
||||||
nodes.NewAPINode().Start()
|
nodes.NewAPINode().Start()
|
||||||
})
|
})
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -15,9 +15,9 @@ require (
|
|||||||
github.com/go-sql-driver/mysql v1.5.0
|
github.com/go-sql-driver/mysql v1.5.0
|
||||||
github.com/go-yaml/yaml v2.1.0+incompatible
|
github.com/go-yaml/yaml v2.1.0+incompatible
|
||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.2
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13
|
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3
|
||||||
github.com/json-iterator/go v1.1.11 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
||||||
github.com/mozillazg/go-pinyin v0.18.0
|
github.com/mozillazg/go-pinyin v0.18.0
|
||||||
github.com/pkg/sftp v1.12.0
|
github.com/pkg/sftp v1.12.0
|
||||||
|
|||||||
6
go.sum
6
go.sum
@@ -234,6 +234,8 @@ github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7Q
|
|||||||
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13 h1:HuEJ5xJfujW1Q6rNDhOu5LQXEBB2qLPah3jYslT8Gz4=
|
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13 h1:HuEJ5xJfujW1Q6rNDhOu5LQXEBB2qLPah3jYslT8Gz4=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24 h1:1cGulkD2SNJJRok5OKwyhP/Ddm+PgSWKOupn0cR36/A=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20211026123858-7de7a21cad24/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||||
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
||||||
@@ -250,6 +252,8 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
|
|||||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
|
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
|
||||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
@@ -320,6 +324,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
|
|||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
github.com/mozillazg/go-pinyin v0.18.0 h1:hQompXO23/0ohH8YNjvfsAITnCQImCiR/Fny8EhIeW0=
|
github.com/mozillazg/go-pinyin v0.18.0 h1:hQompXO23/0ohH8YNjvfsAITnCQImCiR/Fny8EhIeW0=
|
||||||
github.com/mozillazg/go-pinyin v0.18.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
github.com/mozillazg/go-pinyin v0.18.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.3.5"
|
Version = "0.3.7"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,10 +18,10 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.3.5"
|
NodeVersion = "0.3.7"
|
||||||
UserNodeVersion = "0.0.10"
|
UserNodeVersion = "0.2.1"
|
||||||
AuthorityNodeVersion = "0.0.2"
|
AuthorityNodeVersion = "0.0.2"
|
||||||
MonitorNodeVersion = "0.0.3"
|
MonitorNodeVersion = "0.0.3"
|
||||||
DNSNodeVersion = "0.2.0"
|
DNSNodeVersion = "0.2.1"
|
||||||
ReportNodeVersion = "0.1.0"
|
ReportNodeVersion = "0.1.0"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package accounts
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -16,7 +17,7 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
go func() {
|
goman.New(func() {
|
||||||
// 自动支付账单任务
|
// 自动支付账单任务
|
||||||
var ticker = time.NewTicker(12 * time.Hour)
|
var ticker = time.NewTicker(12 * time.Hour)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
@@ -29,7 +30,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +109,7 @@ func (this *UserAccountDAO) UpdateUserAccount(tx *dbs.Tx, accountId int64, delta
|
|||||||
if account == nil {
|
if account == nil {
|
||||||
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
||||||
}
|
}
|
||||||
var userId = int64(account.Id)
|
var userId = int64(account.UserId)
|
||||||
var deltaFloat64 = float64(delta)
|
var deltaFloat64 = float64(delta)
|
||||||
if deltaFloat64 < 0 && account.Total < -deltaFloat64 {
|
if deltaFloat64 < 0 && account.Total < -deltaFloat64 {
|
||||||
return errors.New("not enough account quota to decrease")
|
return errors.New("not enough account quota to decrease")
|
||||||
@@ -235,3 +236,18 @@ func (this *UserAccountDAO) PayBills(tx *dbs.Tx) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckUserAccount 检查用户账户
|
||||||
|
func (this *UserAccountDAO) CheckUserAccount(tx *dbs.Tx, userId int64, accountId int64) error {
|
||||||
|
exists, err := this.Query(tx).
|
||||||
|
Pk(accountId).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return models.ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,9 +8,11 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -321,3 +323,51 @@ func (this *APINodeDAO) CountAllEnabledAPINodesWithSSLPolicyIds(tx *dbs.Tx, sslP
|
|||||||
Param("policyIds", strings.Join(policyStringIds, ",")).
|
Param("policyIds", strings.Join(policyStringIds, ",")).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledAPIAccessIPs 获取所有的API可访问IP地址
|
||||||
|
func (this *APINodeDAO) FindAllEnabledAPIAccessIPs(tx *dbs.Tx, cacheMap *utils.CacheMap) ([]string, error) {
|
||||||
|
var cacheKey = this.Table + ":FindAllEnabledAPIAccessIPs"
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.([]string), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
State(APINodeStateEnabled).
|
||||||
|
Result("JSON_EXTRACT(accessAddrs, '$[*].host') AS host").
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
for _, one := range ones {
|
||||||
|
var host = one.GetString("host")
|
||||||
|
if len(host) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var ips = []string{}
|
||||||
|
err = json.Unmarshal([]byte(host), &ips)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ip := range ips {
|
||||||
|
if !lists.ContainsString(result, ip) {
|
||||||
|
if net.ParseIP(ip) == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap.Put(cacheKey, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -27,6 +28,12 @@ func TestAPINodeDAO_FindEnabledAPINodeIdWithAddr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAPINodeDAO_FindAllEnabledAPIAccessIPs(t *testing.T) {
|
||||||
|
var cacheMap = utils.NewCacheMap()
|
||||||
|
t.Log(NewAPINodeDAO().FindAllEnabledAPIAccessIPs(nil, cacheMap))
|
||||||
|
t.Log(NewAPINodeDAO().FindAllEnabledAPIAccessIPs(nil, cacheMap))
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkAPINodeDAO_New(b *testing.B) {
|
func BenchmarkAPINodeDAO_New(b *testing.B) {
|
||||||
runtime.GOMAXPROCS(1)
|
runtime.GOMAXPROCS(1)
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -49,7 +50,9 @@ type NSAccessLogDAOWrapper struct {
|
|||||||
func init() {
|
func init() {
|
||||||
initializer := NewDBNodeInitializer()
|
initializer := NewDBNodeInitializer()
|
||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
go initializer.Start()
|
goman.New(func() {
|
||||||
|
initializer.Start()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,7 +356,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
||||||
|
|
||||||
// 创建节点日志
|
// 创建节点日志
|
||||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
|
||||||
if createLogErr != nil {
|
if createLogErr != nil {
|
||||||
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
||||||
}
|
}
|
||||||
@@ -397,7 +400,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
||||||
|
|
||||||
// 创建节点日志
|
// 创建节点日志
|
||||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
|
||||||
if createLogErr != nil {
|
if createLogErr != nil {
|
||||||
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,23 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/zero"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"net"
|
"net"
|
||||||
@@ -26,10 +33,53 @@ type HTTPAccessLogDAO dbs.DAO
|
|||||||
|
|
||||||
var SharedHTTPAccessLogDAO *HTTPAccessLogDAO
|
var SharedHTTPAccessLogDAO *HTTPAccessLogDAO
|
||||||
|
|
||||||
|
// 队列
|
||||||
|
var oldAccessLogQueue = make(chan *pb.HTTPAccessLog)
|
||||||
|
var accessLogQueue = make(chan *pb.HTTPAccessLog, 10_000)
|
||||||
|
var accessLogQueueMaxLength = 100_000
|
||||||
|
var accessLogQueuePercent = 100 // 0-100
|
||||||
|
var accessLogCountPerSecond = 10_000 // 0 表示不限制
|
||||||
|
var accessLogConfigJSON = []byte{}
|
||||||
|
var accessLogQueueChanged = make(chan zero.Zero, 1)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dbs.OnReady(func() {
|
dbs.OnReady(func() {
|
||||||
SharedHTTPAccessLogDAO = NewHTTPAccessLogDAO()
|
SharedHTTPAccessLogDAO = NewHTTPAccessLogDAO()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 队列相关
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 检查队列变化
|
||||||
|
goman.New(func() {
|
||||||
|
var ticker = time.NewTicker(60 * time.Second)
|
||||||
|
|
||||||
|
// 先执行一次初始化
|
||||||
|
SharedHTTPAccessLogDAO.SetupQueue()
|
||||||
|
|
||||||
|
// 循环执行
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
SharedHTTPAccessLogDAO.SetupQueue()
|
||||||
|
case <-accessLogQueueChanged:
|
||||||
|
SharedHTTPAccessLogDAO.SetupQueue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// 导出队列内容
|
||||||
|
goman.New(func() {
|
||||||
|
var ticker = time.NewTicker(1 * time.Second)
|
||||||
|
for range ticker.C {
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := SharedHTTPAccessLogDAO.DumpAccessLogsFromQueue(tx, accessLogCountPerSecond)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "dump access logs failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
||||||
@@ -45,6 +95,31 @@ func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
|||||||
|
|
||||||
// CreateHTTPAccessLogs 创建访问日志
|
// CreateHTTPAccessLogs 创建访问日志
|
||||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.HTTPAccessLog) error {
|
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
// 写入队列
|
||||||
|
var queue = accessLogQueue // 这样写非常重要,防止在写入过程中队列有切换
|
||||||
|
for _, accessLog := range accessLogs {
|
||||||
|
if accessLog.FirewallPolicyId == 0 { // 如果是WAF记录,则采取采样率
|
||||||
|
// 采样率
|
||||||
|
if accessLogQueuePercent <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if accessLogQueuePercent < 100 && rands.Int(1, 100) > accessLogQueuePercent {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case queue <- accessLog:
|
||||||
|
default:
|
||||||
|
// 超出的丢弃
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DumpAccessLogsFromQueue 从队列导入访问日志
|
||||||
|
func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(tx *dbs.Tx, size int) error {
|
||||||
dao := randomHTTPAccessLogDAO()
|
dao := randomHTTPAccessLogDAO()
|
||||||
if dao == nil {
|
if dao == nil {
|
||||||
dao = &HTTPAccessLogDAOWrapper{
|
dao = &HTTPAccessLogDAOWrapper{
|
||||||
@@ -52,75 +127,102 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.
|
|||||||
NodeId: 0,
|
NodeId: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.CreateHTTPAccessLogsWithDAO(tx, dao, accessLogs)
|
|
||||||
|
if size <= 0 {
|
||||||
|
size = 1_000_000
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制变量,防止中途改变
|
||||||
|
var oldQueue = oldAccessLogQueue
|
||||||
|
var newQueue = accessLogQueue
|
||||||
|
|
||||||
|
Loop:
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
// old
|
||||||
|
select {
|
||||||
|
case accessLog := <-oldQueue:
|
||||||
|
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue Loop
|
||||||
|
default:
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// new
|
||||||
|
select {
|
||||||
|
case accessLog := <-newQueue:
|
||||||
|
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue Loop
|
||||||
|
default:
|
||||||
|
break Loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateHTTPAccessLogsWithDAO 使用特定的DAO创建访问日志
|
// CreateHTTPAccessLog 写入单条访问日志
|
||||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *HTTPAccessLogDAOWrapper, accessLogs []*pb.HTTPAccessLog) error {
|
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
|
||||||
if daoWrapper == nil {
|
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
|
||||||
return errors.New("dao should not be nil")
|
tableDef, err := findHTTPAccessLogTable(dao.Instance, day, false)
|
||||||
}
|
if err != nil {
|
||||||
if len(accessLogs) == 0 {
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dao := daoWrapper.DAO
|
fields := map[string]interface{}{}
|
||||||
|
fields["serverId"] = accessLog.ServerId
|
||||||
|
fields["nodeId"] = accessLog.NodeId
|
||||||
|
fields["status"] = accessLog.Status
|
||||||
|
fields["createdAt"] = accessLog.Timestamp
|
||||||
|
fields["requestId"] = accessLog.RequestId
|
||||||
|
fields["firewallPolicyId"] = accessLog.FirewallPolicyId
|
||||||
|
fields["firewallRuleGroupId"] = accessLog.FirewallRuleGroupId
|
||||||
|
fields["firewallRuleSetId"] = accessLog.FirewallRuleSetId
|
||||||
|
fields["firewallRuleId"] = accessLog.FirewallRuleId
|
||||||
|
|
||||||
// TODO 改成事务批量提交,以加快速度
|
if len(accessLog.RequestBody) > 0 {
|
||||||
|
fields["requestBody"] = accessLog.RequestBody
|
||||||
|
accessLog.RequestBody = nil
|
||||||
|
}
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
if tableDef.HasRemoteAddr {
|
||||||
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
|
fields["remoteAddr"] = accessLog.RemoteAddr
|
||||||
tableDef, err := findHTTPAccessLogTable(dao.Instance, day, false)
|
}
|
||||||
if err != nil {
|
if tableDef.HasDomain {
|
||||||
return err
|
fields["domain"] = accessLog.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := map[string]interface{}{}
|
content, err := json.Marshal(accessLog)
|
||||||
fields["serverId"] = accessLog.ServerId
|
if err != nil {
|
||||||
fields["nodeId"] = accessLog.NodeId
|
return err
|
||||||
fields["status"] = accessLog.Status
|
}
|
||||||
fields["createdAt"] = accessLog.Timestamp
|
fields["content"] = content
|
||||||
fields["requestId"] = accessLog.RequestId
|
|
||||||
fields["firewallPolicyId"] = accessLog.FirewallPolicyId
|
|
||||||
fields["firewallRuleGroupId"] = accessLog.FirewallRuleGroupId
|
|
||||||
fields["firewallRuleSetId"] = accessLog.FirewallRuleSetId
|
|
||||||
fields["firewallRuleId"] = accessLog.FirewallRuleId
|
|
||||||
|
|
||||||
// TODO 根据集群、服务设置获取IP
|
_, err = dao.Query(tx).
|
||||||
if tableDef.HasRemoteAddr {
|
Table(tableDef.Name).
|
||||||
fields["remoteAddr"] = accessLog.RemoteAddr
|
Sets(fields).
|
||||||
}
|
Insert()
|
||||||
if tableDef.HasDomain {
|
if err != nil {
|
||||||
fields["domain"] = accessLog.Host
|
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
|
||||||
}
|
if strings.Contains(err.Error(), "1146") {
|
||||||
|
tableDef, err = findHTTPAccessLogTable(dao.Instance, day, true)
|
||||||
content, err := json.Marshal(accessLog)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
fields["content"] = content
|
|
||||||
|
|
||||||
_, err = dao.Query(tx).
|
|
||||||
Table(tableDef.Name).
|
|
||||||
Sets(fields).
|
|
||||||
Insert()
|
|
||||||
if err != nil {
|
|
||||||
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
|
|
||||||
if strings.Contains(err.Error(), "1146") {
|
|
||||||
tableDef, err = findHTTPAccessLogTable(dao.Instance, day, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = dao.Query(tx).
|
|
||||||
Table(tableDef.Name).
|
|
||||||
Sets(fields).
|
|
||||||
Insert()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logs.Println("HTTP_ACCESS_LOG", err.Error())
|
|
||||||
}
|
}
|
||||||
|
_, err = dao.Query(tx).
|
||||||
|
Table(tableDef.Name).
|
||||||
|
Sets(fields).
|
||||||
|
Insert()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,6 +432,11 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
|
|||||||
query.Param("intKeyword2", types.Int(pieces[1]))
|
query.Param("intKeyword2", types.Int(pieces[1]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if regexp.MustCompile(`^\d{20,}\s*\.?$`).MatchString(keyword) {
|
||||||
|
where += " OR requestId=:requestId"
|
||||||
|
query.Param("requestId", strings.TrimRight(keyword, ". "))
|
||||||
|
}
|
||||||
|
|
||||||
query.Where("("+where+")").
|
query.Where("("+where+")").
|
||||||
Param("keyword", "%"+keyword+"%")
|
Param("keyword", "%"+keyword+"%")
|
||||||
if useOriginKeyword {
|
if useOriginKeyword {
|
||||||
@@ -459,3 +566,42 @@ func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId s
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetupQueue 建立队列
|
||||||
|
func (this *HTTPAccessLogDAO) SetupQueue() {
|
||||||
|
configJSON, err := SharedSysSettingDAO.ReadSetting(nil, systemconfigs.SettingCodeAccessLogQueue)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "read settings failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(configJSON) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if bytes.Compare(accessLogConfigJSON, configJSON) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
accessLogConfigJSON = configJSON
|
||||||
|
|
||||||
|
var config = &serverconfigs.AccessLogQueueConfig{}
|
||||||
|
err = json.Unmarshal(configJSON, config)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "decode settings failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accessLogQueuePercent = config.Percent
|
||||||
|
accessLogCountPerSecond = config.CountPerSecond
|
||||||
|
if config.MaxLength <= 0 {
|
||||||
|
config.MaxLength = 100_000
|
||||||
|
}
|
||||||
|
|
||||||
|
if accessLogQueueMaxLength != config.MaxLength {
|
||||||
|
accessLogQueueMaxLength = config.MaxLength
|
||||||
|
oldAccessLogQueue = accessLogQueue
|
||||||
|
accessLogQueue = make(chan *pb.HTTPAccessLog, config.MaxLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
remotelogs.Println("HTTP_ACCESS_LOG_QUEUE", "change queue max length: "+types.String(config.MaxLength)+", percent: "+types.String(config.Percent)+", countPerSecond: "+types.String(config.CountPerSecond))
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateHTTPAccessLogs(t *testing.T) {
|
func TestCreateHTTPAccessLog(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
|
|
||||||
err := NewDBNodeInitializer().loop()
|
err := NewDBNodeInitializer().loop()
|
||||||
@@ -26,9 +28,19 @@ func TestCreateHTTPAccessLogs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
dao := randomHTTPAccessLogDAO()
|
dao := randomHTTPAccessLogDAO()
|
||||||
t.Log("dao:", dao)
|
t.Log("dao:", dao)
|
||||||
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLogsWithDAO(tx, dao, []*pb.HTTPAccessLog{accessLog})
|
|
||||||
if err != nil {
|
// 先初始化
|
||||||
t.Fatal(err)
|
_ = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
|
||||||
|
var before = time.Now()
|
||||||
|
defer func() {
|
||||||
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||||
|
}()
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ func (this *HTTPAccessLog) ToPB() (*pb.HTTPAccessLog, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
p.RequestId = this.RequestId
|
p.RequestId = this.RequestId
|
||||||
|
p.RequestBody = []byte(this.RequestBody)
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, group
|
|||||||
config.Name = group.Name
|
config.Name = group.Name
|
||||||
config.Description = group.Description
|
config.Description = group.Description
|
||||||
config.Code = group.Code
|
config.Code = group.Code
|
||||||
|
config.IsTemplate = group.IsTemplate == 1
|
||||||
|
|
||||||
if IsNotNull(group.Sets) {
|
if IsNotNull(group.Sets) {
|
||||||
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
||||||
@@ -125,6 +126,7 @@ func (this *HTTPFirewallRuleGroupDAO) CreateGroupFromConfig(tx *dbs.Tx, groupCon
|
|||||||
op.Description = groupConfig.Description
|
op.Description = groupConfig.Description
|
||||||
op.State = HTTPFirewallRuleGroupStateEnabled
|
op.State = HTTPFirewallRuleGroupStateEnabled
|
||||||
op.Code = groupConfig.Code
|
op.Code = groupConfig.Code
|
||||||
|
op.IsTemplate = groupConfig.IsTemplate
|
||||||
|
|
||||||
// sets
|
// sets
|
||||||
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
||||||
@@ -178,7 +180,7 @@ func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateGroup 修改分组
|
// UpdateGroup 修改分组
|
||||||
func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isOn bool, name string, description string) error {
|
func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isOn bool, name string, code string, description string) error {
|
||||||
if groupId <= 0 {
|
if groupId <= 0 {
|
||||||
return errors.New("invalid groupId")
|
return errors.New("invalid groupId")
|
||||||
}
|
}
|
||||||
@@ -186,6 +188,7 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isO
|
|||||||
op.Id = groupId
|
op.Id = groupId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
|
op.Code = code
|
||||||
op.Description = description
|
op.Description = description
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 防火墙规则分组
|
// HTTPFirewallRuleGroup 防火墙规则分组
|
||||||
type HTTPFirewallRuleGroup struct {
|
type HTTPFirewallRuleGroup struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
Code string `field:"code"` // 代号
|
Code string `field:"code"` // 代号
|
||||||
|
IsTemplate uint8 `field:"isTemplate"` // 是否为预置模板
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
@@ -20,6 +21,7 @@ type HTTPFirewallRuleGroupOperator struct {
|
|||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
Code interface{} // 代号
|
Code interface{} // 代号
|
||||||
|
IsTemplate interface{} // 是否为预置模板
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ func (this *HTTPFirewallRuleSetDAO) ComposeFirewallRuleSet(tx *dbs.Tx, setId int
|
|||||||
config.Description = set.Description
|
config.Description = set.Description
|
||||||
config.Code = set.Code
|
config.Code = set.Code
|
||||||
config.Connector = set.Connector
|
config.Connector = set.Connector
|
||||||
|
config.IgnoreLocal = set.IgnoreLocal == 1
|
||||||
|
|
||||||
if IsNotNull(set.Rules) {
|
if IsNotNull(set.Rules) {
|
||||||
ruleRefs := []*firewallconfigs.HTTPFirewallRuleRef{}
|
ruleRefs := []*firewallconfigs.HTTPFirewallRuleRef{}
|
||||||
@@ -139,6 +140,7 @@ func (this *HTTPFirewallRuleSetDAO) CreateOrUpdateSetFromConfig(tx *dbs.Tx, setC
|
|||||||
op.Name = setConfig.Name
|
op.Name = setConfig.Name
|
||||||
op.Description = setConfig.Description
|
op.Description = setConfig.Description
|
||||||
op.Connector = setConfig.Connector
|
op.Connector = setConfig.Connector
|
||||||
|
op.IgnoreLocal = setConfig.IgnoreLocal
|
||||||
|
|
||||||
if len(setConfig.Actions) == 0 {
|
if len(setConfig.Actions) == 0 {
|
||||||
op.Actions = "[]"
|
op.Actions = "[]"
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type HTTPFirewallRuleSet struct {
|
|||||||
Action string `field:"action"` // 执行的动作(过期)
|
Action string `field:"action"` // 执行的动作(过期)
|
||||||
ActionOptions string `field:"actionOptions"` // 动作的选项(过期)
|
ActionOptions string `field:"actionOptions"` // 动作的选项(过期)
|
||||||
Actions string `field:"actions"` // 一组动作
|
Actions string `field:"actions"` // 一组动作
|
||||||
|
IgnoreLocal uint8 `field:"ignoreLocal"` // 忽略局域网请求
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPFirewallRuleSetOperator struct {
|
type HTTPFirewallRuleSetOperator struct {
|
||||||
@@ -33,6 +34,7 @@ type HTTPFirewallRuleSetOperator struct {
|
|||||||
Action interface{} // 执行的动作(过期)
|
Action interface{} // 执行的动作(过期)
|
||||||
ActionOptions interface{} // 动作的选项(过期)
|
ActionOptions interface{} // 动作的选项(过期)
|
||||||
Actions interface{} // 一组动作
|
Actions interface{} // 一组动作
|
||||||
|
IgnoreLocal interface{} // 忽略局域网请求
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPFirewallRuleSetOperator() *HTTPFirewallRuleSetOperator {
|
func NewHTTPFirewallRuleSetOperator() *HTTPFirewallRuleSetOperator {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -80,22 +80,69 @@ func (this *HTTPHeaderDAO) FindHTTPHeaderName(tx *dbs.Tx, id int64) (string, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateHeader 创建Header
|
// CreateHeader 创建Header
|
||||||
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, name string, value string) (int64, error) {
|
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, userId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) (int64, error) {
|
||||||
op := NewHTTPHeaderOperator()
|
op := NewHTTPHeaderOperator()
|
||||||
|
op.UserId = userId
|
||||||
op.State = HTTPHeaderStateEnabled
|
op.State = HTTPHeaderStateEnabled
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Value = value
|
op.Value = value
|
||||||
|
|
||||||
statusConfig := &shared.HTTPStatusConfig{
|
// status
|
||||||
Always: true,
|
var statusConfig *shared.HTTPStatusConfig
|
||||||
|
if len(status) == 0 {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: false,
|
||||||
|
Codes: status,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
statusJSON, err := json.Marshal(statusConfig)
|
statusJSON, err := json.Marshal(statusConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
op.Status = statusJSON
|
op.Status = statusJSON
|
||||||
|
|
||||||
|
op.DisableRedirect = disableRedirect
|
||||||
|
op.ShouldAppend = shouldAppend
|
||||||
|
op.ShouldReplace = shouldReplace
|
||||||
|
|
||||||
|
if len(replaceValues) == 0 {
|
||||||
|
op.ReplaceValues = "[]"
|
||||||
|
} else {
|
||||||
|
replaceValuesJSON, err := json.Marshal(replaceValues)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.ReplaceValues = replaceValuesJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// methods
|
||||||
|
if len(methods) == 0 {
|
||||||
|
op.Methods = "[]"
|
||||||
|
} else {
|
||||||
|
methodsJSON, err := json.Marshal(methods)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Methods = methodsJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// domains
|
||||||
|
if len(domains) == 0 {
|
||||||
|
op.Domains = "[]"
|
||||||
|
} else {
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
}
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -104,7 +151,7 @@ func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, name string, value string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateHeader 修改Header
|
// UpdateHeader 修改Header
|
||||||
func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string, value string) error {
|
func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) error {
|
||||||
if headerId <= 0 {
|
if headerId <= 0 {
|
||||||
return errors.New("invalid headerId")
|
return errors.New("invalid headerId")
|
||||||
}
|
}
|
||||||
@@ -113,7 +160,63 @@ func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string,
|
|||||||
op.Id = headerId
|
op.Id = headerId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Value = value
|
op.Value = value
|
||||||
err := this.Save(tx, op)
|
|
||||||
|
// status
|
||||||
|
var statusConfig *shared.HTTPStatusConfig
|
||||||
|
if len(status) == 0 {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
statusConfig = &shared.HTTPStatusConfig{
|
||||||
|
Always: false,
|
||||||
|
Codes: status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
statusJSON, err := json.Marshal(statusConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Status = statusJSON
|
||||||
|
|
||||||
|
op.DisableRedirect = disableRedirect
|
||||||
|
op.ShouldAppend = shouldAppend
|
||||||
|
op.ShouldReplace = shouldReplace
|
||||||
|
|
||||||
|
if len(replaceValues) == 0 {
|
||||||
|
op.ReplaceValues = "[]"
|
||||||
|
} else {
|
||||||
|
replaceValuesJSON, err := json.Marshal(replaceValues)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.ReplaceValues = replaceValuesJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// methods
|
||||||
|
if len(methods) == 0 {
|
||||||
|
op.Methods = "[]"
|
||||||
|
} else {
|
||||||
|
methodsJSON, err := json.Marshal(methods)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Methods = methodsJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
// domains
|
||||||
|
if len(domains) == 0 {
|
||||||
|
op.Domains = "[]"
|
||||||
|
} else {
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -136,7 +239,21 @@ func (this *HTTPHeaderDAO) ComposeHeaderConfig(tx *dbs.Tx, headerId int64) (*sha
|
|||||||
config.IsOn = header.IsOn == 1
|
config.IsOn = header.IsOn == 1
|
||||||
config.Name = header.Name
|
config.Name = header.Name
|
||||||
config.Value = header.Value
|
config.Value = header.Value
|
||||||
|
config.DisableRedirect = header.DisableRedirect == 1
|
||||||
|
config.ShouldAppend = header.ShouldAppend == 1
|
||||||
|
|
||||||
|
// replace
|
||||||
|
config.ShouldReplace = header.ShouldReplace == 1
|
||||||
|
if len(header.ReplaceValues) > 0 {
|
||||||
|
var values = []*shared.HTTPHeaderReplaceValue{}
|
||||||
|
err = json.Unmarshal([]byte(header.ReplaceValues), &values)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.ReplaceValues = values
|
||||||
|
}
|
||||||
|
|
||||||
|
// status
|
||||||
if len(header.Status) > 0 {
|
if len(header.Status) > 0 {
|
||||||
status := &shared.HTTPStatusConfig{}
|
status := &shared.HTTPStatusConfig{}
|
||||||
err = json.Unmarshal([]byte(header.Status), status)
|
err = json.Unmarshal([]byte(header.Status), status)
|
||||||
@@ -146,6 +263,26 @@ func (this *HTTPHeaderDAO) ComposeHeaderConfig(tx *dbs.Tx, headerId int64) (*sha
|
|||||||
config.Status = status
|
config.Status = status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// methods
|
||||||
|
if len(header.Methods) > 0 {
|
||||||
|
var methods = []string{}
|
||||||
|
err = json.Unmarshal([]byte(header.Methods), &methods)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Methods = methods
|
||||||
|
}
|
||||||
|
|
||||||
|
// domains
|
||||||
|
if len(header.Domains) > 0 {
|
||||||
|
var domains = []string{}
|
||||||
|
err = json.Unmarshal([]byte(header.Domains), &domains)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Domains = domains
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,44 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// HTTP Header
|
// HTTPHeader HTTP Header
|
||||||
type HTTPHeader struct {
|
type HTTPHeader struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
TemplateId uint32 `field:"templateId"` // 模版ID
|
TemplateId uint32 `field:"templateId"` // 模版ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Value string `field:"value"` // 值
|
Value string `field:"value"` // 值
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
Status string `field:"status"` // 状态码设置
|
Status string `field:"status"` // 状态码设置
|
||||||
State uint8 `field:"state"` // 状态
|
DisableRedirect uint8 `field:"disableRedirect"` // 是否不支持跳转
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
ShouldAppend uint8 `field:"shouldAppend"` // 是否为附加
|
||||||
|
ShouldReplace uint8 `field:"shouldReplace"` // 是否替换变量
|
||||||
|
ReplaceValues string `field:"replaceValues"` // 替换的值
|
||||||
|
Methods string `field:"methods"` // 支持的方法
|
||||||
|
Domains string `field:"domains"` // 支持的域名
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPHeaderOperator struct {
|
type HTTPHeaderOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
TemplateId interface{} // 模版ID
|
TemplateId interface{} // 模版ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Value interface{} // 值
|
Value interface{} // 值
|
||||||
Order interface{} // 排序
|
Order interface{} // 排序
|
||||||
Status interface{} // 状态码设置
|
Status interface{} // 状态码设置
|
||||||
State interface{} // 状态
|
DisableRedirect interface{} // 是否不支持跳转
|
||||||
CreatedAt interface{} // 创建时间
|
ShouldAppend interface{} // 是否为附加
|
||||||
|
ShouldReplace interface{} // 是否替换变量
|
||||||
|
ReplaceValues interface{} // 替换的值
|
||||||
|
Methods interface{} // 支持的方法
|
||||||
|
Domains interface{} // 支持的域名
|
||||||
|
State interface{} // 状态
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPHeaderOperator() *HTTPHeaderOperator {
|
func NewHTTPHeaderOperator() *HTTPHeaderOperator {
|
||||||
|
|||||||
@@ -186,48 +186,6 @@ func (this *HTTPHeaderPolicyDAO) ComposeHeaderPolicyConfig(tx *dbs.Tx, headerPol
|
|||||||
config.Id = int64(policy.Id)
|
config.Id = int64(policy.Id)
|
||||||
config.IsOn = policy.IsOn == 1
|
config.IsOn = policy.IsOn == 1
|
||||||
|
|
||||||
// AddHeaders
|
|
||||||
if len(policy.AddHeaders) > 0 {
|
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
|
||||||
err = json.Unmarshal([]byte(policy.AddHeaders), &refs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(refs) > 0 {
|
|
||||||
for _, ref := range refs {
|
|
||||||
headerConfig, err := SharedHTTPHeaderDAO.ComposeHeaderConfig(tx, ref.HeaderId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
config.AddHeaders = append(config.AddHeaders, headerConfig)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddTrailers
|
|
||||||
if len(policy.AddTrailers) > 0 {
|
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
|
||||||
err = json.Unmarshal([]byte(policy.AddTrailers), &refs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(refs) > 0 {
|
|
||||||
resultRefs := []*shared.HTTPHeaderRef{}
|
|
||||||
for _, ref := range refs {
|
|
||||||
headerConfig, err := SharedHTTPHeaderDAO.ComposeHeaderConfig(tx, ref.HeaderId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if headerConfig == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
resultRefs = append(resultRefs, ref)
|
|
||||||
config.AddTrailers = append(config.AddTrailers, headerConfig)
|
|
||||||
}
|
|
||||||
config.AddHeaderRefs = resultRefs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetHeaders
|
// SetHeaders
|
||||||
if len(policy.SetHeaders) > 0 {
|
if len(policy.SetHeaders) > 0 {
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
refs := []*shared.HTTPHeaderRef{}
|
||||||
@@ -252,30 +210,6 @@ func (this *HTTPHeaderPolicyDAO) ComposeHeaderPolicyConfig(tx *dbs.Tx, headerPol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceHeaders
|
|
||||||
if len(policy.ReplaceHeaders) > 0 {
|
|
||||||
refs := []*shared.HTTPHeaderRef{}
|
|
||||||
err = json.Unmarshal([]byte(policy.ReplaceHeaders), &refs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(refs) > 0 {
|
|
||||||
resultRefs := []*shared.HTTPHeaderRef{}
|
|
||||||
for _, ref := range refs {
|
|
||||||
headerConfig, err := SharedHTTPHeaderDAO.ComposeHeaderConfig(tx, ref.HeaderId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if headerConfig == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
resultRefs = append(resultRefs, ref)
|
|
||||||
config.ReplaceHeaders = append(config.ReplaceHeaders, headerConfig)
|
|
||||||
}
|
|
||||||
config.ReplaceHeaderRefs = resultRefs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete Headers
|
// Delete Headers
|
||||||
if len(policy.DeleteHeaders) > 0 {
|
if len(policy.DeleteHeaders) > 0 {
|
||||||
headers := []string{}
|
headers := []string{}
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ func (this *HTTPLocationDAO) FindHTTPLocationName(tx *dbs.Tx, id int64) (string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLocation 创建路由规则
|
// CreateLocation 创建路由规则
|
||||||
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte) (int64, error) {
|
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte, domains []string) (int64, error) {
|
||||||
op := NewHTTPLocationOperator()
|
op := NewHTTPLocationOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = HTTPLocationStateEnabled
|
op.State = HTTPLocationStateEnabled
|
||||||
@@ -100,7 +100,16 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str
|
|||||||
op.Conds = condsJSON
|
op.Conds = condsJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
if domains == nil {
|
||||||
|
domains = []string{}
|
||||||
|
}
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
|
||||||
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -108,7 +117,7 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateLocation 修改路由规则
|
// UpdateLocation 修改路由规则
|
||||||
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte) error {
|
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte, domains []string) error {
|
||||||
if locationId <= 0 {
|
if locationId <= 0 {
|
||||||
return errors.New("invalid locationId")
|
return errors.New("invalid locationId")
|
||||||
}
|
}
|
||||||
@@ -124,7 +133,16 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
|
|||||||
op.Conds = condsJSON
|
op.Conds = condsJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
if domains == nil {
|
||||||
|
domains = []string{}
|
||||||
|
}
|
||||||
|
domainsJSON, err := json.Marshal(domains)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.Domains = domainsJSON
|
||||||
|
|
||||||
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -195,6 +213,18 @@ func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64,
|
|||||||
config.Conds = conds
|
config.Conds = conds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// domains
|
||||||
|
if len(location.Domains) > 0 {
|
||||||
|
var domains = []string{}
|
||||||
|
err = json.Unmarshal([]byte(location.Domains), &domains)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(domains) > 0 {
|
||||||
|
config.Domains = domains
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if cacheMap != nil {
|
if cacheMap != nil {
|
||||||
cacheMap.Put(cacheKey, config)
|
cacheMap.Put(cacheKey, config)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type HTTPLocation struct {
|
|||||||
UrlPrefix string `field:"urlPrefix"` // URL前缀
|
UrlPrefix string `field:"urlPrefix"` // URL前缀
|
||||||
IsBreak uint8 `field:"isBreak"` // 是否终止匹配
|
IsBreak uint8 `field:"isBreak"` // 是否终止匹配
|
||||||
Conds string `field:"conds"` // 匹配条件
|
Conds string `field:"conds"` // 匹配条件
|
||||||
|
Domains string `field:"domains"` // 专属域名
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPLocationOperator struct {
|
type HTTPLocationOperator struct {
|
||||||
@@ -37,6 +38,7 @@ type HTTPLocationOperator struct {
|
|||||||
UrlPrefix interface{} // URL前缀
|
UrlPrefix interface{} // URL前缀
|
||||||
IsBreak interface{} // 是否终止匹配
|
IsBreak interface{} // 是否终止匹配
|
||||||
Conds interface{} // 匹配条件
|
Conds interface{} // 匹配条件
|
||||||
|
Domains interface{} // 专属域名
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPLocationOperator() *HTTPLocationOperator {
|
func NewHTTPLocationOperator() *HTTPLocationOperator {
|
||||||
|
|||||||
@@ -419,6 +419,21 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
|||||||
config.RemoteAddr = remoteAddrConfig
|
config.RemoteAddr = remoteAddrConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mergeSlashes
|
||||||
|
config.MergeSlashes = web.MergeSlashes == 1
|
||||||
|
|
||||||
|
// 请求限制
|
||||||
|
if len(web.RequestLimit) > 0 {
|
||||||
|
var requestLimitConfig = &serverconfigs.HTTPRequestLimitConfig{}
|
||||||
|
if len(web.RequestLimit) > 0 {
|
||||||
|
err = json.Unmarshal([]byte(web.RequestLimit), requestLimitConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.RequestLimit = requestLimitConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if cacheMap != nil {
|
if cacheMap != nil {
|
||||||
cacheMap.Put(cacheKey, config)
|
cacheMap.Put(cacheKey, config)
|
||||||
}
|
}
|
||||||
@@ -1037,6 +1052,8 @@ func (this *HTTPWebDAO) UpdateWebHostRedirects(tx *dbs.Tx, webId int64, hostRedi
|
|||||||
return this.NotifyUpdate(tx, webId)
|
return this.NotifyUpdate(tx, webId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 通用设置
|
||||||
|
|
||||||
// FindWebHostRedirects 查找主机跳转
|
// FindWebHostRedirects 查找主机跳转
|
||||||
func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, error) {
|
func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, error) {
|
||||||
col, err := this.Query(tx).
|
col, err := this.Query(tx).
|
||||||
@@ -1049,6 +1066,59 @@ func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, e
|
|||||||
return []byte(col), nil
|
return []byte(col), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateWebCommon 修改通用设置
|
||||||
|
func (this *HTTPWebDAO) UpdateWebCommon(tx *dbs.Tx, webId int64, mergeSlashes bool) error {
|
||||||
|
if webId <= 0 {
|
||||||
|
return errors.New("invalid webId")
|
||||||
|
}
|
||||||
|
var op = NewHTTPWebOperator()
|
||||||
|
op.Id = webId
|
||||||
|
op.MergeSlashes = mergeSlashes
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, webId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateWebRequestLimit 修改服务的请求限制
|
||||||
|
func (this *HTTPWebDAO) UpdateWebRequestLimit(tx *dbs.Tx, webId int64, config *serverconfigs.HTTPRequestLimitConfig) error {
|
||||||
|
configJSON, err := json.Marshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(webId).
|
||||||
|
Set("requestLimit", configJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, webId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindWebRequestLimit 获取服务的请求限制
|
||||||
|
func (this *HTTPWebDAO) FindWebRequestLimit(tx *dbs.Tx, webId int64) (*serverconfigs.HTTPRequestLimitConfig, error) {
|
||||||
|
configString, err := this.Query(tx).
|
||||||
|
Pk(webId).
|
||||||
|
Result("requestLimit").
|
||||||
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var config = &serverconfigs.HTTPRequestLimitConfig{}
|
||||||
|
if len(configString) == 0 {
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
err = json.Unmarshal([]byte(configString), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
||||||
// server
|
// server
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ type HTTPWeb struct {
|
|||||||
Auth string `field:"auth"` // 认证策略配置
|
Auth string `field:"auth"` // 认证策略配置
|
||||||
Webp string `field:"webp"` // WebP配置
|
Webp string `field:"webp"` // WebP配置
|
||||||
RemoteAddr string `field:"remoteAddr"` // 客户端IP配置
|
RemoteAddr string `field:"remoteAddr"` // 客户端IP配置
|
||||||
|
MergeSlashes uint8 `field:"mergeSlashes"` // 是否合并路径中的斜杠
|
||||||
|
RequestLimit string `field:"requestLimit"` // 请求限制
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPWebOperator struct {
|
type HTTPWebOperator struct {
|
||||||
@@ -65,6 +67,8 @@ type HTTPWebOperator struct {
|
|||||||
Auth interface{} // 认证策略配置
|
Auth interface{} // 认证策略配置
|
||||||
Webp interface{} // WebP配置
|
Webp interface{} // WebP配置
|
||||||
RemoteAddr interface{} // 客户端IP配置
|
RemoteAddr interface{} // 客户端IP配置
|
||||||
|
MergeSlashes interface{} // 是否合并路径中的斜杠
|
||||||
|
RequestLimit interface{} // 请求限制
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPWebOperator() *HTTPWebOperator {
|
func NewHTTPWebOperator() *HTTPWebOperator {
|
||||||
|
|||||||
@@ -123,14 +123,16 @@ func (this *IPItemDAO) FindEnabledIPItem(tx *dbs.Tx, id int64) (*IPItem, error)
|
|||||||
return result.(*IPItem), err
|
return result.(*IPItem), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableOldIPItem 根据IP删除以前的旧记录
|
// DeleteOldItem 根据IP删除以前的旧记录
|
||||||
func (this *IPItemDAO) DisableOldIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string) error {
|
func (this *IPItemDAO) DeleteOldItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string) error {
|
||||||
return this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
|
UseIndex("ipFrom").
|
||||||
Attr("listId", listId).
|
Attr("listId", listId).
|
||||||
Attr("ipFrom", ipFrom).
|
Attr("ipFrom", ipFrom).
|
||||||
Attr("ipTo", ipTo).
|
Attr("ipTo", ipTo).
|
||||||
Set("state", IPItemStateDisabled).
|
Delete()
|
||||||
UpdateQuickly()
|
// 这里不通知更新
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateIPItem 创建IP
|
// CreateIPItem 创建IP
|
||||||
@@ -184,6 +186,11 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
|
|||||||
}
|
}
|
||||||
itemId := types.Int64(op.Id)
|
itemId := types.Int64(op.Id)
|
||||||
|
|
||||||
|
// 全局名单不需要即时更新,防止数量过多而导致性能问题
|
||||||
|
if listId == firewallconfigs.GlobalListId {
|
||||||
|
return itemId, nil
|
||||||
|
}
|
||||||
|
|
||||||
err = this.NotifyUpdate(tx, itemId)
|
err = this.NotifyUpdate(tx, itemId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -279,6 +286,35 @@ func (this *IPItemDAO) ListIPItemsWithListId(tx *dbs.Tx, listId int64, keyword s
|
|||||||
|
|
||||||
// ListIPItemsAfterVersion 根据版本号查找IP列表
|
// ListIPItemsAfterVersion 根据版本号查找IP列表
|
||||||
func (this *IPItemDAO) ListIPItemsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*IPItem, err error) {
|
func (this *IPItemDAO) ListIPItemsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*IPItem, err error) {
|
||||||
|
// 将过期的设置为已删除,这样是为了在 expiredAt<UNIX_TIMESTAMP()边缘节点让过期的IP有一个执行删除的机会
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
ResultPk().
|
||||||
|
Where("(expiredAt>0 AND expiredAt<=:timestamp)").
|
||||||
|
Param("timestamp", time.Now().Unix()).
|
||||||
|
State(IPItemStateEnabled).
|
||||||
|
Limit(100).
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
var expiredId = one.GetInt64("id")
|
||||||
|
newVersion, err := SharedIPListDAO.IncreaseVersion(tx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Pk(expiredId).
|
||||||
|
Set("state", IPItemStateDisabled).
|
||||||
|
Set("expiredAt", 0).
|
||||||
|
Set("version", newVersion).
|
||||||
|
Update()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
// 这里不要设置状态参数,因为我们要知道哪些是删除的
|
// 这里不要设置状态参数,因为我们要知道哪些是删除的
|
||||||
Gt("version", version).
|
Gt("version", version).
|
||||||
@@ -322,6 +358,7 @@ func (this *IPItemDAO) FindEnabledItemContainsIP(tx *dbs.Tx, listId int64, ip ui
|
|||||||
// FindEnabledItemsWithIP 根据IP查找Item
|
// FindEnabledItemsWithIP 根据IP查找Item
|
||||||
func (this *IPItemDAO) FindEnabledItemsWithIP(tx *dbs.Tx, ip string) (result []*IPItem, err error) {
|
func (this *IPItemDAO) FindEnabledItemsWithIP(tx *dbs.Tx, ip string) (result []*IPItem, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
|
State(IPItemStateEnabled).
|
||||||
Attr("ipFrom", ip).
|
Attr("ipFrom", ip).
|
||||||
Attr("ipTo", "").
|
Attr("ipTo", "").
|
||||||
Where("(expiredAt=0 OR expiredAt>:nowTime)").
|
Where("(expiredAt=0 OR expiredAt>:nowTime)").
|
||||||
@@ -394,6 +431,37 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if listId == firewallconfigs.GlobalListId {
|
||||||
|
sourceNodeId, err := this.Query(tx).
|
||||||
|
Pk(itemId).
|
||||||
|
Result("sourceNodeId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if sourceNodeId > 0 {
|
||||||
|
clusterIds, err := SharedNodeDAO.FindEnabledNodeClusterIds(tx, sourceNodeId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, clusterId := range clusterIds {
|
||||||
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeIPItemChanged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
|
||||||
|
for _, clusterId := range clusterIds {
|
||||||
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeIPItemChanged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
httpFirewallPolicyIds, err := SharedHTTPFirewallPolicyDAO.FindEnabledFirewallPolicyIdsWithIPListId(tx, listId)
|
httpFirewallPolicyIds, err := SharedHTTPFirewallPolicyDAO.FindEnabledFirewallPolicyIdsWithIPListId(tx, listId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ const (
|
|||||||
type MessageType = string
|
type MessageType = string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// 这里的命名问题(首字母大写)为历史遗留问题,暂不修改
|
||||||
|
|
||||||
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed" // 节点健康检查失败
|
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed" // 节点健康检查失败
|
||||||
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp" // 因健康检查节点上线
|
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp" // 因健康检查节点上线
|
||||||
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown" // 因健康检查节点下线
|
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown" // 因健康检查节点下线
|
||||||
@@ -36,8 +38,9 @@ const (
|
|||||||
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
||||||
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
||||||
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
||||||
MessageTypeServerNamesAuditingSuccess MessageType = "ServerNamesAuditingSuccess" // 服务域名审核成功
|
MessageTypeServerNamesAuditingSuccess MessageType = "ServerNamesAuditingSuccess" // 服务域名审核成功(用户)
|
||||||
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败
|
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败(用户)
|
||||||
|
MessageTypeServerNamesRequireAuditing MessageType = "serverNamesRequireAuditing" // 服务域名需要审核(管理员)
|
||||||
MessageTypeThresholdSatisfied MessageType = "ThresholdSatisfied" // 满足阈值
|
MessageTypeThresholdSatisfied MessageType = "ThresholdSatisfied" // 满足阈值
|
||||||
MessageTypeFirewallEvent MessageType = "FirewallEvent" // 防火墙事件
|
MessageTypeFirewallEvent MessageType = "FirewallEvent" // 防火墙事件
|
||||||
MessageTypeIPAddrUp MessageType = "IPAddrUp" // IP地址上线
|
MessageTypeIPAddrUp MessageType = "IPAddrUp" // IP地址上线
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -46,14 +47,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedMessageTaskDAO.CleanExpiredMessageTasks(nil, 30) // 只保留30天
|
err := SharedMessageTaskDAO.CleanExpiredMessageTasks(nil, 30) // 只保留30天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("SharedMessageTaskDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("SharedMessageTaskDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -16,14 +17,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedMessageTaskLogDAO.CleanExpiredLogs(nil, 30) // 只保留30天
|
err := SharedMessageTaskLogDAO.CleanExpiredLogs(nil, 30) // 只保留30天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("SharedMessageTaskLogDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("SharedMessageTaskLogDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,12 @@ func (this *MetricItemDAO) DisableMetricItem(tx *dbs.Tx, itemId int64) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = SharedMetricSumStatDAO.DeleteItemStats(tx, itemId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -20,14 +22,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedMetricStatDAO.Clean(nil, 120) // 只保留120天
|
err := SharedMetricStatDAO.Clean(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("SharedMetricStatDAO: clean expired data failed: " + err.Error())
|
logs.Println("SharedMetricStatDAO: clean expired data failed: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -461,12 +463,39 @@ func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clean 清理数据
|
// Clean 清理数据
|
||||||
func (this *MetricStatDAO) Clean(tx *dbs.Tx, days int64) error {
|
func (this *MetricStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
_, err := this.Query(tx).
|
for _, category := range serverconfigs.FindAllMetricItemCategoryCodes() {
|
||||||
Lt("createdDay", timeutil.FormatTime("Ymd", time.Now().Unix()-days*86400)).
|
var offset int64 = 0
|
||||||
Delete()
|
var size int64 = 100
|
||||||
if err != nil {
|
for {
|
||||||
return err
|
items, err := SharedMetricItemDAO.ListEnabledItems(tx, category, offset, size)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, item := range items {
|
||||||
|
var config = &serverconfigs.MetricItemConfig{
|
||||||
|
Id: int64(item.Id),
|
||||||
|
Period: int(item.Period),
|
||||||
|
PeriodUnit: item.PeriodUnit,
|
||||||
|
}
|
||||||
|
var expiresDay = config.ServerExpiresDay()
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("itemId", item.Id).
|
||||||
|
Lte("createdDay", expiresDay).
|
||||||
|
UseIndex("createdDay").
|
||||||
|
Limit(100_000). // 一次性不要删除太多,防止阻塞其他操作
|
||||||
|
Delete()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(items) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += size
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
@@ -10,14 +11,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNewMetricStatDAO_InsertMany(t *testing.T) {
|
func TestNewMetricStatDAO_InsertMany(t *testing.T) {
|
||||||
for i := 0; i <= 10_000_000; i++ {
|
for i := 0; i <= 1; i++ {
|
||||||
err := NewMetricStatDAO().CreateStat(nil, types.String(i)+"_v1", 18, int64(rands.Int(0, 10000)), int64(rands.Int(0, 10000)), int64(rands.Int(0, 100)), []string{"/html" + types.String(i)}, 1, timeutil.Format("Ymd"), 0)
|
err := NewMetricStatDAO().CreateStat(nil, types.String(i)+"_v1", 18, int64(rands.Int(0, 10000)), int64(rands.Int(0, 10000)), int64(rands.Int(0, 100)), []string{"/html" + types.String(i)}, 1, timeutil.Format("Ymd"), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if i % 10000 == 0 {
|
if i%10000 == 0 {
|
||||||
t.Log(i)
|
t.Log(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.Log("done")
|
t.Log("done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMetricStatDAO_Clean(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := NewMetricStatDAO().Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +1,35 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MetricSumStatDAO dbs.DAO
|
type MetricSumStatDAO dbs.DAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedMetricSumStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("SharedMetricSumStatDAO: clean expired data failed: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func NewMetricSumStatDAO() *MetricSumStatDAO {
|
func NewMetricSumStatDAO() *MetricSumStatDAO {
|
||||||
return dbs.NewDAO(&MetricSumStatDAO{
|
return dbs.NewDAO(&MetricSumStatDAO{
|
||||||
DAOObject: dbs.DAOObject{
|
DAOObject: dbs.DAOObject{
|
||||||
@@ -32,14 +53,15 @@ func init() {
|
|||||||
func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int64, serverId int64, time string, itemId int64, version int32, count int64, total float32) error {
|
func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int64, serverId int64, time string, itemId int64, version int32, count int64, total float32) error {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
InsertOrUpdateQuickly(maps.Map{
|
InsertOrUpdateQuickly(maps.Map{
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
"nodeId": nodeId,
|
"nodeId": nodeId,
|
||||||
"serverId": serverId,
|
"serverId": serverId,
|
||||||
"itemId": itemId,
|
"itemId": itemId,
|
||||||
"version": version,
|
"version": version,
|
||||||
"time": time,
|
"time": time,
|
||||||
"count": count,
|
"count": count,
|
||||||
"total": total,
|
"total": total,
|
||||||
|
"createdDay": timeutil.Format("Ymd"),
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"count": count,
|
"count": count,
|
||||||
"total": total,
|
"total": total,
|
||||||
@@ -84,6 +106,7 @@ func (this *MetricSumStatDAO) FindSumAtTime(tx *dbs.Tx, time string, itemId int6
|
|||||||
// FindServerSum 查找某个服务的统计数据
|
// FindServerSum 查找某个服务的统计数据
|
||||||
func (this *MetricSumStatDAO) FindServerSum(tx *dbs.Tx, serverId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
func (this *MetricSumStatDAO) FindServerSum(tx *dbs.Tx, serverId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
|
UseIndex("server_item_time").
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Attr("time", time).
|
Attr("time", time).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -102,6 +125,7 @@ func (this *MetricSumStatDAO) FindServerSum(tx *dbs.Tx, serverId int64, time str
|
|||||||
// FindClusterSum 查找集群上的统计数据
|
// FindClusterSum 查找集群上的统计数据
|
||||||
func (this *MetricSumStatDAO) FindClusterSum(tx *dbs.Tx, clusterId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
func (this *MetricSumStatDAO) FindClusterSum(tx *dbs.Tx, clusterId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
|
UseIndex("cluster_item_time").
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("time", time).
|
Attr("time", time).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -120,6 +144,7 @@ func (this *MetricSumStatDAO) FindClusterSum(tx *dbs.Tx, clusterId int64, time s
|
|||||||
// FindNodeSum 查找节点上的统计数据
|
// FindNodeSum 查找节点上的统计数据
|
||||||
func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
|
UseIndex("node_item_time").
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Attr("time", time).
|
Attr("time", time).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -134,3 +159,50 @@ func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string,
|
|||||||
}
|
}
|
||||||
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteItemStats 删除某个指标相关的统计数据
|
||||||
|
func (this *MetricSumStatDAO) DeleteItemStats(tx *dbs.Tx, itemId int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("itemId", itemId).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean 清理数据
|
||||||
|
func (this *MetricSumStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
for _, category := range serverconfigs.FindAllMetricItemCategoryCodes() {
|
||||||
|
var offset int64 = 0
|
||||||
|
var size int64 = 100
|
||||||
|
for {
|
||||||
|
items, err := SharedMetricItemDAO.ListEnabledItems(tx, category, offset, size)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, item := range items {
|
||||||
|
var config = &serverconfigs.MetricItemConfig{
|
||||||
|
Id: int64(item.Id),
|
||||||
|
Period: int(item.Period),
|
||||||
|
PeriodUnit: item.PeriodUnit,
|
||||||
|
}
|
||||||
|
var expiresDay = config.ServerExpiresDay()
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("itemId", item.Id).
|
||||||
|
Where("(createdDay IS NULL OR createdDay<:day)").
|
||||||
|
Param("day", expiresDay).
|
||||||
|
UseIndex("createdDay").
|
||||||
|
Limit(100_000). // 一次性不要删除太多,防止阻塞其他操作
|
||||||
|
Delete()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(items) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,4 +3,15 @@ package models
|
|||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMetricSumStatDAO_Clean(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := NewMetricSumStatDAO().Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,27 +2,29 @@ package models
|
|||||||
|
|
||||||
// MetricSumStat 指标统计总和数据
|
// MetricSumStat 指标统计总和数据
|
||||||
type MetricSumStat struct {
|
type MetricSumStat struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
ServerId uint32 `field:"serverId"` // 服务ID
|
ServerId uint32 `field:"serverId"` // 服务ID
|
||||||
ItemId uint64 `field:"itemId"` // 指标
|
ItemId uint64 `field:"itemId"` // 指标
|
||||||
Count uint64 `field:"count"` // 数量
|
Count uint64 `field:"count"` // 数量
|
||||||
Total float64 `field:"total"` // 总和
|
Total float64 `field:"total"` // 总和
|
||||||
Time string `field:"time"` // 分钟值YYYYMMDDHHII
|
Time string `field:"time"` // 分钟值YYYYMMDDHHII
|
||||||
Version uint32 `field:"version"` // 版本号
|
Version uint32 `field:"version"` // 版本号
|
||||||
|
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetricSumStatOperator struct {
|
type MetricSumStatOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId interface{} // 集群ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
ServerId interface{} // 服务ID
|
ServerId interface{} // 服务ID
|
||||||
ItemId interface{} // 指标
|
ItemId interface{} // 指标
|
||||||
Count interface{} // 数量
|
Count interface{} // 数量
|
||||||
Total interface{} // 总和
|
Total interface{} // 总和
|
||||||
Time interface{} // 分钟值YYYYMMDDHHII
|
Time interface{} // 分钟值YYYYMMDDHHII
|
||||||
Version interface{} // 版本号
|
Version interface{} // 版本号
|
||||||
|
CreatedDay interface{} // 创建日期YYYYMMDD
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMetricSumStatOperator() *MetricSumStatOperator {
|
func NewMetricSumStatOperator() *MetricSumStatOperator {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package nameservers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedNSRecordHourlyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedNSRecordHourlyStatDAO.Clean(nil, 30) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateCluster 修改集群
|
// UpdateCluster 修改集群
|
||||||
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string) error {
|
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, nodeTCPMaxConnections int32) error {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return errors.New("invalid clusterId")
|
return errors.New("invalid clusterId")
|
||||||
}
|
}
|
||||||
@@ -188,6 +188,17 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
|
|||||||
op.GrantId = grantId
|
op.GrantId = grantId
|
||||||
op.InstallDir = installDir
|
op.InstallDir = installDir
|
||||||
op.TimeZone = timezone
|
op.TimeZone = timezone
|
||||||
|
|
||||||
|
if nodeMaxThreads < 0 {
|
||||||
|
nodeMaxThreads = 0
|
||||||
|
}
|
||||||
|
op.NodeMaxThreads = nodeMaxThreads
|
||||||
|
|
||||||
|
if nodeTCPMaxConnections < 0 {
|
||||||
|
nodeTCPMaxConnections = 0
|
||||||
|
}
|
||||||
|
op.NodeTCPMaxConnections = nodeTCPMaxConnections
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -588,6 +599,22 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithHTTPFirewallPolicyId
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledNodeClusterIds 查找所有可用的集群
|
||||||
|
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIds(tx *dbs.Tx) ([]int64, error) {
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
State(NodeClusterStateEnabled).
|
||||||
|
ResultPk().
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var result = []int64{}
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, int64(one.(*NodeCluster).Id))
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// FindAllEnabledNodeClusterIdsWithCachePolicyId 查找使用缓存策略的所有集群Ids
|
// FindAllEnabledNodeClusterIdsWithCachePolicyId 查找使用缓存策略的所有集群Ids
|
||||||
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId int64) (result []int64, err error) {
|
func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId int64) (result []int64, err error) {
|
||||||
ones, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
@@ -848,27 +875,27 @@ func (this *NodeClusterDAO) ExistsEnabledCluster(tx *dbs.Tx, clusterId int64) (b
|
|||||||
Exist()
|
Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindClusterTimezone 查找时区
|
// FindClusterBasicInfo 查找集群基础信息
|
||||||
func (this *NodeClusterDAO) FindClusterTimezone(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (string, error) {
|
func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
|
||||||
var cacheKey = this.Table + ":FindEnabledTimeZone:" + types.String(clusterId)
|
var cacheKey = this.Table + ":FindClusterBasicInfo:" + types.String(clusterId)
|
||||||
if cacheMap != nil {
|
if cacheMap != nil {
|
||||||
cache, ok := cacheMap.Get(cacheKey)
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
if ok {
|
if ok {
|
||||||
return cache.(string), nil
|
return cache.(*NodeCluster), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
timeZone, err := this.Query(tx).
|
cluster, err := this.Query(tx).
|
||||||
Pk(clusterId).
|
Pk(clusterId).
|
||||||
Result("timeZone").
|
Result("timeZone", "nodeMaxThreads", "nodeTCPMaxConnections", "cachePolicyId", "httpFirewallPolicyId").
|
||||||
FindStringCol("")
|
Find()
|
||||||
if err != nil {
|
if err != nil || cluster == nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
if cacheMap != nil {
|
if cacheMap != nil {
|
||||||
cacheMap.Put(cacheKey, timeZone)
|
cacheMap.Put(cacheKey, cluster)
|
||||||
}
|
}
|
||||||
return timeZone, nil
|
return cluster.(*NodeCluster), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
|
|||||||
@@ -2,59 +2,63 @@ package models
|
|||||||
|
|
||||||
// NodeCluster 节点集群
|
// NodeCluster 节点集群
|
||||||
type NodeCluster struct {
|
type NodeCluster struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
UseAllAPINodes uint8 `field:"useAllAPINodes"` // 是否使用所有API节点
|
UseAllAPINodes uint8 `field:"useAllAPINodes"` // 是否使用所有API节点
|
||||||
ApiNodes string `field:"apiNodes"` // 使用的API节点
|
ApiNodes string `field:"apiNodes"` // 使用的API节点
|
||||||
InstallDir string `field:"installDir"` // 安装目录
|
InstallDir string `field:"installDir"` // 安装目录
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
GrantId uint32 `field:"grantId"` // 默认认证方式
|
GrantId uint32 `field:"grantId"` // 默认认证方式
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
AutoRegister uint8 `field:"autoRegister"` // 是否开启自动注册
|
AutoRegister uint8 `field:"autoRegister"` // 是否开启自动注册
|
||||||
UniqueId string `field:"uniqueId"` // 唯一ID
|
UniqueId string `field:"uniqueId"` // 唯一ID
|
||||||
Secret string `field:"secret"` // 密钥
|
Secret string `field:"secret"` // 密钥
|
||||||
HealthCheck string `field:"healthCheck"` // 健康检查
|
HealthCheck string `field:"healthCheck"` // 健康检查
|
||||||
DnsName string `field:"dnsName"` // DNS名称
|
DnsName string `field:"dnsName"` // DNS名称
|
||||||
DnsDomainId uint32 `field:"dnsDomainId"` // 域名ID
|
DnsDomainId uint32 `field:"dnsDomainId"` // 域名ID
|
||||||
Dns string `field:"dns"` // DNS配置
|
Dns string `field:"dns"` // DNS配置
|
||||||
Toa string `field:"toa"` // TOA配置
|
Toa string `field:"toa"` // TOA配置
|
||||||
CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID
|
CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID
|
||||||
HttpFirewallPolicyId uint32 `field:"httpFirewallPolicyId"` // WAF策略ID
|
HttpFirewallPolicyId uint32 `field:"httpFirewallPolicyId"` // WAF策略ID
|
||||||
AccessLog string `field:"accessLog"` // 访问日志设置
|
AccessLog string `field:"accessLog"` // 访问日志设置
|
||||||
SystemServices string `field:"systemServices"` // 系统服务设置
|
SystemServices string `field:"systemServices"` // 系统服务设置
|
||||||
TimeZone string `field:"timeZone"` // 时区
|
TimeZone string `field:"timeZone"` // 时区
|
||||||
|
NodeMaxThreads uint32 `field:"nodeMaxThreads"` // 节点最大线程数
|
||||||
|
NodeTCPMaxConnections uint32 `field:"nodeTCPMaxConnections"` // TCP最大连接数
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeClusterOperator struct {
|
type NodeClusterOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
UseAllAPINodes interface{} // 是否使用所有API节点
|
UseAllAPINodes interface{} // 是否使用所有API节点
|
||||||
ApiNodes interface{} // 使用的API节点
|
ApiNodes interface{} // 使用的API节点
|
||||||
InstallDir interface{} // 安装目录
|
InstallDir interface{} // 安装目录
|
||||||
Order interface{} // 排序
|
Order interface{} // 排序
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
GrantId interface{} // 默认认证方式
|
GrantId interface{} // 默认认证方式
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
AutoRegister interface{} // 是否开启自动注册
|
AutoRegister interface{} // 是否开启自动注册
|
||||||
UniqueId interface{} // 唯一ID
|
UniqueId interface{} // 唯一ID
|
||||||
Secret interface{} // 密钥
|
Secret interface{} // 密钥
|
||||||
HealthCheck interface{} // 健康检查
|
HealthCheck interface{} // 健康检查
|
||||||
DnsName interface{} // DNS名称
|
DnsName interface{} // DNS名称
|
||||||
DnsDomainId interface{} // 域名ID
|
DnsDomainId interface{} // 域名ID
|
||||||
Dns interface{} // DNS配置
|
Dns interface{} // DNS配置
|
||||||
Toa interface{} // TOA配置
|
Toa interface{} // TOA配置
|
||||||
CachePolicyId interface{} // 缓存策略ID
|
CachePolicyId interface{} // 缓存策略ID
|
||||||
HttpFirewallPolicyId interface{} // WAF策略ID
|
HttpFirewallPolicyId interface{} // WAF策略ID
|
||||||
AccessLog interface{} // 访问日志设置
|
AccessLog interface{} // 访问日志设置
|
||||||
SystemServices interface{} // 系统服务设置
|
SystemServices interface{} // 系统服务设置
|
||||||
TimeZone interface{} // 时区
|
TimeZone interface{} // 时区
|
||||||
|
NodeMaxThreads interface{} // 节点最大线程数
|
||||||
|
NodeTCPMaxConnections interface{} // TCP最大连接数
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeClusterOperator() *NodeClusterOperator {
|
func NewNodeClusterOperator() *NodeClusterOperator {
|
||||||
|
|||||||
@@ -519,9 +519,9 @@ func (this *NodeDAO) FindAllInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int
|
|||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("isOn", true). // 只监控启用的节点
|
Attr("isOn", true). // 只监控启用的节点
|
||||||
Attr("isInstalled", true). // 只监控已经安装的节点
|
Attr("isInstalled", true). // 只监控已经安装的节点
|
||||||
Attr("isActive", true). // 当前已经在线的
|
Attr("isActive", true). // 当前已经在线的
|
||||||
Where("(status IS NULL OR (JSON_EXTRACT(status, '$.isActive')=false AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>10) OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>120)").
|
Where("(status IS NULL OR (JSON_EXTRACT(status, '$.isActive')=false AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>10) OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>120)").
|
||||||
Result("id", "name").
|
Result("id", "name").
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
@@ -727,6 +727,13 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
|||||||
RegionId: int64(node.RegionId),
|
RegionId: int64(node.RegionId),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// API节点IP
|
||||||
|
apiNodeIPs, err := SharedAPINodeDAO.FindAllEnabledAPIAccessIPs(tx, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.AllowedIPs = append(config.AllowedIPs, apiNodeIPs...)
|
||||||
|
|
||||||
// 获取所有的服务
|
// 获取所有的服务
|
||||||
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(tx, int64(node.Id))
|
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(tx, int64(node.Id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -775,11 +782,17 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
|||||||
var primaryClusterId = int64(node.ClusterId)
|
var primaryClusterId = int64(node.ClusterId)
|
||||||
var clusterIds = []int64{primaryClusterId}
|
var clusterIds = []int64{primaryClusterId}
|
||||||
clusterIds = append(clusterIds, node.DecodeSecondaryClusterIds()...)
|
clusterIds = append(clusterIds, node.DecodeSecondaryClusterIds()...)
|
||||||
|
var clusterIndex = 0
|
||||||
for _, clusterId := range clusterIds {
|
for _, clusterId := range clusterIds {
|
||||||
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
|
nodeCluster, err := SharedNodeClusterDAO.FindClusterBasicInfo(tx, clusterId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if nodeCluster == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpFirewallPolicyId = int64(nodeCluster.HttpFirewallPolicyId)
|
||||||
if httpFirewallPolicyId > 0 {
|
if httpFirewallPolicyId > 0 {
|
||||||
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId, cacheMap)
|
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -791,10 +804,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 缓存策略
|
// 缓存策略
|
||||||
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId, cacheMap)
|
var httpCachePolicyId = int64(nodeCluster.CachePolicyId)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if httpCachePolicyId > 0 {
|
if httpCachePolicyId > 0 {
|
||||||
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(tx, httpCachePolicyId, cacheMap)
|
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(tx, httpCachePolicyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -806,13 +816,20 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 时区
|
// 时区
|
||||||
timeZone, err := SharedNodeClusterDAO.FindClusterTimezone(tx, clusterId, cacheMap)
|
if len(config.TimeZone) == 0 {
|
||||||
if err != nil {
|
var timeZone = nodeCluster.TimeZone
|
||||||
return nil, err
|
if len(timeZone) > 0 {
|
||||||
|
config.TimeZone = timeZone
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if len(timeZone) > 0 {
|
|
||||||
config.TimeZone = timeZone
|
// 最大线程数、TCP连接数
|
||||||
|
if clusterIndex == 0 {
|
||||||
|
config.MaxThreads = int(nodeCluster.NodeMaxThreads)
|
||||||
|
config.TCPMaxConnections = int(nodeCluster.NodeTCPMaxConnections)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clusterIndex++
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存最大容量设置
|
// 缓存最大容量设置
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateGrant 创建认证信息
|
// CreateGrant 创建认证信息
|
||||||
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64) (grantId int64, err error) {
|
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64, su bool) (grantId int64, err error) {
|
||||||
op := NewNodeGrantOperator()
|
op := NewNodeGrantOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -83,12 +83,12 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
|
|||||||
case "user":
|
case "user":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.Password = password
|
op.Password = password
|
||||||
op.Su = false // TODO 需要做到前端可以配置
|
|
||||||
case "privateKey":
|
case "privateKey":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.PrivateKey = privateKey
|
op.PrivateKey = privateKey
|
||||||
op.Passphrase = passphrase
|
op.Passphrase = passphrase
|
||||||
}
|
}
|
||||||
|
op.Su = su
|
||||||
op.Description = description
|
op.Description = description
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
op.State = NodeGrantStateEnabled
|
op.State = NodeGrantStateEnabled
|
||||||
@@ -97,7 +97,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateGrant 修改认证信息
|
// UpdateGrant 修改认证信息
|
||||||
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64) error {
|
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64, su bool) error {
|
||||||
if grantId <= 0 {
|
if grantId <= 0 {
|
||||||
return errors.New("invalid grantId")
|
return errors.New("invalid grantId")
|
||||||
}
|
}
|
||||||
@@ -111,12 +111,12 @@ func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, me
|
|||||||
case "user":
|
case "user":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.Password = password
|
op.Password = password
|
||||||
op.Su = false // TODO 需要做到前端可以配置
|
|
||||||
case "privateKey":
|
case "privateKey":
|
||||||
op.Username = username
|
op.Username = username
|
||||||
op.PrivateKey = privateKey
|
op.PrivateKey = privateKey
|
||||||
op.Passphrase = passphrase
|
op.Passphrase = passphrase
|
||||||
}
|
}
|
||||||
|
op.Su = su
|
||||||
op.Description = description
|
op.Description = description
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ func (this *NodeIPAddressDAO) FindAddressIsHealthy(tx *dbs.Tx, addressId int64)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateAddress 创建IP地址
|
// CreateAddress 创建IP地址
|
||||||
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool) (addressId int64, err error) {
|
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool, groupId int64) (addressId int64, err error) {
|
||||||
if len(role) == 0 {
|
if len(role) == 0 {
|
||||||
role = nodeconfigs.NodeRoleNode
|
role = nodeconfigs.NodeRoleNode
|
||||||
}
|
}
|
||||||
@@ -131,6 +131,7 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId in
|
|||||||
op.Ip = ip
|
op.Ip = ip
|
||||||
op.CanAccess = canAccess
|
op.CanAccess = canAccess
|
||||||
op.IsUp = isUp
|
op.IsUp = isUp
|
||||||
|
op.GroupId = groupId
|
||||||
|
|
||||||
op.State = NodeIPAddressStateEnabled
|
op.State = NodeIPAddressStateEnabled
|
||||||
addressId, err = this.SaveInt64(tx, op)
|
addressId, err = this.SaveInt64(tx, op)
|
||||||
@@ -442,6 +443,7 @@ func (this *NodeIPAddressDAO) UpdateAddressBackupIP(tx *dbs.Tx, addressId int64,
|
|||||||
return errors.New("invalid addressId")
|
return errors.New("invalid addressId")
|
||||||
}
|
}
|
||||||
var op = NewNodeIPAddressOperator()
|
var op = NewNodeIPAddressOperator()
|
||||||
|
op.IsUp = true // IP必须在线备用IP才会有用
|
||||||
op.Id = addressId
|
op.Id = addressId
|
||||||
op.BackupThresholdId = thresholdId
|
op.BackupThresholdId = thresholdId
|
||||||
op.BackupIP = ip
|
op.BackupIP = ip
|
||||||
|
|||||||
44
internal/db/models/node_ip_address_group_dao.go
Normal file
44
internal/db/models/node_ip_address_group_dao.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NodeIPAddressGroupDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewNodeIPAddressGroupDAO() *NodeIPAddressGroupDAO {
|
||||||
|
return dbs.NewDAO(&NodeIPAddressGroupDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeNodeIPAddressGroups",
|
||||||
|
Model: new(NodeIPAddressGroup),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*NodeIPAddressGroupDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedNodeIPAddressGroupDAO *NodeIPAddressGroupDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedNodeIPAddressGroupDAO = NewNodeIPAddressGroupDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindNodeIPAddressGroupName 根据主键查找名称
|
||||||
|
func (this *NodeIPAddressGroupDAO) FindNodeIPAddressGroupName(tx *dbs.Tx, id uint32) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateGroup 创建分组
|
||||||
|
func (this *NodeIPAddressGroupDAO) CreateGroup(tx *dbs.Tx, name string, value string) (int64, error) {
|
||||||
|
var op = NewNodeIPAddressGroupOperator()
|
||||||
|
op.Name = name
|
||||||
|
op.Value = value
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
6
internal/db/models/node_ip_address_group_dao_test.go
Normal file
6
internal/db/models/node_ip_address_group_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
18
internal/db/models/node_ip_address_group_model.go
Normal file
18
internal/db/models/node_ip_address_group_model.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
// NodeIPAddressGroup IP地址分组
|
||||||
|
type NodeIPAddressGroup struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 分组名
|
||||||
|
Value string `field:"value"` // IP值
|
||||||
|
}
|
||||||
|
|
||||||
|
type NodeIPAddressGroupOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
Name interface{} // 分组名
|
||||||
|
Value interface{} // IP值
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNodeIPAddressGroupOperator() *NodeIPAddressGroupOperator {
|
||||||
|
return &NodeIPAddressGroupOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/node_ip_address_group_model_ext.go
Normal file
1
internal/db/models/node_ip_address_group_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
@@ -5,6 +5,7 @@ type NodeIPAddress struct {
|
|||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
Role string `field:"role"` // 节点角色
|
Role string `field:"role"` // 节点角色
|
||||||
|
GroupId uint32 `field:"groupId"` // 所属分组ID
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Ip string `field:"ip"` // IP地址
|
Ip string `field:"ip"` // IP地址
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
@@ -26,6 +27,7 @@ type NodeIPAddressOperator struct {
|
|||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
Role interface{} // 节点角色
|
Role interface{} // 节点角色
|
||||||
|
GroupId interface{} // 所属分组ID
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Ip interface{} // IP地址
|
Ip interface{} // IP地址
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
|
|||||||
@@ -41,8 +41,25 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLog 创建日志
|
// CreateLog 创建日志
|
||||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64) error {
|
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64, logType string, paramsJSON []byte) error {
|
||||||
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description)
|
// 修复以前同样的日志
|
||||||
|
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
|
||||||
|
err := this.Query(tx).
|
||||||
|
Attr("nodeId", nodeId).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Attr("type", logType).
|
||||||
|
Attr("level", "error").
|
||||||
|
Attr("isFixed", 0).
|
||||||
|
JSONContains("params", string(paramsJSON)).
|
||||||
|
Set("isFixed", 1).
|
||||||
|
Set("isRead", 1).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description + "@" + string(paramsJSON))
|
||||||
|
|
||||||
// 检查是否在重复最后一条,避免重复创建
|
// 检查是否在重复最后一条,避免重复创建
|
||||||
lastLog, err := this.Query(tx).
|
lastLog, err := this.Query(tx).
|
||||||
@@ -76,6 +93,12 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nod
|
|||||||
op.Hash = hash
|
op.Hash = hash
|
||||||
op.Count = 1
|
op.Count = 1
|
||||||
op.IsRead = level != "error"
|
op.IsRead = level != "error"
|
||||||
|
|
||||||
|
op.Type = logType
|
||||||
|
if len(paramsJSON) > 0 {
|
||||||
|
op.Params = paramsJSON
|
||||||
|
}
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -253,6 +276,7 @@ func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
|
|||||||
Attr("hash", hash).
|
Attr("hash", hash).
|
||||||
Attr("isFixed", false).
|
Attr("isFixed", false).
|
||||||
Set("isFixed", true).
|
Set("isFixed", true).
|
||||||
|
Set("isRead", true).
|
||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package models
|
|||||||
type NodeLog struct {
|
type NodeLog struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
Role string `field:"role"` // 节点角色
|
Role string `field:"role"` // 节点角色
|
||||||
|
Type string `field:"type"` // 类型
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
Tag string `field:"tag"` // 标签
|
Tag string `field:"tag"` // 标签
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
@@ -16,11 +17,13 @@ type NodeLog struct {
|
|||||||
Count uint32 `field:"count"` // 重复次数
|
Count uint32 `field:"count"` // 重复次数
|
||||||
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
||||||
IsRead uint8 `field:"isRead"` // 是否已读
|
IsRead uint8 `field:"isRead"` // 是否已读
|
||||||
|
Params string `field:"params"` // 参数
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeLogOperator struct {
|
type NodeLogOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
Role interface{} // 节点角色
|
Role interface{} // 节点角色
|
||||||
|
Type interface{} // 类型
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
Tag interface{} // 标签
|
Tag interface{} // 标签
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
@@ -33,6 +36,7 @@ type NodeLogOperator struct {
|
|||||||
Count interface{} // 重复次数
|
Count interface{} // 重复次数
|
||||||
IsFixed interface{} // 是否已处理
|
IsFixed interface{} // 是否已处理
|
||||||
IsRead interface{} // 是否已读
|
IsRead interface{} // 是否已读
|
||||||
|
Params interface{} // 参数
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeLogOperator() *NodeLogOperator {
|
func NewNodeLogOperator() *NodeLogOperator {
|
||||||
|
|||||||
@@ -311,20 +311,30 @@ func (this *NodeTaskDAO) FindAllDoingNodeIds(tx *dbs.Tx, role string) ([]int64,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ExistsDoingNodeTasks 检查是否有正在执行的任务
|
// ExistsDoingNodeTasks 检查是否有正在执行的任务
|
||||||
func (this *NodeTaskDAO) ExistsDoingNodeTasks(tx *dbs.Tx, role string) (bool, error) {
|
func (this *NodeTaskDAO) ExistsDoingNodeTasks(tx *dbs.Tx, role string, excludeTypes []NodeTaskType) (bool, error) {
|
||||||
return this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("role", role).
|
Attr("role", role).
|
||||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||||
Gt("nodeId", 0).
|
Gt("nodeId", 0)
|
||||||
Exist()
|
if len(excludeTypes) > 0 {
|
||||||
|
for _, excludeType := range excludeTypes {
|
||||||
|
query.Neq("type", excludeType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return query.Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExistsErrorNodeTasks 是否有错误的任务
|
// ExistsErrorNodeTasks 是否有错误的任务
|
||||||
func (this *NodeTaskDAO) ExistsErrorNodeTasks(tx *dbs.Tx, role string) (bool, error) {
|
func (this *NodeTaskDAO) ExistsErrorNodeTasks(tx *dbs.Tx, role string, excludeTypes []NodeTaskType) (bool, error) {
|
||||||
return this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("role", role).
|
Attr("role", role).
|
||||||
Where("(isDone=1 AND isOk=0)").
|
Where("(isDone=1 AND isOk=0)")
|
||||||
Exist()
|
if len(excludeTypes) > 0 {
|
||||||
|
for _, excludeType := range excludeTypes {
|
||||||
|
query.Neq("type", excludeType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return query.Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteNodeTask 删除任务
|
// DeleteNodeTask 删除任务
|
||||||
|
|||||||
@@ -55,13 +55,13 @@ func (this *NodeValueDAO) CreateValue(tx *dbs.Tx, clusterId int64, role nodeconf
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteExpiredValues 清除数据
|
// Clean 清除数据
|
||||||
func (this *NodeValueDAO) DeleteExpiredValues(tx *dbs.Tx) error {
|
func (this *NodeValueDAO) Clean(tx *dbs.Tx) error {
|
||||||
// 删除N天之前的所有数据
|
// 删除N天之前的所有数据
|
||||||
expiredDays := 7
|
expiredDays := 2
|
||||||
day := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -expiredDays))
|
day := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -expiredDays))
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Where("day<:day").
|
Where("day<=:day").
|
||||||
Param("day", day).
|
Param("day", day).
|
||||||
Delete()
|
Delete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ const (
|
|||||||
RegionCountryStateDisabled = 0 // 已禁用
|
RegionCountryStateDisabled = 0 // 已禁用
|
||||||
)
|
)
|
||||||
|
|
||||||
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => int
|
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => id
|
||||||
|
var regionCountryIdAndNameCacheMap = map[int64]string{} // country id => name
|
||||||
|
|
||||||
type RegionCountryDAO dbs.DAO
|
type RegionCountryDAO dbs.DAO
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableRegionCountry 启用条目
|
||||||
func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -48,7 +49,7 @@ func (this *RegionCountryDAO) EnableRegionCountry(tx *dbs.Tx, id uint32) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableRegionCountry 禁用条目
|
||||||
func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -57,7 +58,7 @@ func (this *RegionCountryDAO) DisableRegionCountry(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledRegionCountry 查找启用中的条目
|
||||||
func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*RegionCountry, error) {
|
func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*RegionCountry, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -69,15 +70,29 @@ func (this *RegionCountryDAO) FindEnabledRegionCountry(tx *dbs.Tx, id int64) (*R
|
|||||||
return result.(*RegionCountry), err
|
return result.(*RegionCountry), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindRegionCountryName 根据主键查找名称
|
||||||
func (this *RegionCountryDAO) FindRegionCountryName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *RegionCountryDAO) FindRegionCountryName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
SharedCacheLocker.Lock()
|
||||||
|
defer SharedCacheLocker.Unlock()
|
||||||
|
|
||||||
|
name, ok := regionCountryIdAndNameCacheMap[id]
|
||||||
|
if ok {
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
name, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
Result("name").
|
Result("name").
|
||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
regionCountryIdAndNameCacheMap[id] = name
|
||||||
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据数据ID查找国家
|
// FindCountryIdWithDataId 根据数据ID查找国家
|
||||||
func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Attr("dataId", dataId).
|
Attr("dataId", dataId).
|
||||||
@@ -85,7 +100,7 @@ func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string)
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据国家名查找国家ID
|
// FindCountryIdWithName 根据国家名查找国家ID
|
||||||
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Where("JSON_CONTAINS(codes, :countryName)").
|
Where("JSON_CONTAINS(codes, :countryName)").
|
||||||
@@ -94,7 +109,7 @@ func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName stri
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据国家名查找国家ID,并可使用缓存
|
// FindCountryIdWithNameCacheable 根据国家名查找国家ID,并可使用缓存
|
||||||
func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, countryName string) (int64, error) {
|
func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, countryName string) (int64, error) {
|
||||||
SharedCacheLocker.RLock()
|
SharedCacheLocker.RLock()
|
||||||
provinceId, ok := regionCountryNameAndIdCacheMap[countryName]
|
provinceId, ok := regionCountryNameAndIdCacheMap[countryName]
|
||||||
@@ -116,7 +131,7 @@ func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, country
|
|||||||
return countryId, nil
|
return countryId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据数据ID创建国家
|
// CreateCountry 根据数据ID创建国家
|
||||||
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
|
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
|
||||||
op := NewRegionCountryOperator()
|
op := NewRegionCountryOperator()
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -145,7 +160,7 @@ func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId stri
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找所有可用的国家
|
// FindAllEnabledCountriesOrderByPinyin 查找所有可用的国家
|
||||||
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(RegionCountryStateEnabled).
|
State(RegionCountryStateEnabled).
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -21,14 +22,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedServerDailyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedServerDailyStatDAO.Clean(nil, 30) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("ServerDailyStatDAO", "clean expired data failed: "+err.Error())
|
logs.Println("ServerDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -169,6 +169,7 @@ func (this *ServerDAO) CreateServer(tx *dbs.Tx,
|
|||||||
op.ServerNames = serverNamesJSON
|
op.ServerNames = serverNamesJSON
|
||||||
}
|
}
|
||||||
op.IsAuditing = isAuditing
|
op.IsAuditing = isAuditing
|
||||||
|
op.AuditingAt = time.Now().Unix()
|
||||||
if len(auditingServerNamesJSON) > 0 {
|
if len(auditingServerNamesJSON) > 0 {
|
||||||
op.AuditingServerNames = auditingServerNamesJSON
|
op.AuditingServerNames = auditingServerNamesJSON
|
||||||
}
|
}
|
||||||
@@ -533,22 +534,22 @@ func (this *ServerDAO) InitServerWeb(tx *dbs.Tx, serverId int64) (int64, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindServerServerNames 查找ServerNames配置
|
// FindServerServerNames 查找ServerNames配置
|
||||||
func (this *ServerDAO) FindServerServerNames(tx *dbs.Tx, serverId int64) (serverNamesJSON []byte, isAuditing bool, auditingServerNamesJSON []byte, auditingResultJSON []byte, err error) {
|
func (this *ServerDAO) FindServerServerNames(tx *dbs.Tx, serverId int64) (serverNamesJSON []byte, isAuditing bool, auditingAt int64, auditingServerNamesJSON []byte, auditingResultJSON []byte, err error) {
|
||||||
if serverId <= 0 {
|
if serverId <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Pk(serverId).
|
Pk(serverId).
|
||||||
Result("serverNames", "isAuditing", "auditingServerNames", "auditingResult").
|
Result("serverNames", "isAuditing", "auditingAt", "auditingServerNames", "auditingResult").
|
||||||
Find()
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, nil, nil, err
|
return nil, false, 0, nil, nil, err
|
||||||
}
|
}
|
||||||
if one == nil {
|
if one == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
server := one.(*Server)
|
server := one.(*Server)
|
||||||
return []byte(server.ServerNames), server.IsAuditing == 1, []byte(server.AuditingServerNames), []byte(server.AuditingResult), nil
|
return []byte(server.ServerNames), server.IsAuditing == 1, int64(server.AuditingAt), []byte(server.AuditingServerNames), []byte(server.AuditingResult), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateServerNames 修改ServerNames配置
|
// UpdateServerNames 修改ServerNames配置
|
||||||
@@ -580,6 +581,9 @@ func (this *ServerDAO) UpdateAuditingServerNames(tx *dbs.Tx, serverId int64, isA
|
|||||||
op := NewServerOperator()
|
op := NewServerOperator()
|
||||||
op.Id = serverId
|
op.Id = serverId
|
||||||
op.IsAuditing = isAuditing
|
op.IsAuditing = isAuditing
|
||||||
|
if isAuditing {
|
||||||
|
op.AuditingAt = time.Now().Unix()
|
||||||
|
}
|
||||||
if len(auditingServerNamesJSON) == 0 {
|
if len(auditingServerNamesJSON) == 0 {
|
||||||
op.AuditingServerNames = "[]"
|
op.AuditingServerNames = "[]"
|
||||||
} else {
|
} else {
|
||||||
@@ -652,7 +656,7 @@ func (this *ServerDAO) CountAllEnabledServers(tx *dbs.Tx) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledServersMatch 计算所有可用服务数量
|
// CountAllEnabledServersMatch 计算所有可用服务数量
|
||||||
func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag configutils.BoolState, protocolFamily string) (int64, error) {
|
func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag configutils.BoolState, protocolFamilies []string) (int64, error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
State(ServerStateEnabled)
|
State(ServerStateEnabled)
|
||||||
if groupId > 0 {
|
if groupId > 0 {
|
||||||
@@ -678,16 +682,27 @@ func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, ke
|
|||||||
if auditingFlag == configutils.BoolStateYes {
|
if auditingFlag == configutils.BoolStateYes {
|
||||||
query.Attr("isAuditing", true)
|
query.Attr("isAuditing", true)
|
||||||
}
|
}
|
||||||
if protocolFamily == "http" {
|
|
||||||
query.Where("(http IS NOT NULL OR https IS NOT NULL)")
|
var protocolConds = []string{}
|
||||||
} else if protocolFamily == "tcp" {
|
for _, family := range protocolFamilies {
|
||||||
query.Where("(tcp IS NOT NULL OR tls IS NOT NULL)")
|
switch family {
|
||||||
|
case "http":
|
||||||
|
protocolConds = append(protocolConds, "(http IS NOT NULL OR https IS NOT NULL)")
|
||||||
|
case "tcp":
|
||||||
|
protocolConds = append(protocolConds, "(tcp IS NOT NULL OR tls IS NOT NULL)")
|
||||||
|
case "udp":
|
||||||
|
protocolConds = append(protocolConds, "(udp IS NOT NULL)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if len(protocolConds) > 0 {
|
||||||
|
query.Where("(" + strings.Join(protocolConds, " OR ") + ")")
|
||||||
|
}
|
||||||
|
|
||||||
return query.Count()
|
return query.Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListEnabledServersMatch 列出单页的服务
|
// ListEnabledServersMatch 列出单页的服务
|
||||||
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamily string) (result []*Server, err error) {
|
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamilies []string) (result []*Server, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
@@ -718,10 +733,19 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
|
|||||||
if auditingFlag == 1 {
|
if auditingFlag == 1 {
|
||||||
query.Attr("isAuditing", true)
|
query.Attr("isAuditing", true)
|
||||||
}
|
}
|
||||||
if protocolFamily == "http" {
|
var protocolConds = []string{}
|
||||||
query.Where("(http IS NOT NULL OR https IS NOT NULL)")
|
for _, family := range protocolFamilies {
|
||||||
} else if protocolFamily == "tcp" {
|
switch family {
|
||||||
query.Where("(tcp IS NOT NULL OR tls IS NOT NULL)")
|
case "http":
|
||||||
|
protocolConds = append(protocolConds, "(http IS NOT NULL OR https IS NOT NULL)")
|
||||||
|
case "tcp":
|
||||||
|
protocolConds = append(protocolConds, "(tcp IS NOT NULL OR tls IS NOT NULL)")
|
||||||
|
case "udp":
|
||||||
|
protocolConds = append(protocolConds, "(udp IS NOT NULL)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(protocolConds) > 0 {
|
||||||
|
query.Where("(" + strings.Join(protocolConds, " OR ") + ")")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
@@ -1074,7 +1098,8 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
config.UserPlan = &serverconfigs.UserPlanConfig{
|
config.UserPlan = &serverconfigs.UserPlanConfig{
|
||||||
DayTo: userPlan.DayTo,
|
DayTo: userPlan.DayTo,
|
||||||
Plan: &serverconfigs.PlanConfig{
|
Plan: &serverconfigs.PlanConfig{
|
||||||
Id: int64(plan.Id),
|
Id: int64(plan.Id),
|
||||||
|
Name: plan.Name,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1536,16 +1561,67 @@ func (this *ServerDAO) FindEnabledServerIdWithReverseProxyId(tx *dbs.Tx, reverse
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckTCPPortIsUsing 检查TCP端口是否被使用
|
// CheckPortIsUsing 检查端口是否被使用
|
||||||
func (this *ServerDAO) CheckTCPPortIsUsing(tx *dbs.Tx, clusterId int64, port int, excludeServerId int64, excludeProtocol string) (bool, error) {
|
// protocolFamily支持tcp和udp
|
||||||
|
func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, protocolFamily string, port int, excludeServerId int64, excludeProtocol string) (bool, error) {
|
||||||
|
// 检查是否在别的协议中
|
||||||
|
if excludeServerId > 0 {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(excludeServerId).
|
||||||
|
Result("tcp", "tls", "udp", "http", "https").
|
||||||
|
Find()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if one != nil {
|
||||||
|
var server = one.(*Server)
|
||||||
|
for _, protocol := range []string{"http", "https", "tcp", "tls", "udp"} {
|
||||||
|
if protocol == excludeProtocol {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch protocol {
|
||||||
|
case "http":
|
||||||
|
if protocolFamily == "tcp" && lists.ContainsInt(server.DecodeHTTPPorts(), port) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
case "https":
|
||||||
|
if protocolFamily == "tcp" && lists.ContainsInt(server.DecodeHTTPSPorts(), port) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
case "tcp":
|
||||||
|
if protocolFamily == "tcp" && lists.ContainsInt(server.DecodeTCPPorts(), port) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
case "tls":
|
||||||
|
if protocolFamily == "tcp" && lists.ContainsInt(server.DecodeTLSPorts(), port) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
case "udp":
|
||||||
|
// 不需要判断
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他服务中
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
Param("port", types.String(port))
|
Param("port", types.String(port))
|
||||||
if excludeServerId <= 0 {
|
if excludeServerId <= 0 {
|
||||||
query.Where("JSON_CONTAINS(tcpPorts, :port)")
|
switch protocolFamily {
|
||||||
|
case "tcp", "http", "":
|
||||||
|
query.Where("JSON_CONTAINS(tcpPorts, :port)")
|
||||||
|
case "udp":
|
||||||
|
query.Where("JSON_CONTAINS(udpPorts, :port)")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
query.Where("(id!=:serverId AND JSON_CONTAINS(tcpPorts, :port))")
|
switch protocolFamily {
|
||||||
|
case "tcp", "http", "":
|
||||||
|
query.Where("(id!=:serverId AND JSON_CONTAINS(tcpPorts, :port))")
|
||||||
|
case "udp":
|
||||||
|
query.Where("(id!=:serverId AND JSON_CONTAINS(udpPorts, :port))")
|
||||||
|
}
|
||||||
query.Param("serverId", excludeServerId)
|
query.Param("serverId", excludeServerId)
|
||||||
}
|
}
|
||||||
return query.
|
return query.
|
||||||
@@ -1854,9 +1930,11 @@ func (this *ServerDAO) FindServerTrafficLimitConfig(tx *dbs.Tx, serverId int64,
|
|||||||
|
|
||||||
var trafficLimit = serverOne.(*Server).TrafficLimit
|
var trafficLimit = serverOne.(*Server).TrafficLimit
|
||||||
|
|
||||||
err = json.Unmarshal([]byte(trafficLimit), limit)
|
if len(trafficLimit) > 0 {
|
||||||
if err != nil {
|
err = json.Unmarshal([]byte(trafficLimit), limit)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cacheMap != nil {
|
if cacheMap != nil {
|
||||||
@@ -1965,6 +2043,7 @@ func (this *ServerDAO) UpdateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64
|
|||||||
return this.UpdateServerTrafficLimitStatus(tx, trafficLimitConfig, serverId, true)
|
return this.UpdateServerTrafficLimitStatus(tx, trafficLimitConfig, serverId, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateServerTrafficLimitStatus 修改服务的流量限制状态
|
||||||
func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitConfig *serverconfigs.TrafficLimitConfig, serverId int64, isUpdatingConfig bool) error {
|
func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitConfig *serverconfigs.TrafficLimitConfig, serverId int64, isUpdatingConfig bool) error {
|
||||||
if !trafficLimitConfig.IsOn {
|
if !trafficLimitConfig.IsOn {
|
||||||
if isUpdatingConfig {
|
if isUpdatingConfig {
|
||||||
@@ -2073,7 +2152,7 @@ func (this *ServerDAO) ResetServerTotalTraffic(tx *dbs.Tx, serverId int64) error
|
|||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledServerIdWithUserPlanId 查找使用某个套餐的服务
|
// FindEnabledServerIdWithUserPlanId 查找使用某个套餐的服务ID
|
||||||
func (this *ServerDAO) FindEnabledServerIdWithUserPlanId(tx *dbs.Tx, userPlanId int64) (int64, error) {
|
func (this *ServerDAO) FindEnabledServerIdWithUserPlanId(tx *dbs.Tx, userPlanId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
@@ -2082,6 +2161,19 @@ func (this *ServerDAO) FindEnabledServerIdWithUserPlanId(tx *dbs.Tx, userPlanId
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledServerWithUserPlanId 查找使用某个套餐的服务
|
||||||
|
func (this *ServerDAO) FindEnabledServerWithUserPlanId(tx *dbs.Tx, userPlanId int64) (*Server, error) {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
State(ServerStateEnabled).
|
||||||
|
Attr("userPlanId", userPlanId).
|
||||||
|
Result("id", "name", "serverNames", "type").
|
||||||
|
Find()
|
||||||
|
if err != nil || one == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return one.(*Server), nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateServersClusterIdWithPlanId 修改套餐所在集群
|
// UpdateServersClusterIdWithPlanId 修改套餐所在集群
|
||||||
func (this *ServerDAO) UpdateServersClusterIdWithPlanId(tx *dbs.Tx, planId int64, clusterId int64) error {
|
func (this *ServerDAO) UpdateServersClusterIdWithPlanId(tx *dbs.Tx, planId int64, clusterId int64) error {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -2093,6 +2185,40 @@ func (this *ServerDAO) UpdateServersClusterIdWithPlanId(tx *dbs.Tx, planId int64
|
|||||||
|
|
||||||
// UpdateServerUserPlanId 设置服务所属套餐
|
// UpdateServerUserPlanId 设置服务所属套餐
|
||||||
func (this *ServerDAO) UpdateServerUserPlanId(tx *dbs.Tx, serverId int64, userPlanId int64) error {
|
func (this *ServerDAO) UpdateServerUserPlanId(tx *dbs.Tx, serverId int64, userPlanId int64) error {
|
||||||
|
// 取消套餐
|
||||||
|
if userPlanId <= 0 {
|
||||||
|
// 所属用户
|
||||||
|
userId, err := this.Query(tx).
|
||||||
|
Pk(serverId).
|
||||||
|
Result("userId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if userId <= 0 {
|
||||||
|
return errors.New("can not cancel the server plan, because the server has no user")
|
||||||
|
}
|
||||||
|
|
||||||
|
clusterId, err := SharedUserDAO.FindUserClusterId(tx, userId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if clusterId <= 0 {
|
||||||
|
return errors.New("can not cancel the server plan, because the server use does not have a cluster")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(serverId).
|
||||||
|
Set("userPlanId", userPlanId).
|
||||||
|
Set("clusterId", clusterId).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, serverId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置新套餐
|
||||||
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, nil)
|
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ func TestServerDAO_CheckPortIsUsing(t *testing.T) {
|
|||||||
// t.Log("isUsing:", isUsing)
|
// t.Log("isUsing:", isUsing)
|
||||||
//}
|
//}
|
||||||
{
|
{
|
||||||
isUsing, err := SharedServerDAO.CheckTCPPortIsUsing(tx, 18, 3306, 0, "tcp")
|
isUsing, err := SharedServerDAO.CheckPortIsUsing(tx, 18, "tcp", 3306, 0, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ type Server struct {
|
|||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
ServerNames string `field:"serverNames"` // 域名列表
|
ServerNames string `field:"serverNames"` // 域名列表
|
||||||
|
AuditingAt uint64 `field:"auditingAt"` // 审核提交时间
|
||||||
AuditingServerNames string `field:"auditingServerNames"` // 审核中的域名
|
AuditingServerNames string `field:"auditingServerNames"` // 审核中的域名
|
||||||
IsAuditing uint8 `field:"isAuditing"` // 是否正在审核
|
IsAuditing uint8 `field:"isAuditing"` // 是否正在审核
|
||||||
AuditingResult string `field:"auditingResult"` // 审核结果
|
AuditingResult string `field:"auditingResult"` // 审核结果
|
||||||
@@ -53,6 +54,7 @@ type ServerOperator struct {
|
|||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
ServerNames interface{} // 域名列表
|
ServerNames interface{} // 域名列表
|
||||||
|
AuditingAt interface{} // 审核提交时间
|
||||||
AuditingServerNames interface{} // 审核中的域名
|
AuditingServerNames interface{} // 审核中的域名
|
||||||
IsAuditing interface{} // 是否正在审核
|
IsAuditing interface{} // 是否正在审核
|
||||||
AuditingResult interface{} // 审核结果
|
AuditingResult interface{} // 审核结果
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DecodeGroupIds 解析服务所属分组ID
|
// DecodeGroupIds 解析服务所属分组ID
|
||||||
@@ -19,3 +20,108 @@ func (this *Server) DecodeGroupIds() []int64 {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DecodeHTTPPorts 获取HTTP所有端口
|
||||||
|
func (this *Server) DecodeHTTPPorts() (ports []int) {
|
||||||
|
if len(this.Http) > 0 && this.Http != "null" {
|
||||||
|
config := &serverconfigs.HTTPProtocolConfig{}
|
||||||
|
err := json.Unmarshal([]byte(this.Http), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = config.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, listen := range config.Listen {
|
||||||
|
for i := listen.MinPort; i <= listen.MaxPort; i++ {
|
||||||
|
ports = append(ports, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeHTTPSPorts 获取HTTPS所有端口
|
||||||
|
func (this *Server) DecodeHTTPSPorts() (ports []int) {
|
||||||
|
if len(this.Https) > 0 && this.Https != "null" {
|
||||||
|
config := &serverconfigs.HTTPSProtocolConfig{}
|
||||||
|
err := json.Unmarshal([]byte(this.Https), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = config.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, listen := range config.Listen {
|
||||||
|
for i := listen.MinPort; i <= listen.MaxPort; i++ {
|
||||||
|
ports = append(ports, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeTCPPorts 获取TCP所有端口
|
||||||
|
func (this *Server) DecodeTCPPorts() (ports []int) {
|
||||||
|
if len(this.Tcp) > 0 && this.Tcp != "null" {
|
||||||
|
config := &serverconfigs.TCPProtocolConfig{}
|
||||||
|
err := json.Unmarshal([]byte(this.Tcp), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = config.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, listen := range config.Listen {
|
||||||
|
for i := listen.MinPort; i <= listen.MaxPort; i++ {
|
||||||
|
ports = append(ports, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeTLSPorts 获取TLS所有端口
|
||||||
|
func (this *Server) DecodeTLSPorts() (ports []int) {
|
||||||
|
if len(this.Tls) > 0 && this.Tls != "null" {
|
||||||
|
config := &serverconfigs.TLSProtocolConfig{}
|
||||||
|
err := json.Unmarshal([]byte(this.Tls), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = config.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, listen := range config.Listen {
|
||||||
|
for i := listen.MinPort; i <= listen.MaxPort; i++ {
|
||||||
|
ports = append(ports, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeUDPPorts 获取UDP所有端口
|
||||||
|
func (this *Server) DecodeUDPPorts() (ports []int) {
|
||||||
|
if len(this.Udp) > 0 && this.Udp != "null" {
|
||||||
|
config := &serverconfigs.UDPProtocolConfig{}
|
||||||
|
err := json.Unmarshal([]byte(this.Udp), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = config.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, listen := range config.Listen {
|
||||||
|
for i := listen.MinPort; i <= listen.MaxPort; i++ {
|
||||||
|
ports = append(ports, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedNodeClusterTrafficDailyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedNodeClusterTrafficDailyStatDAO.Clean(nil, 30) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedNodeTrafficDailyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedNodeTrafficDailyStatDAO.Clean(nil, 30) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("NodeTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("NodeTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedNodeTrafficHourlyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedNodeTrafficHourlyStatDAO.Clean(nil, 15) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("NodeTrafficHourlyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("NodeTrafficHourlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,32 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedServerClientBrowserMonthlyStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ServerClientBrowserMonthlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type ServerClientBrowserMonthlyStatDAO dbs.DAO
|
type ServerClientBrowserMonthlyStatDAO dbs.DAO
|
||||||
|
|
||||||
func NewServerClientBrowserMonthlyStatDAO() *ServerClientBrowserMonthlyStatDAO {
|
func NewServerClientBrowserMonthlyStatDAO() *ServerClientBrowserMonthlyStatDAO {
|
||||||
@@ -29,7 +49,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加数量
|
// IncreaseMonthlyCount 增加数量
|
||||||
func (this *ServerClientBrowserMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, browserId int64, version string, month string, count int64) error {
|
func (this *ServerClientBrowserMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, browserId int64, version string, month string, count int64) error {
|
||||||
if len(month) != 6 {
|
if len(month) != 6 {
|
||||||
return errors.New("invalid month '" + month + "'")
|
return errors.New("invalid month '" + month + "'")
|
||||||
@@ -51,7 +71,7 @@ func (this *ServerClientBrowserMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找单页数据
|
// ListStats 查找单页数据
|
||||||
func (this *ServerClientBrowserMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerClientBrowserMonthlyStat, err error) {
|
func (this *ServerClientBrowserMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerClientBrowserMonthlyStat, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -63,3 +83,13 @@ func (this *ServerClientBrowserMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId in
|
|||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean 清理统计数据
|
||||||
|
func (this *ServerClientBrowserMonthlyStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
// 只保留两个月的
|
||||||
|
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Lte("month", month).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,3 +17,12 @@ func TestServerClientBrowserMonthlyStatDAO_IncreaseMonthlyCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServerClientBrowserMonthlyStatDAO_Clean(t *testing.T) {
|
||||||
|
var dao = NewServerClientBrowserMonthlyStatDAO()
|
||||||
|
err := dao.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,12 +2,32 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedServerClientSystemMonthlyStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ServerClientSystemMonthlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type ServerClientSystemMonthlyStatDAO dbs.DAO
|
type ServerClientSystemMonthlyStatDAO dbs.DAO
|
||||||
|
|
||||||
func NewServerClientSystemMonthlyStatDAO() *ServerClientSystemMonthlyStatDAO {
|
func NewServerClientSystemMonthlyStatDAO() *ServerClientSystemMonthlyStatDAO {
|
||||||
@@ -29,7 +49,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加数量
|
// IncreaseMonthlyCount 增加数量
|
||||||
func (this *ServerClientSystemMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, systemId int64, version string, month string, count int64) error {
|
func (this *ServerClientSystemMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, systemId int64, version string, month string, count int64) error {
|
||||||
if len(month) != 6 {
|
if len(month) != 6 {
|
||||||
return errors.New("invalid month '" + month + "'")
|
return errors.New("invalid month '" + month + "'")
|
||||||
@@ -51,7 +71,7 @@ func (this *ServerClientSystemMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, s
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找单页数据
|
// ListStats 查找单页数据
|
||||||
func (this *ServerClientSystemMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerClientSystemMonthlyStat, err error) {
|
func (this *ServerClientSystemMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerClientSystemMonthlyStat, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -63,3 +83,13 @@ func (this *ServerClientSystemMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int
|
|||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean 清理统计数据
|
||||||
|
func (this *ServerClientSystemMonthlyStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
// 只保留两个月的
|
||||||
|
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Lte("month", month).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,4 +2,14 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestServerClientSystemMonthlyStatDAO_Clean(t *testing.T) {
|
||||||
|
var dao = NewServerClientSystemMonthlyStatDAO()
|
||||||
|
err := dao.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -22,14 +23,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedServerDomainHourlyStatDAO.Clean(nil, 7) // 只保留7天
|
err := SharedServerDomainHourlyStatDAO.Clean(nil, 7) // 只保留7天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("ServerDomainHourlyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("ServerDomainHourlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package stats
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedServerHTTPFirewallDailyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedServerHTTPFirewallDailyStatDAO.Clean(nil, 30) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("ServerHTTPFirewallDailyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("ServerHTTPFirewallDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,4 +134,3 @@ func (this *ServerHTTPFirewallDailyStatDAO) Clean(tx *dbs.Tx, days int) error {
|
|||||||
Delete()
|
Delete()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package stats
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedServerHTTPFirewallHourlyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedServerHTTPFirewallHourlyStatDAO.Clean(nil, 15) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("ServerHTTPFirewallHourlyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("ServerHTTPFirewallHourlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,32 @@ package stats
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedServerRegionCityMonthlyStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("SharedServerRegionCityMonthlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type ServerRegionCityMonthlyStatDAO dbs.DAO
|
type ServerRegionCityMonthlyStatDAO dbs.DAO
|
||||||
|
|
||||||
func NewServerRegionCityMonthlyStatDAO() *ServerRegionCityMonthlyStatDAO {
|
func NewServerRegionCityMonthlyStatDAO() *ServerRegionCityMonthlyStatDAO {
|
||||||
@@ -30,7 +50,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加数量
|
// IncreaseMonthlyCount 增加数量
|
||||||
func (this *ServerRegionCityMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, cityId int64, month string, count int64) error {
|
func (this *ServerRegionCityMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, cityId int64, month string, count int64) error {
|
||||||
if len(month) != 6 {
|
if len(month) != 6 {
|
||||||
return errors.New("invalid month '" + month + "'")
|
return errors.New("invalid month '" + month + "'")
|
||||||
@@ -51,7 +71,7 @@ func (this *ServerRegionCityMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, ser
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找单页数据
|
// ListStats 查找单页数据
|
||||||
func (this *ServerRegionCityMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, countryId int64, provinceId int64, offset int64, size int64) (result []*ServerRegionCityMonthlyStat, err error) {
|
func (this *ServerRegionCityMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, countryId int64, provinceId int64, offset int64, size int64) (result []*ServerRegionCityMonthlyStat, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -71,3 +91,13 @@ func (this *ServerRegionCityMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64
|
|||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean 清理统计数据
|
||||||
|
func (this *ServerRegionCityMonthlyStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
// 只保留两个月的
|
||||||
|
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Lte("month", month).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,4 +2,14 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestServerRegionCityMonthlyStatDAO_Clean(t *testing.T) {
|
||||||
|
var dao = NewServerRegionCityMonthlyStatDAO()
|
||||||
|
err := dao.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
173
internal/db/models/stats/server_region_country_daily_stat_dao.go
Normal file
173
internal/db/models/stats/server_region_country_daily_stat_dao.go
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package stats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedServerRegionCountryDailyStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ServerRegionCountryDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerRegionCountryDailyStatDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewServerRegionCountryDailyStatDAO() *ServerRegionCountryDailyStatDAO {
|
||||||
|
return dbs.NewDAO(&ServerRegionCountryDailyStatDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeServerRegionCountryDailyStats",
|
||||||
|
Model: new(ServerRegionCountryDailyStat),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*ServerRegionCountryDailyStatDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedServerRegionCountryDailyStatDAO *ServerRegionCountryDailyStatDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedServerRegionCountryDailyStatDAO = NewServerRegionCountryDailyStatDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncreaseDailyStat 增加统计
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) IncreaseDailyStat(tx *dbs.Tx, serverId int64, countryId int64, day string, bytes int64, countRequests int64, attackBytes int64, countAttackRequests int64) error {
|
||||||
|
if len(day) != 8 {
|
||||||
|
return errors.New("invalid day '" + day + "'")
|
||||||
|
}
|
||||||
|
err := this.Query(tx).
|
||||||
|
Param("bytes", bytes).
|
||||||
|
Param("countRequests", countRequests).
|
||||||
|
Param("attackBytes", attackBytes).
|
||||||
|
Param("countAttackRequests", countAttackRequests).
|
||||||
|
InsertOrUpdateQuickly(maps.Map{
|
||||||
|
"serverId": serverId,
|
||||||
|
"countryId": countryId,
|
||||||
|
"day": day,
|
||||||
|
"bytes": bytes,
|
||||||
|
"attackBytes": attackBytes,
|
||||||
|
"countRequests": countRequests,
|
||||||
|
"countAttackRequests": countAttackRequests,
|
||||||
|
}, maps.Map{
|
||||||
|
"bytes": dbs.SQL("bytes+:bytes"),
|
||||||
|
"countRequests": dbs.SQL("countRequests+:countRequests"),
|
||||||
|
"attackBytes": dbs.SQL("attackBytes+:attackBytes"),
|
||||||
|
"countAttackRequests": dbs.SQL("countAttackRequests+:countAttackRequests"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListServerStats 查找单页数据
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) ListServerStats(tx *dbs.Tx, serverId int64, day string, orderField string, offset int64, size int64) (result []*ServerRegionCountryDailyStat, err error) {
|
||||||
|
query := this.Query(tx).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Attr("day", day).
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result)
|
||||||
|
|
||||||
|
switch orderField {
|
||||||
|
case "bytes":
|
||||||
|
query.Desc("bytes")
|
||||||
|
case "countRequests":
|
||||||
|
query.Desc("countRequests")
|
||||||
|
case "attackBytes":
|
||||||
|
query.Desc("attackBytes")
|
||||||
|
query.Gt("attackBytes", 0)
|
||||||
|
case "countAttackRequests":
|
||||||
|
query.Desc("countAttackRequests")
|
||||||
|
query.Gt("countAttackRequests", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListSumStats 查找总体数据
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) ListSumStats(tx *dbs.Tx, day string, orderField string, offset int64, size int64) (result []*ServerRegionCountryDailyStat, err error) {
|
||||||
|
query := this.Query(tx).
|
||||||
|
Attr("day", day).
|
||||||
|
Result("countryId", "SUM(bytes) AS bytes", "SUM(countRequests) AS countRequests", "SUM(attackBytes) AS attackBytes", "SUM(countAttackRequests) AS countAttackRequests").
|
||||||
|
Group("countryId").
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result)
|
||||||
|
|
||||||
|
switch orderField {
|
||||||
|
case "bytes":
|
||||||
|
query.Desc("bytes")
|
||||||
|
case "countRequests":
|
||||||
|
query.Desc("countRequests")
|
||||||
|
case "attackBytes":
|
||||||
|
query.Desc("attackBytes")
|
||||||
|
query.Gt("attackBytes", 0)
|
||||||
|
case "countAttackRequests":
|
||||||
|
query.Desc("countAttackRequests")
|
||||||
|
query.Gt("countAttackRequests", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SumDailyTotalBytes 计算总流量
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) SumDailyTotalBytes(tx *dbs.Tx, day string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("day", day).
|
||||||
|
SumInt64("bytes", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SumDailyTotalAttackRequests 计算总攻击次数
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) SumDailyTotalAttackRequests(tx *dbs.Tx, day string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("day", day).
|
||||||
|
SumInt64("countAttackRequests", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SumDailyTotalBytesWithServerId 计算单个服务的总流量
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) SumDailyTotalBytesWithServerId(tx *dbs.Tx, day string, serverId int64) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("day", day).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
SumInt64("bytes", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SumDailyTotalAttackRequestsWithServerId 计算单个服务的总攻击次数
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) SumDailyTotalAttackRequestsWithServerId(tx *dbs.Tx, day string, serverId int64) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("day", day).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
SumInt64("countAttackRequests", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean 清理统计数据
|
||||||
|
func (this *ServerRegionCountryDailyStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
// 只保留7天的
|
||||||
|
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -7))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Lte("day", day).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package stats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServerRegionCountryDailyStatDAO_IncreaseDailyStat(t *testing.T) {
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := NewServerRegionCountryDailyStatDAO().IncreaseDailyStat(tx, 1, 3, timeutil.Format("Ymd"), 2, 2, 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerRegionCountryDailyStatDAO_ListSumStats(t *testing.T) {
|
||||||
|
var tx *dbs.Tx
|
||||||
|
stats, err := NewServerRegionCountryDailyStatDAO().ListSumStats(tx, timeutil.Format("Ymd"), "countAttackRequests", 0, 10)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, stat := range stats {
|
||||||
|
statJSON, err := json.Marshal(stat)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(string(statJSON))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package stats
|
||||||
|
|
||||||
|
// ServerRegionCountryDailyStat 服务用户区域分布统计(按天)
|
||||||
|
type ServerRegionCountryDailyStat struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
ServerId uint32 `field:"serverId"` // 服务ID
|
||||||
|
CountryId uint32 `field:"countryId"` // 国家/区域ID
|
||||||
|
Day string `field:"day"` // 日期YYYYMMDD
|
||||||
|
CountRequests uint64 `field:"countRequests"` // 请求数量
|
||||||
|
CountAttackRequests uint64 `field:"countAttackRequests"` // 攻击数量
|
||||||
|
AttackBytes uint64 `field:"attackBytes"` // 攻击流量
|
||||||
|
Bytes uint64 `field:"bytes"` // 总流量
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerRegionCountryDailyStatOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
ServerId interface{} // 服务ID
|
||||||
|
CountryId interface{} // 国家/区域ID
|
||||||
|
Day interface{} // 日期YYYYMMDD
|
||||||
|
CountRequests interface{} // 请求数量
|
||||||
|
CountAttackRequests interface{} // 攻击数量
|
||||||
|
AttackBytes interface{} // 攻击流量
|
||||||
|
Bytes interface{} // 总流量
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServerRegionCountryDailyStatOperator() *ServerRegionCountryDailyStatOperator {
|
||||||
|
return &ServerRegionCountryDailyStatOperator{}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package stats
|
||||||
@@ -2,12 +2,32 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedServerRegionCountryMonthlyStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("SharedServerRegionCountryMonthlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type ServerRegionCountryMonthlyStatDAO dbs.DAO
|
type ServerRegionCountryMonthlyStatDAO dbs.DAO
|
||||||
|
|
||||||
func NewServerRegionCountryMonthlyStatDAO() *ServerRegionCountryMonthlyStatDAO {
|
func NewServerRegionCountryMonthlyStatDAO() *ServerRegionCountryMonthlyStatDAO {
|
||||||
@@ -29,7 +49,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加数量
|
// IncreaseMonthlyCount 增加数量
|
||||||
func (this *ServerRegionCountryMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, countryId int64, month string, count int64) error {
|
func (this *ServerRegionCountryMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, countryId int64, month string, count int64) error {
|
||||||
if len(month) != 6 {
|
if len(month) != 6 {
|
||||||
return errors.New("invalid month '" + month + "'")
|
return errors.New("invalid month '" + month + "'")
|
||||||
@@ -50,7 +70,7 @@ func (this *ServerRegionCountryMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找单页数据
|
// ListStats 查找单页数据
|
||||||
func (this *ServerRegionCountryMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerRegionCountryMonthlyStat, err error) {
|
func (this *ServerRegionCountryMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerRegionCountryMonthlyStat, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -62,3 +82,13 @@ func (this *ServerRegionCountryMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId in
|
|||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean 清理统计数据
|
||||||
|
func (this *ServerRegionCountryMonthlyStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
// 只保留两个月的
|
||||||
|
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Lte("month", month).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,12 +2,32 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedServerRegionProviderMonthlyStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("SharedServerRegionProviderMonthlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type ServerRegionProviderMonthlyStatDAO dbs.DAO
|
type ServerRegionProviderMonthlyStatDAO dbs.DAO
|
||||||
|
|
||||||
func NewServerRegionProviderMonthlyStatDAO() *ServerRegionProviderMonthlyStatDAO {
|
func NewServerRegionProviderMonthlyStatDAO() *ServerRegionProviderMonthlyStatDAO {
|
||||||
@@ -29,7 +49,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加数量
|
// IncreaseMonthlyCount 增加数量
|
||||||
func (this *ServerRegionProviderMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, providerId int64, month string, count int64) error {
|
func (this *ServerRegionProviderMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, providerId int64, month string, count int64) error {
|
||||||
if len(month) != 6 {
|
if len(month) != 6 {
|
||||||
return errors.New("invalid month '" + month + "'")
|
return errors.New("invalid month '" + month + "'")
|
||||||
@@ -50,7 +70,7 @@ func (this *ServerRegionProviderMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找单页数据
|
// ListStats 查找单页数据
|
||||||
func (this *ServerRegionProviderMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerRegionProviderMonthlyStat, err error) {
|
func (this *ServerRegionProviderMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerRegionProviderMonthlyStat, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -62,3 +82,13 @@ func (this *ServerRegionProviderMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId i
|
|||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean 清理统计数据
|
||||||
|
func (this *ServerRegionProviderMonthlyStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
// 只保留两个月的
|
||||||
|
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Lte("month", month).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,4 +2,15 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func TestServerRegionProviderMonthlyStatDAO_Clean(t *testing.T) {
|
||||||
|
var dao = NewServerRegionProviderMonthlyStatDAO()
|
||||||
|
err := dao.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,12 +3,32 @@ package stats
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedServerRegionProvinceMonthlyStatDAO.Clean(nil)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("SharedServerRegionProvinceMonthlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type ServerRegionProvinceMonthlyStatDAO dbs.DAO
|
type ServerRegionProvinceMonthlyStatDAO dbs.DAO
|
||||||
|
|
||||||
func NewServerRegionProvinceMonthlyStatDAO() *ServerRegionProvinceMonthlyStatDAO {
|
func NewServerRegionProvinceMonthlyStatDAO() *ServerRegionProvinceMonthlyStatDAO {
|
||||||
@@ -30,7 +50,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加数量
|
// IncreaseMonthlyCount 增加数量
|
||||||
func (this *ServerRegionProvinceMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, provinceId int64, month string, count int64) error {
|
func (this *ServerRegionProvinceMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, provinceId int64, month string, count int64) error {
|
||||||
if len(month) != 6 {
|
if len(month) != 6 {
|
||||||
return errors.New("invalid month '" + month + "'")
|
return errors.New("invalid month '" + month + "'")
|
||||||
@@ -51,7 +71,7 @@ func (this *ServerRegionProvinceMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找单页数据
|
// ListStats 查找单页数据
|
||||||
func (this *ServerRegionProvinceMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, countryId int64, offset int64, size int64) (result []*ServerRegionProvinceMonthlyStat, err error) {
|
func (this *ServerRegionProvinceMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, countryId int64, offset int64, size int64) (result []*ServerRegionProvinceMonthlyStat, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -68,3 +88,13 @@ func (this *ServerRegionProvinceMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId i
|
|||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean 清理统计数据
|
||||||
|
func (this *ServerRegionProvinceMonthlyStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
|
// 只保留两个月的
|
||||||
|
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Lte("month", month).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedTrafficDailyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedTrafficDailyStatDAO.Clean(nil, 30) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("TrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("TrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stats
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -19,14 +20,14 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
// 清理数据任务
|
// 清理数据任务
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := SharedTrafficHourlyStatDAO.Clean(nil, 60) // 只保留60天
|
err := SharedTrafficHourlyStatDAO.Clean(nil, 15) // 只保留N天
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("TrafficHourlyStatDAO", "clean expired data failed: "+err.Error())
|
remotelogs.Error("TrafficHourlyStatDAO", "clean expired data failed: "+err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/zero"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -40,6 +42,16 @@ func (this *SysSettingDAO) UpdateSetting(tx *dbs.Tx, codeFormat string, valueJSO
|
|||||||
|
|
||||||
countRetries := 3
|
countRetries := 3
|
||||||
var lastErr error
|
var lastErr error
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if lastErr == nil {
|
||||||
|
err := this.NotifyUpdate(tx, codeFormat)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("SysSettingDAO", "notify update failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for i := 0; i < countRetries; i++ {
|
for i := 0; i < countRetries; i++ {
|
||||||
settingId, err := this.Query(tx).
|
settingId, err := this.Query(tx).
|
||||||
Attr("code", codeFormat).
|
Attr("code", codeFormat).
|
||||||
@@ -61,6 +73,8 @@ func (this *SysSettingDAO) UpdateSetting(tx *dbs.Tx, codeFormat string, valueJSO
|
|||||||
// 因为错误的原因可能是因为code冲突,所以这里我们继续执行
|
// 因为错误的原因可能是因为code冲突,所以这里我们继续执行
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastErr = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,6 +86,8 @@ func (this *SysSettingDAO) UpdateSetting(tx *dbs.Tx, codeFormat string, valueJSO
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
lastErr = nil
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return lastErr
|
return lastErr
|
||||||
@@ -121,3 +137,12 @@ func (this *SysSettingDAO) ReadGlobalConfig(tx *dbs.Tx) (*serverconfigs.GlobalCo
|
|||||||
}
|
}
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更改
|
||||||
|
func (this *SysSettingDAO) NotifyUpdate(tx *dbs.Tx, code string) error {
|
||||||
|
switch code {
|
||||||
|
case systemconfigs.SettingCodeAccessLogQueue:
|
||||||
|
accessLogQueueChanged <- zero.New()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
@@ -20,7 +21,7 @@ func init() {
|
|||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
var generatedMonths = []string{}
|
var generatedMonths = []string{}
|
||||||
|
|
||||||
go func() {
|
goman.New(func() {
|
||||||
// 自动生成账单任务
|
// 自动生成账单任务
|
||||||
var ticker = time.NewTicker(1 * time.Minute)
|
var ticker = time.NewTicker(1 * time.Minute)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
@@ -37,7 +38,7 @@ func init() {
|
|||||||
generatedMonths = append(generatedMonths, lastMonth)
|
generatedMonths = append(generatedMonths, lastMonth)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +69,17 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindUserBill 查找单个账单
|
||||||
|
func (this *UserBillDAO) FindUserBill(tx *dbs.Tx, billId int64) (*UserBill, error) {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(billId).
|
||||||
|
Find()
|
||||||
|
if err != nil || one == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return one.(*UserBill), nil
|
||||||
|
}
|
||||||
|
|
||||||
// CountAllUserBills 计算账单数量
|
// CountAllUserBills 计算账单数量
|
||||||
func (this *UserBillDAO) CountAllUserBills(tx *dbs.Tx, isPaid int32, userId int64, month string) (int64, error) {
|
func (this *UserBillDAO) CountAllUserBills(tx *dbs.Tx, isPaid int32, userId int64, month string) (int64, error) {
|
||||||
query := this.Query(tx)
|
query := this.Query(tx)
|
||||||
@@ -330,3 +342,33 @@ func (this *UserBillDAO) GenerateBillCode(tx *dbs.Tx) (string, error) {
|
|||||||
}
|
}
|
||||||
return this.GenerateBillCode(tx)
|
return this.GenerateBillCode(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckUserBill 检查用户账单
|
||||||
|
func (this *UserBillDAO) CheckUserBill(tx *dbs.Tx, userId int64, billId int64) error {
|
||||||
|
if userId <= 0 || billId <= 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
exists, err := this.Query(tx).
|
||||||
|
Pk(billId).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SumUnpaidUserBill 计算某个用户未支付总额
|
||||||
|
func (this *UserBillDAO) SumUnpaidUserBill(tx *dbs.Tx, userId int64) (float32, error) {
|
||||||
|
sum, err := this.Query(tx).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Attr("isPaid", 0).
|
||||||
|
Sum("amount", 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return float32(sum), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -276,7 +277,7 @@ func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON [
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindUserFeatures 查找用户Features
|
// FindUserFeatures 查找用户Features
|
||||||
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*UserFeature, error) {
|
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*userconfigs.UserFeature, error) {
|
||||||
featuresJSON, err := this.Query(tx).
|
featuresJSON, err := this.Query(tx).
|
||||||
Pk(userId).
|
Pk(userId).
|
||||||
Result("features").
|
Result("features").
|
||||||
@@ -295,12 +296,12 @@ func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*UserFeature,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否还存在以及设置名称
|
// 检查是否还存在以及设置名称
|
||||||
result := []*UserFeature{}
|
result := []*userconfigs.UserFeature{}
|
||||||
if len(featureCodes) > 0 {
|
if len(featureCodes) > 0 {
|
||||||
for _, featureCode := range featureCodes {
|
for _, featureCode := range featureCodes {
|
||||||
f := FindUserFeature(featureCode)
|
f := userconfigs.FindUserFeature(featureCode)
|
||||||
if f != nil {
|
if f != nil {
|
||||||
result = append(result, &UserFeature{Name: f.Name, Code: f.Code, Description: f.Description})
|
result = append(result, &userconfigs.UserFeature{Name: f.Name, Code: f.Code, Description: f.Description})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
|
|
||||||
var (
|
|
||||||
// 所有功能列表,注意千万不能在运行时进行修改
|
|
||||||
allUserFeatures = []*UserFeature{
|
|
||||||
{
|
|
||||||
Name: "记录访问日志",
|
|
||||||
Code: "server.accessLog",
|
|
||||||
Description: "用户可以开启服务的访问日志",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "查看访问日志",
|
|
||||||
Code: "server.viewAccessLog",
|
|
||||||
Description: "用户可以查看服务的访问日志",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "转发访问日志",
|
|
||||||
Code: "server.accessLog.forward",
|
|
||||||
Description: "用户可以配置访问日志转发到自定义的API",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "负载均衡",
|
|
||||||
Code: "server.tcp",
|
|
||||||
Description: "用户可以添加TCP/TLS负载均衡服务",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "自定义负载均衡端口",
|
|
||||||
Code: "server.tcp.port",
|
|
||||||
Description: "用户可以自定义TCP端口",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "开启WAF",
|
|
||||||
Code: "server.waf",
|
|
||||||
Description: "用户可以开启WAF功能并可以设置黑白名单等",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "费用账单",
|
|
||||||
Code: "finance",
|
|
||||||
Description: "开启费用账单相关功能",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// 用户功能
|
|
||||||
type UserFeature struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Code string `json:"code"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *UserFeature) ToPB() *pb.UserFeature {
|
|
||||||
return &pb.UserFeature{Name: this.Name, Code: this.Code, Description: this.Description}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 所有功能列表
|
|
||||||
func FindAllUserFeatures() []*UserFeature {
|
|
||||||
return allUserFeatures
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询单个功能
|
|
||||||
func FindUserFeature(code string) *UserFeature {
|
|
||||||
for _, feature := range allUserFeatures {
|
|
||||||
if feature.Code == code {
|
|
||||||
return feature
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -86,11 +86,17 @@ func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64, cache
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledUserPlans 计算套餐数量
|
// CountAllEnabledUserPlans 计算套餐数量
|
||||||
func (this *UserPlanDAO) CountAllEnabledUserPlans(tx *dbs.Tx, isAvailable bool, isExpired bool, expiringDays int32) (int64, error) {
|
func (this *UserPlanDAO) CountAllEnabledUserPlans(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int32) (int64, error) {
|
||||||
var query = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
State(UserPlanStateEnabled).
|
State(UserPlanStateEnabled).
|
||||||
Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)").
|
|
||||||
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
|
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
|
||||||
|
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
} else {
|
||||||
|
query.Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)")
|
||||||
|
}
|
||||||
|
|
||||||
var today = timeutil.Format("Y-m-d")
|
var today = timeutil.Format("Y-m-d")
|
||||||
if isAvailable {
|
if isAvailable {
|
||||||
query.Gte("dayTo", today)
|
query.Gte("dayTo", today)
|
||||||
@@ -110,10 +116,11 @@ func (this *UserPlanDAO) CountAllEnabledUserPlans(tx *dbs.Tx, isAvailable bool,
|
|||||||
func (this *UserPlanDAO) ListEnabledUserPlans(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int32, offset int64, size int64) (result []*UserPlan, err error) {
|
func (this *UserPlanDAO) ListEnabledUserPlans(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int32, offset int64, size int64) (result []*UserPlan, err error) {
|
||||||
var query = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
State(UserPlanStateEnabled).
|
State(UserPlanStateEnabled).
|
||||||
Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)").
|
|
||||||
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
|
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
|
||||||
if userId > 0 {
|
if userId > 0 {
|
||||||
query.Attr("userId", userId)
|
query.Attr("userId", userId)
|
||||||
|
} else {
|
||||||
|
query.Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)")
|
||||||
}
|
}
|
||||||
var today = timeutil.Format("Y-m-d")
|
var today = timeutil.Format("Y-m-d")
|
||||||
if isAvailable {
|
if isAvailable {
|
||||||
@@ -137,10 +144,11 @@ func (this *UserPlanDAO) ListEnabledUserPlans(tx *dbs.Tx, userId int64, isAvaila
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateUserPlan 创建套餐
|
// CreateUserPlan 创建套餐
|
||||||
func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64, dayTo string) (int64, error) {
|
func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64, name string, dayTo string) (int64, error) {
|
||||||
var op = NewUserPlanOperator()
|
var op = NewUserPlanOperator()
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.PlanId = planId
|
op.PlanId = planId
|
||||||
|
op.Name = name
|
||||||
op.DayTo = dayTo
|
op.DayTo = dayTo
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = UserStateEnabled
|
op.State = UserStateEnabled
|
||||||
@@ -148,13 +156,14 @@ func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUserPlan 修改套餐
|
// UpdateUserPlan 修改套餐
|
||||||
func (this *UserPlanDAO) UpdateUserPlan(tx *dbs.Tx, userPlanId int64, planId int64, dayTo string, isOn bool) error {
|
func (this *UserPlanDAO) UpdateUserPlan(tx *dbs.Tx, userPlanId int64, planId int64, name string, dayTo string, isOn bool) error {
|
||||||
if userPlanId <= 0 {
|
if userPlanId <= 0 {
|
||||||
return errors.New("invalid userPlanId")
|
return errors.New("invalid userPlanId")
|
||||||
}
|
}
|
||||||
var op = NewUserPlanOperator()
|
var op = NewUserPlanOperator()
|
||||||
op.Id = userPlanId
|
op.Id = userPlanId
|
||||||
op.PlanId = planId
|
op.PlanId = planId
|
||||||
|
op.Name = name
|
||||||
op.DayTo = dayTo
|
op.DayTo = dayTo
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -198,6 +207,22 @@ func (this *UserPlanDAO) FindAllEnabledPlansForServer(tx *dbs.Tx, userId int64,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckUserPlan 检查用户套餐
|
||||||
|
func (this *UserPlanDAO) CheckUserPlan(tx *dbs.Tx, userId int64, userPlanId int64) error {
|
||||||
|
exists, err := this.Query(tx).
|
||||||
|
Pk(userPlanId).
|
||||||
|
Attr("userId", userId).
|
||||||
|
State(UserPlanStateEnabled).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *UserPlanDAO) NotifyUpdate(tx *dbs.Tx, userPlanId int64) error {
|
func (this *UserPlanDAO) NotifyUpdate(tx *dbs.Tx, userPlanId int64) error {
|
||||||
serverId, err := SharedServerDAO.FindEnabledServerIdWithUserPlanId(tx, userPlanId)
|
serverId, err := SharedServerDAO.FindEnabledServerIdWithUserPlanId(tx, userPlanId)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ type UserPlan struct {
|
|||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
PlanId uint32 `field:"planId"` // 套餐ID
|
PlanId uint32 `field:"planId"` // 套餐ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
DayTo string `field:"dayTo"` // 结束日期
|
DayTo string `field:"dayTo"` // 结束日期
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
@@ -15,6 +16,7 @@ type UserPlanOperator struct {
|
|||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
PlanId interface{} // 套餐ID
|
PlanId interface{} // 套餐ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
|
Name interface{} // 名称
|
||||||
DayTo interface{} // 结束日期
|
DayTo interface{} // 结束日期
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import "sync"
|
|||||||
var eventsMap = map[string][]func(){} // event => []callbacks
|
var eventsMap = map[string][]func(){} // event => []callbacks
|
||||||
var locker = sync.Mutex{}
|
var locker = sync.Mutex{}
|
||||||
|
|
||||||
// 增加事件回调
|
// On 增加事件回调
|
||||||
func On(event string, callback func()) {
|
func On(event string, callback func()) {
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
defer locker.Unlock()
|
defer locker.Unlock()
|
||||||
@@ -15,7 +15,7 @@ func On(event string, callback func()) {
|
|||||||
eventsMap[event] = callbacks
|
eventsMap[event] = callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通知事件
|
// Notify 通知事件
|
||||||
func Notify(event string) {
|
func Notify(event string) {
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
callbacks, _ := eventsMap[event]
|
callbacks, _ := eventsMap[event]
|
||||||
|
|||||||
12
internal/goman/instance.go
Normal file
12
internal/goman/instance.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package goman
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Instance struct {
|
||||||
|
Id uint64
|
||||||
|
CreatedTime time.Time
|
||||||
|
File string
|
||||||
|
Line int
|
||||||
|
}
|
||||||
81
internal/goman/lib.go
Normal file
81
internal/goman/lib.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package goman
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var locker = &sync.Mutex{}
|
||||||
|
var instanceMap = map[uint64]*Instance{} // id => *Instance
|
||||||
|
var instanceId = uint64(0)
|
||||||
|
|
||||||
|
// New 新创建goroutine
|
||||||
|
func New(f func()) {
|
||||||
|
_, file, line, _ := runtime.Caller(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
locker.Lock()
|
||||||
|
instanceId++
|
||||||
|
|
||||||
|
var instance = &Instance{
|
||||||
|
Id: instanceId,
|
||||||
|
CreatedTime: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.File = file
|
||||||
|
instance.Line = line
|
||||||
|
|
||||||
|
instanceMap[instanceId] = instance
|
||||||
|
locker.Unlock()
|
||||||
|
|
||||||
|
// run function
|
||||||
|
f()
|
||||||
|
|
||||||
|
locker.Lock()
|
||||||
|
delete(instanceMap, instanceId)
|
||||||
|
locker.Unlock()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWithArgs 创建带有参数的goroutine
|
||||||
|
func NewWithArgs(f func(args ...interface{}), args ...interface{}) {
|
||||||
|
_, file, line, _ := runtime.Caller(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
locker.Lock()
|
||||||
|
instanceId++
|
||||||
|
|
||||||
|
var instance = &Instance{
|
||||||
|
Id: instanceId,
|
||||||
|
CreatedTime: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.File = file
|
||||||
|
instance.Line = line
|
||||||
|
|
||||||
|
instanceMap[instanceId] = instance
|
||||||
|
locker.Unlock()
|
||||||
|
|
||||||
|
// run function
|
||||||
|
f(args...)
|
||||||
|
|
||||||
|
locker.Lock()
|
||||||
|
delete(instanceMap, instanceId)
|
||||||
|
locker.Unlock()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// List 列出所有正在运行goroutine
|
||||||
|
func List() []*Instance {
|
||||||
|
locker.Lock()
|
||||||
|
defer locker.Unlock()
|
||||||
|
|
||||||
|
var result = []*Instance{}
|
||||||
|
for _, instance := range instanceMap {
|
||||||
|
result = append(result, instance)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
28
internal/goman/lib_test.go
Normal file
28
internal/goman/lib_test.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package goman
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
New(func() {
|
||||||
|
t.Log("Hello")
|
||||||
|
|
||||||
|
t.Log(List())
|
||||||
|
})
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
t.Log(List())
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewWithArgs(t *testing.T) {
|
||||||
|
NewWithArgs(func(args ...interface{}) {
|
||||||
|
t.Log(args[0], args[1])
|
||||||
|
}, 1, 2)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
@@ -8,4 +8,5 @@ type Credentials struct {
|
|||||||
PrivateKey string
|
PrivateKey string
|
||||||
Passphrase string
|
Passphrase string
|
||||||
Method string
|
Method string
|
||||||
|
Sudo bool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,7 +94,13 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if credentials.Sudo {
|
||||||
|
client.Sudo(credentials.Password)
|
||||||
|
}
|
||||||
|
|
||||||
this.client = client
|
this.client = client
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,6 +152,10 @@ func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.Node
|
|||||||
return env, err
|
return env, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(uname) == 0 {
|
||||||
|
return nil, errors.New("unable to execute 'uname -a' on this system")
|
||||||
|
}
|
||||||
|
|
||||||
osName := ""
|
osName := ""
|
||||||
archName := ""
|
archName := ""
|
||||||
if strings.Contains(uname, "Darwin") {
|
if strings.Contains(uname, "Darwin") {
|
||||||
|
|||||||
@@ -97,6 +97,12 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
|
|||||||
// 修改配置文件
|
// 修改配置文件
|
||||||
{
|
{
|
||||||
configFile := dir + "/edge-node/configs/api.yaml"
|
configFile := dir + "/edge-node/configs/api.yaml"
|
||||||
|
|
||||||
|
// sudo之后我们需要修改配置目录才能写入文件
|
||||||
|
if this.client.sudo {
|
||||||
|
_, _, _ = this.client.Exec("chown " + this.client.User() + " " + filepath.Dir(configFile))
|
||||||
|
}
|
||||||
|
|
||||||
var data = []byte(`rpc:
|
var data = []byte(`rpc:
|
||||||
endpoints: [ ${endpoints} ]
|
endpoints: [ ${endpoints} ]
|
||||||
nodeId: "${nodeId}"
|
nodeId: "${nodeId}"
|
||||||
@@ -108,7 +114,7 @@ secret: "${nodeSecret}"`)
|
|||||||
|
|
||||||
_, err = this.client.WriteFile(configFile, data)
|
_, err = this.client.WriteFile(configFile, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("write 'configs/api.yaml': " + err.Error())
|
return errors.New("write '" + configFile + "': " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,12 @@ func (this *NSNodeInstaller) Install(dir string, params interface{}, installStat
|
|||||||
// 修改配置文件
|
// 修改配置文件
|
||||||
{
|
{
|
||||||
configFile := dir + "/edge-dns/configs/api.yaml"
|
configFile := dir + "/edge-dns/configs/api.yaml"
|
||||||
|
|
||||||
|
// sudo之后我们需要修改配置目录才能写入文件
|
||||||
|
if this.client.sudo {
|
||||||
|
_, _, _ = this.client.Exec("chown " + this.client.User() + " " + filepath.Dir(configFile))
|
||||||
|
}
|
||||||
|
|
||||||
var data = []byte(`rpc:
|
var data = []byte(`rpc:
|
||||||
endpoints: [ ${endpoints} ]
|
endpoints: [ ${endpoints} ]
|
||||||
nodeId: "${nodeId}"
|
nodeId: "${nodeId}"
|
||||||
@@ -108,7 +114,7 @@ secret: "${nodeSecret}"`)
|
|||||||
|
|
||||||
_, err = this.client.WriteFile(configFile, data)
|
_, err = this.client.WriteFile(configFile, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("write 'configs/api.yaml': " + err.Error())
|
return errors.New("write '" + configFile + "': " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
@@ -37,7 +38,7 @@ func (this *NodeQueue) InstallNodeProcess(nodeId int64, isUpgrading bool) error
|
|||||||
|
|
||||||
// 更新时间
|
// 更新时间
|
||||||
ticker := utils.NewTicker(3 * time.Second)
|
ticker := utils.NewTicker(3 * time.Second)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for ticker.Wait() {
|
for ticker.Wait() {
|
||||||
installStatus.UpdatedAt = time.Now().Unix()
|
installStatus.UpdatedAt = time.Now().Unix()
|
||||||
err := models.SharedNodeDAO.UpdateNodeInstallStatus(nil, nodeId, installStatus)
|
err := models.SharedNodeDAO.UpdateNodeInstallStatus(nil, nodeId, installStatus)
|
||||||
@@ -46,7 +47,7 @@ func (this *NodeQueue) InstallNodeProcess(nodeId int64, isUpgrading bool) error
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
defer func() {
|
defer func() {
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
}()
|
}()
|
||||||
@@ -133,24 +134,6 @@ func (this *NodeQueue) InstallNode(nodeId int64, installStatus *models.NodeInsta
|
|||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安装目录
|
|
||||||
installDir := node.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
clusterId := node.ClusterId
|
|
||||||
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(nil, int64(clusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cluster == nil {
|
|
||||||
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
|
||||||
}
|
|
||||||
installDir = cluster.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
// 默认是 $登录用户/edge-node
|
|
||||||
installDir = "/" + grant.Username + "/edge-node"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// API终端
|
// API终端
|
||||||
apiNodes, err := models.SharedAPINodeDAO.FindAllEnabledAndOnAPINodes(nil)
|
apiNodes, err := models.SharedAPINodeDAO.FindAllEnabledAndOnAPINodes(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -187,6 +170,7 @@ func (this *NodeQueue) InstallNode(nodeId int64, installStatus *models.NodeInsta
|
|||||||
PrivateKey: grant.PrivateKey,
|
PrivateKey: grant.PrivateKey,
|
||||||
Passphrase: grant.Passphrase,
|
Passphrase: grant.Passphrase,
|
||||||
Method: grant.Method,
|
Method: grant.Method,
|
||||||
|
Sudo: grant.Su == 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
installStatus.ErrorCode = "SSH_LOGIN_FAILED"
|
installStatus.ErrorCode = "SSH_LOGIN_FAILED"
|
||||||
@@ -196,6 +180,24 @@ func (this *NodeQueue) InstallNode(nodeId int64, installStatus *models.NodeInsta
|
|||||||
_ = installer.Close()
|
_ = installer.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// 安装目录
|
||||||
|
installDir := node.InstallDir
|
||||||
|
if len(installDir) == 0 {
|
||||||
|
clusterId := node.ClusterId
|
||||||
|
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(nil, int64(clusterId))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if cluster == nil {
|
||||||
|
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
||||||
|
}
|
||||||
|
installDir = cluster.InstallDir
|
||||||
|
if len(installDir) == 0 {
|
||||||
|
// 默认是 $登录用户/edge-node
|
||||||
|
installDir = installer.client.UserHome() + "/edge-node"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = installer.Install(installDir, params, installStatus)
|
err = installer.Install(installDir, params, installStatus)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -250,24 +252,6 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
|
|||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安装目录
|
|
||||||
installDir := node.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
clusterId := node.ClusterId
|
|
||||||
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(nil, int64(clusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cluster == nil {
|
|
||||||
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
|
||||||
}
|
|
||||||
installDir = cluster.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
// 默认是 $登录用户/edge-node
|
|
||||||
installDir = "/" + grant.Username + "/edge-node"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
installer := &NodeInstaller{}
|
installer := &NodeInstaller{}
|
||||||
err = installer.Login(&Credentials{
|
err = installer.Login(&Credentials{
|
||||||
Host: loginParams.Host,
|
Host: loginParams.Host,
|
||||||
@@ -277,6 +261,7 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
|
|||||||
PrivateKey: grant.PrivateKey,
|
PrivateKey: grant.PrivateKey,
|
||||||
Passphrase: grant.Passphrase,
|
Passphrase: grant.Passphrase,
|
||||||
Method: grant.Method,
|
Method: grant.Method,
|
||||||
|
Sudo: grant.Su == 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -286,16 +271,16 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// 检查命令是否存在
|
// 检查命令是否存在
|
||||||
exeFile := installDir + "/edge-node/bin/edge-node"
|
exe, err := this.lookupNodeExe(node, installer.client)
|
||||||
_, err = installer.client.Stat(exeFile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("edge node is not installed correctly, can not find executable file: " + exeFile)
|
return errors.New("edge node was not installed correctly, can not find executable file")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 我们先尝试Systemd启动
|
// 我们先尝试Systemd启动
|
||||||
_, _, _ = installer.client.Exec("systemctl start edge-node")
|
_, _, _ = installer.client.Exec("systemctl start edge-node")
|
||||||
|
|
||||||
_, stderr, err := installer.client.Exec(exeFile + " start")
|
// 执行start
|
||||||
|
_, stderr, err := installer.client.Exec("sudo " + exe + " start")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("start failed: " + err.Error())
|
return errors.New("start failed: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -356,24 +341,6 @@ func (this *NodeQueue) StopNode(nodeId int64) error {
|
|||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安装目录
|
|
||||||
installDir := node.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
clusterId := node.ClusterId
|
|
||||||
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(nil, int64(clusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cluster == nil {
|
|
||||||
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
|
||||||
}
|
|
||||||
installDir = cluster.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
// 默认是 $登录用户/edge-node
|
|
||||||
installDir = "/" + grant.Username + "/edge-node"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
installer := &NodeInstaller{}
|
installer := &NodeInstaller{}
|
||||||
err = installer.Login(&Credentials{
|
err = installer.Login(&Credentials{
|
||||||
Host: loginParams.Host,
|
Host: loginParams.Host,
|
||||||
@@ -383,6 +350,7 @@ func (this *NodeQueue) StopNode(nodeId int64) error {
|
|||||||
PrivateKey: grant.PrivateKey,
|
PrivateKey: grant.PrivateKey,
|
||||||
Passphrase: grant.Passphrase,
|
Passphrase: grant.Passphrase,
|
||||||
Method: grant.Method,
|
Method: grant.Method,
|
||||||
|
Sudo: grant.Su == 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -392,16 +360,16 @@ func (this *NodeQueue) StopNode(nodeId int64) error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// 检查命令是否存在
|
// 检查命令是否存在
|
||||||
exeFile := installDir + "/edge-node/bin/edge-node"
|
exe, err := this.lookupNodeExe(node, installer.client)
|
||||||
_, err = installer.client.Stat(exeFile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("edge node is not installed correctly, can not find executable file: " + exeFile)
|
return errors.New("edge node was not installed correctly, can not find executable file")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 我们先尝试Systemd停止
|
// 我们先尝试Systemd停止
|
||||||
_, _, _ = installer.client.Exec("systemctl stop edge-node")
|
_, _, _ = installer.client.Exec("systemctl stop edge-node")
|
||||||
|
|
||||||
_, stderr, err := installer.client.Exec(exeFile + " stop")
|
// 执行stop
|
||||||
|
_, stderr, err := installer.client.Exec(exe + " stop")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("stop failed: " + err.Error())
|
return errors.New("stop failed: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -411,3 +379,38 @@ func (this *NodeQueue) StopNode(nodeId int64) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *NodeQueue) lookupNodeExe(node *models.Node, client *SSHClient) (string, error) {
|
||||||
|
// 安装目录
|
||||||
|
var nodeDirs = []string{}
|
||||||
|
if len(node.InstallDir) > 0 {
|
||||||
|
nodeDirs = append(nodeDirs, node.InstallDir)
|
||||||
|
}
|
||||||
|
clusterId := node.ClusterId
|
||||||
|
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(nil, int64(clusterId))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if cluster == nil {
|
||||||
|
return "", errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
||||||
|
}
|
||||||
|
if len(cluster.InstallDir) > 0 {
|
||||||
|
nodeDirs = append(nodeDirs, cluster.InstallDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认是 $登录用户/edge-node
|
||||||
|
nodeDirs = append(nodeDirs, client.UserHome()+"/edge-node")
|
||||||
|
|
||||||
|
// edge-boot安装目录
|
||||||
|
nodeDirs = append(nodeDirs, "/usr/local/goedge")
|
||||||
|
|
||||||
|
for _, dir := range nodeDirs {
|
||||||
|
var path = dir + "/edge-node/bin/edge-node"
|
||||||
|
_, err := client.sftp.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return path, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestQueue_InstallNode(t *testing.T) {
|
func TestQueue_InstallNode(t *testing.T) {
|
||||||
queue := NewQueue()
|
queue := NewNodeQueue()
|
||||||
err := queue.InstallNodeProcess(16, false)
|
err := queue.InstallNodeProcess(16, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
@@ -37,7 +38,7 @@ func (this *NSNodeQueue) InstallNodeProcess(nodeId int64, isUpgrading bool) erro
|
|||||||
|
|
||||||
// 更新时间
|
// 更新时间
|
||||||
ticker := utils.NewTicker(3 * time.Second)
|
ticker := utils.NewTicker(3 * time.Second)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for ticker.Wait() {
|
for ticker.Wait() {
|
||||||
installStatus.UpdatedAt = time.Now().Unix()
|
installStatus.UpdatedAt = time.Now().Unix()
|
||||||
err := models.SharedNSNodeDAO.UpdateNodeInstallStatus(nil, nodeId, installStatus)
|
err := models.SharedNSNodeDAO.UpdateNodeInstallStatus(nil, nodeId, installStatus)
|
||||||
@@ -46,7 +47,7 @@ func (this *NSNodeQueue) InstallNodeProcess(nodeId int64, isUpgrading bool) erro
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
defer func() {
|
defer func() {
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
}()
|
}()
|
||||||
@@ -133,24 +134,6 @@ func (this *NSNodeQueue) InstallNode(nodeId int64, installStatus *models.NodeIns
|
|||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安装目录
|
|
||||||
installDir := node.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
clusterId := node.ClusterId
|
|
||||||
cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(nil, int64(clusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cluster == nil {
|
|
||||||
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
|
||||||
}
|
|
||||||
installDir = cluster.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
// 默认是 $登录用户/edge-dns
|
|
||||||
installDir = "/" + grant.Username + "/edge-dns"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// API终端
|
// API终端
|
||||||
apiNodes, err := models.SharedAPINodeDAO.FindAllEnabledAndOnAPINodes(nil)
|
apiNodes, err := models.SharedAPINodeDAO.FindAllEnabledAndOnAPINodes(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -187,6 +170,7 @@ func (this *NSNodeQueue) InstallNode(nodeId int64, installStatus *models.NodeIns
|
|||||||
PrivateKey: grant.PrivateKey,
|
PrivateKey: grant.PrivateKey,
|
||||||
Passphrase: grant.Passphrase,
|
Passphrase: grant.Passphrase,
|
||||||
Method: grant.Method,
|
Method: grant.Method,
|
||||||
|
Sudo: grant.Su == 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
installStatus.ErrorCode = "SSH_LOGIN_FAILED"
|
installStatus.ErrorCode = "SSH_LOGIN_FAILED"
|
||||||
@@ -196,6 +180,24 @@ func (this *NSNodeQueue) InstallNode(nodeId int64, installStatus *models.NodeIns
|
|||||||
_ = installer.Close()
|
_ = installer.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// 安装目录
|
||||||
|
installDir := node.InstallDir
|
||||||
|
if len(installDir) == 0 {
|
||||||
|
clusterId := node.ClusterId
|
||||||
|
cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(nil, int64(clusterId))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if cluster == nil {
|
||||||
|
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
||||||
|
}
|
||||||
|
installDir = cluster.InstallDir
|
||||||
|
if len(installDir) == 0 {
|
||||||
|
// 默认是 $登录用户/edge-dns
|
||||||
|
installDir = installer.client.UserHome() + "/edge-dns"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = installer.Install(installDir, params, installStatus)
|
err = installer.Install(installDir, params, installStatus)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -250,6 +252,24 @@ func (this *NSNodeQueue) StartNode(nodeId int64) error {
|
|||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
installer := &NSNodeInstaller{}
|
||||||
|
err = installer.Login(&Credentials{
|
||||||
|
Host: loginParams.Host,
|
||||||
|
Port: loginParams.Port,
|
||||||
|
Username: grant.Username,
|
||||||
|
Password: grant.Password,
|
||||||
|
PrivateKey: grant.PrivateKey,
|
||||||
|
Passphrase: grant.Passphrase,
|
||||||
|
Method: grant.Method,
|
||||||
|
Sudo: grant.Su == 1,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = installer.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
// 安装目录
|
// 安装目录
|
||||||
installDir := node.InstallDir
|
installDir := node.InstallDir
|
||||||
if len(installDir) == 0 {
|
if len(installDir) == 0 {
|
||||||
@@ -264,27 +284,10 @@ func (this *NSNodeQueue) StartNode(nodeId int64) error {
|
|||||||
installDir = cluster.InstallDir
|
installDir = cluster.InstallDir
|
||||||
if len(installDir) == 0 {
|
if len(installDir) == 0 {
|
||||||
// 默认是 $登录用户/edge-dns
|
// 默认是 $登录用户/edge-dns
|
||||||
installDir = "/" + grant.Username + "/edge-dns"
|
installDir = installer.client.UserHome() + "/edge-dns"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
installer := &NSNodeInstaller{}
|
|
||||||
err = installer.Login(&Credentials{
|
|
||||||
Host: loginParams.Host,
|
|
||||||
Port: loginParams.Port,
|
|
||||||
Username: grant.Username,
|
|
||||||
Password: grant.Password,
|
|
||||||
PrivateKey: grant.PrivateKey,
|
|
||||||
Passphrase: grant.Passphrase,
|
|
||||||
Method: grant.Method,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = installer.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 检查命令是否存在
|
// 检查命令是否存在
|
||||||
exeFile := installDir + "/edge-dns/bin/edge-dns"
|
exeFile := installDir + "/edge-dns/bin/edge-dns"
|
||||||
_, err = installer.client.Stat(exeFile)
|
_, err = installer.client.Stat(exeFile)
|
||||||
@@ -356,6 +359,24 @@ func (this *NSNodeQueue) StopNode(nodeId int64) error {
|
|||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
installer := &NSNodeInstaller{}
|
||||||
|
err = installer.Login(&Credentials{
|
||||||
|
Host: loginParams.Host,
|
||||||
|
Port: loginParams.Port,
|
||||||
|
Username: grant.Username,
|
||||||
|
Password: grant.Password,
|
||||||
|
PrivateKey: grant.PrivateKey,
|
||||||
|
Passphrase: grant.Passphrase,
|
||||||
|
Method: grant.Method,
|
||||||
|
Sudo: grant.Su == 1,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = installer.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
// 安装目录
|
// 安装目录
|
||||||
installDir := node.InstallDir
|
installDir := node.InstallDir
|
||||||
if len(installDir) == 0 {
|
if len(installDir) == 0 {
|
||||||
@@ -370,27 +391,10 @@ func (this *NSNodeQueue) StopNode(nodeId int64) error {
|
|||||||
installDir = cluster.InstallDir
|
installDir = cluster.InstallDir
|
||||||
if len(installDir) == 0 {
|
if len(installDir) == 0 {
|
||||||
// 默认是 $登录用户/edge-dns
|
// 默认是 $登录用户/edge-dns
|
||||||
installDir = "/" + grant.Username + "/edge-dns"
|
installDir = installer.client.UserHome() + "/edge-dns"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
installer := &NSNodeInstaller{}
|
|
||||||
err = installer.Login(&Credentials{
|
|
||||||
Host: loginParams.Host,
|
|
||||||
Port: loginParams.Port,
|
|
||||||
Username: grant.Username,
|
|
||||||
Password: grant.Password,
|
|
||||||
PrivateKey: grant.PrivateKey,
|
|
||||||
Passphrase: grant.Passphrase,
|
|
||||||
Method: grant.Method,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = installer.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 检查命令是否存在
|
// 检查命令是否存在
|
||||||
exeFile := installDir + "/edge-dns/bin/edge-dns"
|
exeFile := installDir + "/edge-dns/bin/edge-dns"
|
||||||
_, err = installer.client.Stat(exeFile)
|
_, err = installer.client.Stat(exeFile)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ import (
|
|||||||
type SSHClient struct {
|
type SSHClient struct {
|
||||||
raw *ssh.Client
|
raw *ssh.Client
|
||||||
sftp *sftp.Client
|
sftp *sftp.Client
|
||||||
|
|
||||||
|
sudo bool
|
||||||
|
sudoPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSSHClient(raw *ssh.Client) (*SSHClient, error) {
|
func NewSSHClient(raw *ssh.Client) (*SSHClient, error) {
|
||||||
@@ -30,8 +33,18 @@ func NewSSHClient(raw *ssh.Client) (*SSHClient, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sudo 设置使用Sudo
|
||||||
|
func (this *SSHClient) Sudo(password string) {
|
||||||
|
this.sudo = true
|
||||||
|
this.sudoPassword = password
|
||||||
|
}
|
||||||
|
|
||||||
// Exec 执行shell命令
|
// Exec 执行shell命令
|
||||||
func (this *SSHClient) Exec(cmd string) (stdout string, stderr string, err error) {
|
func (this *SSHClient) Exec(cmd string) (stdout string, stderr string, err error) {
|
||||||
|
if this.raw.User() != "root" && this.sudo {
|
||||||
|
return this.execSudo(cmd, this.sudoPassword)
|
||||||
|
}
|
||||||
|
|
||||||
session, err := this.raw.NewSession()
|
session, err := this.raw.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
@@ -40,8 +53,8 @@ func (this *SSHClient) Exec(cmd string) (stdout string, stderr string, err error
|
|||||||
_ = session.Close()
|
_ = session.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
stdoutBuf := bytes.NewBuffer([]byte{})
|
var stdoutBuf = &bytes.Buffer{}
|
||||||
stderrBuf := bytes.NewBuffer([]byte{})
|
var stderrBuf = &bytes.Buffer{}
|
||||||
session.Stdout = stdoutBuf
|
session.Stdout = stdoutBuf
|
||||||
session.Stderr = stderrBuf
|
session.Stderr = stderrBuf
|
||||||
err = session.Run(cmd)
|
err = session.Run(cmd)
|
||||||
@@ -51,6 +64,79 @@ func (this *SSHClient) Exec(cmd string) (stdout string, stderr string, err error
|
|||||||
return strings.TrimRight(stdoutBuf.String(), "\n"), stderrBuf.String(), nil
|
return strings.TrimRight(stdoutBuf.String(), "\n"), stderrBuf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// execSudo 使用sudo执行shell命令
|
||||||
|
func (this *SSHClient) execSudo(cmd string, password string) (stdout string, stderr string, err error) {
|
||||||
|
session, err := this.raw.NewSession()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = session.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
modes := ssh.TerminalModes{
|
||||||
|
ssh.ECHO: 0, // disable echo
|
||||||
|
ssh.TTY_OP_ISPEED: 14400,
|
||||||
|
ssh.TTY_OP_OSPEED: 14400,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = session.RequestPty("xterm", 80, 40, modes)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var stderrBuf = &bytes.Buffer{}
|
||||||
|
session.Stderr = stderrBuf
|
||||||
|
|
||||||
|
pipeIn, err := session.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
pipeOut, err := session.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var resultErr error
|
||||||
|
var stdoutBuf = bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
var buf = make([]byte, 512)
|
||||||
|
for {
|
||||||
|
n, err := pipeOut.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
if strings.Contains(string(buf[:n]), "[sudo] password for") {
|
||||||
|
_, err = pipeIn.Write([]byte(password + "\n"))
|
||||||
|
if err != nil {
|
||||||
|
resultErr = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
stdoutBuf.Write(buf[:n])
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = session.Run("sudo " + cmd)
|
||||||
|
|
||||||
|
stdout = strings.TrimSpace(stdoutBuf.String())
|
||||||
|
stderr = strings.TrimSpace(stderrBuf.String())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return stdout, stderr, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resultErr != nil {
|
||||||
|
return stdout, stderr, resultErr
|
||||||
|
}
|
||||||
|
return stdout, stderr, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (this *SSHClient) Listen(network string, addr string) (net.Listener, error) {
|
func (this *SSHClient) Listen(network string, addr string) (net.Listener, error) {
|
||||||
return this.raw.Listen(network, addr)
|
return this.raw.Listen(network, addr)
|
||||||
}
|
}
|
||||||
@@ -152,3 +238,31 @@ func (this *SSHClient) WriteFile(path string, data []byte) (n int, err error) {
|
|||||||
func (this *SSHClient) Remove(path string) error {
|
func (this *SSHClient) Remove(path string) error {
|
||||||
return this.sftp.Remove(path)
|
return this.sftp.Remove(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User 用户名
|
||||||
|
func (this *SSHClient) User() string {
|
||||||
|
return this.raw.User()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserHome 用户地址
|
||||||
|
func (this *SSHClient) UserHome() string {
|
||||||
|
homeStdout, _, err := this.Exec("echo $HOME")
|
||||||
|
if err != nil {
|
||||||
|
return this.defaultUserHome()
|
||||||
|
}
|
||||||
|
|
||||||
|
var home = strings.TrimSpace(homeStdout)
|
||||||
|
if len(home) > 0 {
|
||||||
|
return home
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.defaultUserHome()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SSHClient) defaultUserHome() string {
|
||||||
|
var user = this.raw.User()
|
||||||
|
if user == "root" {
|
||||||
|
return "/root"
|
||||||
|
}
|
||||||
|
return "/home/" + user
|
||||||
|
}
|
||||||
|
|||||||
82
internal/installers/ssh_client_test.go
Normal file
82
internal/installers/ssh_client_test.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package installers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testSSHClient(t *testing.T, username string, password string) *SSHClient {
|
||||||
|
methods := []ssh.AuthMethod{}
|
||||||
|
{
|
||||||
|
authMethod := ssh.Password(password)
|
||||||
|
methods = append(methods, authMethod)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
authMethod := ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
|
||||||
|
if len(questions) == 0 {
|
||||||
|
return []string{}, nil
|
||||||
|
}
|
||||||
|
return []string{password}, nil
|
||||||
|
})
|
||||||
|
methods = append(methods, authMethod)
|
||||||
|
}
|
||||||
|
|
||||||
|
config := &ssh.ClientConfig{
|
||||||
|
User: username,
|
||||||
|
Auth: methods,
|
||||||
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
sshClient, err := ssh.Dial("tcp", "192.168.2.31:22", config)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := NewSSHClient(sshClient)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSSHClient_Home(t *testing.T) {
|
||||||
|
var client = testSSHClient(t, "root", "123456")
|
||||||
|
t.Log(client.UserHome())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSSHClient_Exec(t *testing.T) {
|
||||||
|
var client = testSSHClient(t, "liuxiangchao", "123456")
|
||||||
|
stdout, stderr, err := client.Exec("echo 'Hello'")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("stdout:", stdout, "stderr:", stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSSHClient_SudoExec(t *testing.T) {
|
||||||
|
var client = testSSHClient(t, "liuxiangchao", "123456")
|
||||||
|
client.Sudo("123456")
|
||||||
|
stdout, stderr, err := client.Exec("echo 'Hello'")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("stdout:", stdout, "stderr:", stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSSHClient_SudoExec2(t *testing.T) {
|
||||||
|
var client = testSSHClient(t, "liuxiangchao", "123456")
|
||||||
|
client.Sudo("123456")
|
||||||
|
stdout, stderr, err := client.Exec("/home/liuxiangchao/edge-node/edge-node/bin/edge-node start")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("stdout:", stdout, "stderr:", stderr)
|
||||||
|
}
|
||||||
130
internal/iplibrary/ip2Region.go
Normal file
130
internal/iplibrary/ip2Region.go
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
// 源码改自:https://github.com/lionsoul2014/ip2region/blob/master/binding/golang/ip2region/ip2Region.go
|
||||||
|
|
||||||
|
package iplibrary
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
IndexBlockLength = 12
|
||||||
|
)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
type IP2Region struct {
|
||||||
|
headerSip []int64
|
||||||
|
headerPtr []int64
|
||||||
|
headerLen int64
|
||||||
|
|
||||||
|
// super block index info
|
||||||
|
firstIndexPtr int64
|
||||||
|
lastIndexPtr int64
|
||||||
|
totalBlocks int64
|
||||||
|
|
||||||
|
dbData []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type IpInfo struct {
|
||||||
|
CityId int64
|
||||||
|
Country string
|
||||||
|
Region string
|
||||||
|
Province string
|
||||||
|
City string
|
||||||
|
ISP string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ip IpInfo) String() string {
|
||||||
|
return strconv.FormatInt(ip.CityId, 10) + "|" + ip.Country + "|" + ip.Region + "|" + ip.Province + "|" + ip.City + "|" + ip.ISP
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIpInfo(cityId int64, line []byte) *IpInfo {
|
||||||
|
lineSlice := strings.Split(string(line), "|")
|
||||||
|
ipInfo := &IpInfo{}
|
||||||
|
length := len(lineSlice)
|
||||||
|
ipInfo.CityId = cityId
|
||||||
|
if length < 5 {
|
||||||
|
for i := 0; i <= 5-length; i++ {
|
||||||
|
lineSlice = append(lineSlice, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ipInfo.Country = lineSlice[0]
|
||||||
|
ipInfo.Region = lineSlice[1]
|
||||||
|
ipInfo.Province = lineSlice[2]
|
||||||
|
ipInfo.City = lineSlice[3]
|
||||||
|
ipInfo.ISP = lineSlice[4]
|
||||||
|
return ipInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIP2Region(path string) (*IP2Region, error) {
|
||||||
|
var region = &IP2Region{}
|
||||||
|
region.dbData, err = ioutil.ReadFile(path)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
region.firstIndexPtr = region.ipLongAtOffset(0)
|
||||||
|
region.lastIndexPtr = region.ipLongAtOffset(4)
|
||||||
|
region.totalBlocks = (region.lastIndexPtr-region.firstIndexPtr)/IndexBlockLength + 1
|
||||||
|
return region, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IP2Region) MemorySearch(ipStr string) (ipInfo *IpInfo, err error) {
|
||||||
|
ip, err := ip2long(ipStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
h := this.totalBlocks
|
||||||
|
var dataPtr, l int64
|
||||||
|
for l <= h {
|
||||||
|
m := (l + h) >> 1
|
||||||
|
p := this.firstIndexPtr + m*IndexBlockLength
|
||||||
|
sip := this.ipLongAtOffset(p)
|
||||||
|
if ip < sip {
|
||||||
|
h = m - 1
|
||||||
|
} else {
|
||||||
|
eip := this.ipLongAtOffset(p + 4)
|
||||||
|
if ip > eip {
|
||||||
|
l = m + 1
|
||||||
|
} else {
|
||||||
|
dataPtr = this.ipLongAtOffset(p + 8)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if dataPtr == 0 {
|
||||||
|
return nil, errors.New("not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
dataLen := (dataPtr >> 24) & 0xFF
|
||||||
|
dataPtr = dataPtr & 0x00FFFFFF
|
||||||
|
return getIpInfo(this.ipLongAtOffset(dataPtr), this.dbData[(dataPtr)+4:dataPtr+dataLen]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IP2Region) ipLongAtOffset(offset int64) int64 {
|
||||||
|
return int64(this.dbData[offset]) |
|
||||||
|
int64(this.dbData[offset+1])<<8 |
|
||||||
|
int64(this.dbData[offset+2])<<16 |
|
||||||
|
int64(this.dbData[offset+3])<<24
|
||||||
|
}
|
||||||
|
|
||||||
|
func ip2long(IpStr string) (int64, error) {
|
||||||
|
bits := strings.Split(IpStr, ".")
|
||||||
|
if len(bits) != 4 {
|
||||||
|
return 0, errors.New("ip format error")
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum int64
|
||||||
|
for i, n := range bits {
|
||||||
|
bit, _ := strconv.ParseInt(n, 10, 64)
|
||||||
|
sum += bit << uint(24-8*i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum, nil
|
||||||
|
}
|
||||||
@@ -4,17 +4,16 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
|
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IP2RegionLibrary struct {
|
type IP2RegionLibrary struct {
|
||||||
db *ip2region.Ip2Region
|
db *IP2Region
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IP2RegionLibrary) Load(dbPath string) error {
|
func (this *IP2RegionLibrary) Load(dbPath string) error {
|
||||||
db, err := ip2region.New(dbPath)
|
db, err := NewIP2Region(dbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -77,6 +76,6 @@ func (this *IP2RegionLibrary) Lookup(ip string) (*Result, error) {
|
|||||||
|
|
||||||
func (this *IP2RegionLibrary) Close() {
|
func (this *IP2RegionLibrary) Close() {
|
||||||
if this.db != nil {
|
if this.db != nil {
|
||||||
this.db.Close()
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -19,27 +20,27 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// IP库更新程序
|
// Updater IP库更新程序
|
||||||
type Updater struct {
|
type Updater struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取新对象
|
// NewUpdater 获取新对象
|
||||||
func NewUpdater() *Updater {
|
func NewUpdater() *Updater {
|
||||||
return &Updater{}
|
return &Updater{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 开始更新
|
// Start 开始更新
|
||||||
func (this *Updater) Start() {
|
func (this *Updater) Start() {
|
||||||
// 这里不需要太频繁检查更新,因为通常不需要更新IP库
|
// 这里不需要太频繁检查更新,因为通常不需要更新IP库
|
||||||
ticker := time.NewTicker(1 * time.Hour)
|
ticker := time.NewTicker(1 * time.Hour)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := this.loop()
|
err := this.loop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[IP_LIBRARY]" + err.Error())
|
logs.Println("[IP_LIBRARY]" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 单次任务
|
// 单次任务
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/events"
|
"github.com/TeaOSLab/EdgeAPI/internal/events"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
@@ -29,6 +30,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -41,6 +44,8 @@ type APINode struct {
|
|||||||
serviceInstanceLocker sync.Mutex
|
serviceInstanceLocker sync.Mutex
|
||||||
|
|
||||||
sock *gosock.Sock
|
sock *gosock.Sock
|
||||||
|
|
||||||
|
isStarting bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAPINode() *APINode {
|
func NewAPINode() *APINode {
|
||||||
@@ -51,6 +56,8 @@ func NewAPINode() *APINode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *APINode) Start() {
|
func (this *APINode) Start() {
|
||||||
|
this.isStarting = true
|
||||||
|
|
||||||
logs.Println("[API_NODE]start api node, pid: " + strconv.Itoa(os.Getpid()))
|
logs.Println("[API_NODE]start api node, pid: " + strconv.Itoa(os.Getpid()))
|
||||||
|
|
||||||
// 检查数据库连接
|
// 检查数据库连接
|
||||||
@@ -61,6 +68,7 @@ func (this *APINode) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 本地Sock
|
// 本地Sock
|
||||||
|
logs.Println("[API_NODE]listening sock ...")
|
||||||
err = this.listenSock()
|
err = this.listenSock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[API_NODE]" + err.Error())
|
logs.Println("[API_NODE]" + err.Error())
|
||||||
@@ -68,6 +76,7 @@ func (this *APINode) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 自动升级
|
// 自动升级
|
||||||
|
logs.Println("[API_NODE]auto upgrading ...")
|
||||||
err = this.autoUpgrade()
|
err = this.autoUpgrade()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[API_NODE]auto upgrade failed: " + err.Error())
|
logs.Println("[API_NODE]auto upgrade failed: " + err.Error())
|
||||||
@@ -75,6 +84,7 @@ func (this *APINode) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 自动设置数据库
|
// 自动设置数据库
|
||||||
|
logs.Println("[API_NODE]setup database ...")
|
||||||
err = this.setupDB()
|
err = this.setupDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[API_NODE]setup database '" + err.Error() + "'")
|
logs.Println("[API_NODE]setup database '" + err.Error() + "'")
|
||||||
@@ -83,9 +93,11 @@ func (this *APINode) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 数据库通知启动
|
// 数据库通知启动
|
||||||
|
logs.Println("[API_NODE]notify ready ...")
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
// 读取配置
|
// 读取配置
|
||||||
|
logs.Println("[API_NODE]reading api config ...")
|
||||||
config, err := configs.SharedAPIConfig()
|
config, err := configs.SharedAPIConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[API_NODE]start failed: " + err.Error())
|
logs.Println("[API_NODE]start failed: " + err.Error())
|
||||||
@@ -109,10 +121,14 @@ func (this *APINode) Start() {
|
|||||||
_ = utils.SetRLimit(1024 * 1024)
|
_ = utils.SetRLimit(1024 * 1024)
|
||||||
|
|
||||||
// 状态变更计时器
|
// 状态变更计时器
|
||||||
go NewNodeStatusExecutor().Listen()
|
goman.New(func() {
|
||||||
|
NewNodeStatusExecutor().Listen()
|
||||||
|
})
|
||||||
|
|
||||||
// 访问日志存储管理器
|
// 访问日志存储管理器
|
||||||
go accesslogs.SharedStorageManager.Start()
|
goman.New(func() {
|
||||||
|
accesslogs.SharedStorageManager.Start()
|
||||||
|
})
|
||||||
|
|
||||||
// 监听RPC服务
|
// 监听RPC服务
|
||||||
remotelogs.Println("API_NODE", "starting RPC server ...")
|
remotelogs.Println("API_NODE", "starting RPC server ...")
|
||||||
@@ -124,6 +140,9 @@ func (this *APINode) Start() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 结束启动
|
||||||
|
this.isStarting = false
|
||||||
|
|
||||||
// 保持进程
|
// 保持进程
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
@@ -195,10 +214,10 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
|
|||||||
var rpcServer *grpc.Server
|
var rpcServer *grpc.Server
|
||||||
if tlsConfig == nil {
|
if tlsConfig == nil {
|
||||||
remotelogs.Println("API_NODE", "listening GRPC http://"+listener.Addr().String()+" ...")
|
remotelogs.Println("API_NODE", "listening GRPC http://"+listener.Addr().String()+" ...")
|
||||||
rpcServer = grpc.NewServer()
|
rpcServer = grpc.NewServer(grpc.MaxRecvMsgSize(128 * 1024 * 1024))
|
||||||
} else {
|
} else {
|
||||||
logs.Println("[API_NODE]listening GRPC https://" + listener.Addr().String() + " ...")
|
logs.Println("[API_NODE]listening GRPC https://" + listener.Addr().String() + " ...")
|
||||||
rpcServer = grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))
|
rpcServer = grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)), grpc.MaxRecvMsgSize(128*1024*1024))
|
||||||
}
|
}
|
||||||
this.registerServices(rpcServer)
|
this.registerServices(rpcServer)
|
||||||
err := rpcServer.Serve(listener)
|
err := rpcServer.Serve(listener)
|
||||||
@@ -343,13 +362,13 @@ func (this *APINode) listenPorts(apiNode *models.APINode) (isListening bool) {
|
|||||||
}
|
}
|
||||||
remotelogs.Println("API_NODE", "retry listening port ':"+port+"' only ok")
|
remotelogs.Println("API_NODE", "retry listening port ':"+port+"' only ok")
|
||||||
}
|
}
|
||||||
go func() {
|
goman.New(func() {
|
||||||
err := this.listenRPC(listener, nil)
|
err := this.listenRPC(listener, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("API_NODE", "listening '"+addr+"' rpc: "+err.Error())
|
remotelogs.Error("API_NODE", "listening '"+addr+"' rpc: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
isListening = true
|
isListening = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -390,7 +409,7 @@ func (this *APINode) listenPorts(apiNode *models.APINode) (isListening bool) {
|
|||||||
}
|
}
|
||||||
remotelogs.Println("API_NODE", "retry listening port ':"+port+"' only ok")
|
remotelogs.Println("API_NODE", "retry listening port ':"+port+"' only ok")
|
||||||
}
|
}
|
||||||
go func() {
|
goman.New(func() {
|
||||||
err := this.listenRPC(listener, &tls.Config{
|
err := this.listenRPC(listener, &tls.Config{
|
||||||
Certificates: certs,
|
Certificates: certs,
|
||||||
})
|
})
|
||||||
@@ -398,7 +417,7 @@ func (this *APINode) listenPorts(apiNode *models.APINode) (isListening bool) {
|
|||||||
remotelogs.Error("API_NODE", "listening '"+addr+"' rpc: "+err.Error())
|
remotelogs.Error("API_NODE", "listening '"+addr+"' rpc: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
isListening = true
|
isListening = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -418,7 +437,7 @@ func (this *APINode) listenPorts(apiNode *models.APINode) (isListening bool) {
|
|||||||
remotelogs.Error("API_NODE", "listening REST 'http://"+addr+"' failed: "+err.Error())
|
remotelogs.Error("API_NODE", "listening REST 'http://"+addr+"' failed: "+err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go func() {
|
goman.New(func() {
|
||||||
remotelogs.Println("API_NODE", "listening REST http://"+addr+" ...")
|
remotelogs.Println("API_NODE", "listening REST http://"+addr+" ...")
|
||||||
server := &RestServer{}
|
server := &RestServer{}
|
||||||
err := server.Listen(listener)
|
err := server.Listen(listener)
|
||||||
@@ -426,7 +445,7 @@ func (this *APINode) listenPorts(apiNode *models.APINode) (isListening bool) {
|
|||||||
remotelogs.Error("API_NODE", "listening REST 'http://"+addr+"' failed: "+err.Error())
|
remotelogs.Error("API_NODE", "listening REST 'http://"+addr+"' failed: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
isListening = true
|
isListening = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -451,7 +470,7 @@ func (this *APINode) listenPorts(apiNode *models.APINode) (isListening bool) {
|
|||||||
remotelogs.Error("API_NODE", "listening REST 'https://"+addr+"' failed: "+err.Error())
|
remotelogs.Error("API_NODE", "listening REST 'https://"+addr+"' failed: "+err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go func() {
|
goman.New(func() {
|
||||||
remotelogs.Println("API_NODE", "listening REST https://"+addr+" ...")
|
remotelogs.Println("API_NODE", "listening REST https://"+addr+" ...")
|
||||||
server := &RestServer{}
|
server := &RestServer{}
|
||||||
|
|
||||||
@@ -467,7 +486,7 @@ func (this *APINode) listenPorts(apiNode *models.APINode) (isListening bool) {
|
|||||||
remotelogs.Error("API_NODE", "listening REST 'https://"+addr+"' failed: "+err.Error())
|
remotelogs.Error("API_NODE", "listening REST 'https://"+addr+"' failed: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
isListening = true
|
isListening = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -489,7 +508,7 @@ func (this *APINode) listenSock() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 启动监听
|
// 启动监听
|
||||||
go func() {
|
goman.New(func() {
|
||||||
this.sock.OnCommand(func(cmd *gosock.Command) {
|
this.sock.OnCommand(func(cmd *gosock.Command) {
|
||||||
switch cmd.Code {
|
switch cmd.Code {
|
||||||
case "pid":
|
case "pid":
|
||||||
@@ -515,6 +534,44 @@ func (this *APINode) listenSock() error {
|
|||||||
// 退出主进程
|
// 退出主进程
|
||||||
events.Notify(events.EventQuit)
|
events.Notify(events.EventQuit)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
case "starting": // 是否正在启动
|
||||||
|
_ = cmd.Reply(&gosock.Command{
|
||||||
|
Code: "starting",
|
||||||
|
Params: map[string]interface{}{
|
||||||
|
"isStarting": this.isStarting,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
case "goman":
|
||||||
|
var posMap = map[string]maps.Map{} // file#line => Map
|
||||||
|
for _, instance := range goman.List() {
|
||||||
|
var pos = instance.File + "#" + types.String(instance.Line)
|
||||||
|
m, ok := posMap[pos]
|
||||||
|
if ok {
|
||||||
|
m["count"] = m["count"].(int) + 1
|
||||||
|
} else {
|
||||||
|
m = maps.Map{
|
||||||
|
"pos": pos,
|
||||||
|
"count": 1,
|
||||||
|
}
|
||||||
|
posMap[pos] = m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []maps.Map{}
|
||||||
|
for _, m := range posMap {
|
||||||
|
result = append(result, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(result, func(i, j int) bool {
|
||||||
|
return result[i]["count"].(int) > result[j]["count"].(int)
|
||||||
|
})
|
||||||
|
|
||||||
|
_ = cmd.Reply(&gosock.Command{
|
||||||
|
Params: map[string]interface{}{
|
||||||
|
"total": runtime.NumGoroutine(),
|
||||||
|
"result": result,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -522,7 +579,7 @@ func (this *APINode) listenSock() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("API_NODE", err.Error())
|
logs.Println("API_NODE", err.Error())
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
|
|
||||||
events.On(events.EventQuit, func() {
|
events.On(events.EventQuit, func() {
|
||||||
logs.Println("API_NODE", "quit unix sock")
|
logs.Println("API_NODE", "quit unix sock")
|
||||||
|
|||||||
@@ -8,5 +8,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DAOInterface interface {
|
type DAOInterface interface {
|
||||||
CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64) error
|
CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64, logType string, paramsJSON []byte) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package remotelogs
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
@@ -15,14 +16,14 @@ var sharedDAO DAOInterface
|
|||||||
func init() {
|
func init() {
|
||||||
// 定期上传日志
|
// 定期上传日志
|
||||||
ticker := time.NewTicker(60 * time.Second)
|
ticker := time.NewTicker(60 * time.Second)
|
||||||
go func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
err := uploadLogs()
|
err := uploadLogs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[LOG]" + err.Error())
|
logs.Println("[LOG]" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Println 打印普通信息
|
// Println 打印普通信息
|
||||||
@@ -109,7 +110,7 @@ Loop:
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case log := <-logChan:
|
case log := <-logChan:
|
||||||
err := sharedDAO.CreateLog(nil, nodeconfigs.NodeRoleAPI, log.NodeId, log.ServerId, log.OriginId, log.Level, log.Tag, log.Description, log.CreatedAt)
|
err := sharedDAO.CreateLog(nil, nodeconfigs.NodeRoleAPI, log.NodeId, log.ServerId, log.OriginId, log.Level, log.Tag, log.Description, log.CreatedAt, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user