Compare commits
133 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b512f3d680 | ||
|
|
d4e829e57c | ||
|
|
78798060e7 | ||
|
|
b2569a8dac | ||
|
|
4bd7ec9871 | ||
|
|
95cfad60c4 | ||
|
|
7f51d451f7 | ||
|
|
a4fb0fd795 | ||
|
|
c02928dd44 | ||
|
|
e70c49d407 | ||
|
|
e1c1984fd4 | ||
|
|
f801d304c6 | ||
|
|
178a38c6d9 | ||
|
|
e356707db7 | ||
|
|
8d3043d0fe | ||
|
|
1e494bd1fd | ||
|
|
b726c8d589 | ||
|
|
e71e80703d | ||
|
|
c9b666e5bc | ||
|
|
874139ea07 | ||
|
|
05d79ad606 | ||
|
|
13c78a5fec | ||
|
|
f3e3824b7d | ||
|
|
67473c2dcf | ||
|
|
3921c547be | ||
|
|
3e0d2fda6a | ||
|
|
7ff6c0c18b | ||
|
|
e76464673a | ||
|
|
1ab849d9b0 | ||
|
|
781c851571 | ||
|
|
0b19d93a47 | ||
|
|
2856f7716b | ||
|
|
c9ba24dc96 | ||
|
|
a660fb1f42 | ||
|
|
8586ad6478 | ||
|
|
0fab6fecfe | ||
|
|
d752bb08c7 | ||
|
|
6f845f36c9 | ||
|
|
66bc60a47c | ||
|
|
1c048da1f0 | ||
|
|
da8aa20f83 | ||
|
|
14315923d8 | ||
|
|
3d3228fe96 | ||
|
|
173ac5a8aa | ||
|
|
e9c5d7e7cf | ||
|
|
194127dce9 | ||
|
|
86cb7e9d41 | ||
|
|
b2774de6a2 | ||
|
|
3a4722b701 | ||
|
|
028aea4e3d | ||
|
|
c3dd97a7c1 | ||
|
|
d527fcdd78 | ||
|
|
d6f311e057 | ||
|
|
991e08fa71 | ||
|
|
06b44dc101 | ||
|
|
2d94b994fa | ||
|
|
c036186dde | ||
|
|
bc2ad13037 | ||
|
|
bfa04856aa | ||
|
|
feb1068441 | ||
|
|
181a4d05b0 | ||
|
|
c449265e05 | ||
|
|
e778616b5c | ||
|
|
dad5be2670 | ||
|
|
414afd17b8 | ||
|
|
86a806bca2 | ||
|
|
faed7420a7 | ||
|
|
ae14ff4f9f | ||
|
|
99e1658fdf | ||
|
|
e79264eefc | ||
|
|
dd0e26e7bc | ||
|
|
342c4bfbc2 | ||
|
|
7d2b8fd4c8 | ||
|
|
6426622992 | ||
|
|
3d154411de | ||
|
|
241f41e900 | ||
|
|
6c3d24d895 | ||
|
|
b2a0204f6b | ||
|
|
a9d71652b7 | ||
|
|
1b6bfb33d6 | ||
|
|
aec0d8d681 | ||
|
|
e38871b52d | ||
|
|
c1b4551dd1 | ||
|
|
d479140f87 | ||
|
|
881bb89ac0 | ||
|
|
0e4158f600 | ||
|
|
57bbd77ae5 | ||
|
|
6906b3094b | ||
|
|
aec28b5087 | ||
|
|
b59ed1f73e | ||
|
|
2a83f61bdd | ||
|
|
4f21d60ca4 | ||
|
|
0a6111b2e5 | ||
|
|
4b425e1698 | ||
|
|
bee7da807b | ||
|
|
a906a7db06 | ||
|
|
ea62cf0ff7 | ||
|
|
72e0c55f5d | ||
|
|
3a88f23181 | ||
|
|
967c9080fb | ||
|
|
f44b9434ad | ||
|
|
e21a3c5f8c | ||
|
|
88dae56b6c | ||
|
|
40c3475306 | ||
|
|
318c8dd566 | ||
|
|
a5f30b1573 | ||
|
|
a8ec959c70 | ||
|
|
c393b2f480 | ||
|
|
548f56f8f0 | ||
|
|
9aa71365b9 | ||
|
|
292fb72a26 | ||
|
|
e5315c3b8d | ||
|
|
a4dd7bb75a | ||
|
|
53ef0f3fb2 | ||
|
|
6af8bff802 | ||
|
|
d849f7440a | ||
|
|
425c0ec44c | ||
|
|
aad0b01581 | ||
|
|
bc2c3dfa0b | ||
|
|
0fe76430c6 | ||
|
|
979ff4c44e | ||
|
|
b0b6b5984f | ||
|
|
5d4da6cccb | ||
|
|
912ffa062f | ||
|
|
1db4661c75 | ||
|
|
f7dd9e3f39 | ||
|
|
0cc74b920e | ||
|
|
51c3807d01 | ||
|
|
c2c42ca2b7 | ||
|
|
2a6db6ebfe | ||
|
|
30d8edbdcf | ||
|
|
177afafe12 | ||
|
|
98765b6e2a |
@@ -5,6 +5,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
||||
@@ -26,10 +27,11 @@ func main() {
|
||||
app.Version(teaconst.Version)
|
||||
app.Product(teaconst.ProductName)
|
||||
app.Usage(teaconst.ProcessName + " [start|stop|restart|setup|upgrade|service|daemon|issues]")
|
||||
|
||||
app.On("setup", func() {
|
||||
var setupCmd = setup.NewSetupFromCmd()
|
||||
err := setupCmd.Run()
|
||||
result := maps.Map{}
|
||||
var result = maps.Map{}
|
||||
if err != nil {
|
||||
result["isOk"] = false
|
||||
result["error"] = err.Error()
|
||||
@@ -71,6 +73,14 @@ func main() {
|
||||
}
|
||||
fmt.Println("done")
|
||||
})
|
||||
app.On("reset", func() {
|
||||
err := configs.ResetAPIConfig()
|
||||
if err != nil {
|
||||
fmt.Println("[ERROR]reset failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println("done")
|
||||
})
|
||||
app.On("goman", func() {
|
||||
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
||||
reply, err := sock.Send(&gosock.Command{Code: "goman"})
|
||||
|
||||
@@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
|
||||
"github.com/iwind/gosock/pkg/gosock"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -51,7 +51,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
unzip := utils.NewUnzip(zipPath, targetPath)
|
||||
unzip := helpers.NewUnzip(zipPath, targetPath)
|
||||
err := unzip.Run()
|
||||
if err != nil {
|
||||
stderr("ERROR: " + err.Error())
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package main
|
||||
|
||||
// 注意这里的依赖文件应该最小化,从而使编译后的文件最小化
|
||||
import (
|
||||
"flag"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
|
||||
"github.com/iwind/gosock/pkg/gosock"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -51,7 +52,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
unzip := utils.NewUnzip(zipPath, targetPath)
|
||||
unzip := helpers.NewUnzip(zipPath, targetPath)
|
||||
err := unzip.Run()
|
||||
if err != nil {
|
||||
stderr("ERROR: " + err.Error())
|
||||
|
||||
@@ -18,7 +18,7 @@ func main() {
|
||||
fmt.Println("[ERROR]" + err.Error())
|
||||
return
|
||||
}
|
||||
results, err := setup.NewSQLDump().Dump(db)
|
||||
results, err := setup.NewSQLDump().Dump(db, true)
|
||||
if err != nil {
|
||||
fmt.Println("[ERROR]" + err.Error())
|
||||
return
|
||||
|
||||
13
go.mod
13
go.mod
@@ -14,11 +14,14 @@ require (
|
||||
github.com/golang/protobuf v1.5.2
|
||||
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e
|
||||
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62
|
||||
github.com/miekg/dns v1.1.43
|
||||
github.com/mozillazg/go-pinyin v0.18.0
|
||||
github.com/pkg/sftp v1.12.0
|
||||
github.com/shirou/gopsutil/v3 v3.22.2
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
|
||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
|
||||
github.com/smartwalle/alipay/v3 v3.1.7
|
||||
golang.org/x/crypto v0.1.0
|
||||
golang.org/x/net v0.1.0
|
||||
golang.org/x/sys v0.1.0
|
||||
google.golang.org/grpc v1.45.0
|
||||
google.golang.org/protobuf v1.27.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
@@ -26,21 +29,21 @@ require (
|
||||
|
||||
require (
|
||||
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
|
||||
github.com/cespare/xxhash v1.1.0 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/kr/fs v0.1.0 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||
github.com/miekg/dns v1.1.43 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/smartwalle/crypto4go v1.0.2 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.9 // indirect
|
||||
github.com/tklauser/numcpus v0.3.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
golang.org/x/text v0.4.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
|
||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
|
||||
|
||||
32
go.sum
32
go.sum
@@ -66,6 +66,7 @@ github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7F
|
||||
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
|
||||
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
|
||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
@@ -236,13 +237,8 @@ github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhK
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/infobloxopen/infoblox-go-client v1.1.1/go.mod h1:BXiw7S2b9qJoM8MS40vfgCNB2NLHGusk1DtO16BD9zI=
|
||||
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||
github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
||||
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570 h1:zqz2FiMMkSHXWO1EsTRJDPTwX9xQ4uuyD5GAE4JGlhM=
|
||||
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
||||
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e h1:cw4b6ecXdXvLd45YSstD8r9ClcnVK4ljZMZCept2aOk=
|
||||
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e/go.mod h1:fi/Pq+/5m2HZoseM+39dMF57ANXRt6w4PkGu3NXPc5s=
|
||||
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-20220505115348-f88412125a62 h1:HJH6RDheAY156DnIfJSD/bEvqyXzsZuE2gzs8PuUjoo=
|
||||
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
||||
@@ -409,6 +405,10 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
|
||||
github.com/smartwalle/alipay/v3 v3.1.7 h1:J4U5slABafKVD/b9gPCZe/3HAPB8Pa2NOYOPcugEJBo=
|
||||
github.com/smartwalle/alipay/v3 v3.1.7/go.mod h1:cZUMCCnsux9YAxA0/f3PWUR+7wckWtE1BqxbVRtGij0=
|
||||
github.com/smartwalle/crypto4go v1.0.2 h1:9DUEOOsPhmp00438L4oBdcL8EZG1zumecft5bWj5phI=
|
||||
github.com/smartwalle/crypto4go v1.0.2/go.mod h1:LQ7vCZIb7BE5+MuMtJBuO8ORkkQ01m4DXDBWPzLbkMY=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/assertions v1.0.1 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w=
|
||||
github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
|
||||
@@ -481,6 +481,7 @@ golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8/go.mod h1:6SG95UA2DQfeDnf
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
@@ -492,8 +493,8 @@ golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPh
|
||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE=
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
|
||||
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
@@ -560,8 +561,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
|
||||
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
|
||||
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
@@ -630,14 +631,12 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
|
||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@@ -646,8 +645,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
@@ -789,7 +788,6 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -5,12 +5,18 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/go-acme/lego/v4/challenge/dns01"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type DNSProvider struct {
|
||||
raw dnsclients.ProviderInterface
|
||||
dnsDomain string
|
||||
|
||||
locker sync.Mutex
|
||||
deletedRecordNames []string
|
||||
}
|
||||
|
||||
func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProvider {
|
||||
@@ -21,6 +27,7 @@ func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProv
|
||||
}
|
||||
|
||||
func (this *DNSProvider) Present(domain, token, keyAuth string) error {
|
||||
_ = os.Setenv("LEGO_DISABLE_CNAME_SUPPORT", "true")
|
||||
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
||||
|
||||
// 设置记录
|
||||
@@ -29,31 +36,38 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
|
||||
return errors.New("invalid fqdn value")
|
||||
}
|
||||
var recordName = fqdn[:index]
|
||||
record, err := this.raw.QueryRecord(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
|
||||
if err != nil {
|
||||
return errors.New("query DNS record failed: " + err.Error())
|
||||
|
||||
// 先删除老的
|
||||
this.locker.Lock()
|
||||
var wasDeleted = lists.ContainsString(this.deletedRecordNames, recordName)
|
||||
this.locker.Unlock()
|
||||
|
||||
if !wasDeleted {
|
||||
records, err := this.raw.QueryRecords(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
|
||||
if err != nil {
|
||||
return errors.New("query DNS record failed: " + err.Error())
|
||||
}
|
||||
for _, record := range records {
|
||||
err = this.raw.DeleteRecord(this.dnsDomain, record)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
this.locker.Lock()
|
||||
this.deletedRecordNames = append(this.deletedRecordNames, recordName)
|
||||
this.locker.Unlock()
|
||||
}
|
||||
if record == nil {
|
||||
err = this.raw.AddRecord(this.dnsDomain, &dnstypes.Record{
|
||||
Id: "",
|
||||
Name: recordName,
|
||||
Type: dnstypes.RecordTypeTXT,
|
||||
Value: value,
|
||||
Route: this.raw.DefaultRoute(),
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("create DNS record failed: " + err.Error())
|
||||
}
|
||||
} else {
|
||||
err = this.raw.UpdateRecord(this.dnsDomain, record, &dnstypes.Record{
|
||||
Name: recordName,
|
||||
Type: dnstypes.RecordTypeTXT,
|
||||
Value: value,
|
||||
Route: this.raw.DefaultRoute(),
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("update DNS record failed: " + err.Error())
|
||||
}
|
||||
|
||||
// 添加新的
|
||||
err := this.raw.AddRecord(this.dnsDomain, &dnstypes.Record{
|
||||
Id: "",
|
||||
Name: recordName,
|
||||
Type: dnstypes.RecordTypeTXT,
|
||||
Value: value,
|
||||
Route: this.raw.DefaultRoute(),
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("create DNS record failed: " + err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -9,30 +9,11 @@ type Provider struct {
|
||||
Code string `json:"code"`
|
||||
Description string `json:"description"`
|
||||
APIURL string `json:"apiURL"`
|
||||
TestAPIURL string `json:"testAPIURL"`
|
||||
RequireEAB bool `json:"requireEAB"`
|
||||
EABDescription string `json:"eabDescription"`
|
||||
}
|
||||
|
||||
func FindAllProviders() []*Provider {
|
||||
return []*Provider{
|
||||
{
|
||||
Name: "Let's Encrypt",
|
||||
Code: DefaultProviderCode,
|
||||
Description: "非盈利组织Let's Encrypt提供的免费证书。",
|
||||
APIURL: "https://acme-v02.api.letsencrypt.org/directory",
|
||||
RequireEAB: false,
|
||||
},
|
||||
{
|
||||
Name: "ZeroSSL",
|
||||
Code: "zerossl",
|
||||
Description: "相关文档 <a href=\"https://zerossl.com/documentation/acme/\" target=\"_blank\">https://zerossl.com/documentation/acme/</a>。",
|
||||
APIURL: "https://acme.zerossl.com/v2/DV90",
|
||||
RequireEAB: true,
|
||||
EABDescription: "在官网<a href=\"https://app.zerossl.com/developer\" target=\"_blank\">[Developer]</a>页面底部点击\"Generate\"按钮生成。",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func FindProviderWithCode(code string) *Provider {
|
||||
for _, provider := range FindAllProviders() {
|
||||
if provider.Code == code {
|
||||
|
||||
24
internal/acme/providers_ext.go
Normal file
24
internal/acme/providers_ext.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package acme
|
||||
|
||||
func FindAllProviders() []*Provider {
|
||||
return []*Provider{
|
||||
{
|
||||
Name: "Let's Encrypt",
|
||||
Code: DefaultProviderCode,
|
||||
Description: "非盈利组织Let's Encrypt提供的免费证书。",
|
||||
APIURL: "https://acme-v02.api.letsencrypt.org/directory",
|
||||
RequireEAB: false,
|
||||
},
|
||||
{
|
||||
Name: "ZeroSSL",
|
||||
Code: "zerossl",
|
||||
Description: "相关文档 <a href=\"https://zerossl.com/documentation/acme/\" target=\"_blank\">https://zerossl.com/documentation/acme/</a>。",
|
||||
APIURL: "https://acme.zerossl.com/v2/DV90",
|
||||
RequireEAB: true,
|
||||
EABDescription: "在官网<a href=\"https://app.zerossl.com/developer\" target=\"_blank\">[Developer]</a>页面底部点击\"Generate\"按钮生成。",
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/go-acme/lego/v4/lego"
|
||||
acmelog "github.com/go-acme/lego/v4/log"
|
||||
"github.com/go-acme/lego/v4/registration"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
@@ -56,7 +57,9 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
|
||||
|
||||
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
||||
if !this.debug {
|
||||
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||
if !Tea.IsTesting() {
|
||||
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||
}
|
||||
}
|
||||
|
||||
if this.task.User == nil {
|
||||
@@ -139,7 +142,9 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
||||
|
||||
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
||||
if !this.debug {
|
||||
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||
if !Tea.IsTesting() {
|
||||
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||
}
|
||||
}
|
||||
|
||||
if this.task.User == nil {
|
||||
|
||||
@@ -132,3 +132,47 @@ func (this *APIConfig) WriteFile(path string) error {
|
||||
|
||||
return os.WriteFile(path, data, 0666)
|
||||
}
|
||||
|
||||
// ResetAPIConfig 重置配置
|
||||
func ResetAPIConfig() error {
|
||||
for _, filename := range []string{"api.yaml", "db.yaml"} {
|
||||
// 重置 configs/api.yaml
|
||||
{
|
||||
var configFile = Tea.ConfigFile(filename)
|
||||
stat, err := os.Stat(configFile)
|
||||
if err == nil && !stat.IsDir() {
|
||||
err = os.Remove(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 重置 ~/.edge-api/api.yaml
|
||||
homeDir, homeErr := os.UserHomeDir()
|
||||
if homeErr == nil {
|
||||
var configFile = homeDir + "/." + teaconst.ProcessName + "/" + filename
|
||||
stat, err := os.Stat(configFile)
|
||||
if err == nil && !stat.IsDir() {
|
||||
err = os.Remove(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 重置 /etc/edge-api/api.yaml
|
||||
{
|
||||
var configFile = "/etc/" + teaconst.ProcessName + "/" + filename
|
||||
stat, err := os.Stat(configFile)
|
||||
if err == nil && !stat.IsDir() {
|
||||
err = os.Remove(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package teaconst
|
||||
|
||||
const (
|
||||
Version = "0.5.4"
|
||||
Version = "0.6.3"
|
||||
|
||||
ProductName = "Edge API"
|
||||
ProcessName = "edge-api"
|
||||
@@ -18,13 +18,8 @@ const (
|
||||
|
||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||
|
||||
NodeVersion = "0.5.4"
|
||||
UserNodeVersion = "0.5.0"
|
||||
DNSNodeVersion = "0.2.8"
|
||||
AuthorityNodeVersion = "0.0.2"
|
||||
MonitorNodeVersion = "0.0.4"
|
||||
ReportNodeVersion = "0.1.1"
|
||||
NodeVersion = "0.6.3"
|
||||
|
||||
// SQLVersion SQL版本号
|
||||
SQLVersion = "2"
|
||||
SQLVersion = "9"
|
||||
)
|
||||
|
||||
@@ -14,6 +14,7 @@ type OrderMethod struct {
|
||||
Secret string `field:"secret"` // 密钥
|
||||
Params dbs.JSON `field:"params"` // 参数
|
||||
ClientType string `field:"clientType"` // 客户端类型
|
||||
QrcodeTitle string `field:"qrcodeTitle"` // 二维码标题
|
||||
Order uint32 `field:"order"` // 排序
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
@@ -29,6 +30,7 @@ type OrderMethodOperator struct {
|
||||
Secret any // 密钥
|
||||
Params any // 参数
|
||||
ClientType any // 客户端类型
|
||||
QrcodeTitle any // 二维码标题
|
||||
Order any // 排序
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type UserAccountDailyStatDAO dbs.DAO
|
||||
|
||||
func NewUserAccountDailyStatDAO() *UserAccountDailyStatDAO {
|
||||
return dbs.NewDAO(&UserAccountDailyStatDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeUserAccountDailyStats",
|
||||
Model: new(UserAccountDailyStat),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*UserAccountDailyStatDAO)
|
||||
}
|
||||
|
||||
var SharedUserAccountDailyStatDAO *UserAccountDailyStatDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedUserAccountDailyStatDAO = NewUserAccountDailyStatDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateDailyStat 更新当天统计数据
|
||||
func (this *UserAccountDailyStatDAO) UpdateDailyStat(tx *dbs.Tx) error {
|
||||
var day = timeutil.Format("Ymd")
|
||||
var month = timeutil.Format("Ym")
|
||||
income, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountIncomeEventTypes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
expense, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountExpenseEventTypes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if expense < 0 {
|
||||
expense = -expense
|
||||
}
|
||||
|
||||
return this.Query(tx).
|
||||
InsertOrUpdateQuickly(maps.Map{
|
||||
"day": day,
|
||||
"month": month,
|
||||
"income": income,
|
||||
"expense": expense,
|
||||
}, maps.Map{
|
||||
"income": income,
|
||||
"expense": expense,
|
||||
})
|
||||
}
|
||||
|
||||
// FindDailyStats 查看按天统计
|
||||
func (this *UserAccountDailyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindMonthlyStats 查看某月统计
|
||||
func (this *UserAccountDailyStatDAO) FindMonthlyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Result("SUM(income) AS income", "SUM(expense) AS expense", "month").
|
||||
Between("day", dayFrom, dayTo).
|
||||
Group("month").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
@@ -1,253 +0,0 @@
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
goman.New(func() {
|
||||
// 自动支付账单任务
|
||||
var ticker = time.NewTicker(12 * time.Hour)
|
||||
for range ticker.C {
|
||||
if SharedUserAccountDAO.Instance != nil {
|
||||
err := SharedUserAccountDAO.Instance.RunTx(func(tx *dbs.Tx) error {
|
||||
return SharedUserAccountDAO.PayBills(tx)
|
||||
})
|
||||
if err != nil {
|
||||
remotelogs.Error("USER_ACCOUNT_DAO", "pay bills task failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type UserAccountDAO dbs.DAO
|
||||
|
||||
func NewUserAccountDAO() *UserAccountDAO {
|
||||
return dbs.NewDAO(&UserAccountDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeUserAccounts",
|
||||
Model: new(UserAccount),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*UserAccountDAO)
|
||||
}
|
||||
|
||||
var SharedUserAccountDAO *UserAccountDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedUserAccountDAO = NewUserAccountDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// FindUserAccountWithUserId 根据用户ID查找用户账户
|
||||
func (this *UserAccountDAO) FindUserAccountWithUserId(tx *dbs.Tx, userId int64) (*UserAccount, error) {
|
||||
if userId <= 0 {
|
||||
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
|
||||
}
|
||||
|
||||
// 用户是否存在
|
||||
user, err := models.SharedUserDAO.FindEnabledUser(tx, userId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user == nil {
|
||||
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
|
||||
}
|
||||
|
||||
account, err := this.Query(tx).
|
||||
Attr("userId", userId).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if account != nil {
|
||||
return account.(*UserAccount), nil
|
||||
}
|
||||
|
||||
var op = NewUserAccountOperator()
|
||||
op.UserId = userId
|
||||
_, err = this.SaveInt64(tx, op)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.FindUserAccountWithUserId(tx, userId)
|
||||
}
|
||||
|
||||
// FindUserAccountWithAccountId 根据ID查找用户账户
|
||||
func (this *UserAccountDAO) FindUserAccountWithAccountId(tx *dbs.Tx, accountId int64) (*UserAccount, error) {
|
||||
one, err := this.Query(tx).
|
||||
Pk(accountId).
|
||||
Find()
|
||||
if one != nil {
|
||||
return one.(*UserAccount), nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// UpdateUserAccount 操作用户账户
|
||||
func (this *UserAccountDAO) UpdateUserAccount(tx *dbs.Tx, accountId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
||||
account, err := this.FindUserAccountWithAccountId(tx, accountId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if account == nil {
|
||||
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
||||
}
|
||||
var userId = int64(account.UserId)
|
||||
var deltaFloat64 = float64(delta)
|
||||
if deltaFloat64 < 0 && account.Total < -deltaFloat64 {
|
||||
return errors.New("not enough account quota to decrease")
|
||||
}
|
||||
|
||||
// 操作账户
|
||||
err = this.Query(tx).
|
||||
Pk(account.Id).
|
||||
Set("total", dbs.SQL("total+:delta")).
|
||||
Param("delta", delta).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成日志
|
||||
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, accountId, delta, 0, eventType, description, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateUserAccountFrozen 操作用户账户冻结余额
|
||||
func (this *UserAccountDAO) UpdateUserAccountFrozen(tx *dbs.Tx, userId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
||||
account, err := this.FindUserAccountWithUserId(tx, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var deltaFloat64 = float64(delta)
|
||||
if deltaFloat64 < 0 && account.TotalFrozen < -deltaFloat64 {
|
||||
return errors.New("not enough account frozen quota to decrease")
|
||||
}
|
||||
|
||||
// 操作账户
|
||||
err = this.Query(tx).
|
||||
Pk(account.Id).
|
||||
Set("totalFrozen", dbs.SQL("total+:delta")).
|
||||
Param("delta", delta).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成日志
|
||||
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, int64(account.Id), 0, delta, eventType, description, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountAllAccounts 计算所有账户数量
|
||||
func (this *UserAccountDAO) CountAllAccounts(tx *dbs.Tx, keyword string) (int64, error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
|
||||
query.Param("keyword", keyword)
|
||||
} else {
|
||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// ListAccounts 列出单页账户
|
||||
func (this *UserAccountDAO) ListAccounts(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*UserAccount, err error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
|
||||
query.Param("keyword", keyword)
|
||||
} else {
|
||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
_, err = query.
|
||||
DescPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// PayBills 尝试自动支付账单
|
||||
func (this *UserAccountDAO) PayBills(tx *dbs.Tx) error {
|
||||
bills, err := models.SharedUserBillDAO.FindUnpaidBills(tx, 10000)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 先支付久远的
|
||||
lists.Reverse(bills)
|
||||
|
||||
for _, bill := range bills {
|
||||
if bill.Amount <= 0 {
|
||||
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
account, err := SharedUserAccountDAO.FindUserAccountWithUserId(tx, int64(bill.UserId))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if account == nil || account.Total < bill.Amount {
|
||||
continue
|
||||
}
|
||||
|
||||
// 扣款
|
||||
err = SharedUserAccountDAO.UpdateUserAccount(tx, int64(account.Id), -float32(bill.Amount), userconfigs.AccountEventTypePayBill, "支付账单"+bill.Code, maps.Map{"billId": bill.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 改为已支付
|
||||
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package accounts
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUserAccountDAO_PayBills(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
err := NewUserAccountDAO().PayBills(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type UserAccountLogDAO dbs.DAO
|
||||
|
||||
func NewUserAccountLogDAO() *UserAccountLogDAO {
|
||||
return dbs.NewDAO(&UserAccountLogDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeUserAccountLogs",
|
||||
Model: new(UserAccountLog),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*UserAccountLogDAO)
|
||||
}
|
||||
|
||||
var SharedUserAccountLogDAO *UserAccountLogDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedUserAccountLogDAO = NewUserAccountLogDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// CreateAccountLog 生成用户账户日志
|
||||
func (this *UserAccountLogDAO) CreateAccountLog(tx *dbs.Tx, userId int64, accountId int64, delta float32, deltaFrozen float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
||||
var op = NewUserAccountLogOperator()
|
||||
op.UserId = userId
|
||||
op.AccountId = accountId
|
||||
op.Delta = delta
|
||||
op.DeltaFrozen = deltaFrozen
|
||||
|
||||
account, err := SharedUserAccountDAO.FindUserAccountWithAccountId(tx, accountId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if account == nil {
|
||||
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
||||
}
|
||||
op.Total = account.Total
|
||||
op.TotalFrozen = account.TotalFrozen
|
||||
|
||||
op.EventType = eventType
|
||||
op.Description = description
|
||||
|
||||
if params == nil {
|
||||
params = maps.Map{}
|
||||
}
|
||||
op.Params = params.AsJSON()
|
||||
|
||||
op.Day = timeutil.Format("Ymd")
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return SharedUserAccountDailyStatDAO.UpdateDailyStat(tx)
|
||||
}
|
||||
|
||||
// CountAccountLogs 计算日志数量
|
||||
func (this *UserAccountLogDAO) CountAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string) (int64, error) {
|
||||
var query = this.Query(tx)
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
if accountId > 0 {
|
||||
query.Attr("accountId", accountId)
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
|
||||
query.Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
if len(eventType) > 0 {
|
||||
query.Attr("eventType", eventType)
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// ListAccountLogs 列出单页日志
|
||||
func (this *UserAccountLogDAO) ListAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string, offset int64, size int64) (result []*UserAccountLog, err error) {
|
||||
var query = this.Query(tx)
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
if accountId > 0 {
|
||||
query.Attr("accountId", accountId)
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
|
||||
query.Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
if len(eventType) > 0 {
|
||||
query.Attr("eventType", eventType)
|
||||
}
|
||||
_, err = query.
|
||||
DescPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// SumDailyEventTypes 统计某天数据总和
|
||||
func (this *UserAccountLogDAO) SumDailyEventTypes(tx *dbs.Tx, day string, eventTypes []userconfigs.AccountEventType) (float32, error) {
|
||||
if len(eventTypes) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
result, err := this.Query(tx).
|
||||
Attr("day", day).
|
||||
Attr("eventType", eventTypes).
|
||||
Sum("delta", 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Float32(result), nil
|
||||
}
|
||||
@@ -356,7 +356,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
||||
errMsg = "找不到DNS服务商账号"
|
||||
return
|
||||
}
|
||||
providerInterface := dnsclients.FindProvider(dnsProvider.Type)
|
||||
providerInterface := dnsclients.FindProvider(dnsProvider.Type, int64(dnsProvider.Id))
|
||||
if providerInterface == nil {
|
||||
errMsg = "暂不支持此类型的DNS服务商 '" + dnsProvider.Type + "'"
|
||||
return
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 生成日志
|
||||
// CreateACMETaskLog 生成日志
|
||||
func (this *ACMETaskLogDAO) CreateACMETaskLog(tx *dbs.Tx, taskId int64, isOk bool, errMsg string) error {
|
||||
var op = NewACMETaskLogOperator()
|
||||
op.TaskId = taskId
|
||||
@@ -37,7 +37,7 @@ func (this *ACMETaskLogDAO) CreateACMETaskLog(tx *dbs.Tx, taskId int64, isOk boo
|
||||
return err
|
||||
}
|
||||
|
||||
// 取得任务的最后一条执行日志
|
||||
// FindLatestACMETasKLog 取得任务的最后一条执行日志
|
||||
func (this *ACMETaskLogDAO) FindLatestACMETasKLog(tx *dbs.Tx, taskId int64) (*ACMETaskLog, error) {
|
||||
one, err := this.Query(tx).
|
||||
Attr("taskId", taskId).
|
||||
|
||||
@@ -44,11 +44,17 @@ func (this *AdminDAO) EnableAdmin(tx *dbs.Tx, id int64) (rowsAffected int64, err
|
||||
}
|
||||
|
||||
// DisableAdmin 禁用条目
|
||||
func (this *AdminDAO) DisableAdmin(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
func (this *AdminDAO) DisableAdmin(tx *dbs.Tx, adminId int64) error {
|
||||
err := this.Query(tx).
|
||||
Pk(adminId).
|
||||
Set("state", AdminStateDisabled).
|
||||
Update()
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除AccessTokens
|
||||
return SharedAPIAccessTokenDAO.DeleteAccessTokens(tx, adminId, 0)
|
||||
}
|
||||
|
||||
// FindEnabledAdmin 查找启用中的条目
|
||||
@@ -190,7 +196,19 @@ func (this *AdminDAO) UpdateAdmin(tx *dbs.Tx, adminId int64, username string, ca
|
||||
}
|
||||
op.IsOn = isOn
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !isOn {
|
||||
// 删除AccessTokens
|
||||
err = SharedAPIAccessTokenDAO.DeleteAccessTokens(tx, adminId, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckAdminUsername 检查用户名是否存在
|
||||
|
||||
@@ -81,3 +81,16 @@ func (this *APIAccessTokenDAO) FindAccessToken(tx *dbs.Tx, token string) (*APIAc
|
||||
}
|
||||
return one.(*APIAccessToken), nil
|
||||
}
|
||||
|
||||
// DeleteAccessTokens 删除用户的令牌
|
||||
func (this *APIAccessTokenDAO) DeleteAccessTokens(tx *dbs.Tx, adminId int64, userId int64) error {
|
||||
var query = this.Query(tx)
|
||||
if adminId > 0 {
|
||||
query.Attr("adminId", adminId)
|
||||
} else if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return query.DeleteQuickly()
|
||||
}
|
||||
|
||||
@@ -43,9 +43,9 @@ func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverc
|
||||
}
|
||||
|
||||
if config.SSLPolicyRef != nil {
|
||||
policyId := config.SSLPolicyRef.SSLPolicyId
|
||||
var policyId = config.SSLPolicyRef.SSLPolicyId
|
||||
if policyId > 0 {
|
||||
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, cacheMap)
|
||||
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, false, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -143,7 +143,7 @@ func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*ser
|
||||
if config.SSLPolicyRef != nil {
|
||||
policyId := config.SSLPolicyRef.SSLPolicyId
|
||||
if policyId > 0 {
|
||||
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, cacheMap)
|
||||
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, false, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (this *ApiTokenDAO) FindEnabledTokenWithNodeCacheable(tx *dbs.Tx, nodeId st
|
||||
State(ApiTokenStateEnabled).
|
||||
Find()
|
||||
if one != nil {
|
||||
token := one.(*ApiToken)
|
||||
token = one.(*ApiToken)
|
||||
SharedCacheLocker.Lock()
|
||||
apiTokenCacheMap[nodeId] = token
|
||||
SharedCacheLocker.Unlock()
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
@@ -1,22 +0,0 @@
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// ClientBrowser 终端浏览器信息
|
||||
type ClientBrowser struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 浏览器名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type ClientBrowserOperator struct {
|
||||
Id interface{} // ID
|
||||
Name interface{} // 浏览器名称
|
||||
Codes interface{} // 代号
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewClientBrowserOperator() *ClientBrowserOperator {
|
||||
return &ClientBrowserOperator{}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
@@ -1,22 +0,0 @@
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// ClientSystem 终端操作系统信息
|
||||
type ClientSystem struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 系统名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
State uint8 `field:"state"` //
|
||||
}
|
||||
|
||||
type ClientSystemOperator struct {
|
||||
Id interface{} // ID
|
||||
Name interface{} // 系统名称
|
||||
Codes interface{} // 代号
|
||||
State interface{} //
|
||||
}
|
||||
|
||||
func NewClientSystemOperator() *ClientSystemOperator {
|
||||
return &ClientSystemOperator{}
|
||||
}
|
||||
98
internal/db/models/clients/client_agent_dao.go
Normal file
98
internal/db/models/clients/client_agent_dao.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
type ClientAgentDAO dbs.DAO
|
||||
|
||||
func NewClientAgentDAO() *ClientAgentDAO {
|
||||
return dbs.NewDAO(&ClientAgentDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeClientAgents",
|
||||
Model: new(ClientAgent),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*ClientAgentDAO)
|
||||
}
|
||||
|
||||
var SharedClientAgentDAO *ClientAgentDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedClientAgentDAO = NewClientAgentDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// FindClientAgentName 根据主键查找名称
|
||||
func (this *ClientAgentDAO) FindClientAgentName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindAgent 查找Agent
|
||||
func (this *ClientAgentDAO) FindAgent(tx *dbs.Tx, agentId int64) (*ClientAgent, error) {
|
||||
if agentId <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
one, err := this.Query(tx).
|
||||
Pk(agentId).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*ClientAgent), nil
|
||||
}
|
||||
|
||||
// FindAgentIdWithCode 根据代号查找ID
|
||||
func (this *ClientAgentDAO) FindAgentIdWithCode(tx *dbs.Tx, code string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
ResultPk().
|
||||
Attr("code", code).
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindAgentNameWithCode 根据代号查找Agent名称
|
||||
func (this *ClientAgentDAO) FindAgentNameWithCode(tx *dbs.Tx, code string) (string, error) {
|
||||
return this.Query(tx).
|
||||
Result("name").
|
||||
Attr("code", code).
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// UpdateAgentCountIPs 修改Agent拥有的IP数量
|
||||
func (this *ClientAgentDAO) UpdateAgentCountIPs(tx *dbs.Tx, agentId int64, countIPs int64) error {
|
||||
return this.Query(tx).
|
||||
Pk(agentId).
|
||||
Set("countIPs", countIPs).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// FindAllAgents 查找所有Agents
|
||||
func (this *ClientAgentDAO) FindAllAgents(tx *dbs.Tx) (result []*ClientAgent, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllNSAgents 查找所有DNS可以使用的Agents
|
||||
func (this *ClientAgentDAO) FindAllNSAgents(tx *dbs.Tx) (result []*ClientAgent, err error) {
|
||||
// 注意:允许NS使用所有的Agent,不管有没有IP数据
|
||||
_, err = this.Query(tx).
|
||||
Result("id", "name", "code").
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
6
internal/db/models/clients/client_agent_dao_test.go
Normal file
6
internal/db/models/clients/client_agent_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
105
internal/db/models/clients/client_agent_ip_dao.go
Normal file
105
internal/db/models/clients/client_agent_ip_dao.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
// TODO 需要定时对所有IP的PTR进行检查,剔除已经变更的IP
|
||||
|
||||
type ClientAgentIPDAO dbs.DAO
|
||||
|
||||
func NewClientAgentIPDAO() *ClientAgentIPDAO {
|
||||
return dbs.NewDAO(&ClientAgentIPDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeClientAgentIPs",
|
||||
Model: new(ClientAgentIP),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*ClientAgentIPDAO)
|
||||
}
|
||||
|
||||
var SharedClientAgentIPDAO *ClientAgentIPDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedClientAgentIPDAO = NewClientAgentIPDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// CreateIP 写入IP
|
||||
func (this *ClientAgentIPDAO) CreateIP(tx *dbs.Tx, agentId int64, ip string, ptr string) error {
|
||||
// 检查数据有效性
|
||||
if agentId <= 0 || len(ip) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 限制ptr长度
|
||||
if len(ptr) > 100 {
|
||||
ptr = ptr[:100]
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
exists, err := this.Query(tx).
|
||||
Attr("agentId", agentId).
|
||||
Attr("ip", ip).
|
||||
Exist()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
var op = NewClientAgentIPOperator()
|
||||
op.AgentId = agentId
|
||||
op.IP = ip
|
||||
op.Ptr = ptr
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
// 忽略duplicate错误
|
||||
if models.CheckSQLDuplicateErr(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新Agent IP数量
|
||||
countIPs, err := this.CountAgentIPs(tx, agentId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = SharedClientAgentDAO.UpdateAgentCountIPs(tx, agentId, countIPs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListIPsAfterId 列出某个ID之后的IP
|
||||
func (this *ClientAgentIPDAO) ListIPsAfterId(tx *dbs.Tx, id int64, size int64) (result []*ClientAgentIP, err error) {
|
||||
if id < 0 {
|
||||
id = 0
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Result("id", "ip", "ptr", "agentId").
|
||||
Gt("id", id).
|
||||
AscPk().
|
||||
Limit(size). // 限制单次读取个数
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CountAgentIPs 计算Agent IP数量
|
||||
func (this *ClientAgentIPDAO) CountAgentIPs(tx *dbs.Tx, agentId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("agentId", agentId).
|
||||
Count()
|
||||
}
|
||||
16
internal/db/models/clients/client_agent_ip_dao_test.go
Normal file
16
internal/db/models/clients/client_agent_ip_dao_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientAgentIPDAO_CreateIP(t *testing.T) {
|
||||
var dao = clients.NewClientAgentIPDAO()
|
||||
err := dao.CreateIP(nil, 1, "127.0.0.1", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
20
internal/db/models/clients/client_agent_ip_model.go
Normal file
20
internal/db/models/clients/client_agent_ip_model.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package clients
|
||||
|
||||
// ClientAgentIP Agent IP
|
||||
type ClientAgentIP struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
AgentId uint32 `field:"agentId"` // Agent ID
|
||||
IP string `field:"ip"` // IP地址
|
||||
Ptr string `field:"ptr"` // PTR值
|
||||
}
|
||||
|
||||
type ClientAgentIPOperator struct {
|
||||
Id any // ID
|
||||
AgentId any // Agent ID
|
||||
IP any // IP地址
|
||||
Ptr any // PTR值
|
||||
}
|
||||
|
||||
func NewClientAgentIPOperator() *ClientAgentIPOperator {
|
||||
return &ClientAgentIPOperator{}
|
||||
}
|
||||
1
internal/db/models/clients/client_agent_ip_model_ext.go
Normal file
1
internal/db/models/clients/client_agent_ip_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package clients
|
||||
24
internal/db/models/clients/client_agent_model.go
Normal file
24
internal/db/models/clients/client_agent_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package clients
|
||||
|
||||
// ClientAgent Agent库
|
||||
type ClientAgent struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 名称
|
||||
Code string `field:"code"` // 代号
|
||||
Description string `field:"description"` // 介绍
|
||||
Order uint32 `field:"order"` // 排序
|
||||
CountIPs uint32 `field:"countIPs"` // IP数量
|
||||
}
|
||||
|
||||
type ClientAgentOperator struct {
|
||||
Id any // ID
|
||||
Name any // 名称
|
||||
Code any // 代号
|
||||
Description any // 介绍
|
||||
Order any // 排序
|
||||
CountIPs any // IP数量
|
||||
}
|
||||
|
||||
func NewClientAgentOperator() *ClientAgentOperator {
|
||||
return &ClientAgentOperator{}
|
||||
}
|
||||
6
internal/db/models/clients/client_agent_model_ext.go
Normal file
6
internal/db/models/clients/client_agent_model_ext.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package clients
|
||||
|
||||
// NSRouteCode NS线路代号
|
||||
func (this *ClientAgent) NSRouteCode() string {
|
||||
return "agent:" + this.Code
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
package models
|
||||
package clients
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"strconv"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -13,7 +18,20 @@ const (
|
||||
ClientBrowserStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
var clientBrowserNameAndIdCacheMap = map[string]int64{}
|
||||
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 := SharedClientBrowserDAO.Clean(nil, 7) // 只保留N天
|
||||
if err != nil {
|
||||
remotelogs.Error("SharedClientBrowserDAO", "clean expired data failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type ClientBrowserDAO dbs.DAO
|
||||
|
||||
@@ -74,65 +92,64 @@ func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (stri
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindBrowserIdWithNameCacheable 根据浏览器名称查找浏览器ID
|
||||
func (this *ClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browserName string) (int64, error) {
|
||||
SharedCacheLocker.RLock()
|
||||
browserId, ok := clientBrowserNameAndIdCacheMap[browserName]
|
||||
if ok {
|
||||
SharedCacheLocker.RUnlock()
|
||||
return browserId, nil
|
||||
}
|
||||
SharedCacheLocker.RUnlock()
|
||||
|
||||
browserId, err := this.Query(tx).
|
||||
Where("JSON_CONTAINS(codes, :browserName)").
|
||||
Param("browserName", strconv.Quote(browserName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if browserId > 0 {
|
||||
// 只有找到的时候才放入缓存,以便于我们可以在不存在的时候创建一条新的记录
|
||||
SharedCacheLocker.Lock()
|
||||
clientBrowserNameAndIdCacheMap[browserName] = browserId
|
||||
SharedCacheLocker.Unlock()
|
||||
}
|
||||
|
||||
return browserId, nil
|
||||
}
|
||||
|
||||
// CreateBrowser 创建浏览器
|
||||
func (this *ClientBrowserDAO) CreateBrowser(tx *dbs.Tx, browserName string) (int64, error) {
|
||||
var maxlength = 50
|
||||
// CreateBrowserIfNotExists 创建浏览器信息
|
||||
func (this *ClientBrowserDAO) CreateBrowserIfNotExists(tx *dbs.Tx, browserName string) error {
|
||||
const maxlength = 50
|
||||
if len(browserName) > maxlength {
|
||||
browserName = browserName[:50]
|
||||
}
|
||||
|
||||
SharedCacheLocker.Lock()
|
||||
defer SharedCacheLocker.Unlock()
|
||||
// 检查缓存
|
||||
var cacheKey = "clientBrowser:" + browserName
|
||||
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||
if cacheItem != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查是否已经创建
|
||||
// 检查是否已经存在
|
||||
// 不需要加状态条件
|
||||
browserId, err := this.Query(tx).
|
||||
Attr("name", browserName).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return err
|
||||
}
|
||||
if browserId > 0 {
|
||||
return browserId, nil
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
|
||||
|
||||
return this.Query(tx).
|
||||
Pk(browserId).
|
||||
Set("createdDay", timeutil.Format("Ymd")).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// 如果不存在,则创建之
|
||||
var op = NewClientBrowserOperator()
|
||||
op.Name = browserName
|
||||
codes := []string{browserName}
|
||||
codesJSON, err := json.Marshal(codes)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
op.CreatedDay = timeutil.Format("Ymd")
|
||||
op.State = ClientBrowserStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
browserId, err = this.SaveInt64(tx, op)
|
||||
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
if browserId > 0 {
|
||||
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Clean 清理
|
||||
func (this *ClientBrowserDAO) Clean(tx *dbs.Tx, days int) error {
|
||||
if days <= 0 {
|
||||
days = 30
|
||||
}
|
||||
|
||||
return this.Query(tx).
|
||||
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
|
||||
DeleteQuickly()
|
||||
}
|
||||
33
internal/db/models/clients/client_browser_dao_test.go
Normal file
33
internal/db/models/clients/client_browser_dao_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientBrowserDAO_CreateBrowser(t *testing.T) {
|
||||
var dao = clients.NewClientBrowserDAO()
|
||||
err := dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientBrowserDAO_Clean(t *testing.T) {
|
||||
var dao = clients.NewClientBrowserDAO()
|
||||
err := dao.Clean(nil, 30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
24
internal/db/models/clients/client_browser_model.go
Normal file
24
internal/db/models/clients/client_browser_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package clients
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// ClientBrowser 终端浏览器信息
|
||||
type ClientBrowser struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
Name string `field:"name"` // 浏览器名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type ClientBrowserOperator struct {
|
||||
Id any // ID
|
||||
Name any // 浏览器名称
|
||||
Codes any // 代号
|
||||
CreatedDay any // 创建日期YYYYMMDD
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewClientBrowserOperator() *ClientBrowserOperator {
|
||||
return &ClientBrowserOperator{}
|
||||
}
|
||||
1
internal/db/models/clients/client_browser_model_ext.go
Normal file
1
internal/db/models/clients/client_browser_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package clients
|
||||
@@ -1,11 +1,16 @@
|
||||
package models
|
||||
package clients
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"strconv"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -13,7 +18,20 @@ const (
|
||||
ClientSystemStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
var clientSystemNameAndIdCacheMap = map[string]int64{} // system name => id
|
||||
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 := SharedClientSystemDAO.Clean(nil, 7) // 只保留N天
|
||||
if err != nil {
|
||||
remotelogs.Error("SharedClientSystemDAO", "clean expired data failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type ClientSystemDAO dbs.DAO
|
||||
|
||||
@@ -74,67 +92,63 @@ func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindSystemIdWithNameCacheable 根据操作系统名称查找系统ID
|
||||
func (this *ClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemName string) (int64, error) {
|
||||
SharedCacheLocker.RLock()
|
||||
systemId, ok := clientSystemNameAndIdCacheMap[systemName]
|
||||
if ok {
|
||||
SharedCacheLocker.RUnlock()
|
||||
return systemId, nil
|
||||
}
|
||||
SharedCacheLocker.RUnlock()
|
||||
|
||||
systemId, err := this.Query(tx).
|
||||
Where("JSON_CONTAINS(codes, :systemName)").
|
||||
Param("systemName", strconv.Quote(systemName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if systemId > 0 {
|
||||
// 只有找到的时候才放入缓存,以便于我们可以在不存在的时候创建一条新的记录
|
||||
SharedCacheLocker.Lock()
|
||||
clientSystemNameAndIdCacheMap[systemName] = systemId
|
||||
SharedCacheLocker.Unlock()
|
||||
}
|
||||
|
||||
return systemId, nil
|
||||
}
|
||||
|
||||
// CreateSystem 创建浏览器
|
||||
func (this *ClientSystemDAO) CreateSystem(tx *dbs.Tx, systemName string) (int64, error) {
|
||||
var maxlength = 50
|
||||
// CreateSystemIfNotExists 创建系统信息
|
||||
func (this *ClientSystemDAO) CreateSystemIfNotExists(tx *dbs.Tx, systemName string) error {
|
||||
const maxlength = 50
|
||||
if len(systemName) > maxlength {
|
||||
systemName = systemName[:50]
|
||||
}
|
||||
|
||||
SharedCacheLocker.Lock()
|
||||
defer SharedCacheLocker.Unlock()
|
||||
// 检查缓存
|
||||
var cacheKey = "clientSystem:" + systemName
|
||||
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||
if cacheItem != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查是否已经创建
|
||||
// 检查是否已经存在
|
||||
// 不需要加状态条件
|
||||
systemId, err := this.Query(tx).
|
||||
Attr("name", systemName).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return err
|
||||
}
|
||||
if systemId > 0 {
|
||||
return systemId, nil
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
|
||||
|
||||
return this.Query(tx).
|
||||
Pk(systemId).
|
||||
Set("createdDay", timeutil.Format("Ymd")).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
var op = NewClientSystemOperator()
|
||||
op.Name = systemName
|
||||
|
||||
codes := []string{systemName}
|
||||
codesJSON, err := json.Marshal(codes)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
|
||||
op.CreatedDay = timeutil.Format("Ymd")
|
||||
op.State = ClientSystemStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
systemId, err = this.SaveInt64(tx, op)
|
||||
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
if systemId > 0 {
|
||||
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Clean 清理
|
||||
func (this *ClientSystemDAO) Clean(tx *dbs.Tx, days int) error {
|
||||
if days <= 0 {
|
||||
days = 30
|
||||
}
|
||||
|
||||
return this.Query(tx).
|
||||
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
|
||||
DeleteQuickly()
|
||||
}
|
||||
31
internal/db/models/clients/client_system_dao_test.go
Normal file
31
internal/db/models/clients/client_system_dao_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientSystemDAO_CreateSystemIfNotExists(t *testing.T) {
|
||||
var dao = clients.NewClientSystemDAO()
|
||||
{
|
||||
err := dao.CreateSystemIfNotExists(nil, "Mac OS X")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
{
|
||||
err := dao.CreateSystemIfNotExists(nil, "Mac OS X 2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientSystemDAO_Clean(t *testing.T) {
|
||||
var dao = clients.NewClientSystemDAO()
|
||||
err := dao.Clean(nil, 30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
24
internal/db/models/clients/client_system_model.go
Normal file
24
internal/db/models/clients/client_system_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package clients
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// ClientSystem 终端操作系统信息
|
||||
type ClientSystem struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
Name string `field:"name"` // 系统名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type ClientSystemOperator struct {
|
||||
Id any // ID
|
||||
Name any // 系统名称
|
||||
Codes any // 代号
|
||||
CreatedDay any // 创建日期YYYYMMDD
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewClientSystemOperator() *ClientSystemOperator {
|
||||
return &ClientSystemOperator{}
|
||||
}
|
||||
1
internal/db/models/clients/client_system_model_ext.go
Normal file
1
internal/db/models/clients/client_system_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package clients
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
@@ -29,8 +30,9 @@ var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId =
|
||||
|
||||
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
|
||||
type HTTPAccessLogDAOWrapper struct {
|
||||
DAO *HTTPAccessLogDAO
|
||||
NodeId int64
|
||||
DAO *HTTPAccessLogDAO
|
||||
NodeId int64
|
||||
IsLocal bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -195,7 +197,7 @@ func (this *DBNodeInitializer) loop() error {
|
||||
continue
|
||||
}
|
||||
|
||||
daoObject := dbs.DAOObject{
|
||||
var daoObject = dbs.DAOObject{
|
||||
Instance: db,
|
||||
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
|
||||
Table: tableDef.Name,
|
||||
@@ -210,12 +212,13 @@ func (this *DBNodeInitializer) loop() error {
|
||||
|
||||
accessLogLocker.Lock()
|
||||
accessLogDBMapping[nodeId] = db
|
||||
dao := &HTTPAccessLogDAO{
|
||||
var dao = &HTTPAccessLogDAO{
|
||||
DAOObject: daoObject,
|
||||
}
|
||||
httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
|
||||
DAO: dao,
|
||||
NodeId: nodeId,
|
||||
DAO: dao,
|
||||
NodeId: nodeId,
|
||||
IsLocal: dbutils.IsLocalAddr(node.Host),
|
||||
}
|
||||
accessLogLocker.Unlock()
|
||||
}
|
||||
|
||||
@@ -78,8 +78,8 @@ func (this *DNSTaskDAO) CreateClusterRemoveTask(tx *dbs.Tx, clusterId int64, dom
|
||||
}
|
||||
|
||||
// CreateNodeTask 生成节点任务
|
||||
func (this *DNSTaskDAO) CreateNodeTask(tx *dbs.Tx, nodeId int64, taskType DNSTaskType) error {
|
||||
return this.CreateDNSTask(tx, 0, 0, nodeId, 0, "", taskType)
|
||||
func (this *DNSTaskDAO) CreateNodeTask(tx *dbs.Tx, clusterId int64, nodeId int64, taskType DNSTaskType) error {
|
||||
return this.CreateDNSTask(tx, clusterId, 0, nodeId, 0, "", taskType)
|
||||
}
|
||||
|
||||
// CreateServerTask 生成服务任务
|
||||
|
||||
@@ -67,7 +67,7 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster, checkNodeIssues bo
|
||||
})
|
||||
return
|
||||
}
|
||||
var dnsProvider = dnsclients.FindProvider(provider.Type)
|
||||
var dnsProvider = dnsclients.FindProvider(provider.Type, int64(provider.Id))
|
||||
if dnsProvider == nil {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: cluster.Name,
|
||||
@@ -200,7 +200,7 @@ func FindDefaultDomainRoute(tx *dbs.Tx, domain *dns.DNSDomain) (string, error) {
|
||||
if err != nil {
|
||||
return "", errors.New("decode provider params failed: " + err.Error())
|
||||
}
|
||||
var dnsProvider = dnsclients.FindProvider(provider.Type)
|
||||
var dnsProvider = dnsclients.FindProvider(provider.Type, int64(provider.Id))
|
||||
if dnsProvider == nil {
|
||||
return "", errors.New("not supported provider type '" + provider.Type + "'")
|
||||
}
|
||||
|
||||
207
internal/db/models/formal_client_browser_dao.go
Normal file
207
internal/db/models/formal_client_browser_dao.go
Normal file
@@ -0,0 +1,207 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
FormalClientBrowserStateEnabled = 1 // 已启用
|
||||
FormalClientBrowserStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type FormalClientBrowserDAO dbs.DAO
|
||||
|
||||
func NewFormalClientBrowserDAO() *FormalClientBrowserDAO {
|
||||
return dbs.NewDAO(&FormalClientBrowserDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeFormalClientBrowsers",
|
||||
Model: new(FormalClientBrowser),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*FormalClientBrowserDAO)
|
||||
}
|
||||
|
||||
var SharedFormalClientBrowserDAO *FormalClientBrowserDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedFormalClientBrowserDAO = NewFormalClientBrowserDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableFormalClientBrowser 启用条目
|
||||
func (this *FormalClientBrowserDAO) EnableFormalClientBrowser(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", FormalClientBrowserStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableFormalClientBrowser 禁用条目
|
||||
func (this *FormalClientBrowserDAO) DisableFormalClientBrowser(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", FormalClientBrowserStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledFormalClientBrowser 查找启用中的条目
|
||||
func (this *FormalClientBrowserDAO) FindEnabledFormalClientBrowser(tx *dbs.Tx, id int64) (*FormalClientBrowser, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
State(FormalClientBrowserStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*FormalClientBrowser), err
|
||||
}
|
||||
|
||||
// FindFormalClientBrowserName 根据主键查找名称
|
||||
func (this *FormalClientBrowserDAO) FindFormalClientBrowserName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindBrowserIdWithNameCacheable 根据浏览器名称查找系统ID
|
||||
func (this *FormalClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browserName string) (int64, error) {
|
||||
var cacheKey = "formalClientBrowser:" + browserName
|
||||
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||
if cacheItem != nil {
|
||||
return types.Int64(cacheItem.Value), nil
|
||||
}
|
||||
|
||||
// 先使用 name 查找,因为有索引,所以会快一些
|
||||
browserId, err := this.Query(tx).
|
||||
Attr("name", browserName).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if browserId == 0 {
|
||||
browserId, err = this.Query(tx).
|
||||
Where("JSON_CONTAINS(codes, :browserName)").
|
||||
Param("browserName", strconv.Quote(browserName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
// 即使找不到也要放入到缓存中
|
||||
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
|
||||
|
||||
return browserId, nil
|
||||
}
|
||||
|
||||
// CountBrowsers 计算浏览器数量
|
||||
func (this *FormalClientBrowserDAO) CountBrowsers(tx *dbs.Tx, keyword string) (int64, error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Like("LOWER(codes)", dbutils.QuoteLikeKeyword(strings.ToLower(keyword)))
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// ListBrowsers 列出单页浏览器信息
|
||||
func (this *FormalClientBrowserDAO) ListBrowsers(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*FormalClientBrowser, err error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Like("LOWER(codes)", dbutils.QuoteLikeKeyword(strings.ToLower(keyword)))
|
||||
}
|
||||
_, err = query.
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindBrowserWithDataId 根据dataId查找浏览器信息
|
||||
func (this *FormalClientBrowserDAO) FindBrowserWithDataId(tx *dbs.Tx, dataId string) (*FormalClientBrowser, error) {
|
||||
one, err := this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*FormalClientBrowser), nil
|
||||
}
|
||||
|
||||
// CreateBrowser 创建浏览器信息
|
||||
func (this *FormalClientBrowserDAO) CreateBrowser(tx *dbs.Tx, name string, codes []string, dataId string) (int64, error) {
|
||||
if len(dataId) == 0 {
|
||||
return 0, errors.New("invalid dataId")
|
||||
}
|
||||
|
||||
// 检查 dataId 是否已经存在
|
||||
exists, err := this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if exists {
|
||||
return 0, errors.New("dataId '" + dataId + "' already exists")
|
||||
}
|
||||
|
||||
var op = NewFormalClientBrowserOperator()
|
||||
op.Name = name
|
||||
if len(codes) == 0 {
|
||||
op.Codes = "[]"
|
||||
} else {
|
||||
codesJSON, err := json.Marshal(codes)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
}
|
||||
op.DataId = dataId
|
||||
op.State = FormalClientBrowserStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdateBrowser 修改浏览器信息
|
||||
func (this *FormalClientBrowserDAO) UpdateBrowser(tx *dbs.Tx, browserId int64, name string, codes []string, dataId string) error {
|
||||
if browserId <= 0 {
|
||||
return errors.New("invalid browserId '" + types.String(browserId) + "'")
|
||||
}
|
||||
if len(dataId) == 0 {
|
||||
return errors.New("invalid dataId")
|
||||
}
|
||||
|
||||
var op = NewFormalClientBrowserOperator()
|
||||
op.Id = browserId
|
||||
op.Name = name
|
||||
if len(codes) == 0 {
|
||||
op.Codes = "[]"
|
||||
} else {
|
||||
codesJSON, err := json.Marshal(codes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
}
|
||||
op.DataId = dataId
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package accounts
|
||||
package models_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
24
internal/db/models/formal_client_browser_model.go
Normal file
24
internal/db/models/formal_client_browser_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// FormalClientBrowser 终端浏览器信息
|
||||
type FormalClientBrowser struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 浏览器名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
DataId string `field:"dataId"` // 数据ID
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type FormalClientBrowserOperator struct {
|
||||
Id any // ID
|
||||
Name any // 浏览器名称
|
||||
Codes any // 代号
|
||||
DataId any // 数据ID
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewFormalClientBrowserOperator() *FormalClientBrowserOperator {
|
||||
return &FormalClientBrowserOperator{}
|
||||
}
|
||||
21
internal/db/models/formal_client_browser_model_ext.go
Normal file
21
internal/db/models/formal_client_browser_model_ext.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
)
|
||||
|
||||
// DecodeCodes 解析代号
|
||||
func (this *FormalClientBrowser) DecodeCodes() []string {
|
||||
if IsNull(this.Codes) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var result = []string{}
|
||||
err := json.Unmarshal(this.Codes, &result)
|
||||
if err != nil {
|
||||
remotelogs.Error("FormalClientBrowser.DecodeCodes", err.Error())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
207
internal/db/models/formal_client_system_dao.go
Normal file
207
internal/db/models/formal_client_system_dao.go
Normal file
@@ -0,0 +1,207 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
FormalClientSystemStateEnabled = 1 // 已启用
|
||||
FormalClientSystemStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type FormalClientSystemDAO dbs.DAO
|
||||
|
||||
func NewFormalClientSystemDAO() *FormalClientSystemDAO {
|
||||
return dbs.NewDAO(&FormalClientSystemDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeFormalClientSystems",
|
||||
Model: new(FormalClientSystem),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*FormalClientSystemDAO)
|
||||
}
|
||||
|
||||
var SharedFormalClientSystemDAO *FormalClientSystemDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedFormalClientSystemDAO = NewFormalClientSystemDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableFormalClientSystem 启用条目
|
||||
func (this *FormalClientSystemDAO) EnableFormalClientSystem(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", FormalClientSystemStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableFormalClientSystem 禁用条目
|
||||
func (this *FormalClientSystemDAO) DisableFormalClientSystem(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", FormalClientSystemStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledFormalClientSystem 查找启用中的条目
|
||||
func (this *FormalClientSystemDAO) FindEnabledFormalClientSystem(tx *dbs.Tx, id int64) (*FormalClientSystem, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
State(FormalClientSystemStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*FormalClientSystem), err
|
||||
}
|
||||
|
||||
// FindFormalClientSystemName 根据主键查找名称
|
||||
func (this *FormalClientSystemDAO) FindFormalClientSystemName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindSystemIdWithNameCacheable 根据操作系统名称查找系统ID
|
||||
func (this *FormalClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemName string) (int64, error) {
|
||||
var cacheKey = "formalClientSystem:" + systemName
|
||||
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||
if cacheItem != nil {
|
||||
return types.Int64(cacheItem.Value), nil
|
||||
}
|
||||
|
||||
// 先使用 name 查找,因为有索引,所以会快一些
|
||||
systemId, err := this.Query(tx).
|
||||
Attr("name", systemName).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if systemId == 0 {
|
||||
systemId, err = this.Query(tx).
|
||||
Where("JSON_CONTAINS(codes, :systemName)").
|
||||
Param("systemName", strconv.Quote(systemName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
// 即使找不到也要放入到缓存中
|
||||
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
|
||||
|
||||
return systemId, nil
|
||||
}
|
||||
|
||||
// CreateSystem 创建操作系统信息
|
||||
func (this *FormalClientSystemDAO) CreateSystem(tx *dbs.Tx, name string, codes []string, dataId string) (int64, error) {
|
||||
if len(dataId) == 0 {
|
||||
return 0, errors.New("invalid dataId")
|
||||
}
|
||||
|
||||
// 检查 dataId 是否已经存在
|
||||
exists, err := this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if exists {
|
||||
return 0, errors.New("dataId '" + dataId + "' already exists")
|
||||
}
|
||||
|
||||
var op = NewFormalClientSystemOperator()
|
||||
op.Name = name
|
||||
if len(codes) == 0 {
|
||||
op.Codes = "[]"
|
||||
} else {
|
||||
codesJSON, err := json.Marshal(codes)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
}
|
||||
op.DataId = dataId
|
||||
op.State = FormalClientSystemStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdateSystem 修改操作系统信息
|
||||
func (this *FormalClientSystemDAO) UpdateSystem(tx *dbs.Tx, systemId int64, name string, codes []string, dataId string) error {
|
||||
if systemId <= 0 {
|
||||
return errors.New("invalid systemId '" + types.String(systemId) + "'")
|
||||
}
|
||||
if len(dataId) == 0 {
|
||||
return errors.New("invalid dataId")
|
||||
}
|
||||
|
||||
var op = NewFormalClientSystemOperator()
|
||||
op.Id = systemId
|
||||
op.Name = name
|
||||
if len(codes) == 0 {
|
||||
op.Codes = "[]"
|
||||
} else {
|
||||
codesJSON, err := json.Marshal(codes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.Codes = codesJSON
|
||||
}
|
||||
op.DataId = dataId
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// CountSystems 计算操作系统数量
|
||||
func (this *FormalClientSystemDAO) CountSystems(tx *dbs.Tx, keyword string) (int64, error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Like("LOWER(codes)", dbutils.QuoteLikeKeyword(strings.ToLower(keyword)))
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// ListSystems 列出单页操作系统信息
|
||||
func (this *FormalClientSystemDAO) ListSystems(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*FormalClientSystem, err error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Like("LOWER(codes)", dbutils.QuoteLikeKeyword(strings.ToLower(keyword)))
|
||||
}
|
||||
_, err = query.
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindSystemWithDataId 根据dataId查找操作系统信息
|
||||
func (this *FormalClientSystemDAO) FindSystemWithDataId(tx *dbs.Tx, dataId string) (*FormalClientSystem, error) {
|
||||
one, err := this.Query(tx).
|
||||
Attr("dataId", dataId).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*FormalClientSystem), nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package accounts
|
||||
package models_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
24
internal/db/models/formal_client_system_model.go
Normal file
24
internal/db/models/formal_client_system_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// FormalClientSystem 终端操作系统信息
|
||||
type FormalClientSystem struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 系统名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
DataId string `field:"dataId"` // 数据ID
|
||||
}
|
||||
|
||||
type FormalClientSystemOperator struct {
|
||||
Id any // ID
|
||||
Name any // 系统名称
|
||||
Codes any // 代号
|
||||
State any // 状态
|
||||
DataId any // 数据ID
|
||||
}
|
||||
|
||||
func NewFormalClientSystemOperator() *FormalClientSystemOperator {
|
||||
return &FormalClientSystemOperator{}
|
||||
}
|
||||
21
internal/db/models/formal_client_system_model_ext.go
Normal file
21
internal/db/models/formal_client_system_model_ext.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
)
|
||||
|
||||
// DecodeCodes 解析代号
|
||||
func (this *FormalClientSystem) DecodeCodes() []string {
|
||||
if IsNull(this.Codes) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var result = []string{}
|
||||
err := json.Unmarshal(this.Codes, &result)
|
||||
if err != nil {
|
||||
remotelogs.Error("FormalClientSystem.DecodeCodes", err.Error())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -51,6 +51,10 @@ var (
|
||||
accessLogRowsPerTable int64 = 500_000 // 自动分表的单表最大值
|
||||
)
|
||||
|
||||
func AccessLogQueuePercent() int {
|
||||
return accessLogQueuePercent
|
||||
}
|
||||
|
||||
type accessLogTableQuery struct {
|
||||
daoWrapper *HTTPAccessLogDAOWrapper
|
||||
name string
|
||||
@@ -131,13 +135,13 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.
|
||||
// 写入队列
|
||||
var queue = accessLogQueue // 这样写非常重要,防止在写入过程中队列有切换
|
||||
for _, accessLog := range accessLogs {
|
||||
if accessLog.FirewallPolicyId == 0 { // 如果是WAF记录,则采取采样率
|
||||
if accessLog.FirewallPolicyId == 0 { // 如果是非WAF记录,则采取采样率
|
||||
// 采样率
|
||||
if accessLogQueuePercent <= 0 {
|
||||
return nil
|
||||
}
|
||||
if accessLogQueuePercent < 100 && rands.Int(1, 100) > accessLogQueuePercent {
|
||||
return nil
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,16 +161,27 @@ func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(size int) (hasMore bool, e
|
||||
size = 100
|
||||
}
|
||||
|
||||
if len(oldAccessLogQueue) == 0 && len(accessLogQueue) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var dao = randomHTTPAccessLogDAO()
|
||||
if dao == nil {
|
||||
dao = &HTTPAccessLogDAOWrapper{
|
||||
DAO: SharedHTTPAccessLogDAO,
|
||||
NodeId: 0,
|
||||
}
|
||||
}
|
||||
|
||||
if len(oldAccessLogQueue) == 0 && len(accessLogQueue) == 0 {
|
||||
return false, nil
|
||||
// 检查本地数据库空间
|
||||
if dbutils.IsLocalDatabase && !dbutils.HasFreeSpace {
|
||||
return false, errors.New("dump accesslog failed: there is no enough space left for database (" + dbutils.LocalDatabaseDataDir + ")")
|
||||
}
|
||||
} else if dao.IsLocal {
|
||||
// 检查本地数据库空间
|
||||
// 我们假定本地只能安装一个数据库,访问日志中的数据库和当前API连接的数据库一致
|
||||
if !dbutils.HasFreeSpace {
|
||||
return true, errors.New("dump accesslog failed: there is no enough space left for database (" + dbutils.LocalDatabaseDataDir + ")")
|
||||
}
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
@@ -445,6 +460,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
||||
var requestPathReg = regexp.MustCompile(`requestPath:(\S+)`)
|
||||
var protoReg = regexp.MustCompile(`proto:(\S+)`)
|
||||
var schemeReg = regexp.MustCompile(`scheme:(\S+)`)
|
||||
var methodReg = regexp.MustCompile(`(?:method|requestMethod):(\S+)`)
|
||||
|
||||
var count = len(tableQueries)
|
||||
var wg = &sync.WaitGroup{}
|
||||
@@ -592,6 +608,11 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
||||
query.Where("JSON_EXTRACT(content, '$.requestURI') LIKE :keyword").
|
||||
Param("keyword", dbutils.QuoteLikePrefix("\""+u.RequestURI()))
|
||||
}
|
||||
} else if methodReg.MatchString(keyword) { // method|requestMethod:xxx
|
||||
isSpecialKeyword = true
|
||||
var matches = methodReg.FindStringSubmatch(keyword)
|
||||
query.Where("JSON_EXTRACT(content, '$.requestMethod')=:keyword").
|
||||
Param("keyword", strings.ToUpper(matches[1]))
|
||||
}
|
||||
if !isSpecialKeyword {
|
||||
if regexp.MustCompile(`^ip:.+`).MatchString(keyword) {
|
||||
|
||||
@@ -206,6 +206,7 @@ func (this *HTTPCacheTaskDAO) UpdateTaskStatus(tx *dbs.Tx, taskId int64, isDone
|
||||
|
||||
if isDone {
|
||||
op.DoneAt = time.Now().Unix()
|
||||
op.IsReady = true // 让后台列表能列出来
|
||||
}
|
||||
|
||||
return this.Save(tx, op)
|
||||
|
||||
@@ -182,19 +182,19 @@ func (this *HTTPHeaderPolicyDAO) ComposeHeaderPolicyConfig(tx *dbs.Tx, headerPol
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
config := &shared.HTTPHeaderPolicy{}
|
||||
var config = &shared.HTTPHeaderPolicy{}
|
||||
config.Id = int64(policy.Id)
|
||||
config.IsOn = policy.IsOn
|
||||
|
||||
// SetHeaders
|
||||
if IsNotNull(policy.SetHeaders) {
|
||||
refs := []*shared.HTTPHeaderRef{}
|
||||
var refs = []*shared.HTTPHeaderRef{}
|
||||
err = json.Unmarshal(policy.SetHeaders, &refs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(refs) > 0 {
|
||||
resultRefs := []*shared.HTTPHeaderRef{}
|
||||
var resultRefs = []*shared.HTTPHeaderRef{}
|
||||
for _, ref := range refs {
|
||||
headerConfig, err := SharedHTTPHeaderDAO.ComposeHeaderConfig(tx, ref.HeaderId)
|
||||
if err != nil {
|
||||
@@ -212,7 +212,7 @@ func (this *HTTPHeaderPolicyDAO) ComposeHeaderPolicyConfig(tx *dbs.Tx, headerPol
|
||||
|
||||
// Delete Headers
|
||||
if IsNotNull(policy.DeleteHeaders) {
|
||||
headers := []string{}
|
||||
var headers = []string{}
|
||||
err = json.Unmarshal(policy.DeleteHeaders, &headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -220,6 +220,16 @@ func (this *HTTPHeaderPolicyDAO) ComposeHeaderPolicyConfig(tx *dbs.Tx, headerPol
|
||||
config.DeleteHeaders = headers
|
||||
}
|
||||
|
||||
// CORS
|
||||
if IsNotNull(policy.Cors) {
|
||||
var corsConfig = &shared.HTTPCORSHeaderConfig{}
|
||||
err = json.Unmarshal(policy.Cors, corsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.CORS = corsConfig
|
||||
}
|
||||
|
||||
// Expires
|
||||
// TODO
|
||||
|
||||
@@ -235,6 +245,46 @@ func (this *HTTPHeaderPolicyDAO) FindHeaderPolicyIdWithHeaderId(tx *dbs.Tx, head
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// UpdateHeaderPolicyCORS 修改CORS
|
||||
func (this *HTTPHeaderPolicyDAO) UpdateHeaderPolicyCORS(tx *dbs.Tx, headerPolicyId int64, corsConfig *shared.HTTPCORSHeaderConfig) error {
|
||||
if headerPolicyId <= 0 {
|
||||
return errors.New("invalid headerId")
|
||||
}
|
||||
|
||||
corsJSON, err := json.Marshal(corsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = this.Query(tx).
|
||||
Pk(headerPolicyId).
|
||||
Set("cors", corsJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, headerPolicyId)
|
||||
}
|
||||
|
||||
// CheckUserHeaderPolicy 检查用户权限
|
||||
func (this *HTTPHeaderPolicyDAO) CheckUserHeaderPolicy(tx *dbs.Tx, userId int64, policyId int64) error {
|
||||
if userId <= 0 || policyId <= 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithHeaderPolicyId(tx, policyId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if webId <= 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
return SharedHTTPWebDAO.CheckUserWeb(tx, userId, webId)
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *HTTPHeaderPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
||||
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithHeaderPolicyId(tx, policyId)
|
||||
|
||||
@@ -2,7 +2,7 @@ package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
//
|
||||
// HTTPHeaderPolicy Header定义
|
||||
type HTTPHeaderPolicy struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
@@ -16,21 +16,23 @@ type HTTPHeaderPolicy struct {
|
||||
ReplaceHeaders dbs.JSON `field:"replaceHeaders"` // 替换Header内容
|
||||
Expires dbs.JSON `field:"expires"` // Expires单独设置
|
||||
DeleteHeaders dbs.JSON `field:"deleteHeaders"` // 删除的Headers
|
||||
Cors dbs.JSON `field:"cors"` // CORS配置
|
||||
}
|
||||
|
||||
type HTTPHeaderPolicyOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
State interface{} // 状态
|
||||
AdminId interface{} // 管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
CreatedAt interface{} // 创建时间
|
||||
AddHeaders interface{} // 添加的Header
|
||||
AddTrailers interface{} // 添加的Trailers
|
||||
SetHeaders interface{} // 设置Header
|
||||
ReplaceHeaders interface{} // 替换Header内容
|
||||
Expires interface{} // Expires单独设置
|
||||
DeleteHeaders interface{} // 删除的Headers
|
||||
Id any // ID
|
||||
IsOn any // 是否启用
|
||||
State any // 状态
|
||||
AdminId any // 管理员ID
|
||||
UserId any // 用户ID
|
||||
CreatedAt any // 创建时间
|
||||
AddHeaders any // 添加的Header
|
||||
AddTrailers any // 添加的Trailers
|
||||
SetHeaders any // 设置Header
|
||||
ReplaceHeaders any // 替换Header内容
|
||||
Expires any // Expires单独设置
|
||||
DeleteHeaders any // 删除的Headers
|
||||
Cors any // CORS配置
|
||||
}
|
||||
|
||||
func NewHTTPHeaderPolicyOperator() *HTTPHeaderPolicyOperator {
|
||||
|
||||
@@ -468,6 +468,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
||||
config.Referers = referersConfig
|
||||
}
|
||||
|
||||
// User-Agent
|
||||
if IsNotNull(web.UserAgent) {
|
||||
var userAgentConfig = serverconfigs.NewUserAgentConfig()
|
||||
err = json.Unmarshal(web.UserAgent, userAgentConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.UserAgent = userAgentConfig
|
||||
}
|
||||
|
||||
if cacheMap != nil {
|
||||
cacheMap.Put(cacheKey, config)
|
||||
}
|
||||
@@ -1252,6 +1262,35 @@ func (this *HTTPWebDAO) FindWebReferers(tx *dbs.Tx, webId int64) ([]byte, error)
|
||||
FindJSONCol()
|
||||
}
|
||||
|
||||
// UpdateWebUserAgent 修改User-Agent设置
|
||||
func (this *HTTPWebDAO) UpdateWebUserAgent(tx *dbs.Tx, webId int64, userAgentConfig *serverconfigs.UserAgentConfig) error {
|
||||
if userAgentConfig == nil {
|
||||
return nil
|
||||
}
|
||||
configJSON, err := json.Marshal(userAgentConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = this.Query(tx).
|
||||
Pk(webId).
|
||||
Set("userAgent", configJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, webId)
|
||||
}
|
||||
|
||||
// FindWebUserAgent 查找服务User-Agent配置
|
||||
func (this *HTTPWebDAO) FindWebUserAgent(tx *dbs.Tx, webId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(webId).
|
||||
Result("userAgent").
|
||||
FindJSONCol()
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
||||
// server
|
||||
|
||||
@@ -39,6 +39,7 @@ type HTTPWeb struct {
|
||||
RequestScripts dbs.JSON `field:"requestScripts"` // 请求脚本
|
||||
Uam dbs.JSON `field:"uam"` // UAM设置
|
||||
Referers dbs.JSON `field:"referers"` // 防盗链设置
|
||||
UserAgent dbs.JSON `field:"userAgent"` // UserAgent设置
|
||||
}
|
||||
|
||||
type HTTPWebOperator struct {
|
||||
@@ -77,6 +78,7 @@ type HTTPWebOperator struct {
|
||||
RequestScripts any // 请求脚本
|
||||
Uam any // UAM设置
|
||||
Referers any // 防盗链设置
|
||||
UserAgent any // UserAgent设置
|
||||
}
|
||||
|
||||
func NewHTTPWebOperator() *HTTPWebOperator {
|
||||
|
||||
@@ -353,7 +353,7 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
|
||||
}
|
||||
|
||||
// CountIPItemsWithListId 计算IP数量
|
||||
func (this *IPItemDAO) CountIPItemsWithListId(tx *dbs.Tx, listId int64, ipFrom string, ipTo string, keyword string, eventLevel string) (int64, error) {
|
||||
func (this *IPItemDAO) CountIPItemsWithListId(tx *dbs.Tx, listId int64, keyword string, ipFrom string, ipTo string, eventLevel string) (int64, error) {
|
||||
var query = this.Query(tx).
|
||||
State(IPItemStateEnabled).
|
||||
Attr("listId", listId)
|
||||
@@ -466,8 +466,11 @@ func (this *IPItemDAO) ExistsEnabledItem(tx *dbs.Tx, itemId int64) (bool, error)
|
||||
}
|
||||
|
||||
// CountAllEnabledIPItems 计算数量
|
||||
func (this *IPItemDAO) CountAllEnabledIPItems(tx *dbs.Tx, ip string, listId int64, unread bool, eventLevel string, listType string) (int64, error) {
|
||||
func (this *IPItemDAO) CountAllEnabledIPItems(tx *dbs.Tx, keyword string, ip string, listId int64, unread bool, eventLevel string, listType string) (int64, error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Like("ipFrom", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
if len(ip) > 0 {
|
||||
query.Attr("ipFrom", ip)
|
||||
}
|
||||
@@ -496,8 +499,11 @@ func (this *IPItemDAO) CountAllEnabledIPItems(tx *dbs.Tx, ip string, listId int6
|
||||
}
|
||||
|
||||
// ListAllEnabledIPItems 搜索所有IP
|
||||
func (this *IPItemDAO) ListAllEnabledIPItems(tx *dbs.Tx, ip string, listId int64, unread bool, eventLevel string, listType string, offset int64, size int64) (result []*IPItem, err error) {
|
||||
func (this *IPItemDAO) ListAllEnabledIPItems(tx *dbs.Tx, keyword string, ip string, listId int64, unread bool, eventLevel string, listType string, offset int64, size int64) (result []*IPItem, err error) {
|
||||
var query = this.Query(tx)
|
||||
if len(keyword) > 0 {
|
||||
query.Like("ipFrom", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
if len(ip) > 0 {
|
||||
query.Attr("ipFrom", ip)
|
||||
}
|
||||
@@ -608,7 +614,7 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
||||
return err
|
||||
}
|
||||
for _, clusterId := range clusterIds {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeIPItemChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeIPItemChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -616,7 +622,7 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
||||
} else {
|
||||
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
|
||||
for _, clusterId := range clusterIds {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeIPItemChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeIPItemChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -668,7 +674,7 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
||||
|
||||
if len(resultClusterIds) > 0 {
|
||||
for _, clusterId := range resultClusterIds {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeIPItemChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeIPItemChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -301,7 +301,7 @@ func (this *IPListDAO) NotifyUpdate(tx *dbs.Tx, listId int64, taskType NodeTaskT
|
||||
|
||||
if len(resultClusterIds) > 0 {
|
||||
for _, clusterId := range resultClusterIds {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, taskType)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, taskType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -154,19 +155,16 @@ func (this *MessageDAO) CreateNodeMessage(tx *dbs.Tx, role string, clusterId int
|
||||
|
||||
// CreateMessage 创建普通消息
|
||||
func (this *MessageDAO) CreateMessage(tx *dbs.Tx, adminId int64, userId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
|
||||
body = utils.LimitString(subject, 100)
|
||||
body = utils.LimitString(body, 1024)
|
||||
|
||||
var op = NewMessageOperator()
|
||||
op.AdminId = adminId
|
||||
op.UserId = userId
|
||||
op.Type = messageType
|
||||
op.Level = level
|
||||
|
||||
subjectRunes := []rune(subject)
|
||||
if len(subjectRunes) > 100 {
|
||||
op.Subject = string(subjectRunes[:100]) + "..."
|
||||
} else {
|
||||
op.Subject = subject
|
||||
}
|
||||
|
||||
op.Subject = subject
|
||||
op.Body = body
|
||||
if len(paramsJSON) > 0 {
|
||||
op.Params = paramsJSON
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -11,7 +12,7 @@ func TestMessageTaskDAO_FindSendingMessageTasks(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
var tx *dbs.Tx
|
||||
tasks, err := NewMessageTaskDAO().FindSendingMessageTasks(tx, 100)
|
||||
tasks, err := models.NewMessageTaskDAO().FindSendingMessageTasks(tx, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -20,3 +21,12 @@ func TestMessageTaskDAO_FindSendingMessageTasks(t *testing.T) {
|
||||
t.Log("task:", task.Id, "recipient:", task.RecipientId)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageTaskDAO_CleanExpiredMessageTasks(t *testing.T) {
|
||||
var dao = models.NewMessageTaskDAO()
|
||||
var tx *dbs.Tx
|
||||
err := dao.CleanExpiredMessageTasks(tx, 30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,7 +368,7 @@ func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64, isPublic bool)
|
||||
return err
|
||||
}
|
||||
for _, clusterId := range clusterIds {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeConfigChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -380,7 +380,7 @@ func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64, isPublic bool)
|
||||
return err
|
||||
}
|
||||
for _, clusterId := range clusterIds {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeConfigChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
73
internal/db/models/nameservers/ns_record_dao.go
Normal file
73
internal/db/models/nameservers/ns_record_dao.go
Normal file
@@ -0,0 +1,73 @@
|
||||
//go:build !plus
|
||||
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NSRecordStateEnabled = 1 // 已启用
|
||||
NSRecordStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSRecordDAO dbs.DAO
|
||||
|
||||
func NewNSRecordDAO() *NSRecordDAO {
|
||||
return dbs.NewDAO(&NSRecordDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSRecords",
|
||||
Model: new(NSRecord),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSRecordDAO)
|
||||
}
|
||||
|
||||
var SharedNSRecordDAO *NSRecordDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSRecordDAO = NewNSRecordDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNSRecord 启用条目
|
||||
func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, id uint64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSRecordStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableNSRecord 禁用条目
|
||||
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id uint64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSRecordStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledNSRecord 查找启用中的条目
|
||||
func (this *NSRecordDAO) FindEnabledNSRecord(tx *dbs.Tx, id uint64) (*NSRecord, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
State(NSRecordStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NSRecord), err
|
||||
}
|
||||
|
||||
// FindNSRecordName 根据主键查找名称
|
||||
func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id uint64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_record_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_record_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -11,28 +11,40 @@ type NSRecord struct {
|
||||
Name string `field:"name"` // 记录名
|
||||
Type string `field:"type"` // 类型
|
||||
Value string `field:"value"` // 值
|
||||
MxPriority uint32 `field:"mxPriority"` // MX优先级
|
||||
SrvPriority uint32 `field:"srvPriority"` // SRV优先级
|
||||
SrvWeight uint32 `field:"srvWeight"` // SRV权重
|
||||
SrvPort uint32 `field:"srvPort"` // SRV端口
|
||||
CaaFlag uint8 `field:"caaFlag"` // CAA Flag
|
||||
CaaTag string `field:"caaTag"` // CAA TAG
|
||||
Ttl uint32 `field:"ttl"` // TTL(秒)
|
||||
Weight uint32 `field:"weight"` // 权重
|
||||
RouteIds dbs.JSON `field:"routeIds"` // 线路
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Version uint64 `field:"version"` //
|
||||
Version uint64 `field:"version"` // 版本号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSRecordOperator struct {
|
||||
Id interface{} // ID
|
||||
DomainId interface{} // 域名ID
|
||||
IsOn interface{} // 是否启用
|
||||
Description interface{} // 备注
|
||||
Name interface{} // 记录名
|
||||
Type interface{} // 类型
|
||||
Value interface{} // 值
|
||||
Ttl interface{} // TTL(秒)
|
||||
Weight interface{} // 权重
|
||||
RouteIds interface{} // 线路
|
||||
CreatedAt interface{} // 创建时间
|
||||
Version interface{} //
|
||||
State interface{} // 状态
|
||||
Id any // ID
|
||||
DomainId any // 域名ID
|
||||
IsOn any // 是否启用
|
||||
Description any // 备注
|
||||
Name any // 记录名
|
||||
Type any // 类型
|
||||
Value any // 值
|
||||
MxPriority any // MX优先级
|
||||
SrvPriority any // SRV优先级
|
||||
SrvWeight any // SRV权重
|
||||
SrvPort any // SRV端口
|
||||
CaaFlag any // CAA Flag
|
||||
CaaTag any // CAA TAG
|
||||
Ttl any // TTL(秒)
|
||||
Weight any // 权重
|
||||
RouteIds any // 线路
|
||||
CreatedAt any // 创建时间
|
||||
Version any // 版本号
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewNSRecordOperator() *NSRecordOperator {
|
||||
|
||||
33
internal/db/models/nameservers/ns_route_category_dao.go
Normal file
33
internal/db/models/nameservers/ns_route_category_dao.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NSRouteCategoryStateEnabled = 1 // 已启用
|
||||
NSRouteCategoryStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSRouteCategoryDAO dbs.DAO
|
||||
|
||||
func NewNSRouteCategoryDAO() *NSRouteCategoryDAO {
|
||||
return dbs.NewDAO(&NSRouteCategoryDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSRouteCategories",
|
||||
Model: new(NSRouteCategory),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSRouteCategoryDAO)
|
||||
}
|
||||
|
||||
var SharedNSRouteCategoryDAO *NSRouteCategoryDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSRouteCategoryDAO = NewNSRouteCategoryDAO()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package nameservers_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
26
internal/db/models/nameservers/ns_route_category_model.go
Normal file
26
internal/db/models/nameservers/ns_route_category_model.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package nameservers
|
||||
|
||||
// NSRouteCategory 线路分类
|
||||
type NSRouteCategory struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Name string `field:"name"` // 分类名
|
||||
AdminId uint64 `field:"adminId"` // 管理员ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
Order uint32 `field:"order"` // 排序
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSRouteCategoryOperator struct {
|
||||
Id any // ID
|
||||
IsOn any // 是否启用
|
||||
Name any // 分类名
|
||||
AdminId any // 管理员ID
|
||||
UserId any // 用户ID
|
||||
Order any // 排序
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewNSRouteCategoryOperator() *NSRouteCategoryOperator {
|
||||
return &NSRouteCategoryOperator{}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
73
internal/db/models/nameservers/ns_route_dao.go
Normal file
73
internal/db/models/nameservers/ns_route_dao.go
Normal file
@@ -0,0 +1,73 @@
|
||||
//go:build !plus
|
||||
|
||||
package nameservers
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NSRouteStateEnabled = 1 // 已启用
|
||||
NSRouteStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type NSRouteDAO dbs.DAO
|
||||
|
||||
func NewNSRouteDAO() *NSRouteDAO {
|
||||
return dbs.NewDAO(&NSRouteDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSRoutes",
|
||||
Model: new(NSRoute),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSRouteDAO)
|
||||
}
|
||||
|
||||
var SharedNSRouteDAO *NSRouteDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSRouteDAO = NewNSRouteDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNSRoute 启用条目
|
||||
func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSRouteStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableNSRoute 禁用条目
|
||||
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NSRouteStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledNSRoute 查找启用中的条目
|
||||
func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id uint32) (*NSRoute, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
State(NSRouteStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NSRoute), err
|
||||
}
|
||||
|
||||
// FindNSRouteName 根据主键查找名称
|
||||
func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
6
internal/db/models/nameservers/ns_route_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_route_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package nameservers_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -4,31 +4,39 @@ import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// NSRoute DNS线路
|
||||
type NSRoute struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
DomainId uint32 `field:"domainId"` // 域名ID
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
Name string `field:"name"` // 名称
|
||||
Ranges dbs.JSON `field:"ranges"` // 范围
|
||||
Order uint32 `field:"order"` // 排序
|
||||
Version uint64 `field:"version"` // 版本号
|
||||
Code string `field:"code"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
CategoryId uint32 `field:"categoryId"` // 分类ID
|
||||
DomainId uint64 `field:"domainId"` // 域名ID
|
||||
AdminId uint64 `field:"adminId"` // 管理员ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
IsPublic bool `field:"isPublic"` // 是否公用(管理员创建的线路)
|
||||
Name string `field:"name"` // 名称
|
||||
Ranges dbs.JSON `field:"ranges"` // 范围
|
||||
Order uint32 `field:"order"` // 排序
|
||||
Version uint64 `field:"version"` // 版本号
|
||||
Priority uint32 `field:"priority"` // 优先级,越高越优先
|
||||
Code string `field:"code"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSRouteOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
ClusterId interface{} // 集群ID
|
||||
DomainId interface{} // 域名ID
|
||||
UserId interface{} // 用户ID
|
||||
Name interface{} // 名称
|
||||
Ranges interface{} // 范围
|
||||
Order interface{} // 排序
|
||||
Version interface{} // 版本号
|
||||
Code interface{} // 代号
|
||||
State interface{} // 状态
|
||||
Id any // ID
|
||||
IsOn any // 是否启用
|
||||
ClusterId any // 集群ID
|
||||
CategoryId any // 分类ID
|
||||
DomainId any // 域名ID
|
||||
AdminId any // 管理员ID
|
||||
UserId any // 用户ID
|
||||
IsPublic any // 是否公用(管理员创建的线路)
|
||||
Name any // 名称
|
||||
Ranges any // 范围
|
||||
Order any // 排序
|
||||
Version any // 版本号
|
||||
Priority any // 优先级,越高越优先
|
||||
Code any // 代号
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewNSRouteOperator() *NSRouteOperator {
|
||||
|
||||
@@ -198,7 +198,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
||||
}
|
||||
|
||||
// UpdateCluster 修改集群
|
||||
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool, clockConfig *nodeconfigs.ClockConfig, autoRemoteStart bool, autoInstallTables bool) error {
|
||||
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool, clockConfig *nodeconfigs.ClockConfig, autoRemoteStart bool, autoInstallTables bool, sshParams *nodeconfigs.SSHParams) error {
|
||||
if clusterId <= 0 {
|
||||
return errors.New("invalid clusterId")
|
||||
}
|
||||
@@ -226,6 +226,14 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
|
||||
op.AutoRemoteStart = autoRemoteStart
|
||||
op.AutoInstallNftables = autoInstallTables
|
||||
|
||||
if sshParams != nil {
|
||||
sshParamsJSON, err := json.Marshal(sshParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.SshParams = sshParamsJSON
|
||||
}
|
||||
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -429,7 +437,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSDomainId(tx *dbs.Tx, dn
|
||||
_, err = this.Query(tx).
|
||||
State(NodeClusterStateEnabled).
|
||||
Attr("dnsDomainId", dnsDomainId).
|
||||
Result("id", "name", "dnsName", "dnsDomainId", "isOn").
|
||||
Result("id", "name", "dnsName", "dnsDomainId", "isOn", "dns").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
@@ -454,6 +462,27 @@ func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindClusterSSHParams 查找集群的SSH默认参数
|
||||
func (this *NodeClusterDAO) FindClusterSSHParams(tx *dbs.Tx, clusterId int64) (*nodeconfigs.SSHParams, error) {
|
||||
sshParamsJSON, err := this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("sshParams").
|
||||
FindJSONCol()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var params = nodeconfigs.DefaultSSHParams()
|
||||
if len(sshParamsJSON) == 0 {
|
||||
return params, nil
|
||||
}
|
||||
err = json.Unmarshal(sshParamsJSON, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// FindClusterDNSInfo 查找DNS信息
|
||||
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
|
||||
var cacheKey = this.Table + ":FindClusterDNSInfo:" + types.String(clusterId)
|
||||
@@ -967,7 +996,7 @@ func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, ca
|
||||
cluster, err := this.Query(tx).
|
||||
Pk(clusterId).
|
||||
State(NodeClusterStateEnabled).
|
||||
Result("id", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection", "clock", "globalServerConfig", "autoInstallNftables").
|
||||
Result("id", "name", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection", "clock", "globalServerConfig", "autoInstallNftables").
|
||||
Find()
|
||||
if err != nil || cluster == nil {
|
||||
return nil, err
|
||||
@@ -1132,7 +1161,7 @@ func (this *NodeClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId in
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeDDosProtectionChanged)
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeDDosProtectionChanged)
|
||||
}
|
||||
|
||||
// FindClusterGlobalServerConfig 查询全局服务配置
|
||||
@@ -1175,12 +1204,12 @@ func (this *NodeClusterDAO) UpdateClusterGlobalServerConfig(tx *dbs.Tx, clusterI
|
||||
return err
|
||||
}
|
||||
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeGlobalServerConfigChanged)
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeGlobalServerConfigChanged)
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeConfigChanged)
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeConfigChanged)
|
||||
}
|
||||
|
||||
// NotifyDNSUpdate 通知DNS更新
|
||||
|
||||
@@ -177,5 +177,5 @@ func (this *NodeClusterMetricItemDAO) ExistsClusterItem(tx *dbs.Tx, clusterId in
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *NodeClusterMetricItemDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeConfigChanged)
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeConfigChanged)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ type NodeCluster struct {
|
||||
Order uint32 `field:"order"` // 排序
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
GrantId uint32 `field:"grantId"` // 默认认证方式
|
||||
SshParams dbs.JSON `field:"sshParams"` // SSH默认参数
|
||||
State uint8 `field:"state"` // 状态
|
||||
AutoRegister uint8 `field:"autoRegister"` // 是否开启自动注册
|
||||
UniqueId string `field:"uniqueId"` // 唯一ID
|
||||
@@ -53,6 +54,7 @@ type NodeClusterOperator struct {
|
||||
Order any // 排序
|
||||
CreatedAt any // 创建时间
|
||||
GrantId any // 默认认证方式
|
||||
SshParams any // SSH默认参数
|
||||
State any // 状态
|
||||
AutoRegister any // 是否开启自动注册
|
||||
UniqueId any // 唯一ID
|
||||
|
||||
@@ -13,17 +13,9 @@ import (
|
||||
func (this *NodeCluster) DecodeDNSConfig() (*dnsconfigs.ClusterDNSConfig, error) {
|
||||
if len(this.Dns) == 0 {
|
||||
// 一定要返回一个默认的值,防止产生nil
|
||||
return &dnsconfigs.ClusterDNSConfig{
|
||||
NodesAutoSync: false,
|
||||
ServersAutoSync: false,
|
||||
CNAMEAsDomain: true,
|
||||
IncludingLnNodes: true,
|
||||
}, nil
|
||||
}
|
||||
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
||||
CNAMEAsDomain: true,
|
||||
IncludingLnNodes: true,
|
||||
return dnsconfigs.DefaultClusterDNSConfig(), nil
|
||||
}
|
||||
var dnsConfig = dnsconfigs.DefaultClusterDNSConfig()
|
||||
err := json.Unmarshal(this.Dns, &dnsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/sizes"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
@@ -35,8 +36,6 @@ const (
|
||||
NodeStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
var nodeIdCacheMap = map[string]int64{} // uniqueId => nodeId
|
||||
|
||||
type NodeDAO dbs.DAO
|
||||
|
||||
func NewNodeDAO() *NodeDAO {
|
||||
@@ -77,9 +76,7 @@ func (this *NodeDAO) DisableNode(tx *dbs.Tx, nodeId int64) (err error) {
|
||||
return err
|
||||
}
|
||||
if len(uniqueId) > 0 {
|
||||
SharedCacheLocker.Lock()
|
||||
delete(nodeIdCacheMap, uniqueId)
|
||||
SharedCacheLocker.Unlock()
|
||||
ttlcache.SharedCache.Delete("nodeId@uniqueId@" + uniqueId)
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
@@ -167,6 +164,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
|
||||
op.GroupId = groupId
|
||||
op.RegionId = regionId
|
||||
op.IsOn = 1
|
||||
op.EnableIPLists = 1
|
||||
op.State = NodeStateEnabled
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
@@ -190,7 +188,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
|
||||
}
|
||||
|
||||
// UpdateNode 修改节点
|
||||
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, isOn bool, level int, lnAddrs []string) error {
|
||||
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, isOn bool, level int, lnAddrs []string, enableIPLists bool) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
@@ -250,6 +248,8 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId
|
||||
op.LnAddrs = lnAddrsJSON
|
||||
}
|
||||
|
||||
op.EnableIPLists = enableIPLists
|
||||
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -949,17 +949,19 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
||||
}
|
||||
|
||||
var config = &nodeconfigs.NodeConfig{
|
||||
Id: int64(node.Id),
|
||||
NodeId: node.UniqueId,
|
||||
Secret: node.Secret,
|
||||
IsOn: node.IsOn,
|
||||
Servers: nil,
|
||||
Version: int64(node.Version),
|
||||
Name: node.Name,
|
||||
MaxCPU: types.Int32(node.MaxCPU),
|
||||
RegionId: int64(node.RegionId),
|
||||
Level: types.Int32(node.Level),
|
||||
GroupId: int64(node.GroupId),
|
||||
Id: int64(node.Id),
|
||||
NodeId: node.UniqueId,
|
||||
Secret: node.Secret,
|
||||
IsOn: node.IsOn,
|
||||
Servers: nil,
|
||||
Version: int64(node.Version),
|
||||
Name: node.Name,
|
||||
MaxCPU: types.Int32(node.MaxCPU),
|
||||
RegionId: int64(node.RegionId),
|
||||
Level: types.Int32(node.Level),
|
||||
GroupId: int64(node.GroupId),
|
||||
EnableIPLists: node.EnableIPLists,
|
||||
APINodeAddrs: node.DecodeAPINodeAddrs(),
|
||||
}
|
||||
|
||||
// API节点IP
|
||||
@@ -976,7 +978,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
||||
}
|
||||
|
||||
for _, server := range servers {
|
||||
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true)
|
||||
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, false, cacheMap, true, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1145,6 +1147,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
||||
}
|
||||
|
||||
config.CacheDiskDir = node.CacheDiskDir
|
||||
config.CacheDiskSubDirs = node.DecodeCacheDiskSubDirs()
|
||||
|
||||
// TOA
|
||||
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId, cacheMap)
|
||||
@@ -1279,36 +1282,39 @@ func (this *NodeDAO) UpdateNodeConnectedAPINodes(tx *dbs.Tx, nodeId int64, apiNo
|
||||
|
||||
// FindEnabledNodeIdWithUniqueId 根据UniqueId获取ID
|
||||
func (this *NodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(NodeStateEnabled).
|
||||
Attr("uniqueId", uniqueId).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindEnabledNodeIdWithUniqueIdCacheable 根据UniqueId获取ID,并可以使用缓存
|
||||
func (this *NodeDAO) FindEnabledNodeIdWithUniqueIdCacheable(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||
SharedCacheLocker.RLock()
|
||||
nodeId, ok := nodeIdCacheMap[uniqueId]
|
||||
if ok {
|
||||
SharedCacheLocker.RUnlock()
|
||||
return nodeId, nil
|
||||
var cacheKey = "nodeId@uniqueId@" + uniqueId
|
||||
var item = ttlcache.SharedCache.Read(cacheKey)
|
||||
if item != nil {
|
||||
return types.Int64(item.Value), nil
|
||||
}
|
||||
SharedCacheLocker.RUnlock()
|
||||
nodeId, err := this.Query(tx).
|
||||
|
||||
one, err := this.Query(tx).
|
||||
State(NodeStateEnabled).
|
||||
Attr("uniqueId", uniqueId).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
Result("id", "clusterId").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 检查集群
|
||||
var node = one.(*Node)
|
||||
var clusterId = int64(node.ClusterId)
|
||||
if clusterId <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
isOn, err := SharedNodeClusterDAO.CheckNodeClusterIsOn(tx, clusterId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if nodeId > 0 {
|
||||
SharedCacheLocker.Lock()
|
||||
nodeIdCacheMap[uniqueId] = nodeId
|
||||
SharedCacheLocker.Unlock()
|
||||
if !isOn {
|
||||
return 0, nil
|
||||
}
|
||||
return nodeId, nil
|
||||
|
||||
ttlcache.SharedCache.Write(cacheKey, int64(node.Id), time.Now().Unix()+60)
|
||||
|
||||
return int64(node.Id), nil
|
||||
}
|
||||
|
||||
// CountAllEnabledNodesWithGrantId 计算使用某个认证的节点数量
|
||||
@@ -1417,6 +1423,49 @@ func (this *NodeDAO) CountAllEnabledNodesWithRegionId(tx *dbs.Tx, regionId int64
|
||||
Count()
|
||||
}
|
||||
|
||||
// CountAllNodeRegionInfo 查找所有节点区域信息数量
|
||||
func (this *NodeDAO) CountAllNodeRegionInfo(tx *dbs.Tx, regionId int64) (int64, error) {
|
||||
var query = this.Query(tx).
|
||||
State(NodeStateEnabled).
|
||||
Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)")
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// ListNodeRegionInfo 列出节点区域信息
|
||||
func (this *NodeDAO) ListNodeRegionInfo(tx *dbs.Tx, regionId int64, offset int64, size int64) (result []*Node, err error) {
|
||||
var query = this.Query(tx).
|
||||
Result("id", "name", "clusterId", "regionId").
|
||||
State(NodeStateEnabled).
|
||||
Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)").
|
||||
Asc("IF(regionId=0, 0, 1)"). // 按照 regionId 排序是为了让没有设置区域的节点排在最上面
|
||||
DescPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result)
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
}
|
||||
_, err = query.FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateNodeRegionId 修改节点所在区域
|
||||
func (this *NodeDAO) UpdateNodeRegionId(tx *dbs.Tx, nodeId int64, regionId int64) error {
|
||||
// 这里允许 regionId 为 0
|
||||
err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("regionId", regionId).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, nodeId)
|
||||
}
|
||||
|
||||
// FindAllEnabledNodesDNSWithClusterId 获取一个集群的节点DNS信息
|
||||
func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, includingLnNodes bool) (result []*Node, err error) {
|
||||
if clusterId <= 0 {
|
||||
@@ -1574,7 +1623,7 @@ func (this *NodeDAO) UpdateNodeSystem(tx *dbs.Tx, nodeId int64, maxCPU int32) er
|
||||
}
|
||||
|
||||
// UpdateNodeCache 设置缓存相关
|
||||
func (this *NodeDAO) UpdateNodeCache(tx *dbs.Tx, nodeId int64, maxCacheDiskCapacityJSON []byte, maxCacheMemoryCapacityJSON []byte, cacheDiskDir string) error {
|
||||
func (this *NodeDAO) UpdateNodeCache(tx *dbs.Tx, nodeId int64, maxCacheDiskCapacityJSON []byte, maxCacheMemoryCapacityJSON []byte, cacheDiskDir string, cacheDiskSubDirs []*serverconfigs.CacheDir) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
@@ -1587,7 +1636,17 @@ func (this *NodeDAO) UpdateNodeCache(tx *dbs.Tx, nodeId int64, maxCacheDiskCapac
|
||||
op.MaxCacheMemoryCapacity = maxCacheMemoryCapacityJSON
|
||||
}
|
||||
op.CacheDiskDir = cacheDiskDir
|
||||
err := this.Save(tx, op)
|
||||
|
||||
if cacheDiskSubDirs == nil {
|
||||
cacheDiskSubDirs = []*serverconfigs.CacheDir{}
|
||||
}
|
||||
cacheDiskSubDirsJSON, err := json.Marshal(cacheDiskSubDirs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.CacheDiskSubDirs = cacheDiskSubDirsJSON
|
||||
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1780,6 +1839,12 @@ func (this *NodeDAO) DeleteNodeFromCluster(tx *dbs.Tx, nodeId int64, clusterId i
|
||||
return nil
|
||||
}
|
||||
|
||||
// 提前通知DNS更新,因为后面集群会有变化
|
||||
err = this.NotifyDNSUpdate(tx, nodeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var node = one.(*Node)
|
||||
|
||||
var secondaryClusterIds = []int64{}
|
||||
@@ -1975,11 +2040,51 @@ func (this *NodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProt
|
||||
return err
|
||||
}
|
||||
if clusterId > 0 {
|
||||
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, 0, NodeTaskTypeDDosProtectionChanged, 0)
|
||||
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, 0, 0, NodeTaskTypeDDosProtectionChanged)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindNodeAPIConfig 查找API相关配置信息
|
||||
func (this *NodeDAO) FindNodeAPIConfig(tx *dbs.Tx, nodeId int64) (*Node, error) {
|
||||
if nodeId <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
one, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Result("apiNodeAddrs").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
return one.(*Node), nil
|
||||
}
|
||||
|
||||
// UpdateNodeAPIConfig 修改API相关配置信息
|
||||
func (this *NodeDAO) UpdateNodeAPIConfig(tx *dbs.Tx, nodeId int64, apiNodeAddrs []*serverconfigs.NetworkAddressConfig) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
|
||||
if apiNodeAddrs == nil {
|
||||
apiNodeAddrs = []*serverconfigs.NetworkAddressConfig{}
|
||||
}
|
||||
apiNodeAddrsJSON, err := json.Marshal(apiNodeAddrs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var op = NewNodeOperator()
|
||||
op.Id = nodeId
|
||||
op.ApiNodeAddrs = apiNodeAddrsJSON
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, nodeId)
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知节点相关更新
|
||||
func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||
// 这里只需要通知单个集群即可,因为节点是公用的,更新一个就相当于更新了所有
|
||||
@@ -1987,7 +2092,7 @@ func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, 0, NodeTaskTypeConfigChanged, 0)
|
||||
err = SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -2003,7 +2108,7 @@ func (this *NodeDAO) NotifyLevelUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||
return err
|
||||
}
|
||||
for _, clusterId := range clusterIds {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeNodeLevelChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeNodeLevelChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -2028,7 +2133,7 @@ func (this *NodeDAO) NotifyDNSUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||
if len(dnsInfo.DnsName) == 0 || dnsInfo.DnsDomainId <= 0 {
|
||||
continue
|
||||
}
|
||||
err = dns.SharedDNSTaskDAO.CreateNodeTask(tx, nodeId, dns.DNSTaskTypeNodeChange)
|
||||
err = dns.SharedDNSTaskDAO.CreateNodeTask(tx, clusterId, nodeId, dns.DNSTaskTypeNodeChange)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -77,3 +77,34 @@ func TestNodeDAO_ComposeNodeConfig_ParentNodes(t *testing.T) {
|
||||
}
|
||||
logs.PrintAsJSON(nodeConfig.ParentNodes, t)
|
||||
}
|
||||
|
||||
func TestNodeDAO_FindEnabledNodeIdWithUniqueId(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
var tx *dbs.Tx
|
||||
// init
|
||||
{
|
||||
_, err := models.SharedNodeDAO.FindEnabledNodeIdWithUniqueId(tx, "a186380dbd26ccd49e75d178ec59df1b")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var before = time.Now()
|
||||
nodeId, err := models.SharedNodeDAO.FindEnabledNodeIdWithUniqueId(tx, "a186380dbd26ccd49e75d178ec59df1b")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
|
||||
t.Log("nodeId:", nodeId)
|
||||
|
||||
{
|
||||
before = time.Now()
|
||||
nodeId, err := models.SharedNodeDAO.FindEnabledNodeIdWithUniqueId(tx, "a186380dbd26ccd49e75d178ec59df1b")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
|
||||
t.Log("nodeId:", nodeId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -604,7 +604,7 @@ func (this *NodeIPAddressDAO) NotifyUpdate(tx *dbs.Tx, addressId int64) error {
|
||||
var role = address.(*NodeIPAddress).Role
|
||||
switch role {
|
||||
case nodeconfigs.NodeRoleNode:
|
||||
err = dns.SharedDNSTaskDAO.CreateNodeTask(tx, nodeId, dns.DNSTaskTypeNodeChange)
|
||||
err = dns.SharedDNSTaskDAO.CreateNodeTask(tx, 0, nodeId, dns.DNSTaskTypeNodeChange)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -38,8 +38,11 @@ type Node struct {
|
||||
DnsRoutes dbs.JSON `field:"dnsRoutes"` // DNS线路设置
|
||||
MaxCacheDiskCapacity dbs.JSON `field:"maxCacheDiskCapacity"` // 硬盘缓存容量
|
||||
MaxCacheMemoryCapacity dbs.JSON `field:"maxCacheMemoryCapacity"` // 内存缓存容量
|
||||
CacheDiskDir string `field:"cacheDiskDir"` // 缓存目录
|
||||
CacheDiskDir string `field:"cacheDiskDir"` // 主缓存目录
|
||||
CacheDiskSubDirs dbs.JSON `field:"cacheDiskSubDirs"` // 其他缓存目录
|
||||
DnsResolver dbs.JSON `field:"dnsResolver"` // DNS解析器
|
||||
EnableIPLists bool `field:"enableIPLists"` // 启用IP名单
|
||||
ApiNodeAddrs dbs.JSON `field:"apiNodeAddrs"` // API节点地址
|
||||
}
|
||||
|
||||
type NodeOperator struct {
|
||||
@@ -77,8 +80,11 @@ type NodeOperator struct {
|
||||
DnsRoutes any // DNS线路设置
|
||||
MaxCacheDiskCapacity any // 硬盘缓存容量
|
||||
MaxCacheMemoryCapacity any // 内存缓存容量
|
||||
CacheDiskDir any // 缓存目录
|
||||
CacheDiskDir any // 主缓存目录
|
||||
CacheDiskSubDirs any // 其他缓存目录
|
||||
DnsResolver any // DNS解析器
|
||||
EnableIPLists any // 启用IP名单
|
||||
ApiNodeAddrs any // API节点地址
|
||||
}
|
||||
|
||||
func NewNodeOperator() *NodeOperator {
|
||||
|
||||
@@ -2,7 +2,9 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"sort"
|
||||
@@ -135,6 +137,7 @@ func (this *Node) HasDDoSProtection() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// DecodeMaxCacheDiskCapacity 解析硬盘容量
|
||||
func (this *Node) DecodeMaxCacheDiskCapacity() *shared.SizeCapacity {
|
||||
if this.MaxCacheDiskCapacity.IsNull() {
|
||||
return nil
|
||||
@@ -145,6 +148,7 @@ func (this *Node) DecodeMaxCacheDiskCapacity() *shared.SizeCapacity {
|
||||
return capacity
|
||||
}
|
||||
|
||||
// DecodeMaxCacheMemoryCapacity 解析内存容量
|
||||
func (this *Node) DecodeMaxCacheMemoryCapacity() *shared.SizeCapacity {
|
||||
if this.MaxCacheMemoryCapacity.IsNull() {
|
||||
return nil
|
||||
@@ -169,6 +173,7 @@ func (this *Node) DecodeDNSResolver() *nodeconfigs.DNSResolverConfig {
|
||||
return resolverConfig
|
||||
}
|
||||
|
||||
// DecodeLnAddrs 解析Ln地址
|
||||
func (this *Node) DecodeLnAddrs() []string {
|
||||
if IsNull(this.LnAddrs) {
|
||||
return nil
|
||||
@@ -181,3 +186,31 @@ func (this *Node) DecodeLnAddrs() []string {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// DecodeCacheDiskSubDirs 解析缓存目录
|
||||
func (this *Node) DecodeCacheDiskSubDirs() []*serverconfigs.CacheDir {
|
||||
if IsNull(this.CacheDiskSubDirs) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var result = []*serverconfigs.CacheDir{}
|
||||
err := json.Unmarshal(this.CacheDiskSubDirs, &result)
|
||||
if err != nil {
|
||||
remotelogs.Error("Node.DecodeCacheDiskSubDirs", err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// DecodeAPINodeAddrs 解析API节点地址
|
||||
func (this *Node) DecodeAPINodeAddrs() []*serverconfigs.NetworkAddressConfig {
|
||||
var result = []*serverconfigs.NetworkAddressConfig{}
|
||||
if IsNull(this.ApiNodeAddrs) {
|
||||
return result
|
||||
}
|
||||
|
||||
err := json.Unmarshal(this.ApiNodeAddrs, &result)
|
||||
if err != nil {
|
||||
remotelogs.Error("Node.DecodeAPINodeAddrs", err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
NodePriceItemStateEnabled = 1 // 已启用
|
||||
NodePriceItemStateDisabled = 0 // 已禁用
|
||||
|
||||
NodePriceTypeTraffic = "traffic" // 价格类型之流量
|
||||
)
|
||||
|
||||
type NodePriceItemDAO dbs.DAO
|
||||
|
||||
func NewNodePriceItemDAO() *NodePriceItemDAO {
|
||||
return dbs.NewDAO(&NodePriceItemDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNodePriceItems",
|
||||
Model: new(NodePriceItem),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NodePriceItemDAO)
|
||||
}
|
||||
|
||||
var SharedNodePriceItemDAO *NodePriceItemDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNodePriceItemDAO = NewNodePriceItemDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableNodePriceItem 启用条目
|
||||
func (this *NodePriceItemDAO) EnableNodePriceItem(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NodePriceItemStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableNodePriceItem 禁用条目
|
||||
func (this *NodePriceItemDAO) DisableNodePriceItem(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", NodePriceItemStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledNodePriceItem 查找启用中的条目
|
||||
func (this *NodePriceItemDAO) FindEnabledNodePriceItem(tx *dbs.Tx, id int64) (*NodePriceItem, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", NodePriceItemStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*NodePriceItem), err
|
||||
}
|
||||
|
||||
// FindNodePriceItemName 根据主键查找名称
|
||||
func (this *NodePriceItemDAO) FindNodePriceItemName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateItem 创建价格
|
||||
func (this *NodePriceItemDAO) CreateItem(tx *dbs.Tx, name string, itemType string, bitsFrom, bitsTo int64) (int64, error) {
|
||||
var op = NewNodePriceItemOperator()
|
||||
op.Name = name
|
||||
op.Type = itemType
|
||||
op.BitsFrom = bitsFrom
|
||||
op.BitsTo = bitsTo
|
||||
op.IsOn = true
|
||||
op.State = NodePriceItemStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdateItem 修改价格
|
||||
func (this *NodePriceItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, bitsFrom, bitsTo int64) error {
|
||||
if itemId <= 0 {
|
||||
return errors.New("invalid itemId")
|
||||
}
|
||||
var op = NewNodePriceItemOperator()
|
||||
op.Id = itemId
|
||||
op.Name = name
|
||||
op.BitsFrom = bitsFrom
|
||||
op.BitsTo = bitsTo
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// FindAllEnabledRegionPrices 列出某个区域的所有价格
|
||||
func (this *NodePriceItemDAO) FindAllEnabledRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Attr("type", priceType).
|
||||
State(NodePriceItemStateEnabled).
|
||||
Asc("bitsFrom").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllEnabledAndOnRegionPrices 列出某个区域的所有启用的价格
|
||||
func (this *NodePriceItemDAO) FindAllEnabledAndOnRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Attr("type", priceType).
|
||||
State(NodePriceItemStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Asc("bitsFrom").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// SearchItemsWithBytes 根据字节查找付费项目
|
||||
func (this *NodePriceItemDAO) SearchItemsWithBytes(items []*NodePriceItem, bytes int64) int64 {
|
||||
bytes *= 8
|
||||
|
||||
for _, item := range items {
|
||||
if bytes >= int64(item.BitsFrom) && (bytes < int64(item.BitsTo) || item.BitsTo == 0) {
|
||||
return int64(item.Id)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
@@ -120,8 +120,8 @@ func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*Nod
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllEnabledAndOnRegions 列出所有启用的区域
|
||||
func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
||||
// FindAllAvailableRegions 列出所有启用的区域
|
||||
func (this *NodeRegionDAO) FindAllAvailableRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(NodeRegionStateEnabled).
|
||||
Attr("isOn", true).
|
||||
@@ -132,6 +132,25 @@ func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*Nod
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllRegionIds 查找所有区域ID
|
||||
func (this *NodeRegionDAO) FindAllRegionIds(tx *dbs.Tx) ([]int64, error) {
|
||||
ones, err := this.Query(tx).
|
||||
ResultPk().
|
||||
State(NodeRegionStateEnabled).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var regionIds = []int64{}
|
||||
for _, one := range ones {
|
||||
regionIds = append(regionIds, int64(one.(*NodeRegion).Id))
|
||||
}
|
||||
return regionIds, nil
|
||||
}
|
||||
|
||||
// UpdateRegionOrders 排序
|
||||
func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) error {
|
||||
order := len(regionIds)
|
||||
|
||||
@@ -11,20 +11,20 @@ type NodeRegion struct {
|
||||
Description string `field:"description"` // 描述
|
||||
Order uint32 `field:"order"` // 排序
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Prices dbs.JSON `field:"prices"` // 价格
|
||||
Prices dbs.JSON `field:"prices"` // 流量价格
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NodeRegionOperator struct {
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
IsOn interface{} // 是否启用
|
||||
Name interface{} // 名称
|
||||
Description interface{} // 描述
|
||||
Order interface{} // 排序
|
||||
CreatedAt interface{} // 创建时间
|
||||
Prices interface{} // 价格
|
||||
State interface{} // 状态
|
||||
Id any // ID
|
||||
AdminId any // 管理员ID
|
||||
IsOn any // 是否启用
|
||||
Name any // 名称
|
||||
Description any // 描述
|
||||
Order any // 排序
|
||||
CreatedAt any // 创建时间
|
||||
Prices any // 流量价格
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewNodeRegionOperator() *NodeRegionOperator {
|
||||
|
||||
@@ -14,13 +14,16 @@ import (
|
||||
type NodeTaskType = string
|
||||
|
||||
const (
|
||||
// CDN相关
|
||||
|
||||
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
|
||||
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
|
||||
NodeTaskTypeGlobalServerConfigChanged NodeTaskType = "globalServerConfigChanged" // 全局服务设置变化
|
||||
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
|
||||
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
|
||||
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged"
|
||||
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged"
|
||||
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged" // IP条目变更
|
||||
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged" // 节点版本变化
|
||||
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged" // 脚本配置变化
|
||||
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged" // 节点级别变化
|
||||
NodeTaskTypeUserServersStateChanged NodeTaskType = "userServersStateChanged" // 用户服务状态变化
|
||||
|
||||
// NS相关
|
||||
|
||||
@@ -54,17 +57,30 @@ func init() {
|
||||
}
|
||||
|
||||
// CreateNodeTask 创建单个节点任务
|
||||
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, serverId int64, taskType NodeTaskType, version int64) error {
|
||||
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, userId int64, serverId int64, taskType NodeTaskType) error {
|
||||
if clusterId <= 0 || nodeId <= 0 {
|
||||
return nil
|
||||
}
|
||||
var uniqueId = role + "@" + types.String(nodeId) + "@node@" + types.String(serverId) + "@" + taskType
|
||||
|
||||
// 用户信息
|
||||
// 没有直接加入到 uniqueId 中,是为了兼容以前的字段值
|
||||
if userId > 0 {
|
||||
uniqueId += "@" + types.String(userId)
|
||||
}
|
||||
|
||||
version, err := this.increaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var updatedAt = time.Now().Unix()
|
||||
_, _, err := this.Query(tx).
|
||||
_, _, err = this.Query(tx).
|
||||
InsertOrUpdate(maps.Map{
|
||||
"role": role,
|
||||
"clusterId": clusterId,
|
||||
"nodeId": nodeId,
|
||||
"userId": userId,
|
||||
"serverId": serverId,
|
||||
"type": taskType,
|
||||
"uniqueId": uniqueId,
|
||||
@@ -87,17 +103,25 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
|
||||
}
|
||||
|
||||
// CreateClusterTask 创建集群任务
|
||||
func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId int64, serverId int64, taskType NodeTaskType) error {
|
||||
func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId int64, userId int64, serverId int64, taskType NodeTaskType) error {
|
||||
if clusterId <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var uniqueId = role + "@" + types.String(clusterId) + "@" + types.String(serverId) + "@cluster@" + types.String(serverId) + "@" + taskType
|
||||
var uniqueId = role + "@" + types.String(clusterId) + "@" + types.String(serverId) + "@cluster@" + taskType
|
||||
|
||||
// 用户信息
|
||||
// 没有直接加入到 uniqueId 中,是为了兼容以前的字段值
|
||||
if userId > 0 {
|
||||
uniqueId += "@" + types.String(userId)
|
||||
}
|
||||
|
||||
var updatedAt = time.Now().Unix()
|
||||
_, _, err := this.Query(tx).
|
||||
InsertOrUpdate(maps.Map{
|
||||
"role": role,
|
||||
"clusterId": clusterId,
|
||||
"userId": userId,
|
||||
"serverId": serverId,
|
||||
"nodeId": 0,
|
||||
"type": taskType,
|
||||
@@ -121,7 +145,7 @@ func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId in
|
||||
}
|
||||
|
||||
// ExtractNodeClusterTask 分解边缘节点集群任务
|
||||
func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, serverId int64, taskType NodeTaskType) error {
|
||||
func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, userId int64, serverId int64, taskType NodeTaskType) error {
|
||||
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, clusterId, true, configutils.BoolStateYes)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -138,9 +162,8 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, ser
|
||||
return err
|
||||
}
|
||||
|
||||
var version = time.Now().UnixNano()
|
||||
for _, nodeId := range nodeIds {
|
||||
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, serverId, taskType, version)
|
||||
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, userId, serverId, taskType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -169,11 +192,11 @@ func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx, role string) error {
|
||||
return err
|
||||
}
|
||||
for _, one := range ones {
|
||||
clusterId := int64(one.(*NodeTask).ClusterId)
|
||||
var clusterId = int64(one.(*NodeTask).ClusterId)
|
||||
switch role {
|
||||
case nodeconfigs.NodeRoleNode:
|
||||
var nodeTask = one.(*NodeTask)
|
||||
err = this.ExtractNodeClusterTask(tx, clusterId, int64(nodeTask.ServerId), nodeTask.Type)
|
||||
err = this.ExtractNodeClusterTask(tx, clusterId, int64(nodeTask.UserId), int64(nodeTask.ServerId), nodeTask.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -206,14 +229,22 @@ func (this *NodeTaskDAO) DeleteNodeTasks(tx *dbs.Tx, role string, nodeId int64)
|
||||
}
|
||||
|
||||
// FindDoingNodeTasks 查询一个节点的所有任务
|
||||
func (this *NodeTaskDAO) FindDoingNodeTasks(tx *dbs.Tx, role string, nodeId int64) (result []*NodeTask, err error) {
|
||||
func (this *NodeTaskDAO) FindDoingNodeTasks(tx *dbs.Tx, role string, nodeId int64, version int64) (result []*NodeTask, err error) {
|
||||
if nodeId <= 0 {
|
||||
return
|
||||
}
|
||||
_, err = this.Query(tx).
|
||||
var query = this.Query(tx).
|
||||
Attr("role", role).
|
||||
Attr("nodeId", nodeId).
|
||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||
Asc("version")
|
||||
if version > 0 {
|
||||
query.Lt("LENGTH(version)", 19) // 兼容以往版本
|
||||
query.Gt("version", version)
|
||||
} else {
|
||||
// 第一次访问时只取当前正在执行的或者执行失败的
|
||||
query.Where("(isDone=0 OR (isDone=1 AND isOk=0))")
|
||||
}
|
||||
_, err = query.
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
@@ -221,8 +252,16 @@ func (this *NodeTaskDAO) FindDoingNodeTasks(tx *dbs.Tx, role string, nodeId int6
|
||||
|
||||
// UpdateNodeTaskDone 修改节点任务的完成状态
|
||||
func (this *NodeTaskDAO) UpdateNodeTaskDone(tx *dbs.Tx, taskId int64, isOk bool, errorMessage string) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(taskId).
|
||||
var query = this.Query(tx).
|
||||
Pk(taskId)
|
||||
if !isOk {
|
||||
version, err := this.increaseVersion(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query.Set("version", version)
|
||||
}
|
||||
_, err := query.
|
||||
Set("isDone", 1).
|
||||
Set("isOk", isOk).
|
||||
Set("error", errorMessage).
|
||||
@@ -354,3 +393,8 @@ func (this *NodeTaskDAO) UpdateTasksNotified(tx *dbs.Tx, taskIds []int64) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 生成一个版本号
|
||||
func (this *NodeTaskDAO) increaseVersion(tx *dbs.Tx) (version int64, err error) {
|
||||
return SharedSysLockerDAO.Increase(tx, "NODE_TASK_VERSION", 0)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ func TestNodeTaskDAO_CreateNodeTask(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
var tx *dbs.Tx
|
||||
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, 0, NodeTaskTypeConfigChanged, 0)
|
||||
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func TestNodeTaskDAO_CreateClusterTask(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
var tx *dbs.Tx
|
||||
err := SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, 1, 0, NodeTaskTypeConfigChanged)
|
||||
err := SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, 1, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func TestNodeTaskDAO_ExtractClusterTask(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
var tx *dbs.Tx
|
||||
err := SharedNodeTaskDAO.ExtractNodeClusterTask(tx, 1, 0, NodeTaskTypeConfigChanged)
|
||||
err := SharedNodeTaskDAO.ExtractNodeClusterTask(tx, 1, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ type NodeTask struct {
|
||||
Role string `field:"role"` // 节点角色
|
||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
ServerId uint32 `field:"serverId"` // 服务ID
|
||||
ServerId uint64 `field:"serverId"` // 服务ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
Type string `field:"type"` // 任务类型
|
||||
UniqueId string `field:"uniqueId"` // 唯一ID:nodeId@type
|
||||
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
||||
@@ -18,19 +19,20 @@ type NodeTask struct {
|
||||
}
|
||||
|
||||
type NodeTaskOperator struct {
|
||||
Id interface{} // ID
|
||||
Role interface{} // 节点角色
|
||||
NodeId interface{} // 节点ID
|
||||
ClusterId interface{} // 集群ID
|
||||
ServerId interface{} // 服务ID
|
||||
Type interface{} // 任务类型
|
||||
UniqueId interface{} // 唯一ID:nodeId@type
|
||||
UpdatedAt interface{} // 修改时间
|
||||
IsDone interface{} // 是否已完成
|
||||
IsOk interface{} // 是否已完成
|
||||
Error interface{} // 错误信息
|
||||
IsNotified interface{} // 是否已通知更新
|
||||
Version interface{} // 版本
|
||||
Id any // ID
|
||||
Role any // 节点角色
|
||||
NodeId any // 节点ID
|
||||
ClusterId any // 集群ID
|
||||
ServerId any // 服务ID
|
||||
UserId any // 用户ID
|
||||
Type any // 任务类型
|
||||
UniqueId any // 唯一ID:nodeId@type
|
||||
UpdatedAt any // 修改时间
|
||||
IsDone any // 是否已完成
|
||||
IsOk any // 是否已完成
|
||||
Error any // 错误信息
|
||||
IsNotified any // 是否已通知更新
|
||||
Version any // 版本
|
||||
}
|
||||
|
||||
func NewNodeTaskOperator() *NodeTaskOperator {
|
||||
|
||||
@@ -149,5 +149,5 @@ func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicy
|
||||
|
||||
// NotifyUpdate 通知更改
|
||||
func (this *NSClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, NSNodeTaskTypeConfigChanged)
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, 0, NSNodeTaskTypeConfigChanged)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ type NSCluster struct {
|
||||
Answer dbs.JSON `field:"answer"` // 应答设置
|
||||
SoaSerial uint64 `field:"soaSerial"` // SOA序列号
|
||||
Email string `field:"email"` // 管理员邮箱
|
||||
DetectAgents bool `field:"detectAgents"` // 是否监测Agents
|
||||
}
|
||||
|
||||
type NSClusterOperator struct {
|
||||
@@ -45,6 +46,7 @@ type NSClusterOperator struct {
|
||||
Answer any // 应答设置
|
||||
SoaSerial any // SOA序列号
|
||||
Email any // 管理员邮箱
|
||||
DetectAgents any // 是否监测Agents
|
||||
}
|
||||
|
||||
func NewNSClusterOperator() *NSClusterOperator {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package models
|
||||
package models_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
@@ -22,6 +22,7 @@ type NSNode struct {
|
||||
InactiveNotifiedAt uint64 `field:"inactiveNotifiedAt"` // 离线通知时间
|
||||
ConnectedAPINodes dbs.JSON `field:"connectedAPINodes"` // 当前连接的API节点
|
||||
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
|
||||
ApiNodeAddrs dbs.JSON `field:"apiNodeAddrs"` // API节点地址
|
||||
}
|
||||
|
||||
type NSNodeOperator struct {
|
||||
@@ -43,6 +44,7 @@ type NSNodeOperator struct {
|
||||
InactiveNotifiedAt any // 离线通知时间
|
||||
ConnectedAPINodes any // 当前连接的API节点
|
||||
DdosProtection any // DDoS防护设置
|
||||
ApiNodeAddrs any // API节点地址
|
||||
}
|
||||
|
||||
func NewNSNodeOperator() *NSNodeOperator {
|
||||
|
||||
@@ -1,79 +1 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DecodeInstallStatus 安装状态
|
||||
func (this *NSNode) DecodeInstallStatus() (*NodeInstallStatus, error) {
|
||||
if len(this.InstallStatus) == 0 {
|
||||
return NewNodeInstallStatus(), nil
|
||||
}
|
||||
status := &NodeInstallStatus{}
|
||||
err := json.Unmarshal(this.InstallStatus, status)
|
||||
if err != nil {
|
||||
return NewNodeInstallStatus(), err
|
||||
}
|
||||
|
||||
// 如果N秒钟没有更新状态,则认为不在运行
|
||||
if status.IsRunning && status.UpdatedAt < time.Now().Unix()-10 {
|
||||
status.IsRunning = false
|
||||
status.IsFinished = true
|
||||
status.Error = "timeout"
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// DecodeStatus 节点状态
|
||||
func (this *NSNode) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
|
||||
if len(this.Status) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
status := &nodeconfigs.NodeStatus{}
|
||||
err := json.Unmarshal(this.Status, status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// DecodeDDoSProtection 解析DDoS Protection设置
|
||||
func (this *NSNode) DecodeDDoSProtection() *ddosconfigs.ProtectionConfig {
|
||||
if IsNull(this.DdosProtection) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var result = &ddosconfigs.ProtectionConfig{}
|
||||
err := json.Unmarshal(this.DdosProtection, &result)
|
||||
if err != nil {
|
||||
// ignore err
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// HasDDoSProtection 检查是否有DDOS设置
|
||||
func (this *NSNode) HasDDoSProtection() bool {
|
||||
var config = this.DecodeDDoSProtection()
|
||||
if config != nil {
|
||||
return !config.IsPriorEmpty()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DecodeConnectedAPINodes 解析连接的API节点列表
|
||||
func (this *NSNode) DecodeConnectedAPINodes() []int64 {
|
||||
if IsNull(this.ConnectedAPINodes) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var result = []int64{}
|
||||
err := json.Unmarshal(this.ConnectedAPINodes, &result)
|
||||
if err != nil {
|
||||
// ignore err
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -403,7 +403,7 @@ func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap
|
||||
}
|
||||
config.CertRef = ref
|
||||
if ref.CertId > 0 {
|
||||
certConfig, err := SharedSSLCertDAO.ComposeCertConfig(tx, ref.CertId, cacheMap)
|
||||
certConfig, err := SharedSSLCertDAO.ComposeCertConfig(tx, ref.CertId, false, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//go:build !plus
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
@@ -79,129 +80,6 @@ func (this *PlanDAO) FindPlanName(tx *dbs.Tx, id int64) (string, error) {
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreatePlan 创建套餐
|
||||
func (this *PlanDAO) CreatePlan(tx *dbs.Tx,
|
||||
name string,
|
||||
clusterId int64,
|
||||
trafficLimitJSON []byte,
|
||||
featuresJSON []byte,
|
||||
priceType serverconfigs.PlanPriceType,
|
||||
trafficPriceJSON []byte,
|
||||
bandwidthPriceJSON []byte,
|
||||
monthlyPrice float32,
|
||||
seasonallyPrice float32,
|
||||
yearlyPrice float32) (int64, error) {
|
||||
var op = NewPlanOperator()
|
||||
op.Name = name
|
||||
op.ClusterId = clusterId
|
||||
if len(trafficLimitJSON) > 0 {
|
||||
op.TrafficLimit = trafficLimitJSON
|
||||
}
|
||||
if len(featuresJSON) > 0 {
|
||||
op.Features = featuresJSON
|
||||
}
|
||||
op.PriceType = priceType
|
||||
if len(trafficPriceJSON) > 0 {
|
||||
op.TrafficPrice = trafficPriceJSON
|
||||
}
|
||||
if len(bandwidthPriceJSON) > 0 {
|
||||
op.BandwidthPrice = bandwidthPriceJSON
|
||||
}
|
||||
if monthlyPrice >= 0 {
|
||||
op.MonthlyPrice = monthlyPrice
|
||||
}
|
||||
if seasonallyPrice >= 0 {
|
||||
op.SeasonallyPrice = seasonallyPrice
|
||||
}
|
||||
if yearlyPrice >= 0 {
|
||||
op.YearlyPrice = yearlyPrice
|
||||
}
|
||||
op.IsOn = true
|
||||
op.State = PlanStateEnabled
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdatePlan 修改套餐
|
||||
func (this *PlanDAO) UpdatePlan(tx *dbs.Tx,
|
||||
planId int64,
|
||||
name string,
|
||||
isOn bool,
|
||||
clusterId int64,
|
||||
trafficLimitJSON []byte,
|
||||
featuresJSON []byte,
|
||||
priceType serverconfigs.PlanPriceType,
|
||||
trafficPriceJSON []byte,
|
||||
bandwidthPriceJSON []byte,
|
||||
monthlyPrice float32,
|
||||
seasonallyPrice float32,
|
||||
yearlyPrice float32) error {
|
||||
if planId <= 0 {
|
||||
return errors.New("invalid planId")
|
||||
}
|
||||
|
||||
// 检查集群有无变化
|
||||
oldClusterId, err := this.Query(tx).
|
||||
Pk(planId).
|
||||
Result("clusterId").
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var op = NewPlanOperator()
|
||||
op.Id = planId
|
||||
op.Name = name
|
||||
op.IsOn = isOn
|
||||
op.ClusterId = clusterId
|
||||
if len(trafficLimitJSON) > 0 {
|
||||
op.TrafficLimit = trafficLimitJSON
|
||||
}
|
||||
if len(featuresJSON) > 0 {
|
||||
op.Features = featuresJSON
|
||||
}
|
||||
op.PriceType = priceType
|
||||
if len(trafficPriceJSON) > 0 {
|
||||
op.TrafficPrice = trafficPriceJSON
|
||||
}
|
||||
if len(bandwidthPriceJSON) > 0 {
|
||||
op.BandwidthPrice = bandwidthPriceJSON
|
||||
}
|
||||
if monthlyPrice >= 0 {
|
||||
op.MonthlyPrice = monthlyPrice
|
||||
} else {
|
||||
op.MonthlyPrice = 0
|
||||
}
|
||||
if seasonallyPrice >= 0 {
|
||||
op.SeasonallyPrice = seasonallyPrice
|
||||
} else {
|
||||
op.SeasonallyPrice = 0
|
||||
}
|
||||
if yearlyPrice >= 0 {
|
||||
op.YearlyPrice = yearlyPrice
|
||||
} else {
|
||||
op.YearlyPrice = 0
|
||||
}
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if oldClusterId != clusterId {
|
||||
// 修改服务所属集群
|
||||
err = SharedServerDAO.UpdateServersClusterIdWithPlanId(tx, planId, clusterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = SharedNodeClusterDAO.NotifyUpdate(tx, oldClusterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, planId)
|
||||
}
|
||||
|
||||
// CountAllEnabledPlans 计算套餐的数量
|
||||
func (this *PlanDAO) CountAllEnabledPlans(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
|
||||
@@ -1,38 +1 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
)
|
||||
|
||||
// DecodeTrafficPrice 流量价格配置
|
||||
func (this *Plan) DecodeTrafficPrice() *serverconfigs.PlanTrafficPriceConfig {
|
||||
var config = &serverconfigs.PlanTrafficPriceConfig{}
|
||||
|
||||
if len(this.TrafficPrice) == 0 {
|
||||
return config
|
||||
}
|
||||
|
||||
err := json.Unmarshal(this.TrafficPrice, config)
|
||||
if err != nil {
|
||||
// 忽略错误
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// DecodeBandwidthPrice 带宽价格配置
|
||||
func (this *Plan) DecodeBandwidthPrice() *serverconfigs.PlanBandwidthPriceConfig {
|
||||
var config = &serverconfigs.PlanBandwidthPriceConfig{}
|
||||
|
||||
if len(this.BandwidthPrice) == 0 {
|
||||
return config
|
||||
}
|
||||
|
||||
err := json.Unmarshal(this.BandwidthPrice, config)
|
||||
if err != nil {
|
||||
// 忽略错误
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -117,9 +119,10 @@ func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverI
|
||||
var timePieces = strings.Split(fullTime, ".")
|
||||
var day = timePieces[0]
|
||||
var hour = timePieces[1]
|
||||
|
||||
var bytes = one.GetInt64("bytes")
|
||||
m[day+hour] = &pb.FindHourlyServerBandwidthStatsResponse_Stat{
|
||||
Bytes: one.GetInt64("bytes"),
|
||||
Bytes: bytes,
|
||||
Bits: bytes * 8,
|
||||
Day: day,
|
||||
Hour: types.Int32(hour),
|
||||
}
|
||||
@@ -136,6 +139,7 @@ func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverI
|
||||
} else {
|
||||
result = append(result, &pb.FindHourlyServerBandwidthStatsResponse_Stat{
|
||||
Bytes: 0,
|
||||
Bits: 0,
|
||||
Day: fullHour[:8],
|
||||
Hour: types.Int32(fullHour[8:]),
|
||||
})
|
||||
@@ -178,9 +182,11 @@ func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId
|
||||
var m = map[string]*pb.FindDailyServerBandwidthStatsResponse_Stat{}
|
||||
for _, one := range ones {
|
||||
var day = one.GetString("day")
|
||||
var bytes = one.GetInt64("bytes")
|
||||
|
||||
m[day] = &pb.FindDailyServerBandwidthStatsResponse_Stat{
|
||||
Bytes: one.GetInt64("bytes"),
|
||||
Bytes: bytes,
|
||||
Bits: bytes * 8,
|
||||
Day: day,
|
||||
}
|
||||
}
|
||||
@@ -196,6 +202,7 @@ func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId
|
||||
} else {
|
||||
result = append(result, &pb.FindDailyServerBandwidthStatsResponse_Stat{
|
||||
Bytes: 0,
|
||||
Bits: 0,
|
||||
Day: day,
|
||||
})
|
||||
}
|
||||
@@ -204,6 +211,85 @@ func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FindBandwidthStatsBetweenDays 查找日期段内的带宽峰值
|
||||
// dayFrom YYYYMMDD
|
||||
// dayTo YYYYMMDD
|
||||
func (this *ServerBandwidthStatDAO) FindBandwidthStatsBetweenDays(tx *dbs.Tx, serverId int64, dayFrom string, dayTo string) (result []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat, err error) {
|
||||
if serverId <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if !regexputils.YYYYMMDD.MatchString(dayFrom) {
|
||||
return nil, errors.New("invalid dayFrom '" + dayFrom + "'")
|
||||
}
|
||||
if !regexputils.YYYYMMDD.MatchString(dayTo) {
|
||||
return nil, errors.New("invalid dayTo '" + dayTo + "'")
|
||||
}
|
||||
|
||||
if dayFrom > dayTo {
|
||||
dayFrom, dayTo = dayTo, dayFrom
|
||||
}
|
||||
|
||||
ones, _, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Result("bytes", "day", "timeAt").
|
||||
Attr("serverId", serverId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
FindOnes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var m = map[string]*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{}
|
||||
for _, one := range ones {
|
||||
var day = one.GetString("day")
|
||||
var bytes = one.GetInt64("bytes")
|
||||
var timeAt = one.GetString("timeAt")
|
||||
var key = day + "@" + timeAt
|
||||
|
||||
m[key] = &pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{
|
||||
Bytes: bytes,
|
||||
Bits: bytes * 8,
|
||||
Day: day,
|
||||
TimeAt: timeAt,
|
||||
}
|
||||
}
|
||||
|
||||
allDays, err := utils.RangeDays(dayFrom, dayTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dayTimes, err := utils.Range24HourTimes(5)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 截止到当前时间
|
||||
var currentTime = timeutil.Format("Ymd@Hi")
|
||||
|
||||
for _, day := range allDays {
|
||||
for _, timeAt := range dayTimes {
|
||||
var key = day + "@" + timeAt
|
||||
if key >= currentTime {
|
||||
break
|
||||
}
|
||||
|
||||
stat, ok := m[key]
|
||||
if ok {
|
||||
result = append(result, stat)
|
||||
} else {
|
||||
result = append(result, &pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{
|
||||
Day: day,
|
||||
TimeAt: timeAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FindMonthlyPeekBandwidthBytes 获取某月的带宽峰值
|
||||
// month YYYYMM
|
||||
func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string) (int64, error) {
|
||||
@@ -308,6 +394,134 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
||||
return
|
||||
}
|
||||
|
||||
// FindPercentileBetweenDays 获取日期段内内百分位
|
||||
func (this *ServerBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, serverId int64, dayFrom string, dayTo string, percentile int32) (result *ServerBandwidthStat, err error) {
|
||||
if dayFrom > dayTo {
|
||||
dayFrom, dayTo = dayTo, dayFrom
|
||||
}
|
||||
|
||||
if percentile <= 0 {
|
||||
percentile = 95
|
||||
}
|
||||
|
||||
// 如果是100%以上,则快速返回
|
||||
if percentile >= 100 {
|
||||
one, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Desc("bytes").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*ServerBandwidthStat), nil
|
||||
}
|
||||
|
||||
// 总数量
|
||||
total, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if total == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var offset int64
|
||||
|
||||
if total > 1 {
|
||||
offset = int64(math.Ceil(float64(total) * float64(100-percentile) / 100))
|
||||
}
|
||||
|
||||
// 查询 nth 位置
|
||||
one, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Desc("bytes").
|
||||
Offset(offset).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*ServerBandwidthStat), nil
|
||||
}
|
||||
|
||||
// FindPercentileBetweenTimes 获取时间段内内百分位
|
||||
// timeFrom 开始时间,格式 YYYYMMDDHHII
|
||||
// timeTo 结束时间,格式 YYYYMMDDHHII
|
||||
func (this *ServerBandwidthStatDAO) FindPercentileBetweenTimes(tx *dbs.Tx, serverId int64, timeFrom string, timeTo string, percentile int32) (result *ServerBandwidthStat, err error) {
|
||||
var reg = regexp.MustCompile(`^\d{12}$`)
|
||||
if !reg.MatchString(timeFrom) {
|
||||
return nil, errors.New("invalid timeFrom '" + timeFrom + "'")
|
||||
}
|
||||
if !reg.MatchString(timeTo) {
|
||||
return nil, errors.New("invalid timeTo '" + timeTo + "'")
|
||||
}
|
||||
|
||||
if timeFrom > timeTo {
|
||||
timeFrom, timeTo = timeTo, timeFrom
|
||||
}
|
||||
|
||||
if percentile <= 0 {
|
||||
percentile = 95
|
||||
}
|
||||
|
||||
// 如果是100%以上,则快速返回
|
||||
if percentile >= 100 {
|
||||
one, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Between("CONCAT(day, timeAt)", timeFrom, timeTo).
|
||||
Desc("bytes").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*ServerBandwidthStat), nil
|
||||
}
|
||||
|
||||
// 总数量
|
||||
total, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Between("CONCAT(day, timeAt)", timeFrom, timeTo).
|
||||
Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if total == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var offset int64
|
||||
|
||||
if total > 1 {
|
||||
offset = int64(math.Ceil(float64(total) * float64(100-percentile) / 100))
|
||||
}
|
||||
|
||||
// 查询 nth 位置
|
||||
one, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Between("CONCAT(day, timeAt)", timeFrom, timeTo).
|
||||
Desc("bytes").
|
||||
Offset(offset).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*ServerBandwidthStat), nil
|
||||
}
|
||||
|
||||
// Clean 清理过期数据
|
||||
func (this *ServerBandwidthStatDAO) Clean(tx *dbs.Tx) error {
|
||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -100)) // 保留大约3个月的数据
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user