Compare commits
90 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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.
@@ -14,7 +14,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"
|
||||||
)
|
)
|
||||||
@@ -130,7 +129,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("[]")
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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())
|
||||||
|
|||||||
@@ -40,6 +40,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 {
|
||||||
@@ -75,7 +76,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 +87,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 +125,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,
|
||||||
}
|
}
|
||||||
@@ -146,7 +147,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 +158,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 +196,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,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.5.0"
|
Version = "0.5.3"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,11 +18,11 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.5.0"
|
NodeVersion = "0.5.3"
|
||||||
UserNodeVersion = "0.4.0"
|
UserNodeVersion = "0.5.0"
|
||||||
|
DNSNodeVersion = "0.2.7"
|
||||||
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版本号
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,18 +2,14 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"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/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,22 +27,12 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// NSAccessLogDAOWrapper NS访问日志DAO
|
|
||||||
type NSAccessLogDAOWrapper struct {
|
|
||||||
DAO *NSAccessLogDAO
|
|
||||||
NodeId int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initializer := NewDBNodeInitializer()
|
initializer := NewDBNodeInitializer()
|
||||||
dbs.OnReadyDone(func() {
|
dbs.OnReadyDone(func() {
|
||||||
@@ -78,102 +64,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 +121,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 +145,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 {
|
||||||
// 检查配置是否有变化
|
// 检查配置是否有变化
|
||||||
@@ -308,49 +220,8 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
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)
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
@@ -84,11 +86,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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -132,7 +152,11 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
var dao = randomHTTPAccessLogDAO()
|
var dao = randomHTTPAccessLogDAO()
|
||||||
if dao == nil {
|
if dao == nil {
|
||||||
dao = &HTTPAccessLogDAOWrapper{
|
dao = &HTTPAccessLogDAOWrapper{
|
||||||
@@ -141,14 +165,25 @@ func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(tx *dbs.Tx, size int) erro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if size <= 0 {
|
if len(oldAccessLogQueue) == 0 && len(accessLogQueue) == 0 {
|
||||||
size = 1_000_000
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 开始事务
|
||||||
|
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 +191,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 +203,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 +455,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 +529,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 +805,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 +819,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 {
|
||||||
|
|||||||
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 models
|
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 // 空值列表
|
||||||
|
|||||||
@@ -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,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) error {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return errors.New("invalid clusterId")
|
return errors.New("invalid clusterId")
|
||||||
}
|
}
|
||||||
@@ -203,6 +215,17 @@ 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
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -443,7 +466,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 +491,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 +510,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 +533,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 +554,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 +967,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", "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 +1109,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
|
||||||
@@ -1092,6 +1135,49 @@ func (this *NodeClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId in
|
|||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeDDosProtectionChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 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, 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, NodeTaskTypeConfigChanged)
|
||||||
|
|||||||
@@ -30,45 +30,53 @@ 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{} // 状态
|
State any // 状态
|
||||||
AutoRegister interface{} // 是否开启自动注册
|
AutoRegister any // 是否开启自动注册
|
||||||
UniqueId interface{} // 唯一ID
|
UniqueId any // 唯一ID
|
||||||
Secret interface{} // 密钥
|
Secret any // 密钥
|
||||||
HealthCheck interface{} // 健康检查
|
HealthCheck any // 健康检查
|
||||||
DnsName interface{} // DNS名称
|
DnsName any // DNS名称
|
||||||
DnsDomainId interface{} // 域名ID
|
DnsDomainId any // 域名ID
|
||||||
Dns interface{} // DNS配置
|
Dns any // DNS配置
|
||||||
Toa interface{} // TOA配置
|
Toa any // TOA配置
|
||||||
CachePolicyId interface{} // 缓存策略ID
|
CachePolicyId any // 缓存策略ID
|
||||||
HttpFirewallPolicyId interface{} // WAF策略ID
|
HttpFirewallPolicyId any // WAF策略ID
|
||||||
AccessLog interface{} // 访问日志设置
|
AccessLog any // 访问日志设置
|
||||||
SystemServices interface{} // 系统服务设置
|
SystemServices any // 系统服务设置
|
||||||
TimeZone interface{} // 时区
|
TimeZone any // 时区
|
||||||
NodeMaxThreads interface{} // 节点最大线程数
|
NodeMaxThreads any // 节点最大线程数
|
||||||
DdosProtection interface{} // DDOS端口
|
DdosProtection any // DDoS防护设置
|
||||||
AutoOpenPorts interface{} // 是否自动尝试开放端口
|
AutoOpenPorts any // 是否自动尝试开放端口
|
||||||
IsPinned interface{} // 是否置顶
|
IsPinned any // 是否置顶
|
||||||
Webp interface{} // WebP设置
|
Webp any // WebP设置
|
||||||
Uam interface{} // UAM设置
|
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,13 +14,15 @@ func (this *NodeCluster) DecodeDNSConfig() (*dnsconfigs.ClusterDNSConfig, error)
|
|||||||
if len(this.Dns) == 0 {
|
if len(this.Dns) == 0 {
|
||||||
// 一定要返回一个默认的值,防止产生nil
|
// 一定要返回一个默认的值,防止产生nil
|
||||||
return &dnsconfigs.ClusterDNSConfig{
|
return &dnsconfigs.ClusterDNSConfig{
|
||||||
NodesAutoSync: false,
|
NodesAutoSync: false,
|
||||||
ServersAutoSync: false,
|
ServersAutoSync: false,
|
||||||
CNameAsDomain: true,
|
CNAMEAsDomain: true,
|
||||||
|
IncludingLnNodes: true,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
|
||||||
CNameAsDomain: true,
|
CNAMEAsDomain: true,
|
||||||
|
IncludingLnNodes: true,
|
||||||
}
|
}
|
||||||
err := json.Unmarshal(this.Dns, &dnsConfig)
|
err := json.Unmarshal(this.Dns, &dnsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -48,3 +53,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
|
||||||
|
}
|
||||||
|
|||||||
@@ -140,18 +140,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)
|
||||||
@@ -199,7 +190,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) error {
|
||||||
if nodeId <= 0 {
|
if nodeId <= 0 {
|
||||||
return errors.New("invalid nodeId")
|
return errors.New("invalid nodeId")
|
||||||
}
|
}
|
||||||
@@ -248,6 +239,15 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
@@ -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).
|
||||||
@@ -1076,6 +1076,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 +1113,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++
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1403,7 +1418,7 @@ func (this *NodeDAO) CountAllEnabledNodesWithRegionId(tx *dbs.Tx, regionId int64
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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 +1430,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 +1896,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 +1946,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
|
||||||
|
|||||||
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
|
||||||
|
}
|
||||||
@@ -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"` // 连续在线次数
|
||||||
@@ -42,41 +43,42 @@ type Node struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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解析器
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,20 +14,22 @@ import (
|
|||||||
type NodeTaskType = string
|
type NodeTaskType = string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
|
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
|
||||||
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
|
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
|
||||||
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
|
NodeTaskTypeGlobalServerConfigChanged NodeTaskType = "globalServerConfigChanged" // 全局服务设置变化
|
||||||
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
|
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
|
||||||
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged"
|
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
|
||||||
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged"
|
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged"
|
||||||
|
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged"
|
||||||
|
|
||||||
// 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
|
||||||
@@ -157,45 +159,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).
|
||||||
|
|||||||
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
|
||||||
|
}
|
||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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,75 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeHosts 解析主机地址
|
||||||
|
func (this *NSCluster) DecodeHosts() []string {
|
||||||
|
if IsNull(this.Hosts) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var hosts = []string{}
|
||||||
|
err := json.Unmarshal(this.Hosts, &hosts)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("NSCluster.DecodeHosts", "decode failed: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return hosts
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeSOAConfig 解析SOA设置
|
||||||
|
func (this *NSCluster) DecodeSOAConfig() *dnsconfigs.NSSOAConfig {
|
||||||
|
var config = dnsconfigs.DefaultNSSOAConfig()
|
||||||
|
if IsNull(this.Soa) {
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
err := json.Unmarshal(this.Soa, config)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("NSCluster.DecodeSOAConfig", "decode failed: "+err.Error())
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeAnswerConfig 解析应答设置
|
||||||
|
func (this *NSCluster) DecodeAnswerConfig() *dnsconfigs.NSAnswerConfig {
|
||||||
|
var config = dnsconfigs.DefaultNSAnswerConfig()
|
||||||
|
|
||||||
|
if IsNull(this.Answer) {
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
err := json.Unmarshal(this.Answer, config)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("NSCluster.DecodeAnswerConfig", "decode failed: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|||||||
@@ -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 先什么都不做
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
)
|
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,8 @@ 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/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 +14,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 +85,131 @@ 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]
|
||||||
|
|
||||||
|
m[day+hour] = &pb.FindHourlyServerBandwidthStatsResponse_Stat{
|
||||||
|
Bytes: one.GetInt64("bytes"),
|
||||||
|
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,
|
||||||
|
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")
|
||||||
|
|
||||||
|
m[day] = &pb.FindDailyServerBandwidthStatsResponse_Stat{
|
||||||
|
Bytes: one.GetInt64("bytes"),
|
||||||
|
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,
|
||||||
|
Day: day,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 +266,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 +297,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).
|
||||||
@@ -204,7 +310,7 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
|||||||
|
|
||||||
// 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,24 @@ 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)
|
||||||
|
}
|
||||||
@@ -437,6 +437,31 @@ func (this *ServerDailyStatDAO) FindDailyStats(tx *dbs.Tx, serverId int64, dayFr
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindStatsWithDay 按天查找5分钟级统计
|
||||||
|
// day YYYYMMDD
|
||||||
|
func (this *ServerDailyStatDAO) FindStatsWithDay(tx *dbs.Tx, serverId int64, day string, timeFrom string, timeTo string) (result []*ServerDailyStat, err error) {
|
||||||
|
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var query = this.Query(tx).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Attr("day", day).
|
||||||
|
DescPk()
|
||||||
|
|
||||||
|
if len(timeFrom) > 0 {
|
||||||
|
query.Gte("timeFrom", timeFrom)
|
||||||
|
}
|
||||||
|
if len(timeTo) > 0 {
|
||||||
|
query.Lte("timeTo", timeTo)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
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) {
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -1095,7 +1081,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
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)
|
||||||
}
|
}
|
||||||
@@ -1629,6 +1615,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).
|
||||||
@@ -1867,8 +1880,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 +1890,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
|
||||||
@@ -2549,6 +2593,22 @@ 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
|
||||||
|
}
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(serverId).
|
||||||
|
Set("bandwidthTime", fullTime).
|
||||||
|
Set("bandwidthBytes", bandwidthBytes).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 同步服务所在的集群
|
// NotifyUpdate 同步服务所在的集群
|
||||||
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
||||||
// 创建任务
|
// 创建任务
|
||||||
|
|||||||
@@ -141,7 +141,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 +149,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 +157,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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,53 +48,57 @@ type Server struct {
|
|||||||
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
|
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
|
||||||
LastUserPlanId uint32 `field:"lastUserPlanId"` // 上一次使用的套餐
|
LastUserPlanId uint32 `field:"lastUserPlanId"` // 上一次使用的套餐
|
||||||
Uam dbs.JSON `field:"uam"` // UAM设置
|
Uam dbs.JSON `field:"uam"` // UAM设置
|
||||||
|
BandwidthTime string `field:"bandwidthTime"` // 带宽更新时间,YYYYMMDDHHII
|
||||||
|
BandwidthBytes uint64 `field:"bandwidthBytes"` // 最近带宽峰值
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerOperator struct {
|
type ServerOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
UserId interface{} // 用户ID
|
UserId any // 用户ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
Type interface{} // 服务类型
|
Type any // 服务类型
|
||||||
Name interface{} // 名称
|
Name any // 名称
|
||||||
Description interface{} // 描述
|
Description any // 描述
|
||||||
PlainServerNames interface{} // 扁平化域名列表
|
PlainServerNames any // 扁平化域名列表
|
||||||
ServerNames interface{} // 域名列表
|
ServerNames any // 域名列表
|
||||||
AuditingAt interface{} // 审核提交时间
|
AuditingAt any // 审核提交时间
|
||||||
AuditingServerNames interface{} // 审核中的域名
|
AuditingServerNames any // 审核中的域名
|
||||||
IsAuditing interface{} // 是否正在审核
|
IsAuditing any // 是否正在审核
|
||||||
AuditingResult interface{} // 审核结果
|
AuditingResult any // 审核结果
|
||||||
Http interface{} // HTTP配置
|
Http any // HTTP配置
|
||||||
Https interface{} // HTTPS配置
|
Https any // HTTPS配置
|
||||||
Tcp interface{} // TCP配置
|
Tcp any // TCP配置
|
||||||
Tls interface{} // TLS配置
|
Tls any // TLS配置
|
||||||
Unix interface{} // Unix配置
|
Unix any // Unix配置
|
||||||
Udp interface{} // UDP配置
|
Udp any // UDP配置
|
||||||
WebId interface{} // WEB配置
|
WebId any // WEB配置
|
||||||
ReverseProxy interface{} // 反向代理配置
|
ReverseProxy any // 反向代理配置
|
||||||
GroupIds interface{} // 分组ID列表
|
GroupIds any // 分组ID列表
|
||||||
Config interface{} // 服务配置,自动生成
|
Config any // 服务配置,自动生成
|
||||||
ConfigMd5 interface{} // Md5
|
ConfigMd5 any // Md5
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId any // 集群ID
|
||||||
IncludeNodes interface{} // 部署条件
|
IncludeNodes any // 部署条件
|
||||||
ExcludeNodes interface{} // 节点排除条件
|
ExcludeNodes any // 节点排除条件
|
||||||
Version interface{} // 版本号
|
Version any // 版本号
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
DnsName interface{} // DNS名称
|
DnsName any // DNS名称
|
||||||
TcpPorts interface{} // 所包含TCP端口
|
TcpPorts any // 所包含TCP端口
|
||||||
UdpPorts interface{} // 所包含UDP端口
|
UdpPorts any // 所包含UDP端口
|
||||||
SupportCNAME interface{} // 允许CNAME不在域名名单
|
SupportCNAME any // 允许CNAME不在域名名单
|
||||||
TrafficLimit interface{} // 流量限制
|
TrafficLimit any // 流量限制
|
||||||
TrafficDay interface{} // YYYYMMDD
|
TrafficDay any // YYYYMMDD
|
||||||
TrafficMonth interface{} // YYYYMM
|
TrafficMonth any // YYYYMM
|
||||||
TotalDailyTraffic interface{} // 日流量
|
TotalDailyTraffic any // 日流量
|
||||||
TotalMonthlyTraffic interface{} // 月流量
|
TotalMonthlyTraffic any // 月流量
|
||||||
TrafficLimitStatus interface{} // 流量限制状态
|
TrafficLimitStatus any // 流量限制状态
|
||||||
TotalTraffic interface{} // 总流量
|
TotalTraffic any // 总流量
|
||||||
UserPlanId interface{} // 所属套餐ID
|
UserPlanId any // 所属套餐ID
|
||||||
LastUserPlanId interface{} // 上一次使用的套餐
|
LastUserPlanId any // 上一次使用的套餐
|
||||||
Uam interface{} // UAM设置
|
Uam any // UAM设置
|
||||||
|
BandwidthTime any // 带宽更新时间,YYYYMMDDHHII
|
||||||
|
BandwidthBytes any // 最近带宽峰值
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerOperator() *ServerOperator {
|
func NewServerOperator() *ServerOperator {
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ func (this *SSLCertDAO) UpdateCert(tx *dbs.Tx,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var oldCert = oldOne.(*SSLCert)
|
var oldCert = oldOne.(*SSLCert)
|
||||||
var dataIsChanged = bytes.Compare(certData, oldCert.CertData) != 0 || bytes.Compare(keyData, oldCert.KeyData) != 0
|
var dataIsChanged = !bytes.Equal(certData, oldCert.CertData) || !bytes.Equal(keyData, oldCert.KeyData)
|
||||||
|
|
||||||
var op = NewSSLCertOperator()
|
var op = NewSSLCertOperator()
|
||||||
op.Id = certId
|
op.Id = certId
|
||||||
@@ -358,7 +358,7 @@ func (this *SSLCertDAO) FindAllExpiringCerts(tx *dbs.Tx, days int) (result []*SS
|
|||||||
days = 0
|
days = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
deltaSeconds := int64(days * 86400)
|
var deltaSeconds = int64(days * 86400)
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(SSLCertStateEnabled).
|
State(SSLCertStateEnabled).
|
||||||
Where("FROM_UNIXTIME(timeEndAt, '%Y-%m-%d')=:day AND FROM_UNIXTIME(notifiedAt, '%Y-%m-%d')!=:today").
|
Where("FROM_UNIXTIME(timeEndAt, '%Y-%m-%d')=:day AND FROM_UNIXTIME(notifiedAt, '%Y-%m-%d')!=:today").
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/zero"
|
"github.com/TeaOSLab/EdgeAPI/internal/zero"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SysSettingDAO dbs.DAO
|
type SysSettingDAO dbs.DAO
|
||||||
@@ -211,6 +213,18 @@ func (this *SysSettingDAO) NotifyUpdate(tx *dbs.Tx, code string) error {
|
|||||||
switch code {
|
switch code {
|
||||||
case systemconfigs.SettingCodeAccessLogQueue:
|
case systemconfigs.SettingCodeAccessLogQueue:
|
||||||
accessLogQueueChanged <- zero.New()
|
accessLogQueueChanged <- zero.New()
|
||||||
|
case systemconfigs.SettingCodeAdminUIConfig:
|
||||||
|
// 修改当前时区
|
||||||
|
config, err := this.ReadAdminUIConfig(nil, nil)
|
||||||
|
if err == nil && config != nil {
|
||||||
|
if len(config.TimeZone) == 0 {
|
||||||
|
config.TimeZone = nodeconfigs.DefaultTimeZoneLocation
|
||||||
|
}
|
||||||
|
location, err := time.LoadLocation(config.TimeZone)
|
||||||
|
if err == nil && time.Local != location {
|
||||||
|
time.Local = location
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInDay(tx *dbs.Tx, userId
|
|||||||
|
|
||||||
// Clean 清理过期数据
|
// Clean 清理过期数据
|
||||||
func (this *UserBandwidthStatDAO) Clean(tx *dbs.Tx) error {
|
func (this *UserBandwidthStatDAO) Clean(tx *dbs.Tx) error {
|
||||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -62)) // 保留大约2个月的数据
|
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -100)) // 保留大约3个月的数据
|
||||||
return this.runBatch(func(table string, locker *sync.Mutex) error {
|
return this.runBatch(func(table string, locker *sync.Mutex) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Table(table).
|
Table(table).
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
@@ -352,7 +353,7 @@ func (this *UserDAO) FindUserClusterId(tx *dbs.Tx, userId int64) (int64, error)
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUserFeatures 更新用户Features
|
// UpdateUserFeatures 更新单个用户Features
|
||||||
func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON []byte) error {
|
func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON []byte) error {
|
||||||
if userId <= 0 {
|
if userId <= 0 {
|
||||||
return errors.New("invalid userId")
|
return errors.New("invalid userId")
|
||||||
@@ -370,6 +371,74 @@ func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON [
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateUsersFeatures 更新所有用户的Features
|
||||||
|
func (this *UserDAO) UpdateUsersFeatures(tx *dbs.Tx, featureCodes []string, overwrite bool) error {
|
||||||
|
if featureCodes == nil {
|
||||||
|
featureCodes = []string{}
|
||||||
|
}
|
||||||
|
if overwrite {
|
||||||
|
featureCodesJSON, err := json.Marshal(featureCodes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
State(UserStateEnabled).
|
||||||
|
Set("features", featureCodesJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var lastId int64
|
||||||
|
const size = 1000
|
||||||
|
for {
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
Result("id", "features").
|
||||||
|
State(UserStateEnabled).
|
||||||
|
Gt("id", lastId).
|
||||||
|
Limit(size).
|
||||||
|
AscPk().
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
var userId = one.GetInt64("id")
|
||||||
|
var userFeaturesJSON = one.GetBytes("features")
|
||||||
|
var userFeatures = []string{}
|
||||||
|
if len(userFeaturesJSON) > 0 {
|
||||||
|
err = json.Unmarshal(userFeaturesJSON, &userFeatures)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, featureCode := range featureCodes {
|
||||||
|
if !lists.ContainsString(userFeatures, featureCode) {
|
||||||
|
userFeatures = append(userFeatures, featureCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
userFeaturesJSON, err = json.Marshal(userFeatures)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(userId).
|
||||||
|
Set("features", userFeaturesJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ones) < size {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
lastId += size
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// FindUserFeatures 查找用户Features
|
// FindUserFeatures 查找用户Features
|
||||||
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*userconfigs.UserFeature, error) {
|
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*userconfigs.UserFeature, error) {
|
||||||
featuresJSON, err := this.Query(tx).
|
featuresJSON, err := this.Query(tx).
|
||||||
|
|||||||
@@ -1,5 +1,20 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUserDAO_UpdateUserFeatures(t *testing.T) {
|
||||||
|
var dao = NewUserDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := dao.UpdateUsersFeatures(tx, []string{
|
||||||
|
userconfigs.UserFeatureCodeServerACME,
|
||||||
|
}, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,32 +27,34 @@ type User struct {
|
|||||||
RejectReason string `field:"rejectReason"` // 拒绝理由
|
RejectReason string `field:"rejectReason"` // 拒绝理由
|
||||||
IsVerified bool `field:"isVerified"` // 是否验证通过
|
IsVerified bool `field:"isVerified"` // 是否验证通过
|
||||||
RequirePlans uint8 `field:"requirePlans"` // 是否需要购买套餐
|
RequirePlans uint8 `field:"requirePlans"` // 是否需要购买套餐
|
||||||
|
Modules dbs.JSON `field:"modules"` // 用户模块
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserOperator struct {
|
type UserOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Username interface{} // 用户名
|
Username any // 用户名
|
||||||
Password interface{} // 密码
|
Password any // 密码
|
||||||
Fullname interface{} // 真实姓名
|
Fullname any // 真实姓名
|
||||||
Mobile interface{} // 手机号
|
Mobile any // 手机号
|
||||||
Tel interface{} // 联系电话
|
Tel any // 联系电话
|
||||||
Remark interface{} // 备注
|
Remark any // 备注
|
||||||
Email interface{} // 邮箱地址
|
Email any // 邮箱地址
|
||||||
EmailIsVerified interface{} // 邮箱是否已验证
|
EmailIsVerified any // 邮箱是否已验证
|
||||||
AvatarFileId interface{} // 头像文件ID
|
AvatarFileId any // 头像文件ID
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
Day interface{} // YYYYMMDD
|
Day any // YYYYMMDD
|
||||||
UpdatedAt interface{} // 修改时间
|
UpdatedAt any // 修改时间
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
Source interface{} // 来源
|
Source any // 来源
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId any // 集群ID
|
||||||
Features interface{} // 允许操作的特征
|
Features any // 允许操作的特征
|
||||||
RegisteredIP interface{} // 注册使用的IP
|
RegisteredIP any // 注册使用的IP
|
||||||
IsRejected interface{} // 是否已拒绝
|
IsRejected any // 是否已拒绝
|
||||||
RejectReason interface{} // 拒绝理由
|
RejectReason any // 拒绝理由
|
||||||
IsVerified interface{} // 是否验证通过
|
IsVerified any // 是否验证通过
|
||||||
RequirePlans interface{} // 是否需要购买套餐
|
RequirePlans any // 是否需要购买套餐
|
||||||
|
Modules any // 用户模块
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserOperator() *UserOperator {
|
func NewUserOperator() *UserOperator {
|
||||||
|
|||||||
@@ -1 +1,21 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DecodeModules 解析模块
|
||||||
|
func (this *User) DecodeModules() []string {
|
||||||
|
if len(this.Modules) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.Modules, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("User.DecodeModules", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,3 +35,37 @@ func QuoteLikePrefix(keyword string) string {
|
|||||||
func QuoteLikeSuffix(keyword string) string {
|
func QuoteLikeSuffix(keyword string) string {
|
||||||
return "%" + QuoteLikeKeyword(keyword)
|
return "%" + QuoteLikeKeyword(keyword)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetGlobalVarMin 设置变量最小值
|
||||||
|
func SetGlobalVarMin(db *dbs.DB, variableName string, minValue int) error {
|
||||||
|
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name=?", variableName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(result) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var oldValue = result.GetInt("Value")
|
||||||
|
if oldValue > 0 /** 小于等于0通常表示不限制 **/ && oldValue < minValue {
|
||||||
|
_, err = db.Exec("SET GLOBAL "+variableName+"=?", minValue)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetGlobalVarMax 设置变量最大值
|
||||||
|
func SetGlobalVarMax(db *dbs.DB, variableName string, maxValue int) error {
|
||||||
|
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name=?", variableName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(result) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var oldValue = result.GetInt("Value")
|
||||||
|
if oldValue > maxValue {
|
||||||
|
_, err = db.Exec("SET GLOBAL "+variableName+"=?", maxValue)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ func (this *DNSPodProvider) Auth(params maps.Map) error {
|
|||||||
|
|
||||||
// GetDomains 获取所有域名列表
|
// GetDomains 获取所有域名列表
|
||||||
func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
|
func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
|
||||||
offset := 0
|
var offset = 0
|
||||||
size := 100
|
var size = 100
|
||||||
|
|
||||||
for {
|
for {
|
||||||
domainsResp, err := this.post("/Domain.List", map[string]string{
|
domainsResp, err := this.post("/Domain.List", map[string]string{
|
||||||
@@ -68,7 +68,7 @@ func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
|
|||||||
}
|
}
|
||||||
offset += size
|
offset += size
|
||||||
|
|
||||||
domainsSlice := domainsResp.GetSlice("domains")
|
var domainsSlice = domainsResp.GetSlice("domains")
|
||||||
if len(domainsSlice) == 0 {
|
if len(domainsSlice) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -79,8 +79,8 @@ func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否到头
|
// 检查是否到头
|
||||||
info := domainsResp.GetMap("info")
|
var info = domainsResp.GetMap("info")
|
||||||
recordTotal := info.GetInt("record_total")
|
var recordTotal = info.GetInt("all_total")
|
||||||
if offset >= recordTotal {
|
if offset >= recordTotal {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -90,8 +90,8 @@ func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
|
|||||||
|
|
||||||
// GetRecords 获取域名列表
|
// GetRecords 获取域名列表
|
||||||
func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
|
func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
|
||||||
offset := 0
|
var offset = 0
|
||||||
size := 100
|
var size = 100
|
||||||
for {
|
for {
|
||||||
recordsResp, err := this.post("/Record.List", map[string]string{
|
recordsResp, err := this.post("/Record.List", map[string]string{
|
||||||
"domain": domain,
|
"domain": domain,
|
||||||
@@ -104,7 +104,7 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Recor
|
|||||||
offset += size
|
offset += size
|
||||||
|
|
||||||
// 记录
|
// 记录
|
||||||
recordSlice := recordsResp.GetSlice("records")
|
var recordSlice = recordsResp.GetSlice("records")
|
||||||
for _, record := range recordSlice {
|
for _, record := range recordSlice {
|
||||||
recordMap := maps.NewMap(record)
|
recordMap := maps.NewMap(record)
|
||||||
records = append(records, &dnstypes.Record{
|
records = append(records, &dnstypes.Record{
|
||||||
@@ -118,8 +118,8 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Recor
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否到头
|
// 检查是否到头
|
||||||
info := recordsResp.GetMap("info")
|
var info = recordsResp.GetMap("info")
|
||||||
recordTotal := info.GetInt("record_total")
|
var recordTotal = info.GetInt("record_total")
|
||||||
if offset >= recordTotal {
|
if offset >= recordTotal {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
441
internal/dnsclients/provider_edge_dns_api.go
Normal file
441
internal/dnsclients/provider_edge_dns_api.go
Normal file
@@ -0,0 +1,441 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package dnsclients
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/edgeapi"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var edgeDNSHTTPClient = &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type EdgeDNSAPIProvider struct {
|
||||||
|
host string
|
||||||
|
accessKeyId string
|
||||||
|
accessKeySecret string
|
||||||
|
|
||||||
|
role string // admin | user
|
||||||
|
accessToken string
|
||||||
|
accessTokenExpiresAt int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auth 认证
|
||||||
|
func (this *EdgeDNSAPIProvider) Auth(params maps.Map) error {
|
||||||
|
this.role = params.GetString("role")
|
||||||
|
this.host = params.GetString("host")
|
||||||
|
this.accessKeyId = params.GetString("accessKeyId")
|
||||||
|
this.accessKeySecret = params.GetString("accessKeySecret")
|
||||||
|
|
||||||
|
if len(this.role) == 0 {
|
||||||
|
this.role = "user"
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.host) == 0 {
|
||||||
|
return errors.New("'host' should not be empty")
|
||||||
|
}
|
||||||
|
if !regexp.MustCompile(`^(?i)(http|https):`).MatchString(this.host) {
|
||||||
|
this.host = "http://" + this.host
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.accessKeyId) == 0 {
|
||||||
|
return errors.New("'accessKeyId' should not be empty")
|
||||||
|
}
|
||||||
|
if len(this.accessKeySecret) == 0 {
|
||||||
|
return errors.New("'accessKeySecret' should not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDomains 获取所有域名列表
|
||||||
|
func (this *EdgeDNSAPIProvider) GetDomains() (domains []string, err error) {
|
||||||
|
var offset = 0
|
||||||
|
var size = 100
|
||||||
|
for {
|
||||||
|
var resp = &edgeapi.ListNSDomainsResponse{}
|
||||||
|
err = this.doAPI("/NSDomainService/ListNSDomains", map[string]any{
|
||||||
|
"offset": offset,
|
||||||
|
"size": size,
|
||||||
|
}, resp)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, domain := range resp.Data.NSDomains {
|
||||||
|
domains = append(domains, domain.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(resp.Data.NSDomains) < size {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += size
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRecords 获取域名解析记录列表
|
||||||
|
func (this *EdgeDNSAPIProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
|
||||||
|
var domainResp = &edgeapi.FindDomainWithNameResponse{}
|
||||||
|
err = this.doAPI("/NSDomainService/FindNSDomainWithName", map[string]any{
|
||||||
|
"name": domain,
|
||||||
|
}, domainResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var domainId = domainResp.Data.NSDomain.Id
|
||||||
|
if domainId == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var offset = 0
|
||||||
|
var size = 100
|
||||||
|
for {
|
||||||
|
var recordsResp = &edgeapi.ListNSRecordsResponse{}
|
||||||
|
err = this.doAPI("/NSRecordService/ListNSRecords", map[string]any{
|
||||||
|
"nsDomainId": domainId,
|
||||||
|
"offset": offset,
|
||||||
|
"size": size,
|
||||||
|
}, recordsResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var nsRecords = recordsResp.Data.NSRecords
|
||||||
|
for _, record := range nsRecords {
|
||||||
|
var routeCode = this.DefaultRoute()
|
||||||
|
if len(record.NSRoutes) > 0 {
|
||||||
|
routeCode = record.NSRoutes[0].Code
|
||||||
|
}
|
||||||
|
|
||||||
|
records = append(records, &dnstypes.Record{
|
||||||
|
Id: types.String(record.Id),
|
||||||
|
Name: record.Name,
|
||||||
|
Type: record.Type,
|
||||||
|
Value: record.Value,
|
||||||
|
Route: routeCode,
|
||||||
|
TTL: record.TTL,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(nsRecords) < size {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += size
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRoutes 读取域名支持的线路数据
|
||||||
|
func (this *EdgeDNSAPIProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
|
||||||
|
// default
|
||||||
|
routes = append(routes, &dnstypes.Route{
|
||||||
|
Name: "默认线路",
|
||||||
|
Code: this.DefaultRoute(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// 世界区域
|
||||||
|
{
|
||||||
|
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
|
||||||
|
err = this.doAPI("/NSRouteService/FindAllDefaultWorldRegionRoutes", map[string]any{}, routesResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, route := range routesResp.Data.NSRoutes {
|
||||||
|
routes = append(routes, &dnstypes.Route{
|
||||||
|
Name: route.Name,
|
||||||
|
Code: route.Code,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 中国省份
|
||||||
|
{
|
||||||
|
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
|
||||||
|
err = this.doAPI("/NSRouteService/FindAllDefaultChinaProvinceRoutes", map[string]any{}, routesResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, route := range routesResp.Data.NSRoutes {
|
||||||
|
routes = append(routes, &dnstypes.Route{
|
||||||
|
Name: route.Name,
|
||||||
|
Code: route.Code,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ISP
|
||||||
|
{
|
||||||
|
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
|
||||||
|
err = this.doAPI("/NSRouteService/FindAllDefaultISPRoutes", map[string]any{}, routesResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, route := range routesResp.Data.NSRoutes {
|
||||||
|
routes = append(routes, &dnstypes.Route{
|
||||||
|
Name: route.Name,
|
||||||
|
Code: route.Code,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自定义
|
||||||
|
{
|
||||||
|
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
|
||||||
|
err = this.doAPI("/NSRouteService/FindAllNSRoutes", map[string]any{}, routesResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, route := range routesResp.Data.NSRoutes {
|
||||||
|
routes = append(routes, &dnstypes.Route{
|
||||||
|
Name: route.Name,
|
||||||
|
Code: route.Code,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRecord 查询单个记录
|
||||||
|
func (this *EdgeDNSAPIProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
|
||||||
|
var domainResp = &edgeapi.FindDomainWithNameResponse{}
|
||||||
|
err := this.doAPI("/NSDomainService/FindNSDomainWithName", map[string]any{
|
||||||
|
"name": domain,
|
||||||
|
}, domainResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var domainId = domainResp.Data.NSDomain.Id
|
||||||
|
if domainId == 0 {
|
||||||
|
return nil, errors.New("can not find domain '" + domain + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
var recordResp = &edgeapi.FindNSRecordWithNameAndTypeResponse{}
|
||||||
|
err = this.doAPI("/NSRecordService/FindNSRecordWithNameAndType", map[string]any{
|
||||||
|
"nsDomainId": domainId,
|
||||||
|
"name": name,
|
||||||
|
"type": recordType,
|
||||||
|
}, recordResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var record = recordResp.Data.NSRecord
|
||||||
|
if record.Id <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var routeCode = this.DefaultRoute()
|
||||||
|
if len(record.NSRoutes) > 0 {
|
||||||
|
routeCode = record.NSRoutes[0].Code
|
||||||
|
}
|
||||||
|
|
||||||
|
return &dnstypes.Record{
|
||||||
|
Id: types.String(record.Id),
|
||||||
|
Name: record.Name,
|
||||||
|
Type: record.Type,
|
||||||
|
Value: record.Value,
|
||||||
|
Route: routeCode,
|
||||||
|
TTL: record.TTL,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddRecord 设置记录
|
||||||
|
func (this *EdgeDNSAPIProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
|
||||||
|
var domainResp = &edgeapi.FindDomainWithNameResponse{}
|
||||||
|
err := this.doAPI("/NSDomainService/FindNSDomainWithName", map[string]any{
|
||||||
|
"name": domain,
|
||||||
|
}, domainResp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var domainId = domainResp.Data.NSDomain.Id
|
||||||
|
if domainId == 0 {
|
||||||
|
return errors.New("can not find domain '" + domain + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
|
||||||
|
newRecord.Value += "."
|
||||||
|
}
|
||||||
|
|
||||||
|
var createResp = &edgeapi.CreateNSRecordResponse{}
|
||||||
|
var routes = []string{}
|
||||||
|
if len(newRecord.Route) > 0 {
|
||||||
|
routes = []string{newRecord.Route}
|
||||||
|
}
|
||||||
|
err = this.doAPI("/NSRecordService/CreateNSRecord", map[string]any{
|
||||||
|
"nsDomainId": domainId,
|
||||||
|
"name": newRecord.Name,
|
||||||
|
"type": strings.ToUpper(newRecord.Type),
|
||||||
|
"value": newRecord.Value,
|
||||||
|
"ttl": newRecord.TTL,
|
||||||
|
"nsRouteCodes": routes,
|
||||||
|
}, createResp)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newRecord.Id = types.String(createResp.Data.NSRecordId)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateRecord 修改记录
|
||||||
|
func (this *EdgeDNSAPIProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
|
||||||
|
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
|
||||||
|
newRecord.Value += "."
|
||||||
|
}
|
||||||
|
|
||||||
|
var createResp = &edgeapi.UpdateNSRecordResponse{}
|
||||||
|
var routes = []string{}
|
||||||
|
if len(newRecord.Route) > 0 {
|
||||||
|
routes = []string{newRecord.Route}
|
||||||
|
}
|
||||||
|
err := this.doAPI("/NSRecordService/UpdateNSRecord", map[string]any{
|
||||||
|
"nsRecordId": types.Int64(record.Id),
|
||||||
|
"name": newRecord.Name,
|
||||||
|
"type": strings.ToUpper(newRecord.Type),
|
||||||
|
"value": newRecord.Value,
|
||||||
|
"ttl": newRecord.TTL,
|
||||||
|
"nsRouteCodes": routes,
|
||||||
|
"isOn": true, // important
|
||||||
|
}, createResp)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteRecord 删除记录
|
||||||
|
func (this *EdgeDNSAPIProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
|
||||||
|
var resp = &edgeapi.SuccessResponse{}
|
||||||
|
err := this.doAPI("/NSRecordService/DeleteNSRecord", map[string]any{
|
||||||
|
"nsRecordId": types.Int64(record.Id),
|
||||||
|
}, resp)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultRoute 默认线路
|
||||||
|
func (this *EdgeDNSAPIProvider) DefaultRoute() string {
|
||||||
|
return "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *EdgeDNSAPIProvider) doAPI(path string, params map[string]any, respPtr edgeapi.ResponseInterface) error {
|
||||||
|
accessToken, err := this.getToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
paramsJSON, err := json.Marshal(params)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(http.MethodPost, this.host+path, bytes.NewReader(paramsJSON))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
|
||||||
|
req.Header.Set("X-Edge-Access-Token", accessToken)
|
||||||
|
|
||||||
|
resp, err := edgeDNSHTTPClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return errors.New("invalid response status code '" + types.String(resp.StatusCode) + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data, respPtr)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("decode response failed: " + err.Error() + ", JSON: " + string(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !respPtr.IsValid() {
|
||||||
|
return respPtr.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *EdgeDNSAPIProvider) getToken() (string, error) {
|
||||||
|
if len(this.accessToken) > 0 && this.accessTokenExpiresAt > time.Now().Unix()+600 /** 600秒是防止当前服务器和API服务器之间有时间差 **/ {
|
||||||
|
return this.accessToken, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var params = maps.Map{
|
||||||
|
"type": this.role,
|
||||||
|
"accessKeyId": this.accessKeyId,
|
||||||
|
"accessKey": this.accessKeySecret,
|
||||||
|
}
|
||||||
|
paramsJSON, err := json.Marshal(params)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(http.MethodPost, this.host+"/APIAccessTokenService/getAPIAccessToken", bytes.NewReader(paramsJSON))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
|
||||||
|
resp, err := edgeDNSHTTPClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return "", errors.New("invalid response code '" + types.String(resp.StatusCode) + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var tokenResp = &edgeapi.GetAPIAccessToken{}
|
||||||
|
err = json.Unmarshal(data, tokenResp)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if tokenResp.Code != 200 {
|
||||||
|
return "", errors.New("invalid code '" + types.String(tokenResp.Code) + "', message: " + tokenResp.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.accessToken = tokenResp.Data.Token
|
||||||
|
this.accessTokenExpiresAt = tokenResp.Data.ExpiresAt
|
||||||
|
return this.accessToken, nil
|
||||||
|
}
|
||||||
163
internal/dnsclients/provider_edge_dns_api_test.go
Normal file
163
internal/dnsclients/provider_edge_dns_api_test.go
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package dnsclients_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const edgeDNSAPIDomainName = "hello2.com"
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_GetDomains(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
domains, err := provider.GetDomains()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("domains:", domains)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_GetRecords(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
records, err := provider.GetRecords(edgeDNSAPIDomainName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
logs.PrintAsJSON(records, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_GetRoutes(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
routes, err := provider.GetRoutes(edgeDNSAPIDomainName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
logs.PrintAsJSON(routes, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_QueryRecord(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
record, err := provider.QueryRecord(edgeDNSAPIDomainName, "cdn", dnstypes.RecordTypeA)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
logs.PrintAsJSON(record)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_AddRecord(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = provider.AddRecord(edgeDNSAPIDomainName, &dnstypes.Record{
|
||||||
|
Id: "",
|
||||||
|
Name: "example",
|
||||||
|
Type: dnstypes.RecordTypeA,
|
||||||
|
Value: "10.0.0.1",
|
||||||
|
Route: "china:province:beijing",
|
||||||
|
TTL: 300,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_UpdateRecord(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
record, err := provider.QueryRecord(edgeDNSAPIDomainName, "cdn", dnstypes.RecordTypeA)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if record == nil {
|
||||||
|
t.Log("not found record")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//record.Id = ""
|
||||||
|
err = provider.UpdateRecord(edgeDNSAPIDomainName, record, &dnstypes.Record{
|
||||||
|
Id: "",
|
||||||
|
Name: record.Name,
|
||||||
|
Type: record.Type,
|
||||||
|
Value: "127.0.0.3",
|
||||||
|
Route: record.Route,
|
||||||
|
TTL: 30,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_DeleteRecord(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
record, err := provider.QueryRecord(edgeDNSAPIDomainName, "example", "A")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if record == nil {
|
||||||
|
t.Log("not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = provider.DeleteRecord(edgeDNSAPIDomainName, &dnstypes.Record{
|
||||||
|
Id: record.Id,
|
||||||
|
Name: "example",
|
||||||
|
Type: "A",
|
||||||
|
Value: "",
|
||||||
|
Route: "",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgeDNSAPIProvider_DefaultRoute(t *testing.T) {
|
||||||
|
provider, err := testEdgeDNSAPIProvider()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log(provider.DefaultRoute())
|
||||||
|
}
|
||||||
|
|
||||||
|
func testEdgeDNSAPIProvider() (dnsclients.ProviderInterface, error) {
|
||||||
|
provider := &dnsclients.EdgeDNSAPIProvider{}
|
||||||
|
err := provider.Auth(maps.Map{
|
||||||
|
"role": "user",
|
||||||
|
"host": "http://127.0.0.1:8004",
|
||||||
|
"accessKeyId": "JOvsyXIFqkQbh5kl",
|
||||||
|
"accessKeySecret": "t0RY8YO3R58VbJJNp0RqKw9KWNpObwtE",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return provider, nil
|
||||||
|
}
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package dnsclients
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UserEdgeDNSProvider struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auth 认证
|
|
||||||
func (this *UserEdgeDNSProvider) Auth(params maps.Map) error {
|
|
||||||
// TODO
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDomains 获取所有域名列表
|
|
||||||
func (this *UserEdgeDNSProvider) GetDomains() (domains []string, err error) {
|
|
||||||
// TODO
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRecords 获取域名解析记录列表
|
|
||||||
func (this *UserEdgeDNSProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
|
|
||||||
// TODO
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRoutes 读取域名支持的线路数据
|
|
||||||
func (this *UserEdgeDNSProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
|
|
||||||
// TODO
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryRecord 查询单个记录
|
|
||||||
func (this *UserEdgeDNSProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
|
|
||||||
// TODO
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddRecord 设置记录
|
|
||||||
func (this *UserEdgeDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
|
|
||||||
// TODO
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateRecord 修改记录
|
|
||||||
func (this *UserEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
|
|
||||||
// TODO
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteRecord 删除记录
|
|
||||||
func (this *UserEdgeDNSProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
|
|
||||||
// TODO
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultRoute 默认线路
|
|
||||||
func (this *UserEdgeDNSProvider) DefaultRoute() string {
|
|
||||||
// TODO
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
@@ -13,13 +13,13 @@ const (
|
|||||||
ProviderTypeHuaweiDNS ProviderType = "huaweiDNS" // 华为DNS
|
ProviderTypeHuaweiDNS ProviderType = "huaweiDNS" // 华为DNS
|
||||||
ProviderTypeCloudFlare ProviderType = "cloudFlare" // CloudFlare DNS
|
ProviderTypeCloudFlare ProviderType = "cloudFlare" // CloudFlare DNS
|
||||||
ProviderTypeLocalEdgeDNS ProviderType = "localEdgeDNS" // 和当前系统集成的EdgeDNS
|
ProviderTypeLocalEdgeDNS ProviderType = "localEdgeDNS" // 和当前系统集成的EdgeDNS
|
||||||
ProviderTypeUserEdgeDNS ProviderType = "userEdgeDNS" // 通过API连接的EdgeDNS
|
ProviderTypeEdgeDNSAPI ProviderType = "edgeDNSAPI" // 通过API连接的EdgeDNS
|
||||||
ProviderTypeCustomHTTP ProviderType = "customHTTP" // 自定义HTTP接口
|
ProviderTypeCustomHTTP ProviderType = "customHTTP" // 自定义HTTP接口
|
||||||
)
|
)
|
||||||
|
|
||||||
// FindAllProviderTypes 所有的服务商类型
|
// FindAllProviderTypes 所有的服务商类型
|
||||||
func FindAllProviderTypes() []maps.Map {
|
func FindAllProviderTypes() []maps.Map {
|
||||||
typeMaps := []maps.Map{
|
var typeMaps = []maps.Map{
|
||||||
{
|
{
|
||||||
"name": "阿里云DNS",
|
"name": "阿里云DNS",
|
||||||
"code": ProviderTypeAliDNS,
|
"code": ProviderTypeAliDNS,
|
||||||
@@ -40,6 +40,11 @@ func FindAllProviderTypes() []maps.Map {
|
|||||||
"code": ProviderTypeCloudFlare,
|
"code": ProviderTypeCloudFlare,
|
||||||
"description": "CloudFlare提供的DNS服务。",
|
"description": "CloudFlare提供的DNS服务。",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "EdgeDNS API",
|
||||||
|
"code": ProviderTypeEdgeDNSAPI,
|
||||||
|
"description": "通过API连接GoEdge商业版系统提供的DNS服务。",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
typeMaps = filterTypeMaps(typeMaps)
|
typeMaps = filterTypeMaps(typeMaps)
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ func FindProvider(providerType ProviderType) ProviderInterface {
|
|||||||
return &CloudFlareProvider{}
|
return &CloudFlareProvider{}
|
||||||
case ProviderTypeCustomHTTP:
|
case ProviderTypeCustomHTTP:
|
||||||
return &CustomHTTPProvider{}
|
return &CustomHTTPProvider{}
|
||||||
|
case ProviderTypeEdgeDNSAPI:
|
||||||
|
return &EdgeDNSAPIProvider{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
27
internal/installers/errors.go
Normal file
27
internal/installers/errors.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
|
||||||
|
package installers
|
||||||
|
|
||||||
|
type GrantError struct {
|
||||||
|
err string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newGrantError(err string) *GrantError {
|
||||||
|
return &GrantError{err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *GrantError) Error() string {
|
||||||
|
return this.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *GrantError) String() string {
|
||||||
|
return this.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsGrantError(err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, ok := err.(*GrantError)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
@@ -42,10 +42,10 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 认证
|
// 认证
|
||||||
methods := []ssh.AuthMethod{}
|
var methods = []ssh.AuthMethod{}
|
||||||
if credentials.Method == "user" {
|
if credentials.Method == "user" {
|
||||||
{
|
{
|
||||||
authMethod := ssh.Password(credentials.Password)
|
var authMethod = ssh.Password(credentials.Password)
|
||||||
methods = append(methods, authMethod)
|
methods = append(methods, authMethod)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
|
|||||||
if len(credentials.Username) == 0 {
|
if len(credentials.Username) == 0 {
|
||||||
credentials.Username = "root"
|
credentials.Username = "root"
|
||||||
}
|
}
|
||||||
config := &ssh.ClientConfig{
|
var config = &ssh.ClientConfig{
|
||||||
User: credentials.Username,
|
User: credentials.Username,
|
||||||
Auth: methods,
|
Auth: methods,
|
||||||
HostKeyCallback: hostKeyCallback,
|
HostKeyCallback: hostKeyCallback,
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ secret: "${nodeSecret}"`)
|
|||||||
_, stderr, err = this.client.Exec(dir + "/edge-node/bin/edge-node test")
|
_, stderr, err = this.client.Exec(dir + "/edge-node/bin/edge-node test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
installStatus.ErrorCode = "TEST_FAILED"
|
installStatus.ErrorCode = "TEST_FAILED"
|
||||||
return errors.New("test edge node failed: " + err.Error())
|
return errors.New("test edge node failed: " + err.Error() + ", stderr: " + stderr)
|
||||||
}
|
}
|
||||||
if len(stderr) > 0 {
|
if len(stderr) > 0 {
|
||||||
if regexp.MustCompile(`(?i)rpc`).MatchString(stderr) {
|
if regexp.MustCompile(`(?i)rpc`).MatchString(stderr) {
|
||||||
|
|||||||
@@ -1,147 +0,0 @@
|
|||||||
package installers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSNodeInstaller struct {
|
|
||||||
BaseInstaller
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *NSNodeInstaller) Install(dir string, params interface{}, installStatus *models.NodeInstallStatus) error {
|
|
||||||
if params == nil {
|
|
||||||
return errors.New("'params' required for node installation")
|
|
||||||
}
|
|
||||||
nodeParams, ok := params.(*NodeParams)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("'params' should be *NodeParams")
|
|
||||||
}
|
|
||||||
err := nodeParams.Validate()
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("params validation: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查目标目录是否存在
|
|
||||||
_, err = this.client.Stat(dir)
|
|
||||||
if err != nil {
|
|
||||||
err = this.client.MkdirAll(dir)
|
|
||||||
if err != nil {
|
|
||||||
installStatus.ErrorCode = "CREATE_ROOT_DIRECTORY_FAILED"
|
|
||||||
return errors.New("create directory '" + dir + "' failed: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 安装助手
|
|
||||||
env, err := this.InstallHelper(dir, nodeconfigs.NodeRoleDNS)
|
|
||||||
if err != nil {
|
|
||||||
installStatus.ErrorCode = "INSTALL_HELPER_FAILED"
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 上传安装文件
|
|
||||||
filePrefix := "edge-dns-" + env.OS + "-" + env.Arch
|
|
||||||
zipFile, err := this.LookupLatestInstaller(filePrefix)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(zipFile) == 0 {
|
|
||||||
return errors.New("can not find installer file for " + env.OS + "/" + env.Arch)
|
|
||||||
}
|
|
||||||
targetZip := dir + "/" + filepath.Base(zipFile)
|
|
||||||
err = this.client.Copy(zipFile, targetZip, 0777)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 测试运行环境
|
|
||||||
// 升级的节点暂时不列入测试
|
|
||||||
if !nodeParams.IsUpgrading {
|
|
||||||
_, stderr, err := this.client.Exec(dir + "/" + env.HelperName + " -cmd=test")
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("test failed: " + err.Error())
|
|
||||||
}
|
|
||||||
if len(stderr) > 0 {
|
|
||||||
return errors.New("test failed: " + stderr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是升级则优雅停止先前的进程
|
|
||||||
exePath := dir + "/edge-dns/bin/edge-dns"
|
|
||||||
if nodeParams.IsUpgrading {
|
|
||||||
_, err = this.client.Stat(exePath)
|
|
||||||
if err == nil {
|
|
||||||
_, _, _ = this.client.Exec(exePath + " stop")
|
|
||||||
|
|
||||||
// 删除可执行文件防止冲突
|
|
||||||
err = this.client.Remove(exePath)
|
|
||||||
if err != nil && err != os.ErrNotExist {
|
|
||||||
return errors.New("remove old file failed: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解压
|
|
||||||
_, stderr, err := this.client.Exec(dir + "/" + env.HelperName + " -cmd=unzip -zip=\"" + targetZip + "\" -target=\"" + dir + "\"")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(stderr) > 0 {
|
|
||||||
return errors.New("unzip installer failed: " + stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 修改配置文件
|
|
||||||
{
|
|
||||||
configFile := dir + "/edge-dns/configs/api.yaml"
|
|
||||||
|
|
||||||
// sudo之后我们需要修改配置目录才能写入文件
|
|
||||||
if this.client.sudo {
|
|
||||||
_, _, _ = this.client.Exec("chown " + this.client.User() + " " + filepath.Dir(configFile))
|
|
||||||
}
|
|
||||||
|
|
||||||
var data = []byte(`rpc:
|
|
||||||
endpoints: [ ${endpoints} ]
|
|
||||||
nodeId: "${nodeId}"
|
|
||||||
secret: "${nodeSecret}"`)
|
|
||||||
|
|
||||||
data = bytes.ReplaceAll(data, []byte("${endpoints}"), []byte(nodeParams.QuoteEndpoints()))
|
|
||||||
data = bytes.ReplaceAll(data, []byte("${nodeId}"), []byte(nodeParams.NodeId))
|
|
||||||
data = bytes.ReplaceAll(data, []byte("${nodeSecret}"), []byte(nodeParams.Secret))
|
|
||||||
|
|
||||||
_, err = this.client.WriteFile(configFile, data)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("write '" + configFile + "': " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 测试
|
|
||||||
_, stderr, err = this.client.Exec(dir + "/edge-dns/bin/edge-dns test")
|
|
||||||
if err != nil {
|
|
||||||
installStatus.ErrorCode = "TEST_FAILED"
|
|
||||||
return errors.New("test edge node failed: " + err.Error())
|
|
||||||
}
|
|
||||||
if len(stderr) > 0 {
|
|
||||||
if regexp.MustCompile(`(?i)rpc`).MatchString(stderr) {
|
|
||||||
installStatus.ErrorCode = "RPC_TEST_FAILED"
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors.New("test edge dns node failed: " + stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 启动
|
|
||||||
_, stderr, err = this.client.Exec(dir + "/edge-dns/bin/edge-dns start")
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("start edge dns failed: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(stderr) > 0 {
|
|
||||||
return errors.New("start edge dns failed: " + stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package installers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestDNSNodeInstaller_Install(t *testing.T) {
|
|
||||||
var installer InstallerInterface = &NSNodeInstaller{}
|
|
||||||
err := installer.Login(&Credentials{
|
|
||||||
Host: "192.168.2.30",
|
|
||||||
Port: 22,
|
|
||||||
Username: "root",
|
|
||||||
Password: "123456",
|
|
||||||
PrivateKey: "",
|
|
||||||
Method: "user",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭连接
|
|
||||||
defer func() {
|
|
||||||
err := installer.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 安装
|
|
||||||
err = installer.Install("/opt/edge", &NodeParams{
|
|
||||||
Endpoints: []string{"http://192.168.2.40:8003"},
|
|
||||||
NodeId: "b3f0690c793db5daaa666e89bd7b2301",
|
|
||||||
Secret: "H6nbSzjN3tLYi0ecdtUeDpQdZZPjKL7S",
|
|
||||||
}, &models.NodeInstallStatus{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -218,19 +218,19 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if login == nil {
|
if login == nil {
|
||||||
return errors.New("can not find node login information")
|
return newGrantError("can not find node login information")
|
||||||
}
|
}
|
||||||
loginParams, err := login.DecodeSSHParams()
|
loginParams, err := login.DecodeSSHParams()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return newGrantError(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(loginParams.Host) == 0 {
|
if len(loginParams.Host) == 0 {
|
||||||
return errors.New("ssh host should not be empty")
|
return newGrantError("ssh host should not be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if loginParams.Port <= 0 {
|
if loginParams.Port <= 0 {
|
||||||
return errors.New("ssh port is invalid")
|
return newGrantError("ssh port is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
if loginParams.GrantId == 0 {
|
if loginParams.GrantId == 0 {
|
||||||
@@ -240,7 +240,7 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if grantId == 0 {
|
if grantId == 0 {
|
||||||
return errors.New("can not find node grant")
|
return newGrantError("can not find node grant")
|
||||||
}
|
}
|
||||||
loginParams.GrantId = grantId
|
loginParams.GrantId = grantId
|
||||||
}
|
}
|
||||||
@@ -249,10 +249,10 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if grant == nil {
|
if grant == nil {
|
||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
return newGrantError("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
installer := &NodeInstaller{}
|
var installer = &NodeInstaller{}
|
||||||
err = installer.Login(&Credentials{
|
err = installer.Login(&Credentials{
|
||||||
Host: loginParams.Host,
|
Host: loginParams.Host,
|
||||||
Port: loginParams.Port,
|
Port: loginParams.Port,
|
||||||
|
|||||||
@@ -1,417 +0,0 @@
|
|||||||
package installers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var sharedNSNodeQueue = NewNSNodeQueue()
|
|
||||||
|
|
||||||
type NSNodeQueue struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewNSNodeQueue() *NSNodeQueue {
|
|
||||||
return &NSNodeQueue{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SharedNSNodeQueue() *NSNodeQueue {
|
|
||||||
return sharedNSNodeQueue
|
|
||||||
}
|
|
||||||
|
|
||||||
// InstallNodeProcess 安装边缘节点流程控制
|
|
||||||
func (this *NSNodeQueue) InstallNodeProcess(nodeId int64, isUpgrading bool) error {
|
|
||||||
installStatus := models.NewNodeInstallStatus()
|
|
||||||
installStatus.IsRunning = true
|
|
||||||
installStatus.UpdatedAt = time.Now().Unix()
|
|
||||||
|
|
||||||
err := models.SharedNSNodeDAO.UpdateNodeInstallStatus(nil, nodeId, installStatus)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新时间
|
|
||||||
ticker := utils.NewTicker(3 * time.Second)
|
|
||||||
goman.New(func() {
|
|
||||||
for ticker.Wait() {
|
|
||||||
installStatus.UpdatedAt = time.Now().Unix()
|
|
||||||
err := models.SharedNSNodeDAO.UpdateNodeInstallStatus(nil, nodeId, installStatus)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[INSTALL]" + err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
defer func() {
|
|
||||||
ticker.Stop()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 开始安装
|
|
||||||
err = this.InstallNode(nodeId, installStatus, isUpgrading)
|
|
||||||
|
|
||||||
// 安装结束
|
|
||||||
installStatus.IsRunning = false
|
|
||||||
installStatus.IsFinished = true
|
|
||||||
if err != nil {
|
|
||||||
installStatus.Error = err.Error()
|
|
||||||
} else {
|
|
||||||
installStatus.IsOk = true
|
|
||||||
}
|
|
||||||
err = models.SharedNSNodeDAO.UpdateNodeInstallStatus(nil, nodeId, installStatus)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 修改为已安装
|
|
||||||
if installStatus.IsOk {
|
|
||||||
err = models.SharedNSNodeDAO.UpdateNodeIsInstalled(nil, nodeId, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// InstallNode 安装边缘节点
|
|
||||||
func (this *NSNodeQueue) InstallNode(nodeId int64, installStatus *models.NodeInstallStatus, isUpgrading bool) error {
|
|
||||||
node, err := models.SharedNSNodeDAO.FindEnabledNSNode(nil, nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if node == nil {
|
|
||||||
return errors.New("can not find node, ID:'" + numberutils.FormatInt64(nodeId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 登录信息
|
|
||||||
login, err := models.SharedNodeLoginDAO.FindEnabledNodeLoginWithNodeId(nil, nodeconfigs.NodeRoleDNS, nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if login == nil {
|
|
||||||
installStatus.ErrorCode = "EMPTY_LOGIN"
|
|
||||||
return errors.New("can not find node login information")
|
|
||||||
}
|
|
||||||
loginParams, err := login.DecodeSSHParams()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(loginParams.Host) == 0 {
|
|
||||||
installStatus.ErrorCode = "EMPTY_SSH_HOST"
|
|
||||||
return errors.New("ssh host should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if loginParams.Port <= 0 {
|
|
||||||
installStatus.ErrorCode = "EMPTY_SSH_PORT"
|
|
||||||
return errors.New("ssh port is invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
if loginParams.GrantId == 0 {
|
|
||||||
// 从集群中读取
|
|
||||||
grantId, err := models.SharedNSClusterDAO.FindClusterGrantId(nil, int64(node.ClusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if grantId == 0 {
|
|
||||||
installStatus.ErrorCode = "EMPTY_GRANT"
|
|
||||||
return errors.New("can not find node grant")
|
|
||||||
}
|
|
||||||
loginParams.GrantId = grantId
|
|
||||||
}
|
|
||||||
grant, err := models.SharedNodeGrantDAO.FindEnabledNodeGrant(nil, loginParams.GrantId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if grant == nil {
|
|
||||||
installStatus.ErrorCode = "EMPTY_GRANT"
|
|
||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// API终端
|
|
||||||
apiNodes, err := models.SharedAPINodeDAO.FindAllEnabledAndOnAPINodes(nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(apiNodes) == 0 {
|
|
||||||
return errors.New("no available api nodes")
|
|
||||||
}
|
|
||||||
|
|
||||||
apiEndpoints := []string{}
|
|
||||||
for _, apiNode := range apiNodes {
|
|
||||||
addrConfigs, err := apiNode.DecodeAccessAddrs()
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("decode api node access addresses failed: " + err.Error())
|
|
||||||
}
|
|
||||||
for _, addrConfig := range addrConfigs {
|
|
||||||
apiEndpoints = append(apiEndpoints, addrConfig.FullAddresses()...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
params := &NodeParams{
|
|
||||||
Endpoints: apiEndpoints,
|
|
||||||
NodeId: node.UniqueId,
|
|
||||||
Secret: node.Secret,
|
|
||||||
IsUpgrading: isUpgrading,
|
|
||||||
}
|
|
||||||
|
|
||||||
installer := &NSNodeInstaller{}
|
|
||||||
err = installer.Login(&Credentials{
|
|
||||||
Host: loginParams.Host,
|
|
||||||
Port: loginParams.Port,
|
|
||||||
Username: grant.Username,
|
|
||||||
Password: grant.Password,
|
|
||||||
PrivateKey: grant.PrivateKey,
|
|
||||||
Passphrase: grant.Passphrase,
|
|
||||||
Method: grant.Method,
|
|
||||||
Sudo: grant.Su == 1,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
installStatus.ErrorCode = "SSH_LOGIN_FAILED"
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = installer.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 安装目录
|
|
||||||
installDir := node.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
clusterId := node.ClusterId
|
|
||||||
cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(nil, int64(clusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cluster == nil {
|
|
||||||
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
|
||||||
}
|
|
||||||
installDir = cluster.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
// 默认是 $登录用户/edge-dns
|
|
||||||
installDir = installer.client.UserHome() + "/edge-dns"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = installer.Install(installDir, params, installStatus)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartNode 启动边缘节点
|
|
||||||
func (this *NSNodeQueue) StartNode(nodeId int64) error {
|
|
||||||
node, err := models.SharedNSNodeDAO.FindEnabledNSNode(nil, nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if node == nil {
|
|
||||||
return errors.New("can not find node, ID:'" + numberutils.FormatInt64(nodeId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 登录信息
|
|
||||||
login, err := models.SharedNodeLoginDAO.FindEnabledNodeLoginWithNodeId(nil, nodeconfigs.NodeRoleDNS, nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if login == nil {
|
|
||||||
return errors.New("can not find node login information")
|
|
||||||
}
|
|
||||||
loginParams, err := login.DecodeSSHParams()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(loginParams.Host) == 0 {
|
|
||||||
return errors.New("ssh host should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if loginParams.Port <= 0 {
|
|
||||||
return errors.New("ssh port is invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
if loginParams.GrantId == 0 {
|
|
||||||
// 从集群中读取
|
|
||||||
grantId, err := models.SharedNSClusterDAO.FindClusterGrantId(nil, int64(node.ClusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if grantId == 0 {
|
|
||||||
return errors.New("can not find node grant")
|
|
||||||
}
|
|
||||||
loginParams.GrantId = grantId
|
|
||||||
}
|
|
||||||
grant, err := models.SharedNodeGrantDAO.FindEnabledNodeGrant(nil, loginParams.GrantId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if grant == nil {
|
|
||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
installer := &NSNodeInstaller{}
|
|
||||||
err = installer.Login(&Credentials{
|
|
||||||
Host: loginParams.Host,
|
|
||||||
Port: loginParams.Port,
|
|
||||||
Username: grant.Username,
|
|
||||||
Password: grant.Password,
|
|
||||||
PrivateKey: grant.PrivateKey,
|
|
||||||
Passphrase: grant.Passphrase,
|
|
||||||
Method: grant.Method,
|
|
||||||
Sudo: grant.Su == 1,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = installer.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 安装目录
|
|
||||||
installDir := node.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
clusterId := node.ClusterId
|
|
||||||
cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(nil, int64(clusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cluster == nil {
|
|
||||||
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
|
||||||
}
|
|
||||||
installDir = cluster.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
// 默认是 $登录用户/edge-dns
|
|
||||||
installDir = installer.client.UserHome() + "/edge-dns"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查命令是否存在
|
|
||||||
exeFile := installDir + "/edge-dns/bin/edge-dns"
|
|
||||||
_, err = installer.client.Stat(exeFile)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("edge node is not installed correctly, can not find executable file: " + exeFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 我们先尝试Systemd启动
|
|
||||||
_, _, _ = installer.client.Exec("systemctl start edge-dns")
|
|
||||||
|
|
||||||
_, stderr, err := installer.client.Exec(exeFile + " start")
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("start failed: " + err.Error())
|
|
||||||
}
|
|
||||||
if len(stderr) > 0 {
|
|
||||||
return errors.New("start failed: " + stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// StopNode 停止节点
|
|
||||||
func (this *NSNodeQueue) StopNode(nodeId int64) error {
|
|
||||||
node, err := models.SharedNSNodeDAO.FindEnabledNSNode(nil, nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if node == nil {
|
|
||||||
return errors.New("can not find node, ID:'" + numberutils.FormatInt64(nodeId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 登录信息
|
|
||||||
login, err := models.SharedNodeLoginDAO.FindEnabledNodeLoginWithNodeId(nil, nodeconfigs.NodeRoleDNS, nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if login == nil {
|
|
||||||
return errors.New("can not find node login information")
|
|
||||||
}
|
|
||||||
loginParams, err := login.DecodeSSHParams()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(loginParams.Host) == 0 {
|
|
||||||
return errors.New("ssh host should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if loginParams.Port <= 0 {
|
|
||||||
return errors.New("ssh port is invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
if loginParams.GrantId == 0 {
|
|
||||||
// 从集群中读取
|
|
||||||
grantId, err := models.SharedNSClusterDAO.FindClusterGrantId(nil, int64(node.ClusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if grantId == 0 {
|
|
||||||
return errors.New("can not find node grant")
|
|
||||||
}
|
|
||||||
loginParams.GrantId = grantId
|
|
||||||
}
|
|
||||||
grant, err := models.SharedNodeGrantDAO.FindEnabledNodeGrant(nil, loginParams.GrantId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if grant == nil {
|
|
||||||
return errors.New("can not find user grant with id '" + numberutils.FormatInt64(loginParams.GrantId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
installer := &NSNodeInstaller{}
|
|
||||||
err = installer.Login(&Credentials{
|
|
||||||
Host: loginParams.Host,
|
|
||||||
Port: loginParams.Port,
|
|
||||||
Username: grant.Username,
|
|
||||||
Password: grant.Password,
|
|
||||||
PrivateKey: grant.PrivateKey,
|
|
||||||
Passphrase: grant.Passphrase,
|
|
||||||
Method: grant.Method,
|
|
||||||
Sudo: grant.Su == 1,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = installer.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 安装目录
|
|
||||||
installDir := node.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
clusterId := node.ClusterId
|
|
||||||
cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(nil, int64(clusterId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cluster == nil {
|
|
||||||
return errors.New("can not find cluster, ID:'" + fmt.Sprintf("%d", clusterId) + "'")
|
|
||||||
}
|
|
||||||
installDir = cluster.InstallDir
|
|
||||||
if len(installDir) == 0 {
|
|
||||||
// 默认是 $登录用户/edge-dns
|
|
||||||
installDir = installer.client.UserHome() + "/edge-dns"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查命令是否存在
|
|
||||||
exeFile := installDir + "/edge-dns/bin/edge-dns"
|
|
||||||
_, err = installer.client.Stat(exeFile)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("edge node is not installed correctly, can not find executable file: " + exeFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 我们先尝试Systemd停止
|
|
||||||
_, _, _ = installer.client.Exec("systemctl stop edge-dns")
|
|
||||||
|
|
||||||
_, stderr, err := installer.client.Exec(exeFile + " stop")
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("stop failed: " + err.Error())
|
|
||||||
}
|
|
||||||
if len(stderr) > 0 {
|
|
||||||
return errors.New("stop failed: " + stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package iplibrary
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
// 源码改自:https://github.com/lionsoul2014/ip2region/blob/master/binding/golang/ip2region/ip2Region.go
|
|
||||||
|
|
||||||
package iplibrary
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
IndexBlockLength = 12
|
|
||||||
)
|
|
||||||
|
|
||||||
var err error
|
|
||||||
|
|
||||||
type IP2Region struct {
|
|
||||||
headerSip []int64
|
|
||||||
headerPtr []int64
|
|
||||||
headerLen int64
|
|
||||||
|
|
||||||
// super block index info
|
|
||||||
firstIndexPtr int64
|
|
||||||
lastIndexPtr int64
|
|
||||||
totalBlocks int64
|
|
||||||
|
|
||||||
dbData []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type IpInfo struct {
|
|
||||||
CityId int64
|
|
||||||
Country string
|
|
||||||
Region string
|
|
||||||
Province string
|
|
||||||
City string
|
|
||||||
ISP string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ip IpInfo) String() string {
|
|
||||||
return strconv.FormatInt(ip.CityId, 10) + "|" + ip.Country + "|" + ip.Region + "|" + ip.Province + "|" + ip.City + "|" + ip.ISP
|
|
||||||
}
|
|
||||||
|
|
||||||
func getIpInfo(cityId int64, line []byte) *IpInfo {
|
|
||||||
lineSlice := strings.Split(string(line), "|")
|
|
||||||
ipInfo := &IpInfo{}
|
|
||||||
length := len(lineSlice)
|
|
||||||
ipInfo.CityId = cityId
|
|
||||||
if length < 5 {
|
|
||||||
for i := 0; i <= 5-length; i++ {
|
|
||||||
lineSlice = append(lineSlice, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ipInfo.Country = lineSlice[0]
|
|
||||||
ipInfo.Region = lineSlice[1]
|
|
||||||
ipInfo.Province = lineSlice[2]
|
|
||||||
ipInfo.City = lineSlice[3]
|
|
||||||
ipInfo.ISP = lineSlice[4]
|
|
||||||
return ipInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIP2Region(path string) (*IP2Region, error) {
|
|
||||||
var region = &IP2Region{}
|
|
||||||
region.dbData, err = os.ReadFile(path)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
region.firstIndexPtr = region.ipLongAtOffset(0)
|
|
||||||
region.lastIndexPtr = region.ipLongAtOffset(4)
|
|
||||||
region.totalBlocks = (region.lastIndexPtr-region.firstIndexPtr)/IndexBlockLength + 1
|
|
||||||
return region, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *IP2Region) MemorySearch(ipStr string) (ipInfo *IpInfo, err error) {
|
|
||||||
ip, err := ip2long(ipStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
h := this.totalBlocks
|
|
||||||
var dataPtr, l int64
|
|
||||||
for l <= h {
|
|
||||||
m := (l + h) >> 1
|
|
||||||
p := this.firstIndexPtr + m*IndexBlockLength
|
|
||||||
sip := this.ipLongAtOffset(p)
|
|
||||||
if ip < sip {
|
|
||||||
h = m - 1
|
|
||||||
} else {
|
|
||||||
eip := this.ipLongAtOffset(p + 4)
|
|
||||||
if ip > eip {
|
|
||||||
l = m + 1
|
|
||||||
} else {
|
|
||||||
dataPtr = this.ipLongAtOffset(p + 8)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if dataPtr == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
dataLen := (dataPtr >> 24) & 0xFF
|
|
||||||
dataPtr = dataPtr & 0x00FFFFFF
|
|
||||||
return getIpInfo(this.ipLongAtOffset(dataPtr), this.dbData[(dataPtr)+4:dataPtr+dataLen]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *IP2Region) ipLongAtOffset(offset int64) int64 {
|
|
||||||
return int64(this.dbData[offset]) |
|
|
||||||
int64(this.dbData[offset+1])<<8 |
|
|
||||||
int64(this.dbData[offset+2])<<16 |
|
|
||||||
int64(this.dbData[offset+3])<<24
|
|
||||||
}
|
|
||||||
|
|
||||||
func ip2long(IpStr string) (int64, error) {
|
|
||||||
bits := strings.Split(IpStr, ".")
|
|
||||||
if len(bits) != 4 {
|
|
||||||
return 0, errors.New("ip format error")
|
|
||||||
}
|
|
||||||
|
|
||||||
var sum int64
|
|
||||||
for i, n := range bits {
|
|
||||||
bit, _ := strconv.ParseInt(n, 10, 64)
|
|
||||||
sum += bit << uint(24-8*i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sum, nil
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package iplibrary
|
|
||||||
|
|
||||||
type LibraryInterface interface {
|
|
||||||
// 加载数据库文件
|
|
||||||
Load(dbPath string) error
|
|
||||||
|
|
||||||
// 查询IP
|
|
||||||
Lookup(ip string) (*Result, error)
|
|
||||||
|
|
||||||
// 关闭数据库文件
|
|
||||||
Close()
|
|
||||||
}
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
package iplibrary
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"net"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type IP2RegionLibrary struct {
|
|
||||||
db *IP2Region
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *IP2RegionLibrary) Load(dbPath string) error {
|
|
||||||
db, err := NewIP2Region(dbPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
this.db = db
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *IP2RegionLibrary) Lookup(ip string) (*Result, error) {
|
|
||||||
if this.db == nil {
|
|
||||||
return nil, errors.New("library has not been loaded")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 只支持IPv4
|
|
||||||
if strings.Contains(ip, ":") {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if net.ParseIP(ip) == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
// 防止panic发生
|
|
||||||
err := recover()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("IP2RegionLibrary", "panic: "+fmt.Sprintf("%#v", err))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
info, err := this.db.MemorySearch(ip)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if info.Country == "0" {
|
|
||||||
info.Country = ""
|
|
||||||
}
|
|
||||||
if info.Region == "0" {
|
|
||||||
info.Region = ""
|
|
||||||
}
|
|
||||||
if info.Province == "0" {
|
|
||||||
info.Province = ""
|
|
||||||
}
|
|
||||||
if info.City == "0" {
|
|
||||||
info.City = ""
|
|
||||||
}
|
|
||||||
if info.ISP == "0" {
|
|
||||||
info.ISP = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Result{
|
|
||||||
CityId: info.CityId,
|
|
||||||
Country: info.Country,
|
|
||||||
Region: info.Region,
|
|
||||||
Province: info.Province,
|
|
||||||
City: info.City,
|
|
||||||
ISP: info.ISP,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *IP2RegionLibrary) Close() {
|
|
||||||
if this.db != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
package iplibrary
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"github.com/iwind/TeaGo/rands"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIP2RegionLibrary_Lookup(t *testing.T) {
|
|
||||||
library := &IP2RegionLibrary{}
|
|
||||||
err := library.Load(Tea.Root + "/resources/ipdata/ip2region/ip2region.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
result, err := library.Lookup("114.240.223.47")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
logs.PrintAsJSON(result, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIP2RegionLibrary_Lookup_Valid_IP(t *testing.T) {
|
|
||||||
library := &IP2RegionLibrary{}
|
|
||||||
err := library.Load(Tea.Root + "/resources/ipdata/ip2region/ip2region.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
{
|
|
||||||
result, err := library.Lookup("114.240.223")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
logs.PrintAsJSON(result, t)
|
|
||||||
}
|
|
||||||
{
|
|
||||||
result, err := library.Lookup("abc")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
logs.PrintAsJSON(result, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func TestIP2RegionLibrary_Memory(t *testing.T) {
|
|
||||||
library := &IP2RegionLibrary{}
|
|
||||||
err := library.Load(Tea.Root + "/resources/ipdata/ip2region/ip2region.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
before := time.Now()
|
|
||||||
|
|
||||||
for i := 0; i < 1_000_000; i++ {
|
|
||||||
_, _ = library.Lookup(strconv.Itoa(rands.Int(0, 254)) + "." + strconv.Itoa(rands.Int(0, 254)) + "." + strconv.Itoa(rands.Int(0, 254)) + "." + strconv.Itoa(rands.Int(0, 254)))
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkIP2RegionLibrary_Lookup(b *testing.B) {
|
|
||||||
runtime.GOMAXPROCS(1)
|
|
||||||
|
|
||||||
library := &IP2RegionLibrary{}
|
|
||||||
err := library.Load(Tea.Root + "/resources/ipdata/ip2region/ip2region.db")
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_, _ = library.Lookup(strconv.Itoa(rands.Int(0, 254)) + "." + strconv.Itoa(rands.Int(0, 254)) + "." + strconv.Itoa(rands.Int(0, 254)) + "." + strconv.Itoa(rands.Int(0, 254)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
package iplibrary
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/files"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var SharedManager = NewManager()
|
|
||||||
var SharedLibrary LibraryInterface
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
// 初始化
|
|
||||||
library, err := SharedManager.Load()
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[IP_LIBRARY]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SharedLibrary = library
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type Manager struct {
|
|
||||||
code string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewManager() *Manager {
|
|
||||||
return &Manager{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Manager) Load() (LibraryInterface, error) {
|
|
||||||
// 当前正在使用的IP库代号
|
|
||||||
config, err := models.SharedSysSettingDAO.ReadGlobalConfig(nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
code := config.IPLibrary.Code
|
|
||||||
if len(code) == 0 {
|
|
||||||
code = serverconfigs.DefaultIPLibraryType
|
|
||||||
}
|
|
||||||
|
|
||||||
dir := Tea.Root + "/resources/ipdata/" + code
|
|
||||||
var lastVersion int64 = -1
|
|
||||||
lastFilename := ""
|
|
||||||
for _, file := range files.NewFile(dir).List() {
|
|
||||||
filename := file.Name()
|
|
||||||
|
|
||||||
reg := regexp.MustCompile(`^` + regexp.QuoteMeta(code) + `.(\d+)\.`)
|
|
||||||
if reg.MatchString(filename) { // 先查找有版本号的
|
|
||||||
result := reg.FindStringSubmatch(filename)
|
|
||||||
version := types.Int64(result[1])
|
|
||||||
if version > lastVersion {
|
|
||||||
lastVersion = version
|
|
||||||
lastFilename = filename
|
|
||||||
}
|
|
||||||
} else if strings.HasPrefix(filename, code+".") { // 后查找默认的
|
|
||||||
if lastVersion == -1 {
|
|
||||||
lastFilename = filename
|
|
||||||
lastVersion = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(lastFilename) == 0 {
|
|
||||||
return nil, errors.New("ip library file not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
var libraryPtr LibraryInterface
|
|
||||||
switch code {
|
|
||||||
case serverconfigs.IPLibraryTypeIP2Region:
|
|
||||||
libraryPtr = &IP2RegionLibrary{}
|
|
||||||
default:
|
|
||||||
return nil, errors.New("invalid ip library code '" + code + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = libraryPtr.Load(dir + "/" + lastFilename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return libraryPtr, nil
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package iplibrary
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestManager_Load(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
manager := NewManager()
|
|
||||||
lib, err := manager.Load()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(lib.Lookup("1.2.3.4"))
|
|
||||||
t.Log(lib.Lookup("2.3.4.5"))
|
|
||||||
t.Log(lib.Lookup("200.200.200.200"))
|
|
||||||
t.Log(lib.Lookup("202.106.0.20"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewManager(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
t.Log(SharedLibrary)
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package iplibrary
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/iwind/TeaGo/lists"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Result struct {
|
|
||||||
CityId int64
|
|
||||||
Country string
|
|
||||||
Region string
|
|
||||||
Province string
|
|
||||||
City string
|
|
||||||
ISP string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Result) Summary() string {
|
|
||||||
pieces := []string{}
|
|
||||||
if len(this.Country) > 0 {
|
|
||||||
pieces = append(pieces, this.Country)
|
|
||||||
}
|
|
||||||
if len(this.Province) > 0 && !lists.ContainsString(pieces, this.Province) {
|
|
||||||
pieces = append(pieces, this.Province)
|
|
||||||
}
|
|
||||||
if len(this.City) > 0 && !lists.ContainsString(pieces, this.City) && !lists.ContainsString(pieces, strings.TrimSuffix(this.City, "市")) {
|
|
||||||
pieces = append(pieces, this.City)
|
|
||||||
}
|
|
||||||
return strings.Join(pieces, " ")
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user