Compare commits
150 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
881bb89ac0 | ||
|
|
0e4158f600 | ||
|
|
57bbd77ae5 | ||
|
|
6906b3094b | ||
|
|
aec28b5087 | ||
|
|
b59ed1f73e | ||
|
|
2a83f61bdd | ||
|
|
4f21d60ca4 | ||
|
|
0a6111b2e5 | ||
|
|
4b425e1698 | ||
|
|
bee7da807b | ||
|
|
a906a7db06 | ||
|
|
ea62cf0ff7 | ||
|
|
72e0c55f5d | ||
|
|
3a88f23181 | ||
|
|
967c9080fb | ||
|
|
f44b9434ad | ||
|
|
e21a3c5f8c | ||
|
|
88dae56b6c | ||
|
|
40c3475306 | ||
|
|
318c8dd566 | ||
|
|
a5f30b1573 | ||
|
|
a8ec959c70 | ||
|
|
c393b2f480 | ||
|
|
548f56f8f0 | ||
|
|
9aa71365b9 | ||
|
|
292fb72a26 | ||
|
|
e5315c3b8d | ||
|
|
a4dd7bb75a | ||
|
|
53ef0f3fb2 | ||
|
|
6af8bff802 | ||
|
|
d849f7440a | ||
|
|
425c0ec44c | ||
|
|
aad0b01581 | ||
|
|
bc2c3dfa0b | ||
|
|
0fe76430c6 | ||
|
|
979ff4c44e | ||
|
|
b0b6b5984f | ||
|
|
5d4da6cccb | ||
|
|
912ffa062f | ||
|
|
1db4661c75 | ||
|
|
f7dd9e3f39 | ||
|
|
0cc74b920e | ||
|
|
51c3807d01 | ||
|
|
c2c42ca2b7 | ||
|
|
2a6db6ebfe | ||
|
|
30d8edbdcf | ||
|
|
177afafe12 | ||
|
|
98765b6e2a | ||
|
|
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 | ||
|
|
d0b908bcaa | ||
|
|
3de25d4fe1 | ||
|
|
07194855bf | ||
|
|
d0f1eb13ee | ||
|
|
a0930bfd74 | ||
|
|
08cff8affc | ||
|
|
02132e9262 | ||
|
|
61b6a49885 | ||
|
|
896e54ebe8 | ||
|
|
1b36bad60a | ||
|
|
fc14800d70 | ||
|
|
fa61f277e4 | ||
|
|
9117309472 | ||
|
|
6bb2977d59 | ||
|
|
df9dce76cb | ||
|
|
4cb9c85a1c | ||
|
|
f4f5389ffb | ||
|
|
5d336eb77d | ||
|
|
c552eb3b0e | ||
|
|
455952e9e4 | ||
|
|
7132401c7f | ||
|
|
a4dddfb139 | ||
|
|
7ef32bad97 | ||
|
|
732513a644 | ||
|
|
756cf4a9ae | ||
|
|
a15a630265 | ||
|
|
3fab1b8294 | ||
|
|
215635f429 | ||
|
|
dbb1ae180b | ||
|
|
e8d4d01d85 | ||
|
|
6593989a84 |
@@ -95,9 +95,6 @@ function build() {
|
|||||||
cp -R "$ROOT"/deploy "$DIST/"
|
cp -R "$ROOT"/deploy "$DIST/"
|
||||||
rm -f "$DIST"/deploy/.gitignore
|
rm -f "$DIST"/deploy/.gitignore
|
||||||
cp -R "$ROOT"/installers "$DIST"/
|
cp -R "$ROOT"/installers "$DIST"/
|
||||||
cp -R "$ROOT"/resources "$DIST"/
|
|
||||||
rm -f "$DIST"/resources/ipdata/ip2region/global_region.csv
|
|
||||||
rm -f "$DIST"/resources/ipdata/ip2region/ip.merge.txt
|
|
||||||
|
|
||||||
# building edge installer
|
# building edge installer
|
||||||
echo "building node installer ..."
|
echo "building node installer ..."
|
||||||
@@ -108,12 +105,14 @@ function build() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
# building edge dns installer
|
# building edge dns installer
|
||||||
echo "building dns node installer ..."
|
if [ $TAG = "plus" ]; then
|
||||||
architects=("amd64" "arm64")
|
echo "building dns node installer ..."
|
||||||
for arch in "${architects[@]}"; do
|
architects=("amd64" "arm64")
|
||||||
# TODO support arm, mips ...
|
for arch in "${architects[@]}"; do
|
||||||
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
|
# TODO support arm, mips ...
|
||||||
done
|
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
|
# 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
|
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"
|
prefix: "edge"
|
||||||
models:
|
models:
|
||||||
package: internal/web/models
|
package: internal/web/models
|
||||||
|
|
||||||
|
|
||||||
|
fields:
|
||||||
|
bool: [ "uamIsOn", "followPort", "requestHostExcludingPort", "autoRemoteStart", "autoInstallNftables" ]
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -5,6 +5,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
||||||
@@ -14,7 +15,6 @@ import (
|
|||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"github.com/iwind/gosock/pkg/gosock"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -72,6 +72,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Println("done")
|
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() {
|
app.On("goman", func() {
|
||||||
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
||||||
reply, err := sock.Send(&gosock.Command{Code: "goman"})
|
reply, err := sock.Send(&gosock.Command{Code: "goman"})
|
||||||
@@ -130,7 +138,7 @@ func main() {
|
|||||||
flagSet.BoolVar(&formatJSON, "json", false, "")
|
flagSet.BoolVar(&formatJSON, "json", false, "")
|
||||||
_ = flagSet.Parse(os.Args[2:])
|
_ = flagSet.Parse(os.Args[2:])
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(Tea.LogFile("issues.log"))
|
data, err := os.ReadFile(Tea.LogFile("issues.log"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if formatJSON {
|
if formatJSON {
|
||||||
fmt.Print("[]")
|
fmt.Print("[]")
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
|
||||||
"github.com/iwind/gosock/pkg/gosock"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -51,7 +51,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
unzip := utils.NewUnzip(zipPath, targetPath)
|
unzip := helpers.NewUnzip(zipPath, targetPath)
|
||||||
err := unzip.Run()
|
err := unzip.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
stderr("ERROR: " + err.Error())
|
stderr("ERROR: " + err.Error())
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
// 注意这里的依赖文件应该最小化,从而使编译后的文件最小化
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
|
||||||
"github.com/iwind/gosock/pkg/gosock"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -51,7 +52,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
unzip := utils.NewUnzip(zipPath, targetPath)
|
unzip := helpers.NewUnzip(zipPath, targetPath)
|
||||||
err := unzip.Run()
|
err := unzip.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
stderr("ERROR: " + err.Error())
|
stderr("ERROR: " + err.Error())
|
||||||
|
|||||||
@@ -1,193 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/lists"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// 导入数据
|
|
||||||
if lists.ContainsString(os.Args, "import") {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(Tea.Root + "/resources/ipdata/ip2region/global_region.csv")
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(data) == 0 {
|
|
||||||
logs.Println("[ERROR]file content should not be empty")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
lines := bytes.Split(data, []byte{'\n'})
|
|
||||||
for _, line := range lines {
|
|
||||||
line = bytes.TrimSpace(line)
|
|
||||||
if len(line) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
s := string(line)
|
|
||||||
reg := regexp.MustCompile(`(?U)(\d+),(\d+),(.+),(\d+),`)
|
|
||||||
if !reg.MatchString(s) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
result := reg.FindStringSubmatch(s)
|
|
||||||
dataId := result[1]
|
|
||||||
parentDataId := result[2]
|
|
||||||
name := result[3]
|
|
||||||
level := result[4]
|
|
||||||
|
|
||||||
switch level {
|
|
||||||
case "1": // 国家|地区
|
|
||||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithDataId(nil, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if countryId == 0 {
|
|
||||||
logs.Println("creating country or region ", name)
|
|
||||||
_, err = regions.SharedRegionCountryDAO.CreateCountry(nil, name, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "2": // 省份|地区
|
|
||||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithDataId(nil, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if provinceId == 0 {
|
|
||||||
logs.Println("creating province", name)
|
|
||||||
|
|
||||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithDataId(nil, parentDataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if countryId == 0 {
|
|
||||||
logs.Println("[ERROR]can not find country from data id '" + parentDataId + "'")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = regions.SharedRegionProvinceDAO.CreateProvince(nil, countryId, name, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "3": // 城市
|
|
||||||
cityId, err := regions.SharedRegionCityDAO.FindCityWithDataId(nil, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if cityId == 0 {
|
|
||||||
logs.Println("creating city", name)
|
|
||||||
|
|
||||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithDataId(nil, parentDataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = regions.SharedRegionCityDAO.CreateCity(nil, provinceId, name, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logs.Println("done")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查数据
|
|
||||||
if lists.ContainsString(os.Args, "check") {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(Tea.Root + "/resources/ipdata/ip2region/ip.merge.txt")
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(data) == 0 {
|
|
||||||
logs.Println("[ERROR]file should not be empty")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
lines := bytes.Split(data, []byte("\n"))
|
|
||||||
for index, line := range lines {
|
|
||||||
s := string(bytes.TrimSpace(line))
|
|
||||||
if len(s) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pieces := strings.Split(s, "|")
|
|
||||||
countryName := pieces[2]
|
|
||||||
provinceName := pieces[4]
|
|
||||||
providerName := pieces[6]
|
|
||||||
|
|
||||||
// 记录provider
|
|
||||||
if len(providerName) > 0 && providerName != "0" {
|
|
||||||
providerId, err := regions.SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, providerName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]find provider id failed: " + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if providerId == 0 {
|
|
||||||
logs.Println("creating new provider '"+providerName+"' ... ", index, "line")
|
|
||||||
_, err = regions.SharedRegionProviderDAO.CreateProvider(nil, providerName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("create new provider failed: " + providerName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logs.Println("created new provider '" + providerName + "'")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if lists.ContainsString([]string{"0", "欧洲", "北美地区", "法国南部领地", "非洲地区", "亚太地区"}, countryName) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查国家
|
|
||||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, countryName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if countryId == 0 {
|
|
||||||
logs.Println("[ERROR]can not find country '"+countryName+"', index: ", index, "data: "+s)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查省份
|
|
||||||
if countryName == "中国" {
|
|
||||||
if lists.ContainsString([]string{"0"}, provinceName) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, countryId, provinceName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if provinceId == 0 {
|
|
||||||
logs.Println("[ERROR]can not find province '"+provinceName+"', index: ", index, "data: "+s)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logs.Println("done")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"go/format"
|
"go/format"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -67,7 +66,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile(sqlFile, dst, 0666)
|
err = os.WriteFile(sqlFile, dst, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]write file failed: " + err.Error())
|
fmt.Println("[ERROR]write file failed: " + err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
13
go.mod
13
go.mod
@@ -14,11 +14,14 @@ require (
|
|||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.2
|
||||||
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e
|
||||||
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62
|
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/mozillazg/go-pinyin v0.18.0
|
||||||
github.com/pkg/sftp v1.12.0
|
github.com/pkg/sftp v1.12.0
|
||||||
github.com/shirou/gopsutil/v3 v3.22.2
|
github.com/shirou/gopsutil/v3 v3.22.2
|
||||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
|
github.com/smartwalle/alipay/v3 v3.1.7
|
||||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
|
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/grpc v1.45.0
|
||||||
google.golang.org/protobuf v1.27.1
|
google.golang.org/protobuf v1.27.1
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
@@ -26,21 +29,21 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
|
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/go-ole/go-ole v1.2.6 // indirect
|
||||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/kr/fs v0.1.0 // indirect
|
github.com/kr/fs v0.1.0 // indirect
|
||||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // 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/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // 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/go-sysconf v0.3.9 // indirect
|
||||||
github.com/tklauser/numcpus v0.3.0 // indirect
|
github.com/tklauser/numcpus v0.3.0 // indirect
|
||||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
golang.org/x/text v0.4.0 // indirect
|
||||||
golang.org/x/text v0.3.7 // indirect
|
|
||||||
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
|
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
|
||||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||||
gopkg.in/square/go-jose.v2 v2.6.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 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
|
||||||
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
|
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/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 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 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
||||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
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/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
github.com/infobloxopen/infoblox-go-client v1.1.1/go.mod h1:BXiw7S2b9qJoM8MS40vfgCNB2NLHGusk1DtO16BD9zI=
|
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-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 h1:cw4b6ecXdXvLd45YSstD8r9ClcnVK4ljZMZCept2aOk=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e/go.mod h1:fi/Pq+/5m2HZoseM+39dMF57ANXRt6w4PkGu3NXPc5s=
|
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 h1:HJH6RDheAY156DnIfJSD/bEvqyXzsZuE2gzs8PuUjoo=
|
||||||
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
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=
|
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.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
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/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 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 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w=
|
||||||
github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
|
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-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-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-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-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-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/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-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-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-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.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
|
||||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
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-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-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
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-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-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-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
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-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-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/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-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-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-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-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.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
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-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-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
|
||||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
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.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/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.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.5/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.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
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-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-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/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 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
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-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.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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -24,11 +24,11 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
|
|||||||
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
||||||
|
|
||||||
// 设置记录
|
// 设置记录
|
||||||
index := strings.Index(fqdn, "."+this.dnsDomain)
|
var index = strings.Index(fqdn, "."+this.dnsDomain)
|
||||||
if index < 0 {
|
if index < 0 {
|
||||||
return errors.New("invalid fqdn value")
|
return errors.New("invalid fqdn value")
|
||||||
}
|
}
|
||||||
recordName := fqdn[:index]
|
var recordName = fqdn[:index]
|
||||||
record, err := this.raw.QueryRecord(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
|
record, err := this.raw.QueryRecord(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("query DNS record failed: " + err.Error())
|
return errors.New("query DNS record failed: " + err.Error())
|
||||||
|
|||||||
@@ -9,30 +9,11 @@ type Provider struct {
|
|||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
APIURL string `json:"apiURL"`
|
APIURL string `json:"apiURL"`
|
||||||
|
TestAPIURL string `json:"testAPIURL"`
|
||||||
RequireEAB bool `json:"requireEAB"`
|
RequireEAB bool `json:"requireEAB"`
|
||||||
EABDescription string `json:"eabDescription"`
|
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 {
|
func FindProviderWithCode(code string) *Provider {
|
||||||
for _, provider := range FindAllProviders() {
|
for _, provider := range FindAllProviders() {
|
||||||
if provider.Code == code {
|
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"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
acmelog "github.com/go-acme/lego/v4/log"
|
acmelog "github.com/go-acme/lego/v4/log"
|
||||||
"github.com/go-acme/lego/v4/registration"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
@@ -40,6 +41,7 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
if this.task.Provider.RequireEAB && this.task.Account == nil {
|
if this.task.Provider.RequireEAB && this.task.Account == nil {
|
||||||
err = errors.New("account should not be nil when provider require EAB")
|
err = errors.New("account should not be nil when provider require EAB")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch this.task.AuthType {
|
switch this.task.AuthType {
|
||||||
@@ -55,7 +57,9 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
|
|||||||
|
|
||||||
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
||||||
if !this.debug {
|
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 {
|
if this.task.User == nil {
|
||||||
@@ -75,7 +79,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config := lego.NewConfig(this.task.User)
|
var config = lego.NewConfig(this.task.User)
|
||||||
config.Certificate.KeyType = certcrypto.RSA2048
|
config.Certificate.KeyType = certcrypto.RSA2048
|
||||||
config.CADirURL = this.task.Provider.APIURL
|
config.CADirURL = this.task.Provider.APIURL
|
||||||
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
||||||
@@ -86,7 +90,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注册用户
|
// 注册用户
|
||||||
resource := this.task.User.GetRegistration()
|
var resource = this.task.User.GetRegistration()
|
||||||
if resource != nil {
|
if resource != nil {
|
||||||
resource, err = client.Registration.QueryRegistration()
|
resource, err = client.Registration.QueryRegistration()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -124,7 +128,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 申请证书
|
// 申请证书
|
||||||
request := certificate.ObtainRequest{
|
var request = certificate.ObtainRequest{
|
||||||
Domains: this.task.Domains,
|
Domains: this.task.Domains,
|
||||||
Bundle: true,
|
Bundle: true,
|
||||||
}
|
}
|
||||||
@@ -138,7 +142,9 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
|
|
||||||
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
||||||
if !this.debug {
|
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 {
|
if this.task.User == nil {
|
||||||
@@ -146,7 +152,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config := lego.NewConfig(this.task.User)
|
var config = lego.NewConfig(this.task.User)
|
||||||
config.Certificate.KeyType = certcrypto.RSA2048
|
config.Certificate.KeyType = certcrypto.RSA2048
|
||||||
config.CADirURL = this.task.Provider.APIURL
|
config.CADirURL = this.task.Provider.APIURL
|
||||||
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
||||||
@@ -157,7 +163,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注册用户
|
// 注册用户
|
||||||
resource := this.task.User.GetRegistration()
|
var resource = this.task.User.GetRegistration()
|
||||||
if resource != nil {
|
if resource != nil {
|
||||||
resource, err = client.Registration.QueryRegistration()
|
resource, err = client.Registration.QueryRegistration()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -195,7 +201,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 申请证书
|
// 申请证书
|
||||||
request := certificate.ObtainRequest{
|
var request = certificate.ObtainRequest{
|
||||||
Domains: this.task.Domains,
|
Domains: this.task.Domains,
|
||||||
Bundle: true,
|
Bundle: true,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,3 +132,47 @@ func (this *APIConfig) WriteFile(path string) error {
|
|||||||
|
|
||||||
return os.WriteFile(path, data, 0666)
|
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
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.5.0"
|
Version = "0.5.7"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,11 +18,11 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.5.0"
|
NodeVersion = "0.5.6"
|
||||||
UserNodeVersion = "0.4.0"
|
UserNodeVersion = "0.5.6"
|
||||||
|
DNSNodeVersion = "0.2.8"
|
||||||
AuthorityNodeVersion = "0.0.2"
|
AuthorityNodeVersion = "0.0.2"
|
||||||
MonitorNodeVersion = "0.0.4"
|
MonitorNodeVersion = "0.0.4"
|
||||||
DNSNodeVersion = "0.2.5"
|
|
||||||
ReportNodeVersion = "0.1.1"
|
ReportNodeVersion = "0.1.1"
|
||||||
|
|
||||||
// SQLVersion SQL版本号
|
// SQLVersion SQL版本号
|
||||||
|
|||||||
@@ -13,22 +13,26 @@ type OrderMethod struct {
|
|||||||
Url string `field:"url"` // URL
|
Url string `field:"url"` // URL
|
||||||
Secret string `field:"secret"` // 密钥
|
Secret string `field:"secret"` // 密钥
|
||||||
Params dbs.JSON `field:"params"` // 参数
|
Params dbs.JSON `field:"params"` // 参数
|
||||||
|
ClientType string `field:"clientType"` // 客户端类型
|
||||||
|
QrcodeTitle string `field:"qrcodeTitle"` // 二维码标题
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
type OrderMethodOperator struct {
|
type OrderMethodOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
Name interface{} // 名称
|
Name any // 名称
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Description interface{} // 描述
|
Description any // 描述
|
||||||
ParentCode interface{} // 内置的父级代号
|
ParentCode any // 内置的父级代号
|
||||||
Code interface{} // 代号
|
Code any // 代号
|
||||||
Url interface{} // URL
|
Url any // URL
|
||||||
Secret interface{} // 密钥
|
Secret any // 密钥
|
||||||
Params interface{} // 参数
|
Params any // 参数
|
||||||
Order interface{} // 排序
|
ClientType any // 客户端类型
|
||||||
State interface{} // 状态
|
QrcodeTitle any // 二维码标题
|
||||||
|
Order any // 排序
|
||||||
|
State any // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOrderMethodOperator() *OrderMethodOperator {
|
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
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package acme
|
package acme
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -72,8 +73,9 @@ func (this *ACMEProviderAccountDAO) FindACMEProviderAccountName(tx *dbs.Tx, id i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateAccount 创建账号
|
// CreateAccount 创建账号
|
||||||
func (this *ACMEProviderAccountDAO) CreateAccount(tx *dbs.Tx, name string, providerCode string, eabKid string, eabKey string) (int64, error) {
|
func (this *ACMEProviderAccountDAO) CreateAccount(tx *dbs.Tx, userId int64, name string, providerCode string, eabKid string, eabKey string) (int64, error) {
|
||||||
var op = NewACMEProviderAccountOperator()
|
var op = NewACMEProviderAccountOperator()
|
||||||
|
op.UserId = userId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.ProviderCode = providerCode
|
op.ProviderCode = providerCode
|
||||||
op.EabKid = eabKid
|
op.EabKid = eabKid
|
||||||
@@ -98,15 +100,18 @@ func (this *ACMEProviderAccountDAO) UpdateAccount(tx *dbs.Tx, accountId int64, n
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledAccounts 计算账号数量
|
// CountAllEnabledAccounts 计算账号数量
|
||||||
func (this *ACMEProviderAccountDAO) CountAllEnabledAccounts(tx *dbs.Tx) (int64, error) {
|
func (this *ACMEProviderAccountDAO) CountAllEnabledAccounts(tx *dbs.Tx, userId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
|
State(ACMEProviderAccountStateEnabled).
|
||||||
|
Attr("userId", userId).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListEnabledAccounts 查找单页账号
|
// ListEnabledAccounts 查找单页账号
|
||||||
func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, offset int64, size int64) (result []*ACMEProviderAccount, err error) {
|
func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, userId int64, offset int64, size int64) (result []*ACMEProviderAccount, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(ACMEProviderAccountStateEnabled).
|
State(ACMEProviderAccountStateEnabled).
|
||||||
|
Attr("userId", userId).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Limit(size).
|
Limit(size).
|
||||||
DescPk().
|
DescPk().
|
||||||
@@ -116,12 +121,34 @@ func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, offset int64
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindAllEnabledAccountsWithProviderCode 根据服务商代号查找账号
|
// FindAllEnabledAccountsWithProviderCode 根据服务商代号查找账号
|
||||||
func (this *ACMEProviderAccountDAO) FindAllEnabledAccountsWithProviderCode(tx *dbs.Tx, providerCode string) (result []*ACMEProviderAccount, err error) {
|
func (this *ACMEProviderAccountDAO) FindAllEnabledAccountsWithProviderCode(tx *dbs.Tx, userId int64, providerCode string) (result []*ACMEProviderAccount, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(ACMEProviderAccountStateEnabled).
|
State(ACMEProviderAccountStateEnabled).
|
||||||
Attr("providerCode", providerCode).
|
Attr("providerCode", providerCode).
|
||||||
|
Attr("userId", userId).
|
||||||
DescPk().
|
DescPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckUserAccount 检查是否为用户的服务商账号
|
||||||
|
func (this *ACMEProviderAccountDAO) CheckUserAccount(tx *dbs.Tx, userId int64, accountId int64) error {
|
||||||
|
if userId <= 0 || accountId <= 0 {
|
||||||
|
return models.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := this.Query(tx).
|
||||||
|
Pk(accountId).
|
||||||
|
State(ACMEProviderAccountStateEnabled).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !b {
|
||||||
|
return models.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,24 +3,26 @@ package acme
|
|||||||
// ACMEProviderAccount ACME提供商
|
// ACMEProviderAccount ACME提供商
|
||||||
type ACMEProviderAccount struct {
|
type ACMEProviderAccount struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
IsOn bool `field:"isOn"` // 是否启用
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
ProviderCode string `field:"providerCode"` // 代号
|
ProviderCode string `field:"providerCode"` // 代号
|
||||||
Error string `field:"error"` // 最后一条错误信息
|
|
||||||
EabKid string `field:"eabKid"` // KID
|
EabKid string `field:"eabKid"` // KID
|
||||||
EabKey string `field:"eabKey"` // Key
|
EabKey string `field:"eabKey"` // Key
|
||||||
|
Error string `field:"error"` // 最后一条错误信息
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
type ACMEProviderAccountOperator struct {
|
type ACMEProviderAccountOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
UserId any // 用户ID
|
||||||
Name interface{} // 名称
|
IsOn any // 是否启用
|
||||||
ProviderCode interface{} // 代号
|
Name any // 名称
|
||||||
Error interface{} // 最后一条错误信息
|
ProviderCode any // 代号
|
||||||
EabKid interface{} // KID
|
EabKid any // KID
|
||||||
EabKey interface{} // Key
|
EabKey any // Key
|
||||||
State interface{} // 状态
|
Error any // 最后一条错误信息
|
||||||
|
State any // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewACMEProviderAccountOperator() *ACMEProviderAccountOperator {
|
func NewACMEProviderAccountOperator() *ACMEProviderAccountOperator {
|
||||||
|
|||||||
@@ -106,8 +106,9 @@ func (this *ACMETaskDAO) DisableAllTasksWithCertId(tx *dbs.Tx, certId int64) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledACMETasks 计算所有任务数量
|
// 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) {
|
||||||
query := dbutils.NewQuery(tx, this, adminId, userId)
|
var query = this.Query(tx)
|
||||||
|
query.Attr("userId", userId) // 这个条件必须加上
|
||||||
if isAvailable || isExpired || expiringDays > 0 {
|
if isAvailable || isExpired || expiringDays > 0 {
|
||||||
query.Gt("certId", 0)
|
query.Gt("certId", 0)
|
||||||
|
|
||||||
@@ -137,8 +138,9 @@ func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, use
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListEnabledACMETasks 列出单页任务
|
// 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) {
|
||||||
query := dbutils.NewQuery(tx, this, adminId, userId)
|
var query = this.Query(tx)
|
||||||
|
query.Attr("userId", userId) // 这个条件必须加上
|
||||||
if isAvailable || isExpired || expiringDays > 0 {
|
if isAvailable || isExpired || expiringDays > 0 {
|
||||||
query.Gt("certId", 0)
|
query.Gt("certId", 0)
|
||||||
|
|
||||||
@@ -227,8 +229,13 @@ func (this *ACMETaskDAO) UpdateACMETask(tx *dbs.Tx, acmeTaskId int64, acmeUserId
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckACMETask 检查权限
|
// 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) {
|
||||||
return dbutils.NewQuery(tx, this, adminId, userId).
|
var query = this.Query(tx)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.
|
||||||
State(ACMETaskStateEnabled).
|
State(ACMETaskStateEnabled).
|
||||||
Pk(acmeTaskId).
|
Pk(acmeTaskId).
|
||||||
Exist()
|
Exist()
|
||||||
@@ -319,7 +326,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteUser := acmeutils.NewUser(user.Email, privateKey, func(resource *registration.Resource) error {
|
var remoteUser = acmeutils.NewUser(user.Email, privateKey, func(resource *registration.Resource) error {
|
||||||
resourceJSON, err := json.Marshal(resource)
|
resourceJSON, err := json.Marshal(resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -349,7 +356,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
errMsg = "找不到DNS服务商账号"
|
errMsg = "找不到DNS服务商账号"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
providerInterface := dnsclients.FindProvider(dnsProvider.Type)
|
providerInterface := dnsclients.FindProvider(dnsProvider.Type, int64(dnsProvider.Id))
|
||||||
if providerInterface == nil {
|
if providerInterface == nil {
|
||||||
errMsg = "暂不支持此类型的DNS服务商 '" + dnsProvider.Type + "'"
|
errMsg = "暂不支持此类型的DNS服务商 '" + dnsProvider.Type + "'"
|
||||||
return
|
return
|
||||||
@@ -382,7 +389,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
acmeTask.Provider = acmeProvider
|
acmeTask.Provider = acmeProvider
|
||||||
acmeTask.Account = acmeAccount
|
acmeTask.Account = acmeAccount
|
||||||
|
|
||||||
acmeRequest := acmeutils.NewRequest(acmeTask)
|
var acmeRequest = acmeutils.NewRequest(acmeTask)
|
||||||
acmeRequest.OnAuth(func(domain, token, keyAuth string) {
|
acmeRequest.OnAuth(func(domain, token, keyAuth string) {
|
||||||
err := SharedACMEAuthenticationDAO.CreateAuth(tx, taskId, domain, token, keyAuth)
|
err := SharedACMEAuthenticationDAO.CreateAuth(tx, taskId, domain, token, keyAuth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -398,7 +405,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("ACME", "encode auth data failed: '"+task.AuthURL+"'")
|
remotelogs.Error("ACME", "encode auth data failed: '"+task.AuthURL+"'")
|
||||||
} else {
|
} else {
|
||||||
client := utils.SharedHttpClient(5 * time.Second)
|
var client = utils.SharedHttpClient(10 * time.Second)
|
||||||
req, err := http.NewRequest(http.MethodPost, task.AuthURL, bytes.NewReader(authJSON))
|
req, err := http.NewRequest(http.MethodPost, task.AuthURL, bytes.NewReader(authJSON))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
|
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
|
||||||
@@ -423,7 +430,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 分析证书
|
// 分析证书
|
||||||
sslConfig := &sslconfigs.SSLCertConfig{
|
var sslConfig = &sslconfigs.SSLCertConfig{
|
||||||
CertData: certData,
|
CertData: certData,
|
||||||
KeyData: keyData,
|
KeyData: keyData,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (this *ApiTokenDAO) FindEnabledTokenWithNodeCacheable(tx *dbs.Tx, nodeId st
|
|||||||
State(ApiTokenStateEnabled).
|
State(ApiTokenStateEnabled).
|
||||||
Find()
|
Find()
|
||||||
if one != nil {
|
if one != nil {
|
||||||
token := one.(*ApiToken)
|
token = one.(*ApiToken)
|
||||||
SharedCacheLocker.Lock()
|
SharedCacheLocker.Lock()
|
||||||
apiTokenCacheMap[nodeId] = token
|
apiTokenCacheMap[nodeId] = token
|
||||||
SharedCacheLocker.Unlock()
|
SharedCacheLocker.Unlock()
|
||||||
|
|||||||
@@ -2,18 +2,15 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"hash/crc32"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -31,20 +28,11 @@ type httpAccessLogDefinition struct {
|
|||||||
// HTTP服务访问
|
// HTTP服务访问
|
||||||
var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO
|
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
|
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
|
||||||
type HTTPAccessLogDAOWrapper struct {
|
type HTTPAccessLogDAOWrapper struct {
|
||||||
DAO *HTTPAccessLogDAO
|
DAO *HTTPAccessLogDAO
|
||||||
NodeId int64
|
NodeId int64
|
||||||
}
|
IsLocal bool
|
||||||
|
|
||||||
// NSAccessLogDAOWrapper NS访问日志DAO
|
|
||||||
type NSAccessLogDAOWrapper struct {
|
|
||||||
DAO *NSAccessLogDAO
|
|
||||||
NodeId int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -78,102 +66,28 @@ func AllAccessLogDBs() []*dbs.DB {
|
|||||||
// 获取获取DAO
|
// 获取获取DAO
|
||||||
func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
|
func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
|
||||||
accessLogLocker.RLock()
|
accessLogLocker.RLock()
|
||||||
|
defer accessLogLocker.RUnlock()
|
||||||
if len(httpAccessLogDAOMapping) == 0 {
|
if len(httpAccessLogDAOMapping) == 0 {
|
||||||
dao = nil
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config, err := db.Config()
|
var daoList = []*HTTPAccessLogDAOWrapper{}
|
||||||
if err != nil {
|
|
||||||
return "", false, err
|
for _, d := range httpAccessLogDAOMapping {
|
||||||
|
daoList = append(daoList, d)
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName = "edgeNSAccessLogs_" + day
|
var l = len(daoList)
|
||||||
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
|
if l == 0 {
|
||||||
|
return
|
||||||
accessLogLocker.RLock()
|
|
||||||
_, ok = nsAccessLogTableMapping[cacheKey]
|
|
||||||
accessLogLocker.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return tableName, true, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tableNames, err := db.TableNames()
|
if l == 1 {
|
||||||
if err != nil {
|
return daoList[0]
|
||||||
return tableName, false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tableName, utils.ContainsStringInsensitive(tableNames, tableName), nil
|
return daoList[rands.Int(0, l-1)]
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DBNodeInitializer 初始化数据库连接
|
// DBNodeInitializer 初始化数据库连接
|
||||||
@@ -209,14 +123,14 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeIds := []int64{}
|
var nodeIds = []int64{}
|
||||||
for _, node := range dbNodes {
|
for _, node := range dbNodes {
|
||||||
nodeIds = append(nodeIds, int64(node.Id))
|
nodeIds = append(nodeIds, int64(node.Id))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关掉老的
|
// 关掉老的
|
||||||
accessLogLocker.Lock()
|
accessLogLocker.Lock()
|
||||||
closingDbs := []*dbs.DB{}
|
var closingDbs = []*dbs.DB{}
|
||||||
for nodeId, db := range accessLogDBMapping {
|
for nodeId, db := range accessLogDBMapping {
|
||||||
if !lists.ContainsInt64(nodeIds, nodeId) {
|
if !lists.ContainsInt64(nodeIds, nodeId) {
|
||||||
closingDbs = append(closingDbs, db)
|
closingDbs = append(closingDbs, db)
|
||||||
@@ -233,12 +147,12 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
|
|
||||||
// 启动新的
|
// 启动新的
|
||||||
for _, node := range dbNodes {
|
for _, node := range dbNodes {
|
||||||
nodeId := int64(node.Id)
|
var nodeId = int64(node.Id)
|
||||||
accessLogLocker.Lock()
|
accessLogLocker.Lock()
|
||||||
db, ok := accessLogDBMapping[nodeId]
|
db, ok := accessLogDBMapping[nodeId]
|
||||||
accessLogLocker.Unlock()
|
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 {
|
if ok {
|
||||||
// 检查配置是否有变化
|
// 检查配置是否有变化
|
||||||
@@ -283,7 +197,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
daoObject := dbs.DAOObject{
|
var daoObject = dbs.DAOObject{
|
||||||
Instance: db,
|
Instance: db,
|
||||||
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
|
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
|
||||||
Table: tableDef.Name,
|
Table: tableDef.Name,
|
||||||
@@ -298,59 +212,19 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
|
|
||||||
accessLogLocker.Lock()
|
accessLogLocker.Lock()
|
||||||
accessLogDBMapping[nodeId] = db
|
accessLogDBMapping[nodeId] = db
|
||||||
dao := &HTTPAccessLogDAO{
|
var dao = &HTTPAccessLogDAO{
|
||||||
DAOObject: daoObject,
|
DAOObject: daoObject,
|
||||||
}
|
}
|
||||||
httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
|
httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
|
||||||
DAO: dao,
|
DAO: dao,
|
||||||
NodeId: nodeId,
|
NodeId: nodeId,
|
||||||
|
IsLocal: dbutils.IsLocalAddr(node.Host),
|
||||||
}
|
}
|
||||||
accessLogLocker.Unlock()
|
accessLogLocker.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// nsAccessLog
|
// 扩展
|
||||||
{
|
initAccessLogDAO(db, node)
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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
|
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 计算某个服务商下的域名数量
|
// 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).
|
return this.Query(tx).
|
||||||
State(DNSDomainStateEnabled).
|
State(DNSDomainStateEnabled).
|
||||||
Attr("providerId", providerId).
|
Attr("providerId", providerId).
|
||||||
|
Attr("isDeleted", isDeleted).
|
||||||
|
Attr("isUp", isUp).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,11 +56,13 @@ func (this *DNSTaskDAO) CreateDNSTask(tx *dbs.Tx, clusterId int64, serverId int6
|
|||||||
"isDone": false,
|
"isDone": false,
|
||||||
"isOk": false,
|
"isOk": false,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"version": time.Now().UnixNano(),
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"updatedAt": time.Now().Unix(),
|
"updatedAt": time.Now().Unix(),
|
||||||
"isDone": false,
|
"isDone": false,
|
||||||
"isOk": false,
|
"isOk": false,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"version": time.Now().UnixNano(),
|
||||||
})
|
})
|
||||||
return err
|
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) {
|
func (this *DNSTaskDAO) FindAllDoingTasks(tx *dbs.Tx) (result []*DNSTask, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Attr("isDone", 0).
|
Attr("isDone", 0).
|
||||||
|
Asc("version").
|
||||||
AscPk().
|
AscPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
@@ -109,6 +112,7 @@ func (this *DNSTaskDAO) FindAllDoingOrErrorTasks(tx *dbs.Tx, nodeClusterId int64
|
|||||||
_, err = query.
|
_, err = query.
|
||||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||||
Asc("updatedAt").
|
Asc("updatedAt").
|
||||||
|
Asc("version").
|
||||||
AscPk().
|
AscPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
@@ -163,3 +167,13 @@ func (this *DNSTaskDAO) UpdateDNSTaskDone(tx *dbs.Tx, taskId int64) error {
|
|||||||
op.Error = ""
|
op.Error = ""
|
||||||
return this.Save(tx, op)
|
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) {
|
func TestDNSTaskDAO_CreateDNSTask(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "taskType")
|
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "cdn", "taskType")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,20 +13,22 @@ type DNSTask struct {
|
|||||||
IsDone bool `field:"isDone"` // 是否已完成
|
IsDone bool `field:"isDone"` // 是否已完成
|
||||||
IsOk bool `field:"isOk"` // 是否成功
|
IsOk bool `field:"isOk"` // 是否成功
|
||||||
Error string `field:"error"` // 错误信息
|
Error string `field:"error"` // 错误信息
|
||||||
|
Version uint64 `field:"version"` // 版本
|
||||||
}
|
}
|
||||||
|
|
||||||
type DNSTaskOperator struct {
|
type DNSTaskOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId any // 集群ID
|
||||||
ServerId interface{} // 服务ID
|
ServerId any // 服务ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId any // 节点ID
|
||||||
DomainId interface{} // 域名ID
|
DomainId any // 域名ID
|
||||||
RecordName interface{} // 记录名
|
RecordName any // 记录名
|
||||||
Type interface{} // 任务类型
|
Type any // 任务类型
|
||||||
UpdatedAt interface{} // 更新时间
|
UpdatedAt any // 更新时间
|
||||||
IsDone interface{} // 是否已完成
|
IsDone any // 是否已完成
|
||||||
IsOk interface{} // 是否成功
|
IsOk any // 是否成功
|
||||||
Error interface{} // 错误信息
|
Error any // 错误信息
|
||||||
|
Version any // 版本
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSTaskOperator() *DNSTaskOperator {
|
func NewDNSTaskOperator() *DNSTaskOperator {
|
||||||
|
|||||||
@@ -15,9 +15,12 @@ import (
|
|||||||
|
|
||||||
// CheckClusterDNS 检查集群的DNS问题
|
// CheckClusterDNS 检查集群的DNS问题
|
||||||
// 藏这么深是避免package循环引用的问题
|
// 藏这么深是避免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) {
|
||||||
clusterId := int64(cluster.Id)
|
var clusterId = int64(cluster.Id)
|
||||||
domainId := int64(cluster.DnsDomainId)
|
var domainId = int64(cluster.DnsDomainId)
|
||||||
|
|
||||||
|
// 集群DNS设置
|
||||||
|
var clusterDNSConfig, _ = cluster.DecodeDNSConfig()
|
||||||
|
|
||||||
// 检查域名
|
// 检查域名
|
||||||
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, domainId, nil)
|
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, domainId, nil)
|
||||||
@@ -64,7 +67,7 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var dnsProvider = dnsclients.FindProvider(provider.Type)
|
var dnsProvider = dnsclients.FindProvider(provider.Type, int64(provider.Id))
|
||||||
if dnsProvider == nil {
|
if dnsProvider == nil {
|
||||||
issues = append(issues, &pb.DNSIssue{
|
issues = append(issues, &pb.DNSIssue{
|
||||||
Target: cluster.Name,
|
Target: cluster.Name,
|
||||||
@@ -101,47 +104,27 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
|||||||
// TODO 检查域名是否已解析
|
// TODO 检查域名是否已解析
|
||||||
|
|
||||||
// 检查节点
|
// 检查节点
|
||||||
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true)
|
if checkNodeIssues {
|
||||||
if err != nil {
|
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true, clusterDNSConfig != nil && clusterDNSConfig.IncludingLnNodes)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO 检查节点数量不能为0
|
|
||||||
|
|
||||||
for _, node := range nodes {
|
|
||||||
nodeId := int64(node.Id)
|
|
||||||
|
|
||||||
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查线路是否在已有线路中
|
// TODO 检查节点数量不能为0
|
||||||
for _, routeCode := range routeCodes {
|
|
||||||
routeOk, err := domain.ContainsRouteCode(routeCode)
|
for _, node := range nodes {
|
||||||
|
nodeId := int64(node.Id)
|
||||||
|
|
||||||
|
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !routeOk {
|
if len(routeCodes) == 0 && !hasDefaultRoute {
|
||||||
issues = append(issues, &pb.DNSIssue{
|
issues = append(issues, &pb.DNSIssue{
|
||||||
Target: node.Name,
|
Target: node.Name,
|
||||||
TargetId: nodeId,
|
TargetId: nodeId,
|
||||||
Type: "node",
|
Type: "node",
|
||||||
Description: "线路已经失效,请重新选择",
|
Description: "没有选择节点所属线路",
|
||||||
Params: map[string]string{
|
Params: map[string]string{
|
||||||
"clusterName": cluster.Name,
|
"clusterName": cluster.Name,
|
||||||
"clusterId": numberutils.FormatInt64(clusterId),
|
"clusterId": numberutils.FormatInt64(clusterId),
|
||||||
@@ -150,29 +133,51 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
|||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 检查IP地址
|
// 检查线路是否在已有线路中
|
||||||
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, true, nodeconfigs.NodeRoleNode)
|
for _, routeCode := range routeCodes {
|
||||||
if err != nil {
|
routeOk, err := domain.ContainsRouteCode(routeCode)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
if len(ipAddr) == 0 {
|
}
|
||||||
issues = append(issues, &pb.DNSIssue{
|
if !routeOk {
|
||||||
Target: node.Name,
|
issues = append(issues, &pb.DNSIssue{
|
||||||
TargetId: nodeId,
|
Target: node.Name,
|
||||||
Type: "node",
|
TargetId: nodeId,
|
||||||
Description: "没有设置IP地址",
|
Type: "node",
|
||||||
Params: map[string]string{
|
Description: "线路已经失效,请重新选择",
|
||||||
"clusterName": cluster.Name,
|
Params: map[string]string{
|
||||||
"clusterId": numberutils.FormatInt64(clusterId),
|
"clusterName": cluster.Name,
|
||||||
},
|
"clusterId": numberutils.FormatInt64(clusterId),
|
||||||
MustFix: true,
|
},
|
||||||
})
|
MustFix: true,
|
||||||
continue
|
})
|
||||||
}
|
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
|
return
|
||||||
@@ -195,7 +200,7 @@ func FindDefaultDomainRoute(tx *dbs.Tx, domain *dns.DNSDomain) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("decode provider params failed: " + err.Error())
|
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 {
|
if dnsProvider == nil {
|
||||||
return "", errors.New("not supported provider type '" + provider.Type + "'")
|
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")
|
t.Log("cluster not found, skip the test")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
issues, err := CheckClusterDNS(tx, cluster)
|
issues, err := CheckClusterDNS(tx, cluster, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"golang.org/x/net/idna"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -39,9 +40,10 @@ var SharedHTTPAccessLogDAO *HTTPAccessLogDAO
|
|||||||
var (
|
var (
|
||||||
oldAccessLogQueue = make(chan *pb.HTTPAccessLog)
|
oldAccessLogQueue = make(chan *pb.HTTPAccessLog)
|
||||||
accessLogQueue = make(chan *pb.HTTPAccessLog, 10_000)
|
accessLogQueue = make(chan *pb.HTTPAccessLog, 10_000)
|
||||||
accessLogQueueMaxLength = 100_000
|
accessLogQueueMaxLength = 100_000 // 队列最大长度
|
||||||
accessLogQueuePercent = 100 // 0-100
|
accessLogQueuePercent = 100 // 0-100
|
||||||
accessLogCountPerSecond = 10_000 // 0 表示不限制
|
accessLogCountPerSecond = 10_000 // 每秒钟写入条数,0 表示不限制
|
||||||
|
accessLogPerTx = 100 // 单事务写入条数
|
||||||
accessLogConfigJSON = []byte{}
|
accessLogConfigJSON = []byte{}
|
||||||
accessLogQueueChanged = make(chan zero.Zero, 1)
|
accessLogQueueChanged = make(chan zero.Zero, 1)
|
||||||
|
|
||||||
@@ -49,6 +51,10 @@ var (
|
|||||||
accessLogRowsPerTable int64 = 500_000 // 自动分表的单表最大值
|
accessLogRowsPerTable int64 = 500_000 // 自动分表的单表最大值
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func AccessLogQueuePercent() int {
|
||||||
|
return accessLogQueuePercent
|
||||||
|
}
|
||||||
|
|
||||||
type accessLogTableQuery struct {
|
type accessLogTableQuery struct {
|
||||||
daoWrapper *HTTPAccessLogDAOWrapper
|
daoWrapper *HTTPAccessLogDAOWrapper
|
||||||
name string
|
name string
|
||||||
@@ -84,11 +90,29 @@ func init() {
|
|||||||
// 导出队列内容
|
// 导出队列内容
|
||||||
goman.New(func() {
|
goman.New(func() {
|
||||||
var ticker = time.NewTicker(1 * time.Second)
|
var ticker = time.NewTicker(1 * time.Second)
|
||||||
|
var accessLogPerLoop = accessLogPerTx
|
||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
var tx *dbs.Tx
|
var countTxs = accessLogCountPerSecond / accessLogPerLoop
|
||||||
err := SharedHTTPAccessLogDAO.DumpAccessLogsFromQueue(tx, accessLogCountPerSecond)
|
if countTxs <= 0 {
|
||||||
if err != nil {
|
countTxs = 1
|
||||||
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "dump access logs failed: "+err.Error())
|
}
|
||||||
|
for i := 0; i < countTxs; i++ {
|
||||||
|
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 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -111,13 +135,13 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.
|
|||||||
// 写入队列
|
// 写入队列
|
||||||
var queue = accessLogQueue // 这样写非常重要,防止在写入过程中队列有切换
|
var queue = accessLogQueue // 这样写非常重要,防止在写入过程中队列有切换
|
||||||
for _, accessLog := range accessLogs {
|
for _, accessLog := range accessLogs {
|
||||||
if accessLog.FirewallPolicyId == 0 { // 如果是WAF记录,则采取采样率
|
if accessLog.FirewallPolicyId == 0 { // 如果是非WAF记录,则采取采样率
|
||||||
// 采样率
|
// 采样率
|
||||||
if accessLogQueuePercent <= 0 {
|
if accessLogQueuePercent <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if accessLogQueuePercent < 100 && rands.Int(1, 100) > accessLogQueuePercent {
|
if accessLogQueuePercent < 100 && rands.Int(1, 100) > accessLogQueuePercent {
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,23 +156,49 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DumpAccessLogsFromQueue 从队列导入访问日志
|
// DumpAccessLogsFromQueue 从队列导入访问日志
|
||||||
func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(tx *dbs.Tx, size int) error {
|
func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(size int) (hasMore bool, err error) {
|
||||||
|
if size <= 0 {
|
||||||
|
size = 100
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(oldAccessLogQueue) == 0 && len(accessLogQueue) == 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
var dao = randomHTTPAccessLogDAO()
|
var dao = randomHTTPAccessLogDAO()
|
||||||
if dao == nil {
|
if dao == nil {
|
||||||
dao = &HTTPAccessLogDAOWrapper{
|
dao = &HTTPAccessLogDAOWrapper{
|
||||||
DAO: SharedHTTPAccessLogDAO,
|
DAO: SharedHTTPAccessLogDAO,
|
||||||
NodeId: 0,
|
NodeId: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查本地数据库空间
|
||||||
|
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 + ")")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if size <= 0 {
|
// 开始事务
|
||||||
size = 1_000_000
|
tx, err := dao.DAO.Instance.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = tx.Commit()
|
||||||
|
}()
|
||||||
|
|
||||||
// 复制变量,防止中途改变
|
// 复制变量,防止中途改变
|
||||||
var oldQueue = oldAccessLogQueue
|
var oldQueue = oldAccessLogQueue
|
||||||
var newQueue = accessLogQueue
|
var newQueue = accessLogQueue
|
||||||
|
|
||||||
|
hasMore = true
|
||||||
|
|
||||||
Loop:
|
Loop:
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < size; i++ {
|
||||||
// old
|
// old
|
||||||
@@ -156,7 +206,7 @@ Loop:
|
|||||||
case accessLog := <-oldQueue:
|
case accessLog := <-oldQueue:
|
||||||
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return false, err
|
||||||
}
|
}
|
||||||
continue Loop
|
continue Loop
|
||||||
default:
|
default:
|
||||||
@@ -168,20 +218,28 @@ Loop:
|
|||||||
case accessLog := <-newQueue:
|
case accessLog := <-newQueue:
|
||||||
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return false, err
|
||||||
}
|
}
|
||||||
continue Loop
|
continue Loop
|
||||||
default:
|
default:
|
||||||
|
hasMore = false
|
||||||
break Loop
|
break Loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return hasMore, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateHTTPAccessLog 写入单条访问日志
|
// CreateHTTPAccessLog 写入单条访问日志
|
||||||
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
|
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)
|
tableDef, err := SharedHTTPAccessLogManager.FindLastTable(dao.Instance, day, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -412,6 +470,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
|||||||
|
|
||||||
var dao = tableQuery.daoWrapper.DAO
|
var dao = tableQuery.daoWrapper.DAO
|
||||||
var query = dao.Query(tx)
|
var query = dao.Query(tx)
|
||||||
|
query.Result("id", "serverId", "nodeId", "status", "createdAt", "content", "requestId", "firewallPolicyId", "firewallRuleGroupId", "firewallRuleSetId", "firewallRuleId", "remoteAddr", "domain")
|
||||||
|
|
||||||
// 条件
|
// 条件
|
||||||
if nodeId > 0 {
|
if nodeId > 0 {
|
||||||
@@ -485,6 +544,14 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
|||||||
query.Where("domain LIKE :host2").
|
query.Where("domain LIKE :host2").
|
||||||
Param("host2", domain)
|
Param("host2", domain)
|
||||||
} else {
|
} 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.Attr("domain", domain)
|
||||||
query.UseIndex("domain")
|
query.UseIndex("domain")
|
||||||
}
|
}
|
||||||
@@ -753,7 +820,7 @@ func (this *HTTPAccessLogDAO) SetupQueue() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes.Compare(accessLogConfigJSON, configJSON) == 0 {
|
if bytes.Equal(accessLogConfigJSON, configJSON) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
accessLogConfigJSON = configJSON
|
accessLogConfigJSON = configJSON
|
||||||
@@ -767,6 +834,9 @@ func (this *HTTPAccessLogDAO) SetupQueue() {
|
|||||||
|
|
||||||
accessLogQueuePercent = config.Percent
|
accessLogQueuePercent = config.Percent
|
||||||
accessLogCountPerSecond = config.CountPerSecond
|
accessLogCountPerSecond = config.CountPerSecond
|
||||||
|
if accessLogCountPerSecond <= 0 {
|
||||||
|
accessLogCountPerSecond = 10_000
|
||||||
|
}
|
||||||
if config.MaxLength <= 0 {
|
if config.MaxLength <= 0 {
|
||||||
config.MaxLength = 100_000
|
config.MaxLength = 100_000
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,13 +21,13 @@ func TestCreateHTTPAccessLog(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
accessLog := &pb.HTTPAccessLog{
|
var accessLog = &pb.HTTPAccessLog{
|
||||||
ServerId: 1,
|
ServerId: 1,
|
||||||
NodeId: 4,
|
NodeId: 4,
|
||||||
Status: 200,
|
Status: 200,
|
||||||
Timestamp: time.Now().Unix(),
|
Timestamp: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
dao := randomHTTPAccessLogDAO()
|
var dao = randomHTTPAccessLogDAO()
|
||||||
t.Log("dao:", dao)
|
t.Log("dao:", dao)
|
||||||
|
|
||||||
// 先初始化
|
// 先初始化
|
||||||
@@ -37,12 +37,59 @@ func TestCreateHTTPAccessLog(t *testing.T) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
t.Log(time.Since(before).Seconds()*1000, "ms")
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateHTTPAccessLog_Tx(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
|
||||||
|
err := NewDBNodeInitializer().loop()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var accessLog = &pb.HTTPAccessLog{
|
||||||
|
ServerId: 1,
|
||||||
|
NodeId: 4,
|
||||||
|
Status: 200,
|
||||||
|
Timestamp: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
var dao = randomHTTPAccessLogDAO()
|
||||||
|
t.Log("dao:", dao)
|
||||||
|
|
||||||
|
// 先初始化
|
||||||
|
_ = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
|
||||||
|
var before = time.Now()
|
||||||
|
defer func() {
|
||||||
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||||
|
}()
|
||||||
|
|
||||||
|
tx, err = dao.DAO.Instance.Begin()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for i := 0; i < 200; i++ {
|
||||||
|
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -422,7 +422,7 @@ func (this *HTTPAccessLogManager) checkTableFields(db *dbs.DB, tableName string)
|
|||||||
}
|
}
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
var fieldName = field.GetString("Field")
|
var fieldName = field.GetString("Field")
|
||||||
if strings.ToLower(fieldName) == strings.ToLower("remoteAddr") {
|
if strings.EqualFold(fieldName, "remoteAddr") {
|
||||||
hasRemoteAddrField = true
|
hasRemoteAddrField = true
|
||||||
}
|
}
|
||||||
if strings.ToLower(fieldName) == "domain" {
|
if strings.ToLower(fieldName) == "domain" {
|
||||||
|
|||||||
@@ -68,8 +68,9 @@ func (this *HTTPAuthPolicyDAO) FindEnabledHTTPAuthPolicy(tx *dbs.Tx, id int64) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateHTTPAuthPolicy 创建策略
|
// CreateHTTPAuthPolicy 创建策略
|
||||||
func (this *HTTPAuthPolicyDAO) CreateHTTPAuthPolicy(tx *dbs.Tx, name string, methodType string, paramsJSON []byte) (int64, error) {
|
func (this *HTTPAuthPolicyDAO) CreateHTTPAuthPolicy(tx *dbs.Tx, userId int64, name string, methodType string, paramsJSON []byte) (int64, error) {
|
||||||
var op = NewHTTPAuthPolicyOperator()
|
var op = NewHTTPAuthPolicyOperator()
|
||||||
|
op.UserId = userId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Type = methodType
|
op.Type = methodType
|
||||||
op.Params = paramsJSON
|
op.Params = paramsJSON
|
||||||
@@ -137,6 +138,20 @@ func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, c
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckUserPolicy 检查用户权限
|
||||||
|
func (this *HTTPAuthPolicyDAO) CheckUserPolicy(tx *dbs.Tx, userId int64, policyId int64) error {
|
||||||
|
if userId <= 0 || policyId <= 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithHTTPAuthPolicyId(tx, policyId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return SharedHTTPWebDAO.CheckUserWeb(tx, userId, webId)
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更改
|
// NotifyUpdate 通知更改
|
||||||
func (this *HTTPAuthPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
func (this *HTTPAuthPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
||||||
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithHTTPAuthPolicyId(tx, policyId)
|
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithHTTPAuthPolicyId(tx, policyId)
|
||||||
|
|||||||
@@ -125,25 +125,13 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st
|
|||||||
MinSize: &shared.SizeCapacity{Count: 0, Unit: shared.SizeCapacityUnitKB},
|
MinSize: &shared.SizeCapacity{Count: 0, Unit: shared.SizeCapacityUnitKB},
|
||||||
SkipResponseSetCookie: true,
|
SkipResponseSetCookie: true,
|
||||||
AllowChunkedEncoding: true,
|
AllowChunkedEncoding: true,
|
||||||
Conds: &shared.HTTPRequestCondsConfig{
|
AllowPartialContent: true,
|
||||||
IsOn: true,
|
SimpleCond: &shared.HTTPRequestCond{
|
||||||
Connector: "or",
|
Type: "url-extension",
|
||||||
Groups: []*shared.HTTPRequestCondGroup{
|
IsRequest: true,
|
||||||
{
|
Param: "${requestPathExtension}",
|
||||||
IsOn: true,
|
Operator: shared.RequestCondOperatorIn,
|
||||||
Connector: "or",
|
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"]`,
|
||||||
Conds: []*shared.HTTPRequestCond{
|
|
||||||
{
|
|
||||||
Type: "url-extension",
|
|
||||||
IsRequest: true,
|
|
||||||
Param: "${requestPathExtension}",
|
|
||||||
Operator: shared.RequestCondOperatorIn,
|
|
||||||
Value: `[".html", ".js", ".css", ".gif", ".png", ".bmp", ".jpeg", ".jpg", ".webp", ".ico", ".pdf", ".ttf", ".eot", ".tiff", ".svg", ".svgz", ".eps", ".woff", ".otf", ".woff2", ".tif", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".wav", ".mp3", ".mp4", ".ogg", ".mid", ".midi"]`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Description: "初始化规则",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
refsJSON, err := json.Marshal([]*serverconfigs.HTTPCacheRef{cacheRef})
|
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) {
|
if IsNotNull(web.Auth) {
|
||||||
authConfig := &serverconfigs.HTTPAuthConfig{}
|
var authConfig = &serverconfigs.HTTPAuthConfig{}
|
||||||
err = json.Unmarshal(web.Auth, authConfig)
|
err = json.Unmarshal(web.Auth, authConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -395,6 +395,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
|||||||
if policyConfig != nil {
|
if policyConfig != nil {
|
||||||
ref.AuthPolicy = policyConfig
|
ref.AuthPolicy = policyConfig
|
||||||
newRefs = append(newRefs, ref)
|
newRefs = append(newRefs, ref)
|
||||||
|
authConfig.PolicyRefs = newRefs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
config.Auth = authConfig
|
config.Auth = authConfig
|
||||||
@@ -457,6 +458,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
|||||||
config.UAM = uamConfig
|
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 {
|
if cacheMap != nil {
|
||||||
cacheMap.Put(cacheKey, config)
|
cacheMap.Put(cacheKey, config)
|
||||||
}
|
}
|
||||||
@@ -1042,6 +1053,10 @@ func (this *HTTPWebDAO) FindWebServerGroupId(tx *dbs.Tx, webId int64) (groupId i
|
|||||||
|
|
||||||
// CheckUserWeb 检查用户权限
|
// CheckUserWeb 检查用户权限
|
||||||
func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) error {
|
func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) error {
|
||||||
|
if userId <= 0 || webId <= 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
serverId, err := this.FindWebServerId(tx, webId)
|
serverId, err := this.FindWebServerId(tx, webId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -1208,6 +1223,35 @@ func (this *HTTPWebDAO) FindWebUAM(tx *dbs.Tx, webId int64) ([]byte, error) {
|
|||||||
FindJSONCol()
|
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 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
||||||
// server
|
// server
|
||||||
|
|||||||
@@ -38,43 +38,45 @@ type HTTPWeb struct {
|
|||||||
RequestLimit dbs.JSON `field:"requestLimit"` // 请求限制
|
RequestLimit dbs.JSON `field:"requestLimit"` // 请求限制
|
||||||
RequestScripts dbs.JSON `field:"requestScripts"` // 请求脚本
|
RequestScripts dbs.JSON `field:"requestScripts"` // 请求脚本
|
||||||
Uam dbs.JSON `field:"uam"` // UAM设置
|
Uam dbs.JSON `field:"uam"` // UAM设置
|
||||||
|
Referers dbs.JSON `field:"referers"` // 防盗链设置
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPWebOperator struct {
|
type HTTPWebOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
TemplateId interface{} // 模版ID
|
TemplateId any // 模版ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId any // 用户ID
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
Root interface{} // 根目录
|
Root any // 根目录
|
||||||
Charset interface{} // 字符集
|
Charset any // 字符集
|
||||||
Shutdown interface{} // 临时关闭页面配置
|
Shutdown any // 临时关闭页面配置
|
||||||
Pages interface{} // 特殊页面
|
Pages any // 特殊页面
|
||||||
RedirectToHttps interface{} // 跳转到HTTPS设置
|
RedirectToHttps any // 跳转到HTTPS设置
|
||||||
Indexes interface{} // 首页文件列表
|
Indexes any // 首页文件列表
|
||||||
MaxRequestBodySize interface{} // 最大允许的请求内容尺寸
|
MaxRequestBodySize any // 最大允许的请求内容尺寸
|
||||||
RequestHeader interface{} // 请求Header配置
|
RequestHeader any // 请求Header配置
|
||||||
ResponseHeader interface{} // 响应Header配置
|
ResponseHeader any // 响应Header配置
|
||||||
AccessLog interface{} // 访问日志配置
|
AccessLog any // 访问日志配置
|
||||||
Stat interface{} // 统计配置
|
Stat any // 统计配置
|
||||||
Gzip interface{} // Gzip配置(v0.3.2弃用)
|
Gzip any // Gzip配置(v0.3.2弃用)
|
||||||
Compression interface{} // 压缩配置
|
Compression any // 压缩配置
|
||||||
Cache interface{} // 缓存配置
|
Cache any // 缓存配置
|
||||||
Firewall interface{} // 防火墙设置
|
Firewall any // 防火墙设置
|
||||||
Locations interface{} // 路由规则配置
|
Locations any // 路由规则配置
|
||||||
Websocket interface{} // Websocket设置
|
Websocket any // Websocket设置
|
||||||
RewriteRules interface{} // 重写规则配置
|
RewriteRules any // 重写规则配置
|
||||||
HostRedirects interface{} // 域名跳转
|
HostRedirects any // 域名跳转
|
||||||
Fastcgi interface{} // Fastcgi配置
|
Fastcgi any // Fastcgi配置
|
||||||
Auth interface{} // 认证策略配置
|
Auth any // 认证策略配置
|
||||||
Webp interface{} // WebP配置
|
Webp any // WebP配置
|
||||||
RemoteAddr interface{} // 客户端IP配置
|
RemoteAddr any // 客户端IP配置
|
||||||
MergeSlashes interface{} // 是否合并路径中的斜杠
|
MergeSlashes any // 是否合并路径中的斜杠
|
||||||
RequestLimit interface{} // 请求限制
|
RequestLimit any // 请求限制
|
||||||
RequestScripts interface{} // 请求脚本
|
RequestScripts any // 请求脚本
|
||||||
Uam interface{} // UAM设置
|
Uam any // UAM设置
|
||||||
|
Referers any // 防盗链设置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPWebOperator() *HTTPWebOperator {
|
func NewHTTPWebOperator() *HTTPWebOperator {
|
||||||
|
|||||||
@@ -353,7 +353,7 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountIPItemsWithListId 计算IP数量
|
// 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).
|
var query = this.Query(tx).
|
||||||
State(IPItemStateEnabled).
|
State(IPItemStateEnabled).
|
||||||
Attr("listId", listId)
|
Attr("listId", listId)
|
||||||
@@ -466,8 +466,11 @@ func (this *IPItemDAO) ExistsEnabledItem(tx *dbs.Tx, itemId int64) (bool, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledIPItems 计算数量
|
// 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)
|
var query = this.Query(tx)
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
query.Like("ipFrom", dbutils.QuoteLike(keyword))
|
||||||
|
}
|
||||||
if len(ip) > 0 {
|
if len(ip) > 0 {
|
||||||
query.Attr("ipFrom", ip)
|
query.Attr("ipFrom", ip)
|
||||||
}
|
}
|
||||||
@@ -496,8 +499,11 @@ func (this *IPItemDAO) CountAllEnabledIPItems(tx *dbs.Tx, ip string, listId int6
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListAllEnabledIPItems 搜索所有IP
|
// 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)
|
var query = this.Query(tx)
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
query.Like("ipFrom", dbutils.QuoteLike(keyword))
|
||||||
|
}
|
||||||
if len(ip) > 0 {
|
if len(ip) > 0 {
|
||||||
query.Attr("ipFrom", ip)
|
query.Attr("ipFrom", ip)
|
||||||
}
|
}
|
||||||
@@ -608,7 +614,7 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, clusterId := range clusterIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -616,7 +622,7 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
|||||||
} else {
|
} else {
|
||||||
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
|
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledNodeClusterIds(tx)
|
||||||
for _, clusterId := range clusterIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -668,7 +674,7 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
|||||||
|
|
||||||
if len(resultClusterIds) > 0 {
|
if len(resultClusterIds) > 0 {
|
||||||
for _, clusterId := range resultClusterIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
140
internal/db/models/ip_library_artifact_dao.go
Normal file
140
internal/db/models/ip_library_artifact_dao.go
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
IPLibraryArtifactStateEnabled = 1 // 已启用
|
||||||
|
IPLibraryArtifactStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type IPLibraryArtifactDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewIPLibraryArtifactDAO() *IPLibraryArtifactDAO {
|
||||||
|
return dbs.NewDAO(&IPLibraryArtifactDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeIPLibraryArtifacts",
|
||||||
|
Model: new(IPLibraryArtifact),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*IPLibraryArtifactDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedIPLibraryArtifactDAO *IPLibraryArtifactDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedIPLibraryArtifactDAO = NewIPLibraryArtifactDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableIPLibraryArtifact 启用条目
|
||||||
|
func (this *IPLibraryArtifactDAO) EnableIPLibraryArtifact(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", IPLibraryArtifactStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableIPLibraryArtifact 禁用条目
|
||||||
|
func (this *IPLibraryArtifactDAO) DisableIPLibraryArtifact(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", IPLibraryArtifactStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledIPLibraryArtifact 查找启用中的条目
|
||||||
|
func (this *IPLibraryArtifactDAO) FindEnabledIPLibraryArtifact(tx *dbs.Tx, id int64) (*IPLibraryArtifact, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
State(IPLibraryArtifactStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*IPLibraryArtifact), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateArtifact 创建制品
|
||||||
|
func (this *IPLibraryArtifactDAO) CreateArtifact(tx *dbs.Tx, name string, fileId int64, libraryFileId int64, meta *iplibrary.Meta) (int64, error) {
|
||||||
|
var op = NewIPLibraryArtifactOperator()
|
||||||
|
op.Name = name
|
||||||
|
op.FileId = fileId
|
||||||
|
op.LibraryFileId = libraryFileId
|
||||||
|
|
||||||
|
metaJSON, err := json.Marshal(meta)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Meta = metaJSON
|
||||||
|
op.State = IPLibraryArtifactStateEnabled
|
||||||
|
|
||||||
|
var code = stringutil.Md5(utils.Sha1RandomString())[:8]
|
||||||
|
meta.Code = code
|
||||||
|
op.Code = code // 要比较短,方便识别
|
||||||
|
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllArtifacts 查找制品列表
|
||||||
|
func (this *IPLibraryArtifactDAO) FindAllArtifacts(tx *dbs.Tx) (result []*IPLibraryArtifact, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(IPLibraryArtifactStateEnabled).
|
||||||
|
DescPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindPublicArtifact 查找当前使用的制品
|
||||||
|
func (this *IPLibraryArtifactDAO) FindPublicArtifact(tx *dbs.Tx) (*IPLibraryArtifact, error) {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
State(IPLibraryArtifactStateEnabled).
|
||||||
|
Attr("isPublic", true).
|
||||||
|
Result("id", "fileId", "code").
|
||||||
|
Find()
|
||||||
|
if err != nil || one == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return one.(*IPLibraryArtifact), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateArtifactPublic 使用某个制品
|
||||||
|
func (this *IPLibraryArtifactDAO) UpdateArtifactPublic(tx *dbs.Tx, artifactId int64, isPublic bool) error {
|
||||||
|
// 取消使用
|
||||||
|
if !isPublic {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(artifactId).
|
||||||
|
Set("isPublic", false).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用
|
||||||
|
|
||||||
|
// 先取消别的
|
||||||
|
err := this.Query(tx).
|
||||||
|
Neq("id", artifactId).
|
||||||
|
State(IPLibraryArtifactStateEnabled).
|
||||||
|
Attr("isPublic", true).
|
||||||
|
Set("isPublic", false).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(artifactId).
|
||||||
|
Set("isPublic", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package accounts
|
package models_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
32
internal/db/models/ip_library_artifact_model.go
Normal file
32
internal/db/models/ip_library_artifact_model.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// IPLibraryArtifact IP库制品
|
||||||
|
type IPLibraryArtifact struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
FileId uint64 `field:"fileId"` // 文件ID
|
||||||
|
LibraryFileId uint32 `field:"libraryFileId"` // IP库文件ID
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
Meta dbs.JSON `field:"meta"` // 元数据
|
||||||
|
IsPublic bool `field:"isPublic"` // 是否为公用
|
||||||
|
Code string `field:"code"` // 代号
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type IPLibraryArtifactOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
Name any // 名称
|
||||||
|
FileId any // 文件ID
|
||||||
|
LibraryFileId any // IP库文件ID
|
||||||
|
CreatedAt any // 创建时间
|
||||||
|
Meta any // 元数据
|
||||||
|
IsPublic any // 是否为公用
|
||||||
|
Code any // 代号
|
||||||
|
State any // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIPLibraryArtifactOperator() *IPLibraryArtifactOperator {
|
||||||
|
return &IPLibraryArtifactOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/ip_library_artifact_model_ext.go
Normal file
1
internal/db/models/ip_library_artifact_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
@@ -72,8 +72,9 @@ func (this *IPLibraryFileDAO) FindEnabledIPLibraryFile(tx *dbs.Tx, id int64) (*I
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLibraryFile 创建文件
|
// CreateLibraryFile 创建文件
|
||||||
func (this *IPLibraryFileDAO) CreateLibraryFile(tx *dbs.Tx, template string, emptyValues []string, fileId int64, countries []string, provinces [][2]string, cities [][3]string, towns [][4]string, providers []string) (int64, error) {
|
func (this *IPLibraryFileDAO) CreateLibraryFile(tx *dbs.Tx, name string, template string, emptyValues []string, fileId int64, countries []string, provinces [][2]string, cities [][3]string, towns [][4]string, providers []string) (int64, error) {
|
||||||
var op = NewIPLibraryFileOperator()
|
var op = NewIPLibraryFileOperator()
|
||||||
|
op.Name = name
|
||||||
op.Template = template
|
op.Template = template
|
||||||
|
|
||||||
if emptyValues == nil {
|
if emptyValues == nil {
|
||||||
@@ -137,6 +138,18 @@ func (this *IPLibraryFileDAO) CreateLibraryFile(tx *dbs.Tx, template string, emp
|
|||||||
return this.SaveInt64(tx, op)
|
return this.SaveInt64(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllFinishedLibraryFiles 查找所有已完成的文件
|
||||||
|
func (this *IPLibraryFileDAO) FindAllFinishedLibraryFiles(tx *dbs.Tx) (result []*IPLibraryFile, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(IPLibraryFileStateEnabled).
|
||||||
|
Result("id", "fileId", "createdAt", "generatedFileId", "generatedAt", "name"). // 这里不需要其他信息
|
||||||
|
Attr("isFinished", true).
|
||||||
|
DescPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// FindAllUnfinishedLibraryFiles 查找所有未完成的文件
|
// FindAllUnfinishedLibraryFiles 查找所有未完成的文件
|
||||||
func (this *IPLibraryFileDAO) FindAllUnfinishedLibraryFiles(tx *dbs.Tx) (result []*IPLibraryFile, err error) {
|
func (this *IPLibraryFileDAO) FindAllUnfinishedLibraryFiles(tx *dbs.Tx) (result []*IPLibraryFile, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
@@ -324,7 +337,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
var countries = []*iplibrary.Country{}
|
var countries = []*iplibrary.Country{}
|
||||||
for _, country := range dbCountries {
|
for _, country := range dbCountries {
|
||||||
countries = append(countries, &iplibrary.Country{
|
countries = append(countries, &iplibrary.Country{
|
||||||
Id: int64(country.Id),
|
Id: country.Id,
|
||||||
Name: country.DisplayName(),
|
Name: country.DisplayName(),
|
||||||
Codes: country.AllCodes(),
|
Codes: country.AllCodes(),
|
||||||
})
|
})
|
||||||
@@ -339,7 +352,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
var provinces = []*iplibrary.Province{}
|
var provinces = []*iplibrary.Province{}
|
||||||
for _, province := range dbProvinces {
|
for _, province := range dbProvinces {
|
||||||
provinces = append(provinces, &iplibrary.Province{
|
provinces = append(provinces, &iplibrary.Province{
|
||||||
Id: int64(province.Id),
|
Id: province.Id,
|
||||||
Name: province.DisplayName(),
|
Name: province.DisplayName(),
|
||||||
Codes: province.AllCodes(),
|
Codes: province.AllCodes(),
|
||||||
})
|
})
|
||||||
@@ -354,7 +367,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
var cities = []*iplibrary.City{}
|
var cities = []*iplibrary.City{}
|
||||||
for _, city := range dbCities {
|
for _, city := range dbCities {
|
||||||
cities = append(cities, &iplibrary.City{
|
cities = append(cities, &iplibrary.City{
|
||||||
Id: int64(city.Id),
|
Id: city.Id,
|
||||||
Name: city.DisplayName(),
|
Name: city.DisplayName(),
|
||||||
Codes: city.AllCodes(),
|
Codes: city.AllCodes(),
|
||||||
})
|
})
|
||||||
@@ -369,7 +382,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
var towns = []*iplibrary.Town{}
|
var towns = []*iplibrary.Town{}
|
||||||
for _, town := range dbTowns {
|
for _, town := range dbTowns {
|
||||||
towns = append(towns, &iplibrary.Town{
|
towns = append(towns, &iplibrary.Town{
|
||||||
Id: int64(town.Id),
|
Id: town.Id,
|
||||||
Name: town.DisplayName(),
|
Name: town.DisplayName(),
|
||||||
Codes: town.AllCodes(),
|
Codes: town.AllCodes(),
|
||||||
})
|
})
|
||||||
@@ -384,7 +397,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
var providers = []*iplibrary.Provider{}
|
var providers = []*iplibrary.Provider{}
|
||||||
for _, provider := range dbProviders {
|
for _, provider := range dbProviders {
|
||||||
providers = append(providers, &iplibrary.Provider{
|
providers = append(providers, &iplibrary.Provider{
|
||||||
Id: int64(provider.Id),
|
Id: provider.Id,
|
||||||
Name: provider.DisplayName(),
|
Name: provider.DisplayName(),
|
||||||
Codes: provider.AllCodes(),
|
Codes: provider.AllCodes(),
|
||||||
})
|
})
|
||||||
@@ -392,7 +405,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
|
|
||||||
var libraryCode = utils.Sha1RandomString() // 每次都生成新的code
|
var libraryCode = utils.Sha1RandomString() // 每次都生成新的code
|
||||||
var filePath = dir + "/" + this.composeFilename(libraryFileId, libraryCode)
|
var filePath = dir + "/" + this.composeFilename(libraryFileId, libraryCode)
|
||||||
writer, err := iplibrary.NewFileWriter(filePath, &iplibrary.Meta{
|
var meta = &iplibrary.Meta{
|
||||||
Author: "", // 将来用户可以自行填写
|
Author: "", // 将来用户可以自行填写
|
||||||
CreatedAt: time.Now().Unix(),
|
CreatedAt: time.Now().Unix(),
|
||||||
Countries: countries,
|
Countries: countries,
|
||||||
@@ -400,13 +413,15 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
Cities: cities,
|
Cities: cities,
|
||||||
Towns: towns,
|
Towns: towns,
|
||||||
Providers: providers,
|
Providers: providers,
|
||||||
})
|
}
|
||||||
|
writer, err := iplibrary.NewFileWriter(filePath, meta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = writer.Close()
|
_ = writer.Close()
|
||||||
|
_ = os.Remove(filePath)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = writer.WriteMeta()
|
err = writer.WriteMeta()
|
||||||
@@ -563,6 +578,12 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加制品
|
||||||
|
_, err = SharedIPLibraryArtifactDAO.CreateArtifact(tx, libraryFile.Name, generatedFileId, libraryFileId, meta)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ func TestIPLibraryFileDAO_GenerateIPLibrary(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 3)
|
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import "github.com/iwind/TeaGo/dbs"
|
|||||||
// IPLibraryFile IP库上传的文件
|
// IPLibraryFile IP库上传的文件
|
||||||
type IPLibraryFile struct {
|
type IPLibraryFile struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // IP库名称
|
||||||
FileId uint64 `field:"fileId"` // 原始文件ID
|
FileId uint64 `field:"fileId"` // 原始文件ID
|
||||||
Template string `field:"template"` // 模板
|
Template string `field:"template"` // 模板
|
||||||
EmptyValues dbs.JSON `field:"emptyValues"` // 空值列表
|
EmptyValues dbs.JSON `field:"emptyValues"` // 空值列表
|
||||||
@@ -23,6 +24,7 @@ type IPLibraryFile struct {
|
|||||||
|
|
||||||
type IPLibraryFileOperator struct {
|
type IPLibraryFileOperator struct {
|
||||||
Id any // ID
|
Id any // ID
|
||||||
|
Name any // IP库名称
|
||||||
FileId any // 原始文件ID
|
FileId any // 原始文件ID
|
||||||
Template any // 模板
|
Template any // 模板
|
||||||
EmptyValues any // 空值列表
|
EmptyValues any // 空值列表
|
||||||
|
|||||||
@@ -301,7 +301,7 @@ func (this *IPListDAO) NotifyUpdate(tx *dbs.Tx, listId int64, taskType NodeTaskT
|
|||||||
|
|
||||||
if len(resultClusterIds) > 0 {
|
if len(resultClusterIds) > 0 {
|
||||||
for _, clusterId := range resultClusterIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -38,7 +39,7 @@ func init() {
|
|||||||
func (this *LogDAO) CreateLog(tx *dbs.Tx, adminType string, adminId int64, level string, description string, action string, ip string) error {
|
func (this *LogDAO) CreateLog(tx *dbs.Tx, adminType string, adminId int64, level string, description string, action string, ip string) error {
|
||||||
var op = NewLogOperator()
|
var op = NewLogOperator()
|
||||||
op.Level = level
|
op.Level = level
|
||||||
op.Description = description
|
op.Description = utils.LimitString(description, 1000)
|
||||||
op.Action = action
|
op.Action = action
|
||||||
op.Ip = ip
|
op.Ip = ip
|
||||||
op.Type = adminType
|
op.Type = adminType
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -154,19 +155,16 @@ func (this *MessageDAO) CreateNodeMessage(tx *dbs.Tx, role string, clusterId int
|
|||||||
|
|
||||||
// CreateMessage 创建普通消息
|
// CreateMessage 创建普通消息
|
||||||
func (this *MessageDAO) CreateMessage(tx *dbs.Tx, adminId int64, userId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
|
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()
|
var op = NewMessageOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.Type = messageType
|
op.Type = messageType
|
||||||
op.Level = level
|
op.Level = level
|
||||||
|
|
||||||
subjectRunes := []rune(subject)
|
op.Subject = subject
|
||||||
if len(subjectRunes) > 100 {
|
|
||||||
op.Subject = string(subjectRunes[:100]) + "..."
|
|
||||||
} else {
|
|
||||||
op.Subject = subject
|
|
||||||
}
|
|
||||||
|
|
||||||
op.Body = body
|
op.Body = body
|
||||||
if len(paramsJSON) > 0 {
|
if len(paramsJSON) > 0 {
|
||||||
op.Params = paramsJSON
|
op.Params = paramsJSON
|
||||||
|
|||||||
@@ -368,7 +368,7 @@ func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64, isPublic bool)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, clusterId := range clusterIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -380,7 +380,7 @@ func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64, isPublic bool)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, clusterId := range clusterIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,31 +4,35 @@ import "github.com/iwind/TeaGo/dbs"
|
|||||||
|
|
||||||
// NSDomain DNS域名
|
// NSDomain DNS域名
|
||||||
type NSDomain struct {
|
type NSDomain struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
IsOn bool `field:"isOn"` // 是否启用
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 域名
|
Name string `field:"name"` // 域名
|
||||||
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
|
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
|
||||||
Tsig dbs.JSON `field:"tsig"` // TSIG配置
|
Tsig dbs.JSON `field:"tsig"` // TSIG配置
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
VerifyTXT string `field:"verifyTXT"` // 验证用的TXT
|
||||||
Version uint64 `field:"version"` // 版本号
|
VerifyExpiresAt uint64 `field:"verifyExpiresAt"` // 验证TXT过期时间
|
||||||
Status string `field:"status"` // 状态:none|verified
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
State uint8 `field:"state"` // 状态
|
Version uint64 `field:"version"` // 版本号
|
||||||
|
Status string `field:"status"` // 状态:none|verified
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
type NSDomainOperator struct {
|
type NSDomainOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId any // 集群ID
|
||||||
UserId interface{} // 用户ID
|
UserId any // 用户ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Name interface{} // 域名
|
Name any // 域名
|
||||||
GroupIds interface{} // 分组ID
|
GroupIds any // 分组ID
|
||||||
Tsig interface{} // TSIG配置
|
Tsig any // TSIG配置
|
||||||
CreatedAt interface{} // 创建时间
|
VerifyTXT any // 验证用的TXT
|
||||||
Version interface{} // 版本号
|
VerifyExpiresAt any // 验证TXT过期时间
|
||||||
Status interface{} // 状态:none|verified
|
CreatedAt any // 创建时间
|
||||||
State interface{} // 状态
|
Version any // 版本号
|
||||||
|
Status any // 状态:none|verified
|
||||||
|
State any // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNSDomainOperator() *NSDomainOperator {
|
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
|
Id uint64 `field:"id"` // ID
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
DomainId uint32 `field:"domainId"` // 域名ID
|
DomainId uint64 `field:"domainId"` // 域名ID
|
||||||
RecordId uint64 `field:"recordId"` // 记录ID
|
RecordId uint64 `field:"recordId"` // 记录ID
|
||||||
Day string `field:"day"` // YYYYMMDD
|
Day string `field:"day"` // YYYYMMDD
|
||||||
Hour string `field:"hour"` // YYYYMMDDHH
|
Hour string `field:"hour"` // YYYYMMDDHH
|
||||||
CountRequests uint32 `field:"countRequests"` // 请求数
|
CountRequests uint64 `field:"countRequests"` // 请求数
|
||||||
Bytes uint64 `field:"bytes"` // 流量
|
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 创建集群
|
// 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)
|
uniqueId, err := this.GenUniqueId(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
secret := rands.String(32)
|
var secret = rands.String(32)
|
||||||
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleCluster)
|
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleCluster)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -147,11 +147,12 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
|||||||
op.DnsDomainId = dnsDomainId
|
op.DnsDomainId = dnsDomainId
|
||||||
op.DnsName = dnsName
|
op.DnsName = dnsName
|
||||||
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
||||||
NodesAutoSync: true,
|
NodesAutoSync: true,
|
||||||
ServersAutoSync: true,
|
ServersAutoSync: true,
|
||||||
CNameRecords: []string{},
|
CNAMERecords: []string{},
|
||||||
CNameAsDomain: true,
|
CNAMEAsDomain: true,
|
||||||
TTL: 0,
|
TTL: dnsTTL,
|
||||||
|
IncludingLnNodes: true,
|
||||||
}
|
}
|
||||||
dnsJSON, err := json.Marshal(dnsConfig)
|
dnsJSON, err := json.Marshal(dnsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -172,10 +173,21 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
|||||||
}
|
}
|
||||||
op.SystemServices = systemServicesJSON
|
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.UseAllAPINodes = 1
|
||||||
op.ApiNodes = "[]"
|
op.ApiNodes = "[]"
|
||||||
op.UniqueId = uniqueId
|
op.UniqueId = uniqueId
|
||||||
op.Secret = secret
|
op.Secret = secret
|
||||||
|
op.AutoInstallNftables = autoInstallNftables
|
||||||
op.State = NodeClusterStateEnabled
|
op.State = NodeClusterStateEnabled
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,7 +198,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateCluster 修改集群
|
// UpdateCluster 修改集群
|
||||||
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, 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 {
|
if clusterId <= 0 {
|
||||||
return errors.New("invalid clusterId")
|
return errors.New("invalid clusterId")
|
||||||
}
|
}
|
||||||
@@ -203,6 +215,25 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
|
|||||||
op.NodeMaxThreads = nodeMaxThreads
|
op.NodeMaxThreads = nodeMaxThreads
|
||||||
op.AutoOpenPorts = autoOpenPorts
|
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)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -431,6 +462,27 @@ func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int
|
|||||||
FindInt64Col(0)
|
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信息
|
// FindClusterDNSInfo 查找DNS信息
|
||||||
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
|
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
|
||||||
var cacheKey = this.Table + ":FindClusterDNSInfo:" + types.String(clusterId)
|
var cacheKey = this.Table + ":FindClusterDNSInfo:" + types.String(clusterId)
|
||||||
@@ -443,7 +495,7 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cach
|
|||||||
|
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Pk(clusterId).
|
Pk(clusterId).
|
||||||
Result("id", "name", "dnsName", "dnsDomainId", "dns", "isOn").
|
Result("id", "name", "dnsName", "dnsDomainId", "dns", "isOn", "state").
|
||||||
Find()
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -468,7 +520,7 @@ func (this *NodeClusterDAO) ExistClusterDNSName(tx *dbs.Tx, dnsName string, excl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateClusterDNS 修改集群DNS相关信息
|
// UpdateClusterDNS 修改集群DNS相关信息
|
||||||
func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool, cnameRecords []string, ttl int32, cnameAsDomain bool) error {
|
func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool, cnameRecords []string, ttl int32, cnameAsDomain bool, includingLnNodes bool) error {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return errors.New("invalid clusterId")
|
return errors.New("invalid clusterId")
|
||||||
}
|
}
|
||||||
@@ -487,10 +539,16 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
|
|||||||
|
|
||||||
var oldCluster = oldOne.(*NodeCluster)
|
var oldCluster = oldOne.(*NodeCluster)
|
||||||
var oldDNSDomainId = int64(oldCluster.DnsDomainId)
|
var oldDNSDomainId = int64(oldCluster.DnsDomainId)
|
||||||
|
var shouldRemoveOld = false
|
||||||
if (oldDNSDomainId > 0 && oldDNSDomainId != dnsDomainId) || (oldCluster.DnsName != dnsName) {
|
if (oldDNSDomainId > 0 && oldDNSDomainId != dnsDomainId) || (oldCluster.DnsName != dnsName) {
|
||||||
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
|
if oldDNSDomainId == dnsDomainId {
|
||||||
if err != nil {
|
// 如果只是换子域名,需要在新的域名添加之前,先删除老的子域名,防止无法添加CNAME
|
||||||
return err
|
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
shouldRemoveOld = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,11 +562,12 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
|
|||||||
}
|
}
|
||||||
|
|
||||||
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
||||||
NodesAutoSync: nodesAutoSync,
|
NodesAutoSync: nodesAutoSync,
|
||||||
ServersAutoSync: serversAutoSync,
|
ServersAutoSync: serversAutoSync,
|
||||||
CNameRecords: cnameRecords,
|
CNAMERecords: cnameRecords,
|
||||||
TTL: ttl,
|
TTL: ttl,
|
||||||
CNameAsDomain: cnameAsDomain,
|
CNAMEAsDomain: cnameAsDomain,
|
||||||
|
IncludingLnNodes: includingLnNodes,
|
||||||
}
|
}
|
||||||
dnsJSON, err := json.Marshal(dnsConfig)
|
dnsJSON, err := json.Marshal(dnsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -524,7 +583,20 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 查找集群所属管理员
|
// FindClusterAdminId 查找集群所属管理员
|
||||||
@@ -924,7 +996,7 @@ func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, ca
|
|||||||
cluster, err := this.Query(tx).
|
cluster, err := this.Query(tx).
|
||||||
Pk(clusterId).
|
Pk(clusterId).
|
||||||
State(NodeClusterStateEnabled).
|
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()
|
Find()
|
||||||
if err != nil || cluster == nil {
|
if err != nil || cluster == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1066,7 +1138,7 @@ func (this *NodeClusterDAO) FindClusterDDoSProtection(tx *dbs.Tx, clusterId int6
|
|||||||
return one.(*NodeCluster).DecodeDDoSProtection(), nil
|
return one.(*NodeCluster).DecodeDDoSProtection(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateClusterDDoSProtection 设置集群的DDOS设置
|
// UpdateClusterDDoSProtection 设置集群的DDoS设置
|
||||||
func (this *NodeClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
|
func (this *NodeClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
@@ -1089,12 +1161,55 @@ func (this *NodeClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId in
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
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更新
|
// NotifyDNSUpdate 通知DNS更新
|
||||||
|
|||||||
@@ -177,5 +177,5 @@ func (this *NodeClusterMetricItemDAO) ExistsClusterItem(tx *dbs.Tx, clusterId in
|
|||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeClusterMetricItemDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
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"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
GrantId uint32 `field:"grantId"` // 默认认证方式
|
GrantId uint32 `field:"grantId"` // 默认认证方式
|
||||||
|
SshParams dbs.JSON `field:"sshParams"` // SSH默认参数
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
AutoRegister uint8 `field:"autoRegister"` // 是否开启自动注册
|
AutoRegister uint8 `field:"autoRegister"` // 是否开启自动注册
|
||||||
UniqueId string `field:"uniqueId"` // 唯一ID
|
UniqueId string `field:"uniqueId"` // 唯一ID
|
||||||
@@ -30,45 +31,54 @@ type NodeCluster struct {
|
|||||||
SystemServices dbs.JSON `field:"systemServices"` // 系统服务设置
|
SystemServices dbs.JSON `field:"systemServices"` // 系统服务设置
|
||||||
TimeZone string `field:"timeZone"` // 时区
|
TimeZone string `field:"timeZone"` // 时区
|
||||||
NodeMaxThreads uint32 `field:"nodeMaxThreads"` // 节点最大线程数
|
NodeMaxThreads uint32 `field:"nodeMaxThreads"` // 节点最大线程数
|
||||||
DdosProtection dbs.JSON `field:"ddosProtection"` // DDOS端口
|
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
|
||||||
AutoOpenPorts uint8 `field:"autoOpenPorts"` // 是否自动尝试开放端口
|
AutoOpenPorts uint8 `field:"autoOpenPorts"` // 是否自动尝试开放端口
|
||||||
IsPinned bool `field:"isPinned"` // 是否置顶
|
IsPinned bool `field:"isPinned"` // 是否置顶
|
||||||
Webp dbs.JSON `field:"webp"` // WebP设置
|
Webp dbs.JSON `field:"webp"` // WebP设置
|
||||||
Uam dbs.JSON `field:"uam"` // UAM设置
|
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 {
|
type NodeClusterOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId any // 用户ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Name interface{} // 名称
|
Name any // 名称
|
||||||
UseAllAPINodes interface{} // 是否使用所有API节点
|
UseAllAPINodes any // 是否使用所有API节点
|
||||||
ApiNodes interface{} // 使用的API节点
|
ApiNodes any // 使用的API节点
|
||||||
InstallDir interface{} // 安装目录
|
InstallDir any // 安装目录
|
||||||
Order interface{} // 排序
|
Order any // 排序
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
GrantId interface{} // 默认认证方式
|
GrantId any // 默认认证方式
|
||||||
State interface{} // 状态
|
SshParams any // SSH默认参数
|
||||||
AutoRegister interface{} // 是否开启自动注册
|
State any // 状态
|
||||||
UniqueId interface{} // 唯一ID
|
AutoRegister any // 是否开启自动注册
|
||||||
Secret interface{} // 密钥
|
UniqueId any // 唯一ID
|
||||||
HealthCheck interface{} // 健康检查
|
Secret any // 密钥
|
||||||
DnsName interface{} // DNS名称
|
HealthCheck any // 健康检查
|
||||||
DnsDomainId interface{} // 域名ID
|
DnsName any // DNS名称
|
||||||
Dns interface{} // DNS配置
|
DnsDomainId any // 域名ID
|
||||||
Toa interface{} // TOA配置
|
Dns any // DNS配置
|
||||||
CachePolicyId interface{} // 缓存策略ID
|
Toa any // TOA配置
|
||||||
HttpFirewallPolicyId interface{} // WAF策略ID
|
CachePolicyId any // 缓存策略ID
|
||||||
AccessLog interface{} // 访问日志设置
|
HttpFirewallPolicyId any // WAF策略ID
|
||||||
SystemServices interface{} // 系统服务设置
|
AccessLog any // 访问日志设置
|
||||||
TimeZone interface{} // 时区
|
SystemServices any // 系统服务设置
|
||||||
NodeMaxThreads interface{} // 节点最大线程数
|
TimeZone any // 时区
|
||||||
DdosProtection interface{} // DDOS端口
|
NodeMaxThreads any // 节点最大线程数
|
||||||
AutoOpenPorts interface{} // 是否自动尝试开放端口
|
DdosProtection any // DDoS防护设置
|
||||||
IsPinned interface{} // 是否置顶
|
AutoOpenPorts any // 是否自动尝试开放端口
|
||||||
Webp interface{} // WebP设置
|
IsPinned any // 是否置顶
|
||||||
Uam interface{} // UAM设置
|
Webp any // WebP设置
|
||||||
|
Uam any // UAM设置
|
||||||
|
Clock any // 时钟配置
|
||||||
|
GlobalServerConfig any // 全局服务配置
|
||||||
|
AutoRemoteStart any // 自动远程启动
|
||||||
|
AutoInstallNftables any // 自动安装nftables
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeClusterOperator() *NodeClusterOperator {
|
func NewNodeClusterOperator() *NodeClusterOperator {
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
"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/serverconfigs/ddosconfigs"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,15 +13,9 @@ import (
|
|||||||
func (this *NodeCluster) DecodeDNSConfig() (*dnsconfigs.ClusterDNSConfig, error) {
|
func (this *NodeCluster) DecodeDNSConfig() (*dnsconfigs.ClusterDNSConfig, error) {
|
||||||
if len(this.Dns) == 0 {
|
if len(this.Dns) == 0 {
|
||||||
// 一定要返回一个默认的值,防止产生nil
|
// 一定要返回一个默认的值,防止产生nil
|
||||||
return &dnsconfigs.ClusterDNSConfig{
|
return dnsconfigs.DefaultClusterDNSConfig(), nil
|
||||||
NodesAutoSync: false,
|
|
||||||
ServersAutoSync: false,
|
|
||||||
CNameAsDomain: true,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
|
||||||
CNameAsDomain: true,
|
|
||||||
}
|
}
|
||||||
|
var dnsConfig = dnsconfigs.DefaultClusterDNSConfig()
|
||||||
err := json.Unmarshal(this.Dns, &dnsConfig)
|
err := json.Unmarshal(this.Dns, &dnsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -48,3 +45,27 @@ func (this *NodeCluster) HasDDoSProtection() bool {
|
|||||||
}
|
}
|
||||||
return false
|
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"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/sizes"
|
"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/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
@@ -35,8 +36,6 @@ const (
|
|||||||
NodeStateDisabled = 0 // 已禁用
|
NodeStateDisabled = 0 // 已禁用
|
||||||
)
|
)
|
||||||
|
|
||||||
var nodeIdCacheMap = map[string]int64{} // uniqueId => nodeId
|
|
||||||
|
|
||||||
type NodeDAO dbs.DAO
|
type NodeDAO dbs.DAO
|
||||||
|
|
||||||
func NewNodeDAO() *NodeDAO {
|
func NewNodeDAO() *NodeDAO {
|
||||||
@@ -77,9 +76,7 @@ func (this *NodeDAO) DisableNode(tx *dbs.Tx, nodeId int64) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(uniqueId) > 0 {
|
if len(uniqueId) > 0 {
|
||||||
SharedCacheLocker.Lock()
|
ttlcache.SharedCache.Delete("nodeId@uniqueId@" + uniqueId)
|
||||||
delete(nodeIdCacheMap, uniqueId)
|
|
||||||
SharedCacheLocker.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
@@ -140,18 +137,9 @@ func (this *NodeDAO) FindNodeName(tx *dbs.Tx, id int64) (string, error) {
|
|||||||
|
|
||||||
// CreateNode 创建节点
|
// CreateNode 创建节点
|
||||||
func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterId int64, groupId int64, regionId int64) (nodeId int64, err error) {
|
func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterId int64, groupId int64, regionId int64) (nodeId int64, err error) {
|
||||||
// 检查节点数量
|
err = this.CheckNodesLimit(tx)
|
||||||
if teaconst.MaxNodes > 0 {
|
if err != nil {
|
||||||
count, err := this.Query(tx).
|
return
|
||||||
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) + ",请购买更多配额")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uniqueId, err := this.GenUniqueId(tx)
|
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.GroupId = groupId
|
||||||
op.RegionId = regionId
|
op.RegionId = regionId
|
||||||
op.IsOn = 1
|
op.IsOn = 1
|
||||||
|
op.EnableIPLists = 1
|
||||||
op.State = NodeStateEnabled
|
op.State = NodeStateEnabled
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -199,7 +188,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNode 修改节点
|
// UpdateNode 修改节点
|
||||||
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, isOn bool, level int) 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 {
|
if nodeId <= 0 {
|
||||||
return errors.New("invalid nodeId")
|
return errors.New("invalid nodeId")
|
||||||
}
|
}
|
||||||
@@ -248,8 +237,19 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId
|
|||||||
|
|
||||||
if teaconst.IsPlus {
|
if teaconst.IsPlus {
|
||||||
op.Level = level
|
op.Level = level
|
||||||
|
|
||||||
|
if lnAddrs == nil {
|
||||||
|
lnAddrs = []string{}
|
||||||
|
}
|
||||||
|
lnAddrsJSON, err := json.Marshal(lnAddrs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.LnAddrs = lnAddrsJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
|
op.EnableIPLists = enableIPLists
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -605,7 +605,7 @@ func (this *NodeDAO) FindEnabledNodesWithGroupIdAndLevel(tx *dbs.Tx, groupId int
|
|||||||
}
|
}
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Result("id", "clusterId", "secondaryClusterIds", "uniqueId", "secret").
|
Result("id", "clusterId", "secondaryClusterIds", "uniqueId", "secret", "lnAddrs").
|
||||||
Attr("isOn", true).
|
Attr("isOn", true).
|
||||||
Attr("groupId", groupId).
|
Attr("groupId", groupId).
|
||||||
Attr("level", level).
|
Attr("level", level).
|
||||||
@@ -949,17 +949,18 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
var config = &nodeconfigs.NodeConfig{
|
var config = &nodeconfigs.NodeConfig{
|
||||||
Id: int64(node.Id),
|
Id: int64(node.Id),
|
||||||
NodeId: node.UniqueId,
|
NodeId: node.UniqueId,
|
||||||
Secret: node.Secret,
|
Secret: node.Secret,
|
||||||
IsOn: node.IsOn,
|
IsOn: node.IsOn,
|
||||||
Servers: nil,
|
Servers: nil,
|
||||||
Version: int64(node.Version),
|
Version: int64(node.Version),
|
||||||
Name: node.Name,
|
Name: node.Name,
|
||||||
MaxCPU: types.Int32(node.MaxCPU),
|
MaxCPU: types.Int32(node.MaxCPU),
|
||||||
RegionId: int64(node.RegionId),
|
RegionId: int64(node.RegionId),
|
||||||
Level: types.Int32(node.Level),
|
Level: types.Int32(node.Level),
|
||||||
GroupId: int64(node.GroupId),
|
GroupId: int64(node.GroupId),
|
||||||
|
EnableIPLists: node.EnableIPLists,
|
||||||
}
|
}
|
||||||
|
|
||||||
// API节点IP
|
// API节点IP
|
||||||
@@ -976,7 +977,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true)
|
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1076,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连接数
|
// 最大线程数、TCP连接数
|
||||||
if clusterIndex == 0 {
|
if clusterIndex == 0 {
|
||||||
config.MaxThreads = int(nodeCluster.NodeMaxThreads)
|
config.MaxThreads = int(nodeCluster.NodeMaxThreads)
|
||||||
@@ -1103,6 +1114,11 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
|||||||
config.UAMPolicies[clusterId] = uamPolicy
|
config.UAMPolicies[clusterId] = uamPolicy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 自动安装nftables
|
||||||
|
if clusterIndex == 0 {
|
||||||
|
config.AutoInstallNftables = nodeCluster.AutoInstallNftables
|
||||||
|
}
|
||||||
|
|
||||||
clusterIndex++
|
clusterIndex++
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1264,36 +1280,39 @@ func (this *NodeDAO) UpdateNodeConnectedAPINodes(tx *dbs.Tx, nodeId int64, apiNo
|
|||||||
|
|
||||||
// FindEnabledNodeIdWithUniqueId 根据UniqueId获取ID
|
// FindEnabledNodeIdWithUniqueId 根据UniqueId获取ID
|
||||||
func (this *NodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
func (this *NodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||||
return this.Query(tx).
|
var cacheKey = "nodeId@uniqueId@" + uniqueId
|
||||||
State(NodeStateEnabled).
|
var item = ttlcache.SharedCache.Read(cacheKey)
|
||||||
Attr("uniqueId", uniqueId).
|
if item != nil {
|
||||||
ResultPk().
|
return types.Int64(item.Value), nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
SharedCacheLocker.RUnlock()
|
|
||||||
nodeId, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Attr("uniqueId", uniqueId).
|
Attr("uniqueId", uniqueId).
|
||||||
ResultPk().
|
Result("id", "clusterId").
|
||||||
FindInt64Col(0)
|
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 {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if nodeId > 0 {
|
if !isOn {
|
||||||
SharedCacheLocker.Lock()
|
return 0, nil
|
||||||
nodeIdCacheMap[uniqueId] = nodeId
|
|
||||||
SharedCacheLocker.Unlock()
|
|
||||||
}
|
}
|
||||||
return nodeId, nil
|
|
||||||
|
ttlcache.SharedCache.Write(cacheKey, int64(node.Id), time.Now().Unix()+60)
|
||||||
|
|
||||||
|
return int64(node.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledNodesWithGrantId 计算使用某个认证的节点数量
|
// CountAllEnabledNodesWithGrantId 计算使用某个认证的节点数量
|
||||||
@@ -1402,8 +1421,51 @@ func (this *NodeDAO) CountAllEnabledNodesWithRegionId(tx *dbs.Tx, regionId int64
|
|||||||
Count()
|
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信息
|
// FindAllEnabledNodesDNSWithClusterId 获取一个集群的节点DNS信息
|
||||||
func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool) (result []*Node, err error) {
|
func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, includingLnNodes bool) (result []*Node, err error) {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -1415,6 +1477,9 @@ func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId i
|
|||||||
} else {
|
} else {
|
||||||
query.Attr("clusterId", clusterId)
|
query.Attr("clusterId", clusterId)
|
||||||
}
|
}
|
||||||
|
if !includingLnNodes {
|
||||||
|
query.Lte("level", 1)
|
||||||
|
}
|
||||||
_, err = query.
|
_, err = query.
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Attr("isOn", true).
|
Attr("isOn", true).
|
||||||
@@ -1878,14 +1943,18 @@ func (this *NodeDAO) FindParentNodeConfigs(tx *dbs.Tx, nodeId int64, groupId int
|
|||||||
|
|
||||||
if len(parentNodes) > 0 {
|
if len(parentNodes) > 0 {
|
||||||
for _, node := range parentNodes {
|
for _, node := range parentNodes {
|
||||||
addrs, err := SharedNodeIPAddressDAO.FindNodeAccessAndUpIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
|
// 是否有Ln地址
|
||||||
if err != nil {
|
var addrStrings = node.DecodeLnAddrs()
|
||||||
return nil, err
|
if len(addrStrings) == 0 {
|
||||||
}
|
// 如果没有就取节点的可访问地址
|
||||||
var addrStrings = []string{}
|
addrs, err := SharedNodeIPAddressDAO.FindNodeAccessAndUpIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
|
||||||
for _, addr := range addrs {
|
if err != nil {
|
||||||
if addr.IsOn {
|
return nil, err
|
||||||
addrStrings = append(addrStrings, addr.DNSIP())
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if addr.IsOn {
|
||||||
|
addrStrings = append(addrStrings, addr.DNSIP())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1924,7 +1993,7 @@ func (this *NodeDAO) FindNodeDDoSProtection(tx *dbs.Tx, nodeId int64) (*ddosconf
|
|||||||
return one.(*Node).DecodeDDoSProtection(), nil
|
return one.(*Node).DecodeDDoSProtection(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNodeDDoSProtection 设置集群的DDOS设置
|
// UpdateNodeDDoSProtection 设置集群的DDoS设置
|
||||||
func (this *NodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
|
func (this *NodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
|
||||||
if nodeId <= 0 {
|
if nodeId <= 0 {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
@@ -1953,7 +2022,7 @@ func (this *NodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProt
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if clusterId > 0 {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1965,7 +2034,7 @@ func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1981,7 +2050,7 @@ func (this *NodeDAO) NotifyLevelUpdate(tx *dbs.Tx, nodeId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, clusterId := range clusterIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -43,6 +44,8 @@ func init() {
|
|||||||
|
|
||||||
// CreateLog 创建日志
|
// CreateLog 创建日志
|
||||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64, logType string, paramsJSON []byte) error {
|
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64, logType string, paramsJSON []byte) error {
|
||||||
|
description = utils.LimitString(description, 1000)
|
||||||
|
|
||||||
// 修复以前同样的日志
|
// 修复以前同样的日志
|
||||||
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
|
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
|
||||||
err := this.Query(tx).
|
err := this.Query(tx).
|
||||||
@@ -339,8 +342,8 @@ func (this *NodeLogDAO) CountAllUnreadNodeLogs(tx *dbs.Tx) (int64, error) {
|
|||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNodeLogsRead 设置日志为已读
|
// UpdateNodeLogIdsRead 设置一组日志为已读
|
||||||
func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, nodeLogIds []int64) error {
|
func (this *NodeLogDAO) UpdateNodeLogIdsRead(tx *dbs.Tx, nodeLogIds []int64) error {
|
||||||
for _, logId := range nodeLogIds {
|
for _, logId := range nodeLogIds {
|
||||||
err := this.Query(tx).
|
err := this.Query(tx).
|
||||||
Pk(logId).
|
Pk(logId).
|
||||||
@@ -353,6 +356,16 @@ func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, nodeLogIds []int64) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateNodeLogsRead 设置节点日志为已读
|
||||||
|
func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
|
Attr("nodeId", nodeId).
|
||||||
|
Attr("isRead", false).
|
||||||
|
Set("isRead", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateAllNodeLogsRead 设置所有日志为已读
|
// UpdateAllNodeLogsRead 设置所有日志为已读
|
||||||
func (this *NodeLogDAO) UpdateAllNodeLogsRead(tx *dbs.Tx) error {
|
func (this *NodeLogDAO) UpdateAllNodeLogsRead(tx *dbs.Tx) error {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -378,6 +391,13 @@ func (this *NodeLogDAO) DeleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.N
|
|||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 执行钩子
|
||||||
|
err := this.deleteNodeLogsWithCluster(tx, role, clusterId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var query = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("role", role)
|
Attr("role", role)
|
||||||
|
|
||||||
@@ -385,13 +405,10 @@ func (this *NodeLogDAO) DeleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.N
|
|||||||
case nodeconfigs.NodeRoleNode:
|
case nodeconfigs.NodeRoleNode:
|
||||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE clusterId=:clusterId)")
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE clusterId=:clusterId)")
|
||||||
query.Param("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:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := query.Delete()
|
_, err = query.Delete()
|
||||||
return err
|
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
|
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
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ type Node struct {
|
|||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
Level uint8 `field:"level"` // 级别
|
Level uint8 `field:"level"` // 级别
|
||||||
|
LnAddrs dbs.JSON `field:"lnAddrs"` // Ln级别访问地址
|
||||||
IsOn bool `field:"isOn"` // 是否启用
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
IsUp bool `field:"isUp"` // 是否在线
|
IsUp bool `field:"isUp"` // 是否在线
|
||||||
CountUp uint32 `field:"countUp"` // 连续在线次数
|
CountUp uint32 `field:"countUp"` // 连续在线次数
|
||||||
@@ -39,44 +40,47 @@ type Node struct {
|
|||||||
MaxCacheMemoryCapacity dbs.JSON `field:"maxCacheMemoryCapacity"` // 内存缓存容量
|
MaxCacheMemoryCapacity dbs.JSON `field:"maxCacheMemoryCapacity"` // 内存缓存容量
|
||||||
CacheDiskDir string `field:"cacheDiskDir"` // 缓存目录
|
CacheDiskDir string `field:"cacheDiskDir"` // 缓存目录
|
||||||
DnsResolver dbs.JSON `field:"dnsResolver"` // DNS解析器
|
DnsResolver dbs.JSON `field:"dnsResolver"` // DNS解析器
|
||||||
|
EnableIPLists bool `field:"enableIPLists"` // 启用IP名单
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeOperator struct {
|
type NodeOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId any // 用户ID
|
||||||
Level interface{} // 级别
|
Level any // 级别
|
||||||
IsOn interface{} // 是否启用
|
LnAddrs any // Ln级别访问地址
|
||||||
IsUp interface{} // 是否在线
|
IsOn any // 是否启用
|
||||||
CountUp interface{} // 连续在线次数
|
IsUp any // 是否在线
|
||||||
CountDown interface{} // 连续下线次数
|
CountUp any // 连续在线次数
|
||||||
IsActive interface{} // 是否活跃
|
CountDown any // 连续下线次数
|
||||||
InactiveNotifiedAt interface{} // 离线通知时间
|
IsActive any // 是否活跃
|
||||||
UniqueId interface{} // 节点ID
|
InactiveNotifiedAt any // 离线通知时间
|
||||||
Secret interface{} // 密钥
|
UniqueId any // 节点ID
|
||||||
Name interface{} // 节点名
|
Secret any // 密钥
|
||||||
Code interface{} // 代号
|
Name any // 节点名
|
||||||
ClusterId interface{} // 主集群ID
|
Code any // 代号
|
||||||
SecondaryClusterIds interface{} // 从集群ID
|
ClusterId any // 主集群ID
|
||||||
RegionId interface{} // 区域ID
|
SecondaryClusterIds any // 从集群ID
|
||||||
GroupId interface{} // 分组ID
|
RegionId any // 区域ID
|
||||||
CreatedAt interface{} // 创建时间
|
GroupId any // 分组ID
|
||||||
Status interface{} // 最新的状态
|
CreatedAt any // 创建时间
|
||||||
Version interface{} // 当前版本号
|
Status any // 最新的状态
|
||||||
LatestVersion interface{} // 最后版本号
|
Version any // 当前版本号
|
||||||
InstallDir interface{} // 安装目录
|
LatestVersion any // 最后版本号
|
||||||
IsInstalled interface{} // 是否已安装
|
InstallDir any // 安装目录
|
||||||
InstallStatus interface{} // 安装状态
|
IsInstalled any // 是否已安装
|
||||||
State interface{} // 状态
|
InstallStatus any // 安装状态
|
||||||
ConnectedAPINodes interface{} // 当前连接的API节点
|
State any // 状态
|
||||||
MaxCPU interface{} // 可以使用的最多CPU
|
ConnectedAPINodes any // 当前连接的API节点
|
||||||
MaxThreads interface{} // 最大线程数
|
MaxCPU any // 可以使用的最多CPU
|
||||||
DdosProtection interface{} // DDOS配置
|
MaxThreads any // 最大线程数
|
||||||
DnsRoutes interface{} // DNS线路设置
|
DdosProtection any // DDOS配置
|
||||||
MaxCacheDiskCapacity interface{} // 硬盘缓存容量
|
DnsRoutes any // DNS线路设置
|
||||||
MaxCacheMemoryCapacity interface{} // 内存缓存容量
|
MaxCacheDiskCapacity any // 硬盘缓存容量
|
||||||
CacheDiskDir interface{} // 缓存目录
|
MaxCacheMemoryCapacity any // 内存缓存容量
|
||||||
DnsResolver interface{} // DNS解析器
|
CacheDiskDir any // 缓存目录
|
||||||
|
DnsResolver any // DNS解析器
|
||||||
|
EnableIPLists any // 启用IP名单
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeOperator() *NodeOperator {
|
func NewNodeOperator() *NodeOperator {
|
||||||
|
|||||||
@@ -168,3 +168,16 @@ func (this *Node) DecodeDNSResolver() *nodeconfigs.DNSResolverConfig {
|
|||||||
}
|
}
|
||||||
return resolverConfig
|
return resolverConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Node) DecodeLnAddrs() []string {
|
||||||
|
if IsNull(this.LnAddrs) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.LnAddrs, &result)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,134 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
NodePriceItemStateEnabled = 1 // 已启用
|
|
||||||
NodePriceItemStateDisabled = 0 // 已禁用
|
|
||||||
|
|
||||||
NodePriceTypeTraffic = "traffic" // 价格类型之流量
|
|
||||||
)
|
|
||||||
|
|
||||||
type NodePriceItemDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewNodePriceItemDAO() *NodePriceItemDAO {
|
|
||||||
return dbs.NewDAO(&NodePriceItemDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNodePriceItems",
|
|
||||||
Model: new(NodePriceItem),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NodePriceItemDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNodePriceItemDAO *NodePriceItemDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNodePriceItemDAO = NewNodePriceItemDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableNodePriceItem 启用条目
|
|
||||||
func (this *NodePriceItemDAO) EnableNodePriceItem(tx *dbs.Tx, id int64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", NodePriceItemStateEnabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableNodePriceItem 禁用条目
|
|
||||||
func (this *NodePriceItemDAO) DisableNodePriceItem(tx *dbs.Tx, id int64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", NodePriceItemStateDisabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledNodePriceItem 查找启用中的条目
|
|
||||||
func (this *NodePriceItemDAO) FindEnabledNodePriceItem(tx *dbs.Tx, id int64) (*NodePriceItem, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", NodePriceItemStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*NodePriceItem), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindNodePriceItemName 根据主键查找名称
|
|
||||||
func (this *NodePriceItemDAO) FindNodePriceItemName(tx *dbs.Tx, id int64) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateItem 创建价格
|
|
||||||
func (this *NodePriceItemDAO) CreateItem(tx *dbs.Tx, name string, itemType string, bitsFrom, bitsTo int64) (int64, error) {
|
|
||||||
var op = NewNodePriceItemOperator()
|
|
||||||
op.Name = name
|
|
||||||
op.Type = itemType
|
|
||||||
op.BitsFrom = bitsFrom
|
|
||||||
op.BitsTo = bitsTo
|
|
||||||
op.IsOn = true
|
|
||||||
op.State = NodePriceItemStateEnabled
|
|
||||||
return this.SaveInt64(tx, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateItem 修改价格
|
|
||||||
func (this *NodePriceItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, bitsFrom, bitsTo int64) error {
|
|
||||||
if itemId <= 0 {
|
|
||||||
return errors.New("invalid itemId")
|
|
||||||
}
|
|
||||||
var op = NewNodePriceItemOperator()
|
|
||||||
op.Id = itemId
|
|
||||||
op.Name = name
|
|
||||||
op.BitsFrom = bitsFrom
|
|
||||||
op.BitsTo = bitsTo
|
|
||||||
return this.Save(tx, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindAllEnabledRegionPrices 列出某个区域的所有价格
|
|
||||||
func (this *NodePriceItemDAO) FindAllEnabledRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Attr("type", priceType).
|
|
||||||
State(NodePriceItemStateEnabled).
|
|
||||||
Asc("bitsFrom").
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindAllEnabledAndOnRegionPrices 列出某个区域的所有启用的价格
|
|
||||||
func (this *NodePriceItemDAO) FindAllEnabledAndOnRegionPrices(tx *dbs.Tx, priceType string) (result []*NodePriceItem, err error) {
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Attr("type", priceType).
|
|
||||||
State(NodePriceItemStateEnabled).
|
|
||||||
Attr("isOn", true).
|
|
||||||
Asc("bitsFrom").
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// SearchItemsWithBytes 根据字节查找付费项目
|
|
||||||
func (this *NodePriceItemDAO) SearchItemsWithBytes(items []*NodePriceItem, bytes int64) int64 {
|
|
||||||
bytes *= 8
|
|
||||||
|
|
||||||
for _, item := range items {
|
|
||||||
if bytes >= int64(item.BitsFrom) && (bytes < int64(item.BitsTo) || item.BitsTo == 0) {
|
|
||||||
return int64(item.Id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
)
|
|
||||||
@@ -120,8 +120,8 @@ func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*Nod
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAllEnabledAndOnRegions 列出所有启用的区域
|
// FindAllAvailableRegions 列出所有启用的区域
|
||||||
func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
func (this *NodeRegionDAO) FindAllAvailableRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeRegionStateEnabled).
|
State(NodeRegionStateEnabled).
|
||||||
Attr("isOn", true).
|
Attr("isOn", true).
|
||||||
@@ -132,6 +132,25 @@ func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*Nod
|
|||||||
return
|
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 排序
|
// UpdateRegionOrders 排序
|
||||||
func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) error {
|
func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) error {
|
||||||
order := len(regionIds)
|
order := len(regionIds)
|
||||||
|
|||||||
@@ -11,20 +11,20 @@ type NodeRegion struct {
|
|||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
Prices dbs.JSON `field:"prices"` // 价格
|
Prices dbs.JSON `field:"prices"` // 流量价格
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeRegionOperator struct {
|
type NodeRegionOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Name interface{} // 名称
|
Name any // 名称
|
||||||
Description interface{} // 描述
|
Description any // 描述
|
||||||
Order interface{} // 排序
|
Order any // 排序
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
Prices interface{} // 价格
|
Prices any // 流量价格
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeRegionOperator() *NodeRegionOperator {
|
func NewNodeRegionOperator() *NodeRegionOperator {
|
||||||
|
|||||||
@@ -14,20 +14,25 @@ import (
|
|||||||
type NodeTaskType = string
|
type NodeTaskType = string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
|
// CDN相关
|
||||||
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
|
|
||||||
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
|
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
|
||||||
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
|
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
|
||||||
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged"
|
NodeTaskTypeGlobalServerConfigChanged NodeTaskType = "globalServerConfigChanged" // 全局服务设置变化
|
||||||
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged"
|
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged" // IP条目变更
|
||||||
|
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged" // 节点版本变化
|
||||||
|
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged" // 脚本配置变化
|
||||||
|
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged" // 节点级别变化
|
||||||
|
NodeTaskTypeUserServersStateChanged NodeTaskType = "userServersStateChanged" // 用户服务状态变化
|
||||||
|
|
||||||
// NS相关
|
// NS相关
|
||||||
|
|
||||||
NSNodeTaskTypeConfigChanged NodeTaskType = "nsConfigChanged"
|
NSNodeTaskTypeConfigChanged NodeTaskType = "nsConfigChanged"
|
||||||
NSNodeTaskTypeDomainChanged NodeTaskType = "nsDomainChanged"
|
NSNodeTaskTypeDomainChanged NodeTaskType = "nsDomainChanged"
|
||||||
NSNodeTaskTypeRecordChanged NodeTaskType = "nsRecordChanged"
|
NSNodeTaskTypeRecordChanged NodeTaskType = "nsRecordChanged"
|
||||||
NSNodeTaskTypeRouteChanged NodeTaskType = "nsRouteChanged"
|
NSNodeTaskTypeRouteChanged NodeTaskType = "nsRouteChanged"
|
||||||
NSNodeTaskTypeKeyChanged NodeTaskType = "nsKeyChanged"
|
NSNodeTaskTypeKeyChanged NodeTaskType = "nsKeyChanged"
|
||||||
|
NSNodeTaskTypeDDosProtectionChanged NodeTaskType = "nsDDoSProtectionChanged" // 节点DDoS配置变更
|
||||||
)
|
)
|
||||||
|
|
||||||
type NodeTaskDAO dbs.DAO
|
type NodeTaskDAO dbs.DAO
|
||||||
@@ -52,17 +57,25 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateNodeTask 创建单个节点任务
|
// 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 {
|
if clusterId <= 0 || nodeId <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var uniqueId = role + "@" + types.String(nodeId) + "@node@" + types.String(serverId) + "@" + taskType
|
var uniqueId = role + "@" + types.String(nodeId) + "@node@" + types.String(serverId) + "@" + taskType
|
||||||
|
|
||||||
|
// 用户信息
|
||||||
|
// 没有直接加入到 uniqueId 中,是为了兼容以前的字段值
|
||||||
|
if userId > 0 {
|
||||||
|
uniqueId += "@" + types.String(userId)
|
||||||
|
}
|
||||||
|
|
||||||
var updatedAt = time.Now().Unix()
|
var updatedAt = time.Now().Unix()
|
||||||
_, _, err := this.Query(tx).
|
_, _, err := this.Query(tx).
|
||||||
InsertOrUpdate(maps.Map{
|
InsertOrUpdate(maps.Map{
|
||||||
"role": role,
|
"role": role,
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
"nodeId": nodeId,
|
"nodeId": nodeId,
|
||||||
|
"userId": userId,
|
||||||
"serverId": serverId,
|
"serverId": serverId,
|
||||||
"type": taskType,
|
"type": taskType,
|
||||||
"uniqueId": uniqueId,
|
"uniqueId": uniqueId,
|
||||||
@@ -85,17 +98,25 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateClusterTask 创建集群任务
|
// 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 {
|
if clusterId <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var uniqueId = role + "@" + types.String(clusterId) + "@" + types.String(serverId) + "@cluster@" + types.String(serverId) + "@" + taskType
|
var uniqueId = role + "@" + types.String(clusterId) + "@" + types.String(serverId) + "@cluster@" + taskType
|
||||||
|
|
||||||
|
// 用户信息
|
||||||
|
// 没有直接加入到 uniqueId 中,是为了兼容以前的字段值
|
||||||
|
if userId > 0 {
|
||||||
|
uniqueId += "@" + types.String(userId)
|
||||||
|
}
|
||||||
|
|
||||||
var updatedAt = time.Now().Unix()
|
var updatedAt = time.Now().Unix()
|
||||||
_, _, err := this.Query(tx).
|
_, _, err := this.Query(tx).
|
||||||
InsertOrUpdate(maps.Map{
|
InsertOrUpdate(maps.Map{
|
||||||
"role": role,
|
"role": role,
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
|
"userId": userId,
|
||||||
"serverId": serverId,
|
"serverId": serverId,
|
||||||
"nodeId": 0,
|
"nodeId": 0,
|
||||||
"type": taskType,
|
"type": taskType,
|
||||||
@@ -119,7 +140,7 @@ func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId in
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ExtractNodeClusterTask 分解边缘节点集群任务
|
// 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)
|
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, clusterId, true, configutils.BoolStateYes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -138,7 +159,7 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, ser
|
|||||||
|
|
||||||
var version = time.Now().UnixNano()
|
var version = time.Now().UnixNano()
|
||||||
for _, nodeId := range nodeIds {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -157,45 +178,6 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, ser
|
|||||||
return nil
|
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 分解所有集群任务
|
// ExtractAllClusterTasks 分解所有集群任务
|
||||||
func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx, role string) error {
|
func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx, role string) error {
|
||||||
ones, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
@@ -206,11 +188,11 @@ func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx, role string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, one := range ones {
|
for _, one := range ones {
|
||||||
clusterId := int64(one.(*NodeTask).ClusterId)
|
var clusterId = int64(one.(*NodeTask).ClusterId)
|
||||||
switch role {
|
switch role {
|
||||||
case nodeconfigs.NodeRoleNode:
|
case nodeconfigs.NodeRoleNode:
|
||||||
var nodeTask = one.(*NodeTask)
|
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 {
|
if err != nil {
|
||||||
return err
|
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()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -22,7 +22,7 @@ func TestNodeTaskDAO_CreateClusterTask(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ func TestNodeTaskDAO_ExtractClusterTask(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := SharedNodeTaskDAO.ExtractNodeClusterTask(tx, 1, 0, NodeTaskTypeConfigChanged)
|
err := SharedNodeTaskDAO.ExtractNodeClusterTask(tx, 1, 0, 0, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ type NodeTask struct {
|
|||||||
Role string `field:"role"` // 节点角色
|
Role string `field:"role"` // 节点角色
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群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"` // 任务类型
|
Type string `field:"type"` // 任务类型
|
||||||
UniqueId string `field:"uniqueId"` // 唯一ID:nodeId@type
|
UniqueId string `field:"uniqueId"` // 唯一ID:nodeId@type
|
||||||
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
||||||
@@ -18,19 +19,20 @@ type NodeTask struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type NodeTaskOperator struct {
|
type NodeTaskOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
Role interface{} // 节点角色
|
Role any // 节点角色
|
||||||
NodeId interface{} // 节点ID
|
NodeId any // 节点ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId any // 集群ID
|
||||||
ServerId interface{} // 服务ID
|
ServerId any // 服务ID
|
||||||
Type interface{} // 任务类型
|
UserId any // 用户ID
|
||||||
UniqueId interface{} // 唯一ID:nodeId@type
|
Type any // 任务类型
|
||||||
UpdatedAt interface{} // 修改时间
|
UniqueId any // 唯一ID:nodeId@type
|
||||||
IsDone interface{} // 是否已完成
|
UpdatedAt any // 修改时间
|
||||||
IsOk interface{} // 是否已完成
|
IsDone any // 是否已完成
|
||||||
Error interface{} // 错误信息
|
IsOk any // 是否已完成
|
||||||
IsNotified interface{} // 是否已通知更新
|
Error any // 错误信息
|
||||||
Version interface{} // 版本
|
IsNotified any // 是否已通知更新
|
||||||
|
Version any // 版本
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeTaskOperator() *NodeTaskOperator {
|
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,10 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -81,68 +78,6 @@ func (this *NSClusterDAO) FindEnabledNSClusterName(tx *dbs.Tx, id int64) (string
|
|||||||
FindStringCol("")
|
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 计算可用集群数量
|
// CountAllEnabledClusters 计算可用集群数量
|
||||||
func (this *NSClusterDAO) CountAllEnabledClusters(tx *dbs.Tx) (int64, error) {
|
func (this *NSClusterDAO) CountAllEnabledClusters(tx *dbs.Tx) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -188,23 +123,6 @@ func (this *NSClusterDAO) FindAllEnabledClusterIds(tx *dbs.Tx) ([]int64, error)
|
|||||||
return result, nil
|
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
|
// FindClusterGrantId 查找集群的认证ID
|
||||||
func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64, error) {
|
func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -213,102 +131,6 @@ func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64
|
|||||||
FindInt64Col(0)
|
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集群数量
|
// CountAllClustersWithSSLPolicyIds 计算使用SSL策略的所有NS集群数量
|
||||||
func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
|
func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
|
||||||
if len(sslPolicyIds) == 0 {
|
if len(sslPolicyIds) == 0 {
|
||||||
@@ -327,5 +149,5 @@ func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicy
|
|||||||
|
|
||||||
// NotifyUpdate 通知更改
|
// NotifyUpdate 通知更改
|
||||||
func (this *NSClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
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,31 +4,47 @@ import "github.com/iwind/TeaGo/dbs"
|
|||||||
|
|
||||||
// NSCluster 域名服务器集群
|
// NSCluster 域名服务器集群
|
||||||
type NSCluster struct {
|
type NSCluster struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
IsOn bool `field:"isOn"` // 是否启用
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 集群名
|
Name string `field:"name"` // 集群名
|
||||||
InstallDir string `field:"installDir"` // 安装目录
|
InstallDir string `field:"installDir"` // 安装目录
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
|
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
|
||||||
GrantId uint32 `field:"grantId"` // 授权ID
|
GrantId uint32 `field:"grantId"` // 授权ID
|
||||||
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
|
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
|
||||||
Tcp dbs.JSON `field:"tcp"` // TCP设置
|
Tcp dbs.JSON `field:"tcp"` // TCP设置
|
||||||
Tls dbs.JSON `field:"tls"` // TLS设置
|
Tls dbs.JSON `field:"tls"` // TLS设置
|
||||||
Udp dbs.JSON `field:"udp"` // UDP设置
|
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 {
|
type NSClusterOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Name interface{} // 集群名
|
Name any // 集群名
|
||||||
InstallDir interface{} // 安装目录
|
InstallDir any // 安装目录
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
AccessLog interface{} // 访问日志配置
|
AccessLog any // 访问日志配置
|
||||||
GrantId interface{} // 授权ID
|
GrantId any // 授权ID
|
||||||
Recursion interface{} // 递归DNS设置
|
Recursion any // 递归DNS设置
|
||||||
Tcp interface{} // TCP设置
|
Tcp any // TCP设置
|
||||||
Tls interface{} // TLS设置
|
Tls any // TLS设置
|
||||||
Udp interface{} // UDP设置
|
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 {
|
func NewNSClusterOperator() *NSClusterOperator {
|
||||||
|
|||||||
@@ -1 +1,30 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DecodeDDoSProtection 解析DDOS Protection设置
|
||||||
|
func (this *NSCluster) DecodeDDoSProtection() *ddosconfigs.ProtectionConfig {
|
||||||
|
if IsNull(this.DdosProtection) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = &ddosconfigs.ProtectionConfig{}
|
||||||
|
err := json.Unmarshal(this.DdosProtection, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("NSCluster.DecodeDDoSProtection", "decode failed: "+err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasDDoSProtection 检查是否有DDOS设置
|
||||||
|
func (this *NSCluster) HasDDoSProtection() bool {
|
||||||
|
var config = this.DecodeDDoSProtection()
|
||||||
|
if config != nil {
|
||||||
|
return config.IsOn()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,20 +1,15 @@
|
|||||||
|
//go:build !plus
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"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/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/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -93,110 +88,6 @@ func (this *NSNodeDAO) FindEnabledNSNodeName(tx *dbs.Tx, nodeId int64) (string,
|
|||||||
FindStringCol("")
|
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 计算单个集群中所有低于某个版本的节点数量
|
// CountAllLowerVersionNodesWithClusterId 计算单个集群中所有低于某个版本的节点数量
|
||||||
func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId int64, os string, arch string, version string) (int64, error) {
|
func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId int64, os string, arch string, version string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -212,73 +103,6 @@ func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, cluste
|
|||||||
Count()
|
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
|
// FindEnabledNodeIdWithUniqueId 根据唯一ID获取节点ID
|
||||||
func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -288,37 +112,6 @@ func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string
|
|||||||
FindInt64Col(0)
|
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
|
// GenUniqueId 生成唯一ID
|
||||||
func (this *NSNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
func (this *NSNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
||||||
for {
|
for {
|
||||||
@@ -347,7 +140,7 @@ func (this *NSNodeDAO) UpdateNodeIsInstalled(tx *dbs.Tx, nodeId int64, isInstall
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNodeStatus 更改节点状态
|
// UpdateNodeStatus 更改节点状态
|
||||||
func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error {
|
func (this *NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error {
|
||||||
if nodeStatus == nil {
|
if nodeStatus == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -375,118 +168,6 @@ func (this *NSNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (in
|
|||||||
Count()
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return config, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindNodeClusterId 获取节点的集群ID
|
// FindNodeClusterId 获取节点的集群ID
|
||||||
func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error) {
|
func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -495,140 +176,6 @@ func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error
|
|||||||
FindInt64Col(0)
|
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).
|
|
||||||
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).
|
|
||||||
UpdateQuickly()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||||
// TODO 先什么都不做
|
// TODO 先什么都不做
|
||||||
|
|||||||
@@ -4,41 +4,45 @@ import "github.com/iwind/TeaGo/dbs"
|
|||||||
|
|
||||||
// NSNode 域名服务器节点
|
// NSNode 域名服务器节点
|
||||||
type NSNode struct {
|
type NSNode struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
Name string `field:"name"` // 节点名称
|
Name string `field:"name"` // 节点名称
|
||||||
IsOn bool `field:"isOn"` // 是否启用
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
Status dbs.JSON `field:"status"` // 运行状态
|
Status dbs.JSON `field:"status"` // 运行状态
|
||||||
UniqueId string `field:"uniqueId"` // 节点ID
|
UniqueId string `field:"uniqueId"` // 节点ID
|
||||||
Secret string `field:"secret"` // 密钥
|
Secret string `field:"secret"` // 密钥
|
||||||
IsUp bool `field:"isUp"` // 是否运行
|
IsUp bool `field:"isUp"` // 是否运行
|
||||||
IsInstalled bool `field:"isInstalled"` // 是否已安装
|
IsInstalled bool `field:"isInstalled"` // 是否已安装
|
||||||
InstallStatus dbs.JSON `field:"installStatus"` // 安装状态
|
InstallStatus dbs.JSON `field:"installStatus"` // 安装状态
|
||||||
InstallDir string `field:"installDir"` // 安装目录
|
InstallDir string `field:"installDir"` // 安装目录
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
IsActive bool `field:"isActive"` // 是否活跃
|
IsActive bool `field:"isActive"` // 是否活跃
|
||||||
StatusIsNotified uint8 `field:"statusIsNotified"` // 活跃状态已经通知
|
StatusIsNotified uint8 `field:"statusIsNotified"` // 活跃状态已经通知
|
||||||
ConnectedAPINodes dbs.JSON `field:"connectedAPINodes"` // 当前连接的API节点
|
InactiveNotifiedAt uint64 `field:"inactiveNotifiedAt"` // 离线通知时间
|
||||||
|
ConnectedAPINodes dbs.JSON `field:"connectedAPINodes"` // 当前连接的API节点
|
||||||
|
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
|
||||||
}
|
}
|
||||||
|
|
||||||
type NSNodeOperator struct {
|
type NSNodeOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId any // 集群ID
|
||||||
Name interface{} // 节点名称
|
Name any // 节点名称
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Status interface{} // 运行状态
|
Status any // 运行状态
|
||||||
UniqueId interface{} // 节点ID
|
UniqueId any // 节点ID
|
||||||
Secret interface{} // 密钥
|
Secret any // 密钥
|
||||||
IsUp interface{} // 是否运行
|
IsUp any // 是否运行
|
||||||
IsInstalled interface{} // 是否已安装
|
IsInstalled any // 是否已安装
|
||||||
InstallStatus interface{} // 安装状态
|
InstallStatus any // 安装状态
|
||||||
InstallDir interface{} // 安装目录
|
InstallDir any // 安装目录
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
IsActive interface{} // 是否活跃
|
IsActive any // 是否活跃
|
||||||
StatusIsNotified interface{} // 活跃状态已经通知
|
StatusIsNotified any // 活跃状态已经通知
|
||||||
ConnectedAPINodes interface{} // 当前连接的API节点
|
InactiveNotifiedAt any // 离线通知时间
|
||||||
|
ConnectedAPINodes any // 当前连接的API节点
|
||||||
|
DdosProtection any // DDoS防护设置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNSNodeOperator() *NSNodeOperator {
|
func NewNSNodeOperator() *NSNodeOperator {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,3 +40,40 @@ func (this *NSNode) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
|
|||||||
}
|
}
|
||||||
return status, nil
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DecodeDDoSProtection 解析DDoS Protection设置
|
||||||
|
func (this *NSNode) DecodeDDoSProtection() *ddosconfigs.ProtectionConfig {
|
||||||
|
if IsNull(this.DdosProtection) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = &ddosconfigs.ProtectionConfig{}
|
||||||
|
err := json.Unmarshal(this.DdosProtection, &result)
|
||||||
|
if err != nil {
|
||||||
|
// ignore err
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasDDoSProtection 检查是否有DDOS设置
|
||||||
|
func (this *NSNode) HasDDoSProtection() bool {
|
||||||
|
var config = this.DecodeDDoSProtection()
|
||||||
|
if config != nil {
|
||||||
|
return !config.IsPriorEmpty()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeConnectedAPINodes 解析连接的API节点列表
|
||||||
|
func (this *NSNode) DecodeConnectedAPINodes() []int64 {
|
||||||
|
if IsNull(this.ConnectedAPINodes) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []int64{}
|
||||||
|
err := json.Unmarshal(this.ConnectedAPINodes, &result)
|
||||||
|
if err != nil {
|
||||||
|
// ignore err
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
//go:build !plus
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -79,129 +80,6 @@ func (this *PlanDAO) FindPlanName(tx *dbs.Tx, id int64) (string, error) {
|
|||||||
FindStringCol("")
|
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 计算套餐的数量
|
// CountAllEnabledPlans 计算套餐的数量
|
||||||
func (this *PlanDAO) CountAllEnabledPlans(tx *dbs.Tx) (int64, error) {
|
func (this *PlanDAO) CountAllEnabledPlans(tx *dbs.Tx) (int64, error) {
|
||||||
return this.Query(tx).
|
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
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -162,3 +162,19 @@ func (this *RegionTownDAO) FindSimilarTowns(towns []*RegionTown, townName string
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateTown 创建区县
|
||||||
|
func (this *RegionTownDAO) CreateTown(tx *dbs.Tx, cityId int64, townName string) (int64, error) {
|
||||||
|
var op = NewRegionTownOperator()
|
||||||
|
op.CityId = cityId
|
||||||
|
op.Name = townName
|
||||||
|
|
||||||
|
codes, err := json.Marshal([]string{townName})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Codes = codes
|
||||||
|
|
||||||
|
op.State = RegionTownStateEnabled
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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) {
|
func (this *ReportNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
State(ReportNodeStateEnabled).
|
State(ReportNodeStateEnabled).
|
||||||
|
Attr("isOn", true).
|
||||||
Where("status IS NOT NULL").
|
Where("status IS NOT NULL").
|
||||||
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
|
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
|
||||||
Param("version", utils.VersionToLong(version)).
|
Param("version", utils.VersionToLong(version)).
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -12,6 +15,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"math"
|
"math"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -82,28 +86,215 @@ func (this *ServerBandwidthStatDAO) UpdateServerBandwidth(tx *dbs.Tx, userId int
|
|||||||
func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, minute string) (int64, error) {
|
func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, minute string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Result("bytes").
|
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
|
Result("bytes").
|
||||||
Attr("day", day).
|
Attr("day", day).
|
||||||
Attr("timeAt", minute).
|
Attr("timeAt", minute).
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindHourlyBandwidthStats 按小时获取带宽峰值
|
||||||
|
func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverId int64, hours int32) (result []*pb.FindHourlyServerBandwidthStatsResponse_Stat, err error) {
|
||||||
|
if hours <= 0 {
|
||||||
|
hours = 24
|
||||||
|
}
|
||||||
|
|
||||||
|
var timestamp = time.Now().Unix() - int64(hours)*3600
|
||||||
|
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
Table(this.partialTable(serverId)).
|
||||||
|
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()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var m = map[string]*pb.FindHourlyServerBandwidthStatsResponse_Stat{}
|
||||||
|
for _, one := range ones {
|
||||||
|
var fullTime = one.GetString("fullTime")
|
||||||
|
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: bytes,
|
||||||
|
Bits: bytes * 8,
|
||||||
|
Day: day,
|
||||||
|
Hour: types.Int32(hour),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fullHours, err := utils.RangeHours(timeutil.FormatTime("YmdH", timestamp), timeutil.Format("YmdH"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, fullHour := range fullHours {
|
||||||
|
stat, ok := m[fullHour]
|
||||||
|
if ok {
|
||||||
|
result = append(result, stat)
|
||||||
|
} else {
|
||||||
|
result = append(result, &pb.FindHourlyServerBandwidthStatsResponse_Stat{
|
||||||
|
Bytes: 0,
|
||||||
|
Bits: 0,
|
||||||
|
Day: fullHour[:8],
|
||||||
|
Hour: types.Int32(fullHour[8:]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// FindDailyPeekBandwidthBytes 获取某天的带宽峰值
|
// FindDailyPeekBandwidthBytes 获取某天的带宽峰值
|
||||||
// day YYYYMMDD
|
// day YYYYMMDD
|
||||||
func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string) (int64, error) {
|
func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
|
Attr("serverId", serverId).
|
||||||
Attr("day", day).
|
Attr("day", day).
|
||||||
Result("MAX(bytes)").
|
Result("MAX(bytes)").
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindDailyBandwidthStats 按天获取带宽峰值
|
||||||
|
func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId int64, days int32) (result []*pb.FindDailyServerBandwidthStatsResponse_Stat, err error) {
|
||||||
|
if days <= 0 {
|
||||||
|
days = 14
|
||||||
|
}
|
||||||
|
|
||||||
|
var timestamp = time.Now().Unix() - int64(days)*86400
|
||||||
|
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
Table(this.partialTable(serverId)).
|
||||||
|
Result("MAX(bytes) AS bytes", "day").
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Gte("day", timeutil.FormatTime("Ymd", timestamp)).
|
||||||
|
Group("day").
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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: bytes,
|
||||||
|
Bits: bytes * 8,
|
||||||
|
Day: day,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allDays, err := utils.RangeDays(timeutil.FormatTime("Ymd", timestamp), timeutil.Format("Ymd"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, day := range allDays {
|
||||||
|
stat, ok := m[day]
|
||||||
|
if ok {
|
||||||
|
result = append(result, stat)
|
||||||
|
} else {
|
||||||
|
result = append(result, &pb.FindDailyServerBandwidthStatsResponse_Stat{
|
||||||
|
Bytes: 0,
|
||||||
|
Bits: 0,
|
||||||
|
Day: day,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 获取某月的带宽峰值
|
// FindMonthlyPeekBandwidthBytes 获取某月的带宽峰值
|
||||||
// month YYYYMM
|
// month YYYYMM
|
||||||
func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string) (int64, error) {
|
func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
|
Attr("serverId", serverId).
|
||||||
Between("day", month+"01", month+"31").
|
Between("day", month+"01", month+"31").
|
||||||
Result("MAX(bytes)").
|
Result("MAX(bytes)").
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
@@ -160,8 +351,8 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
|||||||
if percentile >= 100 {
|
if percentile >= 100 {
|
||||||
result, err = this.Query(tx).
|
result, err = this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Result("bytes").
|
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
|
Result("bytes").
|
||||||
Between("day", month+"01", month+"31").
|
Between("day", month+"01", month+"31").
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
@@ -191,8 +382,8 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
|||||||
// 查询 nth 位置
|
// 查询 nth 位置
|
||||||
result, err = this.Query(tx).
|
result, err = this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Result("bytes").
|
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
|
Result("bytes").
|
||||||
Between("day", month+"01", month+"31").
|
Between("day", month+"01", month+"31").
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
@@ -202,9 +393,68 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
|||||||
return
|
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 清理过期数据
|
// Clean 清理过期数据
|
||||||
func (this *ServerBandwidthStatDAO) Clean(tx *dbs.Tx) error {
|
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 {
|
return this.runBatch(func(table string, locker *sync.Mutex) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Table(table).
|
Table(table).
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -77,3 +78,35 @@ func TestServerBandwidthStatDAO_Clean(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log("ok", time.Since(before).Seconds()*1000, "ms")
|
t.Log("ok", time.Since(before).Seconds()*1000, "ms")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServerBandwidthStatDAO_FindHourlyBandwidthStats(t *testing.T) {
|
||||||
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
stats, err := dao.FindHourlyBandwidthStats(tx, 23, 24)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
logs.PrintAsJSON(stats, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) {
|
||||||
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
stats, err := dao.FindDailyBandwidthStats(tx, 23, 14)
|
||||||
|
if err != nil {
|
||||||
|
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
|
Id uint64 `field:"id"` // ID
|
||||||
UserId uint64 `field:"userId"` // 用户ID
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
ServerId uint64 `field:"serverId"` // 服务ID
|
ServerId uint64 `field:"serverId"` // 服务ID
|
||||||
|
RegionId uint32 `field:"regionId"` // 区域ID
|
||||||
Day string `field:"day"` // 日期YYYYMMDD
|
Day string `field:"day"` // 日期YYYYMMDD
|
||||||
TimeAt string `field:"timeAt"` // 时间点HHMM
|
TimeAt string `field:"timeAt"` // 时间点HHMM
|
||||||
Bytes uint64 `field:"bytes"` // 带宽字节
|
Bytes uint64 `field:"bytes"` // 带宽字节
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerBandwidthStatOperator struct {
|
type ServerBandwidthStatOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
UserId interface{} // 用户ID
|
UserId any // 用户ID
|
||||||
ServerId interface{} // 服务ID
|
ServerId any // 服务ID
|
||||||
Day interface{} // 日期YYYYMMDD
|
RegionId any // 区域ID
|
||||||
TimeAt interface{} // 时间点HHMM
|
Day any // 日期YYYYMMDD
|
||||||
Bytes interface{} // 带宽字节
|
TimeAt any // 时间点HHMM
|
||||||
|
Bytes any // 带宽字节
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerBandwidthStatOperator() *ServerBandwidthStatOperator {
|
func NewServerBandwidthStatOperator() *ServerBandwidthStatOperator {
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -11,6 +13,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -83,7 +86,7 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
|
|||||||
InsertOrUpdate(maps.Map{
|
InsertOrUpdate(maps.Map{
|
||||||
"userId": serverUserId,
|
"userId": serverUserId,
|
||||||
"serverId": stat.ServerId,
|
"serverId": stat.ServerId,
|
||||||
"regionId": stat.RegionId,
|
"regionId": stat.NodeRegionId,
|
||||||
"bytes": stat.Bytes,
|
"bytes": stat.Bytes,
|
||||||
"cachedBytes": stat.CachedBytes,
|
"cachedBytes": stat.CachedBytes,
|
||||||
"countRequests": stat.CountRequests,
|
"countRequests": stat.CountRequests,
|
||||||
@@ -193,7 +196,7 @@ func (this *ServerDailyStatDAO) SumUserMonthlyPeek(tx *dbs.Tx, userId int64, reg
|
|||||||
// SumUserDaily 获取某天流量总和
|
// SumUserDaily 获取某天流量总和
|
||||||
// day 格式为YYYYMMDD
|
// day 格式为YYYYMMDD
|
||||||
func (this *ServerDailyStatDAO) SumUserDaily(tx *dbs.Tx, userId int64, regionId int64, day string) (int64, error) {
|
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 {
|
if regionId > 0 {
|
||||||
query.Attr("regionId", regionId)
|
query.Attr("regionId", regionId)
|
||||||
}
|
}
|
||||||
@@ -203,6 +206,27 @@ func (this *ServerDailyStatDAO) SumUserDaily(tx *dbs.Tx, userId int64, regionId
|
|||||||
SumInt64("bytes", 0)
|
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 获取某月流量总和
|
// SumUserMonthly 获取某月流量总和
|
||||||
// month 格式为YYYYMM
|
// month 格式为YYYYMM
|
||||||
func (this *ServerDailyStatDAO) SumUserMonthly(tx *dbs.Tx, userId int64, month string) (int64, error) {
|
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 获取某天内的流量
|
// SumDailyStat 获取某天内的流量
|
||||||
// day 格式为YYYYMMDD
|
// dayFrom 格式为YYYYMMDD
|
||||||
func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day string) (stat *pb.ServerDailyStat, err error) {
|
// 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{}
|
stat = &pb.ServerDailyStat{}
|
||||||
|
|
||||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
if userId <= 0 && serverId <= 0 {
|
||||||
return nil, errors.New("invalid day '" + day + "'")
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
one, _, err := this.Query(tx).
|
if !regexputils.YYYYMMDD.MatchString(dayFrom) {
|
||||||
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").
|
return nil, errors.New("invalid dayFrom '" + dayFrom + "'")
|
||||||
Attr("serverId", serverId).
|
}
|
||||||
Attr("day", day).
|
if !regexputils.YYYYMMDD.MatchString(dayTo) {
|
||||||
FindOne()
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (this *ServerDailyStatDAO) SumDailyStatBeforeMinute(tx *dbs.Tx, serverId int64, day string, minute string) (stat *pb.ServerDailyStat, err error) {
|
||||||
stat = &pb.ServerDailyStat{}
|
stat = &pb.ServerDailyStat{}
|
||||||
|
|
||||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
if !regexputils.YYYYMMDD.MatchString(day) {
|
||||||
return nil, errors.New("invalid day '" + 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) {
|
func (this *ServerDailyStatDAO) SumMonthlyStat(tx *dbs.Tx, serverId int64, month string) (stat *pb.ServerDailyStat, err error) {
|
||||||
stat = &pb.ServerDailyStat{}
|
stat = &pb.ServerDailyStat{}
|
||||||
|
|
||||||
if !regexp.MustCompile(`^\d{6}$`).MatchString(month) {
|
if !regexputils.YYYYMM.MatchString(month) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,7 +446,7 @@ func (this *ServerDailyStatDAO) SumMonthlyStat(tx *dbs.Tx, serverId int64, month
|
|||||||
// SumMonthlyBytes 获取某月内的流量
|
// SumMonthlyBytes 获取某月内的流量
|
||||||
// month 格式为YYYYMM
|
// month 格式为YYYYMM
|
||||||
func (this *ServerDailyStatDAO) SumMonthlyBytes(tx *dbs.Tx, serverId int64, month string) (result int64, err error) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,6 +490,139 @@ func (this *ServerDailyStatDAO) FindDailyStats(tx *dbs.Tx, serverId int64, dayFr
|
|||||||
return
|
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 查找某月有套餐的流量
|
// FindMonthlyStatsWithPlan 查找某月有套餐的流量
|
||||||
// month YYYYMM
|
// month YYYYMM
|
||||||
func (this *ServerDailyStatDAO) FindMonthlyStatsWithPlan(tx *dbs.Tx, month string) (result []*ServerDailyStat, err error) {
|
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
|
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 设置费用
|
// UpdateStatFee 设置费用
|
||||||
func (this *ServerDailyStatDAO) UpdateStatFee(tx *dbs.Tx, statId int64, fee float32) error {
|
func (this *ServerDailyStatDAO) UpdateStatFee(tx *dbs.Tx, statId int64, fee float32) error {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
|
|||||||
@@ -7,16 +7,17 @@ import (
|
|||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestServerDailyStatDAO_SaveStats(t *testing.T) {
|
func TestServerDailyStatDAO_SaveStats(t *testing.T) {
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats := []*pb.ServerDailyStat{
|
stats := []*pb.ServerDailyStat{
|
||||||
{
|
{
|
||||||
ServerId: 1,
|
ServerId: 1,
|
||||||
RegionId: 2,
|
NodeRegionId: 2,
|
||||||
Bytes: 1,
|
Bytes: 1,
|
||||||
CreatedAt: 1607671488,
|
CreatedAt: 1607671488,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
err := NewServerDailyStatDAO().SaveStats(tx, stats)
|
err := NewServerDailyStatDAO().SaveStats(tx, stats)
|
||||||
@@ -30,10 +31,10 @@ func TestServerDailyStatDAO_SaveStats2(t *testing.T) {
|
|||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats := []*pb.ServerDailyStat{
|
stats := []*pb.ServerDailyStat{
|
||||||
{
|
{
|
||||||
ServerId: 1,
|
ServerId: 1,
|
||||||
RegionId: 3,
|
NodeRegionId: 3,
|
||||||
Bytes: 1,
|
Bytes: 1,
|
||||||
CreatedAt: 1607671488,
|
CreatedAt: 1607671488,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
err := NewServerDailyStatDAO().SaveStats(tx, stats)
|
err := NewServerDailyStatDAO().SaveStats(tx, stats)
|
||||||
@@ -83,3 +84,26 @@ func TestServerDailyStatDAO_FindDistinctPlanServerIdsBetweenDay(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log(serverIds)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -759,7 +759,8 @@ func (this *ServerDAO) CountAllEnabledServers(tx *dbs.Tx) (int64, error) {
|
|||||||
|
|
||||||
// CountAllEnabledServersMatch 计算所有可用服务数量
|
// CountAllEnabledServersMatch 计算所有可用服务数量
|
||||||
// 参数:
|
// 参数:
|
||||||
// groupId 分组ID,如果为-1,则搜索没有分组的服务
|
//
|
||||||
|
// groupId 分组ID,如果为-1,则搜索没有分组的服务
|
||||||
func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag configutils.BoolState, protocolFamilies []string) (int64, error) {
|
func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag configutils.BoolState, protocolFamilies []string) (int64, error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
State(ServerStateEnabled)
|
State(ServerStateEnabled)
|
||||||
@@ -810,7 +811,8 @@ func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, ke
|
|||||||
|
|
||||||
// ListEnabledServersMatch 列出单页的服务
|
// ListEnabledServersMatch 列出单页的服务
|
||||||
// 参数:
|
// 参数:
|
||||||
// groupId 分组ID,如果为-1,则搜索没有分组的服务
|
//
|
||||||
|
// groupId 分组ID,如果为-1,则搜索没有分组的服务
|
||||||
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamilies []string, order string) (result []*Server, err error) {
|
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamilies []string, order string) (result []*Server, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
@@ -860,49 +862,33 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 排序
|
// 排序
|
||||||
var day = timeutil.Format("Ymd")
|
var timestamp = (time.Now().Unix()) / 300 * 300
|
||||||
var minute = timeutil.FormatTime("His", time.Now().Unix()/300*300-300)
|
var times = []string{
|
||||||
var selfTable = this.Table
|
timeutil.FormatTime("YmdHi", timestamp),
|
||||||
var statTable = SharedServerDailyStatDAO.Table
|
timeutil.FormatTime("YmdHi", timestamp-300),
|
||||||
var hasOnlyIds = false
|
timeutil.FormatTime("YmdHi", timestamp-300*2),
|
||||||
|
}
|
||||||
|
|
||||||
switch order {
|
switch order {
|
||||||
case "trafficOutAsc":
|
case "trafficOutAsc":
|
||||||
query.Result("id")
|
query.Asc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
|
||||||
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute")
|
query.Param("times", strings.Join(times, ","))
|
||||||
query.Param("day", day)
|
query.DescPk()
|
||||||
query.Param("minute", minute)
|
|
||||||
query.Group(selfTable + ".id")
|
|
||||||
query.Asc("SUM(" + statTable + ".bytes)").
|
|
||||||
DescPk()
|
|
||||||
hasOnlyIds = true
|
|
||||||
case "trafficOutDesc":
|
case "trafficOutDesc":
|
||||||
query.Result("id")
|
query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
|
||||||
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute")
|
query.Param("times", strings.Join(times, ","))
|
||||||
query.Param("day", day)
|
query.DescPk()
|
||||||
query.Param("minute", minute)
|
|
||||||
query.Group(selfTable + ".id")
|
|
||||||
query.Desc("SUM(" + statTable + ".bytes)").
|
|
||||||
DescPk()
|
|
||||||
hasOnlyIds = true
|
|
||||||
default:
|
default:
|
||||||
query.DescPk()
|
query.DescPk()
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
|
|
||||||
if hasOnlyIds {
|
// 修正带宽统计数据
|
||||||
var newResult = []*Server{}
|
for _, server := range result {
|
||||||
for _, one := range result {
|
if len(server.BandwidthTime) > 0 && !lists.ContainsString(times, server.BandwidthTime) {
|
||||||
server, err := this.Find(tx, one.Id)
|
server.BandwidthBytes = 0
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if server == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
newResult = append(newResult, server.(*Server))
|
|
||||||
}
|
}
|
||||||
result = newResult
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -1023,12 +1009,12 @@ func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int6
|
|||||||
if server == nil {
|
if server == nil {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
return this.ComposeServerConfig(tx, server, nil, forNode)
|
return this.ComposeServerConfig(tx, server, nil, forNode, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComposeServerConfig 构造服务的Config
|
// ComposeServerConfig 构造服务的Config
|
||||||
// forNode 是否是节点请求
|
// 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 {
|
if server == nil {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
@@ -1053,7 +1039,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
|
|
||||||
var groupConfig *serverconfigs.ServerGroupConfig
|
var groupConfig *serverconfigs.ServerGroupConfig
|
||||||
for _, groupId := range server.DecodeGroupIds() {
|
for _, groupId := range server.DecodeGroupIds() {
|
||||||
groupConfig1, err := SharedServerGroupDAO.ComposeGroupConfig(tx, groupId, cacheMap)
|
groupConfig1, err := SharedServerGroupDAO.ComposeGroupConfig(tx, groupId, forList, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1076,28 +1062,30 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CNAME
|
// CNAME
|
||||||
config.SupportCNAME = server.SupportCNAME == 1
|
if !forList {
|
||||||
if server.ClusterId > 0 && len(server.DnsName) > 0 {
|
config.SupportCNAME = server.SupportCNAME == 1
|
||||||
clusterDNS, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, int64(server.ClusterId), cacheMap)
|
if server.ClusterId > 0 && len(server.DnsName) > 0 {
|
||||||
if err != nil {
|
clusterDNS, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, int64(server.ClusterId), cacheMap)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if clusterDNS != nil && clusterDNS.DnsDomainId > 0 {
|
|
||||||
clusterDNSConfig, err := clusterDNS.DecodeDNSConfig()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, int64(clusterDNS.DnsDomainId), cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if domain != nil {
|
if domain != nil {
|
||||||
var cname = server.DnsName + "." + domain.Name
|
var cname = server.DnsName + "." + domain.Name
|
||||||
config.CNameDomain = cname
|
config.CNameDomain = cname
|
||||||
if clusterDNSConfig.CNameAsDomain {
|
if clusterDNSConfig.CNAMEAsDomain {
|
||||||
config.CNameAsDomain = true
|
config.CNameAsDomain = true
|
||||||
config.AliasServerNames = append(config.AliasServerNames, cname)
|
config.AliasServerNames = append(config.AliasServerNames, cname)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1188,61 +1176,71 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Web
|
// Web
|
||||||
if server.WebId > 0 {
|
if !forList {
|
||||||
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(server.WebId), cacheMap)
|
if server.WebId > 0 {
|
||||||
if err != nil {
|
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(server.WebId), cacheMap)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
if webConfig != nil {
|
}
|
||||||
config.Web = webConfig
|
if webConfig != nil {
|
||||||
|
config.Web = webConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReverseProxy
|
// ReverseProxy
|
||||||
if IsNotNull(server.ReverseProxy) {
|
if !forList {
|
||||||
var reverseProxyRef = &serverconfigs.ReverseProxyRef{}
|
if IsNotNull(server.ReverseProxy) {
|
||||||
err := json.Unmarshal(server.ReverseProxy, reverseProxyRef)
|
var reverseProxyRef = &serverconfigs.ReverseProxyRef{}
|
||||||
if err != nil {
|
err := json.Unmarshal(server.ReverseProxy, reverseProxyRef)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
config.ReverseProxyRef = reverseProxyRef
|
}
|
||||||
|
config.ReverseProxyRef = reverseProxyRef
|
||||||
|
|
||||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if reverseProxyConfig != nil {
|
if reverseProxyConfig != nil {
|
||||||
config.ReverseProxy = reverseProxyConfig
|
config.ReverseProxy = reverseProxyConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WAF策略
|
// WAF策略
|
||||||
var clusterId = int64(server.ClusterId)
|
var clusterId = int64(server.ClusterId)
|
||||||
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
|
if !forList {
|
||||||
if err != nil {
|
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
|
||||||
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 err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用户套餐
|
// 用户套餐
|
||||||
@@ -1285,7 +1283,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
if config.TrafficLimit != nil && config.TrafficLimit.IsOn && !config.TrafficLimit.IsEmpty() {
|
if config.TrafficLimit != nil && config.TrafficLimit.IsOn && !config.TrafficLimit.IsEmpty() {
|
||||||
if len(server.TrafficLimitStatus) > 0 {
|
if len(server.TrafficLimitStatus) > 0 {
|
||||||
var status = &serverconfigs.TrafficLimitStatus{}
|
var status = &serverconfigs.TrafficLimitStatus{}
|
||||||
err = json.Unmarshal(server.TrafficLimitStatus, status)
|
err := json.Unmarshal(server.TrafficLimitStatus, status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1296,14 +1294,16 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UAM
|
// UAM
|
||||||
if teaconst.IsPlus && IsNotNull(server.Uam) {
|
if !forList {
|
||||||
var uamConfig = &serverconfigs.UAMConfig{}
|
if teaconst.IsPlus && IsNotNull(server.Uam) {
|
||||||
err = json.Unmarshal(server.Uam, uamConfig)
|
var uamConfig = &serverconfigs.UAMConfig{}
|
||||||
if err != nil {
|
err := json.Unmarshal(server.Uam, uamConfig)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
if uamConfig.IsOn {
|
}
|
||||||
config.UAM = uamConfig
|
if uamConfig.IsOn {
|
||||||
|
config.UAM = uamConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1629,6 +1629,33 @@ func (this *ServerDAO) GenerateServerDNSName(tx *dbs.Tx, serverId int64) (string
|
|||||||
return dnsName, nil
|
return dnsName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateServerDNSName 设置CNAME
|
||||||
|
func (this *ServerDAO) UpdateServerDNSName(tx *dbs.Tx, serverId int64, dnsName string) error {
|
||||||
|
if serverId <= 0 || len(dnsName) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
dnsName = strings.ToLower(dnsName)
|
||||||
|
err := this.Query(tx).
|
||||||
|
Pk(serverId).
|
||||||
|
Set("dnsName", dnsName).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyDNSUpdate(tx, serverId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindServerIdWithDNSName 根据CNAME查询服务ID
|
||||||
|
func (this *ServerDAO) FindServerIdWithDNSName(tx *dbs.Tx, clusterId int64, dnsName string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
ResultPk().
|
||||||
|
State(ServerStateEnabled).
|
||||||
|
Attr("clusterId", clusterId).
|
||||||
|
Attr("dnsName", dnsName).
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
// FindServerClusterId 查询当前服务的集群ID
|
// FindServerClusterId 查询当前服务的集群ID
|
||||||
func (this *ServerDAO) FindServerClusterId(tx *dbs.Tx, serverId int64) (int64, error) {
|
func (this *ServerDAO) FindServerClusterId(tx *dbs.Tx, serverId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -1734,11 +1761,11 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
|||||||
}
|
}
|
||||||
|
|
||||||
if oldClusterId > 0 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, 0, NodeTaskTypeIPItemChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, 0, 0, NodeTaskTypeIPItemChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1749,11 +1776,11 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
|||||||
}
|
}
|
||||||
|
|
||||||
if newClusterId > 0 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, 0, NodeTaskTypeIPItemChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, 0, 0, NodeTaskTypeIPItemChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1766,11 +1793,24 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAllEnabledServersWithUserId 查找用户的所有的服务
|
// FindAllBasicServersWithUserId 查找用户的所有服务的基础信息
|
||||||
func (this *ServerDAO) FindAllEnabledServersWithUserId(tx *dbs.Tx, userId int64) (result []*Server, err error) {
|
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).
|
_, err = this.Query(tx).
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
Attr("userId", userId).
|
Attr("userId", userId).
|
||||||
|
Attr("isOn", true).
|
||||||
DescPk().
|
DescPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
@@ -1867,8 +1907,8 @@ func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, protocolFam
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ExistServerNameInCluster 检查ServerName是否已存在
|
// ExistServerNameInCluster 检查ServerName是否已存在
|
||||||
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64) (bool, error) {
|
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64, supportWildcard bool) (bool, error) {
|
||||||
query := this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))").
|
Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))").
|
||||||
Param("jsonQuery1", maps.Map{"name": serverName}.AsJSON()).
|
Param("jsonQuery1", maps.Map{"name": serverName}.AsJSON()).
|
||||||
@@ -1877,7 +1917,38 @@ func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, ser
|
|||||||
query.Neq("id", excludeServerId)
|
query.Neq("id", excludeServerId)
|
||||||
}
|
}
|
||||||
query.State(ServerStateEnabled)
|
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
|
// GenDNSName 生成DNS Name
|
||||||
@@ -2320,21 +2391,21 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitCo
|
|||||||
|
|
||||||
// daily
|
// daily
|
||||||
if trafficLimitConfig.DailyBytes() > 0 {
|
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")
|
untilDay = timeutil.Format("Ymd")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// monthly
|
// monthly
|
||||||
if server.TrafficMonth == timeutil.Format("Ym") && trafficLimitConfig.MonthlyBytes() > 0 {
|
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")
|
untilDay = timeutil.Format("Ym32")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// totally
|
// totally
|
||||||
if trafficLimitConfig.TotalBytes() > 0 {
|
if trafficLimitConfig.TotalBytes() > 0 {
|
||||||
if server.TotalTraffic >= float64(trafficLimitConfig.TotalBytes())/1024/1024/1024 {
|
if server.TotalTraffic >= float64(trafficLimitConfig.TotalBytes())/(1<<30) {
|
||||||
untilDay = "30000101"
|
untilDay = "30000101"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2364,7 +2435,7 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitCo
|
|||||||
|
|
||||||
// IncreaseServerTotalTraffic 增加服务的总流量
|
// IncreaseServerTotalTraffic 增加服务的总流量
|
||||||
func (this *ServerDAO) IncreaseServerTotalTraffic(tx *dbs.Tx, serverId int64, bytes int64) error {
|
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 day = timeutil.Format("Ymd")
|
||||||
var month = timeutil.Format("Ym")
|
var month = timeutil.Format("Ym")
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -2549,6 +2620,40 @@ func (this *ServerDAO) FindUserServerClusterIds(tx *dbs.Tx, userId int64) ([]int
|
|||||||
return clusterIds, nil
|
return clusterIds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateServerBandwidth 更新服务带宽
|
||||||
|
// fullTime YYYYMMDDHHII
|
||||||
|
func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTime string, bandwidthBytes int64) error {
|
||||||
|
if serverId <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if bandwidthBytes < 0 {
|
||||||
|
bandwidthBytes = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
bandwidthTime, err := this.Query(tx).
|
||||||
|
Pk(serverId).
|
||||||
|
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 同步服务所在的集群
|
// NotifyUpdate 同步服务所在的集群
|
||||||
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
||||||
// 创建任务
|
// 创建任务
|
||||||
@@ -2559,7 +2664,7 @@ func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
|||||||
if clusterId == 0 {
|
if clusterId == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, serverId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, serverId, NodeTaskTypeConfigChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifyClusterUpdate 同步指定的集群
|
// NotifyClusterUpdate 同步指定的集群
|
||||||
@@ -2567,7 +2672,7 @@ func (this *ServerDAO) NotifyClusterUpdate(tx *dbs.Tx, clusterId, serverId int64
|
|||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, serverId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, serverId, NodeTaskTypeConfigChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifyDNSUpdate 通知当前集群DNS更新
|
// NotifyDNSUpdate 通知当前集群DNS更新
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -141,7 +142,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
|
|||||||
|
|
||||||
var tx *dbs.Tx
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -189,7 +190,7 @@ func TestServerDAO_FindAllEnabledServersWithNode_Cache(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, server := range servers {
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, server := range servers {
|
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")
|
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) {
|
func BenchmarkServerDAO_CountAllEnabledServers(b *testing.B) {
|
||||||
models.SharedServerDAO = models.NewServerDAO()
|
models.SharedServerDAO = models.NewServerDAO()
|
||||||
|
|
||||||
|
|||||||
@@ -279,7 +279,7 @@ func (this *ServerGroupDAO) InitGroupWeb(tx *dbs.Tx, groupId int64) (int64, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeGroupConfig 组合配置
|
// 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 {
|
if cacheMap == nil {
|
||||||
cacheMap = utils.NewCacheMap()
|
cacheMap = utils.NewCacheMap()
|
||||||
}
|
}
|
||||||
@@ -315,65 +315,67 @@ func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheM
|
|||||||
IsOn: group.IsOn,
|
IsOn: group.IsOn,
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsNotNull(group.HttpReverseProxy) {
|
if !forList {
|
||||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
if IsNotNull(group.HttpReverseProxy) {
|
||||||
err := json.Unmarshal(group.HttpReverseProxy, reverseProxyRef)
|
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||||
if err != nil {
|
err := json.Unmarshal(group.HttpReverseProxy, reverseProxyRef)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
config.HTTPReverseProxyRef = reverseProxyRef
|
}
|
||||||
|
config.HTTPReverseProxyRef = reverseProxyRef
|
||||||
|
|
||||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
|
if reverseProxyConfig != nil {
|
||||||
|
config.HTTPReverseProxy = reverseProxyConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if reverseProxyConfig != nil {
|
|
||||||
config.HTTPReverseProxy = reverseProxyConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if IsNotNull(group.TcpReverseProxy) {
|
if IsNotNull(group.TcpReverseProxy) {
|
||||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||||
err := json.Unmarshal(group.TcpReverseProxy, reverseProxyRef)
|
err := json.Unmarshal(group.TcpReverseProxy, reverseProxyRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.TCPReverseProxyRef = reverseProxyRef
|
config.TCPReverseProxyRef = reverseProxyRef
|
||||||
|
|
||||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
|
if reverseProxyConfig != nil {
|
||||||
|
config.TCPReverseProxy = reverseProxyConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if reverseProxyConfig != nil {
|
|
||||||
config.TCPReverseProxy = reverseProxyConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if IsNotNull(group.UdpReverseProxy) {
|
if IsNotNull(group.UdpReverseProxy) {
|
||||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||||
err := json.Unmarshal(group.UdpReverseProxy, reverseProxyRef)
|
err := json.Unmarshal(group.UdpReverseProxy, reverseProxyRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.UDPReverseProxyRef = reverseProxyRef
|
config.UDPReverseProxyRef = reverseProxyRef
|
||||||
|
|
||||||
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
|
if reverseProxyConfig != nil {
|
||||||
|
config.UDPReverseProxy = reverseProxyConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if reverseProxyConfig != nil {
|
|
||||||
config.UDPReverseProxy = reverseProxyConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// web
|
// web
|
||||||
if group.WebId > 0 {
|
if group.WebId > 0 {
|
||||||
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(group.WebId), cacheMap)
|
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(group.WebId), cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if webConfig != nil {
|
if webConfig != nil {
|
||||||
config.Web = webConfig
|
config.Web = webConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user