Compare commits
116 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
e4e0aab010 | ||
|
|
ed87b4e2a9 | ||
|
|
337eb36d25 | ||
|
|
c44e40d72d | ||
|
|
2e8ba831a1 | ||
|
|
a706c2a5a5 | ||
|
|
093826222a | ||
|
|
0d7b487afc | ||
|
|
8de17b6d9c | ||
|
|
49d217a883 | ||
|
|
4827555899 | ||
|
|
41f3825ee2 | ||
|
|
096aa153ab | ||
|
|
8f8b611ac1 | ||
|
|
08b1c038f1 | ||
|
|
934b10a254 | ||
|
|
4947f13416 | ||
|
|
84198d5948 | ||
|
|
d4a04bc798 | ||
|
|
c94b3c26c1 | ||
|
|
5655f89ba6 | ||
|
|
c568ad3e9a | ||
|
|
d2f532447d | ||
|
|
ec97feab28 | ||
|
|
316cd36f71 | ||
|
|
702a0f1ecf | ||
|
|
2d2b7b7bff | ||
|
|
6f8c5a8e99 | ||
|
|
afb35953e7 | ||
|
|
2a1f78a440 | ||
|
|
fb084a9f48 | ||
|
|
e7f620d28f | ||
|
|
2f64d713e8 | ||
|
|
86bf316468 | ||
|
|
bf320271d4 | ||
|
|
150a63fe98 | ||
|
|
1d1ff11eb7 | ||
|
|
55eecce416 | ||
|
|
5f822062da | ||
|
|
ee2c253e7d | ||
|
|
5b0adb4b84 | ||
|
|
8c72540a6e | ||
|
|
7f811997a9 | ||
|
|
ca94e31451 | ||
|
|
320d381bd9 | ||
|
|
c8057457cc | ||
|
|
c78c4d58ff | ||
|
|
d1f8e7e757 | ||
|
|
5386b30eba | ||
|
|
8ceb1334cd | ||
|
|
023e563de1 | ||
|
|
39e6d11d71 | ||
|
|
a5a9117ce0 | ||
|
|
e95b0bd9a6 | ||
|
|
9699a9adad | ||
|
|
67729abd13 | ||
|
|
4c7ebce97a | ||
|
|
44e7ce9f79 | ||
|
|
3468fcf8a6 | ||
|
|
3522c22a28 | ||
|
|
333a9c6611 | ||
|
|
e3426a84e2 | ||
|
|
a25b0e6c9d | ||
|
|
c271cadabd | ||
|
|
428bb7eb0f | ||
|
|
6ae9f447b6 | ||
|
|
7cc503b698 | ||
|
|
c66e28cb9d | ||
|
|
e5109b24d4 | ||
|
|
695b8482de |
@@ -105,12 +105,14 @@ function build() {
|
||||
done
|
||||
|
||||
# building edge dns installer
|
||||
echo "building dns node installer ..."
|
||||
architects=("amd64" "arm64")
|
||||
for arch in "${architects[@]}"; do
|
||||
# TODO support arm, mips ...
|
||||
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-dns-helper-linux-"${arch}" "$ROOT"/../cmd/installer-dns-helper/main.go
|
||||
done
|
||||
if [ $TAG = "plus" ]; then
|
||||
echo "building dns node installer ..."
|
||||
architects=("amd64" "arm64")
|
||||
for arch in "${architects[@]}"; do
|
||||
# TODO support arm, mips ...
|
||||
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-dns-helper-linux-"${arch}" "$ROOT"/../cmd/installer-dns-helper/main.go
|
||||
done
|
||||
fi
|
||||
|
||||
# building api node
|
||||
env GOOS="$OS" GOARCH="$ARCH" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$DIST"/bin/edge-api "$ROOT"/../cmd/edge-api/main.go
|
||||
|
||||
@@ -9,3 +9,7 @@ dbs:
|
||||
prefix: "edge"
|
||||
models:
|
||||
package: internal/web/models
|
||||
|
||||
|
||||
fields:
|
||||
bool: [ "uamIsOn", "followPort", "requestHostExcludingPort", "autoRemoteStart", "autoInstallNftables" ]
|
||||
|
||||
@@ -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"
|
||||
@@ -71,6 +72,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())
|
||||
|
||||
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=
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
@@ -40,6 +41,7 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
|
||||
}
|
||||
if this.task.Provider.RequireEAB && this.task.Account == nil {
|
||||
err = errors.New("account should not be nil when provider require EAB")
|
||||
return
|
||||
}
|
||||
|
||||
switch this.task.AuthType {
|
||||
@@ -138,7 +140,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.2"
|
||||
Version = "0.5.6"
|
||||
|
||||
ProductName = "Edge API"
|
||||
ProcessName = "edge-api"
|
||||
@@ -18,11 +18,11 @@ const (
|
||||
|
||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||
|
||||
NodeVersion = "0.5.2"
|
||||
UserNodeVersion = "0.4.1"
|
||||
NodeVersion = "0.5.6"
|
||||
UserNodeVersion = "0.5.6"
|
||||
DNSNodeVersion = "0.2.8"
|
||||
AuthorityNodeVersion = "0.0.2"
|
||||
MonitorNodeVersion = "0.0.4"
|
||||
DNSNodeVersion = "0.2.6"
|
||||
ReportNodeVersion = "0.1.1"
|
||||
|
||||
// SQLVersion SQL版本号
|
||||
|
||||
@@ -13,22 +13,26 @@ type OrderMethod struct {
|
||||
Url string `field:"url"` // URL
|
||||
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"` // 状态
|
||||
}
|
||||
|
||||
type OrderMethodOperator struct {
|
||||
Id interface{} // ID
|
||||
Name interface{} // 名称
|
||||
IsOn interface{} // 是否启用
|
||||
Description interface{} // 描述
|
||||
ParentCode interface{} // 内置的父级代号
|
||||
Code interface{} // 代号
|
||||
Url interface{} // URL
|
||||
Secret interface{} // 密钥
|
||||
Params interface{} // 参数
|
||||
Order interface{} // 排序
|
||||
State interface{} // 状态
|
||||
Id any // ID
|
||||
Name any // 名称
|
||||
IsOn any // 是否启用
|
||||
Description any // 描述
|
||||
ParentCode any // 内置的父级代号
|
||||
Code any // 代号
|
||||
Url any // URL
|
||||
Secret any // 密钥
|
||||
Params any // 参数
|
||||
ClientType any // 客户端类型
|
||||
QrcodeTitle any // 二维码标题
|
||||
Order any // 排序
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewOrderMethodOperator() *OrderMethodOperator {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -106,11 +106,8 @@ func (this *ACMETaskDAO) DisableAllTasksWithCertId(tx *dbs.Tx, certId int64) err
|
||||
}
|
||||
|
||||
// CountAllEnabledACMETasks 计算所有任务数量
|
||||
func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string) (int64, error) {
|
||||
func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string) (int64, error) {
|
||||
var query = this.Query(tx)
|
||||
if adminId > 0 {
|
||||
query.Attr("adminId", adminId)
|
||||
}
|
||||
query.Attr("userId", userId) // 这个条件必须加上
|
||||
if isAvailable || isExpired || expiringDays > 0 {
|
||||
query.Gt("certId", 0)
|
||||
@@ -141,11 +138,8 @@ func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, use
|
||||
}
|
||||
|
||||
// ListEnabledACMETasks 列出单页任务
|
||||
func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string, offset int64, size int64) (result []*ACMETask, err error) {
|
||||
func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string, offset int64, size int64) (result []*ACMETask, err error) {
|
||||
var query = this.Query(tx)
|
||||
if adminId > 0 {
|
||||
query.Attr("adminId", adminId)
|
||||
}
|
||||
query.Attr("userId", userId) // 这个条件必须加上
|
||||
if isAvailable || isExpired || expiringDays > 0 {
|
||||
query.Gt("certId", 0)
|
||||
@@ -235,12 +229,11 @@ func (this *ACMETaskDAO) UpdateACMETask(tx *dbs.Tx, acmeTaskId int64, acmeUserId
|
||||
}
|
||||
|
||||
// CheckACMETask 检查权限
|
||||
func (this *ACMETaskDAO) CheckACMETask(tx *dbs.Tx, adminId int64, userId int64, acmeTaskId int64) (bool, error) {
|
||||
func (this *ACMETaskDAO) CheckACMETask(tx *dbs.Tx, userId int64, acmeTaskId int64) (bool, error) {
|
||||
var query = this.Query(tx)
|
||||
if adminId > 0 {
|
||||
query.Attr("adminId", adminId)
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
query.Attr("userId", userId) // 这个条件必须加上
|
||||
|
||||
return query.
|
||||
State(ACMETaskStateEnabled).
|
||||
@@ -363,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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -2,18 +2,15 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"hash/crc32"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -31,20 +28,11 @@ type httpAccessLogDefinition struct {
|
||||
// HTTP服务访问
|
||||
var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO
|
||||
|
||||
// DNS服务访问
|
||||
var nsAccessLogDAOMapping = map[int64]*NSAccessLogDAOWrapper{} // dbNodeId => DAO
|
||||
var nsAccessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true
|
||||
|
||||
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
|
||||
type HTTPAccessLogDAOWrapper struct {
|
||||
DAO *HTTPAccessLogDAO
|
||||
NodeId int64
|
||||
}
|
||||
|
||||
// NSAccessLogDAOWrapper NS访问日志DAO
|
||||
type NSAccessLogDAOWrapper struct {
|
||||
DAO *NSAccessLogDAO
|
||||
NodeId int64
|
||||
DAO *HTTPAccessLogDAO
|
||||
NodeId int64
|
||||
IsLocal bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -78,102 +66,28 @@ func AllAccessLogDBs() []*dbs.DB {
|
||||
// 获取获取DAO
|
||||
func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
|
||||
accessLogLocker.RLock()
|
||||
defer accessLogLocker.RUnlock()
|
||||
if len(httpAccessLogDAOMapping) == 0 {
|
||||
dao = nil
|
||||
} else {
|
||||
for _, d := range httpAccessLogDAOMapping {
|
||||
dao = d
|
||||
break
|
||||
}
|
||||
}
|
||||
accessLogLocker.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
func randomNSAccessLogDAO() (dao *NSAccessLogDAOWrapper) {
|
||||
accessLogLocker.RLock()
|
||||
if len(nsAccessLogDAOMapping) == 0 {
|
||||
dao = nil
|
||||
} else {
|
||||
for _, d := range nsAccessLogDAOMapping {
|
||||
dao = d
|
||||
break
|
||||
}
|
||||
}
|
||||
accessLogLocker.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
func findNSAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
|
||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
||||
err = errors.New("invalid day '" + day + "', should be YYYYMMDD")
|
||||
return
|
||||
}
|
||||
|
||||
config, err := db.Config()
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
var daoList = []*HTTPAccessLogDAOWrapper{}
|
||||
|
||||
for _, d := range httpAccessLogDAOMapping {
|
||||
daoList = append(daoList, d)
|
||||
}
|
||||
|
||||
tableName = "edgeNSAccessLogs_" + day
|
||||
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
|
||||
|
||||
accessLogLocker.RLock()
|
||||
_, ok = nsAccessLogTableMapping[cacheKey]
|
||||
accessLogLocker.RUnlock()
|
||||
if ok {
|
||||
return tableName, true, nil
|
||||
var l = len(daoList)
|
||||
if l == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
tableNames, err := db.TableNames()
|
||||
if err != nil {
|
||||
return tableName, false, err
|
||||
if l == 1 {
|
||||
return daoList[0]
|
||||
}
|
||||
|
||||
return tableName, utils.ContainsStringInsensitive(tableNames, tableName), nil
|
||||
}
|
||||
|
||||
func findNSAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
|
||||
config, err := db.Config()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
tableName := "edgeNSAccessLogs_" + day
|
||||
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
|
||||
|
||||
if !force {
|
||||
accessLogLocker.RLock()
|
||||
_, ok := nsAccessLogTableMapping[cacheKey]
|
||||
accessLogLocker.RUnlock()
|
||||
if ok {
|
||||
return tableName, nil
|
||||
}
|
||||
}
|
||||
|
||||
tableNames, err := db.TableNames()
|
||||
if err != nil {
|
||||
return tableName, err
|
||||
}
|
||||
|
||||
if utils.ContainsStringInsensitive(tableNames, tableName) {
|
||||
accessLogLocker.Lock()
|
||||
nsAccessLogTableMapping[cacheKey] = true
|
||||
accessLogLocker.Unlock()
|
||||
return tableName, nil
|
||||
}
|
||||
|
||||
// 创建表格
|
||||
_, err = db.Exec("CREATE TABLE `" + tableName + "` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordId` int(11) unsigned DEFAULT '0' COMMENT '记录ID',\n `content` json DEFAULT NULL COMMENT '访问数据',\n `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `remoteAddr` varchar(128) DEFAULT NULL COMMENT 'IP',\n PRIMARY KEY (`id`),\n KEY `nodeId` (`nodeId`),\n KEY `domainId` (`domainId`),\n KEY `recordId` (`recordId`),\n KEY `requestId` (`requestId`),\n KEY `remoteAddr` (`remoteAddr`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='域名服务访问日志';")
|
||||
if err != nil {
|
||||
return tableName, err
|
||||
}
|
||||
|
||||
accessLogLocker.Lock()
|
||||
nsAccessLogTableMapping[cacheKey] = true
|
||||
accessLogLocker.Unlock()
|
||||
|
||||
return tableName, nil
|
||||
return daoList[rands.Int(0, l-1)]
|
||||
}
|
||||
|
||||
// DBNodeInitializer 初始化数据库连接
|
||||
@@ -209,14 +123,14 @@ func (this *DBNodeInitializer) loop() error {
|
||||
return err
|
||||
}
|
||||
|
||||
nodeIds := []int64{}
|
||||
var nodeIds = []int64{}
|
||||
for _, node := range dbNodes {
|
||||
nodeIds = append(nodeIds, int64(node.Id))
|
||||
}
|
||||
|
||||
// 关掉老的
|
||||
accessLogLocker.Lock()
|
||||
closingDbs := []*dbs.DB{}
|
||||
var closingDbs = []*dbs.DB{}
|
||||
for nodeId, db := range accessLogDBMapping {
|
||||
if !lists.ContainsInt64(nodeIds, nodeId) {
|
||||
closingDbs = append(closingDbs, db)
|
||||
@@ -233,12 +147,12 @@ func (this *DBNodeInitializer) loop() error {
|
||||
|
||||
// 启动新的
|
||||
for _, node := range dbNodes {
|
||||
nodeId := int64(node.Id)
|
||||
var nodeId = int64(node.Id)
|
||||
accessLogLocker.Lock()
|
||||
db, ok := accessLogDBMapping[nodeId]
|
||||
accessLogLocker.Unlock()
|
||||
|
||||
dsn := node.Username + ":" + node.Password + "@tcp(" + node.Host + ":" + fmt.Sprintf("%d", node.Port) + ")/" + node.Database + "?charset=utf8mb4&timeout=10s"
|
||||
var dsn = node.Username + ":" + node.Password + "@tcp(" + node.Host + ":" + fmt.Sprintf("%d", node.Port) + ")/" + node.Database + "?charset=utf8mb4&timeout=10s"
|
||||
|
||||
if ok {
|
||||
// 检查配置是否有变化
|
||||
@@ -283,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,
|
||||
@@ -298,59 +212,19 @@ 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()
|
||||
}
|
||||
|
||||
// nsAccessLog
|
||||
{
|
||||
tableName, err := findNSAccessLogTable(db, timeutil.Format("Ymd"), false)
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误
|
||||
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
||||
|
||||
// 创建节点日志
|
||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
|
||||
if createLogErr != nil {
|
||||
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
||||
}
|
||||
|
||||
continue
|
||||
} else {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
|
||||
daoObject := dbs.DAOObject{
|
||||
Instance: db,
|
||||
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
|
||||
Table: tableName,
|
||||
PkName: "id",
|
||||
Model: new(NSAccessLog),
|
||||
}
|
||||
err = daoObject.Init()
|
||||
if err != nil {
|
||||
remotelogs.Error("DB_NODE", "initialize dao failed: "+err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
accessLogLocker.Lock()
|
||||
accessLogDBMapping[nodeId] = db
|
||||
dao := &NSAccessLogDAO{
|
||||
DAOObject: daoObject,
|
||||
}
|
||||
nsAccessLogDAOMapping[nodeId] = &NSAccessLogDAOWrapper{
|
||||
DAO: dao,
|
||||
NodeId: nodeId,
|
||||
}
|
||||
accessLogLocker.Unlock()
|
||||
}
|
||||
// 扩展
|
||||
initAccessLogDAO(db, node)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
internal/db/models/db_node_initializer_ext.go
Normal file
11
internal/db/models/db_node_initializer_ext.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
var nsAccessLogDAOMapping = map[int64]any{} // dbNodeId => DAO
|
||||
|
||||
func initAccessLogDAO(db *dbs.DB, node *DBNode) {
|
||||
}
|
||||
@@ -133,11 +133,28 @@ func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(tx *dbs.Tx, provid
|
||||
return
|
||||
}
|
||||
|
||||
// ListDomains 列出单页域名
|
||||
func (this *DNSDomainDAO) ListDomains(tx *dbs.Tx, providerId int64, isDeleted bool, isUp bool, offset int64, size int64) (result []*DNSDomain, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(DNSDomainStateEnabled).
|
||||
Attr("providerId", providerId).
|
||||
Attr("isDeleted", isDeleted).
|
||||
Attr("isUp", isUp).
|
||||
AscPk().
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CountAllEnabledDomainsWithProviderId 计算某个服务商下的域名数量
|
||||
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (int64, error) {
|
||||
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64, isDeleted bool, isUp bool) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(DNSDomainStateEnabled).
|
||||
Attr("providerId", providerId).
|
||||
Attr("isDeleted", isDeleted).
|
||||
Attr("isUp", isUp).
|
||||
Count()
|
||||
}
|
||||
|
||||
|
||||
@@ -56,11 +56,13 @@ func (this *DNSTaskDAO) CreateDNSTask(tx *dbs.Tx, clusterId int64, serverId int6
|
||||
"isDone": false,
|
||||
"isOk": false,
|
||||
"error": "",
|
||||
"version": time.Now().UnixNano(),
|
||||
}, maps.Map{
|
||||
"updatedAt": time.Now().Unix(),
|
||||
"isDone": false,
|
||||
"isOk": false,
|
||||
"error": "",
|
||||
"version": time.Now().UnixNano(),
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -94,6 +96,7 @@ func (this *DNSTaskDAO) CreateDomainTask(tx *dbs.Tx, domainId int64, taskType DN
|
||||
func (this *DNSTaskDAO) FindAllDoingTasks(tx *dbs.Tx) (result []*DNSTask, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Attr("isDone", 0).
|
||||
Asc("version").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
@@ -109,6 +112,7 @@ func (this *DNSTaskDAO) FindAllDoingOrErrorTasks(tx *dbs.Tx, nodeClusterId int64
|
||||
_, err = query.
|
||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||
Asc("updatedAt").
|
||||
Asc("version").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
@@ -163,3 +167,13 @@ func (this *DNSTaskDAO) UpdateDNSTaskDone(tx *dbs.Tx, taskId int64) error {
|
||||
op.Error = ""
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// DeleteDNSTasksWithClusterId 删除集群相关任务
|
||||
func (this *DNSTaskDAO) DeleteDNSTasksWithClusterId(tx *dbs.Tx, clusterId int64) error {
|
||||
if clusterId <= 0 {
|
||||
return nil
|
||||
}
|
||||
return this.Query(tx).
|
||||
Attr("clusterId", clusterId).
|
||||
DeleteQuickly()
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func TestDNSTaskDAO_CreateDNSTask(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "taskType")
|
||||
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "cdn", "taskType")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -13,20 +13,22 @@ type DNSTask struct {
|
||||
IsDone bool `field:"isDone"` // 是否已完成
|
||||
IsOk bool `field:"isOk"` // 是否成功
|
||||
Error string `field:"error"` // 错误信息
|
||||
Version uint64 `field:"version"` // 版本
|
||||
}
|
||||
|
||||
type DNSTaskOperator struct {
|
||||
Id interface{} // ID
|
||||
ClusterId interface{} // 集群ID
|
||||
ServerId interface{} // 服务ID
|
||||
NodeId interface{} // 节点ID
|
||||
DomainId interface{} // 域名ID
|
||||
RecordName interface{} // 记录名
|
||||
Type interface{} // 任务类型
|
||||
UpdatedAt interface{} // 更新时间
|
||||
IsDone interface{} // 是否已完成
|
||||
IsOk interface{} // 是否成功
|
||||
Error interface{} // 错误信息
|
||||
Id any // ID
|
||||
ClusterId any // 集群ID
|
||||
ServerId any // 服务ID
|
||||
NodeId any // 节点ID
|
||||
DomainId any // 域名ID
|
||||
RecordName any // 记录名
|
||||
Type any // 任务类型
|
||||
UpdatedAt any // 更新时间
|
||||
IsDone any // 是否已完成
|
||||
IsOk any // 是否成功
|
||||
Error any // 错误信息
|
||||
Version any // 版本
|
||||
}
|
||||
|
||||
func NewDNSTaskOperator() *DNSTaskOperator {
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
// CheckClusterDNS 检查集群的DNS问题
|
||||
// 藏这么深是避免package循环引用的问题
|
||||
func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSIssue, err error) {
|
||||
func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster, checkNodeIssues bool) (issues []*pb.DNSIssue, err error) {
|
||||
var clusterId = int64(cluster.Id)
|
||||
var domainId = int64(cluster.DnsDomainId)
|
||||
|
||||
@@ -67,7 +67,7 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
||||
})
|
||||
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,
|
||||
@@ -104,47 +104,27 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
||||
// TODO 检查域名是否已解析
|
||||
|
||||
// 检查节点
|
||||
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true, clusterDNSConfig != nil && clusterDNSConfig.IncludingLnNodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查节点数量不能为0
|
||||
|
||||
for _, node := range nodes {
|
||||
nodeId := int64(node.Id)
|
||||
|
||||
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
|
||||
if checkNodeIssues {
|
||||
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true, clusterDNSConfig != nil && clusterDNSConfig.IncludingLnNodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(routeCodes) == 0 && !hasDefaultRoute {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: node.Name,
|
||||
TargetId: nodeId,
|
||||
Type: "node",
|
||||
Description: "没有选择节点所属线路",
|
||||
Params: map[string]string{
|
||||
"clusterName": cluster.Name,
|
||||
"clusterId": numberutils.FormatInt64(clusterId),
|
||||
},
|
||||
MustFix: true,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// 检查线路是否在已有线路中
|
||||
for _, routeCode := range routeCodes {
|
||||
routeOk, err := domain.ContainsRouteCode(routeCode)
|
||||
// TODO 检查节点数量不能为0
|
||||
|
||||
for _, node := range nodes {
|
||||
nodeId := int64(node.Id)
|
||||
|
||||
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !routeOk {
|
||||
if len(routeCodes) == 0 && !hasDefaultRoute {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: node.Name,
|
||||
TargetId: nodeId,
|
||||
Type: "node",
|
||||
Description: "线路已经失效,请重新选择",
|
||||
Description: "没有选择节点所属线路",
|
||||
Params: map[string]string{
|
||||
"clusterName": cluster.Name,
|
||||
"clusterId": numberutils.FormatInt64(clusterId),
|
||||
@@ -153,29 +133,51 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// 检查IP地址
|
||||
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, true, nodeconfigs.NodeRoleNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ipAddr) == 0 {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: node.Name,
|
||||
TargetId: nodeId,
|
||||
Type: "node",
|
||||
Description: "没有设置IP地址",
|
||||
Params: map[string]string{
|
||||
"clusterName": cluster.Name,
|
||||
"clusterId": numberutils.FormatInt64(clusterId),
|
||||
},
|
||||
MustFix: true,
|
||||
})
|
||||
continue
|
||||
}
|
||||
// 检查线路是否在已有线路中
|
||||
for _, routeCode := range routeCodes {
|
||||
routeOk, err := domain.ContainsRouteCode(routeCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !routeOk {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: node.Name,
|
||||
TargetId: nodeId,
|
||||
Type: "node",
|
||||
Description: "线路已经失效,请重新选择",
|
||||
Params: map[string]string{
|
||||
"clusterName": cluster.Name,
|
||||
"clusterId": numberutils.FormatInt64(clusterId),
|
||||
},
|
||||
MustFix: true,
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// TODO 检查是否有解析记录
|
||||
// 检查IP地址
|
||||
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, true, nodeconfigs.NodeRoleNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ipAddr) == 0 {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: node.Name,
|
||||
TargetId: nodeId,
|
||||
Type: "node",
|
||||
Description: "没有设置IP地址",
|
||||
Params: map[string]string{
|
||||
"clusterName": cluster.Name,
|
||||
"clusterId": numberutils.FormatInt64(clusterId),
|
||||
},
|
||||
MustFix: true,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// TODO 检查是否有解析记录
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
@@ -198,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 + "'")
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ func TestNodeClusterDAO_CheckClusterDNS(t *testing.T) {
|
||||
t.Log("cluster not found, skip the test")
|
||||
return
|
||||
}
|
||||
issues, err := CheckClusterDNS(tx, cluster)
|
||||
issues, err := CheckClusterDNS(tx, cluster, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"golang.org/x/net/idna"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -39,7 +40,7 @@ var SharedHTTPAccessLogDAO *HTTPAccessLogDAO
|
||||
var (
|
||||
oldAccessLogQueue = make(chan *pb.HTTPAccessLog)
|
||||
accessLogQueue = make(chan *pb.HTTPAccessLog, 10_000)
|
||||
accessLogQueueMaxLength = 100_000 //队列最大长度
|
||||
accessLogQueueMaxLength = 100_000 // 队列最大长度
|
||||
accessLogQueuePercent = 100 // 0-100
|
||||
accessLogCountPerSecond = 10_000 // 每秒钟写入条数,0 表示不限制
|
||||
accessLogPerTx = 100 // 单事务写入条数
|
||||
@@ -50,6 +51,10 @@ var (
|
||||
accessLogRowsPerTable int64 = 500_000 // 自动分表的单表最大值
|
||||
)
|
||||
|
||||
func AccessLogQueuePercent() int {
|
||||
return accessLogQueuePercent
|
||||
}
|
||||
|
||||
type accessLogTableQuery struct {
|
||||
daoWrapper *HTTPAccessLogDAOWrapper
|
||||
name string
|
||||
@@ -85,13 +90,24 @@ func init() {
|
||||
// 导出队列内容
|
||||
goman.New(func() {
|
||||
var ticker = time.NewTicker(1 * time.Second)
|
||||
var accessLogPerLoop = accessLogPerTx
|
||||
|
||||
for range ticker.C {
|
||||
var countTxs = accessLogCountPerSecond / accessLogPerTx
|
||||
var countTxs = accessLogCountPerSecond / accessLogPerLoop
|
||||
if countTxs <= 0 {
|
||||
countTxs = 1
|
||||
}
|
||||
for i := 0; i < countTxs; i++ {
|
||||
hasMore, err := SharedHTTPAccessLogDAO.DumpAccessLogsFromQueue(accessLogPerTx)
|
||||
var before = time.Now()
|
||||
hasMore, err := SharedHTTPAccessLogDAO.DumpAccessLogsFromQueue(accessLogPerLoop)
|
||||
|
||||
// 如果用时过长,则调整每次写入次数
|
||||
var costMs = time.Since(before).Milliseconds()
|
||||
if costMs > 150 {
|
||||
accessLogPerLoop = accessLogPerTx / 4
|
||||
} else if costMs > 100 {
|
||||
accessLogPerLoop = accessLogPerTx / 2
|
||||
} // 这里不需要恢复成默认值,因为可能是写入数量比较小
|
||||
if err != nil {
|
||||
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "dump access logs failed: "+err.Error())
|
||||
} else if !hasMore {
|
||||
@@ -119,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,17 +160,28 @@ func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(size int) (hasMore bool, e
|
||||
if size <= 0 {
|
||||
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 + ")")
|
||||
}
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
@@ -205,7 +232,14 @@ Loop:
|
||||
|
||||
// CreateHTTPAccessLog 写入单条访问日志
|
||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
|
||||
var day = timeutil.FormatTime("Ymd", accessLog.Timestamp)
|
||||
var day = ""
|
||||
// 注意:如果你修改了 TimeISO8601 的逻辑,这里也需要同步修改
|
||||
if len(accessLog.TimeISO8601) > 10 {
|
||||
day = strings.ReplaceAll(accessLog.TimeISO8601[:10], "-", "")
|
||||
} else {
|
||||
timeutil.FormatTime("Ymd", accessLog.Timestamp)
|
||||
}
|
||||
|
||||
tableDef, err := SharedHTTPAccessLogManager.FindLastTable(dao.Instance, day, true)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -436,6 +470,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
||||
|
||||
var dao = tableQuery.daoWrapper.DAO
|
||||
var query = dao.Query(tx)
|
||||
query.Result("id", "serverId", "nodeId", "status", "createdAt", "content", "requestId", "firewallPolicyId", "firewallRuleGroupId", "firewallRuleSetId", "firewallRuleId", "remoteAddr", "domain")
|
||||
|
||||
// 条件
|
||||
if nodeId > 0 {
|
||||
@@ -509,6 +544,14 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
||||
query.Where("domain LIKE :host2").
|
||||
Param("host2", domain)
|
||||
} else {
|
||||
// 中文域名
|
||||
if !regexp.MustCompile(`^[a-zA-Z0-9-.]+$`).MatchString(domain) {
|
||||
unicodeDomain, err := idna.ToASCII(domain)
|
||||
if err == nil && len(unicodeDomain) > 0 {
|
||||
domain = unicodeDomain
|
||||
}
|
||||
}
|
||||
|
||||
query.Attr("domain", domain)
|
||||
query.UseIndex("domain")
|
||||
}
|
||||
@@ -777,7 +820,7 @@ func (this *HTTPAccessLogDAO) SetupQueue() {
|
||||
return
|
||||
}
|
||||
|
||||
if bytes.Compare(accessLogConfigJSON, configJSON) == 0 {
|
||||
if bytes.Equal(accessLogConfigJSON, configJSON) {
|
||||
return
|
||||
}
|
||||
accessLogConfigJSON = configJSON
|
||||
|
||||
@@ -79,7 +79,7 @@ func TestCreateHTTPAccessLog_Tx(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 1000; i++ {
|
||||
for i := 0; i < 200; i++ {
|
||||
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -422,7 +422,7 @@ func (this *HTTPAccessLogManager) checkTableFields(db *dbs.DB, tableName string)
|
||||
}
|
||||
for _, field := range fields {
|
||||
var fieldName = field.GetString("Field")
|
||||
if strings.ToLower(fieldName) == strings.ToLower("remoteAddr") {
|
||||
if strings.EqualFold(fieldName, "remoteAddr") {
|
||||
hasRemoteAddrField = true
|
||||
}
|
||||
if strings.ToLower(fieldName) == "domain" {
|
||||
|
||||
@@ -125,25 +125,13 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st
|
||||
MinSize: &shared.SizeCapacity{Count: 0, Unit: shared.SizeCapacityUnitKB},
|
||||
SkipResponseSetCookie: true,
|
||||
AllowChunkedEncoding: true,
|
||||
Conds: &shared.HTTPRequestCondsConfig{
|
||||
IsOn: true,
|
||||
Connector: "or",
|
||||
Groups: []*shared.HTTPRequestCondGroup{
|
||||
{
|
||||
IsOn: true,
|
||||
Connector: "or",
|
||||
Conds: []*shared.HTTPRequestCond{
|
||||
{
|
||||
Type: "url-extension",
|
||||
IsRequest: true,
|
||||
Param: "${requestPathExtension}",
|
||||
Operator: shared.RequestCondOperatorIn,
|
||||
Value: `[".html", ".js", ".css", ".gif", ".png", ".bmp", ".jpeg", ".jpg", ".webp", ".ico", ".pdf", ".ttf", ".eot", ".tiff", ".svg", ".svgz", ".eps", ".woff", ".otf", ".woff2", ".tif", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".wav", ".mp3", ".mp4", ".ogg", ".mid", ".midi"]`,
|
||||
},
|
||||
},
|
||||
Description: "初始化规则",
|
||||
},
|
||||
},
|
||||
AllowPartialContent: true,
|
||||
SimpleCond: &shared.HTTPRequestCond{
|
||||
Type: "url-extension",
|
||||
IsRequest: true,
|
||||
Param: "${requestPathExtension}",
|
||||
Operator: shared.RequestCondOperatorIn,
|
||||
Value: `[".html", ".js", ".css", ".gif", ".png", ".bmp", ".jpeg", ".jpg", ".webp", ".ico", ".pdf", ".ttf", ".eot", ".tiff", ".svg", ".svgz", ".eps", ".woff", ".otf", ".woff2", ".tif", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".wav", ".mp3", ".mp4", ".ogg", ".mid", ".midi"]`,
|
||||
},
|
||||
}
|
||||
refsJSON, err := json.Marshal([]*serverconfigs.HTTPCacheRef{cacheRef})
|
||||
|
||||
@@ -381,7 +381,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
||||
|
||||
// 认证
|
||||
if IsNotNull(web.Auth) {
|
||||
authConfig := &serverconfigs.HTTPAuthConfig{}
|
||||
var authConfig = &serverconfigs.HTTPAuthConfig{}
|
||||
err = json.Unmarshal(web.Auth, authConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -395,6 +395,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
||||
if policyConfig != nil {
|
||||
ref.AuthPolicy = policyConfig
|
||||
newRefs = append(newRefs, ref)
|
||||
authConfig.PolicyRefs = newRefs
|
||||
}
|
||||
}
|
||||
config.Auth = authConfig
|
||||
@@ -457,6 +458,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
||||
config.UAM = uamConfig
|
||||
}
|
||||
|
||||
// Referers
|
||||
if IsNotNull(web.Referers) {
|
||||
var referersConfig = &serverconfigs.ReferersConfig{}
|
||||
err = json.Unmarshal(web.Referers, referersConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Referers = referersConfig
|
||||
}
|
||||
|
||||
if cacheMap != nil {
|
||||
cacheMap.Put(cacheKey, config)
|
||||
}
|
||||
@@ -1212,6 +1223,35 @@ func (this *HTTPWebDAO) FindWebUAM(tx *dbs.Tx, webId int64) ([]byte, error) {
|
||||
FindJSONCol()
|
||||
}
|
||||
|
||||
// UpdateWebReferers 修改防盗链设置
|
||||
func (this *HTTPWebDAO) UpdateWebReferers(tx *dbs.Tx, webId int64, referersConfig *serverconfigs.ReferersConfig) error {
|
||||
if referersConfig == nil {
|
||||
return nil
|
||||
}
|
||||
configJSON, err := json.Marshal(referersConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = this.Query(tx).
|
||||
Pk(webId).
|
||||
Set("referers", configJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, webId)
|
||||
}
|
||||
|
||||
// FindWebReferers 查找服务的防盗链配置
|
||||
func (this *HTTPWebDAO) FindWebReferers(tx *dbs.Tx, webId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(webId).
|
||||
Result("referers").
|
||||
FindJSONCol()
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
||||
// server
|
||||
|
||||
@@ -38,43 +38,45 @@ type HTTPWeb struct {
|
||||
RequestLimit dbs.JSON `field:"requestLimit"` // 请求限制
|
||||
RequestScripts dbs.JSON `field:"requestScripts"` // 请求脚本
|
||||
Uam dbs.JSON `field:"uam"` // UAM设置
|
||||
Referers dbs.JSON `field:"referers"` // 防盗链设置
|
||||
}
|
||||
|
||||
type HTTPWebOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
TemplateId interface{} // 模版ID
|
||||
AdminId interface{} // 管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
Root interface{} // 根目录
|
||||
Charset interface{} // 字符集
|
||||
Shutdown interface{} // 临时关闭页面配置
|
||||
Pages interface{} // 特殊页面
|
||||
RedirectToHttps interface{} // 跳转到HTTPS设置
|
||||
Indexes interface{} // 首页文件列表
|
||||
MaxRequestBodySize interface{} // 最大允许的请求内容尺寸
|
||||
RequestHeader interface{} // 请求Header配置
|
||||
ResponseHeader interface{} // 响应Header配置
|
||||
AccessLog interface{} // 访问日志配置
|
||||
Stat interface{} // 统计配置
|
||||
Gzip interface{} // Gzip配置(v0.3.2弃用)
|
||||
Compression interface{} // 压缩配置
|
||||
Cache interface{} // 缓存配置
|
||||
Firewall interface{} // 防火墙设置
|
||||
Locations interface{} // 路由规则配置
|
||||
Websocket interface{} // Websocket设置
|
||||
RewriteRules interface{} // 重写规则配置
|
||||
HostRedirects interface{} // 域名跳转
|
||||
Fastcgi interface{} // Fastcgi配置
|
||||
Auth interface{} // 认证策略配置
|
||||
Webp interface{} // WebP配置
|
||||
RemoteAddr interface{} // 客户端IP配置
|
||||
MergeSlashes interface{} // 是否合并路径中的斜杠
|
||||
RequestLimit interface{} // 请求限制
|
||||
RequestScripts interface{} // 请求脚本
|
||||
Uam interface{} // UAM设置
|
||||
Id any // ID
|
||||
IsOn any // 是否启用
|
||||
TemplateId any // 模版ID
|
||||
AdminId any // 管理员ID
|
||||
UserId any // 用户ID
|
||||
State any // 状态
|
||||
CreatedAt any // 创建时间
|
||||
Root any // 根目录
|
||||
Charset any // 字符集
|
||||
Shutdown any // 临时关闭页面配置
|
||||
Pages any // 特殊页面
|
||||
RedirectToHttps any // 跳转到HTTPS设置
|
||||
Indexes any // 首页文件列表
|
||||
MaxRequestBodySize any // 最大允许的请求内容尺寸
|
||||
RequestHeader any // 请求Header配置
|
||||
ResponseHeader any // 响应Header配置
|
||||
AccessLog any // 访问日志配置
|
||||
Stat any // 统计配置
|
||||
Gzip any // Gzip配置(v0.3.2弃用)
|
||||
Compression any // 压缩配置
|
||||
Cache any // 缓存配置
|
||||
Firewall any // 防火墙设置
|
||||
Locations any // 路由规则配置
|
||||
Websocket any // Websocket设置
|
||||
RewriteRules any // 重写规则配置
|
||||
HostRedirects any // 域名跳转
|
||||
Fastcgi any // Fastcgi配置
|
||||
Auth any // 认证策略配置
|
||||
Webp any // WebP配置
|
||||
RemoteAddr any // 客户端IP配置
|
||||
MergeSlashes any // 是否合并路径中的斜杠
|
||||
RequestLimit any // 请求限制
|
||||
RequestScripts any // 请求脚本
|
||||
Uam any // UAM设置
|
||||
Referers any // 防盗链设置
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -4,31 +4,35 @@ import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// NSDomain DNS域名
|
||||
type NSDomain struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Name string `field:"name"` // 域名
|
||||
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
|
||||
Tsig dbs.JSON `field:"tsig"` // TSIG配置
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Version uint64 `field:"version"` // 版本号
|
||||
Status string `field:"status"` // 状态:none|verified
|
||||
State uint8 `field:"state"` // 状态
|
||||
Id uint64 `field:"id"` // ID
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Name string `field:"name"` // 域名
|
||||
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
|
||||
Tsig dbs.JSON `field:"tsig"` // TSIG配置
|
||||
VerifyTXT string `field:"verifyTXT"` // 验证用的TXT
|
||||
VerifyExpiresAt uint64 `field:"verifyExpiresAt"` // 验证TXT过期时间
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Version uint64 `field:"version"` // 版本号
|
||||
Status string `field:"status"` // 状态:none|verified
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSDomainOperator struct {
|
||||
Id interface{} // ID
|
||||
ClusterId interface{} // 集群ID
|
||||
UserId interface{} // 用户ID
|
||||
IsOn interface{} // 是否启用
|
||||
Name interface{} // 域名
|
||||
GroupIds interface{} // 分组ID
|
||||
Tsig interface{} // TSIG配置
|
||||
CreatedAt interface{} // 创建时间
|
||||
Version interface{} // 版本号
|
||||
Status interface{} // 状态:none|verified
|
||||
State interface{} // 状态
|
||||
Id any // ID
|
||||
ClusterId any // 集群ID
|
||||
UserId any // 用户ID
|
||||
IsOn any // 是否启用
|
||||
Name any // 域名
|
||||
GroupIds any // 分组ID
|
||||
Tsig any // TSIG配置
|
||||
VerifyTXT any // 验证用的TXT
|
||||
VerifyExpiresAt any // 验证TXT过期时间
|
||||
CreatedAt any // 创建时间
|
||||
Version any // 版本号
|
||||
Status any // 状态:none|verified
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewNSDomainOperator() *NSDomainOperator {
|
||||
|
||||
30
internal/db/models/nameservers/ns_plan_model.go
Normal file
30
internal/db/models/nameservers/ns_plan_model.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package nameservers
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// NSPlan NS套餐
|
||||
type NSPlan struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 套餐名称
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
MonthlyPrice float64 `field:"monthlyPrice"` // 月价格
|
||||
YearlyPrice float64 `field:"yearlyPrice"` // 年价格
|
||||
Order uint32 `field:"order"` // 排序
|
||||
Config dbs.JSON `field:"config"` // 配置
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSPlanOperator struct {
|
||||
Id any // ID
|
||||
Name any // 套餐名称
|
||||
IsOn any // 是否启用
|
||||
MonthlyPrice any // 月价格
|
||||
YearlyPrice any // 年价格
|
||||
Order any // 排序
|
||||
Config any // 配置
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewNSPlanOperator() *NSPlanOperator {
|
||||
return &NSPlanOperator{}
|
||||
}
|
||||
1
internal/db/models/nameservers/ns_plan_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_plan_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
@@ -5,11 +5,11 @@ type NSRecordHourlyStat struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||
DomainId uint32 `field:"domainId"` // 域名ID
|
||||
DomainId uint64 `field:"domainId"` // 域名ID
|
||||
RecordId uint64 `field:"recordId"` // 记录ID
|
||||
Day string `field:"day"` // YYYYMMDD
|
||||
Hour string `field:"hour"` // YYYYMMDDHH
|
||||
CountRequests uint32 `field:"countRequests"` // 请求数
|
||||
CountRequests uint64 `field:"countRequests"` // 请求数
|
||||
Bytes uint64 `field:"bytes"` // 流量
|
||||
}
|
||||
|
||||
|
||||
28
internal/db/models/nameservers/ns_user_plan_model.go
Normal file
28
internal/db/models/nameservers/ns_user_plan_model.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package nameservers
|
||||
|
||||
// NSUserPlan 用户套餐
|
||||
type NSUserPlan struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
PlanId uint32 `field:"planId"` // 套餐ID
|
||||
DayFrom string `field:"dayFrom"` // YYYYMMDD
|
||||
DayTo string `field:"dayTo"` // YYYYMMDD
|
||||
PeriodUnit string `field:"periodUnit"` // monthly|yearly
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NSUserPlanOperator struct {
|
||||
Id any // ID
|
||||
UserId any // 用户ID
|
||||
PlanId any // 套餐ID
|
||||
DayFrom any // YYYYMMDD
|
||||
DayTo any // YYYYMMDD
|
||||
PeriodUnit any // monthly|yearly
|
||||
CreatedAt any // 创建时间
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewNSUserPlanOperator() *NSUserPlanOperator {
|
||||
return &NSUserPlanOperator{}
|
||||
}
|
||||
1
internal/db/models/nameservers/ns_user_plan_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_user_plan_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package nameservers
|
||||
@@ -125,13 +125,13 @@ func (this *NodeClusterDAO) FindAllEnableClusterIds(tx *dbs.Tx) (result []int64,
|
||||
}
|
||||
|
||||
// CreateCluster 创建集群
|
||||
func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string, grantId int64, installDir string, dnsDomainId int64, dnsName string, cachePolicyId int64, httpFirewallPolicyId int64, systemServices map[string]maps.Map) (clusterId int64, err error) {
|
||||
func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string, grantId int64, installDir string, dnsDomainId int64, dnsName string, dnsTTL int32, cachePolicyId int64, httpFirewallPolicyId int64, systemServices map[string]maps.Map, globalServerConfig *serverconfigs.GlobalServerConfig, autoInstallNftables bool) (clusterId int64, err error) {
|
||||
uniqueId, err := this.GenUniqueId(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
secret := rands.String(32)
|
||||
var secret = rands.String(32)
|
||||
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleCluster)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -149,9 +149,9 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
||||
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
||||
NodesAutoSync: true,
|
||||
ServersAutoSync: true,
|
||||
CNameRecords: []string{},
|
||||
CNameAsDomain: true,
|
||||
TTL: 0,
|
||||
CNAMERecords: []string{},
|
||||
CNAMEAsDomain: true,
|
||||
TTL: dnsTTL,
|
||||
IncludingLnNodes: true,
|
||||
}
|
||||
dnsJSON, err := json.Marshal(dnsConfig)
|
||||
@@ -173,10 +173,21 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
||||
}
|
||||
op.SystemServices = systemServicesJSON
|
||||
|
||||
// 全局服务配置
|
||||
if globalServerConfig == nil {
|
||||
globalServerConfig = serverconfigs.DefaultGlobalServerConfig()
|
||||
}
|
||||
globalServerConfigJSON, err := json.Marshal(globalServerConfig)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.GlobalServerConfig = globalServerConfigJSON
|
||||
|
||||
op.UseAllAPINodes = 1
|
||||
op.ApiNodes = "[]"
|
||||
op.UniqueId = uniqueId
|
||||
op.Secret = secret
|
||||
op.AutoInstallNftables = autoInstallNftables
|
||||
op.State = NodeClusterStateEnabled
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
@@ -187,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) 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")
|
||||
}
|
||||
@@ -204,6 +215,25 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
|
||||
op.NodeMaxThreads = nodeMaxThreads
|
||||
op.AutoOpenPorts = autoOpenPorts
|
||||
|
||||
if clockConfig != nil {
|
||||
clockJSON, err := json.Marshal(clockConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.Clock = clockJSON
|
||||
}
|
||||
|
||||
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
|
||||
@@ -432,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)
|
||||
@@ -444,7 +495,7 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cach
|
||||
|
||||
one, err := this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("id", "name", "dnsName", "dnsDomainId", "dns", "isOn").
|
||||
Result("id", "name", "dnsName", "dnsDomainId", "dns", "isOn", "state").
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -488,10 +539,16 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
|
||||
|
||||
var oldCluster = oldOne.(*NodeCluster)
|
||||
var oldDNSDomainId = int64(oldCluster.DnsDomainId)
|
||||
var shouldRemoveOld = false
|
||||
if (oldDNSDomainId > 0 && oldDNSDomainId != dnsDomainId) || (oldCluster.DnsName != dnsName) {
|
||||
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
|
||||
if err != nil {
|
||||
return err
|
||||
if oldDNSDomainId == dnsDomainId {
|
||||
// 如果只是换子域名,需要在新的域名添加之前,先删除老的子域名,防止无法添加CNAME
|
||||
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
shouldRemoveOld = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -507,9 +564,9 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
|
||||
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
||||
NodesAutoSync: nodesAutoSync,
|
||||
ServersAutoSync: serversAutoSync,
|
||||
CNameRecords: cnameRecords,
|
||||
CNAMERecords: cnameRecords,
|
||||
TTL: ttl,
|
||||
CNameAsDomain: cnameAsDomain,
|
||||
CNAMEAsDomain: cnameAsDomain,
|
||||
IncludingLnNodes: includingLnNodes,
|
||||
}
|
||||
dnsJSON, err := json.Marshal(dnsConfig)
|
||||
@@ -526,7 +583,20 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyDNSUpdate(tx, clusterId)
|
||||
err = this.NotifyDNSUpdate(tx, clusterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除老的记录
|
||||
if shouldRemoveOld {
|
||||
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindClusterAdminId 查找集群所属管理员
|
||||
@@ -926,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").
|
||||
Result("id", "name", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection", "clock", "globalServerConfig", "autoInstallNftables").
|
||||
Find()
|
||||
if err != nil || cluster == nil {
|
||||
return nil, err
|
||||
@@ -1091,12 +1161,55 @@ 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 查询全局服务配置
|
||||
func (this *NodeClusterDAO) FindClusterGlobalServerConfig(tx *dbs.Tx, clusterId int64) (*serverconfigs.GlobalServerConfig, error) {
|
||||
configJSON, err := this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("globalServerConfig").
|
||||
FindJSONCol()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var config = serverconfigs.DefaultGlobalServerConfig()
|
||||
if IsNull(configJSON) {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
err = json.Unmarshal(configJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// UpdateClusterGlobalServerConfig 修改全局服务配置
|
||||
func (this *NodeClusterDAO) UpdateClusterGlobalServerConfig(tx *dbs.Tx, clusterId int64, config *serverconfigs.GlobalServerConfig) error {
|
||||
if config == nil {
|
||||
config = serverconfigs.DefaultGlobalServerConfig()
|
||||
}
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("globalServerConfig", configJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
@@ -30,45 +31,54 @@ type NodeCluster struct {
|
||||
SystemServices dbs.JSON `field:"systemServices"` // 系统服务设置
|
||||
TimeZone string `field:"timeZone"` // 时区
|
||||
NodeMaxThreads uint32 `field:"nodeMaxThreads"` // 节点最大线程数
|
||||
DdosProtection dbs.JSON `field:"ddosProtection"` // DDOS端口
|
||||
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
|
||||
AutoOpenPorts uint8 `field:"autoOpenPorts"` // 是否自动尝试开放端口
|
||||
IsPinned bool `field:"isPinned"` // 是否置顶
|
||||
Webp dbs.JSON `field:"webp"` // WebP设置
|
||||
Uam dbs.JSON `field:"uam"` // UAM设置
|
||||
Clock dbs.JSON `field:"clock"` // 时钟配置
|
||||
GlobalServerConfig dbs.JSON `field:"globalServerConfig"` // 全局服务配置
|
||||
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
|
||||
AutoInstallNftables bool `field:"autoInstallNftables"` // 自动安装nftables
|
||||
}
|
||||
|
||||
type NodeClusterOperator struct {
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
IsOn interface{} // 是否启用
|
||||
Name interface{} // 名称
|
||||
UseAllAPINodes interface{} // 是否使用所有API节点
|
||||
ApiNodes interface{} // 使用的API节点
|
||||
InstallDir interface{} // 安装目录
|
||||
Order interface{} // 排序
|
||||
CreatedAt interface{} // 创建时间
|
||||
GrantId interface{} // 默认认证方式
|
||||
State interface{} // 状态
|
||||
AutoRegister interface{} // 是否开启自动注册
|
||||
UniqueId interface{} // 唯一ID
|
||||
Secret interface{} // 密钥
|
||||
HealthCheck interface{} // 健康检查
|
||||
DnsName interface{} // DNS名称
|
||||
DnsDomainId interface{} // 域名ID
|
||||
Dns interface{} // DNS配置
|
||||
Toa interface{} // TOA配置
|
||||
CachePolicyId interface{} // 缓存策略ID
|
||||
HttpFirewallPolicyId interface{} // WAF策略ID
|
||||
AccessLog interface{} // 访问日志设置
|
||||
SystemServices interface{} // 系统服务设置
|
||||
TimeZone interface{} // 时区
|
||||
NodeMaxThreads interface{} // 节点最大线程数
|
||||
DdosProtection interface{} // DDOS端口
|
||||
AutoOpenPorts interface{} // 是否自动尝试开放端口
|
||||
IsPinned interface{} // 是否置顶
|
||||
Webp interface{} // WebP设置
|
||||
Uam interface{} // UAM设置
|
||||
Id any // ID
|
||||
AdminId any // 管理员ID
|
||||
UserId any // 用户ID
|
||||
IsOn any // 是否启用
|
||||
Name any // 名称
|
||||
UseAllAPINodes any // 是否使用所有API节点
|
||||
ApiNodes any // 使用的API节点
|
||||
InstallDir any // 安装目录
|
||||
Order any // 排序
|
||||
CreatedAt any // 创建时间
|
||||
GrantId any // 默认认证方式
|
||||
SshParams any // SSH默认参数
|
||||
State any // 状态
|
||||
AutoRegister any // 是否开启自动注册
|
||||
UniqueId any // 唯一ID
|
||||
Secret any // 密钥
|
||||
HealthCheck any // 健康检查
|
||||
DnsName any // DNS名称
|
||||
DnsDomainId any // 域名ID
|
||||
Dns any // DNS配置
|
||||
Toa any // TOA配置
|
||||
CachePolicyId any // 缓存策略ID
|
||||
HttpFirewallPolicyId any // WAF策略ID
|
||||
AccessLog any // 访问日志设置
|
||||
SystemServices any // 系统服务设置
|
||||
TimeZone any // 时区
|
||||
NodeMaxThreads any // 节点最大线程数
|
||||
DdosProtection any // DDoS防护设置
|
||||
AutoOpenPorts any // 是否自动尝试开放端口
|
||||
IsPinned any // 是否置顶
|
||||
Webp any // WebP设置
|
||||
Uam any // UAM设置
|
||||
Clock any // 时钟配置
|
||||
GlobalServerConfig any // 全局服务配置
|
||||
AutoRemoteStart any // 自动远程启动
|
||||
AutoInstallNftables any // 自动安装nftables
|
||||
}
|
||||
|
||||
func NewNodeClusterOperator() *NodeClusterOperator {
|
||||
|
||||
@@ -2,7 +2,10 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
)
|
||||
|
||||
@@ -10,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
|
||||
@@ -50,3 +45,27 @@ func (this *NodeCluster) HasDDoSProtection() bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DecodeClock 解析时钟配置
|
||||
func (this *NodeCluster) DecodeClock() *nodeconfigs.ClockConfig {
|
||||
var clock = nodeconfigs.DefaultClockConfig()
|
||||
if IsNotNull(this.Clock) {
|
||||
err := json.Unmarshal(this.Clock, clock)
|
||||
if err != nil {
|
||||
remotelogs.Error("NodeCluster.DecodeClock()", err.Error())
|
||||
}
|
||||
}
|
||||
return clock
|
||||
}
|
||||
|
||||
// DecodeGlobalServerConfig 解析全局服务配置
|
||||
func (this *NodeCluster) DecodeGlobalServerConfig() *serverconfigs.GlobalServerConfig {
|
||||
var config = serverconfigs.DefaultGlobalServerConfig()
|
||||
if IsNotNull(this.GlobalServerConfig) {
|
||||
err := json.Unmarshal(this.GlobalServerConfig, config)
|
||||
if err != nil {
|
||||
remotelogs.Error("NodeCluster.DecodeGlobalServerConfig()", err.Error())
|
||||
}
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
@@ -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).
|
||||
@@ -140,18 +137,9 @@ func (this *NodeDAO) FindNodeName(tx *dbs.Tx, id int64) (string, error) {
|
||||
|
||||
// CreateNode 创建节点
|
||||
func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterId int64, groupId int64, regionId int64) (nodeId int64, err error) {
|
||||
// 检查节点数量
|
||||
if teaconst.MaxNodes > 0 {
|
||||
count, err := this.Query(tx).
|
||||
State(NodeStateEnabled).
|
||||
Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)").
|
||||
Count()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if int64(teaconst.MaxNodes) <= count {
|
||||
return 0, errors.New("[企业版]超出最大节点数限制:" + types.String(teaconst.MaxNodes) + ",请购买更多配额")
|
||||
}
|
||||
err = this.CheckNodesLimit(tx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uniqueId, err := this.GenUniqueId(tx)
|
||||
@@ -176,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 {
|
||||
@@ -199,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")
|
||||
}
|
||||
@@ -259,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
|
||||
@@ -958,17 +949,18 @@ 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,
|
||||
}
|
||||
|
||||
// API节点IP
|
||||
@@ -985,7 +977,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, cacheMap, true, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1085,6 +1077,16 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
||||
}
|
||||
}
|
||||
|
||||
// 时钟
|
||||
if config.Clock == nil {
|
||||
config.Clock = nodeCluster.DecodeClock()
|
||||
}
|
||||
|
||||
// 全局配置
|
||||
if config.GlobalServerConfig == nil {
|
||||
config.GlobalServerConfig = nodeCluster.DecodeGlobalServerConfig()
|
||||
}
|
||||
|
||||
// 最大线程数、TCP连接数
|
||||
if clusterIndex == 0 {
|
||||
config.MaxThreads = int(nodeCluster.NodeMaxThreads)
|
||||
@@ -1112,6 +1114,11 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
||||
config.UAMPolicies[clusterId] = uamPolicy
|
||||
}
|
||||
|
||||
// 自动安装nftables
|
||||
if clusterIndex == 0 {
|
||||
config.AutoInstallNftables = nodeCluster.AutoInstallNftables
|
||||
}
|
||||
|
||||
clusterIndex++
|
||||
}
|
||||
|
||||
@@ -1273,36 +1280,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 计算使用某个认证的节点数量
|
||||
@@ -1411,6 +1421,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 {
|
||||
@@ -1969,7 +2022,7 @@ 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, 0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1981,7 +2034,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, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1997,7 +2050,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
|
||||
}
|
||||
|
||||
12
internal/db/models/node_dao_limit.go
Normal file
12
internal/db/models/node_dao_limit.go
Normal file
@@ -0,0 +1,12 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
func (this *NodeDAO) CheckNodesLimit(tx *dbs.Tx) error {
|
||||
return nil
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,6 +391,13 @@ func (this *NodeLogDAO) DeleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.N
|
||||
if clusterId <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行钩子
|
||||
err := this.deleteNodeLogsWithCluster(tx, role, clusterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var query = this.Query(tx).
|
||||
Attr("role", role)
|
||||
|
||||
@@ -398,13 +405,10 @@ func (this *NodeLogDAO) DeleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.N
|
||||
case nodeconfigs.NodeRoleNode:
|
||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE clusterId=:clusterId)")
|
||||
query.Param("clusterId", clusterId)
|
||||
case nodeconfigs.NodeRoleDNS:
|
||||
query.Where("nodeId IN (SELECT id FROM " + SharedNSNodeDAO.Table + " WHERE clusterId=:clusterId)")
|
||||
query.Param("clusterId", clusterId)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := query.Delete()
|
||||
_, err = query.Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
13
internal/db/models/node_log_dao_ext.go
Normal file
13
internal/db/models/node_log_dao_ext.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
func (this *NodeLogDAO) deleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.NodeRole, clusterId int64) error {
|
||||
return nil
|
||||
}
|
||||
@@ -155,32 +155,3 @@ func (this *NodeLoginDAO) FindFrequentPorts(tx *dbs.Tx) ([]int32, error) {
|
||||
}
|
||||
return ports, nil
|
||||
}
|
||||
|
||||
func (this *NodeLoginDAO) FindFrequentGrantIds(tx *dbs.Tx, nodeClusterId int64, nsClusterId int64) ([]int64, error) {
|
||||
var query = this.Query(tx).
|
||||
Attr("state", NodeLoginStateEnabled).
|
||||
Result("JSON_EXTRACT(params, '$.grantId') as `grantId`", "COUNT(*) AS c").
|
||||
Having("grantId>0").
|
||||
Desc("c").
|
||||
Limit(3).
|
||||
Group("grantId")
|
||||
if nodeClusterId > 0 {
|
||||
query.Attr("role", nodeconfigs.NodeRoleNode)
|
||||
query.Where("(nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
|
||||
Param("clusterId", nodeClusterId)
|
||||
} else if nsClusterId > 0 {
|
||||
query.Attr("role", nodeconfigs.NodeRoleDNS)
|
||||
query.Where("(nodeId IN (SELECT id FROM "+SharedNSNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
|
||||
Param("clusterId", nsClusterId)
|
||||
}
|
||||
ones, _, err := query.
|
||||
FindOnes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var grantIds = []int64{}
|
||||
for _, one := range ones {
|
||||
grantIds = append(grantIds, one.GetInt64("grantId"))
|
||||
}
|
||||
return grantIds, nil
|
||||
}
|
||||
|
||||
36
internal/db/models/node_login_dao_ext.go
Normal file
36
internal/db/models/node_login_dao_ext.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
func (this *NodeLoginDAO) FindFrequentGrantIds(tx *dbs.Tx, nodeClusterId int64, nsClusterId int64) ([]int64, error) {
|
||||
var query = this.Query(tx).
|
||||
Attr("state", NodeLoginStateEnabled).
|
||||
Result("JSON_EXTRACT(params, '$.grantId') as `grantId`", "COUNT(*) AS c").
|
||||
Having("grantId>0").
|
||||
Desc("c").
|
||||
Limit(3).
|
||||
Group("grantId")
|
||||
if nodeClusterId > 0 {
|
||||
query.Attr("role", nodeconfigs.NodeRoleNode)
|
||||
query.Where("(nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
|
||||
Param("clusterId", nodeClusterId)
|
||||
} else if nsClusterId > 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ones, _, err := query.
|
||||
FindOnes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var grantIds = []int64{}
|
||||
for _, one := range ones {
|
||||
grantIds = append(grantIds, one.GetInt64("grantId"))
|
||||
}
|
||||
return grantIds, nil
|
||||
}
|
||||
@@ -40,6 +40,7 @@ type Node struct {
|
||||
MaxCacheMemoryCapacity dbs.JSON `field:"maxCacheMemoryCapacity"` // 内存缓存容量
|
||||
CacheDiskDir string `field:"cacheDiskDir"` // 缓存目录
|
||||
DnsResolver dbs.JSON `field:"dnsResolver"` // DNS解析器
|
||||
EnableIPLists bool `field:"enableIPLists"` // 启用IP名单
|
||||
}
|
||||
|
||||
type NodeOperator struct {
|
||||
@@ -79,6 +80,7 @@ type NodeOperator struct {
|
||||
MaxCacheMemoryCapacity any // 内存缓存容量
|
||||
CacheDiskDir any // 缓存目录
|
||||
DnsResolver any // DNS解析器
|
||||
EnableIPLists any // 启用IP名单
|
||||
}
|
||||
|
||||
func NewNodeOperator() *NodeOperator {
|
||||
|
||||
@@ -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,12 +14,16 @@ import (
|
||||
type NodeTaskType = string
|
||||
|
||||
const (
|
||||
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
|
||||
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
|
||||
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
|
||||
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
|
||||
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged"
|
||||
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged"
|
||||
// CDN相关
|
||||
|
||||
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
|
||||
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
|
||||
NodeTaskTypeGlobalServerConfigChanged NodeTaskType = "globalServerConfigChanged" // 全局服务设置变化
|
||||
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged" // IP条目变更
|
||||
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged" // 节点版本变化
|
||||
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged" // 脚本配置变化
|
||||
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged" // 节点级别变化
|
||||
NodeTaskTypeUserServersStateChanged NodeTaskType = "userServersStateChanged" // 用户服务状态变化
|
||||
|
||||
// NS相关
|
||||
|
||||
@@ -53,17 +57,25 @@ 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, version int64) 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)
|
||||
}
|
||||
|
||||
var updatedAt = time.Now().Unix()
|
||||
_, _, err := this.Query(tx).
|
||||
InsertOrUpdate(maps.Map{
|
||||
"role": role,
|
||||
"clusterId": clusterId,
|
||||
"nodeId": nodeId,
|
||||
"userId": userId,
|
||||
"serverId": serverId,
|
||||
"type": taskType,
|
||||
"uniqueId": uniqueId,
|
||||
@@ -86,17 +98,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
|
||||
|
||||
// 用户信息
|
||||
// 没有直接加入到 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,
|
||||
@@ -120,7 +140,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
|
||||
@@ -139,7 +159,7 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, ser
|
||||
|
||||
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, version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -158,45 +178,6 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, ser
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractNSClusterTask 分解NS节点集群任务
|
||||
func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
|
||||
nodeIds, err := SharedNSNodeDAO.FindAllNodeIdsMatch(tx, clusterId, true, configutils.BoolStateYes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Attr("role", nodeconfigs.NodeRoleDNS).
|
||||
Attr("clusterId", clusterId).
|
||||
Param("clusterIdString", types.String(clusterId)).
|
||||
Where("nodeId > 0").
|
||||
Attr("type", taskType).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var version = time.Now().UnixNano()
|
||||
for _, nodeId := range nodeIds {
|
||||
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, 0, taskType, version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err = this.Query(tx).
|
||||
Attr("role", nodeconfigs.NodeRoleDNS).
|
||||
Attr("clusterId", clusterId).
|
||||
Attr("nodeId", 0).
|
||||
Attr("type", taskType).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractAllClusterTasks 分解所有集群任务
|
||||
func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx, role string) error {
|
||||
ones, err := this.Query(tx).
|
||||
@@ -207,11 +188,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
|
||||
}
|
||||
|
||||
11
internal/db/models/node_task_dao_ext.go
Normal file
11
internal/db/models/node_task_dao_ext.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// ExtractNSClusterTask 分解NS节点集群任务
|
||||
func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
|
||||
return nil
|
||||
}
|
||||
@@ -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, 0)
|
||||
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 {
|
||||
|
||||
@@ -1,336 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NSAccessLogDAO dbs.DAO
|
||||
|
||||
func NewNSAccessLogDAO() *NSAccessLogDAO {
|
||||
return dbs.NewDAO(&NSAccessLogDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeNSAccessLogs",
|
||||
Model: new(NSAccessLog),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*NSAccessLogDAO)
|
||||
}
|
||||
|
||||
var SharedNSAccessLogDAO *NSAccessLogDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedNSAccessLogDAO = NewNSAccessLogDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// CreateNSAccessLogs 创建访问日志
|
||||
func (this *NSAccessLogDAO) CreateNSAccessLogs(tx *dbs.Tx, accessLogs []*pb.NSAccessLog) error {
|
||||
dao := randomNSAccessLogDAO()
|
||||
if dao == nil {
|
||||
dao = &NSAccessLogDAOWrapper{
|
||||
DAO: SharedNSAccessLogDAO,
|
||||
NodeId: 0,
|
||||
}
|
||||
}
|
||||
return this.CreateNSAccessLogsWithDAO(tx, dao, accessLogs)
|
||||
}
|
||||
|
||||
// CreateNSAccessLogsWithDAO 使用特定的DAO创建访问日志
|
||||
func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NSAccessLogDAOWrapper, accessLogs []*pb.NSAccessLog) error {
|
||||
if daoWrapper == nil {
|
||||
return errors.New("dao should not be nil")
|
||||
}
|
||||
if len(accessLogs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
dao := daoWrapper.DAO
|
||||
|
||||
// TODO 改成事务批量提交,以加快速度
|
||||
|
||||
for _, accessLog := range accessLogs {
|
||||
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
|
||||
table, err := findNSAccessLogTable(dao.Instance, day, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fields := map[string]interface{}{}
|
||||
fields["nodeId"] = accessLog.NsNodeId
|
||||
fields["domainId"] = accessLog.NsDomainId
|
||||
fields["recordId"] = accessLog.NsRecordId
|
||||
fields["createdAt"] = accessLog.Timestamp
|
||||
fields["requestId"] = accessLog.RequestId
|
||||
|
||||
content, err := json.Marshal(accessLog)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fields["content"] = content
|
||||
|
||||
_, err = dao.Query(tx).
|
||||
Table(table).
|
||||
Sets(fields).
|
||||
Insert()
|
||||
if err != nil {
|
||||
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
|
||||
if strings.Contains(err.Error(), "1146") {
|
||||
table, err = findNSAccessLogTable(dao.Instance, day, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dao.Query(tx).
|
||||
Table(table).
|
||||
Sets(fields).
|
||||
Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListAccessLogs 读取往前的 单页访问日志
|
||||
func (this *NSAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, clusterId int64, nodeId int64, domainId int64, recordId int64, recordType string, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, hasMore bool, err error) {
|
||||
if len(day) != 8 {
|
||||
return
|
||||
}
|
||||
|
||||
// 限制能查询的最大条数,防止占用内存过多
|
||||
if size > 1000 {
|
||||
size = 1000
|
||||
}
|
||||
|
||||
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, clusterId, nodeId, domainId, recordId, recordType, keyword, reverse)
|
||||
if err != nil || int64(len(result)) < size {
|
||||
return
|
||||
}
|
||||
|
||||
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, clusterId, nodeId, domainId, recordId, recordType, keyword, reverse)
|
||||
hasMore = len(moreResult) > 0
|
||||
return
|
||||
}
|
||||
|
||||
// 读取往前的单页访问日志
|
||||
func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, clusterId int64, nodeId int64, domainId int64, recordId int64, recordType string, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, err error) {
|
||||
if size <= 0 {
|
||||
return nil, lastRequestId, nil
|
||||
}
|
||||
|
||||
accessLogLocker.RLock()
|
||||
var daoList = []*NSAccessLogDAOWrapper{}
|
||||
for _, daoWrapper := range nsAccessLogDAOMapping {
|
||||
daoList = append(daoList, daoWrapper)
|
||||
}
|
||||
accessLogLocker.RUnlock()
|
||||
|
||||
if len(daoList) == 0 {
|
||||
daoList = []*NSAccessLogDAOWrapper{{
|
||||
DAO: SharedNSAccessLogDAO,
|
||||
NodeId: 0,
|
||||
}}
|
||||
}
|
||||
|
||||
// 检查是否有集群筛选条件
|
||||
var nodeIds []int64
|
||||
if clusterId > 0 && nodeId <= 0 {
|
||||
nodeIds, err = SharedNSNodeDAO.FindEnabledNodeIdsWithClusterId(tx, clusterId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(nodeIds) == 0 {
|
||||
// 没有任何节点则直接返回空
|
||||
return nil, "", nil
|
||||
}
|
||||
}
|
||||
|
||||
var locker = sync.Mutex{}
|
||||
|
||||
var count = len(daoList)
|
||||
var wg = &sync.WaitGroup{}
|
||||
wg.Add(count)
|
||||
for _, daoWrapper := range daoList {
|
||||
go func(daoWrapper *NSAccessLogDAOWrapper) {
|
||||
defer wg.Done()
|
||||
|
||||
dao := daoWrapper.DAO
|
||||
|
||||
tableName, exists, err := findNSAccessLogTableName(dao.Instance, day)
|
||||
if !exists {
|
||||
// 表格不存在则跳过
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
logs.Println("[DB_NODE]" + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var query = dao.Query(tx)
|
||||
|
||||
// 条件
|
||||
if nodeId > 0 {
|
||||
query.Attr("nodeId", nodeId)
|
||||
} else if clusterId > 0 {
|
||||
query.Attr("nodeId", nodeIds)
|
||||
query.Reuse(false)
|
||||
}
|
||||
if domainId > 0 {
|
||||
query.Attr("domainId", domainId)
|
||||
}
|
||||
if recordId > 0 {
|
||||
query.Attr("recordId", recordId)
|
||||
}
|
||||
|
||||
// offset
|
||||
if len(lastRequestId) > 0 {
|
||||
if !reverse {
|
||||
query.Where("requestId<:requestId").
|
||||
Param("requestId", lastRequestId)
|
||||
} else {
|
||||
query.Where("requestId>:requestId").
|
||||
Param("requestId", lastRequestId)
|
||||
}
|
||||
}
|
||||
|
||||
// keyword
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(JSON_EXTRACT(content, '$.remoteAddr') LIKE :keyword OR JSON_EXTRACT(content, '$.questionName') LIKE :keyword OR JSON_EXTRACT(content, '$.recordValue') LIKE :keyword)").
|
||||
Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
|
||||
// record type
|
||||
if len(recordType) > 0 {
|
||||
query.Where("JSON_EXTRACT(content, '$.questionType')=:recordType")
|
||||
query.Param("recordType", recordType)
|
||||
}
|
||||
|
||||
if !reverse {
|
||||
query.Desc("requestId")
|
||||
} else {
|
||||
query.Asc("requestId")
|
||||
}
|
||||
|
||||
// 开始查询
|
||||
ones, err := query.
|
||||
Table(tableName).
|
||||
Limit(size).
|
||||
FindAll()
|
||||
if err != nil {
|
||||
logs.Println("[DB_NODE]" + err.Error())
|
||||
return
|
||||
}
|
||||
locker.Lock()
|
||||
for _, one := range ones {
|
||||
accessLog := one.(*NSAccessLog)
|
||||
result = append(result, accessLog)
|
||||
}
|
||||
locker.Unlock()
|
||||
}(daoWrapper)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if len(result) == 0 {
|
||||
return nil, lastRequestId, nil
|
||||
}
|
||||
|
||||
// 按照requestId排序
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
if !reverse {
|
||||
return result[i].RequestId > result[j].RequestId
|
||||
} else {
|
||||
return result[i].RequestId < result[j].RequestId
|
||||
}
|
||||
})
|
||||
|
||||
if int64(len(result)) > size {
|
||||
result = result[:size]
|
||||
}
|
||||
|
||||
var requestId = result[len(result)-1].RequestId
|
||||
if reverse {
|
||||
lists.Reverse(result)
|
||||
}
|
||||
|
||||
if !reverse {
|
||||
return result, requestId, nil
|
||||
} else {
|
||||
return result, requestId, nil
|
||||
}
|
||||
}
|
||||
|
||||
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
||||
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
|
||||
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
|
||||
return nil, errors.New("invalid requestId")
|
||||
}
|
||||
|
||||
accessLogLocker.RLock()
|
||||
daoList := []*NSAccessLogDAOWrapper{}
|
||||
for _, daoWrapper := range nsAccessLogDAOMapping {
|
||||
daoList = append(daoList, daoWrapper)
|
||||
}
|
||||
accessLogLocker.RUnlock()
|
||||
|
||||
if len(daoList) == 0 {
|
||||
daoList = []*NSAccessLogDAOWrapper{{
|
||||
DAO: SharedNSAccessLogDAO,
|
||||
NodeId: 0,
|
||||
}}
|
||||
}
|
||||
|
||||
count := len(daoList)
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(count)
|
||||
var result *NSAccessLog = nil
|
||||
day := timeutil.FormatTime("Ymd", types.Int64(requestId[:10]))
|
||||
for _, daoWrapper := range daoList {
|
||||
go func(daoWrapper *NSAccessLogDAOWrapper) {
|
||||
defer wg.Done()
|
||||
|
||||
dao := daoWrapper.DAO
|
||||
|
||||
tableName, exists, err := findNSAccessLogTableName(dao.Instance, day)
|
||||
if err != nil {
|
||||
logs.Println("[DB_NODE]" + err.Error())
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
one, err := dao.Query(tx).
|
||||
Table(tableName).
|
||||
Attr("requestId", requestId).
|
||||
Find()
|
||||
if err != nil {
|
||||
logs.Println("[DB_NODE]" + err.Error())
|
||||
return
|
||||
}
|
||||
if one != nil {
|
||||
result = one.(*NSAccessLog)
|
||||
}
|
||||
}(daoWrapper)
|
||||
}
|
||||
wg.Wait()
|
||||
return result, nil
|
||||
}
|
||||
@@ -1,11 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -82,68 +78,6 @@ func (this *NSClusterDAO) FindEnabledNSClusterName(tx *dbs.Tx, id int64) (string
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateCluster 创建集群
|
||||
func (this *NSClusterDAO) CreateCluster(tx *dbs.Tx, name string, accessLogRefJSON []byte) (int64, error) {
|
||||
var op = NewNSClusterOperator()
|
||||
op.Name = name
|
||||
|
||||
if len(accessLogRefJSON) > 0 {
|
||||
op.AccessLog = accessLogRefJSON
|
||||
}
|
||||
|
||||
op.IsOn = true
|
||||
op.State = NSClusterStateEnabled
|
||||
|
||||
// 默认端口
|
||||
// TCP
|
||||
{
|
||||
var config = &serverconfigs.TCPProtocolConfig{}
|
||||
config.IsOn = true
|
||||
config.Listen = []*serverconfigs.NetworkAddressConfig{
|
||||
{
|
||||
Protocol: serverconfigs.ProtocolTCP,
|
||||
PortRange: "53",
|
||||
},
|
||||
}
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Tcp = configJSON
|
||||
}
|
||||
|
||||
// UDP
|
||||
{
|
||||
var config = &serverconfigs.UDPProtocolConfig{}
|
||||
config.IsOn = true
|
||||
config.Listen = []*serverconfigs.NetworkAddressConfig{
|
||||
{
|
||||
Protocol: serverconfigs.ProtocolUDP,
|
||||
PortRange: "53",
|
||||
},
|
||||
}
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Udp = configJSON
|
||||
}
|
||||
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// UpdateCluster 修改集群
|
||||
func (this *NSClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, isOn bool) error {
|
||||
if clusterId <= 0 {
|
||||
return errors.New("invalid clusterId")
|
||||
}
|
||||
var op = NewNSClusterOperator()
|
||||
op.Id = clusterId
|
||||
op.Name = name
|
||||
op.IsOn = isOn
|
||||
return this.Save(tx, op)
|
||||
}
|
||||
|
||||
// CountAllEnabledClusters 计算可用集群数量
|
||||
func (this *NSClusterDAO) CountAllEnabledClusters(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
@@ -189,23 +123,6 @@ func (this *NSClusterDAO) FindAllEnabledClusterIds(tx *dbs.Tx) ([]int64, error)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UpdateClusterAccessLog 设置访问日志
|
||||
func (this *NSClusterDAO) UpdateClusterAccessLog(tx *dbs.Tx, clusterId int64, accessLogJSON []byte) error {
|
||||
return this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("accessLog", accessLogJSON).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// FindClusterAccessLog 读取访问日志配置
|
||||
func (this *NSClusterDAO) FindClusterAccessLog(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
accessLog, err := this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("accessLog").
|
||||
FindStringCol("")
|
||||
return []byte(accessLog), err
|
||||
}
|
||||
|
||||
// FindClusterGrantId 查找集群的认证ID
|
||||
func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
@@ -214,102 +131,6 @@ func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// UpdateRecursion 设置递归DNS
|
||||
func (this *NSClusterDAO) UpdateRecursion(tx *dbs.Tx, clusterId int64, recursionJSON []byte) error {
|
||||
err := this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("recursion", recursionJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, clusterId)
|
||||
}
|
||||
|
||||
// FindClusterRecursion 读取递归DNS配置
|
||||
func (this *NSClusterDAO) FindClusterRecursion(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
recursion, err := this.Query(tx).
|
||||
Result("recursion").
|
||||
Pk(clusterId).
|
||||
FindStringCol("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(recursion), nil
|
||||
}
|
||||
|
||||
// FindClusterTCP 查找集群的TCP设置
|
||||
func (this *NSClusterDAO) FindClusterTCP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("tcp").
|
||||
FindBytesCol()
|
||||
}
|
||||
|
||||
// UpdateClusterTCP 修改集群的TCP设置
|
||||
func (this *NSClusterDAO) UpdateClusterTCP(tx *dbs.Tx, clusterId int64, tcpConfig *serverconfigs.TCPProtocolConfig) error {
|
||||
tcpJSON, err := json.Marshal(tcpConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("tcp", tcpJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, clusterId)
|
||||
}
|
||||
|
||||
// FindClusterTLS 查找集群的TLS设置
|
||||
func (this *NSClusterDAO) FindClusterTLS(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("tls").
|
||||
FindBytesCol()
|
||||
}
|
||||
|
||||
// UpdateClusterTLS 修改集群的TLS设置
|
||||
func (this *NSClusterDAO) UpdateClusterTLS(tx *dbs.Tx, clusterId int64, tlsConfig *serverconfigs.TLSProtocolConfig) error {
|
||||
tlsJSON, err := json.Marshal(tlsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("tls", tlsJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, clusterId)
|
||||
}
|
||||
|
||||
// FindClusterUDP 查找集群的TCP设置
|
||||
func (this *NSClusterDAO) FindClusterUDP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("udp").
|
||||
FindBytesCol()
|
||||
}
|
||||
|
||||
// UpdateClusterUDP 修改集群的UDP设置
|
||||
func (this *NSClusterDAO) UpdateClusterUDP(tx *dbs.Tx, clusterId int64, udpConfig *serverconfigs.UDPProtocolConfig) error {
|
||||
udpJSON, err := json.Marshal(udpConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("udp", udpJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, clusterId)
|
||||
}
|
||||
|
||||
// CountAllClustersWithSSLPolicyIds 计算使用SSL策略的所有NS集群数量
|
||||
func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
|
||||
if len(sslPolicyIds) == 0 {
|
||||
@@ -326,46 +147,7 @@ func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicy
|
||||
Count()
|
||||
}
|
||||
|
||||
// FindClusterDDoSProtection 获取集群的DDoS设置
|
||||
func (this *NSClusterDAO) FindClusterDDoSProtection(tx *dbs.Tx, clusterId int64) (*ddosconfigs.ProtectionConfig, error) {
|
||||
one, err := this.Query(tx).
|
||||
Result("ddosProtection").
|
||||
Pk(clusterId).
|
||||
Find()
|
||||
if one == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*NSCluster).DecodeDDoSProtection(), nil
|
||||
}
|
||||
|
||||
// UpdateClusterDDoSProtection 设置集群的DDoS设置
|
||||
func (this *NSClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
|
||||
if clusterId <= 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
var op = NewNSClusterOperator()
|
||||
op.Id = clusterId
|
||||
|
||||
if ddosProtection == nil {
|
||||
op.DdosProtection = "{}"
|
||||
} else {
|
||||
ddosProtectionJSON, err := json.Marshal(ddosProtection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.DdosProtection = ddosProtectionJSON
|
||||
}
|
||||
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, NSNodeTaskTypeDDosProtectionChanged)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
@@ -4,33 +4,47 @@ import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// NSCluster 域名服务器集群
|
||||
type NSCluster struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Name string `field:"name"` // 集群名
|
||||
InstallDir string `field:"installDir"` // 安装目录
|
||||
State uint8 `field:"state"` // 状态
|
||||
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
|
||||
GrantId uint32 `field:"grantId"` // 授权ID
|
||||
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
|
||||
Tcp dbs.JSON `field:"tcp"` // TCP设置
|
||||
Tls dbs.JSON `field:"tls"` // TLS设置
|
||||
Udp dbs.JSON `field:"udp"` // UDP设置
|
||||
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Name string `field:"name"` // 集群名
|
||||
InstallDir string `field:"installDir"` // 安装目录
|
||||
State uint8 `field:"state"` // 状态
|
||||
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
|
||||
GrantId uint32 `field:"grantId"` // 授权ID
|
||||
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
|
||||
Tcp dbs.JSON `field:"tcp"` // TCP设置
|
||||
Tls dbs.JSON `field:"tls"` // TLS设置
|
||||
Udp dbs.JSON `field:"udp"` // UDP设置
|
||||
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
|
||||
Hosts dbs.JSON `field:"hosts"` // DNS主机地址
|
||||
Soa dbs.JSON `field:"soa"` // SOA配置
|
||||
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
|
||||
TimeZone string `field:"timeZone"` // 时区
|
||||
Answer dbs.JSON `field:"answer"` // 应答设置
|
||||
SoaSerial uint64 `field:"soaSerial"` // SOA序列号
|
||||
Email string `field:"email"` // 管理员邮箱
|
||||
}
|
||||
|
||||
type NSClusterOperator struct {
|
||||
Id any // ID
|
||||
IsOn any // 是否启用
|
||||
Name any // 集群名
|
||||
InstallDir any // 安装目录
|
||||
State any // 状态
|
||||
AccessLog any // 访问日志配置
|
||||
GrantId any // 授权ID
|
||||
Recursion any // 递归DNS设置
|
||||
Tcp any // TCP设置
|
||||
Tls any // TLS设置
|
||||
Udp any // UDP设置
|
||||
DdosProtection any // DDoS防护设置
|
||||
Id any // ID
|
||||
IsOn any // 是否启用
|
||||
Name any // 集群名
|
||||
InstallDir any // 安装目录
|
||||
State any // 状态
|
||||
AccessLog any // 访问日志配置
|
||||
GrantId any // 授权ID
|
||||
Recursion any // 递归DNS设置
|
||||
Tcp any // TCP设置
|
||||
Tls any // TLS设置
|
||||
Udp any // UDP设置
|
||||
DdosProtection any // DDoS防护设置
|
||||
Hosts any // DNS主机地址
|
||||
Soa any // SOA配置
|
||||
AutoRemoteStart any // 自动远程启动
|
||||
TimeZone any // 时区
|
||||
Answer any // 应答设置
|
||||
SoaSerial any // SOA序列号
|
||||
Email any // 管理员邮箱
|
||||
}
|
||||
|
||||
func NewNSClusterOperator() *NSClusterOperator {
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,7 @@ func (this *NSCluster) DecodeDDoSProtection() *ddosconfigs.ProtectionConfig {
|
||||
var result = &ddosconfigs.ProtectionConfig{}
|
||||
err := json.Unmarshal(this.DdosProtection, &result)
|
||||
if err != nil {
|
||||
// ignore err
|
||||
remotelogs.Error("NSCluster.DecodeDDoSProtection", "decode failed: "+err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -26,4 +27,4 @@ func (this *NSCluster) HasDDoSProtection() bool {
|
||||
return config.IsOn()
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
//go:build !plus
|
||||
|
||||
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"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -95,110 +88,6 @@ func (this *NSNodeDAO) FindEnabledNSNodeName(tx *dbs.Tx, nodeId int64) (string,
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindAllEnabledNodesWithClusterId 查找一个集群下的所有节点
|
||||
func (this *NSNodeDAO) FindAllEnabledNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NSNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Attr("clusterId", clusterId).
|
||||
State(NSNodeStateEnabled).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CountAllEnabledNodes 所有集群的可用的节点数量
|
||||
func (this *NSNodeDAO) CountAllEnabledNodes(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(NSNodeStateEnabled).
|
||||
Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)").
|
||||
Count()
|
||||
}
|
||||
|
||||
// CountAllOfflineNodes 计算离线节点数量
|
||||
func (this *NSNodeDAO) CountAllOfflineNodes(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(NSNodeStateEnabled).
|
||||
Where("(status IS NULL OR JSON_EXTRACT(status, '$.updatedAt')<UNIX_TIMESTAMP()-120)").
|
||||
Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)").
|
||||
Count()
|
||||
}
|
||||
|
||||
// CountAllEnabledNodesMatch 计算满足条件的节点数量
|
||||
func (this *NSNodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string) (int64, error) {
|
||||
query := this.Query(tx)
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
}
|
||||
// 安装状态
|
||||
switch installState {
|
||||
case configutils.BoolStateAll:
|
||||
// 所有
|
||||
case configutils.BoolStateYes:
|
||||
query.Attr("isInstalled", 1)
|
||||
case configutils.BoolStateNo:
|
||||
query.Attr("isInstalled", 0)
|
||||
}
|
||||
|
||||
// 在线状态
|
||||
switch activeState {
|
||||
case configutils.BoolStateAll:
|
||||
// 所有
|
||||
case configutils.BoolStateYes:
|
||||
query.Where("(isActive=1 AND JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60)")
|
||||
case configutils.BoolStateNo:
|
||||
query.Where("(isActive=0 OR status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword)").
|
||||
Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
|
||||
return query.
|
||||
State(NSNodeStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// ListAllEnabledNodesMatch 列出单页匹配的节点
|
||||
func (this *NSNodeDAO) ListAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string, offset int64, size int64) (result []*NSNode, err error) {
|
||||
query := this.Query(tx)
|
||||
|
||||
// 安装状态
|
||||
switch installState {
|
||||
case configutils.BoolStateAll:
|
||||
// 所有
|
||||
case configutils.BoolStateYes:
|
||||
query.Attr("isInstalled", 1)
|
||||
case configutils.BoolStateNo:
|
||||
query.Attr("isInstalled", 0)
|
||||
}
|
||||
|
||||
// 在线状态
|
||||
switch activeState {
|
||||
case configutils.BoolStateAll:
|
||||
// 所有
|
||||
case configutils.BoolStateYes:
|
||||
query.Where("(isActive=1 AND JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60)")
|
||||
case configutils.BoolStateNo:
|
||||
query.Where("(isActive=0 OR status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||
}
|
||||
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword)").
|
||||
Param("keyword", dbutils.QuoteLike(keyword))
|
||||
}
|
||||
_, err = query.
|
||||
State(NSNodeStateEnabled).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
DescPk().
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CountAllLowerVersionNodesWithClusterId 计算单个集群中所有低于某个版本的节点数量
|
||||
func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId int64, os string, arch string, version string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
@@ -214,73 +103,6 @@ func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, cluste
|
||||
Count()
|
||||
}
|
||||
|
||||
// CreateNode 创建节点
|
||||
func (this *NSNodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterId int64) (nodeId int64, err error) {
|
||||
uniqueId, err := this.GenUniqueId(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
secret := rands.String(32)
|
||||
|
||||
// 保存API Token
|
||||
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleDNS)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var op = NewNSNodeOperator()
|
||||
op.AdminId = adminId
|
||||
op.Name = name
|
||||
op.UniqueId = uniqueId
|
||||
op.Secret = secret
|
||||
op.ClusterId = clusterId
|
||||
op.IsOn = 1
|
||||
op.State = NSNodeStateEnabled
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 通知节点更新
|
||||
nodeId = types.Int64(op.Id)
|
||||
err = this.NotifyUpdate(tx, nodeId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 通知DNS更新
|
||||
err = this.NotifyDNSUpdate(tx, nodeId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return nodeId, nil
|
||||
}
|
||||
|
||||
// UpdateNode 修改节点
|
||||
func (this *NSNodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, isOn bool) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
var op = NewNSNodeOperator()
|
||||
op.Id = nodeId
|
||||
op.Name = name
|
||||
op.ClusterId = clusterId
|
||||
op.IsOn = isOn
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = this.NotifyUpdate(tx, nodeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyDNSUpdate(tx, nodeId)
|
||||
}
|
||||
|
||||
// FindEnabledNodeIdWithUniqueId 根据唯一ID获取节点ID
|
||||
func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
@@ -290,37 +112,6 @@ func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindNodeInstallStatus 查询节点的安装状态
|
||||
func (this *NSNodeDAO) FindNodeInstallStatus(tx *dbs.Tx, nodeId int64) (*NodeInstallStatus, error) {
|
||||
node, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Result("installStatus", "isInstalled").
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node == nil {
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
|
||||
installStatus := node.(*NSNode).InstallStatus
|
||||
isInstalled := node.(*NSNode).IsInstalled
|
||||
if len(installStatus) == 0 {
|
||||
return NewNodeInstallStatus(), nil
|
||||
}
|
||||
|
||||
status := &NodeInstallStatus{}
|
||||
err = json.Unmarshal(installStatus, status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isInstalled {
|
||||
status.IsFinished = true
|
||||
status.IsOk = true
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// GenUniqueId 生成唯一ID
|
||||
func (this *NSNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
||||
for {
|
||||
@@ -377,131 +168,6 @@ func (this *NSNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (in
|
||||
Count()
|
||||
}
|
||||
|
||||
// ComposeNodeConfig 组合节点配置
|
||||
func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.NSNodeConfig, error) {
|
||||
if nodeId <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
node, err := this.FindEnabledNSNode(tx, nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
cluster, err := SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(node.ClusterId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cluster == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var config = &dnsconfigs.NSNodeConfig{
|
||||
Id: int64(node.Id),
|
||||
NodeId: node.UniqueId,
|
||||
Secret: node.Secret,
|
||||
ClusterId: int64(node.ClusterId),
|
||||
}
|
||||
|
||||
// 访问日志
|
||||
// 全局配置
|
||||
{
|
||||
globalValue, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeNSAccessLogSetting)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(globalValue) > 0 {
|
||||
var ref = &dnsconfigs.NSAccessLogRef{}
|
||||
err = json.Unmarshal(globalValue, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.AccessLogRef = ref
|
||||
}
|
||||
|
||||
// 集群配置
|
||||
if len(cluster.AccessLog) > 0 {
|
||||
ref := &dnsconfigs.NSAccessLogRef{}
|
||||
err = json.Unmarshal(cluster.AccessLog, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ref.IsPrior {
|
||||
config.AccessLogRef = ref
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 递归DNS配置
|
||||
if IsNotNull(cluster.Recursion) {
|
||||
var recursionConfig = &dnsconfigs.RecursionConfig{}
|
||||
err = json.Unmarshal(cluster.Recursion, recursionConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.RecursionConfig = recursionConfig
|
||||
}
|
||||
|
||||
// TCP
|
||||
if IsNotNull(cluster.Tcp) {
|
||||
var tcpConfig = &serverconfigs.TCPProtocolConfig{}
|
||||
err = json.Unmarshal(cluster.Tcp, tcpConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.TCP = tcpConfig
|
||||
}
|
||||
|
||||
// TLS
|
||||
if IsNotNull(cluster.Tls) {
|
||||
var tlsConfig = &serverconfigs.TLSProtocolConfig{}
|
||||
err = json.Unmarshal(cluster.Tls, tlsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// SSL
|
||||
if tlsConfig.SSLPolicyRef != nil {
|
||||
sslPolicyConfig, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, tlsConfig.SSLPolicyRef.SSLPolicyId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sslPolicyConfig != nil {
|
||||
tlsConfig.SSLPolicy = sslPolicyConfig
|
||||
}
|
||||
}
|
||||
|
||||
config.TLS = tlsConfig
|
||||
}
|
||||
|
||||
// UDP
|
||||
if IsNotNull(cluster.Udp) {
|
||||
var udpConfig = &serverconfigs.UDPProtocolConfig{}
|
||||
err = json.Unmarshal(cluster.Udp, udpConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.UDP = udpConfig
|
||||
}
|
||||
|
||||
// DDoS
|
||||
config.DDoSProtection = cluster.DecodeDDoSProtection()
|
||||
|
||||
// DDoS Protection
|
||||
var ddosProtection = node.DecodeDDoSProtection()
|
||||
if ddosProtection != nil {
|
||||
if config.DDoSProtection == nil {
|
||||
config.DDoSProtection = ddosProtection
|
||||
} else {
|
||||
config.DDoSProtection.Merge(ddosProtection)
|
||||
}
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// FindNodeClusterId 获取节点的集群ID
|
||||
func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
@@ -510,197 +176,6 @@ func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindNodeActive 检查节点活跃状态
|
||||
func (this *NSNodeDAO) FindNodeActive(tx *dbs.Tx, nodeId int64) (bool, error) {
|
||||
isActive, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Result("isActive").
|
||||
FindIntCol(0)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return isActive == 1, nil
|
||||
}
|
||||
|
||||
// UpdateNodeActive 修改节点活跃状态
|
||||
func (this *NSNodeDAO) UpdateNodeActive(tx *dbs.Tx, nodeId int64, isActive bool) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
_, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("isActive", isActive).
|
||||
Set("statusIsNotified", false).
|
||||
Set("inactiveNotifiedAt", 0).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateNodeConnectedAPINodes 修改当前连接的API节点
|
||||
func (this *NSNodeDAO) UpdateNodeConnectedAPINodes(tx *dbs.Tx, nodeId int64, apiNodeIds []int64) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
|
||||
var op = NewNSNodeOperator()
|
||||
op.Id = nodeId
|
||||
|
||||
if len(apiNodeIds) > 0 {
|
||||
apiNodeIdsJSON, err := json.Marshal(apiNodeIds)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
op.ConnectedAPINodes = apiNodeIdsJSON
|
||||
} else {
|
||||
op.ConnectedAPINodes = "[]"
|
||||
}
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// FindAllNotifyingInactiveNodesWithClusterId 取得某个集群所有等待通知离线离线的节点
|
||||
func (this *NSNodeDAO) FindAllNotifyingInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NSNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(NSNodeStateEnabled).
|
||||
Attr("clusterId", clusterId).
|
||||
Attr("isOn", true). // 只监控启用的节点
|
||||
Attr("isInstalled", true). // 只监控已经安装的节点
|
||||
Attr("isActive", false). // 当前已经离线的
|
||||
Attr("statusIsNotified", false).
|
||||
Result("id", "name").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateNodeStatusIsNotified 设置状态已经通知
|
||||
func (this *NSNodeDAO) UpdateNodeStatusIsNotified(tx *dbs.Tx, nodeId int64) error {
|
||||
return this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("statusIsNotified", true).
|
||||
Set("inactiveNotifiedAt", time.Now().Unix()).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// FindNodeInactiveNotifiedAt 读取上次的节点离线通知时间
|
||||
func (this *NSNodeDAO) FindNodeInactiveNotifiedAt(tx *dbs.Tx, nodeId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Result("inactiveNotifiedAt").
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindAllNodeIdsMatch 匹配节点并返回节点ID
|
||||
func (this *NSNodeDAO) FindAllNodeIdsMatch(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, isOn configutils.BoolState) (result []int64, err error) {
|
||||
query := this.Query(tx)
|
||||
query.State(NSNodeStateEnabled)
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
}
|
||||
if isOn == configutils.BoolStateYes {
|
||||
query.Attr("isOn", true)
|
||||
} else if isOn == configutils.BoolStateNo {
|
||||
query.Attr("isOn", false)
|
||||
}
|
||||
query.Result("id")
|
||||
ones, _, err := query.FindOnes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, one := range ones {
|
||||
result = append(result, one.GetInt64("id"))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateNodeInstallStatus 修改节点的安装状态
|
||||
func (this *NSNodeDAO) UpdateNodeInstallStatus(tx *dbs.Tx, nodeId int64, status *NodeInstallStatus) error {
|
||||
if status == nil {
|
||||
_, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("installStatus", "null").
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.Marshal(status)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("installStatus", string(data)).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledNodeIdsWithClusterId 查找集群下的所有节点
|
||||
func (this *NSNodeDAO) FindEnabledNodeIdsWithClusterId(tx *dbs.Tx, clusterId int64) ([]int64, error) {
|
||||
if clusterId <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ones, err := this.Query(tx).
|
||||
ResultPk().
|
||||
Attr("clusterId", clusterId).
|
||||
State(NSNodeStateEnabled).
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result = []int64{}
|
||||
for _, one := range ones {
|
||||
result = append(result, int64(one.(*NSNode).Id))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FindNodeDDoSProtection 获取节点的DDOS设置
|
||||
func (this *NSNodeDAO) FindNodeDDoSProtection(tx *dbs.Tx, nodeId int64) (*ddosconfigs.ProtectionConfig, error) {
|
||||
one, err := this.Query(tx).
|
||||
Result("ddosProtection").
|
||||
Pk(nodeId).
|
||||
Find()
|
||||
if one == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*NSNode).DecodeDDoSProtection(), nil
|
||||
}
|
||||
|
||||
// UpdateNodeDDoSProtection 设置集群的DDOS设置
|
||||
func (this *NSNodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
|
||||
if nodeId <= 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
var op = NewNSNodeOperator()
|
||||
op.Id = nodeId
|
||||
|
||||
if ddosProtection == nil {
|
||||
op.DdosProtection = "{}"
|
||||
} else {
|
||||
ddosProtectionJSON, err := json.Marshal(ddosProtection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.DdosProtection = ddosProtectionJSON
|
||||
}
|
||||
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clusterId, err := this.FindNodeClusterId(tx, nodeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if clusterId > 0 {
|
||||
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, 0, NSNodeTaskTypeDDosProtectionChanged, 0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||
// TODO 先什么都不做
|
||||
|
||||
@@ -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,6 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -320,6 +320,7 @@ func (this *ReportNodeDAO) FindNodeAllowIPs(tx *dbs.Tx, nodeId int64) ([]string,
|
||||
func (this *ReportNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(ReportNodeStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Where("status IS NOT NULL").
|
||||
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
|
||||
Param("version", utils.VersionToLong(version)).
|
||||
|
||||
@@ -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"
|
||||
@@ -85,8 +86,8 @@ func (this *ServerBandwidthStatDAO) UpdateServerBandwidth(tx *dbs.Tx, userId int
|
||||
func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, minute string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Result("bytes").
|
||||
Attr("serverId", serverId).
|
||||
Result("bytes").
|
||||
Attr("day", day).
|
||||
Attr("timeAt", minute).
|
||||
FindInt64Col(0)
|
||||
@@ -102,8 +103,8 @@ func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverI
|
||||
|
||||
ones, _, err := this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Result("MAX(bytes) AS bytes", "CONCAT(day, '.', SUBSTRING(timeAt, 1, 2)) AS fullTime").
|
||||
Attr("serverId", serverId).
|
||||
Result("MAX(bytes) AS bytes", "CONCAT(day, '.', SUBSTRING(timeAt, 1, 2)) AS fullTime").
|
||||
Gte("CONCAT(day, '.', SUBSTRING(timeAt, 1, 2))", timeutil.FormatTime("Ymd.H", timestamp)).
|
||||
Group("fullTime").
|
||||
FindOnes()
|
||||
@@ -117,9 +118,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 +138,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:]),
|
||||
})
|
||||
@@ -150,6 +153,7 @@ func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverI
|
||||
func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Attr("day", day).
|
||||
Result("MAX(bytes)").
|
||||
FindInt64Col(0)
|
||||
@@ -177,9 +181,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,
|
||||
}
|
||||
}
|
||||
@@ -195,6 +201,7 @@ func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId
|
||||
} else {
|
||||
result = append(result, &pb.FindDailyServerBandwidthStatsResponse_Stat{
|
||||
Bytes: 0,
|
||||
Bits: 0,
|
||||
Day: day,
|
||||
})
|
||||
}
|
||||
@@ -203,11 +210,91 @@ 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) {
|
||||
return this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Attr("serverId", serverId).
|
||||
Between("day", month+"01", month+"31").
|
||||
Result("MAX(bytes)").
|
||||
FindInt64Col(0)
|
||||
@@ -264,8 +351,8 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
||||
if percentile >= 100 {
|
||||
result, err = this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Result("bytes").
|
||||
Attr("serverId", serverId).
|
||||
Result("bytes").
|
||||
Between("day", month+"01", month+"31").
|
||||
Desc("bytes").
|
||||
Limit(1).
|
||||
@@ -295,8 +382,8 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
||||
// 查询 nth 位置
|
||||
result, err = this.Query(tx).
|
||||
Table(this.partialTable(serverId)).
|
||||
Result("bytes").
|
||||
Attr("serverId", serverId).
|
||||
Result("bytes").
|
||||
Between("day", month+"01", month+"31").
|
||||
Desc("bytes").
|
||||
Offset(offset).
|
||||
@@ -306,9 +393,68 @@ 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
|
||||
}
|
||||
|
||||
// Clean 清理过期数据
|
||||
func (this *ServerBandwidthStatDAO) Clean(tx *dbs.Tx) error {
|
||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -62)) // 保留大约2个月的数据
|
||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -100)) // 保留大约3个月的数据
|
||||
return this.runBatch(func(table string, locker *sync.Mutex) error {
|
||||
_, err := this.Query(tx).
|
||||
Table(table).
|
||||
|
||||
@@ -89,7 +89,6 @@ func TestServerBandwidthStatDAO_FindHourlyBandwidthStats(t *testing.T) {
|
||||
logs.PrintAsJSON(stats, t)
|
||||
}
|
||||
|
||||
|
||||
func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) {
|
||||
var dao = models.NewServerBandwidthStatDAO()
|
||||
var tx *dbs.Tx
|
||||
@@ -98,4 +97,16 @@ func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logs.PrintAsJSON(stats, t)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerBandwidthStatDAO_FindBandwidthStatsBetweenDays(t *testing.T) {
|
||||
var dao = models.NewServerBandwidthStatDAO()
|
||||
var tx *dbs.Tx
|
||||
stats, err := dao.FindBandwidthStatsBetweenDays(tx, 23, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -2)), timeutil.Format("Ymd"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, stat := range stats {
|
||||
t.Log(stat.Day, stat.TimeAt, "bytes:", stat.Bytes, "bits:", stat.Bits)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,18 +5,20 @@ type ServerBandwidthStat struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
ServerId uint64 `field:"serverId"` // 服务ID
|
||||
RegionId uint32 `field:"regionId"` // 区域ID
|
||||
Day string `field:"day"` // 日期YYYYMMDD
|
||||
TimeAt string `field:"timeAt"` // 时间点HHMM
|
||||
Bytes uint64 `field:"bytes"` // 带宽字节
|
||||
}
|
||||
|
||||
type ServerBandwidthStatOperator struct {
|
||||
Id interface{} // ID
|
||||
UserId interface{} // 用户ID
|
||||
ServerId interface{} // 服务ID
|
||||
Day interface{} // 日期YYYYMMDD
|
||||
TimeAt interface{} // 时间点HHMM
|
||||
Bytes interface{} // 带宽字节
|
||||
Id any // ID
|
||||
UserId any // 用户ID
|
||||
ServerId any // 服务ID
|
||||
RegionId any // 区域ID
|
||||
Day any // 日期YYYYMMDD
|
||||
TimeAt any // 时间点HHMM
|
||||
Bytes any // 带宽字节
|
||||
}
|
||||
|
||||
func NewServerBandwidthStatOperator() *ServerBandwidthStatOperator {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"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"
|
||||
@@ -11,6 +13,7 @@ import (
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -83,7 +86,7 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
|
||||
InsertOrUpdate(maps.Map{
|
||||
"userId": serverUserId,
|
||||
"serverId": stat.ServerId,
|
||||
"regionId": stat.RegionId,
|
||||
"regionId": stat.NodeRegionId,
|
||||
"bytes": stat.Bytes,
|
||||
"cachedBytes": stat.CachedBytes,
|
||||
"countRequests": stat.CountRequests,
|
||||
@@ -193,7 +196,7 @@ func (this *ServerDailyStatDAO) SumUserMonthlyPeek(tx *dbs.Tx, userId int64, reg
|
||||
// SumUserDaily 获取某天流量总和
|
||||
// day 格式为YYYYMMDD
|
||||
func (this *ServerDailyStatDAO) SumUserDaily(tx *dbs.Tx, userId int64, regionId int64, day string) (int64, error) {
|
||||
query := this.Query(tx)
|
||||
var query = this.Query(tx)
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
}
|
||||
@@ -203,6 +206,27 @@ func (this *ServerDailyStatDAO) SumUserDaily(tx *dbs.Tx, userId int64, regionId
|
||||
SumInt64("bytes", 0)
|
||||
}
|
||||
|
||||
// SumUserTrafficBytesBetweenDays 获取用户某个日期段内的流量总和
|
||||
func (this *ServerDailyStatDAO) SumUserTrafficBytesBetweenDays(tx *dbs.Tx, userId int64, regionId int64, dayFrom string, dayTo string) (int64, error) {
|
||||
if !regexputils.YYYYMMDD.MatchString(dayFrom) {
|
||||
return 0, errors.New("invalid 'dayFrom':" + dayFrom)
|
||||
}
|
||||
if !regexputils.YYYYMMDD.MatchString(dayTo) {
|
||||
return 0, errors.New("invalid 'dayTo':" + dayTo)
|
||||
}
|
||||
|
||||
var query = this.Query(tx)
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
} else if regionId < 0 { // 表示没有分配区域的流量
|
||||
query.Attr("regionId", 0)
|
||||
}
|
||||
return query.
|
||||
Attr("userId", userId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
SumInt64("bytes", 0)
|
||||
}
|
||||
|
||||
// SumUserMonthly 获取某月流量总和
|
||||
// month 格式为YYYYMM
|
||||
func (this *ServerDailyStatDAO) SumUserMonthly(tx *dbs.Tx, userId int64, month string) (int64, error) {
|
||||
@@ -295,19 +319,48 @@ func (this *ServerDailyStatDAO) SumHourlyStat(tx *dbs.Tx, serverId int64, hour s
|
||||
}
|
||||
|
||||
// SumDailyStat 获取某天内的流量
|
||||
// day 格式为YYYYMMDD
|
||||
func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day string) (stat *pb.ServerDailyStat, err error) {
|
||||
// dayFrom 格式为YYYYMMDD
|
||||
// dayTo 格式为YYYYMMDD
|
||||
func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, userId int64, serverId int64, regionId int64, dayFrom string, dayTo string) (stat *pb.ServerDailyStat, err error) {
|
||||
stat = &pb.ServerDailyStat{}
|
||||
|
||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
||||
return nil, errors.New("invalid day '" + day + "'")
|
||||
if userId <= 0 && serverId <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
one, _, err := this.Query(tx).
|
||||
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests, SUM(countAttackRequests) AS countAttackRequests, SUM(attackBytes) AS attackBytes").
|
||||
Attr("serverId", serverId).
|
||||
Attr("day", day).
|
||||
FindOne()
|
||||
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
|
||||
}
|
||||
|
||||
var query = this.Query(tx).
|
||||
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests, SUM(countAttackRequests) AS countAttackRequests, SUM(attackBytes) AS attackBytes")
|
||||
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
|
||||
if serverId > 0 {
|
||||
query.Attr("serverId", serverId)
|
||||
}
|
||||
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
}
|
||||
|
||||
if dayFrom == dayTo {
|
||||
query.Attr("day", dayFrom)
|
||||
} else {
|
||||
query.Between("day", dayFrom, dayTo)
|
||||
}
|
||||
|
||||
one, _, err := query.FindOne()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -332,7 +385,7 @@ func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day str
|
||||
func (this *ServerDailyStatDAO) SumDailyStatBeforeMinute(tx *dbs.Tx, serverId int64, day string, minute string) (stat *pb.ServerDailyStat, err error) {
|
||||
stat = &pb.ServerDailyStat{}
|
||||
|
||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
||||
if !regexputils.YYYYMMDD.MatchString(day) {
|
||||
return nil, errors.New("invalid day '" + day + "'")
|
||||
}
|
||||
|
||||
@@ -364,7 +417,7 @@ func (this *ServerDailyStatDAO) SumDailyStatBeforeMinute(tx *dbs.Tx, serverId in
|
||||
func (this *ServerDailyStatDAO) SumMonthlyStat(tx *dbs.Tx, serverId int64, month string) (stat *pb.ServerDailyStat, err error) {
|
||||
stat = &pb.ServerDailyStat{}
|
||||
|
||||
if !regexp.MustCompile(`^\d{6}$`).MatchString(month) {
|
||||
if !regexputils.YYYYMM.MatchString(month) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -393,7 +446,7 @@ func (this *ServerDailyStatDAO) SumMonthlyStat(tx *dbs.Tx, serverId int64, month
|
||||
// SumMonthlyBytes 获取某月内的流量
|
||||
// month 格式为YYYYMM
|
||||
func (this *ServerDailyStatDAO) SumMonthlyBytes(tx *dbs.Tx, serverId int64, month string) (result int64, err error) {
|
||||
if !regexp.MustCompile(`^\d{6}$`).MatchString(month) {
|
||||
if !regexputils.YYYYMM.MatchString(month) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -437,6 +490,139 @@ func (this *ServerDailyStatDAO) FindDailyStats(tx *dbs.Tx, serverId int64, dayFr
|
||||
return
|
||||
}
|
||||
|
||||
// FindStatsWithDay 按天查找5分钟级统计
|
||||
// day YYYYMMDD
|
||||
// timeFrom HHII00
|
||||
// timeTo HHII59
|
||||
func (this *ServerDailyStatDAO) FindStatsWithDay(tx *dbs.Tx, serverId int64, day string, timeFrom string, timeTo string) (result []*ServerDailyStat, err error) {
|
||||
if !regexputils.YYYYMMDD.MatchString(day) {
|
||||
return
|
||||
}
|
||||
|
||||
var query = this.Query(tx).
|
||||
Result("SUM(bytes) AS bytes", "SUM(cachedBytes) AS cachedBytes", "SUM(countRequests) AS countRequests", "SUM(countCachedRequests) AS countCachedRequests", "SUM(countAttackRequests) AS countAttackRequests", "SUM(attackBytes) AS attackBytes", "day", "timeFrom", "MIN(timeTo) AS timeTo").
|
||||
Attr("serverId", serverId).
|
||||
Attr("day", day).
|
||||
Group("day").Group("timeFrom", dbs.QueryOrderDesc)
|
||||
|
||||
if len(timeFrom) > 0 {
|
||||
query.Gte("timeFrom", timeFrom)
|
||||
}
|
||||
if len(timeTo) > 0 {
|
||||
query.Lte("timeTo", timeTo)
|
||||
}
|
||||
|
||||
_, err = query.
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FindStatsBetweenDays 查找日期段内的5分钟统计
|
||||
func (this *ServerDailyStatDAO) FindStatsBetweenDays(tx *dbs.Tx, userId int64, serverId int64, regionId int64, dayFrom string, dayTo string) (result []*ServerDailyStat, err error) {
|
||||
if !regexputils.YYYYMMDD.MatchString(dayFrom) || !regexputils.YYYYMMDD.MatchString(dayTo) {
|
||||
return
|
||||
}
|
||||
|
||||
if userId <= 0 && serverId <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if dayFrom > dayTo {
|
||||
dayFrom, dayTo = dayTo, dayFrom
|
||||
}
|
||||
|
||||
var query = this.Query(tx)
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
|
||||
if serverId > 0 {
|
||||
query.Attr("serverId", serverId)
|
||||
} else {
|
||||
query.Result("SUM(bytes) AS bytes", "SUM(cachedBytes) AS cachedBytes", "SUM(countRequests) AS countRequests", "SUM(countCachedRequests) AS countCachedRequests", "SUM(countAttackRequests) AS countAttackRequests", "SUM(attackBytes) AS attackBytes", "MIN(day) AS day", "MIN(timeFrom) AS timeFrom", "MIN(timeTo) AS timeTo")
|
||||
query.Group("day").Group("timeFrom")
|
||||
}
|
||||
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
} else if regionId < 0 { // 表示未分配区域的流量
|
||||
query.Attr("regionId", 0)
|
||||
}
|
||||
|
||||
// 不需要排序
|
||||
query.Between("day", dayFrom, dayTo)
|
||||
_, err = query.
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var m = map[string]*ServerDailyStat{} // day @ timeFrom => *ServerDailyStat
|
||||
for _, stat := range result {
|
||||
var key = stat.Day + "@" + stat.TimeFrom
|
||||
mStat, ok := m[key]
|
||||
if ok {
|
||||
mStat.Bytes += stat.Bytes
|
||||
mStat.CachedBytes += stat.CachedBytes
|
||||
mStat.AttackBytes += stat.AttackBytes
|
||||
mStat.CountRequests += stat.CountRequests
|
||||
mStat.CountAttackRequests += stat.CountAttackRequests
|
||||
mStat.CountCachedRequests += stat.CountCachedRequests
|
||||
} else {
|
||||
m[key] = stat
|
||||
}
|
||||
}
|
||||
|
||||
// 填充空白
|
||||
rangeDays, 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@Hi00")
|
||||
|
||||
result = nil
|
||||
for _, day := range rangeDays {
|
||||
for _, timeAt /** HHII **/ := range dayTimes {
|
||||
var key = day + "@" + timeAt + "00"
|
||||
|
||||
if key >= currentTime {
|
||||
break
|
||||
}
|
||||
|
||||
stat, ok := m[key]
|
||||
if ok {
|
||||
result = append(result, stat)
|
||||
} else {
|
||||
var hour = types.Int(timeAt[:2])
|
||||
var minute = types.Int(timeAt[2:])
|
||||
|
||||
minute += 4
|
||||
|
||||
result = append(result, &ServerDailyStat{
|
||||
Day: day,
|
||||
TimeFrom: timeAt + "00",
|
||||
TimeTo: fmt.Sprintf("%02d%02d59", hour, minute),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FindMonthlyStatsWithPlan 查找某月有套餐的流量
|
||||
// month YYYYMM
|
||||
func (this *ServerDailyStatDAO) FindMonthlyStatsWithPlan(tx *dbs.Tx, month string) (result []*ServerDailyStat, err error) {
|
||||
@@ -512,6 +698,23 @@ func (this *ServerDailyStatDAO) FindDistinctServerIds(tx *dbs.Tx, dayFrom string
|
||||
return serverIds, nil
|
||||
}
|
||||
|
||||
// FindDistinctUserIds 查找所有有流量的用户ID
|
||||
func (this *ServerDailyStatDAO) FindDistinctUserIds(tx *dbs.Tx, dayFrom string, dayTo string) (userIds []int64, err error) {
|
||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||
dayTo = strings.ReplaceAll(dayTo, "-", "")
|
||||
ones, _, err := this.Query(tx).
|
||||
Result("DISTINCT(userId) AS userId").
|
||||
Between("day", dayFrom, dayTo).
|
||||
FindOnes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, one := range ones {
|
||||
userIds = append(userIds, one.GetInt64("userId"))
|
||||
}
|
||||
return userIds, nil
|
||||
}
|
||||
|
||||
// UpdateStatFee 设置费用
|
||||
func (this *ServerDailyStatDAO) UpdateStatFee(tx *dbs.Tx, statId int64, fee float32) error {
|
||||
return this.Query(tx).
|
||||
|
||||
@@ -7,16 +7,17 @@ import (
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestServerDailyStatDAO_SaveStats(t *testing.T) {
|
||||
var tx *dbs.Tx
|
||||
stats := []*pb.ServerDailyStat{
|
||||
{
|
||||
ServerId: 1,
|
||||
RegionId: 2,
|
||||
Bytes: 1,
|
||||
CreatedAt: 1607671488,
|
||||
ServerId: 1,
|
||||
NodeRegionId: 2,
|
||||
Bytes: 1,
|
||||
CreatedAt: 1607671488,
|
||||
},
|
||||
}
|
||||
err := NewServerDailyStatDAO().SaveStats(tx, stats)
|
||||
@@ -30,10 +31,10 @@ func TestServerDailyStatDAO_SaveStats2(t *testing.T) {
|
||||
var tx *dbs.Tx
|
||||
stats := []*pb.ServerDailyStat{
|
||||
{
|
||||
ServerId: 1,
|
||||
RegionId: 3,
|
||||
Bytes: 1,
|
||||
CreatedAt: 1607671488,
|
||||
ServerId: 1,
|
||||
NodeRegionId: 3,
|
||||
Bytes: 1,
|
||||
CreatedAt: 1607671488,
|
||||
},
|
||||
}
|
||||
err := NewServerDailyStatDAO().SaveStats(tx, stats)
|
||||
@@ -83,3 +84,26 @@ func TestServerDailyStatDAO_FindDistinctPlanServerIdsBetweenDay(t *testing.T) {
|
||||
}
|
||||
t.Log(serverIds)
|
||||
}
|
||||
|
||||
func TestServerDailyStatDAO_FindStatsBetweenDays(t *testing.T) {
|
||||
var tx *dbs.Tx
|
||||
stats, err := NewServerDailyStatDAO().FindStatsBetweenDays(tx, 1, 0, 0, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -1)), timeutil.Format("Ymd"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, stat := range stats {
|
||||
t.Log(stat.Day, stat.TimeFrom, stat.TimeTo, stat.Bytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerDailyStatDAO_FindStatsWithDay(t *testing.T) {
|
||||
var dao = NewServerDailyStatDAO()
|
||||
var tx *dbs.Tx
|
||||
stats, err := dao.FindStatsWithDay(tx, 23, timeutil.Format("Ymd"), "000000", "235900")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, stat := range stats {
|
||||
t.Log(stat.TimeFrom, stat.TimeTo, stat.Bytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -862,20 +862,21 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
|
||||
}
|
||||
|
||||
// 排序
|
||||
var timestamp = time.Now().Unix() / 300 * 300
|
||||
var currentTime = timeutil.FormatTime("YmdHi", timestamp)
|
||||
var prevTime = timeutil.FormatTime("YmdHi", timestamp-300)
|
||||
var timestamp = (time.Now().Unix()) / 300 * 300
|
||||
var times = []string{
|
||||
timeutil.FormatTime("YmdHi", timestamp),
|
||||
timeutil.FormatTime("YmdHi", timestamp-300),
|
||||
timeutil.FormatTime("YmdHi", timestamp-300*2),
|
||||
}
|
||||
|
||||
switch order {
|
||||
case "trafficOutAsc":
|
||||
query.Asc("IF(IF(bandwidthTime=:currentTime, bandwidthBytes, 0) > 0, IF(bandwidthTime=:currentTime, bandwidthBytes, 0), IF(bandwidthTime=:prevTime, bandwidthBytes, 0))")
|
||||
query.Param("currentTime", currentTime)
|
||||
query.Param("prevTime", prevTime)
|
||||
query.Asc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
|
||||
query.Param("times", strings.Join(times, ","))
|
||||
query.DescPk()
|
||||
case "trafficOutDesc":
|
||||
query.Desc("IF(IF(bandwidthTime=:currentTime, bandwidthBytes, 0) > 0, IF(bandwidthTime=:currentTime, bandwidthBytes, 0), IF(bandwidthTime=:prevTime, bandwidthBytes, 0))")
|
||||
query.Param("currentTime", currentTime)
|
||||
query.Param("prevTime", prevTime)
|
||||
query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
|
||||
query.Param("times", strings.Join(times, ","))
|
||||
query.DescPk()
|
||||
default:
|
||||
query.DescPk()
|
||||
@@ -885,7 +886,7 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
|
||||
|
||||
// 修正带宽统计数据
|
||||
for _, server := range result {
|
||||
if len(server.BandwidthTime) > 0 && server.BandwidthBytes > 0 && server.BandwidthTime < prevTime {
|
||||
if len(server.BandwidthTime) > 0 && !lists.ContainsString(times, server.BandwidthTime) {
|
||||
server.BandwidthBytes = 0
|
||||
}
|
||||
}
|
||||
@@ -1008,12 +1009,12 @@ func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int6
|
||||
if server == nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return this.ComposeServerConfig(tx, server, nil, forNode)
|
||||
return this.ComposeServerConfig(tx, server, nil, forNode, false)
|
||||
}
|
||||
|
||||
// ComposeServerConfig 构造服务的Config
|
||||
// forNode 是否是节点请求
|
||||
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap *utils.CacheMap, forNode bool) (*serverconfigs.ServerConfig, error) {
|
||||
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap *utils.CacheMap, forNode bool, forList bool) (*serverconfigs.ServerConfig, error) {
|
||||
if server == nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
@@ -1038,7 +1039,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
||||
|
||||
var groupConfig *serverconfigs.ServerGroupConfig
|
||||
for _, groupId := range server.DecodeGroupIds() {
|
||||
groupConfig1, err := SharedServerGroupDAO.ComposeGroupConfig(tx, groupId, cacheMap)
|
||||
groupConfig1, err := SharedServerGroupDAO.ComposeGroupConfig(tx, groupId, forList, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1061,28 +1062,30 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
||||
}
|
||||
|
||||
// CNAME
|
||||
config.SupportCNAME = server.SupportCNAME == 1
|
||||
if server.ClusterId > 0 && len(server.DnsName) > 0 {
|
||||
clusterDNS, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, int64(server.ClusterId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if clusterDNS != nil && clusterDNS.DnsDomainId > 0 {
|
||||
clusterDNSConfig, err := clusterDNS.DecodeDNSConfig()
|
||||
if !forList {
|
||||
config.SupportCNAME = server.SupportCNAME == 1
|
||||
if server.ClusterId > 0 && len(server.DnsName) > 0 {
|
||||
clusterDNS, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, int64(server.ClusterId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if clusterDNS != nil && clusterDNS.DnsDomainId > 0 {
|
||||
clusterDNSConfig, err := clusterDNS.DecodeDNSConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, int64(clusterDNS.DnsDomainId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domain != nil {
|
||||
var cname = server.DnsName + "." + domain.Name
|
||||
config.CNameDomain = cname
|
||||
if clusterDNSConfig.CNameAsDomain {
|
||||
config.CNameAsDomain = true
|
||||
config.AliasServerNames = append(config.AliasServerNames, cname)
|
||||
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, int64(clusterDNS.DnsDomainId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domain != nil {
|
||||
var cname = server.DnsName + "." + domain.Name
|
||||
config.CNameDomain = cname
|
||||
if clusterDNSConfig.CNAMEAsDomain {
|
||||
config.CNameAsDomain = true
|
||||
config.AliasServerNames = append(config.AliasServerNames, cname)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1173,61 +1176,71 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
||||
}
|
||||
|
||||
// Web
|
||||
if server.WebId > 0 {
|
||||
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(server.WebId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if webConfig != nil {
|
||||
config.Web = webConfig
|
||||
if !forList {
|
||||
if server.WebId > 0 {
|
||||
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(server.WebId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if webConfig != nil {
|
||||
config.Web = webConfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReverseProxy
|
||||
if IsNotNull(server.ReverseProxy) {
|
||||
var reverseProxyRef = &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(server.ReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.ReverseProxyRef = reverseProxyRef
|
||||
if !forList {
|
||||
if IsNotNull(server.ReverseProxy) {
|
||||
var reverseProxyRef = &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(server.ReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.ReverseProxyRef = reverseProxyRef
|
||||
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.ReverseProxy = reverseProxyConfig
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.ReverseProxy = reverseProxyConfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WAF策略
|
||||
var clusterId = int64(server.ClusterId)
|
||||
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if httpFirewallPolicyId > 0 {
|
||||
config.HTTPFirewallPolicyId = httpFirewallPolicyId
|
||||
}
|
||||
|
||||
// 缓存策略
|
||||
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if httpCachePolicyId > 0 {
|
||||
config.HTTPCachePolicyId = httpCachePolicyId
|
||||
}
|
||||
|
||||
// traffic limit
|
||||
if len(server.TrafficLimit) > 0 {
|
||||
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
|
||||
err = json.Unmarshal(server.TrafficLimit, trafficLimitConfig)
|
||||
if !forList {
|
||||
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.TrafficLimit = trafficLimitConfig
|
||||
if httpFirewallPolicyId > 0 {
|
||||
config.HTTPFirewallPolicyId = httpFirewallPolicyId
|
||||
}
|
||||
}
|
||||
|
||||
// 缓存策略
|
||||
if !forList {
|
||||
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if httpCachePolicyId > 0 {
|
||||
config.HTTPCachePolicyId = httpCachePolicyId
|
||||
}
|
||||
}
|
||||
|
||||
// traffic limit
|
||||
if !forList {
|
||||
if len(server.TrafficLimit) > 0 {
|
||||
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
|
||||
err := json.Unmarshal(server.TrafficLimit, trafficLimitConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.TrafficLimit = trafficLimitConfig
|
||||
}
|
||||
}
|
||||
|
||||
// 用户套餐
|
||||
@@ -1270,7 +1283,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
||||
if config.TrafficLimit != nil && config.TrafficLimit.IsOn && !config.TrafficLimit.IsEmpty() {
|
||||
if len(server.TrafficLimitStatus) > 0 {
|
||||
var status = &serverconfigs.TrafficLimitStatus{}
|
||||
err = json.Unmarshal(server.TrafficLimitStatus, status)
|
||||
err := json.Unmarshal(server.TrafficLimitStatus, status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1281,14 +1294,16 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
||||
}
|
||||
|
||||
// UAM
|
||||
if teaconst.IsPlus && IsNotNull(server.Uam) {
|
||||
var uamConfig = &serverconfigs.UAMConfig{}
|
||||
err = json.Unmarshal(server.Uam, uamConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if uamConfig.IsOn {
|
||||
config.UAM = uamConfig
|
||||
if !forList {
|
||||
if teaconst.IsPlus && IsNotNull(server.Uam) {
|
||||
var uamConfig = &serverconfigs.UAMConfig{}
|
||||
err := json.Unmarshal(server.Uam, uamConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if uamConfig.IsOn {
|
||||
config.UAM = uamConfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1746,11 +1761,11 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
||||
}
|
||||
|
||||
if oldClusterId > 0 {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, 0, NodeTaskTypeConfigChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, 0, NodeTaskTypeIPItemChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, 0, 0, NodeTaskTypeIPItemChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1761,11 +1776,11 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
||||
}
|
||||
|
||||
if newClusterId > 0 {
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, 0, NodeTaskTypeConfigChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, 0, 0, NodeTaskTypeConfigChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, 0, NodeTaskTypeIPItemChanged)
|
||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, 0, 0, NodeTaskTypeIPItemChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1778,11 +1793,24 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
||||
return err
|
||||
}
|
||||
|
||||
// FindAllEnabledServersWithUserId 查找用户的所有的服务
|
||||
func (this *ServerDAO) FindAllEnabledServersWithUserId(tx *dbs.Tx, userId int64) (result []*Server, err error) {
|
||||
// FindAllBasicServersWithUserId 查找用户的所有服务的基础信息
|
||||
func (this *ServerDAO) FindAllBasicServersWithUserId(tx *dbs.Tx, userId int64) (result []*Server, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Result("id", "serverNames", "name", "isOn", "type", "groupIds", "clusterId", "dnsName").
|
||||
State(ServerStateEnabled).
|
||||
Attr("userId", userId).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindAllAvailableServersWithUserId 查找用户的所有可用服务信息
|
||||
func (this *ServerDAO) FindAllAvailableServersWithUserId(tx *dbs.Tx, userId int64) (result []*Server, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(ServerStateEnabled).
|
||||
Attr("userId", userId).
|
||||
Attr("isOn", true).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
@@ -1879,8 +1907,8 @@ func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, protocolFam
|
||||
}
|
||||
|
||||
// ExistServerNameInCluster 检查ServerName是否已存在
|
||||
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64) (bool, error) {
|
||||
query := this.Query(tx).
|
||||
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64, supportWildcard bool) (bool, error) {
|
||||
var query = this.Query(tx).
|
||||
Attr("clusterId", clusterId).
|
||||
Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))").
|
||||
Param("jsonQuery1", maps.Map{"name": serverName}.AsJSON()).
|
||||
@@ -1889,7 +1917,38 @@ func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, ser
|
||||
query.Neq("id", excludeServerId)
|
||||
}
|
||||
query.State(ServerStateEnabled)
|
||||
return query.Exist()
|
||||
exists, err := query.Exist()
|
||||
if err != nil || exists {
|
||||
return exists, err
|
||||
}
|
||||
|
||||
if supportWildcard {
|
||||
var countPieces = strings.Count(serverName, ".")
|
||||
for {
|
||||
var index = strings.Index(serverName, ".")
|
||||
if index > 0 {
|
||||
serverName = serverName[index+1:]
|
||||
var search = strings.Repeat("*.", countPieces-strings.Count(serverName, ".")) + serverName
|
||||
var query = this.Query(tx).
|
||||
Attr("clusterId", clusterId).
|
||||
Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))").
|
||||
Param("jsonQuery1", maps.Map{"name": search}.AsJSON()).
|
||||
Param("jsonQuery2", maps.Map{"subNames": search}.AsJSON())
|
||||
if excludeServerId > 0 {
|
||||
query.Neq("id", excludeServerId)
|
||||
}
|
||||
query.State(ServerStateEnabled)
|
||||
exists, err = query.Exist()
|
||||
if err != nil || exists {
|
||||
return exists, err
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// GenDNSName 生成DNS Name
|
||||
@@ -2332,21 +2391,21 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitCo
|
||||
|
||||
// daily
|
||||
if trafficLimitConfig.DailyBytes() > 0 {
|
||||
if server.TrafficDay == timeutil.Format("Ymd") && server.TotalDailyTraffic >= float64(trafficLimitConfig.DailyBytes())/1024/1024/1024 {
|
||||
if server.TrafficDay == timeutil.Format("Ymd") && server.TotalDailyTraffic >= float64(trafficLimitConfig.DailyBytes())/(1<<30) {
|
||||
untilDay = timeutil.Format("Ymd")
|
||||
}
|
||||
}
|
||||
|
||||
// monthly
|
||||
if server.TrafficMonth == timeutil.Format("Ym") && trafficLimitConfig.MonthlyBytes() > 0 {
|
||||
if server.TotalMonthlyTraffic >= float64(trafficLimitConfig.MonthlyBytes())/1024/1024/1024 {
|
||||
if server.TotalMonthlyTraffic >= float64(trafficLimitConfig.MonthlyBytes())/(1<<30) {
|
||||
untilDay = timeutil.Format("Ym32")
|
||||
}
|
||||
}
|
||||
|
||||
// totally
|
||||
if trafficLimitConfig.TotalBytes() > 0 {
|
||||
if server.TotalTraffic >= float64(trafficLimitConfig.TotalBytes())/1024/1024/1024 {
|
||||
if server.TotalTraffic >= float64(trafficLimitConfig.TotalBytes())/(1<<30) {
|
||||
untilDay = "30000101"
|
||||
}
|
||||
}
|
||||
@@ -2376,7 +2435,7 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitCo
|
||||
|
||||
// IncreaseServerTotalTraffic 增加服务的总流量
|
||||
func (this *ServerDAO) IncreaseServerTotalTraffic(tx *dbs.Tx, serverId int64, bytes int64) error {
|
||||
var gb = float64(bytes) / 1024 / 1024 / 1024
|
||||
var gb = float64(bytes) / (1 << 30)
|
||||
var day = timeutil.Format("Ymd")
|
||||
var month = timeutil.Format("Ym")
|
||||
return this.Query(tx).
|
||||
@@ -2570,11 +2629,29 @@ func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTim
|
||||
if bandwidthBytes < 0 {
|
||||
bandwidthBytes = 0
|
||||
}
|
||||
return this.Query(tx).
|
||||
|
||||
bandwidthTime, err := this.Query(tx).
|
||||
Pk(serverId).
|
||||
Set("bandwidthTime", fullTime).
|
||||
Set("bandwidthBytes", bandwidthBytes).
|
||||
UpdateQuickly()
|
||||
Result("bandwidthTime").
|
||||
FindStringCol("")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if bandwidthTime != fullTime {
|
||||
return this.Query(tx).
|
||||
Pk(serverId).
|
||||
Set("bandwidthTime", fullTime).
|
||||
Set("bandwidthBytes", bandwidthBytes).
|
||||
UpdateQuickly()
|
||||
} else {
|
||||
return this.Query(tx).
|
||||
Pk(serverId).
|
||||
Set("bandwidthTime", fullTime).
|
||||
Set("bandwidthBytes", dbs.SQL("bandwidthBytes+:bytes")).
|
||||
Param("bytes", bandwidthBytes).
|
||||
UpdateQuickly()
|
||||
}
|
||||
}
|
||||
|
||||
// NotifyUpdate 同步服务所在的集群
|
||||
@@ -2587,7 +2664,7 @@ func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
||||
if clusterId == 0 {
|
||||
return nil
|
||||
}
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, serverId, NodeTaskTypeConfigChanged)
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, serverId, NodeTaskTypeConfigChanged)
|
||||
}
|
||||
|
||||
// NotifyClusterUpdate 同步指定的集群
|
||||
@@ -2595,7 +2672,7 @@ func (this *ServerDAO) NotifyClusterUpdate(tx *dbs.Tx, clusterId, serverId int64
|
||||
if clusterId <= 0 {
|
||||
return nil
|
||||
}
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, serverId, NodeTaskTypeConfigChanged)
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, serverId, NodeTaskTypeConfigChanged)
|
||||
}
|
||||
|
||||
// NotifyDNSUpdate 通知当前集群DNS更新
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -141,7 +142,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
|
||||
|
||||
var tx *dbs.Tx
|
||||
{
|
||||
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "hello.teaos.cn", 0)
|
||||
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "hello.teaos.cn", 0, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -149,7 +150,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
|
||||
}
|
||||
|
||||
{
|
||||
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 0)
|
||||
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 0, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -157,7 +158,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
|
||||
}
|
||||
|
||||
{
|
||||
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 23)
|
||||
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 23, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -189,7 +190,7 @@ func TestServerDAO_FindAllEnabledServersWithNode_Cache(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, server := range servers {
|
||||
_, _ = models.SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true)
|
||||
_, _ = models.SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true, false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +201,7 @@ func TestServerDAO_FindAllEnabledServersWithNode_Cache(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, server := range servers {
|
||||
_, _ = models.SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true)
|
||||
_, _ = models.SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true, false)
|
||||
}
|
||||
}
|
||||
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||
@@ -322,6 +323,15 @@ func TestServerDAO_FindBool(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerDAO_UpdateServerBandwidth(t *testing.T) {
|
||||
var dao = models.NewServerDAO()
|
||||
var tx *dbs.Tx
|
||||
err := dao.UpdateServerBandwidth(tx, 1, timeutil.FormatTime("YmdHi", time.Now().Unix()/300*300), 1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkServerDAO_CountAllEnabledServers(b *testing.B) {
|
||||
models.SharedServerDAO = models.NewServerDAO()
|
||||
|
||||
|
||||
@@ -279,7 +279,7 @@ func (this *ServerGroupDAO) InitGroupWeb(tx *dbs.Tx, groupId int64) (int64, erro
|
||||
}
|
||||
|
||||
// ComposeGroupConfig 组合配置
|
||||
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap *utils.CacheMap) (*serverconfigs.ServerGroupConfig, error) {
|
||||
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, forList bool, cacheMap *utils.CacheMap) (*serverconfigs.ServerGroupConfig, error) {
|
||||
if cacheMap == nil {
|
||||
cacheMap = utils.NewCacheMap()
|
||||
}
|
||||
@@ -315,65 +315,67 @@ func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheM
|
||||
IsOn: group.IsOn,
|
||||
}
|
||||
|
||||
if IsNotNull(group.HttpReverseProxy) {
|
||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(group.HttpReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.HTTPReverseProxyRef = reverseProxyRef
|
||||
if !forList {
|
||||
if IsNotNull(group.HttpReverseProxy) {
|
||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(group.HttpReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.HTTPReverseProxyRef = reverseProxyRef
|
||||
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.HTTPReverseProxy = reverseProxyConfig
|
||||
}
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.HTTPReverseProxy = reverseProxyConfig
|
||||
}
|
||||
}
|
||||
|
||||
if IsNotNull(group.TcpReverseProxy) {
|
||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(group.TcpReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.TCPReverseProxyRef = reverseProxyRef
|
||||
if IsNotNull(group.TcpReverseProxy) {
|
||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(group.TcpReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.TCPReverseProxyRef = reverseProxyRef
|
||||
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.TCPReverseProxy = reverseProxyConfig
|
||||
}
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.TCPReverseProxy = reverseProxyConfig
|
||||
}
|
||||
}
|
||||
|
||||
if IsNotNull(group.UdpReverseProxy) {
|
||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(group.UdpReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.UDPReverseProxyRef = reverseProxyRef
|
||||
if IsNotNull(group.UdpReverseProxy) {
|
||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||
err := json.Unmarshal(group.UdpReverseProxy, reverseProxyRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.UDPReverseProxyRef = reverseProxyRef
|
||||
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.UDPReverseProxy = reverseProxyConfig
|
||||
}
|
||||
}
|
||||
if reverseProxyConfig != nil {
|
||||
config.UDPReverseProxy = reverseProxyConfig
|
||||
}
|
||||
}
|
||||
|
||||
// web
|
||||
if group.WebId > 0 {
|
||||
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(group.WebId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if webConfig != nil {
|
||||
config.Web = webConfig
|
||||
// web
|
||||
if group.WebId > 0 {
|
||||
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(group.WebId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if webConfig != nil {
|
||||
config.Web = webConfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -125,3 +125,33 @@ func (this *Server) DecodeUDPPorts() (ports []int) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DecodeServerNames 获取域名
|
||||
func (this *Server) DecodeServerNames() (serverNames []*serverconfigs.ServerNameConfig, count int) {
|
||||
if len(this.ServerNames) == 0 {
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
serverNames = []*serverconfigs.ServerNameConfig{}
|
||||
err := json.Unmarshal(this.ServerNames, &serverNames)
|
||||
if err != nil {
|
||||
remotelogs.Error("Server/DecodeServerNames", "decode server names failed: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
for _, serverName := range serverNames {
|
||||
count += serverName.Count()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FirstServerName 获取第一个域名
|
||||
func (this *Server) FirstServerName() string {
|
||||
serverNames, _ := this.DecodeServerNames()
|
||||
if len(serverNames) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return serverNames[0].FirstName()
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ func (this *SSLCertDAO) UpdateCert(tx *dbs.Tx,
|
||||
return nil
|
||||
}
|
||||
var oldCert = oldOne.(*SSLCert)
|
||||
var dataIsChanged = bytes.Compare(certData, oldCert.CertData) != 0 || bytes.Compare(keyData, oldCert.KeyData) != 0
|
||||
var dataIsChanged = !bytes.Equal(certData, oldCert.CertData) || !bytes.Equal(keyData, oldCert.KeyData)
|
||||
|
||||
var op = NewSSLCertOperator()
|
||||
op.Id = certId
|
||||
|
||||
@@ -141,3 +141,12 @@ func (this *SysLockerDAO) Increase(tx *dbs.Tx, key string, defaultValue int64) (
|
||||
Result("version").
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
|
||||
// 读取当前版本号
|
||||
func (this *SysLockerDAO) Read(tx *dbs.Tx, key string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("key", key).
|
||||
Result("version").
|
||||
FindInt64Col(0)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/zero"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SysSettingDAO dbs.DAO
|
||||
@@ -140,42 +142,6 @@ func (this *SysSettingDAO) ReadGlobalConfig(tx *dbs.Tx) (*serverconfigs.GlobalCo
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// ReadUserServerConfig 读取用户服务配置
|
||||
func (this *SysSettingDAO) ReadUserServerConfig(tx *dbs.Tx) (*userconfigs.UserServerConfig, error) {
|
||||
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeUserServerConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(valueJSON) == 0 {
|
||||
return userconfigs.DefaultUserServerConfig(), nil
|
||||
}
|
||||
|
||||
var config = userconfigs.DefaultUserServerConfig()
|
||||
err = json.Unmarshal(valueJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// ReadUserFinanceConfig 读取用户服务配置
|
||||
func (this *SysSettingDAO) ReadUserFinanceConfig(tx *dbs.Tx) (*userconfigs.UserFinanceConfig, error) {
|
||||
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeUserFinanceConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(valueJSON) == 0 {
|
||||
return userconfigs.DefaultUserFinanceConfig(), nil
|
||||
}
|
||||
|
||||
var config = userconfigs.DefaultUserFinanceConfig()
|
||||
err = json.Unmarshal(valueJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// ReadAdminUIConfig 读取管理员界面配置
|
||||
func (this *SysSettingDAO) ReadAdminUIConfig(tx *dbs.Tx, cacheMap *utils.CacheMap) (*systemconfigs.AdminUIConfig, error) {
|
||||
var cacheKey = this.Table + ":ReadAdminUIConfig"
|
||||
@@ -211,6 +177,36 @@ func (this *SysSettingDAO) NotifyUpdate(tx *dbs.Tx, code string) error {
|
||||
switch code {
|
||||
case systemconfigs.SettingCodeAccessLogQueue:
|
||||
accessLogQueueChanged <- zero.New()
|
||||
case systemconfigs.SettingCodeAdminUIConfig:
|
||||
// 修改当前时区
|
||||
config, err := this.ReadAdminUIConfig(nil, nil)
|
||||
if err == nil && config != nil {
|
||||
if len(config.TimeZone) == 0 {
|
||||
config.TimeZone = nodeconfigs.DefaultTimeZoneLocation
|
||||
}
|
||||
location, err := time.LoadLocation(config.TimeZone)
|
||||
if err == nil && time.Local != location {
|
||||
time.Local = location
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadUserServerConfig 读取用户服务配置
|
||||
func (this *SysSettingDAO) ReadUserServerConfig(tx *dbs.Tx) (*userconfigs.UserServerConfig, error) {
|
||||
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeUserServerConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(valueJSON) == 0 {
|
||||
return userconfigs.DefaultUserServerConfig(), nil
|
||||
}
|
||||
|
||||
var config = userconfigs.DefaultUserServerConfig()
|
||||
err = json.Unmarshal(valueJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ type SysSetting struct {
|
||||
}
|
||||
|
||||
type SysSettingOperator struct {
|
||||
Id interface{} // ID
|
||||
UserId interface{} // 用户ID
|
||||
Code interface{} // 代号
|
||||
Value interface{} // 配置值
|
||||
Id any // ID
|
||||
UserId any // 用户ID
|
||||
Code any // 代号
|
||||
Value any // 配置值
|
||||
}
|
||||
|
||||
func NewSysSettingOperator() *SysSettingOperator {
|
||||
|
||||
63
internal/db/models/traffic_package_dao.go
Normal file
63
internal/db/models/traffic_package_dao.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
TrafficPackageStateEnabled = 1 // 已启用
|
||||
TrafficPackageStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type TrafficPackageDAO dbs.DAO
|
||||
|
||||
func NewTrafficPackageDAO() *TrafficPackageDAO {
|
||||
return dbs.NewDAO(&TrafficPackageDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeTrafficPackages",
|
||||
Model: new(TrafficPackage),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*TrafficPackageDAO)
|
||||
}
|
||||
|
||||
var SharedTrafficPackageDAO *TrafficPackageDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedTrafficPackageDAO = NewTrafficPackageDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableTrafficPackage 启用条目
|
||||
func (this *TrafficPackageDAO) EnableTrafficPackage(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", TrafficPackageStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableTrafficPackage 禁用条目
|
||||
func (this *TrafficPackageDAO) DisableTrafficPackage(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", TrafficPackageStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledTrafficPackage 查找启用中的条目
|
||||
func (this *TrafficPackageDAO) FindEnabledTrafficPackage(tx *dbs.Tx, id int64) (*TrafficPackage, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
State(TrafficPackageStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*TrafficPackage), err
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package accounts
|
||||
package models_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
24
internal/db/models/traffic_package_model.go
Normal file
24
internal/db/models/traffic_package_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package models
|
||||
|
||||
// TrafficPackage 流量包
|
||||
type TrafficPackage struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Size uint32 `field:"size"` // 尺寸
|
||||
Unit string `field:"unit"` // 单位(gb|tb等)
|
||||
Bytes uint64 `field:"bytes"` // 字节
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type TrafficPackageOperator struct {
|
||||
Id any // ID
|
||||
Size any // 尺寸
|
||||
Unit any // 单位(gb|tb等)
|
||||
Bytes any // 字节
|
||||
IsOn any // 是否启用
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewTrafficPackageOperator() *TrafficPackageOperator {
|
||||
return &TrafficPackageOperator{}
|
||||
}
|
||||
1
internal/db/models/traffic_package_model_ext.go
Normal file
1
internal/db/models/traffic_package_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
63
internal/db/models/traffic_package_period_dao.go
Normal file
63
internal/db/models/traffic_package_period_dao.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
TrafficPackagePeriodStateEnabled = 1 // 已启用
|
||||
TrafficPackagePeriodStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type TrafficPackagePeriodDAO dbs.DAO
|
||||
|
||||
func NewTrafficPackagePeriodDAO() *TrafficPackagePeriodDAO {
|
||||
return dbs.NewDAO(&TrafficPackagePeriodDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeTrafficPackagePeriods",
|
||||
Model: new(TrafficPackagePeriod),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*TrafficPackagePeriodDAO)
|
||||
}
|
||||
|
||||
var SharedTrafficPackagePeriodDAO *TrafficPackagePeriodDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedTrafficPackagePeriodDAO = NewTrafficPackagePeriodDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableTrafficPackagePeriod 启用条目
|
||||
func (this *TrafficPackagePeriodDAO) EnableTrafficPackagePeriod(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", TrafficPackagePeriodStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableTrafficPackagePeriod 禁用条目
|
||||
func (this *TrafficPackagePeriodDAO) DisableTrafficPackagePeriod(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", TrafficPackagePeriodStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledTrafficPackagePeriod 查找启用中的条目
|
||||
func (this *TrafficPackagePeriodDAO) FindEnabledTrafficPackagePeriod(tx *dbs.Tx, id int64) (*TrafficPackagePeriod, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
State(TrafficPackagePeriodStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*TrafficPackagePeriod), err
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package accounts
|
||||
package models_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
24
internal/db/models/traffic_package_period_model.go
Normal file
24
internal/db/models/traffic_package_period_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package models
|
||||
|
||||
// TrafficPackagePeriod 流量包有效期
|
||||
type TrafficPackagePeriod struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
Count uint32 `field:"count"` // 数量
|
||||
Unit string `field:"unit"` // 单位:month, year
|
||||
Months uint32 `field:"months"` // 月数
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type TrafficPackagePeriodOperator struct {
|
||||
Id any // ID
|
||||
IsOn any // 是否启用
|
||||
Count any // 数量
|
||||
Unit any // 单位:month, year
|
||||
Months any // 月数
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewTrafficPackagePeriodOperator() *TrafficPackagePeriodOperator {
|
||||
return &TrafficPackagePeriodOperator{}
|
||||
}
|
||||
1
internal/db/models/traffic_package_period_model_ext.go
Normal file
1
internal/db/models/traffic_package_period_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
28
internal/db/models/traffic_package_price_dao.go
Normal file
28
internal/db/models/traffic_package_price_dao.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
type TrafficPackagePriceDAO dbs.DAO
|
||||
|
||||
func NewTrafficPackagePriceDAO() *TrafficPackagePriceDAO {
|
||||
return dbs.NewDAO(&TrafficPackagePriceDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeTrafficPackagePrices",
|
||||
Model: new(TrafficPackagePrice),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*TrafficPackagePriceDAO)
|
||||
}
|
||||
|
||||
var SharedTrafficPackagePriceDAO *TrafficPackagePriceDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedTrafficPackagePriceDAO = NewTrafficPackagePriceDAO()
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package models
|
||||
package models_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
24
internal/db/models/traffic_package_price_model.go
Normal file
24
internal/db/models/traffic_package_price_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package models
|
||||
|
||||
// TrafficPackagePrice 流量包价格
|
||||
type TrafficPackagePrice struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
PackageId uint32 `field:"packageId"` // 套餐ID
|
||||
RegionId uint32 `field:"regionId"` // 区域ID
|
||||
PeriodId uint32 `field:"periodId"` // 有效期ID
|
||||
Price float64 `field:"price"` // 价格
|
||||
DiscountPrice float64 `field:"discountPrice"` // 折后价格
|
||||
}
|
||||
|
||||
type TrafficPackagePriceOperator struct {
|
||||
Id any // ID
|
||||
PackageId any // 套餐ID
|
||||
RegionId any // 区域ID
|
||||
PeriodId any // 有效期ID
|
||||
Price any // 价格
|
||||
DiscountPrice any // 折后价格
|
||||
}
|
||||
|
||||
func NewTrafficPackagePriceOperator() *TrafficPackagePriceOperator {
|
||||
return &TrafficPackagePriceOperator{}
|
||||
}
|
||||
1
internal/db/models/traffic_package_price_model_ext.go
Normal file
1
internal/db/models/traffic_package_price_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
@@ -1,8 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -10,6 +14,8 @@ import (
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"math"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -55,7 +61,7 @@ func init() {
|
||||
}
|
||||
|
||||
// UpdateUserBandwidth 写入数据
|
||||
func (this *UserBandwidthStatDAO) UpdateUserBandwidth(tx *dbs.Tx, userId int64, day string, timeAt string, bytes int64) error {
|
||||
func (this *UserBandwidthStatDAO) UpdateUserBandwidth(tx *dbs.Tx, userId int64, regionId int64, day string, timeAt string, bytes int64) error {
|
||||
if userId <= 0 {
|
||||
// 如果用户ID不大于0,则说明服务不属于任何用户,此时不需要处理
|
||||
return nil
|
||||
@@ -65,10 +71,11 @@ func (this *UserBandwidthStatDAO) UpdateUserBandwidth(tx *dbs.Tx, userId int64,
|
||||
Table(this.partialTable(userId)).
|
||||
Param("bytes", bytes).
|
||||
InsertOrUpdateQuickly(maps.Map{
|
||||
"userId": userId,
|
||||
"day": day,
|
||||
"timeAt": timeAt,
|
||||
"bytes": bytes,
|
||||
"userId": userId,
|
||||
"regionId": regionId,
|
||||
"day": day,
|
||||
"timeAt": timeAt,
|
||||
"bytes": bytes,
|
||||
}, maps.Map{
|
||||
"bytes": dbs.SQL("bytes+:bytes"),
|
||||
})
|
||||
@@ -79,9 +86,12 @@ func (this *UserBandwidthStatDAO) UpdateUserBandwidth(tx *dbs.Tx, userId int64,
|
||||
func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInMonth(tx *dbs.Tx, userId int64, month string) (*UserBandwidthStat, error) {
|
||||
one, err := this.Query(tx).
|
||||
Table(this.partialTable(userId)).
|
||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", "SUM(bytes) AS bytes").
|
||||
Attr("userId", userId).
|
||||
Between("day", month+"01", month+"31").
|
||||
Desc("bytes").
|
||||
Group("day").
|
||||
Group("timeAt").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
@@ -89,14 +99,100 @@ func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInMonth(tx *dbs.Tx, userI
|
||||
return one.(*UserBandwidthStat), nil
|
||||
}
|
||||
|
||||
// FindPercentileBetweenDays 获取日期段内内百分位
|
||||
// regionId 如果为 -1 表示没有区域的带宽;如果为 0 表示所有区域的带宽
|
||||
func (this *UserBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, userId int64, regionId int64, dayFrom string, dayTo string, percentile int32) (result *UserBandwidthStat, err error) {
|
||||
if dayFrom > dayTo {
|
||||
dayFrom, dayTo = dayTo, dayFrom
|
||||
}
|
||||
|
||||
if percentile <= 0 {
|
||||
percentile = 95
|
||||
}
|
||||
|
||||
// 如果是100%以上,则快速返回
|
||||
if percentile >= 100 {
|
||||
var query = this.Query(tx).
|
||||
Table(this.partialTable(userId))
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
} else if regionId < 0 {
|
||||
query.Attr("regionId", 0)
|
||||
}
|
||||
one, err := query.
|
||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", "SUM(bytes) AS bytes").
|
||||
Attr("userId", userId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Desc("bytes").
|
||||
Group("day").
|
||||
Group("timeAt").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*UserBandwidthStat), nil
|
||||
}
|
||||
|
||||
// 总数量
|
||||
var totalQuery = this.Query(tx).
|
||||
Table(this.partialTable(userId))
|
||||
if regionId > 0 {
|
||||
totalQuery.Attr("regionId", regionId)
|
||||
} else if regionId < 0 {
|
||||
totalQuery.Attr("regionId", 0)
|
||||
}
|
||||
total, err := totalQuery.
|
||||
Attr("userId", userId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
CountAttr("DISTINCT day, timeAt")
|
||||
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 位置
|
||||
var query = this.Query(tx).
|
||||
Table(this.partialTable(userId))
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
} else if regionId < 0 {
|
||||
query.Attr("regionId", 0)
|
||||
}
|
||||
one, err := query.
|
||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", "SUM(bytes) AS bytes").
|
||||
Attr("userId", userId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Desc("bytes").
|
||||
Group("day").
|
||||
Group("timeAt").
|
||||
Offset(offset).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*UserBandwidthStat), nil
|
||||
}
|
||||
|
||||
// FindUserPeekBandwidthInDay 读取某日带宽峰值
|
||||
// day YYYYMMDD
|
||||
func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInDay(tx *dbs.Tx, userId int64, day string) (*UserBandwidthStat, error) {
|
||||
one, err := this.Query(tx).
|
||||
Table(this.partialTable(userId)).
|
||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "MIN(day) AS day", "timeAt", "SUM(bytes) AS bytes").
|
||||
Attr("userId", userId).
|
||||
Attr("day", day).
|
||||
Desc("bytes").
|
||||
Group("timeAt").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
@@ -104,9 +200,121 @@ func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInDay(tx *dbs.Tx, userId
|
||||
return one.(*UserBandwidthStat), nil
|
||||
}
|
||||
|
||||
// FindUserBandwidthStatsBetweenDays 查找日期段内的带宽峰值
|
||||
// dayFrom YYYYMMDD
|
||||
// dayTo YYYYMMDD
|
||||
func (this *UserBandwidthStatDAO) FindUserBandwidthStatsBetweenDays(tx *dbs.Tx, userId int64, regionId int64, dayFrom string, dayTo string) (result []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat, err error) {
|
||||
if userId <= 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
|
||||
}
|
||||
|
||||
var query = this.Query(tx).
|
||||
Table(this.partialTable(userId))
|
||||
if regionId > 0 {
|
||||
query.Attr("regionId", regionId)
|
||||
}
|
||||
ones, _, err := query.
|
||||
Result("SUM(bytes) AS bytes", "day", "timeAt").
|
||||
Attr("userId", userId).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Group("day").
|
||||
Group("timeAt").
|
||||
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
|
||||
}
|
||||
|
||||
// FindDistinctUserIds 获取所有有带宽的用户ID
|
||||
// dayFrom YYYYMMDD
|
||||
// dayTo YYYYMMDD
|
||||
func (this *UserBandwidthStatDAO) FindDistinctUserIds(tx *dbs.Tx, dayFrom string, dayTo string) (userIds []int64, err error) {
|
||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||
dayTo = strings.ReplaceAll(dayTo, "-", "")
|
||||
|
||||
err = this.runBatch(func(table string, locker *sync.Mutex) error {
|
||||
ones, err := this.Query(tx).
|
||||
Table(table).
|
||||
Between("day", dayFrom, dayTo).
|
||||
Result("DISTINCT userId").
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, one := range ones {
|
||||
locker.Lock()
|
||||
userIds = append(userIds, int64(one.(*UserBandwidthStat).UserId))
|
||||
locker.Unlock()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Clean 清理过期数据
|
||||
func (this *UserBandwidthStatDAO) Clean(tx *dbs.Tx) error {
|
||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -62)) // 保留大约2个月的数据
|
||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -100)) // 保留大约3个月的数据
|
||||
return this.runBatch(func(table string, locker *sync.Mutex) error {
|
||||
_, err := this.Query(tx).
|
||||
Table(table).
|
||||
|
||||
@@ -5,14 +5,38 @@ import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUserBandwidthStatDAO_FindUserPeekBandwidthInMonth(t *testing.T) {
|
||||
var dao = models.NewUserBandwidthStatDAO()
|
||||
var tx *dbs.Tx
|
||||
stat, err := dao.FindUserPeekBandwidthInMonth(tx, 1, timeutil.Format("Ym"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
logs.PrintAsJSON(stat, t)
|
||||
}
|
||||
|
||||
func TestUserBandwidthStatDAO_FindUserPeekBandwidthInDay(t *testing.T) {
|
||||
var dao = models.NewUserBandwidthStatDAO()
|
||||
var tx *dbs.Tx
|
||||
stat, err := dao.FindUserPeekBandwidthInDay(tx, 1, timeutil.Format("Ymd"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
logs.PrintAsJSON(stat, t)
|
||||
}
|
||||
|
||||
func TestUserBandwidthStatDAO_UpdateServerBandwidth(t *testing.T) {
|
||||
var dao = models.NewUserBandwidthStatDAO()
|
||||
var tx *dbs.Tx
|
||||
err := dao.UpdateUserBandwidth(tx, 1, timeutil.Format("Ymd"), timeutil.Format("Hi"), 1024)
|
||||
err := dao.UpdateUserBandwidth(tx, 1, 0, timeutil.Format("Ymd"), timeutil.Format("Hi"), 1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -28,3 +52,30 @@ func TestUserBandwidthStatDAO_Clean(t *testing.T) {
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestUserBandwidthStatDAO_FindBandwidthStatsBetweenDays(t *testing.T) {
|
||||
var dao = models.NewUserBandwidthStatDAO()
|
||||
var tx *dbs.Tx
|
||||
stats, err := dao.FindUserBandwidthStatsBetweenDays(tx, 1, 0, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -2)), timeutil.Format("Ymd"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var dataCount = 0
|
||||
for _, stat := range stats {
|
||||
t.Log(stat.Day, stat.TimeAt, "bytes:", stat.Bytes, "bits:", stat.Bits)
|
||||
if stat.Bytes > 0 {
|
||||
dataCount++
|
||||
}
|
||||
}
|
||||
t.Log("data count:", dataCount)
|
||||
}
|
||||
|
||||
func TestUserBandwidthStatDAO_FindPercentileBetweenDays(t *testing.T) {
|
||||
var dao = models.NewUserBandwidthStatDAO()
|
||||
var tx *dbs.Tx
|
||||
stat, err := dao.FindPercentileBetweenDays(tx, 1, 0, timeutil.Format("Ymd"), timeutil.Format("Ymd"), 95)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logs.PrintAsJSON(stat, t)
|
||||
}
|
||||
|
||||
@@ -2,19 +2,21 @@ package models
|
||||
|
||||
// UserBandwidthStat 用户月带宽峰值
|
||||
type UserBandwidthStat struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
Day string `field:"day"` // 日期YYYYMMDD
|
||||
TimeAt string `field:"timeAt"` // 时间点HHII
|
||||
Bytes uint64 `field:"bytes"` // 带宽
|
||||
Id uint64 `field:"id"` // ID
|
||||
UserId uint64 `field:"userId"` // 用户ID
|
||||
Day string `field:"day"` // 日期YYYYMMDD
|
||||
TimeAt string `field:"timeAt"` // 时间点HHII
|
||||
Bytes uint64 `field:"bytes"` // 带宽
|
||||
RegionId uint32 `field:"regionId"` // 区域ID
|
||||
}
|
||||
|
||||
type UserBandwidthStatOperator struct {
|
||||
Id interface{} // ID
|
||||
UserId interface{} // 用户ID
|
||||
Day interface{} // 日期YYYYMMDD
|
||||
TimeAt interface{} // 时间点HHII
|
||||
Bytes interface{} // 带宽
|
||||
Id any // ID
|
||||
UserId any // 用户ID
|
||||
Day any // 日期YYYYMMDD
|
||||
TimeAt any // 时间点HHII
|
||||
Bytes any // 带宽
|
||||
RegionId any // 区域ID
|
||||
}
|
||||
|
||||
func NewUserBandwidthStatOperator() *UserBandwidthStatOperator {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user