Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
004e640321 | ||
|
|
7ad315ae4b | ||
|
|
ba938e5361 | ||
|
|
9ddf02a0e6 | ||
|
|
ebcbd5690d | ||
|
|
bbca766fa4 | ||
|
|
99c7819d3a | ||
|
|
08bb3e66f8 | ||
|
|
9159820742 | ||
|
|
1a565b2ebb | ||
|
|
98847c53ea | ||
|
|
14bafc8f20 | ||
|
|
58a5bd0092 | ||
|
|
4f1ce52f6a | ||
|
|
14ba7f6899 | ||
|
|
e582e37c06 | ||
|
|
6a3fa9f0ca | ||
|
|
e0a9965fed | ||
|
|
481fa8cd2d | ||
|
|
95349dc457 | ||
|
|
fc839f96d2 | ||
|
|
0414cc02e8 | ||
|
|
b8babaae39 | ||
|
|
285ce1b312 | ||
|
|
c309da81ae | ||
|
|
c325fde52b | ||
|
|
0f69b45d25 | ||
|
|
e02084ba5d | ||
|
|
642b23dbb7 | ||
|
|
b1dc385c87 | ||
|
|
89a69e3165 | ||
|
|
530954dd6c | ||
|
|
33635f7a1b | ||
|
|
8ac964e805 | ||
|
|
a1519baf0f | ||
|
|
e6e32a39bb | ||
|
|
d828b7f8a4 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
*_plus.go
|
*_plus.go
|
||||||
*-plus.sh
|
*-plus.sh
|
||||||
|
*_plus_test.go
|
||||||
@@ -88,6 +88,7 @@ function build() {
|
|||||||
mkdir "$DIST"/bin
|
mkdir "$DIST"/bin
|
||||||
mkdir "$DIST"/configs
|
mkdir "$DIST"/configs
|
||||||
mkdir "$DIST"/logs
|
mkdir "$DIST"/logs
|
||||||
|
mkdir "$DIST"/data
|
||||||
fi
|
fi
|
||||||
cp "$ROOT"/configs/api.template.yaml "$DIST"/configs/
|
cp "$ROOT"/configs/api.template.yaml "$DIST"/configs/
|
||||||
cp "$ROOT"/configs/db.template.yaml "$DIST"/configs/
|
cp "$ROOT"/configs/db.template.yaml "$DIST"/configs/
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -12,7 +12,7 @@ require (
|
|||||||
github.com/go-acme/lego/v4 v4.5.2
|
github.com/go-acme/lego/v4 v4.5.2
|
||||||
github.com/go-sql-driver/mysql v1.5.0
|
github.com/go-sql-driver/mysql v1.5.0
|
||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.2
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e
|
||||||
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62
|
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62
|
||||||
github.com/mozillazg/go-pinyin v0.18.0
|
github.com/mozillazg/go-pinyin v0.18.0
|
||||||
github.com/pkg/sftp v1.12.0
|
github.com/pkg/sftp v1.12.0
|
||||||
@@ -21,7 +21,7 @@ require (
|
|||||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
|
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
|
||||||
google.golang.org/grpc v1.45.0
|
google.golang.org/grpc v1.45.0
|
||||||
google.golang.org/protobuf v1.27.1
|
google.golang.org/protobuf v1.27.1
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -239,6 +239,8 @@ github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7Q
|
|||||||
github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570 h1:zqz2FiMMkSHXWO1EsTRJDPTwX9xQ4uuyD5GAE4JGlhM=
|
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570 h1:zqz2FiMMkSHXWO1EsTRJDPTwX9xQ4uuyD5GAE4JGlhM=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e h1:cw4b6ecXdXvLd45YSstD8r9ClcnVK4ljZMZCept2aOk=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e/go.mod h1:fi/Pq+/5m2HZoseM+39dMF57ANXRt6w4PkGu3NXPc5s=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||||
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62 h1:HJH6RDheAY156DnIfJSD/bEvqyXzsZuE2gzs8PuUjoo=
|
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62 h1:HJH6RDheAY156DnIfJSD/bEvqyXzsZuE2gzs8PuUjoo=
|
||||||
@@ -789,6 +791,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type BaseStorage struct {
|
|
||||||
isOk bool
|
|
||||||
version int
|
|
||||||
firewallOnly bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) SetVersion(version int) {
|
|
||||||
this.version = version
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) Version() int {
|
|
||||||
return this.version
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) IsOk() bool {
|
|
||||||
return this.isOk
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) SetOk(isOk bool) {
|
|
||||||
this.isOk = isOk
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) SetFirewallOnly(firewallOnly bool) {
|
|
||||||
this.firewallOnly = firewallOnly
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marshal 对日志进行编码
|
|
||||||
func (this *BaseStorage) Marshal(accessLog *pb.HTTPAccessLog) ([]byte, error) {
|
|
||||||
return json.Marshal(accessLog)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FormatVariables 格式化字符串中的变量
|
|
||||||
func (this *BaseStorage) FormatVariables(s string) string {
|
|
||||||
var now = time.Now()
|
|
||||||
return configutils.ParseVariables(s, func(varName string) (value string) {
|
|
||||||
switch varName {
|
|
||||||
case "year":
|
|
||||||
return strconv.Itoa(now.Year())
|
|
||||||
case "month":
|
|
||||||
return fmt.Sprintf("%02d", now.Month())
|
|
||||||
case "week":
|
|
||||||
_, week := now.ISOWeek()
|
|
||||||
return fmt.Sprintf("%02d", week)
|
|
||||||
case "day":
|
|
||||||
return fmt.Sprintf("%02d", now.Day())
|
|
||||||
case "hour":
|
|
||||||
return fmt.Sprintf("%02d", now.Hour())
|
|
||||||
case "minute":
|
|
||||||
return fmt.Sprintf("%02d", now.Minute())
|
|
||||||
case "second":
|
|
||||||
return fmt.Sprintf("%02d", now.Second())
|
|
||||||
case "date":
|
|
||||||
return fmt.Sprintf("%d%02d%02d", now.Year(), now.Month(), now.Day())
|
|
||||||
}
|
|
||||||
|
|
||||||
return varName
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"os/exec"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CommandStorage 通过命令行存储
|
|
||||||
type CommandStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogCommandStorageConfig
|
|
||||||
|
|
||||||
writeLocker sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCommandStorage(config *serverconfigs.AccessLogCommandStorageConfig) *CommandStorage {
|
|
||||||
return &CommandStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *CommandStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 启动
|
|
||||||
func (this *CommandStorage) Start() error {
|
|
||||||
if len(this.config.Command) == 0 {
|
|
||||||
return errors.New("'command' should not be empty")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *CommandStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
this.writeLocker.Lock()
|
|
||||||
defer this.writeLocker.Unlock()
|
|
||||||
|
|
||||||
cmd := exec.Command(this.config.Command, this.config.Args...)
|
|
||||||
if len(this.config.Dir) > 0 {
|
|
||||||
cmd.Dir = this.config.Dir
|
|
||||||
}
|
|
||||||
|
|
||||||
stdout := bytes.NewBuffer([]byte{})
|
|
||||||
cmd.Stdout = stdout
|
|
||||||
|
|
||||||
w, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write([]byte("\n"))
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ = w.Close()
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
|
|
||||||
if stdout.Len() > 0 {
|
|
||||||
logs.Error(errors.New(string(stdout.Bytes())))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *CommandStorage) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCommandStorage_Write(t *testing.T) {
|
|
||||||
php, err := exec.LookPath("php")
|
|
||||||
if err != nil { // not found php, so we can not test
|
|
||||||
t.Log("php:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
before := time.Now()
|
|
||||||
|
|
||||||
storage := NewCommandStorage(&serverconfigs.AccessLogCommandStorageConfig{
|
|
||||||
Command: php,
|
|
||||||
Args: []string{cwd + "/tests/command_storage.php"},
|
|
||||||
})
|
|
||||||
err = storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/hello",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/world",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/lu",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/ping",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Log(time.Since(before).Seconds(), "seconds")
|
|
||||||
}
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ESStorage ElasticSearch存储策略
|
|
||||||
type ESStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogESStorageConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewESStorage(config *serverconfigs.AccessLogESStorageConfig) *ESStorage {
|
|
||||||
return &ESStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *ESStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *ESStorage) Start() error {
|
|
||||||
if len(this.config.Endpoint) == 0 {
|
|
||||||
return errors.New("'endpoint' should not be nil")
|
|
||||||
}
|
|
||||||
if !regexp.MustCompile(`(?i)^(http|https)://`).MatchString(this.config.Endpoint) {
|
|
||||||
this.config.Endpoint = "http://" + this.config.Endpoint
|
|
||||||
}
|
|
||||||
if len(this.config.Index) == 0 {
|
|
||||||
return errors.New("'index' should not be nil")
|
|
||||||
}
|
|
||||||
if len(this.config.MappingType) == 0 {
|
|
||||||
return errors.New("'mappingType' should not be nil")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
bulk := &strings.Builder{}
|
|
||||||
indexName := this.FormatVariables(this.config.Index)
|
|
||||||
typeName := this.FormatVariables(this.config.MappingType)
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(accessLog.RequestId) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
opData, err := json.Marshal(map[string]interface{}{
|
|
||||||
"index": map[string]interface{}{
|
|
||||||
"_index": indexName,
|
|
||||||
"_type": typeName,
|
|
||||||
"_id": accessLog.RequestId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_ES_STORAGE", "write failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_ES_STORAGE", "marshal data failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
bulk.Write(opData)
|
|
||||||
bulk.WriteString("\n")
|
|
||||||
bulk.Write(data)
|
|
||||||
bulk.WriteString("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
if bulk.Len() == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, this.config.Endpoint+"/_bulk", strings.NewReader(bulk.String()))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("User-Agent", strings.ReplaceAll(teaconst.ProductName, " ", "-")+"/"+teaconst.Version)
|
|
||||||
if len(this.config.Username) > 0 || len(this.config.Password) > 0 {
|
|
||||||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(this.config.Username+":"+this.config.Password)))
|
|
||||||
}
|
|
||||||
client := utils.SharedHttpClient(10 * time.Second)
|
|
||||||
defer func() {
|
|
||||||
_ = req.Body.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = resp.Body.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
bodyData, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
return errors.New("ElasticSearch response status code: " + fmt.Sprintf("%d", resp.StatusCode) + " content: " + string(bodyData))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *ESStorage) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestESStorage_Write(t *testing.T) {
|
|
||||||
storage := NewESStorage(&serverconfigs.AccessLogESStorageConfig{
|
|
||||||
Endpoint: "http://127.0.0.1:9200",
|
|
||||||
Index: "logs",
|
|
||||||
MappingType: "accessLogs",
|
|
||||||
Username: "hello",
|
|
||||||
Password: "world",
|
|
||||||
})
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "POST",
|
|
||||||
RequestPath: "/1",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
TimeISO8601: "2018-07-23T22:23:35+08:00",
|
|
||||||
Header: map[string]*pb.Strings{
|
|
||||||
"Content-Type": {Values: []string{"text/html"}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/2",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
TimeISO8601: "2018-07-23T22:23:35+08:00",
|
|
||||||
Header: map[string]*pb.Strings{
|
|
||||||
"Content-Type": {Values: []string{"text/css"}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FileStorage 文件存储策略
|
|
||||||
type FileStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogFileStorageConfig
|
|
||||||
|
|
||||||
writeLocker sync.Mutex
|
|
||||||
|
|
||||||
files map[string]*os.File // path => *File
|
|
||||||
filesLocker sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFileStorage(config *serverconfigs.AccessLogFileStorageConfig) *FileStorage {
|
|
||||||
return &FileStorage{
|
|
||||||
config: config,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *FileStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *FileStorage) Start() error {
|
|
||||||
if len(this.config.Path) == 0 {
|
|
||||||
return errors.New("'path' should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
this.files = map[string]*os.File{}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write 写入日志
|
|
||||||
func (this *FileStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fp := this.fp()
|
|
||||||
if fp == nil {
|
|
||||||
return errors.New("file pointer should not be nil")
|
|
||||||
}
|
|
||||||
this.writeLocker.Lock()
|
|
||||||
defer this.writeLocker.Unlock()
|
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = fp.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
_ = this.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
_, _ = fp.WriteString("\n")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *FileStorage) Close() error {
|
|
||||||
this.filesLocker.Lock()
|
|
||||||
defer this.filesLocker.Unlock()
|
|
||||||
|
|
||||||
var resultErr error
|
|
||||||
for _, f := range this.files {
|
|
||||||
err := f.Close()
|
|
||||||
if err != nil {
|
|
||||||
resultErr = err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resultErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *FileStorage) fp() *os.File {
|
|
||||||
path := this.FormatVariables(this.config.Path)
|
|
||||||
|
|
||||||
this.filesLocker.Lock()
|
|
||||||
defer this.filesLocker.Unlock()
|
|
||||||
fp, ok := this.files[path]
|
|
||||||
if ok {
|
|
||||||
return fp
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭其他的文件
|
|
||||||
for _, f := range this.files {
|
|
||||||
_ = f.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 是否创建文件目录
|
|
||||||
if this.config.AutoCreate {
|
|
||||||
dir := filepath.Dir(path)
|
|
||||||
_, err := os.Stat(dir)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = os.MkdirAll(dir, 0777)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 打开新文件
|
|
||||||
fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
this.files[path] = fp
|
|
||||||
|
|
||||||
return fp
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFileStorage_Write(t *testing.T) {
|
|
||||||
storage := NewFileStorage(&serverconfigs.AccessLogFileStorageConfig{
|
|
||||||
Path: Tea.Root + "/logs/access-${date}.log",
|
|
||||||
})
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestPath: "/hello",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestPath: "/world",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestPath: "/1",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestPath: "/2",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "POST",
|
|
||||||
RequestPath: "/1",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/2",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
|
|
||||||
// StorageInterface 日志存储接口
|
|
||||||
type StorageInterface interface {
|
|
||||||
// Version 获取版本
|
|
||||||
Version() int
|
|
||||||
|
|
||||||
// SetVersion 设置版本
|
|
||||||
SetVersion(version int)
|
|
||||||
|
|
||||||
// SetFirewallOnly 设置是否只处理防火墙相关的访问日志
|
|
||||||
SetFirewallOnly(firewallOnly bool)
|
|
||||||
|
|
||||||
IsOk() bool
|
|
||||||
|
|
||||||
SetOk(ok bool)
|
|
||||||
|
|
||||||
// Config 获取配置
|
|
||||||
Config() interface{}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
Start() error
|
|
||||||
|
|
||||||
// Write 写入日志
|
|
||||||
Write(accessLogs []*pb.HTTPAccessLog) error
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
Close() error
|
|
||||||
}
|
|
||||||
@@ -1,185 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/lists"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var SharedStorageManager = NewStorageManager()
|
|
||||||
|
|
||||||
type StorageManager struct {
|
|
||||||
storageMap map[int64]StorageInterface // policyId => Storage
|
|
||||||
|
|
||||||
locker sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewStorageManager() *StorageManager {
|
|
||||||
return &StorageManager{
|
|
||||||
storageMap: map[int64]StorageInterface{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *StorageManager) Start() {
|
|
||||||
var ticker = time.NewTicker(1 * time.Minute)
|
|
||||||
if Tea.IsTesting() {
|
|
||||||
ticker = time.NewTicker(5 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 启动时执行一次
|
|
||||||
var err = this.Loop()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "update error: "+err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// 循环执行
|
|
||||||
for range ticker.C {
|
|
||||||
err := this.Loop()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "update error: "+err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loop 更新
|
|
||||||
func (this *StorageManager) Loop() error {
|
|
||||||
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAndOnPolicies(nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var policyIds = []int64{}
|
|
||||||
for _, policy := range policies {
|
|
||||||
if policy.IsOn {
|
|
||||||
policyIds = append(policyIds, int64(policy.Id))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.locker.Lock()
|
|
||||||
defer this.locker.Unlock()
|
|
||||||
|
|
||||||
// 关闭不用的
|
|
||||||
for policyId, storage := range this.storageMap {
|
|
||||||
if !lists.ContainsInt64(policyIds, policyId) {
|
|
||||||
err := storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "close '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
}
|
|
||||||
delete(this.storageMap, policyId)
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "remove '"+types.String(policyId)+"'")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, policy := range policies {
|
|
||||||
var policyId = int64(policy.Id)
|
|
||||||
storage, ok := this.storageMap[policyId]
|
|
||||||
if ok {
|
|
||||||
// 检查配置是否有变更
|
|
||||||
if types.Int(policy.Version) != storage.Version() {
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "close policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
|
|
||||||
// 继续往下执行
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(policy.Options) > 0 {
|
|
||||||
err = json.Unmarshal(policy.Options, storage.Config())
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "unmarshal policy '"+types.String(policyId)+"' config failed: "+err.Error())
|
|
||||||
storage.SetOk(false)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
storage.SetVersion(types.Int(policy.Version))
|
|
||||||
storage.SetFirewallOnly(policy.FirewallOnly == 1)
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
storage.SetOk(true)
|
|
||||||
remotelogs.Println("ACCESS_LOG_STORAGE_MANAGER", "restart policy '"+types.String(policyId)+"'")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
storage, err := this.createStorage(policy.Type, policy.Options)
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "create policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
storage.SetVersion(types.Int(policy.Version))
|
|
||||||
storage.SetFirewallOnly(policy.FirewallOnly == 1)
|
|
||||||
this.storageMap[policyId] = storage
|
|
||||||
err = storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
storage.SetOk(true)
|
|
||||||
remotelogs.Println("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"'")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *StorageManager) createStorage(storageType string, optionsJSON []byte) (StorageInterface, error) {
|
|
||||||
switch storageType {
|
|
||||||
case serverconfigs.AccessLogStorageTypeFile:
|
|
||||||
var config = &serverconfigs.AccessLogFileStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewFileStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeES:
|
|
||||||
var config = &serverconfigs.AccessLogESStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewESStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeTCP:
|
|
||||||
var config = &serverconfigs.AccessLogTCPStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewTCPStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeSyslog:
|
|
||||||
var config = &serverconfigs.AccessLogSyslogStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewSyslogStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeCommand:
|
|
||||||
var config = &serverconfigs.AccessLogCommandStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewCommandStorage(config), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("invalid policy type '" + storageType + "'")
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestStorageManager_Loop(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
var storage = NewStorageManager()
|
|
||||||
err := storage.Loop()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(storage.storageMap)
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
//go:build !plus
|
|
||||||
// +build !plus
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *StorageManager) Write(policyId int64, accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SyslogStorageProtocol = string
|
|
||||||
|
|
||||||
const (
|
|
||||||
SyslogStorageProtocolTCP SyslogStorageProtocol = "tcp"
|
|
||||||
SyslogStorageProtocolUDP SyslogStorageProtocol = "udp"
|
|
||||||
SyslogStorageProtocolNone SyslogStorageProtocol = "none"
|
|
||||||
SyslogStorageProtocolSocket SyslogStorageProtocol = "socket"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SyslogStoragePriority = int
|
|
||||||
|
|
||||||
// SyslogStorage syslog存储策略
|
|
||||||
type SyslogStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogSyslogStorageConfig
|
|
||||||
|
|
||||||
exe string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSyslogStorage(config *serverconfigs.AccessLogSyslogStorageConfig) *SyslogStorage {
|
|
||||||
return &SyslogStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *SyslogStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *SyslogStorage) Start() error {
|
|
||||||
if runtime.GOOS != "linux" {
|
|
||||||
return errors.New("'syslog' storage only works on linux")
|
|
||||||
}
|
|
||||||
|
|
||||||
exe, err := exec.LookPath("logger")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
this.exe = exe
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *SyslogStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
args := []string{}
|
|
||||||
if len(this.config.Tag) > 0 {
|
|
||||||
args = append(args, "-t", this.config.Tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
if this.config.Priority >= 0 {
|
|
||||||
args = append(args, "-p", strconv.Itoa(this.config.Priority))
|
|
||||||
}
|
|
||||||
|
|
||||||
switch this.config.Protocol {
|
|
||||||
case SyslogStorageProtocolTCP:
|
|
||||||
args = append(args, "-T")
|
|
||||||
if len(this.config.ServerAddr) > 0 {
|
|
||||||
args = append(args, "-n", this.config.ServerAddr)
|
|
||||||
}
|
|
||||||
if this.config.ServerPort > 0 {
|
|
||||||
args = append(args, "-P", strconv.Itoa(this.config.ServerPort))
|
|
||||||
}
|
|
||||||
case SyslogStorageProtocolUDP:
|
|
||||||
args = append(args, "-d")
|
|
||||||
if len(this.config.ServerAddr) > 0 {
|
|
||||||
args = append(args, "-n", this.config.ServerAddr)
|
|
||||||
}
|
|
||||||
if this.config.ServerPort > 0 {
|
|
||||||
args = append(args, "-P", strconv.Itoa(this.config.ServerPort))
|
|
||||||
}
|
|
||||||
case SyslogStorageProtocolSocket:
|
|
||||||
args = append(args, "-u")
|
|
||||||
args = append(args, this.config.Socket)
|
|
||||||
case SyslogStorageProtocolNone:
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
args = append(args, "-S", "10240")
|
|
||||||
|
|
||||||
var cmd = exec.Command(this.exe, args...)
|
|
||||||
var stderrBuffer = &bytes.Buffer{}
|
|
||||||
cmd.Stderr = stderrBuffer
|
|
||||||
|
|
||||||
w, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_POLICY_SYSLOG", "marshal accesslog failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write([]byte("\n"))
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_POLICY_SYSLOG", "write accesslog failed: "+err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = w.Close()
|
|
||||||
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("send syslog failed: " + err.Error() + ", stderr: " + stderrBuffer.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *SyslogStorage) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"net"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TCPStorage TCP存储策略
|
|
||||||
type TCPStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogTCPStorageConfig
|
|
||||||
|
|
||||||
writeLocker sync.Mutex
|
|
||||||
|
|
||||||
connLocker sync.Mutex
|
|
||||||
conn net.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTCPStorage(config *serverconfigs.AccessLogTCPStorageConfig) *TCPStorage {
|
|
||||||
return &TCPStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TCPStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *TCPStorage) Start() error {
|
|
||||||
if len(this.config.Network) == 0 {
|
|
||||||
return errors.New("'network' should not be empty")
|
|
||||||
}
|
|
||||||
if len(this.config.Addr) == 0 {
|
|
||||||
return errors.New("'addr' should not be empty")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *TCPStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err := this.connect()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
conn := this.conn
|
|
||||||
if conn == nil {
|
|
||||||
return errors.New("connection should not be nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
this.writeLocker.Lock()
|
|
||||||
defer this.writeLocker.Unlock()
|
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = conn.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
_ = this.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
_, err = conn.Write([]byte("\n"))
|
|
||||||
if err != nil {
|
|
||||||
_ = this.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *TCPStorage) Close() error {
|
|
||||||
this.connLocker.Lock()
|
|
||||||
defer this.connLocker.Unlock()
|
|
||||||
|
|
||||||
if this.conn != nil {
|
|
||||||
err := this.conn.Close()
|
|
||||||
this.conn = nil
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TCPStorage) connect() error {
|
|
||||||
this.connLocker.Lock()
|
|
||||||
defer this.connLocker.Unlock()
|
|
||||||
|
|
||||||
if this.conn != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := net.Dial(this.config.Network, this.config.Addr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
this.conn = conn
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTCPStorage_Write(t *testing.T) {
|
|
||||||
go func() {
|
|
||||||
server, err := net.Listen("tcp", "127.0.0.1:9981")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for {
|
|
||||||
conn, err := server.Accept()
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
for {
|
|
||||||
n, err := conn.Read(buf)
|
|
||||||
if n > 0 {
|
|
||||||
t.Log(string(buf[:n]))
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
_ = server.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
storage := NewTCPStorage(&serverconfigs.AccessLogTCPStorageConfig{
|
|
||||||
Network: "tcp",
|
|
||||||
Addr: "127.0.0.1:9981",
|
|
||||||
})
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "POST",
|
|
||||||
RequestPath: "/1",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/2",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
// test command storage
|
|
||||||
|
|
||||||
// open access log file
|
|
||||||
$fp = fopen("/tmp/goedge-command-storage.log", "a+");
|
|
||||||
|
|
||||||
// read access logs from stdin
|
|
||||||
$stdin = fopen("php://stdin", "r");
|
|
||||||
while(true) {
|
|
||||||
if (feof($stdin)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$line = fgets($stdin);
|
|
||||||
|
|
||||||
// write to access log file
|
|
||||||
fwrite($fp, $line);
|
|
||||||
}
|
|
||||||
|
|
||||||
// close file pointers
|
|
||||||
fclose($fp);
|
|
||||||
fclose($stdin);
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/go-acme/lego/v4/challenge/dns01"
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
||||||
"github.com/go-acme/lego/v4/lego"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
acmelog "github.com/go-acme/lego/v4/log"
|
acmelog "github.com/go-acme/lego/v4/log"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ func (this *MyProvider) CleanUp(domain, token, keyAuth string) error {
|
|||||||
|
|
||||||
// 参考 https://go-acme.github.io/lego/usage/library/
|
// 参考 https://go-acme.github.io/lego/usage/library/
|
||||||
func TestGenerate(t *testing.T) {
|
func TestGenerate(t *testing.T) {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
|
|
||||||
// 生成私钥
|
// 生成私钥
|
||||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
@@ -94,7 +94,7 @@ func TestGenerate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerate_EAB(t *testing.T) {
|
func TestGenerate_EAB(t *testing.T) {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
|
|
||||||
// 生成私钥
|
// 生成私钥
|
||||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/go-acme/lego/v4/lego"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
acmelog "github.com/go-acme/lego/v4/log"
|
acmelog "github.com/go-acme/lego/v4/log"
|
||||||
"github.com/go-acme/lego/v4/registration"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
|
|||||||
|
|
||||||
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
||||||
if !this.debug {
|
if !this.debug {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.task.User == nil {
|
if this.task.User == nil {
|
||||||
@@ -138,7 +138,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
|
|
||||||
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
||||||
if !this.debug {
|
if !this.debug {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.task.User == nil {
|
if this.task.User == nil {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
@@ -42,7 +41,7 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
var err error
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
data, err = ioutil.ReadFile(path)
|
data, err = os.ReadFile(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if path == localFile {
|
if path == localFile {
|
||||||
isFromLocal = true
|
isFromLocal = true
|
||||||
@@ -63,7 +62,7 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
|
|
||||||
if !isFromLocal {
|
if !isFromLocal {
|
||||||
// 恢复文件
|
// 恢复文件
|
||||||
_ = ioutil.WriteFile(localFile, data, 0666)
|
_ = os.WriteFile(localFile, data, 0666)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 恢复数据库文件
|
// 恢复数据库文件
|
||||||
@@ -80,9 +79,9 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
data, err := ioutil.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_ = ioutil.WriteFile(dbConfigFile, data, 0666)
|
_ = os.WriteFile(dbConfigFile, data, 0666)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,14 +121,14 @@ func (this *APIConfig) WriteFile(path string) error {
|
|||||||
for _, backupDir := range backupDirs {
|
for _, backupDir := range backupDirs {
|
||||||
stat, err := os.Stat(backupDir)
|
stat, err := os.Stat(backupDir)
|
||||||
if err == nil && stat.IsDir() {
|
if err == nil && stat.IsDir() {
|
||||||
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666)
|
_ = os.WriteFile(backupDir+"/"+filename, data, 0666)
|
||||||
} else if err != nil && os.IsNotExist(err) {
|
} else if err != nil && os.IsNotExist(err) {
|
||||||
err = os.Mkdir(backupDir, 0777)
|
err = os.Mkdir(backupDir, 0777)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666)
|
_ = os.WriteFile(backupDir+"/"+filename, data, 0666)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ioutil.WriteFile(path, data, 0666)
|
return os.WriteFile(path, data, 0666)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.4.10"
|
Version = "0.5.0"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,11 +18,11 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.4.10"
|
NodeVersion = "0.5.0"
|
||||||
UserNodeVersion = "0.3.6"
|
UserNodeVersion = "0.4.0"
|
||||||
AuthorityNodeVersion = "0.0.2"
|
AuthorityNodeVersion = "0.0.2"
|
||||||
MonitorNodeVersion = "0.0.4"
|
MonitorNodeVersion = "0.0.4"
|
||||||
DNSNodeVersion = "0.2.4"
|
DNSNodeVersion = "0.2.5"
|
||||||
ReportNodeVersion = "0.1.1"
|
ReportNodeVersion = "0.1.1"
|
||||||
|
|
||||||
// SQLVersion SQL版本号
|
// SQLVersion SQL版本号
|
||||||
|
|||||||
33
internal/db/models/accounts/order_method_dao.go
Normal file
33
internal/db/models/accounts/order_method_dao.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
OrderMethodStateEnabled = 1 // 已启用
|
||||||
|
OrderMethodStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrderMethodDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewOrderMethodDAO() *OrderMethodDAO {
|
||||||
|
return dbs.NewDAO(&OrderMethodDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeOrderMethods",
|
||||||
|
Model: new(OrderMethod),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*OrderMethodDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedOrderMethodDAO *OrderMethodDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedOrderMethodDAO = NewOrderMethodDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package nameservers
|
package accounts_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
36
internal/db/models/accounts/order_method_model.go
Normal file
36
internal/db/models/accounts/order_method_model.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// OrderMethod 订单支付方式
|
||||||
|
type OrderMethod struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
|
Description string `field:"description"` // 描述
|
||||||
|
ParentCode string `field:"parentCode"` // 内置的父级代号
|
||||||
|
Code string `field:"code"` // 代号
|
||||||
|
Url string `field:"url"` // URL
|
||||||
|
Secret string `field:"secret"` // 密钥
|
||||||
|
Params dbs.JSON `field:"params"` // 参数
|
||||||
|
Order uint32 `field:"order"` // 排序
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderMethodOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
Name interface{} // 名称
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Description interface{} // 描述
|
||||||
|
ParentCode interface{} // 内置的父级代号
|
||||||
|
Code interface{} // 代号
|
||||||
|
Url interface{} // URL
|
||||||
|
Secret interface{} // 密钥
|
||||||
|
Params interface{} // 参数
|
||||||
|
Order interface{} // 排序
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOrderMethodOperator() *OrderMethodOperator {
|
||||||
|
return &OrderMethodOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/accounts/order_method_model_ext.go
Normal file
1
internal/db/models/accounts/order_method_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
33
internal/db/models/accounts/user_order_dao.go
Normal file
33
internal/db/models/accounts/user_order_dao.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
UserOrderStateEnabled = 1 // 已启用
|
||||||
|
UserOrderStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserOrderDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewUserOrderDAO() *UserOrderDAO {
|
||||||
|
return dbs.NewDAO(&UserOrderDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeUserOrders",
|
||||||
|
Model: new(UserOrder),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*UserOrderDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedUserOrderDAO *UserOrderDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedUserOrderDAO = NewUserOrderDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package nameservers
|
package accounts_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
28
internal/db/models/accounts/user_order_log_dao.go
Normal file
28
internal/db/models/accounts/user_order_log_dao.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserOrderLogDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewUserOrderLogDAO() *UserOrderLogDAO {
|
||||||
|
return dbs.NewDAO(&UserOrderLogDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeUserOrderLogs",
|
||||||
|
Model: new(UserOrderLog),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*UserOrderLogDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedUserOrderLogDAO *UserOrderLogDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedUserOrderLogDAO = NewUserOrderLogDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package nameservers
|
package accounts_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
28
internal/db/models/accounts/user_order_log_model.go
Normal file
28
internal/db/models/accounts/user_order_log_model.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// UserOrderLog 订单日志
|
||||||
|
type UserOrderLog struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
AdminId uint64 `field:"adminId"` // 管理员ID
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
OrderId uint64 `field:"orderId"` // 订单ID
|
||||||
|
Status string `field:"status"` // 状态
|
||||||
|
Snapshot dbs.JSON `field:"snapshot"` // 状态快照
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserOrderLogOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
AdminId interface{} // 管理员ID
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
OrderId interface{} // 订单ID
|
||||||
|
Status interface{} // 状态
|
||||||
|
Snapshot interface{} // 状态快照
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserOrderLogOperator() *UserOrderLogOperator {
|
||||||
|
return &UserOrderLogOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/accounts/user_order_log_model_ext.go
Normal file
1
internal/db/models/accounts/user_order_log_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
40
internal/db/models/accounts/user_order_model.go
Normal file
40
internal/db/models/accounts/user_order_model.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// UserOrder 用户订单
|
||||||
|
type UserOrder struct {
|
||||||
|
Id uint64 `field:"id"` // 用户订单
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
Code string `field:"code"` // 订单号
|
||||||
|
Type string `field:"type"` // 订单类型
|
||||||
|
MethodId uint32 `field:"methodId"` // 支付方式
|
||||||
|
Status string `field:"status"` // 订单状态
|
||||||
|
Amount float64 `field:"amount"` // 金额
|
||||||
|
Params dbs.JSON `field:"params"` // 附加参数
|
||||||
|
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
CancelledAt uint64 `field:"cancelledAt"` // 取消时间
|
||||||
|
FinishedAt uint64 `field:"finishedAt"` // 结束时间
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserOrderOperator struct {
|
||||||
|
Id interface{} // 用户订单
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
Code interface{} // 订单号
|
||||||
|
Type interface{} // 订单类型
|
||||||
|
MethodId interface{} // 支付方式
|
||||||
|
Status interface{} // 订单状态
|
||||||
|
Amount interface{} // 金额
|
||||||
|
Params interface{} // 附加参数
|
||||||
|
ExpiredAt interface{} // 过期时间
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
CancelledAt interface{} // 取消时间
|
||||||
|
FinishedAt interface{} // 结束时间
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserOrderOperator() *UserOrderOperator {
|
||||||
|
return &UserOrderOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/accounts/user_order_model_ext.go
Normal file
1
internal/db/models/accounts/user_order_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
@@ -63,6 +63,19 @@ func (this *AdminDAO) FindEnabledAdmin(tx *dbs.Tx, id int64) (*Admin, error) {
|
|||||||
return result.(*Admin), err
|
return result.(*Admin), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindBasicAdmin 查找管理员基本信息
|
||||||
|
func (this *AdminDAO) FindBasicAdmin(tx *dbs.Tx, id int64) (*Admin, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Result("id", "username", "fullname").
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", AdminStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*Admin), err
|
||||||
|
}
|
||||||
|
|
||||||
// ExistEnabledAdmin 检查管理员是否存在
|
// ExistEnabledAdmin 检查管理员是否存在
|
||||||
func (this *AdminDAO) ExistEnabledAdmin(tx *dbs.Tx, adminId int64) (bool, error) {
|
func (this *AdminDAO) ExistEnabledAdmin(tx *dbs.Tx, adminId int64) (bool, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
|
|||||||
@@ -14,23 +14,23 @@ type Admin struct {
|
|||||||
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
Modules dbs.JSON `field:"modules"` // 允许的模块
|
Modules dbs.JSON `field:"modules"` // 允许的模块
|
||||||
CanLogin uint8 `field:"canLogin"` // 是否可以登录
|
CanLogin bool `field:"canLogin"` // 是否可以登录
|
||||||
Theme string `field:"theme"` // 模板设置
|
Theme string `field:"theme"` // 模板设置
|
||||||
}
|
}
|
||||||
|
|
||||||
type AdminOperator struct {
|
type AdminOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Username interface{} // 用户名
|
Username any // 用户名
|
||||||
Password interface{} // 密码
|
Password any // 密码
|
||||||
Fullname interface{} // 全名
|
Fullname any // 全名
|
||||||
IsSuper interface{} // 是否为超级管理员
|
IsSuper any // 是否为超级管理员
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
UpdatedAt interface{} // 修改时间
|
UpdatedAt any // 修改时间
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
Modules interface{} // 允许的模块
|
Modules any // 允许的模块
|
||||||
CanLogin interface{} // 是否可以登录
|
CanLogin any // 是否可以登录
|
||||||
Theme interface{} // 模板设置
|
Theme any // 模板设置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAdminOperator() *AdminOperator {
|
func NewAdminOperator() *AdminOperator {
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
package authority
|
package authority
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
|
||||||
_ "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"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthorityKeyDAO dbs.DAO
|
type AuthorityKeyDAO dbs.DAO
|
||||||
@@ -33,63 +29,3 @@ func init() {
|
|||||||
_, _ = SharedAuthorityKeyDAO.IsPlus(nil)
|
_, _ = SharedAuthorityKeyDAO.IsPlus(nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateKey 设置Key
|
|
||||||
func (this *AuthorityKeyDAO) UpdateKey(tx *dbs.Tx, value string, dayFrom string, dayTo string, hostname string, macAddresses []string, company string) error {
|
|
||||||
one, err := this.Query(tx).
|
|
||||||
AscPk().
|
|
||||||
Find()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var op = NewAuthorityKeyOperator()
|
|
||||||
if one != nil {
|
|
||||||
op.Id = one.(*AuthorityKey).Id
|
|
||||||
}
|
|
||||||
op.Value = value
|
|
||||||
op.DayFrom = dayFrom
|
|
||||||
op.DayTo = dayTo
|
|
||||||
op.Hostname = hostname
|
|
||||||
|
|
||||||
if len(macAddresses) == 0 {
|
|
||||||
macAddresses = []string{}
|
|
||||||
}
|
|
||||||
macAddressesJSON, err := json.Marshal(macAddresses)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
op.MacAddresses = macAddressesJSON
|
|
||||||
op.Company = company
|
|
||||||
op.UpdatedAt = time.Now().Unix()
|
|
||||||
|
|
||||||
return this.Save(tx, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadKey 读取Key
|
|
||||||
func (this *AuthorityKeyDAO) ReadKey(tx *dbs.Tx) (key *AuthorityKey, err error) {
|
|
||||||
one, err := this.Query(tx).
|
|
||||||
AscPk().
|
|
||||||
Find()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if one == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
key = one.(*AuthorityKey)
|
|
||||||
|
|
||||||
// 顺便更新相关变量
|
|
||||||
if key.DayTo >= timeutil.Format("Y-m-d") {
|
|
||||||
teaconst.IsPlus = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResetKey 重置Key
|
|
||||||
func (this *AuthorityKeyDAO) ResetKey(tx *dbs.Tx) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Delete()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
package authority
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestAuthorityKeyDAO_UpdateValue(t *testing.T) {
|
|
||||||
err := NewAuthorityKeyDAO().UpdateKey(nil, "12345678", "", "", "", []string{}, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log("ok")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAuthorityKeyDAO_ReadValue(t *testing.T) {
|
|
||||||
value, err := NewAuthorityKeyDAO().ReadKey(nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(value)
|
|
||||||
}
|
|
||||||
@@ -93,7 +93,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ func (this *HTTPAccessLogPolicyDAO) CountAllEnabledPolicies(tx *dbs.Tx) (int64,
|
|||||||
func (this *HTTPAccessLogPolicyDAO) ListEnabledPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPAccessLogPolicy, err error) {
|
func (this *HTTPAccessLogPolicyDAO) ListEnabledPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPAccessLogPolicy, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(HTTPAccessLogPolicyStateEnabled).
|
State(HTTPAccessLogPolicyStateEnabled).
|
||||||
|
Desc("isOn").
|
||||||
DescPk().
|
DescPk().
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Limit(size).
|
Limit(size).
|
||||||
|
|||||||
@@ -12,23 +12,23 @@ type HTTPCacheTask struct {
|
|||||||
Day string `field:"day"` // 创建日期YYYYMMDD
|
Day string `field:"day"` // 创建日期YYYYMMDD
|
||||||
IsDone bool `field:"isDone"` // 是否已完成
|
IsDone bool `field:"isDone"` // 是否已完成
|
||||||
IsOk bool `field:"isOk"` // 是否完全成功
|
IsOk bool `field:"isOk"` // 是否完全成功
|
||||||
IsReady uint8 `field:"isReady"` // 是否已准备好
|
IsReady bool `field:"isReady"` // 是否已准备好
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPCacheTaskOperator struct {
|
type HTTPCacheTaskOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
UserId interface{} // 用户ID
|
UserId any // 用户ID
|
||||||
Type interface{} // 任务类型:purge|fetch
|
Type any // 任务类型:purge|fetch
|
||||||
KeyType interface{} // Key类型
|
KeyType any // Key类型
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
DoneAt interface{} // 完成时间
|
DoneAt any // 完成时间
|
||||||
Day interface{} // 创建日期YYYYMMDD
|
Day any // 创建日期YYYYMMDD
|
||||||
IsDone interface{} // 是否已完成
|
IsDone any // 是否已完成
|
||||||
IsOk interface{} // 是否完全成功
|
IsOk any // 是否完全成功
|
||||||
IsReady interface{} // 是否已准备好
|
IsReady any // 是否已准备好
|
||||||
Description interface{} // 描述
|
Description any // 描述
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPCacheTaskOperator() *HTTPCacheTaskOperator {
|
func NewHTTPCacheTaskOperator() *HTTPCacheTaskOperator {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableIPLibrary 启用条目
|
||||||
func (this *IPLibraryDAO) EnableIPLibrary(tx *dbs.Tx, id int64) error {
|
func (this *IPLibraryDAO) EnableIPLibrary(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -42,7 +42,7 @@ func (this *IPLibraryDAO) EnableIPLibrary(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableIPLibrary 禁用条目
|
||||||
func (this *IPLibraryDAO) DisableIPLibrary(tx *dbs.Tx, id int64) error {
|
func (this *IPLibraryDAO) DisableIPLibrary(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -51,7 +51,7 @@ func (this *IPLibraryDAO) DisableIPLibrary(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledIPLibrary 查找启用中的条目
|
||||||
func (this *IPLibraryDAO) FindEnabledIPLibrary(tx *dbs.Tx, id int64) (*IPLibrary, error) {
|
func (this *IPLibraryDAO) FindEnabledIPLibrary(tx *dbs.Tx, id int64) (*IPLibrary, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -63,7 +63,7 @@ func (this *IPLibraryDAO) FindEnabledIPLibrary(tx *dbs.Tx, id int64) (*IPLibrary
|
|||||||
return result.(*IPLibrary), err
|
return result.(*IPLibrary), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找某个类型的IP库列表
|
// FindAllEnabledIPLibrariesWithType 查找某个类型的IP库列表
|
||||||
func (this *IPLibraryDAO) FindAllEnabledIPLibrariesWithType(tx *dbs.Tx, libraryType string) (result []*IPLibrary, err error) {
|
func (this *IPLibraryDAO) FindAllEnabledIPLibrariesWithType(tx *dbs.Tx, libraryType string) (result []*IPLibrary, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(IPLibraryStateEnabled).
|
State(IPLibraryStateEnabled).
|
||||||
@@ -74,7 +74,7 @@ func (this *IPLibraryDAO) FindAllEnabledIPLibrariesWithType(tx *dbs.Tx, libraryT
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找某个类型的最新的IP库
|
// FindLatestIPLibraryWithType 查找某个类型的最新的IP库
|
||||||
func (this *IPLibraryDAO) FindLatestIPLibraryWithType(tx *dbs.Tx, libraryType string) (*IPLibrary, error) {
|
func (this *IPLibraryDAO) FindLatestIPLibraryWithType(tx *dbs.Tx, libraryType string) (*IPLibrary, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
State(IPLibraryStateEnabled).
|
State(IPLibraryStateEnabled).
|
||||||
@@ -90,7 +90,7 @@ func (this *IPLibraryDAO) FindLatestIPLibraryWithType(tx *dbs.Tx, libraryType st
|
|||||||
return one.(*IPLibrary), nil
|
return one.(*IPLibrary), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建新的IP库
|
// CreateIPLibrary 创建新的IP库
|
||||||
func (this *IPLibraryDAO) CreateIPLibrary(tx *dbs.Tx, libraryType string, fileId int64) (int64, error) {
|
func (this *IPLibraryDAO) CreateIPLibrary(tx *dbs.Tx, libraryType string, fileId int64) (int64, error) {
|
||||||
var op = NewIPLibraryOperator()
|
var op = NewIPLibraryOperator()
|
||||||
op.Type = libraryType
|
op.Type = libraryType
|
||||||
|
|||||||
572
internal/db/models/ip_library_file_dao.go
Normal file
572
internal/db/models/ip_library_file_dao.go
Normal file
@@ -0,0 +1,572 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
||||||
|
"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"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
IPLibraryFileStateEnabled = 1 // 已启用
|
||||||
|
IPLibraryFileStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type IPLibraryFileDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewIPLibraryFileDAO() *IPLibraryFileDAO {
|
||||||
|
return dbs.NewDAO(&IPLibraryFileDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeIPLibraryFiles",
|
||||||
|
Model: new(IPLibraryFile),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*IPLibraryFileDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedIPLibraryFileDAO *IPLibraryFileDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedIPLibraryFileDAO = NewIPLibraryFileDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableIPLibraryFile 启用条目
|
||||||
|
func (this *IPLibraryFileDAO) EnableIPLibraryFile(tx *dbs.Tx, id uint64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", IPLibraryFileStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableIPLibraryFile 禁用条目
|
||||||
|
func (this *IPLibraryFileDAO) DisableIPLibraryFile(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", IPLibraryFileStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledIPLibraryFile 查找启用中的条目
|
||||||
|
func (this *IPLibraryFileDAO) FindEnabledIPLibraryFile(tx *dbs.Tx, id int64) (*IPLibraryFile, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
State(IPLibraryFileStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*IPLibraryFile), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
var op = NewIPLibraryFileOperator()
|
||||||
|
op.Template = template
|
||||||
|
|
||||||
|
if emptyValues == nil {
|
||||||
|
emptyValues = []string{}
|
||||||
|
}
|
||||||
|
emptyValuesJSON, err := json.Marshal(emptyValues)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.EmptyValues = emptyValuesJSON
|
||||||
|
|
||||||
|
op.FileId = fileId
|
||||||
|
|
||||||
|
if countries == nil {
|
||||||
|
countries = []string{}
|
||||||
|
}
|
||||||
|
countriesJSON, err := json.Marshal(countries)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Countries = countriesJSON
|
||||||
|
|
||||||
|
if provinces == nil {
|
||||||
|
provinces = [][2]string{}
|
||||||
|
}
|
||||||
|
provincesJSON, err := json.Marshal(provinces)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Provinces = provincesJSON
|
||||||
|
|
||||||
|
if cities == nil {
|
||||||
|
cities = [][3]string{}
|
||||||
|
}
|
||||||
|
citiesJSON, err := json.Marshal(cities)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Cities = citiesJSON
|
||||||
|
|
||||||
|
if towns == nil {
|
||||||
|
towns = [][4]string{}
|
||||||
|
}
|
||||||
|
townsJSON, err := json.Marshal(towns)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Towns = townsJSON
|
||||||
|
|
||||||
|
if providers == nil {
|
||||||
|
providers = []string{}
|
||||||
|
}
|
||||||
|
providersJSON, err := json.Marshal(providers)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Providers = providersJSON
|
||||||
|
|
||||||
|
op.IsFinished = false
|
||||||
|
op.State = IPLibraryFileStateEnabled
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllUnfinishedLibraryFiles 查找所有未完成的文件
|
||||||
|
func (this *IPLibraryFileDAO) FindAllUnfinishedLibraryFiles(tx *dbs.Tx) (result []*IPLibraryFile, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(IPLibraryFileStateEnabled).
|
||||||
|
Result("id", "fileId", "createdAt"). // 这里不需要其他信息
|
||||||
|
Attr("isFinished", false).
|
||||||
|
DescPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLibraryFileIsFinished 设置文件为已完成
|
||||||
|
func (this *IPLibraryFileDAO) UpdateLibraryFileIsFinished(tx *dbs.Tx, fileId int64) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(fileId).
|
||||||
|
Set("isFinished", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileCountries 获取IP库中的国家/地区
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileCountries(tx *dbs.Tx, fileId int64) ([]string, error) {
|
||||||
|
countriesJSON, err := this.Query(tx).
|
||||||
|
Result("countries").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(countriesJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []string{}
|
||||||
|
err = json.Unmarshal(countriesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileProvinces 获取IP库中的省份
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileProvinces(tx *dbs.Tx, fileId int64) ([][2]string, error) {
|
||||||
|
provincesJSON, err := this.Query(tx).
|
||||||
|
Result("provinces").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(provincesJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = [][2]string{}
|
||||||
|
err = json.Unmarshal(provincesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileCities 获取IP库中的城市
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileCities(tx *dbs.Tx, fileId int64) ([][3]string, error) {
|
||||||
|
citiesJSON, err := this.Query(tx).
|
||||||
|
Result("cities").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(citiesJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = [][3]string{}
|
||||||
|
err = json.Unmarshal(citiesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileTowns 获取IP库中的区县
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileTowns(tx *dbs.Tx, fileId int64) ([][4]string, error) {
|
||||||
|
townsJSON, err := this.Query(tx).
|
||||||
|
Result("towns").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(townsJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = [][4]string{}
|
||||||
|
err = json.Unmarshal(townsJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileProviders 获取IP库中的ISP运营商
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileProviders(tx *dbs.Tx, fileId int64) ([]string, error) {
|
||||||
|
providersJSON, err := this.Query(tx).
|
||||||
|
Result("providers").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(providersJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []string{}
|
||||||
|
err = json.Unmarshal(providersJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64) error {
|
||||||
|
one, err := this.Query(tx).Pk(libraryFileId).Find()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if one == nil {
|
||||||
|
return errors.New("the library file not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
var libraryFile = one.(*IPLibraryFile)
|
||||||
|
template, err := iplibrary.NewTemplate(libraryFile.Template)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("create template from '" + libraryFile.Template + "' failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileId = int64(libraryFile.FileId)
|
||||||
|
if fileId == 0 {
|
||||||
|
return errors.New("the library file has not been uploaded yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
var dir = Tea.Root + "/data"
|
||||||
|
stat, err := os.Stat(dir)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.Mkdir(dir, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
|
||||||
|
}
|
||||||
|
} else if !stat.IsDir() {
|
||||||
|
_ = os.Remove(dir)
|
||||||
|
|
||||||
|
err = os.Mkdir(dir, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO 删除以往生成的文件,但要考虑到文件正在被别的任务所使用
|
||||||
|
|
||||||
|
// 国家
|
||||||
|
dbCountries, err := regions.SharedRegionCountryDAO.FindAllCountries(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var countries = []*iplibrary.Country{}
|
||||||
|
for _, country := range dbCountries {
|
||||||
|
countries = append(countries, &iplibrary.Country{
|
||||||
|
Id: int64(country.Id),
|
||||||
|
Name: country.DisplayName(),
|
||||||
|
Codes: country.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 省份
|
||||||
|
dbProvinces, err := regions.SharedRegionProvinceDAO.FindAllEnabledProvinces(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var provinces = []*iplibrary.Province{}
|
||||||
|
for _, province := range dbProvinces {
|
||||||
|
provinces = append(provinces, &iplibrary.Province{
|
||||||
|
Id: int64(province.Id),
|
||||||
|
Name: province.DisplayName(),
|
||||||
|
Codes: province.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 城市
|
||||||
|
dbCities, err := regions.SharedRegionCityDAO.FindAllEnabledCities(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var cities = []*iplibrary.City{}
|
||||||
|
for _, city := range dbCities {
|
||||||
|
cities = append(cities, &iplibrary.City{
|
||||||
|
Id: int64(city.Id),
|
||||||
|
Name: city.DisplayName(),
|
||||||
|
Codes: city.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 区县
|
||||||
|
dbTowns, err := regions.SharedRegionTownDAO.FindAllRegionTowns(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var towns = []*iplibrary.Town{}
|
||||||
|
for _, town := range dbTowns {
|
||||||
|
towns = append(towns, &iplibrary.Town{
|
||||||
|
Id: int64(town.Id),
|
||||||
|
Name: town.DisplayName(),
|
||||||
|
Codes: town.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ISP运营商
|
||||||
|
dbProviders, err := regions.SharedRegionProviderDAO.FindAllEnabledProviders(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var providers = []*iplibrary.Provider{}
|
||||||
|
for _, provider := range dbProviders {
|
||||||
|
providers = append(providers, &iplibrary.Provider{
|
||||||
|
Id: int64(provider.Id),
|
||||||
|
Name: provider.DisplayName(),
|
||||||
|
Codes: provider.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var libraryCode = utils.Sha1RandomString() // 每次都生成新的code
|
||||||
|
var filePath = dir + "/" + this.composeFilename(libraryFileId, libraryCode)
|
||||||
|
writer, err := iplibrary.NewFileWriter(filePath, &iplibrary.Meta{
|
||||||
|
Author: "", // 将来用户可以自行填写
|
||||||
|
CreatedAt: time.Now().Unix(),
|
||||||
|
Countries: countries,
|
||||||
|
Provinces: provinces,
|
||||||
|
Cities: cities,
|
||||||
|
Towns: towns,
|
||||||
|
Providers: providers,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = writer.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = writer.WriteMeta()
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("write meta failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
chunkIds, err := SharedFileChunkDAO.FindAllFileChunkIds(tx, fileId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// countries etc ...
|
||||||
|
var countryMap = map[string]int64{} // countryName => countryId
|
||||||
|
for _, country := range dbCountries {
|
||||||
|
for _, code := range country.AllCodes() {
|
||||||
|
countryMap[code] = int64(country.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var provinceMap = map[string]int64{} // countryId_provinceName => provinceId
|
||||||
|
for _, province := range dbProvinces {
|
||||||
|
for _, code := range province.AllCodes() {
|
||||||
|
provinceMap[types.String(province.CountryId)+"_"+code] = int64(province.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cityMap = map[string]int64{} // provinceId_cityName => cityId
|
||||||
|
for _, city := range dbCities {
|
||||||
|
for _, code := range city.AllCodes() {
|
||||||
|
cityMap[types.String(city.ProvinceId)+"_"+code] = int64(city.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var townMap = map[string]int64{} // cityId_townName => townId
|
||||||
|
for _, town := range dbTowns {
|
||||||
|
for _, code := range town.AllCodes() {
|
||||||
|
townMap[types.String(town.CityId)+"_"+code] = int64(town.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var providerMap = map[string]int64{} // providerName => providerId
|
||||||
|
for _, provider := range dbProviders {
|
||||||
|
for _, code := range provider.AllCodes() {
|
||||||
|
providerMap[code] = int64(provider.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dataParser, err := iplibrary.NewParser(&iplibrary.ParserConfig{
|
||||||
|
Template: template,
|
||||||
|
EmptyValues: libraryFile.DecodeEmptyValues(),
|
||||||
|
Iterator: func(values map[string]string) error {
|
||||||
|
var ipFrom = values["ipFrom"]
|
||||||
|
var ipTo = values["ipTo"]
|
||||||
|
|
||||||
|
var countryName = values["country"]
|
||||||
|
var provinceName = values["province"]
|
||||||
|
var cityName = values["city"]
|
||||||
|
var townName = values["town"]
|
||||||
|
var providerName = values["provider"]
|
||||||
|
|
||||||
|
var countryId = countryMap[countryName]
|
||||||
|
var provinceId int64
|
||||||
|
var cityId int64 = 0
|
||||||
|
var townId int64 = 0
|
||||||
|
var providerId = providerMap[providerName]
|
||||||
|
|
||||||
|
if countryId > 0 {
|
||||||
|
provinceId = provinceMap[types.String(countryId)+"_"+provinceName]
|
||||||
|
if provinceId > 0 {
|
||||||
|
cityId = cityMap[types.String(provinceId)+"_"+cityName]
|
||||||
|
if cityId > 0 {
|
||||||
|
townId = townMap[types.String(cityId)+"_"+townName]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writer.Write(ipFrom, ipTo, countryId, provinceId, cityId, townId, providerId)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("write failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, chunkId := range chunkIds {
|
||||||
|
chunk, err := SharedFileChunkDAO.FindFileChunk(tx, chunkId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if chunk == nil {
|
||||||
|
return errors.New("invalid chunk file, please upload again")
|
||||||
|
}
|
||||||
|
dataParser.Write(chunk.Data)
|
||||||
|
err = dataParser.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writer.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将生成的内容写入到文件
|
||||||
|
stat, err = os.Stat(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("stat generated file failed: " + err.Error())
|
||||||
|
}
|
||||||
|
generatedFileId, err := SharedFileDAO.CreateFile(tx, 0, 0, "ipLibraryFile", "", libraryCode+".db", stat.Size(), "", false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fp, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("open generated file failed: " + err.Error())
|
||||||
|
}
|
||||||
|
var buf = make([]byte, 256*1024)
|
||||||
|
for {
|
||||||
|
n, err := fp.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
_, err = SharedFileChunkDAO.CreateFileChunk(tx, generatedFileId, buf[:n])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = SharedFileDAO.UpdateFileIsFinished(tx, generatedFileId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置code
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(libraryFileId).
|
||||||
|
Set("code", libraryCode).
|
||||||
|
Set("isFinished", true).
|
||||||
|
Set("generatedFileId", generatedFileId).
|
||||||
|
Set("generatedAt", time.Now().Unix()).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 组合IP库文件名
|
||||||
|
func (this *IPLibraryFileDAO) composeFilename(libraryFileId int64, code string) string {
|
||||||
|
return "ip-library-" + types.String(libraryFileId) + "-" + code + ".db"
|
||||||
|
}
|
||||||
19
internal/db/models/ip_library_file_dao_test.go
Normal file
19
internal/db/models/ip_library_file_dao_test.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package models_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIPLibraryFileDAO_GenerateIPLibrary(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 3)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
44
internal/db/models/ip_library_file_model.go
Normal file
44
internal/db/models/ip_library_file_model.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// IPLibraryFile IP库上传的文件
|
||||||
|
type IPLibraryFile struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
FileId uint64 `field:"fileId"` // 原始文件ID
|
||||||
|
Template string `field:"template"` // 模板
|
||||||
|
EmptyValues dbs.JSON `field:"emptyValues"` // 空值列表
|
||||||
|
GeneratedFileId uint64 `field:"generatedFileId"` // 生成的文件ID
|
||||||
|
GeneratedAt uint64 `field:"generatedAt"` // 生成时间
|
||||||
|
IsFinished bool `field:"isFinished"` // 是否已经完成
|
||||||
|
Countries dbs.JSON `field:"countries"` // 国家/地区
|
||||||
|
Provinces dbs.JSON `field:"provinces"` // 省份
|
||||||
|
Cities dbs.JSON `field:"cities"` // 城市
|
||||||
|
Towns dbs.JSON `field:"towns"` // 区县
|
||||||
|
Providers dbs.JSON `field:"providers"` // ISP服务商
|
||||||
|
Code string `field:"code"` // 文件代号
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 上传时间
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type IPLibraryFileOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
FileId any // 原始文件ID
|
||||||
|
Template any // 模板
|
||||||
|
EmptyValues any // 空值列表
|
||||||
|
GeneratedFileId any // 生成的文件ID
|
||||||
|
GeneratedAt any // 生成时间
|
||||||
|
IsFinished any // 是否已经完成
|
||||||
|
Countries any // 国家/地区
|
||||||
|
Provinces any // 省份
|
||||||
|
Cities any // 城市
|
||||||
|
Towns any // 区县
|
||||||
|
Providers any // ISP服务商
|
||||||
|
Code any // 文件代号
|
||||||
|
CreatedAt any // 上传时间
|
||||||
|
State any // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIPLibraryFileOperator() *IPLibraryFileOperator {
|
||||||
|
return &IPLibraryFileOperator{}
|
||||||
|
}
|
||||||
69
internal/db/models/ip_library_file_model_ext.go
Normal file
69
internal/db/models/ip_library_file_model_ext.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeCountries() []string {
|
||||||
|
var countries = []string{}
|
||||||
|
if IsNotNull(this.Countries) {
|
||||||
|
err := json.Unmarshal(this.Countries, &countries)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return countries
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeProvinces() [][2]string {
|
||||||
|
var provinces = [][2]string{}
|
||||||
|
if IsNotNull(this.Provinces) {
|
||||||
|
err := json.Unmarshal(this.Provinces, &provinces)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return provinces
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeCities() [][3]string {
|
||||||
|
var cities = [][3]string{}
|
||||||
|
if IsNotNull(this.Cities) {
|
||||||
|
err := json.Unmarshal(this.Cities, &cities)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cities
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeTowns() [][4]string {
|
||||||
|
var towns = [][4]string{}
|
||||||
|
if IsNotNull(this.Towns) {
|
||||||
|
err := json.Unmarshal(this.Towns, &towns)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return towns
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeProviders() []string {
|
||||||
|
var providers = []string{}
|
||||||
|
if IsNotNull(this.Providers) {
|
||||||
|
err := json.Unmarshal(this.Providers, &providers)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeEmptyValues() []string {
|
||||||
|
var result = []string{}
|
||||||
|
if IsNotNull(this.EmptyValues) {
|
||||||
|
err := json.Unmarshal(this.EmptyValues, &result)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -1,22 +1,26 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// IP库
|
// IPLibrary IP库
|
||||||
type IPLibrary struct {
|
type IPLibrary struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
FileId uint32 `field:"fileId"` // 文件ID
|
FileId uint32 `field:"fileId"` // 文件ID
|
||||||
Type string `field:"type"` // 类型
|
Type string `field:"type"` // 类型
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
IsPublic bool `field:"isPublic"` // 是否公用
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPLibraryOperator struct {
|
type IPLibraryOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
FileId interface{} // 文件ID
|
FileId any // 文件ID
|
||||||
Type interface{} // 类型
|
Type any // 类型
|
||||||
State interface{} // 状态
|
Name any // 名称
|
||||||
CreatedAt interface{} // 创建时间
|
IsPublic any // 是否公用
|
||||||
|
State any // 状态
|
||||||
|
CreatedAt any // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPLibraryOperator() *IPLibraryOperator {
|
func NewIPLibraryOperator() *IPLibraryOperator {
|
||||||
|
|||||||
@@ -1,290 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
NSDomainStateEnabled = 1 // 已启用
|
|
||||||
NSDomainStateDisabled = 0 // 已禁用
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSDomainDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewNSDomainDAO() *NSDomainDAO {
|
|
||||||
return dbs.NewDAO(&NSDomainDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNSDomains",
|
|
||||||
Model: new(NSDomain),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NSDomainDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNSDomainDAO *NSDomainDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNSDomainDAO = NewNSDomainDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableNSDomain 启用条目
|
|
||||||
func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, domainId int64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(domainId).
|
|
||||||
Set("state", NSDomainStateEnabled).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx, domainId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableNSDomain 禁用条目
|
|
||||||
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, domainId int64) error {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Pk(domainId).
|
|
||||||
Set("state", NSDomainStateDisabled).
|
|
||||||
Set("version", version).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx, domainId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledNSDomain 查找启用中的条目
|
|
||||||
func (this *NSDomainDAO) FindEnabledNSDomain(tx *dbs.Tx, id int64) (*NSDomain, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", NSDomainStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*NSDomain), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindNSDomainName 根据主键查找名称
|
|
||||||
func (this *NSDomainDAO) FindNSDomainName(tx *dbs.Tx, id int64) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateDomain 创建域名
|
|
||||||
func (this *NSDomainDAO) CreateDomain(tx *dbs.Tx, clusterId int64, userId int64, name string) (int64, error) {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var op = NewNSDomainOperator()
|
|
||||||
op.ClusterId = clusterId
|
|
||||||
op.UserId = userId
|
|
||||||
op.Name = name
|
|
||||||
op.Version = version
|
|
||||||
op.IsOn = true
|
|
||||||
op.State = NSDomainStateEnabled
|
|
||||||
domainId, err := this.SaveInt64(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = this.NotifyUpdate(tx, domainId)
|
|
||||||
if err != nil {
|
|
||||||
return domainId, err
|
|
||||||
}
|
|
||||||
return domainId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateDomain 修改域名
|
|
||||||
func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int64, userId int64, isOn bool) error {
|
|
||||||
if domainId <= 0 {
|
|
||||||
return errors.New("invalid domainId")
|
|
||||||
}
|
|
||||||
|
|
||||||
oldClusterId, err := this.Query(tx).
|
|
||||||
Pk(domainId).
|
|
||||||
Result("clusterId").
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var op = NewNSDomainOperator()
|
|
||||||
op.Id = domainId
|
|
||||||
op.ClusterId = clusterId
|
|
||||||
op.UserId = userId
|
|
||||||
op.IsOn = isOn
|
|
||||||
op.Version = version
|
|
||||||
err = this.Save(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 通知更新
|
|
||||||
if oldClusterId > 0 && oldClusterId != clusterId {
|
|
||||||
err = models.SharedNSClusterDAO.NotifyUpdate(tx, oldClusterId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.NotifyUpdate(tx, domainId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountAllEnabledDomains 计算域名数量
|
|
||||||
func (this *NSDomainDAO) CountAllEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string) (int64, error) {
|
|
||||||
query := this.Query(tx)
|
|
||||||
if clusterId > 0 {
|
|
||||||
query.Attr("clusterId", clusterId)
|
|
||||||
} else {
|
|
||||||
query.Where("clusterId IN (SELECT id FROM " + models.SharedNSClusterDAO.Table + " WHERE state=1)")
|
|
||||||
}
|
|
||||||
if userId > 0 {
|
|
||||||
query.Attr("userId", userId)
|
|
||||||
} else {
|
|
||||||
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
|
|
||||||
}
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("(name LIKE :keyword)").
|
|
||||||
Param("keyword", dbutils.QuoteLike(keyword))
|
|
||||||
}
|
|
||||||
|
|
||||||
return query.
|
|
||||||
State(NSDomainStateEnabled).
|
|
||||||
Count()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListEnabledDomains 列出单页域名
|
|
||||||
func (this *NSDomainDAO) ListEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string, offset int64, size int64) (result []*NSDomain, err error) {
|
|
||||||
query := this.Query(tx)
|
|
||||||
if clusterId > 0 {
|
|
||||||
query.Attr("clusterId", clusterId)
|
|
||||||
} else {
|
|
||||||
query.Where("clusterId IN (SELECT id FROM " + models.SharedNSClusterDAO.Table + " WHERE state=1)")
|
|
||||||
}
|
|
||||||
if userId > 0 {
|
|
||||||
query.Attr("userId", userId)
|
|
||||||
} else {
|
|
||||||
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
|
|
||||||
}
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("(name LIKE :keyword)").
|
|
||||||
Param("keyword", dbutils.QuoteLike(keyword))
|
|
||||||
}
|
|
||||||
_, err = query.
|
|
||||||
State(NSDomainStateEnabled).
|
|
||||||
DescPk().
|
|
||||||
Offset(offset).
|
|
||||||
Limit(size).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncreaseVersion 增加版本
|
|
||||||
func (this *NSDomainDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
|
||||||
return models.SharedSysLockerDAO.Increase(tx, "NS_DOMAIN_VERSION", 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListDomainsAfterVersion 列出某个版本后的域名
|
|
||||||
func (this *NSDomainDAO) ListDomainsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSDomain, err error) {
|
|
||||||
if size <= 0 {
|
|
||||||
size = 10000
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Gte("version", version).
|
|
||||||
Limit(size).
|
|
||||||
Asc("version").
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindDomainIdWithName 根据名称查找域名
|
|
||||||
func (this *NSDomainDAO) FindDomainIdWithName(tx *dbs.Tx, clusterId int64, name string) (int64, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Attr("clusterId", clusterId).
|
|
||||||
Attr("name", name).
|
|
||||||
State(NSDomainStateEnabled).
|
|
||||||
ResultPk().
|
|
||||||
FindInt64Col(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledDomainTSIG 获取TSIG配置
|
|
||||||
func (this *NSDomainDAO) FindEnabledDomainTSIG(tx *dbs.Tx, domainId int64) ([]byte, error) {
|
|
||||||
tsig, err := this.Query(tx).
|
|
||||||
Pk(domainId).
|
|
||||||
Result("tsig").
|
|
||||||
FindStringCol("")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return []byte(tsig), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateDomainTSIG 修改TSIG配置
|
|
||||||
func (this *NSDomainDAO) UpdateDomainTSIG(tx *dbs.Tx, domainId int64, tsigJSON []byte) error {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = this.Query(tx).
|
|
||||||
Pk(domainId).
|
|
||||||
Set("tsig", tsigJSON).
|
|
||||||
Set("version", version).
|
|
||||||
UpdateQuickly()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.NotifyUpdate(tx, domainId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledDomainClusterId 获取域名的集群ID
|
|
||||||
func (this *NSDomainDAO) FindEnabledDomainClusterId(tx *dbs.Tx, domainId int64) (int64, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(domainId).
|
|
||||||
State(NSDomainStateEnabled).
|
|
||||||
Result("clusterId").
|
|
||||||
FindInt64Col(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotifyUpdate 通知更改
|
|
||||||
func (this *NSDomainDAO) NotifyUpdate(tx *dbs.Tx, domainId int64) error {
|
|
||||||
clusterId, err := this.Query(tx).
|
|
||||||
Result("clusterId").
|
|
||||||
Pk(domainId).
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if clusterId > 0 {
|
|
||||||
return models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeDomainChanged)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
24
internal/db/models/nameservers/ns_domain_group_model.go
Normal file
24
internal/db/models/nameservers/ns_domain_group_model.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
// NSDomainGroup 域名分组
|
||||||
|
type NSDomainGroup struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
Name string `field:"name"` // 分组名称
|
||||||
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
|
Order uint32 `field:"order"` // 排序
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type NSDomainGroupOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
Name interface{} // 分组名称
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Order interface{} // 排序
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNSDomainGroupOperator() *NSDomainGroupOperator {
|
||||||
|
return &NSDomainGroupOperator{}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package nameservers
|
||||||
@@ -4,15 +4,17 @@ import "github.com/iwind/TeaGo/dbs"
|
|||||||
|
|
||||||
// NSDomain DNS域名
|
// NSDomain DNS域名
|
||||||
type NSDomain struct {
|
type NSDomain struct {
|
||||||
Id uint32 `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"` // 域名
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
|
||||||
Version uint64 `field:"version"` // 版本
|
|
||||||
State uint8 `field:"state"` // 状态
|
|
||||||
Tsig dbs.JSON `field:"tsig"` // TSIG配置
|
Tsig dbs.JSON `field:"tsig"` // TSIG配置
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
Version uint64 `field:"version"` // 版本号
|
||||||
|
Status string `field:"status"` // 状态:none|verified
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
type NSDomainOperator struct {
|
type NSDomainOperator struct {
|
||||||
@@ -21,10 +23,12 @@ type NSDomainOperator struct {
|
|||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
Name interface{} // 域名
|
Name interface{} // 域名
|
||||||
CreatedAt interface{} // 创建时间
|
GroupIds interface{} // 分组ID
|
||||||
Version interface{} // 版本
|
|
||||||
State interface{} // 状态
|
|
||||||
Tsig interface{} // TSIG配置
|
Tsig interface{} // TSIG配置
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
Version interface{} // 版本号
|
||||||
|
Status interface{} // 状态:none|verified
|
||||||
|
State interface{} // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNSDomainOperator() *NSDomainOperator {
|
func NewNSDomainOperator() *NSDomainOperator {
|
||||||
|
|||||||
@@ -1 +1,20 @@
|
|||||||
package nameservers
|
package nameservers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (this *NSDomain) DecodeGroupIds() []int64 {
|
||||||
|
if models.IsNull(this.GroupIds) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []int64{}
|
||||||
|
err := json.Unmarshal(this.GroupIds, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("NSDomain", "DecodeGroupIds:"+err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,209 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
NSKeyStateEnabled = 1 // 已启用
|
|
||||||
NSKeyStateDisabled = 0 // 已禁用
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSKeyDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewNSKeyDAO() *NSKeyDAO {
|
|
||||||
return dbs.NewDAO(&NSKeyDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNSKeys",
|
|
||||||
Model: new(NSKey),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NSKeyDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNSKeyDAO *NSKeyDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNSKeyDAO = NewNSKeyDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableNSKey 启用条目
|
|
||||||
func (this *NSKeyDAO) EnableNSKey(tx *dbs.Tx, id int64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", NSKeyStateEnabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableNSKey 禁用条目
|
|
||||||
func (this *NSKeyDAO) DisableNSKey(tx *dbs.Tx, keyId int64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(keyId).
|
|
||||||
Set("state", NSKeyStateDisabled).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx, keyId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledNSKey 查找启用中的条目
|
|
||||||
func (this *NSKeyDAO) FindEnabledNSKey(tx *dbs.Tx, id int64) (*NSKey, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", NSKeyStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*NSKey), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindNSKeyName 根据主键查找名称
|
|
||||||
func (this *NSKeyDAO) FindNSKeyName(tx *dbs.Tx, id int64) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateKey 创建Key
|
|
||||||
func (this *NSKeyDAO) CreateKey(tx *dbs.Tx, domainId int64, zoneId int64, name string, algo dnsconfigs.KeyAlgorithmType, secret string, secretType string) (int64, error) {
|
|
||||||
var op = NewNSKeyOperator()
|
|
||||||
op.DomainId = domainId
|
|
||||||
op.ZoneId = zoneId
|
|
||||||
op.Name = name
|
|
||||||
op.Algo = algo
|
|
||||||
op.Secret = secret
|
|
||||||
op.SecretType = secretType
|
|
||||||
op.State = NSKeyStateEnabled
|
|
||||||
keyId, err := this.SaveInt64(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = this.NotifyUpdate(tx, keyId)
|
|
||||||
if err != nil {
|
|
||||||
return keyId, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return keyId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateKey 修改Key
|
|
||||||
func (this *NSKeyDAO) UpdateKey(tx *dbs.Tx, keyId int64, name string, algo dnsconfigs.KeyAlgorithmType, secret string, secretType string, isOn bool) error {
|
|
||||||
if keyId <= 0 {
|
|
||||||
return errors.New("invalid keyId")
|
|
||||||
}
|
|
||||||
var op = NewNSKeyOperator()
|
|
||||||
op.Id = keyId
|
|
||||||
op.Name = name
|
|
||||||
op.Algo = algo
|
|
||||||
op.Secret = secret
|
|
||||||
op.SecretType = secretType
|
|
||||||
op.IsOn = isOn
|
|
||||||
err := this.Save(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx, keyId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountEnabledKeys 计算Key的数量
|
|
||||||
func (this *NSKeyDAO) CountEnabledKeys(tx *dbs.Tx, domainId int64, zoneId int64) (int64, error) {
|
|
||||||
var query = this.Query(tx).
|
|
||||||
State(NSKeyStateEnabled)
|
|
||||||
if domainId > 0 {
|
|
||||||
query.Attr("domainId", domainId)
|
|
||||||
}
|
|
||||||
if zoneId > 0 {
|
|
||||||
query.Attr("zoneId", zoneId)
|
|
||||||
}
|
|
||||||
return query.Count()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListEnabledKeys 列出单页Key
|
|
||||||
func (this *NSKeyDAO) ListEnabledKeys(tx *dbs.Tx, domainId int64, zoneId int64, offset int64, size int64) (result []*NSKey, err error) {
|
|
||||||
var query = this.Query(tx).
|
|
||||||
State(NSKeyStateEnabled)
|
|
||||||
if domainId > 0 {
|
|
||||||
query.Attr("domainId", domainId)
|
|
||||||
}
|
|
||||||
if zoneId > 0 {
|
|
||||||
query.Attr("zoneId", zoneId)
|
|
||||||
}
|
|
||||||
_, err = query.
|
|
||||||
DescPk().
|
|
||||||
Offset(offset).
|
|
||||||
Limit(size).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncreaseVersion 增加版本
|
|
||||||
func (this *NSKeyDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
|
||||||
return models.SharedSysLockerDAO.Increase(tx, "NS_KEY_VERSION", 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListKeysAfterVersion 列出某个版本后的密钥
|
|
||||||
func (this *NSKeyDAO) ListKeysAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSKey, err error) {
|
|
||||||
if size <= 0 {
|
|
||||||
size = 10000
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Gte("version", version).
|
|
||||||
Limit(size).
|
|
||||||
Asc("version").
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
|
||||||
func (this *NSKeyDAO) NotifyUpdate(tx *dbs.Tx, keyId int64) error {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = this.Query(tx).
|
|
||||||
Pk(keyId).
|
|
||||||
Set("version", version).
|
|
||||||
UpdateQuickly()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 通知集群
|
|
||||||
domainId, err := this.Query(tx).
|
|
||||||
Pk(keyId).
|
|
||||||
Result("domainId").
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if domainId > 0 {
|
|
||||||
clusterId, err := SharedNSDomainDAO.FindEnabledDomainClusterId(tx, domainId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if clusterId > 0 {
|
|
||||||
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeKeyChanged)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSQuestionOptionDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewNSQuestionOptionDAO() *NSQuestionOptionDAO {
|
|
||||||
return dbs.NewDAO(&NSQuestionOptionDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNSQuestionOptions",
|
|
||||||
Model: new(NSQuestionOption),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NSQuestionOptionDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNSQuestionOptionDAO *NSQuestionOptionDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNSQuestionOptionDAO = NewNSQuestionOptionDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindNSQuestionOptionName 根据主键查找名称
|
|
||||||
func (this *NSQuestionOptionDAO) FindNSQuestionOptionName(tx *dbs.Tx, id uint64) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateOption 创建选项
|
|
||||||
func (this *NSQuestionOptionDAO) CreateOption(tx *dbs.Tx, name string, values maps.Map) (int64, error) {
|
|
||||||
if values == nil {
|
|
||||||
values = maps.Map{}
|
|
||||||
}
|
|
||||||
var op = NewNSQuestionOptionOperator()
|
|
||||||
op.Name = name
|
|
||||||
op.Values = values.AsJSON()
|
|
||||||
return this.SaveInt64(tx, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindOption 读取选项
|
|
||||||
func (this *NSQuestionOptionDAO) FindOption(tx *dbs.Tx, optionId int64) (*NSQuestionOption, error) {
|
|
||||||
one, err := this.Query(tx).
|
|
||||||
Pk(optionId).
|
|
||||||
Find()
|
|
||||||
if one == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return one.(*NSQuestionOption), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteOption 删除选项
|
|
||||||
func (this *NSQuestionOptionDAO) DeleteOption(tx *dbs.Tx, optionId int64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(optionId).
|
|
||||||
Delete()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@@ -1,289 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
NSRecordStateEnabled = 1 // 已启用
|
|
||||||
NSRecordStateDisabled = 0 // 已禁用
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSRecordDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewNSRecordDAO() *NSRecordDAO {
|
|
||||||
return dbs.NewDAO(&NSRecordDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNSRecords",
|
|
||||||
Model: new(NSRecord),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NSRecordDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNSRecordDAO *NSRecordDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNSRecordDAO = NewNSRecordDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableNSRecord 启用条目
|
|
||||||
func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, recordId int64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(recordId).
|
|
||||||
Set("state", NSRecordStateEnabled).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx, recordId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableNSRecord 禁用条目
|
|
||||||
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, recordId int64) error {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Pk(recordId).
|
|
||||||
Set("state", NSRecordStateDisabled).
|
|
||||||
Set("version", version).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx, recordId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledNSRecord 查找启用中的条目
|
|
||||||
func (this *NSRecordDAO) FindEnabledNSRecord(tx *dbs.Tx, id int64) (*NSRecord, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", NSRecordStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*NSRecord), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindNSRecordName 根据主键查找名称
|
|
||||||
func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id int64) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateRecord 创建记录
|
|
||||||
func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []string) (int64, error) {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var op = NewNSRecordOperator()
|
|
||||||
op.DomainId = domainId
|
|
||||||
op.Description = description
|
|
||||||
op.Name = name
|
|
||||||
op.Type = dnsType
|
|
||||||
op.Value = value
|
|
||||||
op.Ttl = ttl
|
|
||||||
|
|
||||||
if len(routeIds) == 0 {
|
|
||||||
op.RouteIds = `["default"]`
|
|
||||||
} else {
|
|
||||||
routeIds, err := json.Marshal(routeIds)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
op.RouteIds = routeIds
|
|
||||||
}
|
|
||||||
|
|
||||||
op.IsOn = true
|
|
||||||
op.State = NSRecordStateEnabled
|
|
||||||
op.Version = version
|
|
||||||
recordId, err := this.SaveInt64(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = this.NotifyUpdate(tx, recordId)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return recordId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateRecord 修改记录
|
|
||||||
func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []string, isOn bool) error {
|
|
||||||
if recordId <= 0 {
|
|
||||||
return errors.New("invalid recordId")
|
|
||||||
}
|
|
||||||
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var op = NewNSRecordOperator()
|
|
||||||
op.Id = recordId
|
|
||||||
op.Description = description
|
|
||||||
op.Name = name
|
|
||||||
op.Type = dnsType
|
|
||||||
op.Value = value
|
|
||||||
op.Ttl = ttl
|
|
||||||
op.IsOn = isOn
|
|
||||||
|
|
||||||
if len(routeIds) == 0 {
|
|
||||||
op.RouteIds = `["default"]`
|
|
||||||
} else {
|
|
||||||
routeIds, err := json.Marshal(routeIds)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
op.RouteIds = routeIds
|
|
||||||
}
|
|
||||||
|
|
||||||
op.Version = version
|
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.NotifyUpdate(tx, recordId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountAllEnabledDomainRecords 计算域名中记录数量
|
|
||||||
func (this *NSRecordDAO) CountAllEnabledDomainRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeCode string) (int64, error) {
|
|
||||||
query := this.Query(tx).
|
|
||||||
Attr("domainId", domainId).
|
|
||||||
State(NSRecordStateEnabled)
|
|
||||||
if len(dnsType) > 0 {
|
|
||||||
query.Attr("type", dnsType)
|
|
||||||
}
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
|
||||||
Param("keyword", dbutils.QuoteLike(keyword))
|
|
||||||
}
|
|
||||||
if len(routeCode) > 0 {
|
|
||||||
routeCodeJSON, err := json.Marshal(routeCode)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
query.JSONContains("routeIds", string(routeCodeJSON))
|
|
||||||
}
|
|
||||||
return query.Count()
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountAllEnabledRecords 计算所有记录数量
|
|
||||||
func (this *NSRecordDAO) CountAllEnabledRecords(tx *dbs.Tx) (int64, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Where("domainId IN (SELECT id FROM " + SharedNSDomainDAO.Table + " WHERE state=1)").
|
|
||||||
State(NSRecordStateEnabled).
|
|
||||||
Count()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListEnabledRecords 列出单页记录
|
|
||||||
func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeCode string, offset int64, size int64) (result []*NSRecord, err error) {
|
|
||||||
query := this.Query(tx).
|
|
||||||
Attr("domainId", domainId).
|
|
||||||
State(NSRecordStateEnabled)
|
|
||||||
if len(dnsType) > 0 {
|
|
||||||
query.Attr("type", dnsType)
|
|
||||||
}
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
|
||||||
Param("keyword", dbutils.QuoteLike(keyword))
|
|
||||||
}
|
|
||||||
if len(routeCode) > 0 {
|
|
||||||
routeCodeJSON, err := json.Marshal(routeCode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
query.JSONContains("routeIds", string(routeCodeJSON))
|
|
||||||
}
|
|
||||||
_, err = query.
|
|
||||||
DescPk().
|
|
||||||
Offset(offset).
|
|
||||||
Limit(size).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncreaseVersion 增加版本
|
|
||||||
func (this *NSRecordDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
|
||||||
return models.SharedSysLockerDAO.Increase(tx, "NS_RECORD_VERSION", 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListRecordsAfterVersion 列出某个版本后的记录
|
|
||||||
func (this *NSRecordDAO) ListRecordsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRecord, err error) {
|
|
||||||
if size <= 0 {
|
|
||||||
size = 10000
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Gte("version", version).
|
|
||||||
Limit(size).
|
|
||||||
Asc("version").
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledRecordWithName 查询单条记录
|
|
||||||
func (this *NSRecordDAO) FindEnabledRecordWithName(tx *dbs.Tx, domainId int64, recordName string, recordType dnsconfigs.RecordType) (*NSRecord, error) {
|
|
||||||
record, err := this.Query(tx).
|
|
||||||
State(NSRecordStateEnabled).
|
|
||||||
Attr("domainId", domainId).
|
|
||||||
Attr("name", recordName).
|
|
||||||
Attr("type", recordType).
|
|
||||||
Find()
|
|
||||||
if record == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return record.(*NSRecord), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
|
||||||
func (this *NSRecordDAO) NotifyUpdate(tx *dbs.Tx, recordId int64) error {
|
|
||||||
domainId, err := this.Query(tx).
|
|
||||||
Pk(recordId).
|
|
||||||
Result("domainId").
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if domainId == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
clusterId, err := SharedNSDomainDAO.FindEnabledDomainClusterId(tx, domainId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if clusterId > 0 {
|
|
||||||
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeRecordChanged)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNSRecord_DecodeRouteIds(t *testing.T) {
|
|
||||||
{
|
|
||||||
record := &NSRecord{}
|
|
||||||
t.Log(record.DecodeRouteIds())
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
record := &NSRecord{RouteIds: []byte("[]")}
|
|
||||||
t.Log(record.DecodeRouteIds())
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
record := &NSRecord{RouteIds: []byte("[1, 2, 3]")}
|
|
||||||
t.Log(record.DecodeRouteIds())
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
record := &NSRecord{RouteIds: []byte(`["id:1", "id:2", "isp:liantong"]`)}
|
|
||||||
t.Log(record.DecodeRouteIds())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,169 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/rands"
|
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSRecordHourlyStatDAO dbs.DAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReadyDone(func() {
|
|
||||||
// 清理数据任务
|
|
||||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
|
||||||
goman.New(func() {
|
|
||||||
for range ticker.C {
|
|
||||||
err := SharedNSRecordHourlyStatDAO.Clean(nil, 30) // 只保留N天
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("NodeClusterTrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewNSRecordHourlyStatDAO() *NSRecordHourlyStatDAO {
|
|
||||||
return dbs.NewDAO(&NSRecordHourlyStatDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNSRecordHourlyStats",
|
|
||||||
Model: new(NSRecordHourlyStat),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NSRecordHourlyStatDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNSRecordHourlyStatDAO *NSRecordHourlyStatDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNSRecordHourlyStatDAO = NewNSRecordHourlyStatDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncreaseHourlyStat 增加统计数据
|
|
||||||
func (this *NSRecordHourlyStatDAO) IncreaseHourlyStat(tx *dbs.Tx, clusterId int64, nodeId int64, hour string, domainId int64, recordId int64, countRequests int64, bytes int64) error {
|
|
||||||
if len(hour) != 10 {
|
|
||||||
return errors.New("invalid hour '" + hour + "'")
|
|
||||||
}
|
|
||||||
return this.Query(tx).
|
|
||||||
Param("countRequests", countRequests).
|
|
||||||
Param("bytes", bytes).
|
|
||||||
InsertOrUpdateQuickly(maps.Map{
|
|
||||||
"clusterId": clusterId,
|
|
||||||
"nodeId": nodeId,
|
|
||||||
"domainId": domainId,
|
|
||||||
"recordId": recordId,
|
|
||||||
"day": hour[:8],
|
|
||||||
"hour": hour,
|
|
||||||
"countRequests": countRequests,
|
|
||||||
"bytes": bytes,
|
|
||||||
}, maps.Map{
|
|
||||||
"countRequests": dbs.SQL("countRequests+:countRequests"),
|
|
||||||
"bytes": dbs.SQL("bytes+:bytes"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindHourlyStats 按小时统计
|
|
||||||
func (this *NSRecordHourlyStatDAO) FindHourlyStats(tx *dbs.Tx, hourFrom string, hourTo string) (result []*NSRecordHourlyStat, err error) {
|
|
||||||
ones, err := this.Query(tx).
|
|
||||||
Result("hour", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
|
||||||
Between("hour", hourFrom, hourTo).
|
|
||||||
Group("hour").
|
|
||||||
FindAll()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var m = map[string]*NSRecordHourlyStat{} // hour => *NSRecordHourlyStat
|
|
||||||
for _, one := range ones {
|
|
||||||
m[one.(*NSRecordHourlyStat).Hour] = one.(*NSRecordHourlyStat)
|
|
||||||
}
|
|
||||||
hours, err := utils.RangeHours(hourFrom, hourTo)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, hour := range hours {
|
|
||||||
stat, ok := m[hour]
|
|
||||||
if ok {
|
|
||||||
result = append(result, stat)
|
|
||||||
} else {
|
|
||||||
result = append(result, &NSRecordHourlyStat{
|
|
||||||
Hour: hour,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindDailyStats 按天统计
|
|
||||||
func (this *NSRecordHourlyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*NSRecordHourlyStat, err error) {
|
|
||||||
ones, err := this.Query(tx).
|
|
||||||
Result("day", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
|
||||||
Between("day", dayFrom, dayTo).
|
|
||||||
Group("day").
|
|
||||||
FindAll()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var m = map[string]*NSRecordHourlyStat{} // day => *NSRecordHourlyStat
|
|
||||||
for _, one := range ones {
|
|
||||||
m[one.(*NSRecordHourlyStat).Day] = one.(*NSRecordHourlyStat)
|
|
||||||
}
|
|
||||||
days, err := utils.RangeDays(dayFrom, dayTo)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, day := range days {
|
|
||||||
stat, ok := m[day]
|
|
||||||
if ok {
|
|
||||||
result = append(result, stat)
|
|
||||||
} else {
|
|
||||||
result = append(result, &NSRecordHourlyStat{
|
|
||||||
Day: day,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListTopNodes 节点排行
|
|
||||||
func (this *NSRecordHourlyStatDAO) ListTopNodes(tx *dbs.Tx, hourFrom string, hourTo string, size int64) (result []*NSRecordHourlyStat, err error) {
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Result("MIN(clusterId) AS clusterId", "nodeId", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
|
||||||
Between("hour", hourFrom, hourTo).
|
|
||||||
Group("nodeId").
|
|
||||||
Limit(size).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListTopDomains 域名排行
|
|
||||||
func (this *NSRecordHourlyStatDAO) ListTopDomains(tx *dbs.Tx, hourFrom string, hourTo string, size int64) (result []*NSRecordHourlyStat, err error) {
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Result("domainId", "SUM(countRequests) AS countRequests", "SUM(bytes) AS bytes").
|
|
||||||
Between("hour", hourFrom, hourTo).
|
|
||||||
Group("domainId").
|
|
||||||
Limit(size).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean 清理历史数据
|
|
||||||
func (this *NSRecordHourlyStatDAO) Clean(tx *dbs.Tx, days int) error {
|
|
||||||
var hour = timeutil.Format("Ymd00", time.Now().AddDate(0, 0, -days))
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Lt("hour", hour).
|
|
||||||
Delete()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@@ -1,268 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
NSRouteStateEnabled = 1 // 已启用
|
|
||||||
NSRouteStateDisabled = 0 // 已禁用
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSRouteDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewNSRouteDAO() *NSRouteDAO {
|
|
||||||
return dbs.NewDAO(&NSRouteDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNSRoutes",
|
|
||||||
Model: new(NSRoute),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NSRouteDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNSRouteDAO *NSRouteDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNSRouteDAO = NewNSRouteDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableNSRoute 启用条目
|
|
||||||
func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, routeId int64) error {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Pk(routeId).
|
|
||||||
Set("state", NSRouteStateEnabled).
|
|
||||||
Set("version", version).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableNSRoute 禁用条目
|
|
||||||
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, routeId int64) error {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Pk(routeId).
|
|
||||||
Set("state", NSRouteStateDisabled).
|
|
||||||
Set("version", version).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return this.NotifyUpdate(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledNSRoute 查找启用中的条目
|
|
||||||
func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id int64) (*NSRoute, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", NSRouteStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*NSRoute), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledRouteWithCode 根据代号获取线路信息
|
|
||||||
func (this *NSRouteDAO) FindEnabledRouteWithCode(tx *dbs.Tx, code string) (*NSRoute, error) {
|
|
||||||
if regexp.MustCompile(`^id:\d+$`).MatchString(code) {
|
|
||||||
var routeId = types.Int64(code[strings.Index(code, ":")+1:])
|
|
||||||
route, err := this.FindEnabledNSRoute(tx, routeId)
|
|
||||||
if route == nil || err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
route.Code = "id:" + types.String(routeId)
|
|
||||||
return route, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
route := dnsconfigs.FindDefaultRoute(code)
|
|
||||||
if route == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return &NSRoute{
|
|
||||||
Id: 0,
|
|
||||||
IsOn: true,
|
|
||||||
Name: route.Name,
|
|
||||||
Code: route.Code,
|
|
||||||
State: NSRouteStateEnabled,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindNSRouteName 根据主键查找名称
|
|
||||||
func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateRoute 创建线路
|
|
||||||
func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64, userId int64, name string, rangesJSON []byte) (int64, error) {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var op = NewNSRouteOperator()
|
|
||||||
op.ClusterId = clusterId
|
|
||||||
op.DomainId = domainId
|
|
||||||
op.UserId = userId
|
|
||||||
op.Name = name
|
|
||||||
if len(rangesJSON) > 0 {
|
|
||||||
op.Ranges = rangesJSON
|
|
||||||
} else {
|
|
||||||
op.Ranges = "[]"
|
|
||||||
}
|
|
||||||
op.IsOn = true
|
|
||||||
op.State = NSRouteStateEnabled
|
|
||||||
op.Version = version
|
|
||||||
routeId, err := this.SaveInt64(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = this.NotifyUpdate(tx)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return routeId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateRoute 修改线路
|
|
||||||
func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rangesJSON []byte) error {
|
|
||||||
if routeId <= 0 {
|
|
||||||
return errors.New("invalid routeId")
|
|
||||||
}
|
|
||||||
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var op = NewNSRouteOperator()
|
|
||||||
op.Id = routeId
|
|
||||||
op.Name = name
|
|
||||||
if len(rangesJSON) > 0 {
|
|
||||||
op.Ranges = rangesJSON
|
|
||||||
} else {
|
|
||||||
op.Ranges = "[]"
|
|
||||||
}
|
|
||||||
|
|
||||||
op.Version = version
|
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.NotifyUpdate(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateRouteOrders 修改线路排序
|
|
||||||
func (this *NSRouteDAO) UpdateRouteOrders(tx *dbs.Tx, routeIds []int64) error {
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
order := len(routeIds)
|
|
||||||
for _, routeId := range routeIds {
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Pk(routeId).
|
|
||||||
Set("order", order).
|
|
||||||
Set("version", version).
|
|
||||||
Update()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
order--
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.NotifyUpdate(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindAllEnabledRoutes 列出所有线路
|
|
||||||
func (this *NSRouteDAO) FindAllEnabledRoutes(tx *dbs.Tx, clusterId int64, domainId int64, userId int64) (result []*NSRoute, err error) {
|
|
||||||
query := this.Query(tx).
|
|
||||||
State(NSRouteStateEnabled).
|
|
||||||
Slice(&result).
|
|
||||||
Desc("order").
|
|
||||||
AscPk()
|
|
||||||
if clusterId > 0 {
|
|
||||||
query.Attr("clusterId", clusterId)
|
|
||||||
} else {
|
|
||||||
// 不查询所有集群的线路
|
|
||||||
query.Attr("clusterId", 0)
|
|
||||||
}
|
|
||||||
if domainId > 0 {
|
|
||||||
query.Attr("domainId", domainId)
|
|
||||||
}
|
|
||||||
if userId > 0 {
|
|
||||||
query.Attr("userId", userId)
|
|
||||||
}
|
|
||||||
_, err = query.FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncreaseVersion 增加版本
|
|
||||||
func (this *NSRouteDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
|
||||||
return models.SharedSysLockerDAO.Increase(tx, "NS_ROUTE_VERSION", 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListRoutesAfterVersion 列出某个版本后的域名
|
|
||||||
func (this *NSRouteDAO) ListRoutesAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRoute, err error) {
|
|
||||||
if size <= 0 {
|
|
||||||
size = 10000
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Gte("version", version).
|
|
||||||
Limit(size).
|
|
||||||
Asc("version").
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
|
||||||
func (this *NSRouteDAO) NotifyUpdate(tx *dbs.Tx) error {
|
|
||||||
// 线路变更时所有集群都要更新
|
|
||||||
clusterIds, err := models.SharedNSClusterDAO.FindAllEnabledClusterIds(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, clusterId := range clusterIds {
|
|
||||||
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, models.NSNodeTaskTypeRouteChanged)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
)
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
NSZoneStateEnabled = 1 // 已启用
|
|
||||||
NSZoneStateDisabled = 0 // 已禁用
|
|
||||||
)
|
|
||||||
|
|
||||||
type NSZoneDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewNSZoneDAO() *NSZoneDAO {
|
|
||||||
return dbs.NewDAO(&NSZoneDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeNSZones",
|
|
||||||
Model: new(NSZone),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*NSZoneDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedNSZoneDAO *NSZoneDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedNSZoneDAO = NewNSZoneDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableNSZone 启用条目
|
|
||||||
func (this *NSZoneDAO) EnableNSZone(tx *dbs.Tx, id uint64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", NSZoneStateEnabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableNSZone 禁用条目
|
|
||||||
func (this *NSZoneDAO) DisableNSZone(tx *dbs.Tx, id uint64) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", NSZoneStateDisabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledNSZone 查找启用中的条目
|
|
||||||
func (this *NSZoneDAO) FindEnabledNSZone(tx *dbs.Tx, id uint64) (*NSZone, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", NSZoneStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*NSZone), err
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
)
|
|
||||||
@@ -55,12 +55,16 @@ func (this *NodeClusterDAO) EnableNodeCluster(tx *dbs.Tx, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DisableNodeCluster 禁用条目
|
// DisableNodeCluster 禁用条目
|
||||||
func (this *NodeClusterDAO) DisableNodeCluster(tx *dbs.Tx, id int64) error {
|
func (this *NodeClusterDAO) DisableNodeCluster(tx *dbs.Tx, clusterId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(clusterId).
|
||||||
Set("state", NodeClusterStateDisabled).
|
Set("state", NodeClusterStateDisabled).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return SharedNodeLogDAO.DeleteNodeLogsWithCluster(tx, nodeconfigs.NodeRoleNode, clusterId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledNodeCluster 查找集群
|
// FindEnabledNodeCluster 查找集群
|
||||||
|
|||||||
@@ -2,4 +2,16 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNodeClusterDAO_DisableNodeCluster(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := SharedNodeClusterDAO.DisableNodeCluster(nil, 46)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -1668,10 +1668,31 @@ func (this *NodeDAO) UpdateNodeActive(tx *dbs.Tx, nodeId int64, isActive bool) e
|
|||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(nodeId).
|
Pk(nodeId).
|
||||||
Set("isActive", isActive).
|
Set("isActive", isActive).
|
||||||
|
Set("inactiveNotifiedAt", 0).
|
||||||
Update()
|
Update()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateNodeInactiveNotifiedAt 修改节点的离线通知时间
|
||||||
|
func (this *NodeDAO) UpdateNodeInactiveNotifiedAt(tx *dbs.Tx, nodeId int64, inactiveAt int64) error {
|
||||||
|
if nodeId <= 0 {
|
||||||
|
return errors.New("invalid nodeId")
|
||||||
|
}
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Set("inactiveNotifiedAt", inactiveAt).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindNodeInactiveNotifiedAt 读取上次的节点离线通知时间
|
||||||
|
func (this *NodeDAO) FindNodeInactiveNotifiedAt(tx *dbs.Tx, nodeId int64) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Result("inactiveNotifiedAt").
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
// FindNodeActive 检查节点活跃状态
|
// FindNodeActive 检查节点活跃状态
|
||||||
func (this *NodeDAO) FindNodeActive(tx *dbs.Tx, nodeId int64) (bool, error) {
|
func (this *NodeDAO) FindNodeActive(tx *dbs.Tx, nodeId int64) (bool, error) {
|
||||||
isActive, err := this.Query(tx).
|
isActive, err := this.Query(tx).
|
||||||
@@ -1827,6 +1848,19 @@ func (this *NodeDAO) FindParentNodeConfigs(tx *dbs.Tx, nodeId int64, groupId int
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
} else if nodeId > 0 {
|
||||||
|
// 当前节点所属分组
|
||||||
|
groupId, err = this.Query(tx).Result("groupId").FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if groupId > 0 {
|
||||||
|
parentNodes, err = this.FindEnabledNodesWithGroupIdAndLevel(tx, groupId, level+1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 当前集群的L2
|
// 当前集群的L2
|
||||||
|
|||||||
@@ -363,9 +363,35 @@ func (this *NodeLogDAO) UpdateAllNodeLogsRead(tx *dbs.Tx) error {
|
|||||||
|
|
||||||
// DeleteNodeLogs 删除某个节点上的日志
|
// DeleteNodeLogs 删除某个节点上的日志
|
||||||
func (this *NodeLogDAO) DeleteNodeLogs(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
|
func (this *NodeLogDAO) DeleteNodeLogs(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
|
||||||
|
if nodeId <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Attr("role", role).
|
Attr("role", role).
|
||||||
Delete()
|
Delete()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteNodeLogsWithCluster 删除某个集群下的所有日志
|
||||||
|
func (this *NodeLogDAO) DeleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.NodeRole, clusterId int64) error {
|
||||||
|
if clusterId <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var query = this.Query(tx).
|
||||||
|
Attr("role", role)
|
||||||
|
|
||||||
|
switch role {
|
||||||
|
case nodeconfigs.NodeRoleNode:
|
||||||
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE clusterId=:clusterId)")
|
||||||
|
query.Param("clusterId", clusterId)
|
||||||
|
case nodeconfigs.NodeRoleDNS:
|
||||||
|
query.Where("nodeId IN (SELECT id FROM " + SharedNSNodeDAO.Table + " WHERE clusterId=:clusterId)")
|
||||||
|
query.Param("clusterId", clusterId)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := query.Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type Node struct {
|
|||||||
CountUp uint32 `field:"countUp"` // 连续在线次数
|
CountUp uint32 `field:"countUp"` // 连续在线次数
|
||||||
CountDown uint32 `field:"countDown"` // 连续下线次数
|
CountDown uint32 `field:"countDown"` // 连续下线次数
|
||||||
IsActive bool `field:"isActive"` // 是否活跃
|
IsActive bool `field:"isActive"` // 是否活跃
|
||||||
|
InactiveNotifiedAt uint64 `field:"inactiveNotifiedAt"` // 离线通知时间
|
||||||
UniqueId string `field:"uniqueId"` // 节点ID
|
UniqueId string `field:"uniqueId"` // 节点ID
|
||||||
Secret string `field:"secret"` // 密钥
|
Secret string `field:"secret"` // 密钥
|
||||||
Name string `field:"name"` // 节点名
|
Name string `field:"name"` // 节点名
|
||||||
@@ -50,6 +51,7 @@ type NodeOperator struct {
|
|||||||
CountUp interface{} // 连续在线次数
|
CountUp interface{} // 连续在线次数
|
||||||
CountDown interface{} // 连续下线次数
|
CountDown interface{} // 连续下线次数
|
||||||
IsActive interface{} // 是否活跃
|
IsActive interface{} // 是否活跃
|
||||||
|
InactiveNotifiedAt interface{} // 离线通知时间
|
||||||
UniqueId interface{} // 节点ID
|
UniqueId interface{} // 节点ID
|
||||||
Secret interface{} // 密钥
|
Secret interface{} // 密钥
|
||||||
Name interface{} // 节点名
|
Name interface{} // 节点名
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NS
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListAccessLogs 读取往前的 单页访问日志
|
// ListAccessLogs 读取往前的 单页访问日志
|
||||||
func (this *NSAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, nodeId int64, domainId int64, recordId int64, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, hasMore bool, err error) {
|
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 {
|
if len(day) != 8 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -121,24 +121,24 @@ func (this *NSAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, siz
|
|||||||
size = 1000
|
size = 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, nodeId, domainId, recordId, keyword, reverse)
|
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, clusterId, nodeId, domainId, recordId, recordType, keyword, reverse)
|
||||||
if err != nil || int64(len(result)) < size {
|
if err != nil || int64(len(result)) < size {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, nodeId, domainId, recordId, keyword, reverse)
|
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, clusterId, nodeId, domainId, recordId, recordType, keyword, reverse)
|
||||||
hasMore = len(moreResult) > 0
|
hasMore = len(moreResult) > 0
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取往前的单页访问日志
|
// 读取往前的单页访问日志
|
||||||
func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, nodeId int64, domainId int64, recordId int64, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, err error) {
|
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 {
|
if size <= 0 {
|
||||||
return nil, lastRequestId, nil
|
return nil, lastRequestId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
accessLogLocker.RLock()
|
accessLogLocker.RLock()
|
||||||
daoList := []*NSAccessLogDAOWrapper{}
|
var daoList = []*NSAccessLogDAOWrapper{}
|
||||||
for _, daoWrapper := range nsAccessLogDAOMapping {
|
for _, daoWrapper := range nsAccessLogDAOMapping {
|
||||||
daoList = append(daoList, daoWrapper)
|
daoList = append(daoList, daoWrapper)
|
||||||
}
|
}
|
||||||
@@ -151,10 +151,23 @@ func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, siz
|
|||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
locker := sync.Mutex{}
|
// 检查是否有集群筛选条件
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
count := len(daoList)
|
var locker = sync.Mutex{}
|
||||||
wg := &sync.WaitGroup{}
|
|
||||||
|
var count = len(daoList)
|
||||||
|
var wg = &sync.WaitGroup{}
|
||||||
wg.Add(count)
|
wg.Add(count)
|
||||||
for _, daoWrapper := range daoList {
|
for _, daoWrapper := range daoList {
|
||||||
go func(daoWrapper *NSAccessLogDAOWrapper) {
|
go func(daoWrapper *NSAccessLogDAOWrapper) {
|
||||||
@@ -172,11 +185,14 @@ func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, siz
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
query := dao.Query(tx)
|
var query = dao.Query(tx)
|
||||||
|
|
||||||
// 条件
|
// 条件
|
||||||
if nodeId > 0 {
|
if nodeId > 0 {
|
||||||
query.Attr("nodeId", nodeId)
|
query.Attr("nodeId", nodeId)
|
||||||
|
} else if clusterId > 0 {
|
||||||
|
query.Attr("nodeId", nodeIds)
|
||||||
|
query.Reuse(false)
|
||||||
}
|
}
|
||||||
if domainId > 0 {
|
if domainId > 0 {
|
||||||
query.Attr("domainId", domainId)
|
query.Attr("domainId", domainId)
|
||||||
@@ -202,6 +218,12 @@ func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, siz
|
|||||||
Param("keyword", dbutils.QuoteLike(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 {
|
if !reverse {
|
||||||
query.Desc("requestId")
|
query.Desc("requestId")
|
||||||
} else {
|
} else {
|
||||||
@@ -244,7 +266,7 @@ func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, siz
|
|||||||
result = result[:size]
|
result = result[:size]
|
||||||
}
|
}
|
||||||
|
|
||||||
requestId := result[len(result)-1].RequestId
|
var requestId = result[len(result)-1].RequestId
|
||||||
if reverse {
|
if reverse {
|
||||||
lists.Reverse(result)
|
lists.Reverse(result)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"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"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -44,12 +48,16 @@ func (this *NSClusterDAO) EnableNSCluster(tx *dbs.Tx, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DisableNSCluster 禁用条目
|
// DisableNSCluster 禁用条目
|
||||||
func (this *NSClusterDAO) DisableNSCluster(tx *dbs.Tx, id int64) error {
|
func (this *NSClusterDAO) DisableNSCluster(tx *dbs.Tx, clusterId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(clusterId).
|
||||||
Set("state", NSClusterStateDisabled).
|
Set("state", NSClusterStateDisabled).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return SharedNodeLogDAO.DeleteNodeLogsWithCluster(tx, nodeconfigs.NodeRoleDNS, clusterId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledNSCluster 查找启用中的条目
|
// FindEnabledNSCluster 查找启用中的条目
|
||||||
@@ -84,6 +92,42 @@ func (this *NSClusterDAO) CreateCluster(tx *dbs.Tx, name string, accessLogRefJSO
|
|||||||
|
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = NSClusterStateEnabled
|
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)
|
return this.SaveInt64(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,6 +237,94 @@ func (this *NSClusterDAO) FindClusterRecursion(tx *dbs.Tx, clusterId int64) ([]b
|
|||||||
return []byte(recursion), nil
|
return []byte(recursion), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindClusterTCP 查找集群的TCP设置
|
||||||
|
func (this *NSClusterDAO) FindClusterTCP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Result("tcp").
|
||||||
|
FindBytesCol()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateClusterTCP 修改集群的TCP设置
|
||||||
|
func (this *NSClusterDAO) UpdateClusterTCP(tx *dbs.Tx, clusterId int64, tcpConfig *serverconfigs.TCPProtocolConfig) error {
|
||||||
|
tcpJSON, err := json.Marshal(tcpConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Set("tcp", tcpJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, clusterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindClusterTLS 查找集群的TLS设置
|
||||||
|
func (this *NSClusterDAO) FindClusterTLS(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Result("tls").
|
||||||
|
FindBytesCol()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateClusterTLS 修改集群的TLS设置
|
||||||
|
func (this *NSClusterDAO) UpdateClusterTLS(tx *dbs.Tx, clusterId int64, tlsConfig *serverconfigs.TLSProtocolConfig) error {
|
||||||
|
tlsJSON, err := json.Marshal(tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Set("tls", tlsJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, clusterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindClusterUDP 查找集群的TCP设置
|
||||||
|
func (this *NSClusterDAO) FindClusterUDP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Result("udp").
|
||||||
|
FindBytesCol()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateClusterUDP 修改集群的UDP设置
|
||||||
|
func (this *NSClusterDAO) UpdateClusterUDP(tx *dbs.Tx, clusterId int64, udpConfig *serverconfigs.UDPProtocolConfig) error {
|
||||||
|
udpJSON, err := json.Marshal(udpConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Set("udp", udpJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, clusterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountAllClustersWithSSLPolicyIds 计算使用SSL策略的所有NS集群数量
|
||||||
|
func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
|
||||||
|
if len(sslPolicyIds) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
policyStringIds := []string{}
|
||||||
|
for _, policyId := range sslPolicyIds {
|
||||||
|
policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10))
|
||||||
|
}
|
||||||
|
return this.Query(tx).
|
||||||
|
State(NSClusterStateEnabled).
|
||||||
|
Where("(FIND_IN_SET(JSON_EXTRACT(tls, '$.sslPolicyRef.sslPolicyId'), :policyIds)) ").
|
||||||
|
Param("policyIds", strings.Join(policyStringIds, ",")).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更改
|
// NotifyUpdate 通知更改
|
||||||
func (this *NSClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
func (this *NSClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, NSNodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, NSNodeTaskTypeConfigChanged)
|
||||||
|
|||||||
@@ -3,4 +3,17 @@ package models
|
|||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func TestNSClusterDAO_DisableNodeCluster(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := SharedNSClusterDAO.DisableNSCluster(nil, 7)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ type NSCluster struct {
|
|||||||
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设置
|
||||||
|
Tls dbs.JSON `field:"tls"` // TLS设置
|
||||||
|
Udp dbs.JSON `field:"udp"` // UDP设置
|
||||||
}
|
}
|
||||||
|
|
||||||
type NSClusterOperator struct {
|
type NSClusterOperator struct {
|
||||||
@@ -23,6 +26,9 @@ type NSClusterOperator struct {
|
|||||||
AccessLog interface{} // 访问日志配置
|
AccessLog interface{} // 访问日志配置
|
||||||
GrantId interface{} // 授权ID
|
GrantId interface{} // 授权ID
|
||||||
Recursion interface{} // 递归DNS设置
|
Recursion interface{} // 递归DNS设置
|
||||||
|
Tcp interface{} // TCP设置
|
||||||
|
Tls interface{} // TLS设置
|
||||||
|
Udp interface{} // UDP设置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNSClusterOperator() *NSClusterOperator {
|
func NewNSClusterOperator() *NSClusterOperator {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
"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/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"
|
||||||
@@ -395,7 +396,7 @@ func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &dnsconfigs.NSNodeConfig{
|
var config = &dnsconfigs.NSNodeConfig{
|
||||||
Id: int64(node.Id),
|
Id: int64(node.Id),
|
||||||
NodeId: node.UniqueId,
|
NodeId: node.UniqueId,
|
||||||
Secret: node.Secret,
|
Secret: node.Secret,
|
||||||
@@ -432,19 +433,57 @@ func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 递归DNS配置
|
// 递归DNS配置
|
||||||
recursionJSON, err := SharedNSClusterDAO.FindClusterRecursion(tx, int64(node.ClusterId))
|
if IsNotNull(cluster.Recursion) {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(recursionJSON) > 0 {
|
|
||||||
var recursionConfig = &dnsconfigs.RecursionConfig{}
|
var recursionConfig = &dnsconfigs.RecursionConfig{}
|
||||||
err = json.Unmarshal(recursionJSON, recursionConfig)
|
err = json.Unmarshal(cluster.Recursion, recursionConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.RecursionConfig = recursionConfig
|
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
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,6 +609,26 @@ func (this *NSNodeDAO) UpdateNodeInstallStatus(tx *dbs.Tx, nodeId int64, status
|
|||||||
return err
|
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 先什么都不做
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -105,7 +108,18 @@ func (this *RegionCityDAO) CreateCity(tx *dbs.Tx, provinceId int64, name string,
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindCityIdWithNameCacheable 根据城市名查找城市ID
|
// FindCityIdWithName 根据城市名查找城市ID
|
||||||
|
func (this *RegionCityDAO) FindCityIdWithName(tx *dbs.Tx, provinceId int64, cityName string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("provinceId", provinceId).
|
||||||
|
Where("(name=:cityName OR customName=:cityName OR JSON_CONTAINS(codes, :cityNameJSON) OR JSON_CONTAINS(customCodes, :cityNameJSON))").
|
||||||
|
Param("cityName", cityName).
|
||||||
|
Param("cityNameJSON", strconv.Quote(cityName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindCityIdWithNameCacheable 根据城市名查找城市ID,并加入缓存
|
||||||
func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId int64, cityName string) (int64, error) {
|
func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId int64, cityName string) (int64, error) {
|
||||||
key := cityName + "@" + numberutils.FormatInt64(provinceId)
|
key := cityName + "@" + numberutils.FormatInt64(provinceId)
|
||||||
|
|
||||||
@@ -119,16 +133,19 @@ func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId in
|
|||||||
|
|
||||||
cityId, err := this.Query(tx).
|
cityId, err := this.Query(tx).
|
||||||
Attr("provinceId", provinceId).
|
Attr("provinceId", provinceId).
|
||||||
Where("JSON_CONTAINS(codes, :cityName)").
|
Where("(name=:cityName OR customName=:cityName OR JSON_CONTAINS(codes, :cityNameJSON) OR JSON_CONTAINS(customCodes, :cityNameJSON))").
|
||||||
Param("cityName", strconv.Quote(cityName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("cityName", cityName).
|
||||||
|
Param("cityNameJSON", strconv.Quote(cityName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
SharedCacheLocker.Lock()
|
if cityId > 0 {
|
||||||
regionCityNameAndIdCacheMap[key] = cityId
|
SharedCacheLocker.Lock()
|
||||||
SharedCacheLocker.Unlock()
|
regionCityNameAndIdCacheMap[key] = cityId
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
return cityId, nil
|
return cityId, nil
|
||||||
}
|
}
|
||||||
@@ -141,3 +158,75 @@ func (this *RegionCityDAO) FindAllEnabledCities(tx *dbs.Tx) (result []*RegionCit
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledCitiesWithProvinceId 获取某个省份下的所有城市
|
||||||
|
func (this *RegionCityDAO) FindAllEnabledCitiesWithProvinceId(tx *dbs.Tx, provinceId int64) (result []*RegionCity, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Attr("provinceId", provinceId).
|
||||||
|
State(RegionCityStateEnabled).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateCityCustom 自定义城市信息
|
||||||
|
func (this *RegionCityDAO) UpdateCityCustom(tx *dbs.Tx, cityId int64, customName string, customCodes []string) error {
|
||||||
|
if customCodes == nil {
|
||||||
|
customCodes = []string{}
|
||||||
|
}
|
||||||
|
customCodesJSON, err := json.Marshal(customCodes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
SharedCacheLocker.Lock()
|
||||||
|
regionCityNameAndIdCacheMap = map[string]int64{}
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(cityId).
|
||||||
|
Set("customName", customName).
|
||||||
|
Set("customCodes", customCodesJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindSimilarCities 查找类似城市名
|
||||||
|
func (this *RegionCityDAO) FindSimilarCities(cities []*RegionCity, cityName string, size int) (result []*RegionCity) {
|
||||||
|
if len(cities) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var similarResult = []maps.Map{}
|
||||||
|
|
||||||
|
for _, city := range cities {
|
||||||
|
var similarityList = []float32{}
|
||||||
|
for _, code := range city.AllCodes() {
|
||||||
|
var similarity = utils.Similar(cityName, code)
|
||||||
|
if similarity > 0 {
|
||||||
|
similarityList = append(similarityList, similarity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(similarityList) > 0 {
|
||||||
|
similarResult = append(similarResult, maps.Map{
|
||||||
|
"similarity": numberutils.Max(similarityList...),
|
||||||
|
"city": city,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(similarResult, func(i, j int) bool {
|
||||||
|
return similarResult[i].GetFloat32("similarity") > similarResult[j].GetFloat32("similarity")
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(similarResult) > size {
|
||||||
|
similarResult = similarResult[:size]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range similarResult {
|
||||||
|
result = append(result, r.Get("city").(*RegionCity))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,23 +2,27 @@ package regions
|
|||||||
|
|
||||||
import "github.com/iwind/TeaGo/dbs"
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
// RegionCity 区域城市
|
// RegionCity 区域-城市
|
||||||
type RegionCity struct {
|
type RegionCity struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
ProvinceId uint32 `field:"provinceId"` // 省份ID
|
ProvinceId uint32 `field:"provinceId"` // 省份ID
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Codes dbs.JSON `field:"codes"` // 代号
|
Codes dbs.JSON `field:"codes"` // 代号
|
||||||
State uint8 `field:"state"` // 状态
|
CustomName string `field:"customName"` // 自定义名称
|
||||||
DataId string `field:"dataId"` // 原始数据ID
|
CustomCodes dbs.JSON `field:"customCodes"` // 自定义代号
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
DataId string `field:"dataId"` // 原始数据ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegionCityOperator struct {
|
type RegionCityOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
ProvinceId interface{} // 省份ID
|
ProvinceId interface{} // 省份ID
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Codes interface{} // 代号
|
Codes interface{} // 代号
|
||||||
State interface{} // 状态
|
CustomName interface{} // 自定义名称
|
||||||
DataId interface{} // 原始数据ID
|
CustomCodes interface{} // 自定义代号
|
||||||
|
State interface{} // 状态
|
||||||
|
DataId interface{} // 原始数据ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRegionCityOperator() *RegionCityOperator {
|
func NewRegionCityOperator() *RegionCityOperator {
|
||||||
|
|||||||
@@ -2,17 +2,56 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *RegionCity) DecodeCodes() []string {
|
func (this *RegionCity) DecodeCodes() []string {
|
||||||
if len(this.Codes) == 0 {
|
if len(this.Codes) == 0 {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
result := []string{}
|
var result = []string{}
|
||||||
err := json.Unmarshal(this.Codes, &result)
|
err := json.Unmarshal(this.Codes, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
remotelogs.Error("RegionCity.DecodeCodes", err.Error())
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *RegionCity) DecodeCustomCodes() []string {
|
||||||
|
if len(this.CustomCodes) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.CustomCodes, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("RegionCity.DecodeCustomCodes", err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionCity) DisplayName() string {
|
||||||
|
if len(this.CustomName) > 0 {
|
||||||
|
return this.CustomName
|
||||||
|
}
|
||||||
|
return this.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionCity) AllCodes() []string {
|
||||||
|
var codes = this.DecodeCodes()
|
||||||
|
|
||||||
|
if len(this.Name) > 0 && !lists.ContainsString(codes, this.Name) {
|
||||||
|
codes = append(codes, this.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.CustomName) > 0 && !lists.ContainsString(codes, this.CustomName) {
|
||||||
|
codes = append(codes, this.CustomName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, code := range this.DecodeCustomCodes() {
|
||||||
|
if !lists.ContainsString(codes, code) {
|
||||||
|
codes = append(codes, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return codes
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,11 +2,15 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"github.com/mozillazg/go-pinyin"
|
"github.com/mozillazg/go-pinyin"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -16,6 +20,10 @@ const (
|
|||||||
RegionCountryStateDisabled = 0 // 已禁用
|
RegionCountryStateDisabled = 0 // 已禁用
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CountryChinaId = 1
|
||||||
|
)
|
||||||
|
|
||||||
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => id
|
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => id
|
||||||
var regionCountryIdAndNameCacheMap = map[int64]string{} // country id => name
|
var regionCountryIdAndNameCacheMap = map[int64]string{} // country id => name
|
||||||
|
|
||||||
@@ -88,7 +96,9 @@ func (this *RegionCountryDAO) FindRegionCountryName(tx *dbs.Tx, id int64) (strin
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
regionCountryIdAndNameCacheMap[id] = name
|
if len(name) > 0 {
|
||||||
|
regionCountryIdAndNameCacheMap[id] = name
|
||||||
|
}
|
||||||
return name, nil
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,8 +113,9 @@ func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string)
|
|||||||
// FindCountryIdWithName 根据国家名查找国家ID
|
// FindCountryIdWithName 根据国家名查找国家ID
|
||||||
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Where("JSON_CONTAINS(codes, :countryName)").
|
Where("(name=:countryName OR JSON_CONTAINS(codes, :countryNameJSON) OR customName=:countryName OR JSON_CONTAINS(customCodes, :countryNameJSON))").
|
||||||
Param("countryName", strconv.Quote(countryName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("countryName", countryName).
|
||||||
|
Param("countryNameJSON", strconv.Quote(countryName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
@@ -124,9 +135,11 @@ func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, country
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedCacheLocker.Lock()
|
if countryId > 0 {
|
||||||
regionCountryNameAndIdCacheMap[countryName] = countryId
|
SharedCacheLocker.Lock()
|
||||||
SharedCacheLocker.Unlock()
|
regionCountryNameAndIdCacheMap[countryName] = countryId
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
return countryId, nil
|
return countryId, nil
|
||||||
}
|
}
|
||||||
@@ -160,7 +173,7 @@ func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId stri
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAllEnabledCountriesOrderByPinyin 查找所有可用的国家
|
// FindAllEnabledCountriesOrderByPinyin 查找所有可用的国家并按拼音排序
|
||||||
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(RegionCountryStateEnabled).
|
State(RegionCountryStateEnabled).
|
||||||
@@ -169,3 +182,76 @@ func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin(tx *dbs.Tx) (
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllCountries 查找所有可用的国家
|
||||||
|
func (this *RegionCountryDAO) FindAllCountries(tx *dbs.Tx) (result []*RegionCountry, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(RegionCountryStateEnabled).
|
||||||
|
Slice(&result).
|
||||||
|
AscPk().
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateCountryCustom 修改国家/地区自定义
|
||||||
|
func (this *RegionCountryDAO) UpdateCountryCustom(tx *dbs.Tx, countryId int64, customName string, customCodes []string) error {
|
||||||
|
if customCodes == nil {
|
||||||
|
customCodes = []string{}
|
||||||
|
}
|
||||||
|
customCodesJSON, err := json.Marshal(customCodes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
SharedCacheLocker.Lock()
|
||||||
|
regionCountryNameAndIdCacheMap = map[string]int64{}
|
||||||
|
regionCountryIdAndNameCacheMap = map[int64]string{}
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(countryId).
|
||||||
|
Set("customName", customName).
|
||||||
|
Set("customCodes", customCodesJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindSimilarCountries 查找类似国家/地区名
|
||||||
|
func (this *RegionCountryDAO) FindSimilarCountries(countries []*RegionCountry, countryName string, size int) (result []*RegionCountry) {
|
||||||
|
if len(countries) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var similarResult = []maps.Map{}
|
||||||
|
|
||||||
|
for _, country := range countries {
|
||||||
|
var similarityList = []float32{}
|
||||||
|
for _, code := range country.AllCodes() {
|
||||||
|
var similarity = utils.Similar(countryName, code)
|
||||||
|
if similarity > 0 {
|
||||||
|
similarityList = append(similarityList, similarity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(similarityList) > 0 {
|
||||||
|
similarResult = append(similarResult, maps.Map{
|
||||||
|
"similarity": numberutils.Max(similarityList...),
|
||||||
|
"country": country,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(similarResult, func(i, j int) bool {
|
||||||
|
return similarResult[i].GetFloat32("similarity") > similarResult[j].GetFloat32("similarity")
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(similarResult) > size {
|
||||||
|
similarResult = similarResult[:size]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range similarResult {
|
||||||
|
result = append(result, r.Get("country").(*RegionCountry))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,11 +8,29 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestRegionCountryDAO_FindCountryIdWithName(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
for _, name := range []string{
|
||||||
|
"中国",
|
||||||
|
"中华人民共和国",
|
||||||
|
"美国",
|
||||||
|
"美利坚合众国",
|
||||||
|
"美利坚",
|
||||||
|
} {
|
||||||
|
countryId, err := SharedRegionCountryDAO.FindCountryIdWithName(nil, name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(name, ":", countryId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRegionCountryDAO_FindCountryIdWithCountryNameCacheable(t *testing.T) {
|
func TestRegionCountryDAO_FindCountryIdWithCountryNameCacheable(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
now := time.Now()
|
var now = time.Now()
|
||||||
countryId, err := SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, "中国")
|
countryId, err := SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, "中国")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -20,3 +38,24 @@ func TestRegionCountryDAO_FindCountryIdWithCountryNameCacheable(t *testing.T) {
|
|||||||
t.Log("countryId", countryId, time.Since(now).Seconds()*1000, "ms")
|
t.Log("countryId", countryId, time.Since(now).Seconds()*1000, "ms")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRegionCountryDAO_FindSimilarCountries(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
countries, err := SharedRegionCountryDAO.FindAllCountries(tx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, countryName := range []string{"中国", "布基纳法索", "哥伦比亚", "德意志共和国", "美利坚", "刚果金"} {
|
||||||
|
t.Log("====" + countryName + "====")
|
||||||
|
var countries = SharedRegionCountryDAO.FindSimilarCountries(countries, countryName, 5)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, country := range countries {
|
||||||
|
t.Log(country.Name, country.AllCodes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,23 +2,27 @@ package regions
|
|||||||
|
|
||||||
import "github.com/iwind/TeaGo/dbs"
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
// RegionCountry 区域国家|地区
|
// RegionCountry 区域-国家/地区
|
||||||
type RegionCountry struct {
|
type RegionCountry struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Codes dbs.JSON `field:"codes"` // 代号
|
Codes dbs.JSON `field:"codes"` // 代号
|
||||||
State uint8 `field:"state"` // 状态
|
CustomName string `field:"customName"` // 自定义名称
|
||||||
DataId string `field:"dataId"` // 原始数据ID
|
CustomCodes dbs.JSON `field:"customCodes"` // 自定义代号
|
||||||
Pinyin dbs.JSON `field:"pinyin"` // 拼音
|
State uint8 `field:"state"` // 状态
|
||||||
|
DataId string `field:"dataId"` // 原始数据ID
|
||||||
|
Pinyin dbs.JSON `field:"pinyin"` // 拼音
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegionCountryOperator struct {
|
type RegionCountryOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Codes interface{} // 代号
|
Codes interface{} // 代号
|
||||||
State interface{} // 状态
|
CustomName interface{} // 自定义名称
|
||||||
DataId interface{} // 原始数据ID
|
CustomCodes interface{} // 自定义代号
|
||||||
Pinyin interface{} // 拼音
|
State interface{} // 状态
|
||||||
|
DataId interface{} // 原始数据ID
|
||||||
|
Pinyin interface{} // 拼音
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRegionCountryOperator() *RegionCountryOperator {
|
func NewRegionCountryOperator() *RegionCountryOperator {
|
||||||
|
|||||||
@@ -2,17 +2,56 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *RegionCountry) DecodeCodes() []string {
|
func (this *RegionCountry) DecodeCodes() []string {
|
||||||
if len(this.Codes) == 0 {
|
if len(this.Codes) == 0 {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
result := []string{}
|
var result = []string{}
|
||||||
err := json.Unmarshal(this.Codes, &result)
|
err := json.Unmarshal(this.Codes, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
remotelogs.Error("RegionCountry.DecodeCodes", err.Error())
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *RegionCountry) DecodeCustomCodes() []string {
|
||||||
|
if len(this.CustomCodes) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.CustomCodes, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("RegionCountry.DecodeCustomCodes", err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionCountry) DisplayName() string {
|
||||||
|
if len(this.CustomName) > 0 {
|
||||||
|
return this.CustomName
|
||||||
|
}
|
||||||
|
return this.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionCountry) AllCodes() []string {
|
||||||
|
var codes = this.DecodeCodes()
|
||||||
|
|
||||||
|
if len(this.Name) > 0 && !lists.ContainsString(codes, this.Name) {
|
||||||
|
codes = append(codes, this.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.CustomName) > 0 && !lists.ContainsString(codes, this.CustomName) {
|
||||||
|
codes = append(codes, this.CustomName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, code := range this.DecodeCustomCodes() {
|
||||||
|
if !lists.ContainsString(codes, code) {
|
||||||
|
codes = append(codes, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return codes
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,13 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -74,7 +78,17 @@ func (this *RegionProviderDAO) FindRegionProviderName(tx *dbs.Tx, id uint32) (st
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindProviderIdWithNameCacheable 根据服务商名称查找服务商ID
|
// FindProviderIdWithName 根据服务商名称查找服务商ID
|
||||||
|
func (this *RegionProviderDAO) FindProviderIdWithName(tx *dbs.Tx, providerName string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Where("(name=:providerName OR customName=:providerName OR JSON_CONTAINS(codes, :providerNameJSON) OR JSON_CONTAINS(customCodes, :providerNameJSON))").
|
||||||
|
Param("providerName", providerName).
|
||||||
|
Param("providerNameJSON", strconv.Quote(providerName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindProviderIdWithNameCacheable 根据服务商名称查找服务商ID,并保存进缓存
|
||||||
func (this *RegionProviderDAO) FindProviderIdWithNameCacheable(tx *dbs.Tx, providerName string) (int64, error) {
|
func (this *RegionProviderDAO) FindProviderIdWithNameCacheable(tx *dbs.Tx, providerName string) (int64, error) {
|
||||||
SharedCacheLocker.RLock()
|
SharedCacheLocker.RLock()
|
||||||
providerId, ok := regionProviderNameAndIdCacheMap[providerName]
|
providerId, ok := regionProviderNameAndIdCacheMap[providerName]
|
||||||
@@ -85,17 +99,20 @@ func (this *RegionProviderDAO) FindProviderIdWithNameCacheable(tx *dbs.Tx, provi
|
|||||||
SharedCacheLocker.RUnlock()
|
SharedCacheLocker.RUnlock()
|
||||||
|
|
||||||
providerId, err := this.Query(tx).
|
providerId, err := this.Query(tx).
|
||||||
Where("JSON_CONTAINS(codes, :providerName)").
|
Where("(name=:providerName OR customName=:providerName OR JSON_CONTAINS(codes, :providerNameJSON) OR JSON_CONTAINS(customCodes, :providerNameJSON))").
|
||||||
Param("providerName", strconv.Quote(providerName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("providerName", providerName).
|
||||||
|
Param("providerNameJSON", strconv.Quote(providerName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedCacheLocker.Lock()
|
if providerId > 0 {
|
||||||
regionProviderNameAndIdCacheMap[providerName] = providerId
|
SharedCacheLocker.Lock()
|
||||||
SharedCacheLocker.Unlock()
|
regionProviderNameAndIdCacheMap[providerName] = providerId
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
return providerId, nil
|
return providerId, nil
|
||||||
}
|
}
|
||||||
@@ -121,3 +138,65 @@ func (this *RegionProviderDAO) FindAllEnabledProviders(tx *dbs.Tx) (result []*Re
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateProviderCustom 修改ISP自定义信息
|
||||||
|
func (this *RegionProviderDAO) UpdateProviderCustom(tx *dbs.Tx, providerId int64, customName string, customCodes []string) error {
|
||||||
|
if customCodes == nil {
|
||||||
|
customCodes = []string{}
|
||||||
|
}
|
||||||
|
customCodesJSON, err := json.Marshal(customCodes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
SharedCacheLocker.Lock()
|
||||||
|
regionProviderNameAndIdCacheMap = map[string]int64{}
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(providerId).
|
||||||
|
Set("customName", customName).
|
||||||
|
Set("customCodes", customCodesJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindSimilarProviders 查找类似ISP运营商
|
||||||
|
func (this *RegionProviderDAO) FindSimilarProviders(providers []*RegionProvider, providerName string, size int) (result []*RegionProvider) {
|
||||||
|
if len(providers) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var similarResult = []maps.Map{}
|
||||||
|
|
||||||
|
for _, provider := range providers {
|
||||||
|
var similarityList = []float32{}
|
||||||
|
for _, code := range provider.AllCodes() {
|
||||||
|
var similarity = utils.Similar(providerName, code)
|
||||||
|
if similarity > 0 {
|
||||||
|
similarityList = append(similarityList, similarity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(similarityList) > 0 {
|
||||||
|
similarResult = append(similarResult, maps.Map{
|
||||||
|
"similarity": numberutils.Max(similarityList...),
|
||||||
|
"provider": provider,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(similarResult, func(i, j int) bool {
|
||||||
|
return similarResult[i].GetFloat32("similarity") > similarResult[j].GetFloat32("similarity")
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(similarResult) > size {
|
||||||
|
similarResult = similarResult[:size]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range similarResult {
|
||||||
|
result = append(result, r.Get("provider").(*RegionProvider))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,19 +2,23 @@ package regions
|
|||||||
|
|
||||||
import "github.com/iwind/TeaGo/dbs"
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
// RegionProvider 区域ISP
|
// RegionProvider 区域-运营商
|
||||||
type RegionProvider struct {
|
type RegionProvider struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Codes dbs.JSON `field:"codes"` // 代号
|
Codes dbs.JSON `field:"codes"` // 代号
|
||||||
State uint8 `field:"state"` // 状态
|
CustomName string `field:"customName"` // 自定义名称
|
||||||
|
CustomCodes dbs.JSON `field:"customCodes"` // 自定义代号
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegionProviderOperator struct {
|
type RegionProviderOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Codes interface{} // 代号
|
Codes interface{} // 代号
|
||||||
State interface{} // 状态
|
CustomName interface{} // 自定义名称
|
||||||
|
CustomCodes interface{} // 自定义代号
|
||||||
|
State interface{} // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRegionProviderOperator() *RegionProviderOperator {
|
func NewRegionProviderOperator() *RegionProviderOperator {
|
||||||
|
|||||||
@@ -2,17 +2,56 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *RegionProvider) DecodeCodes() []string {
|
func (this *RegionProvider) DecodeCodes() []string {
|
||||||
if len(this.Codes) == 0 {
|
if len(this.Codes) == 0 {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
result := []string{}
|
var result = []string{}
|
||||||
err := json.Unmarshal(this.Codes, &result)
|
err := json.Unmarshal(this.Codes, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
remotelogs.Error("RegionProvider.DecodeCodes", err.Error())
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *RegionProvider) DecodeCustomCodes() []string {
|
||||||
|
if len(this.CustomCodes) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.CustomCodes, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("RegionProvider.DecodeCustomCodes", err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionProvider) DisplayName() string {
|
||||||
|
if len(this.CustomName) > 0 {
|
||||||
|
return this.CustomName
|
||||||
|
}
|
||||||
|
return this.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionProvider) AllCodes() []string {
|
||||||
|
var codes = this.DecodeCodes()
|
||||||
|
|
||||||
|
if len(this.Name) > 0 && !lists.ContainsString(codes, this.Name) {
|
||||||
|
codes = append(codes, this.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.CustomName) > 0 && !lists.ContainsString(codes, this.CustomName) {
|
||||||
|
codes = append(codes, this.CustomName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, code := range this.DecodeCustomCodes() {
|
||||||
|
if !lists.ContainsString(codes, code) {
|
||||||
|
codes = append(codes, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return codes
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,7 +41,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableRegionProvince 启用条目
|
||||||
func (this *RegionProvinceDAO) EnableRegionProvince(tx *dbs.Tx, id int64) error {
|
func (this *RegionProvinceDAO) EnableRegionProvince(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -47,7 +50,7 @@ func (this *RegionProvinceDAO) EnableRegionProvince(tx *dbs.Tx, id int64) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableRegionProvince 禁用条目
|
||||||
func (this *RegionProvinceDAO) DisableRegionProvince(tx *dbs.Tx, id int64) error {
|
func (this *RegionProvinceDAO) DisableRegionProvince(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -56,7 +59,7 @@ func (this *RegionProvinceDAO) DisableRegionProvince(tx *dbs.Tx, id int64) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledRegionProvince 查找启用中的条目
|
||||||
func (this *RegionProvinceDAO) FindEnabledRegionProvince(tx *dbs.Tx, id int64) (*RegionProvince, error) {
|
func (this *RegionProvinceDAO) FindEnabledRegionProvince(tx *dbs.Tx, id int64) (*RegionProvince, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -68,7 +71,7 @@ func (this *RegionProvinceDAO) FindEnabledRegionProvince(tx *dbs.Tx, id int64) (
|
|||||||
return result.(*RegionProvince), err
|
return result.(*RegionProvince), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindRegionProvinceName 根据主键查找名称
|
||||||
func (this *RegionProvinceDAO) FindRegionProvinceName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *RegionProvinceDAO) FindRegionProvinceName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -76,7 +79,7 @@ func (this *RegionProvinceDAO) FindRegionProvinceName(tx *dbs.Tx, id int64) (str
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据数据ID查找省份
|
// FindProvinceIdWithDataId 根据数据ID查找省份
|
||||||
func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Attr("dataId", dataId).
|
Attr("dataId", dataId).
|
||||||
@@ -84,17 +87,18 @@ func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId strin
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据省份名查找省份ID
|
// FindProvinceIdWithName 根据省份名查找省份ID
|
||||||
func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
|
func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Attr("countryId", countryId).
|
Attr("countryId", countryId).
|
||||||
Where("JSON_CONTAINS(codes, :provinceName)").
|
Where("(name=:provinceName OR customName=:provinceName OR JSON_CONTAINS(codes, :provinceNameJSON) OR JSON_CONTAINS(customCodes, :provinceNameJSON))").
|
||||||
Param("provinceName", strconv.Quote(provinceName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
Param("provinceName", provinceName).
|
||||||
|
Param("provinceNameJSON", strconv.Quote(provinceName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据省份名查找省份ID,并可使用缓存
|
// FindProvinceIdWithNameCacheable 根据省份名查找省份ID,并可使用缓存
|
||||||
func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
|
func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
|
||||||
var key = provinceName + "@" + numberutils.FormatInt64(countryId)
|
var key = provinceName + "@" + numberutils.FormatInt64(countryId)
|
||||||
|
|
||||||
@@ -110,14 +114,16 @@ func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, count
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
SharedCacheLocker.Lock()
|
if provinceId > 0 {
|
||||||
regionProvinceNameAndIdCacheMap[key] = provinceId
|
SharedCacheLocker.Lock()
|
||||||
SharedCacheLocker.Unlock()
|
regionProvinceNameAndIdCacheMap[key] = provinceId
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
return provinceId, nil
|
return provinceId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建省份
|
// CreateProvince 创建省份
|
||||||
func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name string, dataId string) (int64, error) {
|
func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name string, dataId string) (int64, error) {
|
||||||
var op = NewRegionProvinceOperator()
|
var op = NewRegionProvinceOperator()
|
||||||
op.CountryId = countryId
|
op.CountryId = countryId
|
||||||
@@ -138,7 +144,7 @@ func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找所有省份
|
// FindAllEnabledProvincesWithCountryId 查找某个国家/地区的所有省份
|
||||||
func (this *RegionProvinceDAO) FindAllEnabledProvincesWithCountryId(tx *dbs.Tx, countryId int64) (result []*RegionProvince, err error) {
|
func (this *RegionProvinceDAO) FindAllEnabledProvincesWithCountryId(tx *dbs.Tx, countryId int64) (result []*RegionProvince, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(RegionProvinceStateEnabled).
|
State(RegionProvinceStateEnabled).
|
||||||
@@ -148,3 +154,76 @@ func (this *RegionProvinceDAO) FindAllEnabledProvincesWithCountryId(tx *dbs.Tx,
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledProvinces 查找所有省份
|
||||||
|
func (this *RegionProvinceDAO) FindAllEnabledProvinces(tx *dbs.Tx) (result []*RegionProvince, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(RegionProvinceStateEnabled).
|
||||||
|
Asc().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateProvinceCustom 修改自定义省份信息
|
||||||
|
func (this *RegionProvinceDAO) UpdateProvinceCustom(tx *dbs.Tx, provinceId int64, customName string, customCodes []string) error {
|
||||||
|
if customCodes == nil {
|
||||||
|
customCodes = []string{}
|
||||||
|
}
|
||||||
|
customCodesJSON, err := json.Marshal(customCodes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清空缓存
|
||||||
|
defer func() {
|
||||||
|
SharedCacheLocker.Lock()
|
||||||
|
regionProvinceNameAndIdCacheMap = map[string]int64{}
|
||||||
|
SharedCacheLocker.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(provinceId).
|
||||||
|
Set("customName", customName).
|
||||||
|
Set("customCodes", customCodesJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindSimilarProvinces 查找类似省份名
|
||||||
|
func (this *RegionProvinceDAO) FindSimilarProvinces(provinces []*RegionProvince, provinceName string, size int) (result []*RegionProvince) {
|
||||||
|
if len(provinces) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var similarResult = []maps.Map{}
|
||||||
|
|
||||||
|
for _, province := range provinces {
|
||||||
|
var similarityList = []float32{}
|
||||||
|
for _, code := range province.AllCodes() {
|
||||||
|
var similarity = utils.Similar(provinceName, code)
|
||||||
|
if similarity > 0 {
|
||||||
|
similarityList = append(similarityList, similarity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(similarityList) > 0 {
|
||||||
|
similarResult = append(similarResult, maps.Map{
|
||||||
|
"similarity": numberutils.Max(similarityList...),
|
||||||
|
"province": province,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(similarResult, func(i, j int) bool {
|
||||||
|
return similarResult[i].GetFloat32("similarity") > similarResult[j].GetFloat32("similarity")
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(similarResult) > size {
|
||||||
|
similarResult = similarResult[:size]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range similarResult {
|
||||||
|
result = append(result, r.Get("province").(*RegionProvince))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRegionProvinceDAO_FindProvinceIdWithProvinceName(t *testing.T) {
|
func TestRegionProvinceDAO_FindProvinceIdWithNameCacheable(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
@@ -29,3 +29,51 @@ func TestRegionProvinceDAO_FindProvinceIdWithProvinceName(t *testing.T) {
|
|||||||
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
|
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRegionProvinceDAO_FindProvinceIdWithName(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
for _, name := range []string{
|
||||||
|
"安徽",
|
||||||
|
"安徽省",
|
||||||
|
"广西",
|
||||||
|
"广西省",
|
||||||
|
"广西壮族自治区",
|
||||||
|
"皖",
|
||||||
|
} {
|
||||||
|
provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithName(tx, 1, name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(name, "=>", provinceId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegionProvinceDAO_FindSimilarProvinces(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
var countryId int64 = 1
|
||||||
|
provinces, err := SharedRegionProvinceDAO.FindAllEnabledProvincesWithCountryId(tx, countryId)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, provinceName := range []string{
|
||||||
|
"北京",
|
||||||
|
"北京市",
|
||||||
|
"安徽",
|
||||||
|
"安徽省",
|
||||||
|
"大北京",
|
||||||
|
} {
|
||||||
|
t.Log("====" + provinceName + "====")
|
||||||
|
var provinces = SharedRegionProvinceDAO.FindSimilarProvinces(provinces, provinceName, 5)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, province := range provinces {
|
||||||
|
t.Log(province.Name, province.AllCodes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,23 +2,27 @@ package regions
|
|||||||
|
|
||||||
import "github.com/iwind/TeaGo/dbs"
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
// RegionProvince 区域省份
|
// RegionProvince 区域-省份
|
||||||
type RegionProvince struct {
|
type RegionProvince struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
CountryId uint32 `field:"countryId"` // 国家ID
|
CountryId uint32 `field:"countryId"` // 国家ID
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Codes dbs.JSON `field:"codes"` // 代号
|
Codes dbs.JSON `field:"codes"` // 代号
|
||||||
State uint8 `field:"state"` // 状态
|
CustomName string `field:"customName"` // 自定义名称
|
||||||
DataId string `field:"dataId"` // 原始数据ID
|
CustomCodes dbs.JSON `field:"customCodes"` // 自定义代号
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
DataId string `field:"dataId"` // 原始数据ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegionProvinceOperator struct {
|
type RegionProvinceOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
CountryId interface{} // 国家ID
|
CountryId interface{} // 国家ID
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Codes interface{} // 代号
|
Codes interface{} // 代号
|
||||||
State interface{} // 状态
|
CustomName interface{} // 自定义名称
|
||||||
DataId interface{} // 原始数据ID
|
CustomCodes interface{} // 自定义代号
|
||||||
|
State interface{} // 状态
|
||||||
|
DataId interface{} // 原始数据ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRegionProvinceOperator() *RegionProvinceOperator {
|
func NewRegionProvinceOperator() *RegionProvinceOperator {
|
||||||
|
|||||||
@@ -2,17 +2,56 @@ package regions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *RegionProvince) DecodeCodes() []string {
|
func (this *RegionProvince) DecodeCodes() []string {
|
||||||
if len(this.Codes) == 0 {
|
if len(this.Codes) == 0 {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
result := []string{}
|
var result = []string{}
|
||||||
err := json.Unmarshal(this.Codes, &result)
|
err := json.Unmarshal(this.Codes, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
remotelogs.Error("RegionProvince.DecodeCodes", err.Error())
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *RegionProvince) DecodeCustomCodes() []string {
|
||||||
|
if len(this.CustomCodes) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.CustomCodes, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("RegionProvince.DecodeCustomCodes", err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionProvince) DisplayName() string {
|
||||||
|
if len(this.CustomName) > 0 {
|
||||||
|
return this.CustomName
|
||||||
|
}
|
||||||
|
return this.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionProvince) AllCodes() []string {
|
||||||
|
var codes = this.DecodeCodes()
|
||||||
|
|
||||||
|
if len(this.Name) > 0 && !lists.ContainsString(codes, this.Name) {
|
||||||
|
codes = append(codes, this.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.CustomName) > 0 && !lists.ContainsString(codes, this.CustomName) {
|
||||||
|
codes = append(codes, this.CustomName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, code := range this.DecodeCustomCodes() {
|
||||||
|
if !lists.ContainsString(codes, code) {
|
||||||
|
codes = append(codes, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return codes
|
||||||
|
}
|
||||||
|
|||||||
164
internal/db/models/regions/region_town_dao.go
Normal file
164
internal/db/models/regions/region_town_dao.go
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
package regions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
RegionTownStateEnabled = 1 // 已启用
|
||||||
|
RegionTownStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type RegionTownDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewRegionTownDAO() *RegionTownDAO {
|
||||||
|
return dbs.NewDAO(&RegionTownDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeRegionTowns",
|
||||||
|
Model: new(RegionTown),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*RegionTownDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedRegionTownDAO *RegionTownDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedRegionTownDAO = NewRegionTownDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableRegionTown 启用条目
|
||||||
|
func (this *RegionTownDAO) EnableRegionTown(tx *dbs.Tx, id uint32) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", RegionTownStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableRegionTown 禁用条目
|
||||||
|
func (this *RegionTownDAO) DisableRegionTown(tx *dbs.Tx, id uint32) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", RegionTownStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledRegionTown 查找启用中的区县
|
||||||
|
func (this *RegionTownDAO) FindEnabledRegionTown(tx *dbs.Tx, id int64) (*RegionTown, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", RegionTownStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*RegionTown), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllRegionTowns 获取所有的区县
|
||||||
|
func (this *RegionTownDAO) FindAllRegionTowns(tx *dbs.Tx) (result []*RegionTown, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(RegionTownStateEnabled).
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllRegionTownsWithCityId 根据城市查找区县
|
||||||
|
func (this *RegionTownDAO) FindAllRegionTownsWithCityId(tx *dbs.Tx, cityId int64) (result []*RegionTown, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(RegionTownStateEnabled).
|
||||||
|
Attr("cityId", cityId).
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindTownIdWithName 根据区县名查找区县ID
|
||||||
|
func (this *RegionTownDAO) FindTownIdWithName(tx *dbs.Tx, cityId int64, townName string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("cityId", cityId).
|
||||||
|
Where("(name=:townName OR customName=:townName OR JSON_CONTAINS(codes, :townNameJSON) OR JSON_CONTAINS(customCodes, :townNameJSON))").
|
||||||
|
Param("townName", townName).
|
||||||
|
Param("townNameJSON", strconv.Quote(townName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindRegionTownName 根据主键查找名称
|
||||||
|
func (this *RegionTownDAO) FindRegionTownName(tx *dbs.Tx, id uint32) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateTownCustom 修改自定义县级信息
|
||||||
|
func (this *RegionTownDAO) UpdateTownCustom(tx *dbs.Tx, townId int64, customName string, customCodes []string) error {
|
||||||
|
if customCodes == nil {
|
||||||
|
customCodes = []string{}
|
||||||
|
}
|
||||||
|
customCodesJSON, err := json.Marshal(customCodes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(townId).
|
||||||
|
Set("customName", customName).
|
||||||
|
Set("customCodes", customCodesJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindSimilarTowns 查找类似区县
|
||||||
|
func (this *RegionTownDAO) FindSimilarTowns(towns []*RegionTown, townName string, size int) (result []*RegionTown) {
|
||||||
|
if len(towns) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var similarResult = []maps.Map{}
|
||||||
|
|
||||||
|
for _, town := range towns {
|
||||||
|
var similarityList = []float32{}
|
||||||
|
for _, code := range town.AllCodes() {
|
||||||
|
var similarity = utils.Similar(townName, code)
|
||||||
|
if similarity > 0 {
|
||||||
|
similarityList = append(similarityList, similarity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(similarityList) > 0 {
|
||||||
|
similarResult = append(similarResult, maps.Map{
|
||||||
|
"similarity": numberutils.Max(similarityList...),
|
||||||
|
"town": town,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(similarResult, func(i, j int) bool {
|
||||||
|
return similarResult[i].GetFloat32("similarity") > similarResult[j].GetFloat32("similarity")
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(similarResult) > size {
|
||||||
|
similarResult = similarResult[:size]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range similarResult {
|
||||||
|
result = append(result, r.Get("town").(*RegionTown))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package nameservers
|
package regions_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
30
internal/db/models/regions/region_town_model.go
Normal file
30
internal/db/models/regions/region_town_model.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package regions
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// RegionTown 区域-省份
|
||||||
|
type RegionTown struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
CityId uint32 `field:"cityId"` // 城市ID
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
Codes dbs.JSON `field:"codes"` // 代号
|
||||||
|
CustomName string `field:"customName"` // 自定义名称
|
||||||
|
CustomCodes dbs.JSON `field:"customCodes"` // 自定义代号
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
DataId string `field:"dataId"` // 原始数据ID
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegionTownOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
CityId interface{} // 城市ID
|
||||||
|
Name interface{} // 名称
|
||||||
|
Codes interface{} // 代号
|
||||||
|
CustomName interface{} // 自定义名称
|
||||||
|
CustomCodes interface{} // 自定义代号
|
||||||
|
State interface{} // 状态
|
||||||
|
DataId interface{} // 原始数据ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRegionTownOperator() *RegionTownOperator {
|
||||||
|
return &RegionTownOperator{}
|
||||||
|
}
|
||||||
57
internal/db/models/regions/region_town_model_ext.go
Normal file
57
internal/db/models/regions/region_town_model_ext.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package regions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (this *RegionTown) DecodeCodes() []string {
|
||||||
|
if len(this.Codes) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.Codes, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("RegionTown.DecodeCodes", err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionTown) DecodeCustomCodes() []string {
|
||||||
|
if len(this.CustomCodes) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal(this.CustomCodes, &result)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("RegionTown.DecodeCustomCodes", err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionTown) DisplayName() string {
|
||||||
|
if len(this.CustomName) > 0 {
|
||||||
|
return this.CustomName
|
||||||
|
}
|
||||||
|
return this.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RegionTown) AllCodes() []string {
|
||||||
|
var codes = this.DecodeCodes()
|
||||||
|
|
||||||
|
if len(this.Name) > 0 && !lists.ContainsString(codes, this.Name) {
|
||||||
|
codes = append(codes, this.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.CustomName) > 0 && !lists.ContainsString(codes, this.CustomName) {
|
||||||
|
codes = append(codes, this.CustomName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, code := range this.DecodeCustomCodes() {
|
||||||
|
if !lists.ContainsString(codes, code) {
|
||||||
|
codes = append(codes, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return codes
|
||||||
|
}
|
||||||
@@ -269,7 +269,7 @@ func (this *ReportNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNodeStatus 更改节点状态
|
// UpdateNodeStatus 更改节点状态
|
||||||
func (this ReportNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *reporterconfigs.Status) error {
|
func (this *ReportNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *reporterconfigs.Status) error {
|
||||||
if nodeStatus == nil {
|
if nodeStatus == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,6 +124,32 @@ func (this *ServerBandwidthStatDAO) FindServerStats(tx *dbs.Tx, serverId int64,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllServerStatsWithDay 查找某个服务的当天的所有带宽峰值
|
||||||
|
// day YYYYMMDD
|
||||||
|
func (this *ServerBandwidthStatDAO) FindAllServerStatsWithDay(tx *dbs.Tx, serverId int64, day string) (result []*ServerBandwidthStat, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Table(this.partialTable(serverId)).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Attr("day", day).
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllServerStatsWithMonth 查找某个服务的当月的所有带宽峰值
|
||||||
|
// month YYYYMM
|
||||||
|
func (this *ServerBandwidthStatDAO) FindAllServerStatsWithMonth(tx *dbs.Tx, serverId int64, month string) (result []*ServerBandwidthStat, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Table(this.partialTable(serverId)).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Between("day", month+"01", month+"31").
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// FindMonthlyPercentile 获取某月内百分位
|
// FindMonthlyPercentile 获取某月内百分位
|
||||||
func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId int64, month string, percentile int) (result int64, err error) {
|
func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId int64, month string, percentile int) (result int64, err error) {
|
||||||
if percentile <= 0 {
|
if percentile <= 0 {
|
||||||
|
|||||||
@@ -43,6 +43,30 @@ func TestServerBandwidthStatDAO_FindMonthlyPercentile(t *testing.T) {
|
|||||||
t.Log(dao.FindMonthlyPercentile(tx, 23, timeutil.Format("Ym"), 95))
|
t.Log(dao.FindMonthlyPercentile(tx, 23, timeutil.Format("Ym"), 95))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServerBandwidthStatDAO_FindAllServerStatsWithMonth(t *testing.T) {
|
||||||
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
stats, err := dao.FindAllServerStatsWithMonth(tx, 23, timeutil.Format("Ym"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, stat := range stats {
|
||||||
|
t.Logf("%+v", stat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerBandwidthStatDAO_FindAllServerStatsWithDay(t *testing.T) {
|
||||||
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
stats, err := dao.FindAllServerStatsWithDay(tx, 23, timeutil.Format("Ymd"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, stat := range stats {
|
||||||
|
t.Logf("%+v", stat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestServerBandwidthStatDAO_Clean(t *testing.T) {
|
func TestServerBandwidthStatDAO_Clean(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -270,7 +269,7 @@ func (this *CloudFlareProvider) doAPI(method string, apiPath string, args map[st
|
|||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@@ -180,5 +180,5 @@ func (this *CustomHTTPProvider) post(params maps.Map) (respData []byte, err erro
|
|||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
return nil, errors.New("status should be 200, but got '" + strconv.Itoa(resp.StatusCode) + "'")
|
return nil, errors.New("status should be 200, but got '" + strconv.Itoa(resp.StatusCode) + "'")
|
||||||
}
|
}
|
||||||
return ioutil.ReadAll(resp.Body)
|
return io.ReadAll(resp.Body)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -276,7 +276,7 @@ func (this *DNSPodProvider) post(path string, params map[string]string) (maps.Ma
|
|||||||
defer func() {
|
defer func() {
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}()
|
}()
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -1502,7 +1501,7 @@ func (this *HuaweiDNSProvider) doAPI(method string, apiPath string, args map[str
|
|||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,296 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package dnsclients
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LocalEdgeDNSProvider struct {
|
|
||||||
clusterId int64 // 集群ID
|
|
||||||
ttl int32 // TTL
|
|
||||||
|
|
||||||
BaseProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auth 认证
|
|
||||||
func (this *LocalEdgeDNSProvider) Auth(params maps.Map) error {
|
|
||||||
this.clusterId = params.GetInt64("clusterId")
|
|
||||||
if this.clusterId <= 0 {
|
|
||||||
return errors.New("'clusterId' should be greater than 0")
|
|
||||||
}
|
|
||||||
|
|
||||||
this.ttl = params.GetInt32("ttl")
|
|
||||||
if this.ttl <= 0 {
|
|
||||||
this.ttl = 3600
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDomains 获取所有域名列表
|
|
||||||
func (this *LocalEdgeDNSProvider) GetDomains() (domains []string, err error) {
|
|
||||||
var tx *dbs.Tx
|
|
||||||
domainOnes, err := nameservers.SharedNSDomainDAO.ListEnabledDomains(tx, this.clusterId, 0, "", 0, 1000)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, domain := range domainOnes {
|
|
||||||
domains = append(domains, domain.Name)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRecords 获取域名解析记录列表
|
|
||||||
func (this *LocalEdgeDNSProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
|
|
||||||
var tx *dbs.Tx
|
|
||||||
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if domainId == 0 {
|
|
||||||
return nil, errors.New("can not find domain '" + domain + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
offset := int64(0)
|
|
||||||
size := int64(1000)
|
|
||||||
for {
|
|
||||||
result, err := nameservers.SharedNSRecordDAO.ListEnabledRecords(tx, domainId, "", "", "", offset, size)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(result) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
for _, record := range result {
|
|
||||||
if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Value, ".") {
|
|
||||||
record.Value += "."
|
|
||||||
}
|
|
||||||
|
|
||||||
routeIds := record.DecodeRouteIds()
|
|
||||||
if len(routeIds) == 0 {
|
|
||||||
routeIds = []string{dnsconfigs.DefaultRouteCode}
|
|
||||||
}
|
|
||||||
records = append(records, &dnstypes.Record{
|
|
||||||
Id: fmt.Sprintf("%d", record.Id),
|
|
||||||
Name: record.Name,
|
|
||||||
Type: record.Type,
|
|
||||||
Value: record.Value,
|
|
||||||
Route: routeIds[0],
|
|
||||||
TTL: types.Int32(record.Ttl),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
offset += size
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRoutes 读取域名支持的线路数据
|
|
||||||
func (this *LocalEdgeDNSProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
|
|
||||||
var tx *dbs.Tx
|
|
||||||
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if domainId == 0 {
|
|
||||||
return nil, errors.New("can not find domain '" + domain + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认线路
|
|
||||||
for _, route := range dnsconfigs.AllDefaultRoutes {
|
|
||||||
routes = append(routes, &dnstypes.Route{
|
|
||||||
Name: route.Name,
|
|
||||||
Code: route.Code,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 自定义线路
|
|
||||||
result, err := nameservers.SharedNSRouteDAO.FindAllEnabledRoutes(tx, 0, 0, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, route := range result {
|
|
||||||
routes = append(routes, &dnstypes.Route{
|
|
||||||
Name: route.Name,
|
|
||||||
Code: "id:" + types.String(route.Id),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认ISP
|
|
||||||
for _, route := range dnsconfigs.AllDefaultISPRoutes {
|
|
||||||
routes = append(routes, &dnstypes.Route{
|
|
||||||
Name: route.Name,
|
|
||||||
Code: route.Code,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认中国省份
|
|
||||||
for _, route := range dnsconfigs.AllDefaultChinaProvinceRoutes {
|
|
||||||
routes = append(routes, &dnstypes.Route{
|
|
||||||
Name: route.Name,
|
|
||||||
Code: route.Code,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认全球国家/地区
|
|
||||||
for _, route := range dnsconfigs.AllDefaultWorldRegionRoutes {
|
|
||||||
routes = append(routes, &dnstypes.Route{
|
|
||||||
Name: route.Name,
|
|
||||||
Code: route.Code,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryRecord 查询单个记录
|
|
||||||
func (this *LocalEdgeDNSProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
|
|
||||||
var tx *dbs.Tx
|
|
||||||
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if domainId == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
record, err := nameservers.SharedNSRecordDAO.FindEnabledRecordWithName(tx, domainId, name, recordType)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if record == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
routeIds := record.DecodeRouteIds()
|
|
||||||
var routeIdString = ""
|
|
||||||
if len(routeIds) > 0 {
|
|
||||||
routeIdString = routeIds[0]
|
|
||||||
} else {
|
|
||||||
routeIdString = dnsconfigs.DefaultRouteCode
|
|
||||||
}
|
|
||||||
|
|
||||||
return &dnstypes.Record{
|
|
||||||
Id: fmt.Sprintf("%d", record.Id),
|
|
||||||
Name: record.Name,
|
|
||||||
Type: record.Type,
|
|
||||||
Value: record.Value,
|
|
||||||
Route: routeIdString,
|
|
||||||
TTL: types.Int32(record.Ttl),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddRecord 设置记录
|
|
||||||
func (this *LocalEdgeDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
|
|
||||||
var tx *dbs.Tx
|
|
||||||
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, newRecord)
|
|
||||||
}
|
|
||||||
if domainId == 0 {
|
|
||||||
return this.WrapError(errors.New("can not find domain '"+domain+"'"), domain, newRecord)
|
|
||||||
}
|
|
||||||
|
|
||||||
var routeIds = []string{}
|
|
||||||
if len(newRecord.Route) > 0 {
|
|
||||||
routeIds = append(routeIds, newRecord.Route)
|
|
||||||
}
|
|
||||||
|
|
||||||
if newRecord.TTL <= 0 {
|
|
||||||
newRecord.TTL = this.ttl
|
|
||||||
}
|
|
||||||
_, err = nameservers.SharedNSRecordDAO.CreateRecord(tx, domainId, "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, newRecord)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateRecord 修改记录
|
|
||||||
func (this *LocalEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
|
|
||||||
var tx *dbs.Tx
|
|
||||||
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, newRecord)
|
|
||||||
}
|
|
||||||
if domainId == 0 {
|
|
||||||
return errors.New("can not find domain '" + domain + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
var routeIds []string
|
|
||||||
if len(newRecord.Route) > 0 {
|
|
||||||
routeIds = append(routeIds, newRecord.Route)
|
|
||||||
}
|
|
||||||
|
|
||||||
if newRecord.TTL <= 0 {
|
|
||||||
newRecord.TTL = this.ttl
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(record.Id) > 0 {
|
|
||||||
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(record.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds, true)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, newRecord)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
realRecord, err := nameservers.SharedNSRecordDAO.FindEnabledRecordWithName(tx, domainId, record.Name, record.Type)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, newRecord)
|
|
||||||
}
|
|
||||||
if realRecord != nil {
|
|
||||||
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(realRecord.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds, true)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, newRecord)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteRecord 删除记录
|
|
||||||
func (this *LocalEdgeDNSProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
|
|
||||||
var tx *dbs.Tx
|
|
||||||
domainId, err := nameservers.SharedNSDomainDAO.FindDomainIdWithName(tx, this.clusterId, domain)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, record)
|
|
||||||
}
|
|
||||||
if domainId == 0 {
|
|
||||||
return errors.New("can not find domain '" + domain + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(record.Id) > 0 {
|
|
||||||
err = nameservers.SharedNSRecordDAO.DisableNSRecord(tx, types.Int64(record.Id))
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, record)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
realRecord, err := nameservers.SharedNSRecordDAO.FindEnabledRecordWithName(tx, domainId, record.Name, record.Type)
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, record)
|
|
||||||
}
|
|
||||||
if realRecord != nil {
|
|
||||||
err = nameservers.SharedNSRecordDAO.DisableNSRecord(tx, types.Int64(realRecord.Id))
|
|
||||||
if err != nil {
|
|
||||||
return this.WrapError(err, domain, record)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultRoute 默认线路
|
|
||||||
func (this *LocalEdgeDNSProvider) DefaultRoute() string {
|
|
||||||
return "default"
|
|
||||||
}
|
|
||||||
@@ -1,182 +0,0 @@
|
|||||||
// 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/dbs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
const testClusterId = 7
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_GetDomains(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
domains, err := provider.GetDomains()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log("domains:", domains)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_GetRecords(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
records, err := provider.GetRecords("teaos.cn")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
logs.PrintAsJSON(records, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_GetRoutes(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
routes, err := provider.GetRoutes("teaos.cn")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
logs.PrintAsJSON(routes, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_QueryRecord(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
record, err := provider.QueryRecord("teaos.cn", "cdn", dnstypes.RecordTypeA)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
logs.PrintAsJSON(record)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_AddRecord(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = provider.AddRecord("teaos.cn", &dnstypes.Record{
|
|
||||||
Id: "",
|
|
||||||
Name: "example",
|
|
||||||
Type: dnstypes.RecordTypeA,
|
|
||||||
Value: "10.0.0.1",
|
|
||||||
Route: "id:7",
|
|
||||||
TTL: 300,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log("ok")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_UpdateRecord(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
record, err := provider.QueryRecord("teaos.cn", "cdn", dnstypes.RecordTypeA)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if record == nil {
|
|
||||||
t.Log("not found record")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//record.Id = ""
|
|
||||||
err = provider.UpdateRecord("teaos.cn", record, &dnstypes.Record{
|
|
||||||
Id: "",
|
|
||||||
Name: record.Name,
|
|
||||||
Type: record.Type,
|
|
||||||
Value: "127.0.0.3",
|
|
||||||
Route: record.Route,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log("ok")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_DeleteRecord(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = provider.DeleteRecord("teaos.cn", &dnstypes.Record{
|
|
||||||
Id: "",
|
|
||||||
Name: "example",
|
|
||||||
Type: "A",
|
|
||||||
Value: "",
|
|
||||||
Route: "",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log("ok")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLocalEdgeDNSProvider_DefaultRoute(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
provider := &dnsclients.LocalEdgeDNSProvider{}
|
|
||||||
err := provider.Auth(maps.Map{
|
|
||||||
"clusterId": testClusterId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Log(provider.DefaultRoute())
|
|
||||||
}
|
|
||||||
@@ -52,28 +52,6 @@ func FindAllProviderTypes() []maps.Map {
|
|||||||
return typeMaps
|
return typeMaps
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindProvider 查找服务商实例
|
|
||||||
func FindProvider(providerType ProviderType) ProviderInterface {
|
|
||||||
switch providerType {
|
|
||||||
case ProviderTypeDNSPod:
|
|
||||||
return &DNSPodProvider{}
|
|
||||||
case ProviderTypeAliDNS:
|
|
||||||
return &AliDNSProvider{}
|
|
||||||
case ProviderTypeHuaweiDNS:
|
|
||||||
return &HuaweiDNSProvider{}
|
|
||||||
case ProviderTypeCloudFlare:
|
|
||||||
return &CloudFlareProvider{}
|
|
||||||
case ProviderTypeLocalEdgeDNS:
|
|
||||||
return &LocalEdgeDNSProvider{}
|
|
||||||
case ProviderTypeUserEdgeDNS:
|
|
||||||
return &UserEdgeDNSProvider{}
|
|
||||||
case ProviderTypeCustomHTTP:
|
|
||||||
return &CustomHTTPProvider{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return filterProvider(providerType)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindProviderTypeName 查找服务商名称
|
// FindProviderTypeName 查找服务商名称
|
||||||
func FindProviderTypeName(providerType ProviderType) string {
|
func FindProviderTypeName(providerType ProviderType) string {
|
||||||
for _, t := range FindAllProviderTypes() {
|
for _, t := range FindAllProviderTypes() {
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user