Compare commits

...

106 Commits

Author SHA1 Message Date
刘祥超
c2c42ca2b7 优化服务列表返回速度 2022-09-30 13:50:19 +08:00
刘祥超
2a6db6ebfe 边缘节点远程安装文件最小化(从16.xM减少到2.xM) 2022-09-30 10:34:32 +08:00
刘祥超
30d8edbdcf 执行uname和systemctl时增加命令完整路径 2022-09-30 09:40:38 +08:00
刘祥超
177afafe12 完善订单相关表 2022-09-29 10:22:17 +08:00
刘祥超
98765b6e2a 版本调整为v0.5.5 2022-09-28 18:56:47 +08:00
刘祥超
e4e0aab010 阶段性提交 2022-09-28 17:38:52 +08:00
刘祥超
ed87b4e2a9 用户节点版本改为0.5.4 2022-09-28 08:56:40 +08:00
刘祥超
337eb36d25 DNS版本改为0.2.8 2022-09-28 08:16:57 +08:00
刘祥超
c44e40d72d systemd服务增加BEGIN INIT INFO 2022-09-28 08:16:49 +08:00
刘祥超
2e8ba831a1 DNS版本修改为0.2.7.1 2022-09-27 08:06:16 +08:00
刘祥超
a706c2a5a5 将版本修改为0.5.4 2022-09-26 15:17:00 +08:00
刘祥超
093826222a 将版本修改为v0.5.3.1 2022-09-26 12:16:44 +08:00
刘祥超
0d7b487afc 修复开源版本无法编译的问题 2022-09-26 12:16:20 +08:00
刘祥超
8de17b6d9c 只有在数据库用户是root时才执行某些命令 2022-09-26 12:16:08 +08:00
刘祥超
49d217a883 提交EdgeDNS API相关代码 2022-09-26 11:51:45 +08:00
刘祥超
4827555899 启动过程增加多个提示 2022-09-26 11:00:58 +08:00
刘祥超
41f3825ee2 使用并发队列安装和升级数据表/启动时自动调整MySQL变量 2022-09-25 20:34:19 +08:00
刘祥超
096aa153ab 创建缓存策略时默认缓存条件自动支持206 Partial Content,并使用简单条件设置 2022-09-25 10:37:05 +08:00
刘祥超
8f8b611ac1 查询5分钟流量接口增加timeFrom和timeTo参数 2022-09-24 18:55:33 +08:00
刘祥超
08b1c038f1 服务流量接口增加5分钟查询接口 2022-09-24 18:36:27 +08:00
刘祥超
934b10a254 REST 接口接收内容为空时,默认为 {} 2022-09-24 18:35:29 +08:00
刘祥超
4947f13416 更新SQL 2022-09-24 18:34:46 +08:00
刘祥超
84198d5948 优化智能DNS相关代码 2022-09-24 14:07:19 +08:00
刘祥超
d4a04bc798 智能DNS支持应答模式配置 2022-09-23 19:01:18 +08:00
刘祥超
c94b3c26c1 优化代码 2022-09-23 15:25:51 +08:00
刘祥超
5655f89ba6 优化代码 2022-09-23 09:28:19 +08:00
刘祥超
c568ad3e9a 查看访问日志列表时,不查询requestBody和responseBody,防止内容过大 2022-09-22 16:47:36 +08:00
刘祥超
d2f532447d 同步防盗链功能 2022-09-22 16:41:02 +08:00
刘祥超
ec97feab28 增加防盗链功能 2022-09-22 16:33:46 +08:00
刘祥超
316cd36f71 删除集群的时候,同时删除对应的域名解析/集群切换二级域名先删除再添加新的域名解析 2022-09-22 14:09:28 +08:00
刘祥超
702a0f1ecf DNS访问日志创建表已经存在时不提示错误 2022-09-21 20:41:21 +08:00
刘祥超
2d2b7b7bff 优化代码 2022-09-21 15:09:39 +08:00
刘祥超
6f8c5a8e99 修复按日带宽峰值查询错误的Bug 2022-09-20 13:57:45 +08:00
刘祥超
afb35953e7 DNS集群增加时区选项 2022-09-19 17:00:39 +08:00
刘祥超
2a1f78a440 修复一处带宽计算错误 2022-09-19 14:39:22 +08:00
刘祥超
fb084a9f48 优化代码 2022-09-18 12:39:17 +08:00
刘祥超
e7f620d28f 同步域名解析时不再强制要求修复节点问题 2022-09-18 10:41:29 +08:00
刘祥超
2f64d713e8 优化代码/DNS域名增加分页 2022-09-18 10:23:04 +08:00
刘祥超
86bf316468 可以设置是否自动安装nftables 2022-09-17 21:05:18 +08:00
刘祥超
bf320271d4 修复DNSPod只能取前100个域名的问题 2022-09-17 17:16:30 +08:00
刘祥超
150a63fe98 用户端刷新预热缓存超过5分钟自动认为已完成 2022-09-17 16:50:03 +08:00
刘祥超
1d1ff11eb7 优化接口权限 2022-09-17 16:07:37 +08:00
刘祥超
55eecce416 集群增加是否远程启动选项 2022-09-17 15:11:34 +08:00
刘祥超
5f822062da 检查域名是否存在时同时检查泛域名 2022-09-17 11:38:47 +08:00
刘祥超
ee2c253e7d 节点离线时尝试自动通过API节点远程启动 2022-09-17 10:18:00 +08:00
刘祥超
5b0adb4b84 创建集群的时候初始化全局服务配置 2022-09-16 19:34:19 +08:00
刘祥超
8c72540a6e 集群设置中增加服务设置 2022-09-16 18:42:14 +08:00
刘祥超
7f811997a9 证书申请任务不区分管理员 2022-09-16 15:34:03 +08:00
刘祥超
ca94e31451 启动时自动设置binlog过期时间 2022-09-16 10:39:17 +08:00
刘祥超
320d381bd9 集群增加自动同步时钟选项 2022-09-15 15:56:50 +08:00
刘祥超
c8057457cc 更新SQL 2022-09-15 11:48:51 +08:00
刘祥超
c78c4d58ff 用户系统可以切换CDN和智能DNS 2022-09-13 19:04:31 +08:00
刘祥超
d1f8e7e757 增加DNS套餐相关数据 2022-09-13 10:49:58 +08:00
刘祥超
5386b30eba 用户节点版本修改为0.5.0 2022-09-13 10:49:17 +08:00
刘祥超
8ceb1334cd 调整GRPC参数 2022-09-12 22:03:06 +08:00
刘祥超
023e563de1 调整GRPC参数 2022-09-12 22:00:12 +08:00
刘祥超
39e6d11d71 优化代码 2022-09-11 14:41:06 +08:00
刘祥超
a5a9117ce0 增强系统配置接口的权限检查 2022-09-10 17:00:54 +08:00
刘祥超
e95b0bd9a6 实现DNS域名验证 2022-09-10 16:13:21 +08:00
刘祥超
9699a9adad 自动调整写入单次写入访问日志数量 2022-09-09 21:15:03 +08:00
刘祥超
67729abd13 修复多个日志数据库节点写入NS日志时分布不平均的问题 2022-09-09 21:06:43 +08:00
刘祥超
4c7ebce97a 修复多个日志数据库节点分布不平均的问题 2022-09-09 20:37:45 +08:00
刘祥超
44e7ce9f79 自动升级NS域名状态为verified 2022-09-09 15:25:32 +08:00
刘祥超
3468fcf8a6 DNS版本修改为0.2.7 2022-09-09 14:29:29 +08:00
刘祥超
3522c22a28 修复无法一个常量错误 2022-09-09 10:39:28 +08:00
刘祥超
333a9c6611 修改管理界面设置中的时区时同时也会应用到API节点/API节点默认时区为Asia/Shanghai 2022-09-09 10:28:44 +08:00
刘祥超
e3426a84e2 域名解析增加EdgeDNS API 2022-09-08 19:36:32 +08:00
刘祥超
a25b0e6c9d 创建集群的时候可以设置DNS记录的默认TTL 2022-09-08 11:02:26 +08:00
刘祥超
c271cadabd 计算区域监控终端低版本数量时忽略已禁用的终端 2022-09-08 10:14:02 +08:00
刘祥超
428bb7eb0f 访问日志支持中文域名搜索 2022-09-07 17:07:46 +08:00
刘祥超
6ae9f447b6 增加启动过程标记 2022-09-07 15:57:31 +08:00
刘祥超
7cc503b698 将版本修改为0.5.3 2022-09-06 09:23:28 +08:00
刘祥超
c66e28cb9d 减少带宽统计显示的空档期 2022-09-05 16:04:34 +08:00
刘祥超
e5109b24d4 版本修改为0.5.2.1 2022-09-05 16:02:55 +08:00
刘祥超
695b8482de 将版本修改为0.5.3 2022-09-05 11:03:12 +08:00
刘祥超
d0b908bcaa 自动添加firewalld使用异步 2022-09-04 06:36:22 +08:00
刘祥超
3de25d4fe1 优化代码 2022-09-03 22:23:16 +08:00
刘祥超
07194855bf 优化代码 2022-09-03 22:03:22 +08:00
刘祥超
d0f1eb13ee 优化节点活跃检测机制 2022-09-03 12:43:06 +08:00
刘祥超
a0930bfd74 远程安装节点出错时打印stderr 2022-08-30 11:40:01 +08:00
刘祥超
08cff8affc 可以通过用户API修改鉴权 2022-08-30 11:23:35 +08:00
刘祥超
02132e9262 用户系统也可以申请ACME证书 2022-08-28 20:02:13 +08:00
刘祥超
61b6a49885 增加修改全体用户功能API 2022-08-28 17:01:09 +08:00
刘祥超
896e54ebe8 提供按小时、按天查询带宽峰值的API 2022-08-28 15:56:16 +08:00
刘祥超
1b36bad60a 指标统计使用事务 2022-08-27 18:50:42 +08:00
刘祥超
fc14800d70 服务列表带宽使用新的算法 2022-08-27 18:39:00 +08:00
刘祥超
fa61f277e4 服务访问日志改成通过事务写入,以提升写入速度 2022-08-27 14:57:47 +08:00
刘祥超
9117309472 可以修改服务的CNAME 2022-08-26 19:51:21 +08:00
刘祥超
6bb2977d59 Ln节点可以指定访问IP 2022-08-25 20:37:10 +08:00
刘祥超
df9dce76cb 集群DNS设置中增加”包含Ln节点“选项 2022-08-25 19:18:30 +08:00
刘祥超
4cb9c85a1c 节点运行日志可以按照节点ID设置为已读 2022-08-25 18:26:52 +08:00
刘祥超
f4f5389ffb 请求限制API支持用户调用 2022-08-25 15:35:55 +08:00
刘祥超
5d336eb77d 优化代码 2022-08-23 21:42:05 +08:00
刘祥超
c552eb3b0e IP库增加制品管理/统计中相关区域名称可以显示别名 2022-08-23 19:40:17 +08:00
刘祥超
455952e9e4 提交SQL 2022-08-22 15:12:20 +08:00
刘祥超
7132401c7f NS节点基本的DDoS防护 2022-08-22 15:11:22 +08:00
刘祥超
a4dddfb139 优化代码 2022-08-22 11:02:16 +08:00
刘祥超
7ef32bad97 IP库改为手动初始化 2022-08-21 23:09:59 +08:00
刘祥超
732513a644 用户节点版本修改为0.4.1 2022-08-21 20:50:00 +08:00
刘祥超
756cf4a9ae 初步完成新版IP库 2022-08-21 20:38:34 +08:00
刘祥超
a15a630265 更新SQL 2022-08-20 19:57:25 +08:00
刘祥超
3fab1b8294 DNS节点版本号改为0.2.6 2022-08-20 15:27:02 +08:00
刘祥超
215635f429 版本修改为0.5.2 2022-08-17 18:58:20 +08:00
刘祥超
dbb1ae180b 版本修改为0.5.1 2022-08-15 19:38:40 +08:00
刘祥超
e8d4d01d85 改进一处日志 2022-08-15 15:17:09 +08:00
刘祥超
6593989a84 修复日志内容可能过长而无法存入数据库的问题 2022-08-15 15:05:47 +08:00
219 changed files with 5231 additions and 8076 deletions

View File

@@ -95,9 +95,6 @@ function build() {
cp -R "$ROOT"/deploy "$DIST/"
rm -f "$DIST"/deploy/.gitignore
cp -R "$ROOT"/installers "$DIST"/
cp -R "$ROOT"/resources "$DIST"/
rm -f "$DIST"/resources/ipdata/ip2region/global_region.csv
rm -f "$DIST"/resources/ipdata/ip2region/ip.merge.txt
# building edge installer
echo "building node installer ..."
@@ -108,12 +105,14 @@ function build() {
done
# building edge dns installer
echo "building dns node installer ..."
architects=("amd64" "arm64")
for arch in "${architects[@]}"; do
# TODO support arm, mips ...
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-dns-helper-linux-"${arch}" "$ROOT"/../cmd/installer-dns-helper/main.go
done
if [ $TAG = "plus" ]; then
echo "building dns node installer ..."
architects=("amd64" "arm64")
for arch in "${architects[@]}"; do
# TODO support arm, mips ...
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-dns-helper-linux-"${arch}" "$ROOT"/../cmd/installer-dns-helper/main.go
done
fi
# building api node
env GOOS="$OS" GOARCH="$ARCH" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$DIST"/bin/edge-api "$ROOT"/../cmd/edge-api/main.go

View File

@@ -9,3 +9,7 @@ dbs:
prefix: "edge"
models:
package: internal/web/models
fields:
bool: [ "uamIsOn", "followPort", "requestHostExcludingPort", "autoRemoteStart", "autoInstallNftables" ]

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,6 @@ import (
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"github.com/iwind/gosock/pkg/gosock"
"io/ioutil"
"log"
"os"
)
@@ -130,7 +129,7 @@ func main() {
flagSet.BoolVar(&formatJSON, "json", false, "")
_ = flagSet.Parse(os.Args[2:])
data, err := ioutil.ReadFile(Tea.LogFile("issues.log"))
data, err := os.ReadFile(Tea.LogFile("issues.log"))
if err != nil {
if formatJSON {
fmt.Print("[]")

View File

@@ -2,7 +2,7 @@ package main
import (
"flag"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
"github.com/iwind/gosock/pkg/gosock"
"os"
"os/exec"
@@ -51,7 +51,7 @@ func main() {
return
}
unzip := utils.NewUnzip(zipPath, targetPath)
unzip := helpers.NewUnzip(zipPath, targetPath)
err := unzip.Run()
if err != nil {
stderr("ERROR: " + err.Error())

View File

@@ -1,8 +1,9 @@
package main
// 注意这里的依赖文件应该最小化,从而使编译后的文件最小化
import (
"flag"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
"github.com/iwind/gosock/pkg/gosock"
"os"
"os/exec"
@@ -51,7 +52,7 @@ func main() {
return
}
unzip := utils.NewUnzip(zipPath, targetPath)
unzip := helpers.NewUnzip(zipPath, targetPath)
err := unzip.Run()
if err != nil {
stderr("ERROR: " + err.Error())

View File

@@ -1,193 +0,0 @@
package main
import (
"bytes"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/iwind/TeaGo/Tea"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"io/ioutil"
"os"
"regexp"
"strings"
)
func main() {
// 导入数据
if lists.ContainsString(os.Args, "import") {
dbs.NotifyReady()
data, err := ioutil.ReadFile(Tea.Root + "/resources/ipdata/ip2region/global_region.csv")
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if len(data) == 0 {
logs.Println("[ERROR]file content should not be empty")
return
}
lines := bytes.Split(data, []byte{'\n'})
for _, line := range lines {
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
s := string(line)
reg := regexp.MustCompile(`(?U)(\d+),(\d+),(.+),(\d+),`)
if !reg.MatchString(s) {
continue
}
result := reg.FindStringSubmatch(s)
dataId := result[1]
parentDataId := result[2]
name := result[3]
level := result[4]
switch level {
case "1": // 国家|地区
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithDataId(nil, dataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if countryId == 0 {
logs.Println("creating country or region ", name)
_, err = regions.SharedRegionCountryDAO.CreateCountry(nil, name, dataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
}
case "2": // 省份|地区
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithDataId(nil, dataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if provinceId == 0 {
logs.Println("creating province", name)
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithDataId(nil, parentDataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if countryId == 0 {
logs.Println("[ERROR]can not find country from data id '" + parentDataId + "'")
return
}
_, err = regions.SharedRegionProvinceDAO.CreateProvince(nil, countryId, name, dataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
}
case "3": // 城市
cityId, err := regions.SharedRegionCityDAO.FindCityWithDataId(nil, dataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if cityId == 0 {
logs.Println("creating city", name)
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithDataId(nil, parentDataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
_, err = regions.SharedRegionCityDAO.CreateCity(nil, provinceId, name, dataId)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
}
}
}
logs.Println("done")
}
// 检查数据
if lists.ContainsString(os.Args, "check") {
dbs.NotifyReady()
data, err := ioutil.ReadFile(Tea.Root + "/resources/ipdata/ip2region/ip.merge.txt")
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if len(data) == 0 {
logs.Println("[ERROR]file should not be empty")
return
}
lines := bytes.Split(data, []byte("\n"))
for index, line := range lines {
s := string(bytes.TrimSpace(line))
if len(s) == 0 {
continue
}
pieces := strings.Split(s, "|")
countryName := pieces[2]
provinceName := pieces[4]
providerName := pieces[6]
// 记录provider
if len(providerName) > 0 && providerName != "0" {
providerId, err := regions.SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, providerName)
if err != nil {
logs.Println("[ERROR]find provider id failed: " + err.Error())
return
}
if providerId == 0 {
logs.Println("creating new provider '"+providerName+"' ... ", index, "line")
_, err = regions.SharedRegionProviderDAO.CreateProvider(nil, providerName)
if err != nil {
logs.Println("create new provider failed: " + providerName)
return
}
logs.Println("created new provider '" + providerName + "'")
return
}
}
if lists.ContainsString([]string{"0", "欧洲", "北美地区", "法国南部领地", "非洲地区", "亚太地区"}, countryName) {
continue
}
// 检查国家
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, countryName)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if countryId == 0 {
logs.Println("[ERROR]can not find country '"+countryName+"', index: ", index, "data: "+s)
return
}
// 检查省份
if countryName == "中国" {
if lists.ContainsString([]string{"0"}, provinceName) {
continue
}
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, countryId, provinceName)
if err != nil {
logs.Println("[ERROR]" + err.Error())
return
}
if provinceId == 0 {
logs.Println("[ERROR]can not find province '"+provinceName+"', index: ", index, "data: "+s)
return
}
}
}
logs.Println("done")
}
}

View File

@@ -7,7 +7,6 @@ import (
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -67,7 +66,7 @@ func init() {
return
}
err = ioutil.WriteFile(sqlFile, dst, 0666)
err = os.WriteFile(sqlFile, dst, 0666)
if err != nil {
fmt.Println("[ERROR]write file failed: " + err.Error())
return

View File

@@ -24,11 +24,11 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)
// 设置记录
index := strings.Index(fqdn, "."+this.dnsDomain)
var index = strings.Index(fqdn, "."+this.dnsDomain)
if index < 0 {
return errors.New("invalid fqdn value")
}
recordName := fqdn[:index]
var recordName = fqdn[:index]
record, err := this.raw.QueryRecord(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
if err != nil {
return errors.New("query DNS record failed: " + err.Error())

View File

@@ -40,6 +40,7 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
}
if this.task.Provider.RequireEAB && this.task.Account == nil {
err = errors.New("account should not be nil when provider require EAB")
return
}
switch this.task.AuthType {
@@ -75,7 +76,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
return
}
config := lego.NewConfig(this.task.User)
var config = lego.NewConfig(this.task.User)
config.Certificate.KeyType = certcrypto.RSA2048
config.CADirURL = this.task.Provider.APIURL
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
@@ -86,7 +87,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
}
// 注册用户
resource := this.task.User.GetRegistration()
var resource = this.task.User.GetRegistration()
if resource != nil {
resource, err = client.Registration.QueryRegistration()
if err != nil {
@@ -124,7 +125,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
}
// 申请证书
request := certificate.ObtainRequest{
var request = certificate.ObtainRequest{
Domains: this.task.Domains,
Bundle: true,
}
@@ -146,7 +147,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
return
}
config := lego.NewConfig(this.task.User)
var config = lego.NewConfig(this.task.User)
config.Certificate.KeyType = certcrypto.RSA2048
config.CADirURL = this.task.Provider.APIURL
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
@@ -157,7 +158,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
}
// 注册用户
resource := this.task.User.GetRegistration()
var resource = this.task.User.GetRegistration()
if resource != nil {
resource, err = client.Registration.QueryRegistration()
if err != nil {
@@ -195,7 +196,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
}
// 申请证书
request := certificate.ObtainRequest{
var request = certificate.ObtainRequest{
Domains: this.task.Domains,
Bundle: true,
}

View File

@@ -1,7 +1,7 @@
package teaconst
const (
Version = "0.5.0"
Version = "0.5.5"
ProductName = "Edge API"
ProcessName = "edge-api"
@@ -18,11 +18,11 @@ const (
// 其他节点版本号,用来检测是否有需要升级的节点
NodeVersion = "0.5.0"
UserNodeVersion = "0.4.0"
NodeVersion = "0.5.5"
UserNodeVersion = "0.5.5"
DNSNodeVersion = "0.2.8"
AuthorityNodeVersion = "0.0.2"
MonitorNodeVersion = "0.0.4"
DNSNodeVersion = "0.2.5"
ReportNodeVersion = "0.1.1"
// SQLVersion SQL版本号

View File

@@ -13,22 +13,26 @@ type OrderMethod struct {
Url string `field:"url"` // URL
Secret string `field:"secret"` // 密钥
Params dbs.JSON `field:"params"` // 参数
ClientType string `field:"clientType"` // 客户端类型
QrcodeTitle string `field:"qrcodeTitle"` // 二维码标题
Order uint32 `field:"order"` // 排序
State uint8 `field:"state"` // 状态
}
type OrderMethodOperator struct {
Id interface{} // ID
Name interface{} // 名称
IsOn interface{} // 是否启用
Description interface{} // 描述
ParentCode interface{} // 内置的父级代号
Code interface{} // 代号
Url interface{} // URL
Secret interface{} // 密钥
Params interface{} // 参数
Order interface{} // 排序
State interface{} // 状态
Id any // ID
Name any // 名称
IsOn any // 是否启用
Description any // 描述
ParentCode any // 内置的父级代号
Code any // 代号
Url any // URL
Secret any // 密钥
Params any // 参数
ClientType any // 客户端类型
QrcodeTitle any // 二维码标题
Order any // 排序
State any // 状态
}
func NewOrderMethodOperator() *OrderMethodOperator {

View File

@@ -1,6 +1,7 @@
package acme
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -72,8 +73,9 @@ func (this *ACMEProviderAccountDAO) FindACMEProviderAccountName(tx *dbs.Tx, id i
}
// CreateAccount 创建账号
func (this *ACMEProviderAccountDAO) CreateAccount(tx *dbs.Tx, name string, providerCode string, eabKid string, eabKey string) (int64, error) {
func (this *ACMEProviderAccountDAO) CreateAccount(tx *dbs.Tx, userId int64, name string, providerCode string, eabKid string, eabKey string) (int64, error) {
var op = NewACMEProviderAccountOperator()
op.UserId = userId
op.Name = name
op.ProviderCode = providerCode
op.EabKid = eabKid
@@ -98,15 +100,18 @@ func (this *ACMEProviderAccountDAO) UpdateAccount(tx *dbs.Tx, accountId int64, n
}
// CountAllEnabledAccounts 计算账号数量
func (this *ACMEProviderAccountDAO) CountAllEnabledAccounts(tx *dbs.Tx) (int64, error) {
func (this *ACMEProviderAccountDAO) CountAllEnabledAccounts(tx *dbs.Tx, userId int64) (int64, error) {
return this.Query(tx).
State(ACMEProviderAccountStateEnabled).
Attr("userId", userId).
Count()
}
// ListEnabledAccounts 查找单页账号
func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, offset int64, size int64) (result []*ACMEProviderAccount, err error) {
func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, userId int64, offset int64, size int64) (result []*ACMEProviderAccount, err error) {
_, err = this.Query(tx).
State(ACMEProviderAccountStateEnabled).
Attr("userId", userId).
Offset(offset).
Limit(size).
DescPk().
@@ -116,12 +121,34 @@ func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, offset int64
}
// FindAllEnabledAccountsWithProviderCode 根据服务商代号查找账号
func (this *ACMEProviderAccountDAO) FindAllEnabledAccountsWithProviderCode(tx *dbs.Tx, providerCode string) (result []*ACMEProviderAccount, err error) {
func (this *ACMEProviderAccountDAO) FindAllEnabledAccountsWithProviderCode(tx *dbs.Tx, userId int64, providerCode string) (result []*ACMEProviderAccount, err error) {
_, err = this.Query(tx).
State(ACMEProviderAccountStateEnabled).
Attr("providerCode", providerCode).
Attr("userId", userId).
DescPk().
Slice(&result).
FindAll()
return
}
// CheckUserAccount 检查是否为用户的服务商账号
func (this *ACMEProviderAccountDAO) CheckUserAccount(tx *dbs.Tx, userId int64, accountId int64) error {
if userId <= 0 || accountId <= 0 {
return models.ErrNotFound
}
b, err := this.Query(tx).
Pk(accountId).
State(ACMEProviderAccountStateEnabled).
Attr("userId", userId).
Exist()
if err != nil {
return err
}
if !b {
return models.ErrNotFound
}
return nil
}

View File

@@ -3,24 +3,26 @@ package acme
// ACMEProviderAccount ACME提供商
type ACMEProviderAccount struct {
Id uint64 `field:"id"` // ID
UserId uint64 `field:"userId"` // 用户ID
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 名称
ProviderCode string `field:"providerCode"` // 代号
Error string `field:"error"` // 最后一条错误信息
EabKid string `field:"eabKid"` // KID
EabKey string `field:"eabKey"` // Key
Error string `field:"error"` // 最后一条错误信息
State uint8 `field:"state"` // 状态
}
type ACMEProviderAccountOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启
Name interface{} // 名称
ProviderCode interface{} // 代号
Error interface{} // 最后一条错误信息
EabKid interface{} // KID
EabKey interface{} // Key
State interface{} // 状态
Id any // ID
UserId any // 用户ID
IsOn any // 是否启用
Name any // 名称
ProviderCode any // 代号
EabKid any // KID
EabKey any // Key
Error any // 最后一条错误信息
State any // 状态
}
func NewACMEProviderAccountOperator() *ACMEProviderAccountOperator {

View File

@@ -106,8 +106,9 @@ func (this *ACMETaskDAO) DisableAllTasksWithCertId(tx *dbs.Tx, certId int64) err
}
// CountAllEnabledACMETasks 计算所有任务数量
func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string) (int64, error) {
query := dbutils.NewQuery(tx, this, adminId, userId)
func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string) (int64, error) {
var query = this.Query(tx)
query.Attr("userId", userId) // 这个条件必须加上
if isAvailable || isExpired || expiringDays > 0 {
query.Gt("certId", 0)
@@ -137,8 +138,9 @@ func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, use
}
// ListEnabledACMETasks 列出单页任务
func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string, offset int64, size int64) (result []*ACMETask, err error) {
query := dbutils.NewQuery(tx, this, adminId, userId)
func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string, offset int64, size int64) (result []*ACMETask, err error) {
var query = this.Query(tx)
query.Attr("userId", userId) // 这个条件必须加上
if isAvailable || isExpired || expiringDays > 0 {
query.Gt("certId", 0)
@@ -227,8 +229,13 @@ func (this *ACMETaskDAO) UpdateACMETask(tx *dbs.Tx, acmeTaskId int64, acmeUserId
}
// CheckACMETask 检查权限
func (this *ACMETaskDAO) CheckACMETask(tx *dbs.Tx, adminId int64, userId int64, acmeTaskId int64) (bool, error) {
return dbutils.NewQuery(tx, this, adminId, userId).
func (this *ACMETaskDAO) CheckACMETask(tx *dbs.Tx, userId int64, acmeTaskId int64) (bool, error) {
var query = this.Query(tx)
if userId > 0 {
query.Attr("userId", userId)
}
return query.
State(ACMETaskStateEnabled).
Pk(acmeTaskId).
Exist()
@@ -319,7 +326,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
return
}
remoteUser := acmeutils.NewUser(user.Email, privateKey, func(resource *registration.Resource) error {
var remoteUser = acmeutils.NewUser(user.Email, privateKey, func(resource *registration.Resource) error {
resourceJSON, err := json.Marshal(resource)
if err != nil {
return err
@@ -382,7 +389,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
acmeTask.Provider = acmeProvider
acmeTask.Account = acmeAccount
acmeRequest := acmeutils.NewRequest(acmeTask)
var acmeRequest = acmeutils.NewRequest(acmeTask)
acmeRequest.OnAuth(func(domain, token, keyAuth string) {
err := SharedACMEAuthenticationDAO.CreateAuth(tx, taskId, domain, token, keyAuth)
if err != nil {
@@ -398,7 +405,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
if err != nil {
remotelogs.Error("ACME", "encode auth data failed: '"+task.AuthURL+"'")
} else {
client := utils.SharedHttpClient(5 * time.Second)
var client = utils.SharedHttpClient(10 * time.Second)
req, err := http.NewRequest(http.MethodPost, task.AuthURL, bytes.NewReader(authJSON))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
@@ -423,7 +430,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
}
// 分析证书
sslConfig := &sslconfigs.SSLCertConfig{
var sslConfig = &sslconfigs.SSLCertConfig{
CertData: certData,
KeyData: keyData,
}

View File

@@ -2,18 +2,14 @@ package models
import (
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"hash/crc32"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
@@ -31,22 +27,12 @@ type httpAccessLogDefinition struct {
// HTTP服务访问
var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO
// DNS服务访问
var nsAccessLogDAOMapping = map[int64]*NSAccessLogDAOWrapper{} // dbNodeId => DAO
var nsAccessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
type HTTPAccessLogDAOWrapper struct {
DAO *HTTPAccessLogDAO
NodeId int64
}
// NSAccessLogDAOWrapper NS访问日志DAO
type NSAccessLogDAOWrapper struct {
DAO *NSAccessLogDAO
NodeId int64
}
func init() {
initializer := NewDBNodeInitializer()
dbs.OnReadyDone(func() {
@@ -78,102 +64,28 @@ func AllAccessLogDBs() []*dbs.DB {
// 获取获取DAO
func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
accessLogLocker.RLock()
defer accessLogLocker.RUnlock()
if len(httpAccessLogDAOMapping) == 0 {
dao = nil
} else {
for _, d := range httpAccessLogDAOMapping {
dao = d
break
}
}
accessLogLocker.RUnlock()
return
}
func randomNSAccessLogDAO() (dao *NSAccessLogDAOWrapper) {
accessLogLocker.RLock()
if len(nsAccessLogDAOMapping) == 0 {
dao = nil
} else {
for _, d := range nsAccessLogDAOMapping {
dao = d
break
}
}
accessLogLocker.RUnlock()
return
}
func findNSAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
err = errors.New("invalid day '" + day + "', should be YYYYMMDD")
return
}
config, err := db.Config()
if err != nil {
return "", false, err
var daoList = []*HTTPAccessLogDAOWrapper{}
for _, d := range httpAccessLogDAOMapping {
daoList = append(daoList, d)
}
tableName = "edgeNSAccessLogs_" + day
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
accessLogLocker.RLock()
_, ok = nsAccessLogTableMapping[cacheKey]
accessLogLocker.RUnlock()
if ok {
return tableName, true, nil
var l = len(daoList)
if l == 0 {
return
}
tableNames, err := db.TableNames()
if err != nil {
return tableName, false, err
if l == 1 {
return daoList[0]
}
return tableName, utils.ContainsStringInsensitive(tableNames, tableName), nil
}
func findNSAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
config, err := db.Config()
if err != nil {
return "", err
}
tableName := "edgeNSAccessLogs_" + day
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
if !force {
accessLogLocker.RLock()
_, ok := nsAccessLogTableMapping[cacheKey]
accessLogLocker.RUnlock()
if ok {
return tableName, nil
}
}
tableNames, err := db.TableNames()
if err != nil {
return tableName, err
}
if utils.ContainsStringInsensitive(tableNames, tableName) {
accessLogLocker.Lock()
nsAccessLogTableMapping[cacheKey] = true
accessLogLocker.Unlock()
return tableName, nil
}
// 创建表格
_, err = db.Exec("CREATE TABLE `" + tableName + "` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordId` int(11) unsigned DEFAULT '0' COMMENT '记录ID',\n `content` json DEFAULT NULL COMMENT '访问数据',\n `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `remoteAddr` varchar(128) DEFAULT NULL COMMENT 'IP',\n PRIMARY KEY (`id`),\n KEY `nodeId` (`nodeId`),\n KEY `domainId` (`domainId`),\n KEY `recordId` (`recordId`),\n KEY `requestId` (`requestId`),\n KEY `remoteAddr` (`remoteAddr`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='域名服务访问日志';")
if err != nil {
return tableName, err
}
accessLogLocker.Lock()
nsAccessLogTableMapping[cacheKey] = true
accessLogLocker.Unlock()
return tableName, nil
return daoList[rands.Int(0, l-1)]
}
// DBNodeInitializer 初始化数据库连接
@@ -209,14 +121,14 @@ func (this *DBNodeInitializer) loop() error {
return err
}
nodeIds := []int64{}
var nodeIds = []int64{}
for _, node := range dbNodes {
nodeIds = append(nodeIds, int64(node.Id))
}
// 关掉老的
accessLogLocker.Lock()
closingDbs := []*dbs.DB{}
var closingDbs = []*dbs.DB{}
for nodeId, db := range accessLogDBMapping {
if !lists.ContainsInt64(nodeIds, nodeId) {
closingDbs = append(closingDbs, db)
@@ -233,12 +145,12 @@ func (this *DBNodeInitializer) loop() error {
// 启动新的
for _, node := range dbNodes {
nodeId := int64(node.Id)
var nodeId = int64(node.Id)
accessLogLocker.Lock()
db, ok := accessLogDBMapping[nodeId]
accessLogLocker.Unlock()
dsn := node.Username + ":" + node.Password + "@tcp(" + node.Host + ":" + fmt.Sprintf("%d", node.Port) + ")/" + node.Database + "?charset=utf8mb4&timeout=10s"
var dsn = node.Username + ":" + node.Password + "@tcp(" + node.Host + ":" + fmt.Sprintf("%d", node.Port) + ")/" + node.Database + "?charset=utf8mb4&timeout=10s"
if ok {
// 检查配置是否有变化
@@ -308,49 +220,8 @@ func (this *DBNodeInitializer) loop() error {
accessLogLocker.Unlock()
}
// nsAccessLog
{
tableName, err := findNSAccessLogTable(db, timeutil.Format("Ymd"), false)
if err != nil {
if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
if createLogErr != nil {
remotelogs.Error("NODE_LOG", createLogErr.Error())
}
continue
} else {
err = nil
}
}
daoObject := dbs.DAOObject{
Instance: db,
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
Table: tableName,
PkName: "id",
Model: new(NSAccessLog),
}
err = daoObject.Init()
if err != nil {
remotelogs.Error("DB_NODE", "initialize dao failed: "+err.Error())
continue
}
accessLogLocker.Lock()
accessLogDBMapping[nodeId] = db
dao := &NSAccessLogDAO{
DAOObject: daoObject,
}
nsAccessLogDAOMapping[nodeId] = &NSAccessLogDAOWrapper{
DAO: dao,
NodeId: nodeId,
}
accessLogLocker.Unlock()
}
// 扩展
initAccessLogDAO(db, node)
}
}

View File

@@ -0,0 +1,11 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build !plus
package models
import "github.com/iwind/TeaGo/dbs"
var nsAccessLogDAOMapping = map[int64]any{} // dbNodeId => DAO
func initAccessLogDAO(db *dbs.DB, node *DBNode) {
}

View File

@@ -133,11 +133,28 @@ func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(tx *dbs.Tx, provid
return
}
// ListDomains 列出单页域名
func (this *DNSDomainDAO) ListDomains(tx *dbs.Tx, providerId int64, isDeleted bool, isUp bool, offset int64, size int64) (result []*DNSDomain, err error) {
_, err = this.Query(tx).
State(DNSDomainStateEnabled).
Attr("providerId", providerId).
Attr("isDeleted", isDeleted).
Attr("isUp", isUp).
AscPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// CountAllEnabledDomainsWithProviderId 计算某个服务商下的域名数量
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (int64, error) {
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64, isDeleted bool, isUp bool) (int64, error) {
return this.Query(tx).
State(DNSDomainStateEnabled).
Attr("providerId", providerId).
Attr("isDeleted", isDeleted).
Attr("isUp", isUp).
Count()
}

View File

@@ -56,11 +56,13 @@ func (this *DNSTaskDAO) CreateDNSTask(tx *dbs.Tx, clusterId int64, serverId int6
"isDone": false,
"isOk": false,
"error": "",
"version": time.Now().UnixNano(),
}, maps.Map{
"updatedAt": time.Now().Unix(),
"isDone": false,
"isOk": false,
"error": "",
"version": time.Now().UnixNano(),
})
return err
}
@@ -94,6 +96,7 @@ func (this *DNSTaskDAO) CreateDomainTask(tx *dbs.Tx, domainId int64, taskType DN
func (this *DNSTaskDAO) FindAllDoingTasks(tx *dbs.Tx) (result []*DNSTask, err error) {
_, err = this.Query(tx).
Attr("isDone", 0).
Asc("version").
AscPk().
Slice(&result).
FindAll()
@@ -109,6 +112,7 @@ func (this *DNSTaskDAO) FindAllDoingOrErrorTasks(tx *dbs.Tx, nodeClusterId int64
_, err = query.
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
Asc("updatedAt").
Asc("version").
AscPk().
Slice(&result).
FindAll()
@@ -163,3 +167,13 @@ func (this *DNSTaskDAO) UpdateDNSTaskDone(tx *dbs.Tx, taskId int64) error {
op.Error = ""
return this.Save(tx, op)
}
// DeleteDNSTasksWithClusterId 删除集群相关任务
func (this *DNSTaskDAO) DeleteDNSTasksWithClusterId(tx *dbs.Tx, clusterId int64) error {
if clusterId <= 0 {
return nil
}
return this.Query(tx).
Attr("clusterId", clusterId).
DeleteQuickly()
}

View File

@@ -9,7 +9,7 @@ import (
func TestDNSTaskDAO_CreateDNSTask(t *testing.T) {
dbs.NotifyReady()
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "taskType")
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "cdn", "taskType")
if err != nil {
t.Fatal(err)
}

View File

@@ -13,20 +13,22 @@ type DNSTask struct {
IsDone bool `field:"isDone"` // 是否已完成
IsOk bool `field:"isOk"` // 是否成功
Error string `field:"error"` // 错误信息
Version uint64 `field:"version"` // 版本
}
type DNSTaskOperator struct {
Id interface{} // ID
ClusterId interface{} // 集群ID
ServerId interface{} // 服务ID
NodeId interface{} // 节点ID
DomainId interface{} // 域名ID
RecordName interface{} // 记录名
Type interface{} // 任务类型
UpdatedAt interface{} // 更新时间
IsDone interface{} // 是否已完成
IsOk interface{} // 是否成功
Error interface{} // 错误信息
Id any // ID
ClusterId any // 集群ID
ServerId any // 服务ID
NodeId any // 节点ID
DomainId any // 域名ID
RecordName any // 记录名
Type any // 任务类型
UpdatedAt any // 更新时间
IsDone any // 是否已完成
IsOk any // 是否成功
Error any // 错误信息
Version any // 版本
}
func NewDNSTaskOperator() *DNSTaskOperator {

View File

@@ -15,9 +15,12 @@ import (
// CheckClusterDNS 检查集群的DNS问题
// 藏这么深是避免package循环引用的问题
func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSIssue, err error) {
clusterId := int64(cluster.Id)
domainId := int64(cluster.DnsDomainId)
func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster, checkNodeIssues bool) (issues []*pb.DNSIssue, err error) {
var clusterId = int64(cluster.Id)
var domainId = int64(cluster.DnsDomainId)
// 集群DNS设置
var clusterDNSConfig, _ = cluster.DecodeDNSConfig()
// 检查域名
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, domainId, nil)
@@ -101,47 +104,27 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
// TODO 检查域名是否已解析
// 检查节点
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true)
if err != nil {
return nil, err
}
// TODO 检查节点数量不能为0
for _, node := range nodes {
nodeId := int64(node.Id)
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
if checkNodeIssues {
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true, clusterDNSConfig != nil && clusterDNSConfig.IncludingLnNodes)
if err != nil {
return nil, err
}
if len(routeCodes) == 0 && !hasDefaultRoute {
issues = append(issues, &pb.DNSIssue{
Target: node.Name,
TargetId: nodeId,
Type: "node",
Description: "没有选择节点所属线路",
Params: map[string]string{
"clusterName": cluster.Name,
"clusterId": numberutils.FormatInt64(clusterId),
},
MustFix: true,
})
continue
}
// 检查线路是否在已有线路中
for _, routeCode := range routeCodes {
routeOk, err := domain.ContainsRouteCode(routeCode)
// TODO 检查节点数量不能为0
for _, node := range nodes {
nodeId := int64(node.Id)
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
if err != nil {
return nil, err
}
if !routeOk {
if len(routeCodes) == 0 && !hasDefaultRoute {
issues = append(issues, &pb.DNSIssue{
Target: node.Name,
TargetId: nodeId,
Type: "node",
Description: "线路已经失效,请重新选择",
Description: "没有选择节点所属线路",
Params: map[string]string{
"clusterName": cluster.Name,
"clusterId": numberutils.FormatInt64(clusterId),
@@ -150,29 +133,51 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
})
continue
}
}
// 检查IP地址
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, true, nodeconfigs.NodeRoleNode)
if err != nil {
return nil, err
}
if len(ipAddr) == 0 {
issues = append(issues, &pb.DNSIssue{
Target: node.Name,
TargetId: nodeId,
Type: "node",
Description: "没有设置IP地址",
Params: map[string]string{
"clusterName": cluster.Name,
"clusterId": numberutils.FormatInt64(clusterId),
},
MustFix: true,
})
continue
}
// 检查线路是否在已有线路中
for _, routeCode := range routeCodes {
routeOk, err := domain.ContainsRouteCode(routeCode)
if err != nil {
return nil, err
}
if !routeOk {
issues = append(issues, &pb.DNSIssue{
Target: node.Name,
TargetId: nodeId,
Type: "node",
Description: "线路已经失效,请重新选择",
Params: map[string]string{
"clusterName": cluster.Name,
"clusterId": numberutils.FormatInt64(clusterId),
},
MustFix: true,
})
continue
}
}
// TODO 检查是否有解析记录
// 检查IP地址
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, true, nodeconfigs.NodeRoleNode)
if err != nil {
return nil, err
}
if len(ipAddr) == 0 {
issues = append(issues, &pb.DNSIssue{
Target: node.Name,
TargetId: nodeId,
Type: "node",
Description: "没有设置IP地址",
Params: map[string]string{
"clusterName": cluster.Name,
"clusterId": numberutils.FormatInt64(clusterId),
},
MustFix: true,
})
continue
}
// TODO 检查是否有解析记录
}
}
return

View File

@@ -21,7 +21,7 @@ func TestNodeClusterDAO_CheckClusterDNS(t *testing.T) {
t.Log("cluster not found, skip the test")
return
}
issues, err := CheckClusterDNS(tx, cluster)
issues, err := CheckClusterDNS(tx, cluster, true)
if err != nil {
t.Fatal(err)
}

View File

@@ -21,6 +21,7 @@ import (
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"golang.org/x/net/idna"
"net"
"net/http"
"net/url"
@@ -39,9 +40,10 @@ var SharedHTTPAccessLogDAO *HTTPAccessLogDAO
var (
oldAccessLogQueue = make(chan *pb.HTTPAccessLog)
accessLogQueue = make(chan *pb.HTTPAccessLog, 10_000)
accessLogQueueMaxLength = 100_000
accessLogQueuePercent = 100 // 0-100
accessLogCountPerSecond = 10_000 // 0 表示不限制
accessLogQueueMaxLength = 100_000 // 队列最大长度
accessLogQueuePercent = 100 // 0-100
accessLogCountPerSecond = 10_000 // 每秒钟写入条数,0 表示不限制
accessLogPerTx = 100 // 单事务写入条数
accessLogConfigJSON = []byte{}
accessLogQueueChanged = make(chan zero.Zero, 1)
@@ -84,11 +86,29 @@ func init() {
// 导出队列内容
goman.New(func() {
var ticker = time.NewTicker(1 * time.Second)
var accessLogPerLoop = accessLogPerTx
for range ticker.C {
var tx *dbs.Tx
err := SharedHTTPAccessLogDAO.DumpAccessLogsFromQueue(tx, accessLogCountPerSecond)
if err != nil {
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "dump access logs failed: "+err.Error())
var countTxs = accessLogCountPerSecond / accessLogPerLoop
if countTxs <= 0 {
countTxs = 1
}
for i := 0; i < countTxs; i++ {
var before = time.Now()
hasMore, err := SharedHTTPAccessLogDAO.DumpAccessLogsFromQueue(accessLogPerLoop)
// 如果用时过长,则调整每次写入次数
var costMs = time.Since(before).Milliseconds()
if costMs > 150 {
accessLogPerLoop = accessLogPerTx / 4
} else if costMs > 100 {
accessLogPerLoop = accessLogPerTx / 2
} // 这里不需要恢复成默认值,因为可能是写入数量比较小
if err != nil {
remotelogs.Error("HTTP_ACCESS_LOG_QUEUE", "dump access logs failed: "+err.Error())
} else if !hasMore {
break
}
}
}
})
@@ -132,7 +152,11 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.
}
// DumpAccessLogsFromQueue 从队列导入访问日志
func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(tx *dbs.Tx, size int) error {
func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(size int) (hasMore bool, err error) {
if size <= 0 {
size = 100
}
var dao = randomHTTPAccessLogDAO()
if dao == nil {
dao = &HTTPAccessLogDAOWrapper{
@@ -141,14 +165,25 @@ func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(tx *dbs.Tx, size int) erro
}
}
if size <= 0 {
size = 1_000_000
if len(oldAccessLogQueue) == 0 && len(accessLogQueue) == 0 {
return false, nil
}
// 开始事务
tx, err := dao.DAO.Instance.Begin()
if err != nil {
return false, err
}
defer func() {
_ = tx.Commit()
}()
// 复制变量,防止中途改变
var oldQueue = oldAccessLogQueue
var newQueue = accessLogQueue
hasMore = true
Loop:
for i := 0; i < size; i++ {
// old
@@ -156,7 +191,7 @@ Loop:
case accessLog := <-oldQueue:
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
if err != nil {
return err
return false, err
}
continue Loop
default:
@@ -168,20 +203,28 @@ Loop:
case accessLog := <-newQueue:
err := this.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
if err != nil {
return err
return false, err
}
continue Loop
default:
hasMore = false
break Loop
}
}
return nil
return hasMore, nil
}
// CreateHTTPAccessLog 写入单条访问日志
func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLogDAO, accessLog *pb.HTTPAccessLog) error {
var day = timeutil.FormatTime("Ymd", accessLog.Timestamp)
var day = ""
// 注意:如果你修改了 TimeISO8601 的逻辑,这里也需要同步修改
if len(accessLog.TimeISO8601) > 10 {
day = strings.ReplaceAll(accessLog.TimeISO8601[:10], "-", "")
} else {
timeutil.FormatTime("Ymd", accessLog.Timestamp)
}
tableDef, err := SharedHTTPAccessLogManager.FindLastTable(dao.Instance, day, true)
if err != nil {
return err
@@ -412,6 +455,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
var dao = tableQuery.daoWrapper.DAO
var query = dao.Query(tx)
query.Result("id", "serverId", "nodeId", "status", "createdAt", "content", "requestId", "firewallPolicyId", "firewallRuleGroupId", "firewallRuleSetId", "firewallRuleId", "remoteAddr", "domain")
// 条件
if nodeId > 0 {
@@ -485,6 +529,14 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
query.Where("domain LIKE :host2").
Param("host2", domain)
} else {
// 中文域名
if !regexp.MustCompile(`^[a-zA-Z0-9-.]+$`).MatchString(domain) {
unicodeDomain, err := idna.ToASCII(domain)
if err == nil && len(unicodeDomain) > 0 {
domain = unicodeDomain
}
}
query.Attr("domain", domain)
query.UseIndex("domain")
}
@@ -753,7 +805,7 @@ func (this *HTTPAccessLogDAO) SetupQueue() {
return
}
if bytes.Compare(accessLogConfigJSON, configJSON) == 0 {
if bytes.Equal(accessLogConfigJSON, configJSON) {
return
}
accessLogConfigJSON = configJSON
@@ -767,6 +819,9 @@ func (this *HTTPAccessLogDAO) SetupQueue() {
accessLogQueuePercent = config.Percent
accessLogCountPerSecond = config.CountPerSecond
if accessLogCountPerSecond <= 0 {
accessLogCountPerSecond = 10_000
}
if config.MaxLength <= 0 {
config.MaxLength = 100_000
}

View File

@@ -21,13 +21,13 @@ func TestCreateHTTPAccessLog(t *testing.T) {
t.Fatal(err)
}
accessLog := &pb.HTTPAccessLog{
var accessLog = &pb.HTTPAccessLog{
ServerId: 1,
NodeId: 4,
Status: 200,
Timestamp: time.Now().Unix(),
}
dao := randomHTTPAccessLogDAO()
var dao = randomHTTPAccessLogDAO()
t.Log("dao:", dao)
// 先初始化
@@ -37,12 +37,59 @@ func TestCreateHTTPAccessLog(t *testing.T) {
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
for i := 0; i < 1000; i++ {
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
if err != nil {
t.Fatal(err)
}
}
t.Log("ok")
}
func TestCreateHTTPAccessLog_Tx(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
var accessLog = &pb.HTTPAccessLog{
ServerId: 1,
NodeId: 4,
Status: 200,
Timestamp: time.Now().Unix(),
}
var dao = randomHTTPAccessLogDAO()
t.Log("dao:", dao)
// 先初始化
_ = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
var before = time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
tx, err = dao.DAO.Instance.Begin()
if err != nil {
t.Fatal(err)
}
for i := 0; i < 200; i++ {
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
if err != nil {
t.Fatal(err)
}
}
err = tx.Commit()
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -422,7 +422,7 @@ func (this *HTTPAccessLogManager) checkTableFields(db *dbs.DB, tableName string)
}
for _, field := range fields {
var fieldName = field.GetString("Field")
if strings.ToLower(fieldName) == strings.ToLower("remoteAddr") {
if strings.EqualFold(fieldName, "remoteAddr") {
hasRemoteAddrField = true
}
if strings.ToLower(fieldName) == "domain" {

View File

@@ -68,8 +68,9 @@ func (this *HTTPAuthPolicyDAO) FindEnabledHTTPAuthPolicy(tx *dbs.Tx, id int64) (
}
// CreateHTTPAuthPolicy 创建策略
func (this *HTTPAuthPolicyDAO) CreateHTTPAuthPolicy(tx *dbs.Tx, name string, methodType string, paramsJSON []byte) (int64, error) {
func (this *HTTPAuthPolicyDAO) CreateHTTPAuthPolicy(tx *dbs.Tx, userId int64, name string, methodType string, paramsJSON []byte) (int64, error) {
var op = NewHTTPAuthPolicyOperator()
op.UserId = userId
op.Name = name
op.Type = methodType
op.Params = paramsJSON
@@ -137,6 +138,20 @@ func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, c
return config, nil
}
// CheckUserPolicy 检查用户权限
func (this *HTTPAuthPolicyDAO) CheckUserPolicy(tx *dbs.Tx, userId int64, policyId int64) error {
if userId <= 0 || policyId <= 0 {
return ErrNotFound
}
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithHTTPAuthPolicyId(tx, policyId)
if err != nil {
return err
}
return SharedHTTPWebDAO.CheckUserWeb(tx, userId, webId)
}
// NotifyUpdate 通知更改
func (this *HTTPAuthPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithHTTPAuthPolicyId(tx, policyId)

View File

@@ -125,25 +125,13 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st
MinSize: &shared.SizeCapacity{Count: 0, Unit: shared.SizeCapacityUnitKB},
SkipResponseSetCookie: true,
AllowChunkedEncoding: true,
Conds: &shared.HTTPRequestCondsConfig{
IsOn: true,
Connector: "or",
Groups: []*shared.HTTPRequestCondGroup{
{
IsOn: true,
Connector: "or",
Conds: []*shared.HTTPRequestCond{
{
Type: "url-extension",
IsRequest: true,
Param: "${requestPathExtension}",
Operator: shared.RequestCondOperatorIn,
Value: `[".html", ".js", ".css", ".gif", ".png", ".bmp", ".jpeg", ".jpg", ".webp", ".ico", ".pdf", ".ttf", ".eot", ".tiff", ".svg", ".svgz", ".eps", ".woff", ".otf", ".woff2", ".tif", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".wav", ".mp3", ".mp4", ".ogg", ".mid", ".midi"]`,
},
},
Description: "初始化规则",
},
},
AllowPartialContent: true,
SimpleCond: &shared.HTTPRequestCond{
Type: "url-extension",
IsRequest: true,
Param: "${requestPathExtension}",
Operator: shared.RequestCondOperatorIn,
Value: `[".html", ".js", ".css", ".gif", ".png", ".bmp", ".jpeg", ".jpg", ".webp", ".ico", ".pdf", ".ttf", ".eot", ".tiff", ".svg", ".svgz", ".eps", ".woff", ".otf", ".woff2", ".tif", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".wav", ".mp3", ".mp4", ".ogg", ".mid", ".midi"]`,
},
}
refsJSON, err := json.Marshal([]*serverconfigs.HTTPCacheRef{cacheRef})

View File

@@ -381,7 +381,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
// 认证
if IsNotNull(web.Auth) {
authConfig := &serverconfigs.HTTPAuthConfig{}
var authConfig = &serverconfigs.HTTPAuthConfig{}
err = json.Unmarshal(web.Auth, authConfig)
if err != nil {
return nil, err
@@ -395,6 +395,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
if policyConfig != nil {
ref.AuthPolicy = policyConfig
newRefs = append(newRefs, ref)
authConfig.PolicyRefs = newRefs
}
}
config.Auth = authConfig
@@ -457,6 +458,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
config.UAM = uamConfig
}
// Referers
if IsNotNull(web.Referers) {
var referersConfig = &serverconfigs.ReferersConfig{}
err = json.Unmarshal(web.Referers, referersConfig)
if err != nil {
return nil, err
}
config.Referers = referersConfig
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
@@ -1042,6 +1053,10 @@ func (this *HTTPWebDAO) FindWebServerGroupId(tx *dbs.Tx, webId int64) (groupId i
// CheckUserWeb 检查用户权限
func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) error {
if userId <= 0 || webId <= 0 {
return ErrNotFound
}
serverId, err := this.FindWebServerId(tx, webId)
if err != nil {
return err
@@ -1208,6 +1223,35 @@ func (this *HTTPWebDAO) FindWebUAM(tx *dbs.Tx, webId int64) ([]byte, error) {
FindJSONCol()
}
// UpdateWebReferers 修改防盗链设置
func (this *HTTPWebDAO) UpdateWebReferers(tx *dbs.Tx, webId int64, referersConfig *serverconfigs.ReferersConfig) error {
if referersConfig == nil {
return nil
}
configJSON, err := json.Marshal(referersConfig)
if err != nil {
return err
}
err = this.Query(tx).
Pk(webId).
Set("referers", configJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, webId)
}
// FindWebReferers 查找服务的防盗链配置
func (this *HTTPWebDAO) FindWebReferers(tx *dbs.Tx, webId int64) ([]byte, error) {
return this.Query(tx).
Pk(webId).
Result("referers").
FindJSONCol()
}
// NotifyUpdate 通知更新
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
// server

View File

@@ -38,43 +38,45 @@ type HTTPWeb struct {
RequestLimit dbs.JSON `field:"requestLimit"` // 请求限制
RequestScripts dbs.JSON `field:"requestScripts"` // 请求脚本
Uam dbs.JSON `field:"uam"` // UAM设置
Referers dbs.JSON `field:"referers"` // 防盗链设置
}
type HTTPWebOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
TemplateId interface{} // 模版ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
State interface{} // 状态
CreatedAt interface{} // 创建时间
Root interface{} // 根目录
Charset interface{} // 字符集
Shutdown interface{} // 临时关闭页面配置
Pages interface{} // 特殊页面
RedirectToHttps interface{} // 跳转到HTTPS设置
Indexes interface{} // 首页文件列表
MaxRequestBodySize interface{} // 最大允许的请求内容尺寸
RequestHeader interface{} // 请求Header配置
ResponseHeader interface{} // 响应Header配置
AccessLog interface{} // 访问日志配置
Stat interface{} // 统计配置
Gzip interface{} // Gzip配置v0.3.2弃用)
Compression interface{} // 压缩配置
Cache interface{} // 缓存配置
Firewall interface{} // 防火墙设置
Locations interface{} // 路由规则配置
Websocket interface{} // Websocket设置
RewriteRules interface{} // 重写规则配置
HostRedirects interface{} // 域名跳转
Fastcgi interface{} // Fastcgi配置
Auth interface{} // 认证策略配置
Webp interface{} // WebP配置
RemoteAddr interface{} // 客户端IP配置
MergeSlashes interface{} // 是否合并路径中的斜杠
RequestLimit interface{} // 请求限制
RequestScripts interface{} // 请求脚本
Uam interface{} // UAM设置
Id any // ID
IsOn any // 是否启用
TemplateId any // 模版ID
AdminId any // 管理员ID
UserId any // 用户ID
State any // 状态
CreatedAt any // 创建时间
Root any // 根目录
Charset any // 字符集
Shutdown any // 临时关闭页面配置
Pages any // 特殊页面
RedirectToHttps any // 跳转到HTTPS设置
Indexes any // 首页文件列表
MaxRequestBodySize any // 最大允许的请求内容尺寸
RequestHeader any // 请求Header配置
ResponseHeader any // 响应Header配置
AccessLog any // 访问日志配置
Stat any // 统计配置
Gzip any // Gzip配置v0.3.2弃用)
Compression any // 压缩配置
Cache any // 缓存配置
Firewall any // 防火墙设置
Locations any // 路由规则配置
Websocket any // Websocket设置
RewriteRules any // 重写规则配置
HostRedirects any // 域名跳转
Fastcgi any // Fastcgi配置
Auth any // 认证策略配置
Webp any // WebP配置
RemoteAddr any // 客户端IP配置
MergeSlashes any // 是否合并路径中的斜杠
RequestLimit any // 请求限制
RequestScripts any // 请求脚本
Uam any // UAM设置
Referers any // 防盗链设置
}
func NewHTTPWebOperator() *HTTPWebOperator {

View File

@@ -0,0 +1,140 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
stringutil "github.com/iwind/TeaGo/utils/string"
)
const (
IPLibraryArtifactStateEnabled = 1 // 已启用
IPLibraryArtifactStateDisabled = 0 // 已禁用
)
type IPLibraryArtifactDAO dbs.DAO
func NewIPLibraryArtifactDAO() *IPLibraryArtifactDAO {
return dbs.NewDAO(&IPLibraryArtifactDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeIPLibraryArtifacts",
Model: new(IPLibraryArtifact),
PkName: "id",
},
}).(*IPLibraryArtifactDAO)
}
var SharedIPLibraryArtifactDAO *IPLibraryArtifactDAO
func init() {
dbs.OnReady(func() {
SharedIPLibraryArtifactDAO = NewIPLibraryArtifactDAO()
})
}
// EnableIPLibraryArtifact 启用条目
func (this *IPLibraryArtifactDAO) EnableIPLibraryArtifact(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", IPLibraryArtifactStateEnabled).
Update()
return err
}
// DisableIPLibraryArtifact 禁用条目
func (this *IPLibraryArtifactDAO) DisableIPLibraryArtifact(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", IPLibraryArtifactStateDisabled).
Update()
return err
}
// FindEnabledIPLibraryArtifact 查找启用中的条目
func (this *IPLibraryArtifactDAO) FindEnabledIPLibraryArtifact(tx *dbs.Tx, id int64) (*IPLibraryArtifact, error) {
result, err := this.Query(tx).
Pk(id).
State(IPLibraryArtifactStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*IPLibraryArtifact), err
}
// CreateArtifact 创建制品
func (this *IPLibraryArtifactDAO) CreateArtifact(tx *dbs.Tx, name string, fileId int64, libraryFileId int64, meta *iplibrary.Meta) (int64, error) {
var op = NewIPLibraryArtifactOperator()
op.Name = name
op.FileId = fileId
op.LibraryFileId = libraryFileId
metaJSON, err := json.Marshal(meta)
if err != nil {
return 0, err
}
op.Meta = metaJSON
op.State = IPLibraryArtifactStateEnabled
var code = stringutil.Md5(utils.Sha1RandomString())[:8]
meta.Code = code
op.Code = code // 要比较短,方便识别
return this.SaveInt64(tx, op)
}
// FindAllArtifacts 查找制品列表
func (this *IPLibraryArtifactDAO) FindAllArtifacts(tx *dbs.Tx) (result []*IPLibraryArtifact, err error) {
_, err = this.Query(tx).
State(IPLibraryArtifactStateEnabled).
DescPk().
Slice(&result).
FindAll()
return
}
// FindPublicArtifact 查找当前使用的制品
func (this *IPLibraryArtifactDAO) FindPublicArtifact(tx *dbs.Tx) (*IPLibraryArtifact, error) {
one, err := this.Query(tx).
State(IPLibraryArtifactStateEnabled).
Attr("isPublic", true).
Result("id", "fileId", "code").
Find()
if err != nil || one == nil {
return nil, err
}
return one.(*IPLibraryArtifact), nil
}
// UpdateArtifactPublic 使用某个制品
func (this *IPLibraryArtifactDAO) UpdateArtifactPublic(tx *dbs.Tx, artifactId int64, isPublic bool) error {
// 取消使用
if !isPublic {
return this.Query(tx).
Pk(artifactId).
Set("isPublic", false).
UpdateQuickly()
}
// 使用
// 先取消别的
err := this.Query(tx).
Neq("id", artifactId).
State(IPLibraryArtifactStateEnabled).
Attr("isPublic", true).
Set("isPublic", false).
UpdateQuickly()
if err != nil {
return err
}
return this.Query(tx).
Pk(artifactId).
Set("isPublic", true).
UpdateQuickly()
}

View File

@@ -1,4 +1,4 @@
package models
package models_test
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -0,0 +1,32 @@
package models
import "github.com/iwind/TeaGo/dbs"
// IPLibraryArtifact IP库制品
type IPLibraryArtifact struct {
Id uint32 `field:"id"` // ID
Name string `field:"name"` // 名称
FileId uint64 `field:"fileId"` // 文件ID
LibraryFileId uint32 `field:"libraryFileId"` // IP库文件ID
CreatedAt uint64 `field:"createdAt"` // 创建时间
Meta dbs.JSON `field:"meta"` // 元数据
IsPublic bool `field:"isPublic"` // 是否为公用
Code string `field:"code"` // 代号
State uint8 `field:"state"` // 状态
}
type IPLibraryArtifactOperator struct {
Id any // ID
Name any // 名称
FileId any // 文件ID
LibraryFileId any // IP库文件ID
CreatedAt any // 创建时间
Meta any // 元数据
IsPublic any // 是否为公用
Code any // 代号
State any // 状态
}
func NewIPLibraryArtifactOperator() *IPLibraryArtifactOperator {
return &IPLibraryArtifactOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -72,8 +72,9 @@ func (this *IPLibraryFileDAO) FindEnabledIPLibraryFile(tx *dbs.Tx, id int64) (*I
}
// CreateLibraryFile 创建文件
func (this *IPLibraryFileDAO) CreateLibraryFile(tx *dbs.Tx, template string, emptyValues []string, fileId int64, countries []string, provinces [][2]string, cities [][3]string, towns [][4]string, providers []string) (int64, error) {
func (this *IPLibraryFileDAO) CreateLibraryFile(tx *dbs.Tx, name string, template string, emptyValues []string, fileId int64, countries []string, provinces [][2]string, cities [][3]string, towns [][4]string, providers []string) (int64, error) {
var op = NewIPLibraryFileOperator()
op.Name = name
op.Template = template
if emptyValues == nil {
@@ -137,6 +138,18 @@ func (this *IPLibraryFileDAO) CreateLibraryFile(tx *dbs.Tx, template string, emp
return this.SaveInt64(tx, op)
}
// FindAllFinishedLibraryFiles 查找所有已完成的文件
func (this *IPLibraryFileDAO) FindAllFinishedLibraryFiles(tx *dbs.Tx) (result []*IPLibraryFile, err error) {
_, err = this.Query(tx).
State(IPLibraryFileStateEnabled).
Result("id", "fileId", "createdAt", "generatedFileId", "generatedAt", "name"). // 这里不需要其他信息
Attr("isFinished", true).
DescPk().
Slice(&result).
FindAll()
return
}
// FindAllUnfinishedLibraryFiles 查找所有未完成的文件
func (this *IPLibraryFileDAO) FindAllUnfinishedLibraryFiles(tx *dbs.Tx) (result []*IPLibraryFile, err error) {
_, err = this.Query(tx).
@@ -324,7 +337,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
var countries = []*iplibrary.Country{}
for _, country := range dbCountries {
countries = append(countries, &iplibrary.Country{
Id: int64(country.Id),
Id: country.Id,
Name: country.DisplayName(),
Codes: country.AllCodes(),
})
@@ -339,7 +352,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
var provinces = []*iplibrary.Province{}
for _, province := range dbProvinces {
provinces = append(provinces, &iplibrary.Province{
Id: int64(province.Id),
Id: province.Id,
Name: province.DisplayName(),
Codes: province.AllCodes(),
})
@@ -354,7 +367,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
var cities = []*iplibrary.City{}
for _, city := range dbCities {
cities = append(cities, &iplibrary.City{
Id: int64(city.Id),
Id: city.Id,
Name: city.DisplayName(),
Codes: city.AllCodes(),
})
@@ -369,7 +382,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
var towns = []*iplibrary.Town{}
for _, town := range dbTowns {
towns = append(towns, &iplibrary.Town{
Id: int64(town.Id),
Id: town.Id,
Name: town.DisplayName(),
Codes: town.AllCodes(),
})
@@ -384,7 +397,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
var providers = []*iplibrary.Provider{}
for _, provider := range dbProviders {
providers = append(providers, &iplibrary.Provider{
Id: int64(provider.Id),
Id: provider.Id,
Name: provider.DisplayName(),
Codes: provider.AllCodes(),
})
@@ -392,7 +405,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
var libraryCode = utils.Sha1RandomString() // 每次都生成新的code
var filePath = dir + "/" + this.composeFilename(libraryFileId, libraryCode)
writer, err := iplibrary.NewFileWriter(filePath, &iplibrary.Meta{
var meta = &iplibrary.Meta{
Author: "", // 将来用户可以自行填写
CreatedAt: time.Now().Unix(),
Countries: countries,
@@ -400,13 +413,15 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
Cities: cities,
Towns: towns,
Providers: providers,
})
}
writer, err := iplibrary.NewFileWriter(filePath, meta)
if err != nil {
return err
}
defer func() {
_ = writer.Close()
_ = os.Remove(filePath)
}()
err = writer.WriteMeta()
@@ -563,6 +578,12 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
return err
}
// 添加制品
_, err = SharedIPLibraryArtifactDAO.CreateArtifact(tx, libraryFile.Name, generatedFileId, libraryFileId, meta)
if err != nil {
return err
}
return nil
}

View File

@@ -12,7 +12,7 @@ func TestIPLibraryFileDAO_GenerateIPLibrary(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 3)
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 4)
if err != nil {
t.Fatal(err)
}

View File

@@ -5,6 +5,7 @@ import "github.com/iwind/TeaGo/dbs"
// IPLibraryFile IP库上传的文件
type IPLibraryFile struct {
Id uint64 `field:"id"` // ID
Name string `field:"name"` // IP库名称
FileId uint64 `field:"fileId"` // 原始文件ID
Template string `field:"template"` // 模板
EmptyValues dbs.JSON `field:"emptyValues"` // 空值列表
@@ -23,6 +24,7 @@ type IPLibraryFile struct {
type IPLibraryFileOperator struct {
Id any // ID
Name any // IP库名称
FileId any // 原始文件ID
Template any // 模板
EmptyValues any // 空值列表

View File

@@ -3,6 +3,7 @@ package models
import (
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -38,7 +39,7 @@ func init() {
func (this *LogDAO) CreateLog(tx *dbs.Tx, adminType string, adminId int64, level string, description string, action string, ip string) error {
var op = NewLogOperator()
op.Level = level
op.Description = description
op.Description = utils.LimitString(description, 1000)
op.Action = action
op.Ip = ip
op.Type = adminType

View File

@@ -4,31 +4,35 @@ import "github.com/iwind/TeaGo/dbs"
// NSDomain DNS域名
type NSDomain struct {
Id uint64 `field:"id"` // ID
ClusterId uint32 `field:"clusterId"` // 集群ID
UserId uint32 `field:"userId"` // 用户ID
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 域名
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
Tsig dbs.JSON `field:"tsig"` // TSIG配置
CreatedAt uint64 `field:"createdAt"` // 创建时间
Version uint64 `field:"version"` // 版本号
Status string `field:"status"` // 状态none|verified
State uint8 `field:"state"` // 状态
Id uint64 `field:"id"` // ID
ClusterId uint32 `field:"clusterId"` // 集群ID
UserId uint32 `field:"userId"` // 用户ID
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 域名
GroupIds dbs.JSON `field:"groupIds"` // 分组ID
Tsig dbs.JSON `field:"tsig"` // TSIG配置
VerifyTXT string `field:"verifyTXT"` // 验证用的TXT
VerifyExpiresAt uint64 `field:"verifyExpiresAt"` // 验证TXT过期时间
CreatedAt uint64 `field:"createdAt"` // 创建时间
Version uint64 `field:"version"` // 版本号
Status string `field:"status"` // 状态none|verified
State uint8 `field:"state"` // 状态
}
type NSDomainOperator struct {
Id interface{} // ID
ClusterId interface{} // 集群ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
Name interface{} // 域名
GroupIds interface{} // 分组ID
Tsig interface{} // TSIG配置
CreatedAt interface{} // 创建时间
Version interface{} // 版本号
Status interface{} // 状态none|verified
State interface{} // 状态
Id any // ID
ClusterId any // 集群ID
UserId any // 用户ID
IsOn any // 是否启用
Name any // 域名
GroupIds any // 分组ID
Tsig any // TSIG配置
VerifyTXT any // 验证用的TXT
VerifyExpiresAt any // 验证TXT过期时间
CreatedAt any // 创建时间
Version any // 版本号
Status any // 状态none|verified
State any // 状态
}
func NewNSDomainOperator() *NSDomainOperator {

View File

@@ -0,0 +1,30 @@
package nameservers
import "github.com/iwind/TeaGo/dbs"
// NSPlan NS套餐
type NSPlan struct {
Id uint32 `field:"id"` // ID
Name string `field:"name"` // 套餐名称
IsOn bool `field:"isOn"` // 是否启用
MonthlyPrice float64 `field:"monthlyPrice"` // 月价格
YearlyPrice float64 `field:"yearlyPrice"` // 年价格
Order uint32 `field:"order"` // 排序
Config dbs.JSON `field:"config"` // 配置
State uint8 `field:"state"` // 状态
}
type NSPlanOperator struct {
Id any // ID
Name any // 套餐名称
IsOn any // 是否启用
MonthlyPrice any // 月价格
YearlyPrice any // 年价格
Order any // 排序
Config any // 配置
State any // 状态
}
func NewNSPlanOperator() *NSPlanOperator {
return &NSPlanOperator{}
}

View File

@@ -0,0 +1 @@
package nameservers

View File

@@ -5,11 +5,11 @@ type NSRecordHourlyStat struct {
Id uint64 `field:"id"` // ID
ClusterId uint32 `field:"clusterId"` // 集群ID
NodeId uint32 `field:"nodeId"` // 节点ID
DomainId uint32 `field:"domainId"` // 域名ID
DomainId uint64 `field:"domainId"` // 域名ID
RecordId uint64 `field:"recordId"` // 记录ID
Day string `field:"day"` // YYYYMMDD
Hour string `field:"hour"` // YYYYMMDDHH
CountRequests uint32 `field:"countRequests"` // 请求数
CountRequests uint64 `field:"countRequests"` // 请求数
Bytes uint64 `field:"bytes"` // 流量
}

View File

@@ -0,0 +1,28 @@
package nameservers
// NSUserPlan 用户套餐
type NSUserPlan struct {
Id uint64 `field:"id"` // ID
UserId uint64 `field:"userId"` // 用户ID
PlanId uint32 `field:"planId"` // 套餐ID
DayFrom string `field:"dayFrom"` // YYYYMMDD
DayTo string `field:"dayTo"` // YYYYMMDD
PeriodUnit string `field:"periodUnit"` // monthly|yearly
CreatedAt uint64 `field:"createdAt"` // 创建时间
State uint8 `field:"state"` // 状态
}
type NSUserPlanOperator struct {
Id any // ID
UserId any // 用户ID
PlanId any // 套餐ID
DayFrom any // YYYYMMDD
DayTo any // YYYYMMDD
PeriodUnit any // monthly|yearly
CreatedAt any // 创建时间
State any // 状态
}
func NewNSUserPlanOperator() *NSUserPlanOperator {
return &NSUserPlanOperator{}
}

View File

@@ -0,0 +1 @@
package nameservers

View File

@@ -125,13 +125,13 @@ func (this *NodeClusterDAO) FindAllEnableClusterIds(tx *dbs.Tx) (result []int64,
}
// CreateCluster 创建集群
func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string, grantId int64, installDir string, dnsDomainId int64, dnsName string, cachePolicyId int64, httpFirewallPolicyId int64, systemServices map[string]maps.Map) (clusterId int64, err error) {
func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string, grantId int64, installDir string, dnsDomainId int64, dnsName string, dnsTTL int32, cachePolicyId int64, httpFirewallPolicyId int64, systemServices map[string]maps.Map, globalServerConfig *serverconfigs.GlobalServerConfig, autoInstallNftables bool) (clusterId int64, err error) {
uniqueId, err := this.GenUniqueId(tx)
if err != nil {
return 0, err
}
secret := rands.String(32)
var secret = rands.String(32)
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleCluster)
if err != nil {
return 0, err
@@ -147,11 +147,12 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
op.DnsDomainId = dnsDomainId
op.DnsName = dnsName
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
NodesAutoSync: true,
ServersAutoSync: true,
CNameRecords: []string{},
CNameAsDomain: true,
TTL: 0,
NodesAutoSync: true,
ServersAutoSync: true,
CNAMERecords: []string{},
CNAMEAsDomain: true,
TTL: dnsTTL,
IncludingLnNodes: true,
}
dnsJSON, err := json.Marshal(dnsConfig)
if err != nil {
@@ -172,10 +173,21 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
}
op.SystemServices = systemServicesJSON
// 全局服务配置
if globalServerConfig == nil {
globalServerConfig = serverconfigs.DefaultGlobalServerConfig()
}
globalServerConfigJSON, err := json.Marshal(globalServerConfig)
if err != nil {
return 0, err
}
op.GlobalServerConfig = globalServerConfigJSON
op.UseAllAPINodes = 1
op.ApiNodes = "[]"
op.UniqueId = uniqueId
op.Secret = secret
op.AutoInstallNftables = autoInstallNftables
op.State = NodeClusterStateEnabled
err = this.Save(tx, op)
if err != nil {
@@ -186,7 +198,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
}
// UpdateCluster 修改集群
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool) error {
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool, clockConfig *nodeconfigs.ClockConfig, autoRemoteStart bool, autoInstallTables bool) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
}
@@ -203,6 +215,17 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
op.NodeMaxThreads = nodeMaxThreads
op.AutoOpenPorts = autoOpenPorts
if clockConfig != nil {
clockJSON, err := json.Marshal(clockConfig)
if err != nil {
return err
}
op.Clock = clockJSON
}
op.AutoRemoteStart = autoRemoteStart
op.AutoInstallNftables = autoInstallTables
err := this.Save(tx, op)
if err != nil {
return err
@@ -443,7 +466,7 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cach
one, err := this.Query(tx).
Pk(clusterId).
Result("id", "name", "dnsName", "dnsDomainId", "dns", "isOn").
Result("id", "name", "dnsName", "dnsDomainId", "dns", "isOn", "state").
Find()
if err != nil {
return nil, err
@@ -468,7 +491,7 @@ func (this *NodeClusterDAO) ExistClusterDNSName(tx *dbs.Tx, dnsName string, excl
}
// UpdateClusterDNS 修改集群DNS相关信息
func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool, cnameRecords []string, ttl int32, cnameAsDomain bool) error {
func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool, cnameRecords []string, ttl int32, cnameAsDomain bool, includingLnNodes bool) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
}
@@ -487,10 +510,16 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
var oldCluster = oldOne.(*NodeCluster)
var oldDNSDomainId = int64(oldCluster.DnsDomainId)
var shouldRemoveOld = false
if (oldDNSDomainId > 0 && oldDNSDomainId != dnsDomainId) || (oldCluster.DnsName != dnsName) {
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
if err != nil {
return err
if oldDNSDomainId == dnsDomainId {
// 如果只是换子域名需要在新的域名添加之前先删除老的子域名防止无法添加CNAME
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
if err != nil {
return err
}
} else {
shouldRemoveOld = true
}
}
@@ -504,11 +533,12 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
}
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
NodesAutoSync: nodesAutoSync,
ServersAutoSync: serversAutoSync,
CNameRecords: cnameRecords,
TTL: ttl,
CNameAsDomain: cnameAsDomain,
NodesAutoSync: nodesAutoSync,
ServersAutoSync: serversAutoSync,
CNAMERecords: cnameRecords,
TTL: ttl,
CNAMEAsDomain: cnameAsDomain,
IncludingLnNodes: includingLnNodes,
}
dnsJSON, err := json.Marshal(dnsConfig)
if err != nil {
@@ -524,7 +554,20 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
if err != nil {
return err
}
return this.NotifyDNSUpdate(tx, clusterId)
err = this.NotifyDNSUpdate(tx, clusterId)
if err != nil {
return err
}
// 删除老的记录
if shouldRemoveOld {
err = dns.SharedDNSTaskDAO.CreateClusterRemoveTask(tx, clusterId, oldDNSDomainId, oldCluster.DnsName)
if err != nil {
return err
}
}
return nil
}
// FindClusterAdminId 查找集群所属管理员
@@ -924,7 +967,7 @@ func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, ca
cluster, err := this.Query(tx).
Pk(clusterId).
State(NodeClusterStateEnabled).
Result("id", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection").
Result("id", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection", "clock", "globalServerConfig", "autoInstallNftables").
Find()
if err != nil || cluster == nil {
return nil, err
@@ -1066,7 +1109,7 @@ func (this *NodeClusterDAO) FindClusterDDoSProtection(tx *dbs.Tx, clusterId int6
return one.(*NodeCluster).DecodeDDoSProtection(), nil
}
// UpdateClusterDDoSProtection 设置集群的DDOS设置
// UpdateClusterDDoSProtection 设置集群的DDoS设置
func (this *NodeClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
if clusterId <= 0 {
return ErrNotFound
@@ -1092,6 +1135,49 @@ func (this *NodeClusterDAO) UpdateClusterDDoSProtection(tx *dbs.Tx, clusterId in
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeDDosProtectionChanged)
}
// FindClusterGlobalServerConfig 查询全局服务配置
func (this *NodeClusterDAO) FindClusterGlobalServerConfig(tx *dbs.Tx, clusterId int64) (*serverconfigs.GlobalServerConfig, error) {
configJSON, err := this.Query(tx).
Pk(clusterId).
Result("globalServerConfig").
FindJSONCol()
if err != nil {
return nil, err
}
var config = serverconfigs.DefaultGlobalServerConfig()
if IsNull(configJSON) {
return config, nil
}
err = json.Unmarshal(configJSON, config)
if err != nil {
return nil, err
}
return config, nil
}
// UpdateClusterGlobalServerConfig 修改全局服务配置
func (this *NodeClusterDAO) UpdateClusterGlobalServerConfig(tx *dbs.Tx, clusterId int64, config *serverconfigs.GlobalServerConfig) error {
if config == nil {
config = serverconfigs.DefaultGlobalServerConfig()
}
configJSON, err := json.Marshal(config)
if err != nil {
return err
}
err = this.Query(tx).
Pk(clusterId).
Set("globalServerConfig", configJSON).
UpdateQuickly()
if err != nil {
return err
}
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeGlobalServerConfigChanged)
}
// NotifyUpdate 通知更新
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeConfigChanged)

View File

@@ -30,45 +30,53 @@ type NodeCluster struct {
SystemServices dbs.JSON `field:"systemServices"` // 系统服务设置
TimeZone string `field:"timeZone"` // 时区
NodeMaxThreads uint32 `field:"nodeMaxThreads"` // 节点最大线程数
DdosProtection dbs.JSON `field:"ddosProtection"` // DDOS端口
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
AutoOpenPorts uint8 `field:"autoOpenPorts"` // 是否自动尝试开放端口
IsPinned bool `field:"isPinned"` // 是否置顶
Webp dbs.JSON `field:"webp"` // WebP设置
Uam dbs.JSON `field:"uam"` // UAM设置
Clock dbs.JSON `field:"clock"` // 时钟配置
GlobalServerConfig dbs.JSON `field:"globalServerConfig"` // 全局服务配置
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
AutoInstallNftables bool `field:"autoInstallNftables"` // 自动安装nftables
}
type NodeClusterOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
Name interface{} // 名称
UseAllAPINodes interface{} // 是否使用所有API节点
ApiNodes interface{} // 使用的API节点
InstallDir interface{} // 安装目录
Order interface{} // 排序
CreatedAt interface{} // 创建时间
GrantId interface{} // 默认认证方式
State interface{} // 状态
AutoRegister interface{} // 是否开启自动注册
UniqueId interface{} // 唯一ID
Secret interface{} // 密钥
HealthCheck interface{} // 健康检查
DnsName interface{} // DNS名称
DnsDomainId interface{} // 域名ID
Dns interface{} // DNS配置
Toa interface{} // TOA配置
CachePolicyId interface{} // 缓存策略ID
HttpFirewallPolicyId interface{} // WAF策略ID
AccessLog interface{} // 访问日志设置
SystemServices interface{} // 系统服务设置
TimeZone interface{} // 时区
NodeMaxThreads interface{} // 节点最大线程数
DdosProtection interface{} // DDOS端口
AutoOpenPorts interface{} // 是否自动尝试开放端口
IsPinned interface{} // 是否置顶
Webp interface{} // WebP设置
Uam interface{} // UAM设置
Id any // ID
AdminId any // 管理员ID
UserId any // 用户ID
IsOn any // 是否启用
Name any // 名称
UseAllAPINodes any // 是否使用所有API节点
ApiNodes any // 使用的API节点
InstallDir any // 安装目录
Order any // 排序
CreatedAt any // 创建时间
GrantId any // 默认认证方式
State any // 状态
AutoRegister any // 是否开启自动注册
UniqueId any // 唯一ID
Secret any // 密钥
HealthCheck any // 健康检查
DnsName any // DNS名称
DnsDomainId any // 域名ID
Dns any // DNS配置
Toa any // TOA配置
CachePolicyId any // 缓存策略ID
HttpFirewallPolicyId any // WAF策略ID
AccessLog any // 访问日志设置
SystemServices any // 系统服务设置
TimeZone any // 时区
NodeMaxThreads any // 节点最大线程数
DdosProtection any // DDoS防护设置
AutoOpenPorts any // 是否自动尝试开放端口
IsPinned any // 是否置顶
Webp any // WebP设置
Uam any // UAM设置
Clock any // 时钟配置
GlobalServerConfig any // 全局服务配置
AutoRemoteStart any // 自动远程启动
AutoInstallNftables any // 自动安装nftables
}
func NewNodeClusterOperator() *NodeClusterOperator {

View File

@@ -2,7 +2,10 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
)
@@ -11,13 +14,15 @@ func (this *NodeCluster) DecodeDNSConfig() (*dnsconfigs.ClusterDNSConfig, error)
if len(this.Dns) == 0 {
// 一定要返回一个默认的值防止产生nil
return &dnsconfigs.ClusterDNSConfig{
NodesAutoSync: false,
ServersAutoSync: false,
CNameAsDomain: true,
NodesAutoSync: false,
ServersAutoSync: false,
CNAMEAsDomain: true,
IncludingLnNodes: true,
}, nil
}
var dnsConfig = &dnsconfigs.ClusterDNSConfig{
CNameAsDomain: true,
CNAMEAsDomain: true,
IncludingLnNodes: true,
}
err := json.Unmarshal(this.Dns, &dnsConfig)
if err != nil {
@@ -48,3 +53,27 @@ func (this *NodeCluster) HasDDoSProtection() bool {
}
return false
}
// DecodeClock 解析时钟配置
func (this *NodeCluster) DecodeClock() *nodeconfigs.ClockConfig {
var clock = nodeconfigs.DefaultClockConfig()
if IsNotNull(this.Clock) {
err := json.Unmarshal(this.Clock, clock)
if err != nil {
remotelogs.Error("NodeCluster.DecodeClock()", err.Error())
}
}
return clock
}
// DecodeGlobalServerConfig 解析全局服务配置
func (this *NodeCluster) DecodeGlobalServerConfig() *serverconfigs.GlobalServerConfig {
var config = serverconfigs.DefaultGlobalServerConfig()
if IsNotNull(this.GlobalServerConfig) {
err := json.Unmarshal(this.GlobalServerConfig, config)
if err != nil {
remotelogs.Error("NodeCluster.DecodeGlobalServerConfig()", err.Error())
}
}
return config
}

View File

@@ -140,18 +140,9 @@ func (this *NodeDAO) FindNodeName(tx *dbs.Tx, id int64) (string, error) {
// CreateNode 创建节点
func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterId int64, groupId int64, regionId int64) (nodeId int64, err error) {
// 检查节点数量
if teaconst.MaxNodes > 0 {
count, err := this.Query(tx).
State(NodeStateEnabled).
Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)").
Count()
if err != nil {
return 0, err
}
if int64(teaconst.MaxNodes) <= count {
return 0, errors.New("[企业版]超出最大节点数限制:" + types.String(teaconst.MaxNodes) + ",请购买更多配额")
}
err = this.CheckNodesLimit(tx)
if err != nil {
return
}
uniqueId, err := this.GenUniqueId(tx)
@@ -199,7 +190,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
}
// UpdateNode 修改节点
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, isOn bool, level int) error {
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, isOn bool, level int, lnAddrs []string) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
@@ -248,6 +239,15 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId
if teaconst.IsPlus {
op.Level = level
if lnAddrs == nil {
lnAddrs = []string{}
}
lnAddrsJSON, err := json.Marshal(lnAddrs)
if err != nil {
return err
}
op.LnAddrs = lnAddrsJSON
}
err = this.Save(tx, op)
@@ -605,7 +605,7 @@ func (this *NodeDAO) FindEnabledNodesWithGroupIdAndLevel(tx *dbs.Tx, groupId int
}
_, err = this.Query(tx).
State(NodeStateEnabled).
Result("id", "clusterId", "secondaryClusterIds", "uniqueId", "secret").
Result("id", "clusterId", "secondaryClusterIds", "uniqueId", "secret", "lnAddrs").
Attr("isOn", true).
Attr("groupId", groupId).
Attr("level", level).
@@ -976,7 +976,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
}
for _, server := range servers {
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true)
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true, false)
if err != nil {
return nil, err
}
@@ -1076,6 +1076,16 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
}
}
// 时钟
if config.Clock == nil {
config.Clock = nodeCluster.DecodeClock()
}
// 全局配置
if config.GlobalServerConfig == nil {
config.GlobalServerConfig = nodeCluster.DecodeGlobalServerConfig()
}
// 最大线程数、TCP连接数
if clusterIndex == 0 {
config.MaxThreads = int(nodeCluster.NodeMaxThreads)
@@ -1103,6 +1113,11 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
config.UAMPolicies[clusterId] = uamPolicy
}
// 自动安装nftables
if clusterIndex == 0 {
config.AutoInstallNftables = nodeCluster.AutoInstallNftables
}
clusterIndex++
}
@@ -1403,7 +1418,7 @@ func (this *NodeDAO) CountAllEnabledNodesWithRegionId(tx *dbs.Tx, regionId int64
}
// FindAllEnabledNodesDNSWithClusterId 获取一个集群的节点DNS信息
func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool) (result []*Node, err error) {
func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, includingLnNodes bool) (result []*Node, err error) {
if clusterId <= 0 {
return nil, nil
}
@@ -1415,6 +1430,9 @@ func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId i
} else {
query.Attr("clusterId", clusterId)
}
if !includingLnNodes {
query.Lte("level", 1)
}
_, err = query.
State(NodeStateEnabled).
Attr("isOn", true).
@@ -1878,14 +1896,18 @@ func (this *NodeDAO) FindParentNodeConfigs(tx *dbs.Tx, nodeId int64, groupId int
if len(parentNodes) > 0 {
for _, node := range parentNodes {
addrs, err := SharedNodeIPAddressDAO.FindNodeAccessAndUpIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
if err != nil {
return nil, err
}
var addrStrings = []string{}
for _, addr := range addrs {
if addr.IsOn {
addrStrings = append(addrStrings, addr.DNSIP())
// 是否有Ln地址
var addrStrings = node.DecodeLnAddrs()
if len(addrStrings) == 0 {
// 如果没有就取节点的可访问地址
addrs, err := SharedNodeIPAddressDAO.FindNodeAccessAndUpIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
if err != nil {
return nil, err
}
for _, addr := range addrs {
if addr.IsOn {
addrStrings = append(addrStrings, addr.DNSIP())
}
}
}
@@ -1924,7 +1946,7 @@ func (this *NodeDAO) FindNodeDDoSProtection(tx *dbs.Tx, nodeId int64) (*ddosconf
return one.(*Node).DecodeDDoSProtection(), nil
}
// UpdateNodeDDoSProtection 设置集群的DDOS设置
// UpdateNodeDDoSProtection 设置集群的DDoS设置
func (this *NodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProtection *ddosconfigs.ProtectionConfig) error {
if nodeId <= 0 {
return ErrNotFound

View File

@@ -0,0 +1,12 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build !plus
package models
import (
"github.com/iwind/TeaGo/dbs"
)
func (this *NodeDAO) CheckNodesLimit(tx *dbs.Tx) error {
return nil
}

View File

@@ -4,6 +4,7 @@ import (
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql"
@@ -43,6 +44,8 @@ func init() {
// CreateLog 创建日志
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64, logType string, paramsJSON []byte) error {
description = utils.LimitString(description, 1000)
// 修复以前同样的日志
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
err := this.Query(tx).
@@ -339,8 +342,8 @@ func (this *NodeLogDAO) CountAllUnreadNodeLogs(tx *dbs.Tx) (int64, error) {
Count()
}
// UpdateNodeLogsRead 设置日志为已读
func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, nodeLogIds []int64) error {
// UpdateNodeLogIdsRead 设置一组日志为已读
func (this *NodeLogDAO) UpdateNodeLogIdsRead(tx *dbs.Tx, nodeLogIds []int64) error {
for _, logId := range nodeLogIds {
err := this.Query(tx).
Pk(logId).
@@ -353,6 +356,16 @@ func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, nodeLogIds []int64) error
return nil
}
// UpdateNodeLogsRead 设置节点日志为已读
func (this *NodeLogDAO) UpdateNodeLogsRead(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
return this.Query(tx).
Attr("role", role).
Attr("nodeId", nodeId).
Attr("isRead", false).
Set("isRead", true).
UpdateQuickly()
}
// UpdateAllNodeLogsRead 设置所有日志为已读
func (this *NodeLogDAO) UpdateAllNodeLogsRead(tx *dbs.Tx) error {
return this.Query(tx).
@@ -378,6 +391,13 @@ func (this *NodeLogDAO) DeleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.N
if clusterId <= 0 {
return nil
}
// 执行钩子
err := this.deleteNodeLogsWithCluster(tx, role, clusterId)
if err != nil {
return err
}
var query = this.Query(tx).
Attr("role", role)
@@ -385,13 +405,10 @@ func (this *NodeLogDAO) DeleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.N
case nodeconfigs.NodeRoleNode:
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE clusterId=:clusterId)")
query.Param("clusterId", clusterId)
case nodeconfigs.NodeRoleDNS:
query.Where("nodeId IN (SELECT id FROM " + SharedNSNodeDAO.Table + " WHERE clusterId=:clusterId)")
query.Param("clusterId", clusterId)
default:
return nil
}
_, err := query.Delete()
_, err = query.Delete()
return err
}

View File

@@ -0,0 +1,13 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build !plus
package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/dbs"
)
func (this *NodeLogDAO) deleteNodeLogsWithCluster(tx *dbs.Tx, role nodeconfigs.NodeRole, clusterId int64) error {
return nil
}

View File

@@ -155,32 +155,3 @@ func (this *NodeLoginDAO) FindFrequentPorts(tx *dbs.Tx) ([]int32, error) {
}
return ports, nil
}
func (this *NodeLoginDAO) FindFrequentGrantIds(tx *dbs.Tx, nodeClusterId int64, nsClusterId int64) ([]int64, error) {
var query = this.Query(tx).
Attr("state", NodeLoginStateEnabled).
Result("JSON_EXTRACT(params, '$.grantId') as `grantId`", "COUNT(*) AS c").
Having("grantId>0").
Desc("c").
Limit(3).
Group("grantId")
if nodeClusterId > 0 {
query.Attr("role", nodeconfigs.NodeRoleNode)
query.Where("(nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
Param("clusterId", nodeClusterId)
} else if nsClusterId > 0 {
query.Attr("role", nodeconfigs.NodeRoleDNS)
query.Where("(nodeId IN (SELECT id FROM "+SharedNSNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
Param("clusterId", nsClusterId)
}
ones, _, err := query.
FindOnes()
if err != nil {
return nil, err
}
var grantIds = []int64{}
for _, one := range ones {
grantIds = append(grantIds, one.GetInt64("grantId"))
}
return grantIds, nil
}

View File

@@ -0,0 +1,36 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build !plus
package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/dbs"
)
func (this *NodeLoginDAO) FindFrequentGrantIds(tx *dbs.Tx, nodeClusterId int64, nsClusterId int64) ([]int64, error) {
var query = this.Query(tx).
Attr("state", NodeLoginStateEnabled).
Result("JSON_EXTRACT(params, '$.grantId') as `grantId`", "COUNT(*) AS c").
Having("grantId>0").
Desc("c").
Limit(3).
Group("grantId")
if nodeClusterId > 0 {
query.Attr("role", nodeconfigs.NodeRoleNode)
query.Where("(nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
Param("clusterId", nodeClusterId)
} else if nsClusterId > 0 {
return nil, nil
}
ones, _, err := query.
FindOnes()
if err != nil {
return nil, err
}
var grantIds = []int64{}
for _, one := range ones {
grantIds = append(grantIds, one.GetInt64("grantId"))
}
return grantIds, nil
}

View File

@@ -8,6 +8,7 @@ type Node struct {
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
Level uint8 `field:"level"` // 级别
LnAddrs dbs.JSON `field:"lnAddrs"` // Ln级别访问地址
IsOn bool `field:"isOn"` // 是否启用
IsUp bool `field:"isUp"` // 是否在线
CountUp uint32 `field:"countUp"` // 连续在线次数
@@ -42,41 +43,42 @@ type Node struct {
}
type NodeOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
Level interface{} // 级别
IsOn interface{} // 是否启用
IsUp interface{} // 是否在线
CountUp interface{} // 连续在线次数
CountDown interface{} // 连续线次数
IsActive interface{} // 是否活跃
InactiveNotifiedAt interface{} // 离线通知时间
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
Name interface{} // 节点名
Code interface{} // 代号
ClusterId interface{} // 主集群ID
SecondaryClusterIds interface{} // 集群ID
RegionId interface{} // 区域ID
GroupId interface{} // 分组ID
CreatedAt interface{} // 创建时间
Status interface{} // 最新的状态
Version interface{} // 当前版本号
LatestVersion interface{} // 最后版本号
InstallDir interface{} // 安装目录
IsInstalled interface{} // 是否已安装
InstallStatus interface{} // 安装状态
State interface{} // 状态
ConnectedAPINodes interface{} // 当前连接的API节点
MaxCPU interface{} // 可以使用的最多CPU
MaxThreads interface{} // 最大线程数
DdosProtection interface{} // DDOS配置
DnsRoutes interface{} // DNS线路设
MaxCacheDiskCapacity interface{} // 硬盘缓存容量
MaxCacheMemoryCapacity interface{} // 内存缓存容量
CacheDiskDir interface{} // 缓存目录
DnsResolver interface{} // DNS解析器
Id any // ID
AdminId any // 管理员ID
UserId any // 用户ID
Level any // 级别
LnAddrs any // Ln级别访问地址
IsOn any // 是否启用
IsUp any // 是否在线
CountUp any // 连续线次数
CountDown any // 连续下线次数
IsActive any // 是否活跃
InactiveNotifiedAt any // 离线通知时间
UniqueId any // 节点ID
Secret any // 密钥
Name any // 节点名
Code any // 代号
ClusterId any // 集群ID
SecondaryClusterIds any // 从集群ID
RegionId any // 区域ID
GroupId any // 分组ID
CreatedAt any // 创建时间
Status any // 最新的状态
Version any // 当前版本号
LatestVersion any // 最后版本号
InstallDir any // 安装目录
IsInstalled any // 是否已安装
InstallStatus any // 安装状态
State any // 状态
ConnectedAPINodes any // 当前连接的API节点
MaxCPU any // 可以使用的最多CPU
MaxThreads any // 最大线程数
DdosProtection any // DDOS配
DnsRoutes any // DNS线路设置
MaxCacheDiskCapacity any // 硬盘缓存容量
MaxCacheMemoryCapacity any // 内存缓存容量
CacheDiskDir any // 缓存目录
DnsResolver any // DNS解析器
}
func NewNodeOperator() *NodeOperator {

View File

@@ -168,3 +168,16 @@ func (this *Node) DecodeDNSResolver() *nodeconfigs.DNSResolverConfig {
}
return resolverConfig
}
func (this *Node) DecodeLnAddrs() []string {
if IsNull(this.LnAddrs) {
return nil
}
var result = []string{}
err := json.Unmarshal(this.LnAddrs, &result)
if err != nil {
// ignore error
}
return result
}

View File

@@ -14,20 +14,22 @@ import (
type NodeTaskType = string
const (
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged"
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged"
NodeTaskTypeConfigChanged NodeTaskType = "configChanged" // 节点整体配置变化
NodeTaskTypeDDosProtectionChanged NodeTaskType = "ddosProtectionChanged" // 节点DDoS配置变更
NodeTaskTypeGlobalServerConfigChanged NodeTaskType = "globalServerConfigChanged" // 全局服务设置变化
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
NodeTaskTypeScriptsChanged NodeTaskType = "scriptsChanged"
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged"
// NS相关
NSNodeTaskTypeConfigChanged NodeTaskType = "nsConfigChanged"
NSNodeTaskTypeDomainChanged NodeTaskType = "nsDomainChanged"
NSNodeTaskTypeRecordChanged NodeTaskType = "nsRecordChanged"
NSNodeTaskTypeRouteChanged NodeTaskType = "nsRouteChanged"
NSNodeTaskTypeKeyChanged NodeTaskType = "nsKeyChanged"
NSNodeTaskTypeConfigChanged NodeTaskType = "nsConfigChanged"
NSNodeTaskTypeDomainChanged NodeTaskType = "nsDomainChanged"
NSNodeTaskTypeRecordChanged NodeTaskType = "nsRecordChanged"
NSNodeTaskTypeRouteChanged NodeTaskType = "nsRouteChanged"
NSNodeTaskTypeKeyChanged NodeTaskType = "nsKeyChanged"
NSNodeTaskTypeDDosProtectionChanged NodeTaskType = "nsDDoSProtectionChanged" // 节点DDoS配置变更
)
type NodeTaskDAO dbs.DAO
@@ -157,45 +159,6 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, ser
return nil
}
// ExtractNSClusterTask 分解NS节点集群任务
func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
nodeIds, err := SharedNSNodeDAO.FindAllNodeIdsMatch(tx, clusterId, true, configutils.BoolStateYes)
if err != nil {
return err
}
_, err = this.Query(tx).
Attr("role", nodeconfigs.NodeRoleDNS).
Attr("clusterId", clusterId).
Param("clusterIdString", types.String(clusterId)).
Where("nodeId > 0").
Attr("type", taskType).
Delete()
if err != nil {
return err
}
var version = time.Now().UnixNano()
for _, nodeId := range nodeIds {
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, 0, taskType, version)
if err != nil {
return err
}
}
_, err = this.Query(tx).
Attr("role", nodeconfigs.NodeRoleDNS).
Attr("clusterId", clusterId).
Attr("nodeId", 0).
Attr("type", taskType).
Delete()
if err != nil {
return err
}
return nil
}
// ExtractAllClusterTasks 分解所有集群任务
func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx, role string) error {
ones, err := this.Query(tx).

View File

@@ -0,0 +1,11 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build !plus
package models
import "github.com/iwind/TeaGo/dbs"
// ExtractNSClusterTask 分解NS节点集群任务
func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
return nil
}

View File

@@ -1,336 +0,0 @@
package models
import (
"encoding/json"
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"sort"
"strings"
"sync"
"time"
)
type NSAccessLogDAO dbs.DAO
func NewNSAccessLogDAO() *NSAccessLogDAO {
return dbs.NewDAO(&NSAccessLogDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNSAccessLogs",
Model: new(NSAccessLog),
PkName: "id",
},
}).(*NSAccessLogDAO)
}
var SharedNSAccessLogDAO *NSAccessLogDAO
func init() {
dbs.OnReady(func() {
SharedNSAccessLogDAO = NewNSAccessLogDAO()
})
}
// CreateNSAccessLogs 创建访问日志
func (this *NSAccessLogDAO) CreateNSAccessLogs(tx *dbs.Tx, accessLogs []*pb.NSAccessLog) error {
dao := randomNSAccessLogDAO()
if dao == nil {
dao = &NSAccessLogDAOWrapper{
DAO: SharedNSAccessLogDAO,
NodeId: 0,
}
}
return this.CreateNSAccessLogsWithDAO(tx, dao, accessLogs)
}
// CreateNSAccessLogsWithDAO 使用特定的DAO创建访问日志
func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NSAccessLogDAOWrapper, accessLogs []*pb.NSAccessLog) error {
if daoWrapper == nil {
return errors.New("dao should not be nil")
}
if len(accessLogs) == 0 {
return nil
}
dao := daoWrapper.DAO
// TODO 改成事务批量提交,以加快速度
for _, accessLog := range accessLogs {
day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
table, err := findNSAccessLogTable(dao.Instance, day, false)
if err != nil {
return err
}
fields := map[string]interface{}{}
fields["nodeId"] = accessLog.NsNodeId
fields["domainId"] = accessLog.NsDomainId
fields["recordId"] = accessLog.NsRecordId
fields["createdAt"] = accessLog.Timestamp
fields["requestId"] = accessLog.RequestId
content, err := json.Marshal(accessLog)
if err != nil {
return err
}
fields["content"] = content
_, err = dao.Query(tx).
Table(table).
Sets(fields).
Insert()
if err != nil {
// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试
if strings.Contains(err.Error(), "1146") {
table, err = findNSAccessLogTable(dao.Instance, day, true)
if err != nil {
return err
}
_, err = dao.Query(tx).
Table(table).
Sets(fields).
Insert()
if err != nil {
return err
}
}
}
}
return nil
}
// ListAccessLogs 读取往前的 单页访问日志
func (this *NSAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, clusterId int64, nodeId int64, domainId int64, recordId int64, recordType string, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, hasMore bool, err error) {
if len(day) != 8 {
return
}
// 限制能查询的最大条数,防止占用内存过多
if size > 1000 {
size = 1000
}
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, clusterId, nodeId, domainId, recordId, recordType, keyword, reverse)
if err != nil || int64(len(result)) < size {
return
}
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, clusterId, nodeId, domainId, recordId, recordType, keyword, reverse)
hasMore = len(moreResult) > 0
return
}
// 读取往前的单页访问日志
func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, clusterId int64, nodeId int64, domainId int64, recordId int64, recordType string, keyword string, reverse bool) (result []*NSAccessLog, nextLastRequestId string, err error) {
if size <= 0 {
return nil, lastRequestId, nil
}
accessLogLocker.RLock()
var daoList = []*NSAccessLogDAOWrapper{}
for _, daoWrapper := range nsAccessLogDAOMapping {
daoList = append(daoList, daoWrapper)
}
accessLogLocker.RUnlock()
if len(daoList) == 0 {
daoList = []*NSAccessLogDAOWrapper{{
DAO: SharedNSAccessLogDAO,
NodeId: 0,
}}
}
// 检查是否有集群筛选条件
var nodeIds []int64
if clusterId > 0 && nodeId <= 0 {
nodeIds, err = SharedNSNodeDAO.FindEnabledNodeIdsWithClusterId(tx, clusterId)
if err != nil {
return
}
if len(nodeIds) == 0 {
// 没有任何节点则直接返回空
return nil, "", nil
}
}
var locker = sync.Mutex{}
var count = len(daoList)
var wg = &sync.WaitGroup{}
wg.Add(count)
for _, daoWrapper := range daoList {
go func(daoWrapper *NSAccessLogDAOWrapper) {
defer wg.Done()
dao := daoWrapper.DAO
tableName, exists, err := findNSAccessLogTableName(dao.Instance, day)
if !exists {
// 表格不存在则跳过
return
}
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
var query = dao.Query(tx)
// 条件
if nodeId > 0 {
query.Attr("nodeId", nodeId)
} else if clusterId > 0 {
query.Attr("nodeId", nodeIds)
query.Reuse(false)
}
if domainId > 0 {
query.Attr("domainId", domainId)
}
if recordId > 0 {
query.Attr("recordId", recordId)
}
// offset
if len(lastRequestId) > 0 {
if !reverse {
query.Where("requestId<:requestId").
Param("requestId", lastRequestId)
} else {
query.Where("requestId>:requestId").
Param("requestId", lastRequestId)
}
}
// keyword
if len(keyword) > 0 {
query.Where("(JSON_EXTRACT(content, '$.remoteAddr') LIKE :keyword OR JSON_EXTRACT(content, '$.questionName') LIKE :keyword OR JSON_EXTRACT(content, '$.recordValue') LIKE :keyword)").
Param("keyword", dbutils.QuoteLike(keyword))
}
// record type
if len(recordType) > 0 {
query.Where("JSON_EXTRACT(content, '$.questionType')=:recordType")
query.Param("recordType", recordType)
}
if !reverse {
query.Desc("requestId")
} else {
query.Asc("requestId")
}
// 开始查询
ones, err := query.
Table(tableName).
Limit(size).
FindAll()
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
locker.Lock()
for _, one := range ones {
accessLog := one.(*NSAccessLog)
result = append(result, accessLog)
}
locker.Unlock()
}(daoWrapper)
}
wg.Wait()
if len(result) == 0 {
return nil, lastRequestId, nil
}
// 按照requestId排序
sort.Slice(result, func(i, j int) bool {
if !reverse {
return result[i].RequestId > result[j].RequestId
} else {
return result[i].RequestId < result[j].RequestId
}
})
if int64(len(result)) > size {
result = result[:size]
}
var requestId = result[len(result)-1].RequestId
if reverse {
lists.Reverse(result)
}
if !reverse {
return result, requestId, nil
} else {
return result, requestId, nil
}
}
// FindAccessLogWithRequestId 根据请求ID获取访问日志
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
return nil, errors.New("invalid requestId")
}
accessLogLocker.RLock()
daoList := []*NSAccessLogDAOWrapper{}
for _, daoWrapper := range nsAccessLogDAOMapping {
daoList = append(daoList, daoWrapper)
}
accessLogLocker.RUnlock()
if len(daoList) == 0 {
daoList = []*NSAccessLogDAOWrapper{{
DAO: SharedNSAccessLogDAO,
NodeId: 0,
}}
}
count := len(daoList)
wg := &sync.WaitGroup{}
wg.Add(count)
var result *NSAccessLog = nil
day := timeutil.FormatTime("Ymd", types.Int64(requestId[:10]))
for _, daoWrapper := range daoList {
go func(daoWrapper *NSAccessLogDAOWrapper) {
defer wg.Done()
dao := daoWrapper.DAO
tableName, exists, err := findNSAccessLogTableName(dao.Instance, day)
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
if !exists {
return
}
one, err := dao.Query(tx).
Table(tableName).
Attr("requestId", requestId).
Find()
if err != nil {
logs.Println("[DB_NODE]" + err.Error())
return
}
if one != nil {
result = one.(*NSAccessLog)
}
}(daoWrapper)
}
wg.Wait()
return result, nil
}

View File

@@ -1,10 +1,7 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -81,68 +78,6 @@ func (this *NSClusterDAO) FindEnabledNSClusterName(tx *dbs.Tx, id int64) (string
FindStringCol("")
}
// CreateCluster 创建集群
func (this *NSClusterDAO) CreateCluster(tx *dbs.Tx, name string, accessLogRefJSON []byte) (int64, error) {
var op = NewNSClusterOperator()
op.Name = name
if len(accessLogRefJSON) > 0 {
op.AccessLog = accessLogRefJSON
}
op.IsOn = true
op.State = NSClusterStateEnabled
// 默认端口
// TCP
{
var config = &serverconfigs.TCPProtocolConfig{}
config.IsOn = true
config.Listen = []*serverconfigs.NetworkAddressConfig{
{
Protocol: serverconfigs.ProtocolTCP,
PortRange: "53",
},
}
configJSON, err := json.Marshal(config)
if err != nil {
return 0, err
}
op.Tcp = configJSON
}
// UDP
{
var config = &serverconfigs.UDPProtocolConfig{}
config.IsOn = true
config.Listen = []*serverconfigs.NetworkAddressConfig{
{
Protocol: serverconfigs.ProtocolUDP,
PortRange: "53",
},
}
configJSON, err := json.Marshal(config)
if err != nil {
return 0, err
}
op.Udp = configJSON
}
return this.SaveInt64(tx, op)
}
// UpdateCluster 修改集群
func (this *NSClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, isOn bool) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
}
var op = NewNSClusterOperator()
op.Id = clusterId
op.Name = name
op.IsOn = isOn
return this.Save(tx, op)
}
// CountAllEnabledClusters 计算可用集群数量
func (this *NSClusterDAO) CountAllEnabledClusters(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
@@ -188,23 +123,6 @@ func (this *NSClusterDAO) FindAllEnabledClusterIds(tx *dbs.Tx) ([]int64, error)
return result, nil
}
// UpdateClusterAccessLog 设置访问日志
func (this *NSClusterDAO) UpdateClusterAccessLog(tx *dbs.Tx, clusterId int64, accessLogJSON []byte) error {
return this.Query(tx).
Pk(clusterId).
Set("accessLog", accessLogJSON).
UpdateQuickly()
}
// FindClusterAccessLog 读取访问日志配置
func (this *NSClusterDAO) FindClusterAccessLog(tx *dbs.Tx, clusterId int64) ([]byte, error) {
accessLog, err := this.Query(tx).
Pk(clusterId).
Result("accessLog").
FindStringCol("")
return []byte(accessLog), err
}
// FindClusterGrantId 查找集群的认证ID
func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
@@ -213,102 +131,6 @@ func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64
FindInt64Col(0)
}
// UpdateRecursion 设置递归DNS
func (this *NSClusterDAO) UpdateRecursion(tx *dbs.Tx, clusterId int64, recursionJSON []byte) error {
err := this.Query(tx).
Pk(clusterId).
Set("recursion", recursionJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, clusterId)
}
// FindClusterRecursion 读取递归DNS配置
func (this *NSClusterDAO) FindClusterRecursion(tx *dbs.Tx, clusterId int64) ([]byte, error) {
recursion, err := this.Query(tx).
Result("recursion").
Pk(clusterId).
FindStringCol("")
if err != nil {
return nil, err
}
return []byte(recursion), nil
}
// FindClusterTCP 查找集群的TCP设置
func (this *NSClusterDAO) FindClusterTCP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
return this.Query(tx).
Pk(clusterId).
Result("tcp").
FindBytesCol()
}
// UpdateClusterTCP 修改集群的TCP设置
func (this *NSClusterDAO) UpdateClusterTCP(tx *dbs.Tx, clusterId int64, tcpConfig *serverconfigs.TCPProtocolConfig) error {
tcpJSON, err := json.Marshal(tcpConfig)
if err != nil {
return err
}
err = this.Query(tx).
Pk(clusterId).
Set("tcp", tcpJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, clusterId)
}
// FindClusterTLS 查找集群的TLS设置
func (this *NSClusterDAO) FindClusterTLS(tx *dbs.Tx, clusterId int64) ([]byte, error) {
return this.Query(tx).
Pk(clusterId).
Result("tls").
FindBytesCol()
}
// UpdateClusterTLS 修改集群的TLS设置
func (this *NSClusterDAO) UpdateClusterTLS(tx *dbs.Tx, clusterId int64, tlsConfig *serverconfigs.TLSProtocolConfig) error {
tlsJSON, err := json.Marshal(tlsConfig)
if err != nil {
return err
}
err = this.Query(tx).
Pk(clusterId).
Set("tls", tlsJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, clusterId)
}
// FindClusterUDP 查找集群的TCP设置
func (this *NSClusterDAO) FindClusterUDP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
return this.Query(tx).
Pk(clusterId).
Result("udp").
FindBytesCol()
}
// UpdateClusterUDP 修改集群的UDP设置
func (this *NSClusterDAO) UpdateClusterUDP(tx *dbs.Tx, clusterId int64, udpConfig *serverconfigs.UDPProtocolConfig) error {
udpJSON, err := json.Marshal(udpConfig)
if err != nil {
return err
}
err = this.Query(tx).
Pk(clusterId).
Set("udp", udpJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, clusterId)
}
// CountAllClustersWithSSLPolicyIds 计算使用SSL策略的所有NS集群数量
func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) {
if len(sslPolicyIds) == 0 {

View File

@@ -4,31 +4,47 @@ import "github.com/iwind/TeaGo/dbs"
// NSCluster 域名服务器集群
type NSCluster struct {
Id uint32 `field:"id"` // ID
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 集群名
InstallDir string `field:"installDir"` // 安装目录
State uint8 `field:"state"` // 状态
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
GrantId uint32 `field:"grantId"` // 授权ID
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
Tcp dbs.JSON `field:"tcp"` // TCP设置
Tls dbs.JSON `field:"tls"` // TLS设置
Udp dbs.JSON `field:"udp"` // UDP设置
Id uint32 `field:"id"` // ID
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 集群名
InstallDir string `field:"installDir"` // 安装目录
State uint8 `field:"state"` // 状态
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
GrantId uint32 `field:"grantId"` // 授权ID
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
Tcp dbs.JSON `field:"tcp"` // TCP设置
Tls dbs.JSON `field:"tls"` // TLS设置
Udp dbs.JSON `field:"udp"` // UDP设置
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
Hosts dbs.JSON `field:"hosts"` // DNS主机地址
Soa dbs.JSON `field:"soa"` // SOA配置
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
TimeZone string `field:"timeZone"` // 时区
Answer dbs.JSON `field:"answer"` // 应答设置
SoaSerial uint64 `field:"soaSerial"` // SOA序列号
Email string `field:"email"` // 管理员邮箱
}
type NSClusterOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
Name interface{} // 集群名
InstallDir interface{} // 安装目录
State interface{} // 状态
AccessLog interface{} // 访问日志配置
GrantId interface{} // 授权ID
Recursion interface{} // 递归DNS设置
Tcp interface{} // TCP设置
Tls interface{} // TLS设置
Udp interface{} // UDP设置
Id any // ID
IsOn any // 是否启用
Name any // 集群名
InstallDir any // 安装目录
State any // 状态
AccessLog any // 访问日志配置
GrantId any // 授权ID
Recursion any // 递归DNS设置
Tcp any // TCP设置
Tls any // TLS设置
Udp any // UDP设置
DdosProtection any // DDoS防护设置
Hosts any // DNS主机地址
Soa any // SOA配置
AutoRemoteStart any // 自动远程启动
TimeZone any // 时区
Answer any // 应答设置
SoaSerial any // SOA序列号
Email any // 管理员邮箱
}
func NewNSClusterOperator() *NSClusterOperator {

View File

@@ -1 +1,30 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
)
// DecodeDDoSProtection 解析DDOS Protection设置
func (this *NSCluster) DecodeDDoSProtection() *ddosconfigs.ProtectionConfig {
if IsNull(this.DdosProtection) {
return nil
}
var result = &ddosconfigs.ProtectionConfig{}
err := json.Unmarshal(this.DdosProtection, &result)
if err != nil {
remotelogs.Error("NSCluster.DecodeDDoSProtection", "decode failed: "+err.Error())
}
return result
}
// HasDDoSProtection 检查是否有DDOS设置
func (this *NSCluster) HasDDoSProtection() bool {
var config = this.DecodeDDoSProtection()
if config != nil {
return config.IsOn()
}
return false
}

View File

@@ -1,20 +1,15 @@
//go:build !plus
package models
import (
"encoding/json"
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
)
const (
@@ -93,110 +88,6 @@ func (this *NSNodeDAO) FindEnabledNSNodeName(tx *dbs.Tx, nodeId int64) (string,
FindStringCol("")
}
// FindAllEnabledNodesWithClusterId 查找一个集群下的所有节点
func (this *NSNodeDAO) FindAllEnabledNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NSNode, err error) {
_, err = this.Query(tx).
Attr("clusterId", clusterId).
State(NSNodeStateEnabled).
DescPk().
Slice(&result).
FindAll()
return
}
// CountAllEnabledNodes 所有集群的可用的节点数量
func (this *NSNodeDAO) CountAllEnabledNodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(NSNodeStateEnabled).
Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)").
Count()
}
// CountAllOfflineNodes 计算离线节点数量
func (this *NSNodeDAO) CountAllOfflineNodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(NSNodeStateEnabled).
Where("(status IS NULL OR JSON_EXTRACT(status, '$.updatedAt')<UNIX_TIMESTAMP()-120)").
Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)").
Count()
}
// CountAllEnabledNodesMatch 计算满足条件的节点数量
func (this *NSNodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string) (int64, error) {
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
// 安装状态
switch installState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Attr("isInstalled", 1)
case configutils.BoolStateNo:
query.Attr("isInstalled", 0)
}
// 在线状态
switch activeState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Where("(isActive=1 AND JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60)")
case configutils.BoolStateNo:
query.Where("(isActive=0 OR status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", dbutils.QuoteLike(keyword))
}
return query.
State(NSNodeStateEnabled).
Count()
}
// ListAllEnabledNodesMatch 列出单页匹配的节点
func (this *NSNodeDAO) ListAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string, offset int64, size int64) (result []*NSNode, err error) {
query := this.Query(tx)
// 安装状态
switch installState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Attr("isInstalled", 1)
case configutils.BoolStateNo:
query.Attr("isInstalled", 0)
}
// 在线状态
switch activeState {
case configutils.BoolStateAll:
// 所有
case configutils.BoolStateYes:
query.Where("(isActive=1 AND JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60)")
case configutils.BoolStateNo:
query.Where("(isActive=0 OR status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
}
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", dbutils.QuoteLike(keyword))
}
_, err = query.
State(NSNodeStateEnabled).
Offset(offset).
Limit(size).
Slice(&result).
DescPk().
FindAll()
return
}
// CountAllLowerVersionNodesWithClusterId 计算单个集群中所有低于某个版本的节点数量
func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId int64, os string, arch string, version string) (int64, error) {
return this.Query(tx).
@@ -212,73 +103,6 @@ func (this *NSNodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, cluste
Count()
}
// CreateNode 创建节点
func (this *NSNodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterId int64) (nodeId int64, err error) {
uniqueId, err := this.GenUniqueId(tx)
if err != nil {
return 0, err
}
secret := rands.String(32)
// 保存API Token
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleDNS)
if err != nil {
return
}
var op = NewNSNodeOperator()
op.AdminId = adminId
op.Name = name
op.UniqueId = uniqueId
op.Secret = secret
op.ClusterId = clusterId
op.IsOn = 1
op.State = NSNodeStateEnabled
err = this.Save(tx, op)
if err != nil {
return 0, err
}
// 通知节点更新
nodeId = types.Int64(op.Id)
err = this.NotifyUpdate(tx, nodeId)
if err != nil {
return 0, err
}
// 通知DNS更新
err = this.NotifyDNSUpdate(tx, nodeId)
if err != nil {
return 0, err
}
return nodeId, nil
}
// UpdateNode 修改节点
func (this *NSNodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, isOn bool) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
var op = NewNSNodeOperator()
op.Id = nodeId
op.Name = name
op.ClusterId = clusterId
op.IsOn = isOn
err := this.Save(tx, op)
if err != nil {
return err
}
err = this.NotifyUpdate(tx, nodeId)
if err != nil {
return err
}
return this.NotifyDNSUpdate(tx, nodeId)
}
// FindEnabledNodeIdWithUniqueId 根据唯一ID获取节点ID
func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
return this.Query(tx).
@@ -288,37 +112,6 @@ func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string
FindInt64Col(0)
}
// FindNodeInstallStatus 查询节点的安装状态
func (this *NSNodeDAO) FindNodeInstallStatus(tx *dbs.Tx, nodeId int64) (*NodeInstallStatus, error) {
node, err := this.Query(tx).
Pk(nodeId).
Result("installStatus", "isInstalled").
Find()
if err != nil {
return nil, err
}
if node == nil {
return nil, errors.New("not found")
}
installStatus := node.(*NSNode).InstallStatus
isInstalled := node.(*NSNode).IsInstalled
if len(installStatus) == 0 {
return NewNodeInstallStatus(), nil
}
status := &NodeInstallStatus{}
err = json.Unmarshal(installStatus, status)
if err != nil {
return nil, err
}
if isInstalled {
status.IsFinished = true
status.IsOk = true
}
return status, nil
}
// GenUniqueId 生成唯一ID
func (this *NSNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
for {
@@ -347,7 +140,7 @@ func (this *NSNodeDAO) UpdateNodeIsInstalled(tx *dbs.Tx, nodeId int64, isInstall
}
// UpdateNodeStatus 更改节点状态
func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error {
func (this *NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error {
if nodeStatus == nil {
return nil
}
@@ -375,118 +168,6 @@ func (this *NSNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (in
Count()
}
// ComposeNodeConfig 组合节点配置
func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.NSNodeConfig, error) {
if nodeId <= 0 {
return nil, nil
}
node, err := this.FindEnabledNSNode(tx, nodeId)
if err != nil {
return nil, err
}
if node == nil {
return nil, nil
}
cluster, err := SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(node.ClusterId))
if err != nil {
return nil, err
}
if cluster == nil {
return nil, nil
}
var config = &dnsconfigs.NSNodeConfig{
Id: int64(node.Id),
NodeId: node.UniqueId,
Secret: node.Secret,
ClusterId: int64(node.ClusterId),
}
// 访问日志
// 全局配置
{
globalValue, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeNSAccessLogSetting)
if err != nil {
return nil, err
}
if len(globalValue) > 0 {
var ref = &dnsconfigs.NSAccessLogRef{}
err = json.Unmarshal(globalValue, ref)
if err != nil {
return nil, err
}
config.AccessLogRef = ref
}
// 集群配置
if len(cluster.AccessLog) > 0 {
ref := &dnsconfigs.NSAccessLogRef{}
err = json.Unmarshal(cluster.AccessLog, ref)
if err != nil {
return nil, err
}
if ref.IsPrior {
config.AccessLogRef = ref
}
}
}
// 递归DNS配置
if IsNotNull(cluster.Recursion) {
var recursionConfig = &dnsconfigs.RecursionConfig{}
err = json.Unmarshal(cluster.Recursion, recursionConfig)
if err != nil {
return nil, err
}
config.RecursionConfig = recursionConfig
}
// TCP
if IsNotNull(cluster.Tcp) {
var tcpConfig = &serverconfigs.TCPProtocolConfig{}
err = json.Unmarshal(cluster.Tcp, tcpConfig)
if err != nil {
return nil, err
}
config.TCP = tcpConfig
}
// TLS
if IsNotNull(cluster.Tls) {
var tlsConfig = &serverconfigs.TLSProtocolConfig{}
err = json.Unmarshal(cluster.Tls, tlsConfig)
if err != nil {
return nil, err
}
// SSL
if tlsConfig.SSLPolicyRef != nil {
sslPolicyConfig, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, tlsConfig.SSLPolicyRef.SSLPolicyId, nil)
if err != nil {
return nil, err
}
if sslPolicyConfig != nil {
tlsConfig.SSLPolicy = sslPolicyConfig
}
}
config.TLS = tlsConfig
}
// UDP
if IsNotNull(cluster.Udp) {
var udpConfig = &serverconfigs.UDPProtocolConfig{}
err = json.Unmarshal(cluster.Udp, udpConfig)
if err != nil {
return nil, err
}
config.UDP = udpConfig
}
return config, nil
}
// FindNodeClusterId 获取节点的集群ID
func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error) {
return this.Query(tx).
@@ -495,140 +176,6 @@ func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error
FindInt64Col(0)
}
// FindNodeActive 检查节点活跃状态
func (this *NSNodeDAO) FindNodeActive(tx *dbs.Tx, nodeId int64) (bool, error) {
isActive, err := this.Query(tx).
Pk(nodeId).
Result("isActive").
FindIntCol(0)
if err != nil {
return false, err
}
return isActive == 1, nil
}
// UpdateNodeActive 修改节点活跃状态
func (this *NSNodeDAO) UpdateNodeActive(tx *dbs.Tx, nodeId int64, isActive bool) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
_, err := this.Query(tx).
Pk(nodeId).
Set("isActive", isActive).
Set("statusIsNotified", false).
Update()
return err
}
// UpdateNodeConnectedAPINodes 修改当前连接的API节点
func (this *NSNodeDAO) UpdateNodeConnectedAPINodes(tx *dbs.Tx, nodeId int64, apiNodeIds []int64) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
var op = NewNSNodeOperator()
op.Id = nodeId
if len(apiNodeIds) > 0 {
apiNodeIdsJSON, err := json.Marshal(apiNodeIds)
if err != nil {
return errors.Wrap(err)
}
op.ConnectedAPINodes = apiNodeIdsJSON
} else {
op.ConnectedAPINodes = "[]"
}
err := this.Save(tx, op)
return err
}
// FindAllNotifyingInactiveNodesWithClusterId 取得某个集群所有等待通知离线离线的节点
func (this *NSNodeDAO) FindAllNotifyingInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NSNode, err error) {
_, err = this.Query(tx).
State(NSNodeStateEnabled).
Attr("clusterId", clusterId).
Attr("isOn", true). // 只监控启用的节点
Attr("isInstalled", true). // 只监控已经安装的节点
Attr("isActive", false). // 当前已经离线的
Attr("statusIsNotified", false).
Result("id", "name").
Slice(&result).
FindAll()
return
}
// UpdateNodeStatusIsNotified 设置状态已经通知
func (this *NSNodeDAO) UpdateNodeStatusIsNotified(tx *dbs.Tx, nodeId int64) error {
return this.Query(tx).
Pk(nodeId).
Set("statusIsNotified", true).
UpdateQuickly()
}
// FindAllNodeIdsMatch 匹配节点并返回节点ID
func (this *NSNodeDAO) FindAllNodeIdsMatch(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, isOn configutils.BoolState) (result []int64, err error) {
query := this.Query(tx)
query.State(NSNodeStateEnabled)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}
if isOn == configutils.BoolStateYes {
query.Attr("isOn", true)
} else if isOn == configutils.BoolStateNo {
query.Attr("isOn", false)
}
query.Result("id")
ones, _, err := query.FindOnes()
if err != nil {
return nil, err
}
for _, one := range ones {
result = append(result, one.GetInt64("id"))
}
return
}
// UpdateNodeInstallStatus 修改节点的安装状态
func (this *NSNodeDAO) UpdateNodeInstallStatus(tx *dbs.Tx, nodeId int64, status *NodeInstallStatus) error {
if status == nil {
_, err := this.Query(tx).
Pk(nodeId).
Set("installStatus", "null").
Update()
return err
}
data, err := json.Marshal(status)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(nodeId).
Set("installStatus", string(data)).
Update()
return err
}
// FindEnabledNodeIdsWithClusterId 查找集群下的所有节点
func (this *NSNodeDAO) FindEnabledNodeIdsWithClusterId(tx *dbs.Tx, clusterId int64) ([]int64, error) {
if clusterId <= 0 {
return nil, nil
}
ones, err := this.Query(tx).
ResultPk().
Attr("clusterId", clusterId).
State(NSNodeStateEnabled).
FindAll()
if err != nil {
return nil, err
}
var result = []int64{}
for _, one := range ones {
result = append(result, int64(one.(*NSNode).Id))
}
return result, nil
}
// NotifyUpdate 通知更新
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
// TODO 先什么都不做

View File

@@ -1,6 +0,0 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -4,41 +4,45 @@ import "github.com/iwind/TeaGo/dbs"
// NSNode 域名服务器节点
type NSNode struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
ClusterId uint32 `field:"clusterId"` // 集群ID
Name string `field:"name"` // 节点名称
IsOn bool `field:"isOn"` // 是否启用
Status dbs.JSON `field:"status"` // 运行状态
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
IsUp bool `field:"isUp"` // 是否运行
IsInstalled bool `field:"isInstalled"` // 是否已安装
InstallStatus dbs.JSON `field:"installStatus"` // 安装状态
InstallDir string `field:"installDir"` // 安装目录
State uint8 `field:"state"` // 状态
IsActive bool `field:"isActive"` // 是否活跃
StatusIsNotified uint8 `field:"statusIsNotified"` // 活跃状态已经通知
ConnectedAPINodes dbs.JSON `field:"connectedAPINodes"` // 当前连接的API节点
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
ClusterId uint32 `field:"clusterId"` // 集群ID
Name string `field:"name"` // 节点名称
IsOn bool `field:"isOn"` // 是否启用
Status dbs.JSON `field:"status"` // 运行状态
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
IsUp bool `field:"isUp"` // 是否运行
IsInstalled bool `field:"isInstalled"` // 是否已安装
InstallStatus dbs.JSON `field:"installStatus"` // 安装状态
InstallDir string `field:"installDir"` // 安装目录
State uint8 `field:"state"` // 状态
IsActive bool `field:"isActive"` // 是否活跃
StatusIsNotified uint8 `field:"statusIsNotified"` // 活跃状态已经通知
InactiveNotifiedAt uint64 `field:"inactiveNotifiedAt"` // 离线通知时间
ConnectedAPINodes dbs.JSON `field:"connectedAPINodes"` // 当前连接的API节点
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
}
type NSNodeOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
ClusterId interface{} // 集群ID
Name interface{} // 节点名称
IsOn interface{} // 是否启用
Status interface{} // 运行状态
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
IsUp interface{} // 是否运行
IsInstalled interface{} // 是否已安装
InstallStatus interface{} // 安装状态
InstallDir interface{} // 安装目录
State interface{} // 状态
IsActive interface{} // 是否活跃
StatusIsNotified interface{} // 活跃状态已经通知
ConnectedAPINodes interface{} // 当前连接的API节点
Id any // ID
AdminId any // 管理员ID
ClusterId any // 集群ID
Name any // 节点名称
IsOn any // 是否启用
Status any // 运行状态
UniqueId any // 节点ID
Secret any // 密钥
IsUp any // 是否运行
IsInstalled any // 是否已安装
InstallStatus any // 安装状态
InstallDir any // 安装目录
State any // 状态
IsActive any // 是否活跃
StatusIsNotified any // 活跃状态已经通知
InactiveNotifiedAt any // 离线通知时间
ConnectedAPINodes any // 当前连接的API节点
DdosProtection any // DDoS防护设置
}
func NewNSNodeOperator() *NSNodeOperator {

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
"time"
)
@@ -39,3 +40,40 @@ func (this *NSNode) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
}
return status, nil
}
// DecodeDDoSProtection 解析DDoS Protection设置
func (this *NSNode) DecodeDDoSProtection() *ddosconfigs.ProtectionConfig {
if IsNull(this.DdosProtection) {
return nil
}
var result = &ddosconfigs.ProtectionConfig{}
err := json.Unmarshal(this.DdosProtection, &result)
if err != nil {
// ignore err
}
return result
}
// HasDDoSProtection 检查是否有DDOS设置
func (this *NSNode) HasDDoSProtection() bool {
var config = this.DecodeDDoSProtection()
if config != nil {
return !config.IsPriorEmpty()
}
return false
}
// DecodeConnectedAPINodes 解析连接的API节点列表
func (this *NSNode) DecodeConnectedAPINodes() []int64 {
if IsNull(this.ConnectedAPINodes) {
return nil
}
var result = []int64{}
err := json.Unmarshal(this.ConnectedAPINodes, &result)
if err != nil {
// ignore err
}
return result
}

View File

@@ -162,3 +162,19 @@ func (this *RegionTownDAO) FindSimilarTowns(towns []*RegionTown, townName string
return
}
// CreateTown 创建区县
func (this *RegionTownDAO) CreateTown(tx *dbs.Tx, cityId int64, townName string) (int64, error) {
var op = NewRegionTownOperator()
op.CityId = cityId
op.Name = townName
codes, err := json.Marshal([]string{townName})
if err != nil {
return 0, err
}
op.Codes = codes
op.State = RegionTownStateEnabled
return this.SaveInt64(tx, op)
}

View File

@@ -320,6 +320,7 @@ func (this *ReportNodeDAO) FindNodeAllowIPs(tx *dbs.Tx, nodeId int64) ([]string,
func (this *ReportNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
return this.Query(tx).
State(ReportNodeStateEnabled).
Attr("isOn", true).
Where("status IS NOT NULL").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("version", utils.VersionToLong(version)).

View File

@@ -4,6 +4,8 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -12,6 +14,7 @@ import (
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"math"
"strings"
"sync"
"time"
)
@@ -82,28 +85,131 @@ func (this *ServerBandwidthStatDAO) UpdateServerBandwidth(tx *dbs.Tx, userId int
func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, minute string) (int64, error) {
return this.Query(tx).
Table(this.partialTable(serverId)).
Result("bytes").
Attr("serverId", serverId).
Result("bytes").
Attr("day", day).
Attr("timeAt", minute).
FindInt64Col(0)
}
// FindHourlyBandwidthStats 按小时获取带宽峰值
func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverId int64, hours int32) (result []*pb.FindHourlyServerBandwidthStatsResponse_Stat, err error) {
if hours <= 0 {
hours = 24
}
var timestamp = time.Now().Unix() - int64(hours)*3600
ones, _, err := this.Query(tx).
Table(this.partialTable(serverId)).
Attr("serverId", serverId).
Result("MAX(bytes) AS bytes", "CONCAT(day, '.', SUBSTRING(timeAt, 1, 2)) AS fullTime").
Gte("CONCAT(day, '.', SUBSTRING(timeAt, 1, 2))", timeutil.FormatTime("Ymd.H", timestamp)).
Group("fullTime").
FindOnes()
if err != nil {
return nil, err
}
var m = map[string]*pb.FindHourlyServerBandwidthStatsResponse_Stat{}
for _, one := range ones {
var fullTime = one.GetString("fullTime")
var timePieces = strings.Split(fullTime, ".")
var day = timePieces[0]
var hour = timePieces[1]
m[day+hour] = &pb.FindHourlyServerBandwidthStatsResponse_Stat{
Bytes: one.GetInt64("bytes"),
Day: day,
Hour: types.Int32(hour),
}
}
fullHours, err := utils.RangeHours(timeutil.FormatTime("YmdH", timestamp), timeutil.Format("YmdH"))
if err != nil {
return nil, err
}
for _, fullHour := range fullHours {
stat, ok := m[fullHour]
if ok {
result = append(result, stat)
} else {
result = append(result, &pb.FindHourlyServerBandwidthStatsResponse_Stat{
Bytes: 0,
Day: fullHour[:8],
Hour: types.Int32(fullHour[8:]),
})
}
}
return result, nil
}
// FindDailyPeekBandwidthBytes 获取某天的带宽峰值
// day YYYYMMDD
func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string) (int64, error) {
return this.Query(tx).
Table(this.partialTable(serverId)).
Attr("serverId", serverId).
Attr("day", day).
Result("MAX(bytes)").
FindInt64Col(0)
}
// FindDailyBandwidthStats 按天获取带宽峰值
func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId int64, days int32) (result []*pb.FindDailyServerBandwidthStatsResponse_Stat, err error) {
if days <= 0 {
days = 14
}
var timestamp = time.Now().Unix() - int64(days)*86400
ones, _, err := this.Query(tx).
Table(this.partialTable(serverId)).
Result("MAX(bytes) AS bytes", "day").
Attr("serverId", serverId).
Gte("day", timeutil.FormatTime("Ymd", timestamp)).
Group("day").
FindOnes()
if err != nil {
return nil, err
}
var m = map[string]*pb.FindDailyServerBandwidthStatsResponse_Stat{}
for _, one := range ones {
var day = one.GetString("day")
m[day] = &pb.FindDailyServerBandwidthStatsResponse_Stat{
Bytes: one.GetInt64("bytes"),
Day: day,
}
}
allDays, err := utils.RangeDays(timeutil.FormatTime("Ymd", timestamp), timeutil.Format("Ymd"))
if err != nil {
return nil, err
}
for _, day := range allDays {
stat, ok := m[day]
if ok {
result = append(result, stat)
} else {
result = append(result, &pb.FindDailyServerBandwidthStatsResponse_Stat{
Bytes: 0,
Day: day,
})
}
}
return result, nil
}
// FindMonthlyPeekBandwidthBytes 获取某月的带宽峰值
// month YYYYMM
func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string) (int64, error) {
return this.Query(tx).
Table(this.partialTable(serverId)).
Attr("serverId", serverId).
Between("day", month+"01", month+"31").
Result("MAX(bytes)").
FindInt64Col(0)
@@ -160,8 +266,8 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
if percentile >= 100 {
result, err = this.Query(tx).
Table(this.partialTable(serverId)).
Result("bytes").
Attr("serverId", serverId).
Result("bytes").
Between("day", month+"01", month+"31").
Desc("bytes").
Limit(1).
@@ -191,8 +297,8 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
// 查询 nth 位置
result, err = this.Query(tx).
Table(this.partialTable(serverId)).
Result("bytes").
Attr("serverId", serverId).
Result("bytes").
Between("day", month+"01", month+"31").
Desc("bytes").
Offset(offset).
@@ -204,7 +310,7 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
// Clean 清理过期数据
func (this *ServerBandwidthStatDAO) Clean(tx *dbs.Tx) error {
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -62)) // 保留大约2个月的数据
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -100)) // 保留大约3个月的数据
return this.runBatch(func(table string, locker *sync.Mutex) error {
_, err := this.Query(tx).
Table(table).

View File

@@ -6,6 +6,7 @@ import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"testing"
@@ -77,3 +78,24 @@ func TestServerBandwidthStatDAO_Clean(t *testing.T) {
}
t.Log("ok", time.Since(before).Seconds()*1000, "ms")
}
func TestServerBandwidthStatDAO_FindHourlyBandwidthStats(t *testing.T) {
var dao = models.NewServerBandwidthStatDAO()
var tx *dbs.Tx
stats, err := dao.FindHourlyBandwidthStats(tx, 23, 24)
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(stats, t)
}
func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) {
var dao = models.NewServerBandwidthStatDAO()
var tx *dbs.Tx
stats, err := dao.FindDailyBandwidthStats(tx, 23, 14)
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(stats, t)
}

View File

@@ -437,6 +437,31 @@ func (this *ServerDailyStatDAO) FindDailyStats(tx *dbs.Tx, serverId int64, dayFr
return
}
// FindStatsWithDay 按天查找5分钟级统计
// day YYYYMMDD
func (this *ServerDailyStatDAO) FindStatsWithDay(tx *dbs.Tx, serverId int64, day string, timeFrom string, timeTo string) (result []*ServerDailyStat, err error) {
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
return
}
var query = this.Query(tx).
Attr("serverId", serverId).
Attr("day", day).
DescPk()
if len(timeFrom) > 0 {
query.Gte("timeFrom", timeFrom)
}
if len(timeTo) > 0 {
query.Lte("timeTo", timeTo)
}
_, err = query.
Slice(&result).
FindAll()
return
}
// FindMonthlyStatsWithPlan 查找某月有套餐的流量
// month YYYYMM
func (this *ServerDailyStatDAO) FindMonthlyStatsWithPlan(tx *dbs.Tx, month string) (result []*ServerDailyStat, err error) {

View File

@@ -759,7 +759,8 @@ func (this *ServerDAO) CountAllEnabledServers(tx *dbs.Tx) (int64, error) {
// CountAllEnabledServersMatch 计算所有可用服务数量
// 参数:
// groupId 分组ID如果为-1则搜索没有分组的服务
//
// groupId 分组ID如果为-1则搜索没有分组的服务
func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag configutils.BoolState, protocolFamilies []string) (int64, error) {
query := this.Query(tx).
State(ServerStateEnabled)
@@ -810,7 +811,8 @@ func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, ke
// ListEnabledServersMatch 列出单页的服务
// 参数:
// groupId 分组ID如果为-1则搜索没有分组的服务
//
// groupId 分组ID如果为-1则搜索没有分组的服务
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamilies []string, order string) (result []*Server, err error) {
query := this.Query(tx).
State(ServerStateEnabled).
@@ -860,49 +862,33 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
}
// 排序
var day = timeutil.Format("Ymd")
var minute = timeutil.FormatTime("His", time.Now().Unix()/300*300-300)
var selfTable = this.Table
var statTable = SharedServerDailyStatDAO.Table
var hasOnlyIds = false
var timestamp = (time.Now().Unix()) / 300 * 300
var times = []string{
timeutil.FormatTime("YmdHi", timestamp),
timeutil.FormatTime("YmdHi", timestamp-300),
timeutil.FormatTime("YmdHi", timestamp-300*2),
}
switch order {
case "trafficOutAsc":
query.Result("id")
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute")
query.Param("day", day)
query.Param("minute", minute)
query.Group(selfTable + ".id")
query.Asc("SUM(" + statTable + ".bytes)").
DescPk()
hasOnlyIds = true
query.Asc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
query.Param("times", strings.Join(times, ","))
query.DescPk()
case "trafficOutDesc":
query.Result("id")
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute")
query.Param("day", day)
query.Param("minute", minute)
query.Group(selfTable + ".id")
query.Desc("SUM(" + statTable + ".bytes)").
DescPk()
hasOnlyIds = true
query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
query.Param("times", strings.Join(times, ","))
query.DescPk()
default:
query.DescPk()
}
_, err = query.FindAll()
if hasOnlyIds {
var newResult = []*Server{}
for _, one := range result {
server, err := this.Find(tx, one.Id)
if err != nil {
return nil, err
}
if server == nil {
continue
}
newResult = append(newResult, server.(*Server))
// 修正带宽统计数据
for _, server := range result {
if len(server.BandwidthTime) > 0 && !lists.ContainsString(times, server.BandwidthTime) {
server.BandwidthBytes = 0
}
result = newResult
}
return
@@ -1023,12 +1009,12 @@ func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int6
if server == nil {
return nil, ErrNotFound
}
return this.ComposeServerConfig(tx, server, nil, forNode)
return this.ComposeServerConfig(tx, server, nil, forNode, false)
}
// ComposeServerConfig 构造服务的Config
// forNode 是否是节点请求
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap *utils.CacheMap, forNode bool) (*serverconfigs.ServerConfig, error) {
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap *utils.CacheMap, forNode bool, forList bool) (*serverconfigs.ServerConfig, error) {
if server == nil {
return nil, ErrNotFound
}
@@ -1053,7 +1039,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
var groupConfig *serverconfigs.ServerGroupConfig
for _, groupId := range server.DecodeGroupIds() {
groupConfig1, err := SharedServerGroupDAO.ComposeGroupConfig(tx, groupId, cacheMap)
groupConfig1, err := SharedServerGroupDAO.ComposeGroupConfig(tx, groupId, forList, cacheMap)
if err != nil {
return nil, err
}
@@ -1076,28 +1062,30 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
}
// CNAME
config.SupportCNAME = server.SupportCNAME == 1
if server.ClusterId > 0 && len(server.DnsName) > 0 {
clusterDNS, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, int64(server.ClusterId), cacheMap)
if err != nil {
return nil, err
}
if clusterDNS != nil && clusterDNS.DnsDomainId > 0 {
clusterDNSConfig, err := clusterDNS.DecodeDNSConfig()
if !forList {
config.SupportCNAME = server.SupportCNAME == 1
if server.ClusterId > 0 && len(server.DnsName) > 0 {
clusterDNS, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, int64(server.ClusterId), cacheMap)
if err != nil {
return nil, err
}
if clusterDNS != nil && clusterDNS.DnsDomainId > 0 {
clusterDNSConfig, err := clusterDNS.DecodeDNSConfig()
if err != nil {
return nil, err
}
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, int64(clusterDNS.DnsDomainId), cacheMap)
if err != nil {
return nil, err
}
if domain != nil {
var cname = server.DnsName + "." + domain.Name
config.CNameDomain = cname
if clusterDNSConfig.CNameAsDomain {
config.CNameAsDomain = true
config.AliasServerNames = append(config.AliasServerNames, cname)
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, int64(clusterDNS.DnsDomainId), cacheMap)
if err != nil {
return nil, err
}
if domain != nil {
var cname = server.DnsName + "." + domain.Name
config.CNameDomain = cname
if clusterDNSConfig.CNAMEAsDomain {
config.CNameAsDomain = true
config.AliasServerNames = append(config.AliasServerNames, cname)
}
}
}
}
@@ -1188,61 +1176,71 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
}
// Web
if server.WebId > 0 {
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(server.WebId), cacheMap)
if err != nil {
return nil, err
}
if webConfig != nil {
config.Web = webConfig
if !forList {
if server.WebId > 0 {
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(server.WebId), cacheMap)
if err != nil {
return nil, err
}
if webConfig != nil {
config.Web = webConfig
}
}
}
// ReverseProxy
if IsNotNull(server.ReverseProxy) {
var reverseProxyRef = &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(server.ReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.ReverseProxyRef = reverseProxyRef
if !forList {
if IsNotNull(server.ReverseProxy) {
var reverseProxyRef = &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(server.ReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.ReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.ReverseProxy = reverseProxyConfig
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.ReverseProxy = reverseProxyConfig
}
}
}
// WAF策略
var clusterId = int64(server.ClusterId)
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
if err != nil {
return nil, err
}
if httpFirewallPolicyId > 0 {
config.HTTPFirewallPolicyId = httpFirewallPolicyId
}
// 缓存策略
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId, cacheMap)
if err != nil {
return nil, err
}
if httpCachePolicyId > 0 {
config.HTTPCachePolicyId = httpCachePolicyId
}
// traffic limit
if len(server.TrafficLimit) > 0 {
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
err = json.Unmarshal(server.TrafficLimit, trafficLimitConfig)
if !forList {
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId, cacheMap)
if err != nil {
return nil, err
}
config.TrafficLimit = trafficLimitConfig
if httpFirewallPolicyId > 0 {
config.HTTPFirewallPolicyId = httpFirewallPolicyId
}
}
// 缓存策略
if !forList {
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId, cacheMap)
if err != nil {
return nil, err
}
if httpCachePolicyId > 0 {
config.HTTPCachePolicyId = httpCachePolicyId
}
}
// traffic limit
if !forList {
if len(server.TrafficLimit) > 0 {
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
err := json.Unmarshal(server.TrafficLimit, trafficLimitConfig)
if err != nil {
return nil, err
}
config.TrafficLimit = trafficLimitConfig
}
}
// 用户套餐
@@ -1285,7 +1283,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
if config.TrafficLimit != nil && config.TrafficLimit.IsOn && !config.TrafficLimit.IsEmpty() {
if len(server.TrafficLimitStatus) > 0 {
var status = &serverconfigs.TrafficLimitStatus{}
err = json.Unmarshal(server.TrafficLimitStatus, status)
err := json.Unmarshal(server.TrafficLimitStatus, status)
if err != nil {
return nil, err
}
@@ -1296,14 +1294,16 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
}
// UAM
if teaconst.IsPlus && IsNotNull(server.Uam) {
var uamConfig = &serverconfigs.UAMConfig{}
err = json.Unmarshal(server.Uam, uamConfig)
if err != nil {
return nil, err
}
if uamConfig.IsOn {
config.UAM = uamConfig
if !forList {
if teaconst.IsPlus && IsNotNull(server.Uam) {
var uamConfig = &serverconfigs.UAMConfig{}
err := json.Unmarshal(server.Uam, uamConfig)
if err != nil {
return nil, err
}
if uamConfig.IsOn {
config.UAM = uamConfig
}
}
}
@@ -1629,6 +1629,33 @@ func (this *ServerDAO) GenerateServerDNSName(tx *dbs.Tx, serverId int64) (string
return dnsName, nil
}
// UpdateServerDNSName 设置CNAME
func (this *ServerDAO) UpdateServerDNSName(tx *dbs.Tx, serverId int64, dnsName string) error {
if serverId <= 0 || len(dnsName) == 0 {
return nil
}
dnsName = strings.ToLower(dnsName)
err := this.Query(tx).
Pk(serverId).
Set("dnsName", dnsName).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyDNSUpdate(tx, serverId)
}
// FindServerIdWithDNSName 根据CNAME查询服务ID
func (this *ServerDAO) FindServerIdWithDNSName(tx *dbs.Tx, clusterId int64, dnsName string) (int64, error) {
return this.Query(tx).
ResultPk().
State(ServerStateEnabled).
Attr("clusterId", clusterId).
Attr("dnsName", dnsName).
FindInt64Col(0)
}
// FindServerClusterId 查询当前服务的集群ID
func (this *ServerDAO) FindServerClusterId(tx *dbs.Tx, serverId int64) (int64, error) {
return this.Query(tx).
@@ -1867,8 +1894,8 @@ func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, protocolFam
}
// ExistServerNameInCluster 检查ServerName是否已存在
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64) (bool, error) {
query := this.Query(tx).
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64, supportWildcard bool) (bool, error) {
var query = this.Query(tx).
Attr("clusterId", clusterId).
Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))").
Param("jsonQuery1", maps.Map{"name": serverName}.AsJSON()).
@@ -1877,7 +1904,38 @@ func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, ser
query.Neq("id", excludeServerId)
}
query.State(ServerStateEnabled)
return query.Exist()
exists, err := query.Exist()
if err != nil || exists {
return exists, err
}
if supportWildcard {
var countPieces = strings.Count(serverName, ".")
for {
var index = strings.Index(serverName, ".")
if index > 0 {
serverName = serverName[index+1:]
var search = strings.Repeat("*.", countPieces-strings.Count(serverName, ".")) + serverName
var query = this.Query(tx).
Attr("clusterId", clusterId).
Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))").
Param("jsonQuery1", maps.Map{"name": search}.AsJSON()).
Param("jsonQuery2", maps.Map{"subNames": search}.AsJSON())
if excludeServerId > 0 {
query.Neq("id", excludeServerId)
}
query.State(ServerStateEnabled)
exists, err = query.Exist()
if err != nil || exists {
return exists, err
}
} else {
break
}
}
}
return false, nil
}
// GenDNSName 生成DNS Name
@@ -2549,6 +2607,22 @@ func (this *ServerDAO) FindUserServerClusterIds(tx *dbs.Tx, userId int64) ([]int
return clusterIds, nil
}
// UpdateServerBandwidth 更新服务带宽
// fullTime YYYYMMDDHHII
func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTime string, bandwidthBytes int64) error {
if serverId <= 0 {
return nil
}
if bandwidthBytes < 0 {
bandwidthBytes = 0
}
return this.Query(tx).
Pk(serverId).
Set("bandwidthTime", fullTime).
Set("bandwidthBytes", bandwidthBytes).
UpdateQuickly()
}
// NotifyUpdate 同步服务所在的集群
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
// 创建任务

View File

@@ -141,7 +141,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
var tx *dbs.Tx
{
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "hello.teaos.cn", 0)
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "hello.teaos.cn", 0, true)
if err != nil {
t.Fatal(err)
}
@@ -149,7 +149,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
}
{
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 0)
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 0, true)
if err != nil {
t.Fatal(err)
}
@@ -157,7 +157,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
}
{
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 23)
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 23, true)
if err != nil {
t.Fatal(err)
}

View File

@@ -279,7 +279,7 @@ func (this *ServerGroupDAO) InitGroupWeb(tx *dbs.Tx, groupId int64) (int64, erro
}
// ComposeGroupConfig 组合配置
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap *utils.CacheMap) (*serverconfigs.ServerGroupConfig, error) {
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, forList bool, cacheMap *utils.CacheMap) (*serverconfigs.ServerGroupConfig, error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
@@ -315,65 +315,67 @@ func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheM
IsOn: group.IsOn,
}
if IsNotNull(group.HttpReverseProxy) {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(group.HttpReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.HTTPReverseProxyRef = reverseProxyRef
if !forList {
if IsNotNull(group.HttpReverseProxy) {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(group.HttpReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.HTTPReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.HTTPReverseProxy = reverseProxyConfig
}
}
if reverseProxyConfig != nil {
config.HTTPReverseProxy = reverseProxyConfig
}
}
if IsNotNull(group.TcpReverseProxy) {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(group.TcpReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.TCPReverseProxyRef = reverseProxyRef
if IsNotNull(group.TcpReverseProxy) {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(group.TcpReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.TCPReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.TCPReverseProxy = reverseProxyConfig
}
}
if reverseProxyConfig != nil {
config.TCPReverseProxy = reverseProxyConfig
}
}
if IsNotNull(group.UdpReverseProxy) {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(group.UdpReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.UDPReverseProxyRef = reverseProxyRef
if IsNotNull(group.UdpReverseProxy) {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal(group.UdpReverseProxy, reverseProxyRef)
if err != nil {
return nil, err
}
config.UDPReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.UDPReverseProxy = reverseProxyConfig
}
}
if reverseProxyConfig != nil {
config.UDPReverseProxy = reverseProxyConfig
}
}
// web
if group.WebId > 0 {
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(group.WebId), cacheMap)
if err != nil {
return nil, err
}
if webConfig != nil {
config.Web = webConfig
// web
if group.WebId > 0 {
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(group.WebId), cacheMap)
if err != nil {
return nil, err
}
if webConfig != nil {
config.Web = webConfig
}
}
}

View File

@@ -48,53 +48,57 @@ type Server struct {
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
LastUserPlanId uint32 `field:"lastUserPlanId"` // 上一次使用的套餐
Uam dbs.JSON `field:"uam"` // UAM设置
BandwidthTime string `field:"bandwidthTime"` // 带宽更新时间YYYYMMDDHHII
BandwidthBytes uint64 `field:"bandwidthBytes"` // 最近带宽峰值
}
type ServerOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
UserId interface{} // 用户ID
AdminId interface{} // 管理员ID
Type interface{} // 服务类型
Name interface{} // 名称
Description interface{} // 描述
PlainServerNames interface{} // 扁平化域名列表
ServerNames interface{} // 域名列表
AuditingAt interface{} // 审核提交时间
AuditingServerNames interface{} // 审核中的域名
IsAuditing interface{} // 是否正在审核
AuditingResult interface{} // 审核结果
Http interface{} // HTTP配置
Https interface{} // HTTPS配置
Tcp interface{} // TCP配置
Tls interface{} // TLS配置
Unix interface{} // Unix配置
Udp interface{} // UDP配置
WebId interface{} // WEB配置
ReverseProxy interface{} // 反向代理配置
GroupIds interface{} // 分组ID列表
Config interface{} // 服务配置,自动生成
ConfigMd5 interface{} // Md5
ClusterId interface{} // 集群ID
IncludeNodes interface{} // 部署条件
ExcludeNodes interface{} // 节点排除条件
Version interface{} // 版本号
CreatedAt interface{} // 创建时间
State interface{} // 状态
DnsName interface{} // DNS名称
TcpPorts interface{} // 所包含TCP端口
UdpPorts interface{} // 所包含UDP端口
SupportCNAME interface{} // 允许CNAME不在域名名单
TrafficLimit interface{} // 流量限制
TrafficDay interface{} // YYYYMMDD
TrafficMonth interface{} // YYYYMM
TotalDailyTraffic interface{} // 日流量
TotalMonthlyTraffic interface{} // 月流量
TrafficLimitStatus interface{} // 流量限制状态
TotalTraffic interface{} // 总流量
UserPlanId interface{} // 所属套餐ID
LastUserPlanId interface{} // 上一次使用的套餐
Uam interface{} // UAM设置
Id any // ID
IsOn any // 是否启用
UserId any // 用户ID
AdminId any // 管理员ID
Type any // 服务类型
Name any // 名称
Description any // 描述
PlainServerNames any // 扁平化域名列表
ServerNames any // 域名列表
AuditingAt any // 审核提交时间
AuditingServerNames any // 审核中的域名
IsAuditing any // 是否正在审核
AuditingResult any // 审核结果
Http any // HTTP配置
Https any // HTTPS配置
Tcp any // TCP配置
Tls any // TLS配置
Unix any // Unix配置
Udp any // UDP配置
WebId any // WEB配置
ReverseProxy any // 反向代理配置
GroupIds any // 分组ID列表
Config any // 服务配置,自动生成
ConfigMd5 any // Md5
ClusterId any // 集群ID
IncludeNodes any // 部署条件
ExcludeNodes any // 节点排除条件
Version any // 版本号
CreatedAt any // 创建时间
State any // 状态
DnsName any // DNS名称
TcpPorts any // 所包含TCP端口
UdpPorts any // 所包含UDP端口
SupportCNAME any // 允许CNAME不在域名名单
TrafficLimit any // 流量限制
TrafficDay any // YYYYMMDD
TrafficMonth any // YYYYMM
TotalDailyTraffic any // 日流量
TotalMonthlyTraffic any // 月流量
TrafficLimitStatus any // 流量限制状态
TotalTraffic any // 总流量
UserPlanId any // 所属套餐ID
LastUserPlanId any // 上一次使用的套餐
Uam any // UAM设置
BandwidthTime any // 带宽更新时间YYYYMMDDHHII
BandwidthBytes any // 最近带宽峰值
}
func NewServerOperator() *ServerOperator {

View File

@@ -149,7 +149,7 @@ func (this *SSLCertDAO) UpdateCert(tx *dbs.Tx,
return nil
}
var oldCert = oldOne.(*SSLCert)
var dataIsChanged = bytes.Compare(certData, oldCert.CertData) != 0 || bytes.Compare(keyData, oldCert.KeyData) != 0
var dataIsChanged = !bytes.Equal(certData, oldCert.CertData) || !bytes.Equal(keyData, oldCert.KeyData)
var op = NewSSLCertOperator()
op.Id = certId
@@ -358,7 +358,7 @@ func (this *SSLCertDAO) FindAllExpiringCerts(tx *dbs.Tx, days int) (result []*SS
days = 0
}
deltaSeconds := int64(days * 86400)
var deltaSeconds = int64(days * 86400)
_, err = this.Query(tx).
State(SSLCertStateEnabled).
Where("FROM_UNIXTIME(timeEndAt, '%Y-%m-%d')=:day AND FROM_UNIXTIME(notifiedAt, '%Y-%m-%d')!=:today").

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/zero"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
@@ -13,6 +14,7 @@ import (
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
"time"
)
type SysSettingDAO dbs.DAO
@@ -211,6 +213,18 @@ func (this *SysSettingDAO) NotifyUpdate(tx *dbs.Tx, code string) error {
switch code {
case systemconfigs.SettingCodeAccessLogQueue:
accessLogQueueChanged <- zero.New()
case systemconfigs.SettingCodeAdminUIConfig:
// 修改当前时区
config, err := this.ReadAdminUIConfig(nil, nil)
if err == nil && config != nil {
if len(config.TimeZone) == 0 {
config.TimeZone = nodeconfigs.DefaultTimeZoneLocation
}
location, err := time.LoadLocation(config.TimeZone)
if err == nil && time.Local != location {
time.Local = location
}
}
}
return nil
}

View File

@@ -106,7 +106,7 @@ func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInDay(tx *dbs.Tx, userId
// Clean 清理过期数据
func (this *UserBandwidthStatDAO) Clean(tx *dbs.Tx) error {
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -62)) // 保留大约2个月的数据
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -100)) // 保留大约3个月的数据
return this.runBatch(func(table string, locker *sync.Mutex) error {
_, err := this.Query(tx).
Table(table).

View File

@@ -10,6 +10,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string"
timeutil "github.com/iwind/TeaGo/utils/time"
@@ -352,7 +353,7 @@ func (this *UserDAO) FindUserClusterId(tx *dbs.Tx, userId int64) (int64, error)
FindInt64Col(0)
}
// UpdateUserFeatures 更新用户Features
// UpdateUserFeatures 更新单个用户Features
func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON []byte) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -370,6 +371,74 @@ func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON [
return nil
}
// UpdateUsersFeatures 更新所有用户的Features
func (this *UserDAO) UpdateUsersFeatures(tx *dbs.Tx, featureCodes []string, overwrite bool) error {
if featureCodes == nil {
featureCodes = []string{}
}
if overwrite {
featureCodesJSON, err := json.Marshal(featureCodes)
if err != nil {
return err
}
err = this.Query(tx).
State(UserStateEnabled).
Set("features", featureCodesJSON).
UpdateQuickly()
return err
}
var lastId int64
const size = 1000
for {
ones, _, err := this.Query(tx).
Result("id", "features").
State(UserStateEnabled).
Gt("id", lastId).
Limit(size).
AscPk().
FindOnes()
if err != nil {
return err
}
for _, one := range ones {
var userId = one.GetInt64("id")
var userFeaturesJSON = one.GetBytes("features")
var userFeatures = []string{}
if len(userFeaturesJSON) > 0 {
err = json.Unmarshal(userFeaturesJSON, &userFeatures)
if err != nil {
return err
}
}
for _, featureCode := range featureCodes {
if !lists.ContainsString(userFeatures, featureCode) {
userFeatures = append(userFeatures, featureCode)
}
}
userFeaturesJSON, err = json.Marshal(userFeatures)
if err != nil {
return err
}
err = this.Query(tx).
Pk(userId).
Set("features", userFeaturesJSON).
UpdateQuickly()
if err != nil {
return err
}
}
if len(ones) < size {
break
}
lastId += size
}
return nil
}
// FindUserFeatures 查找用户Features
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*userconfigs.UserFeature, error) {
featuresJSON, err := this.Query(tx).

View File

@@ -1,5 +1,20 @@
package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
)
func TestUserDAO_UpdateUserFeatures(t *testing.T) {
var dao = NewUserDAO()
var tx *dbs.Tx
err := dao.UpdateUsersFeatures(tx, []string{
userconfigs.UserFeatureCodeServerACME,
}, false)
if err != nil {
t.Fatal(err)
}
}

View File

@@ -27,32 +27,34 @@ type User struct {
RejectReason string `field:"rejectReason"` // 拒绝理由
IsVerified bool `field:"isVerified"` // 是否验证通过
RequirePlans uint8 `field:"requirePlans"` // 是否需要购买套餐
Modules dbs.JSON `field:"modules"` // 用户模块
}
type UserOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
Username interface{} // 用户名
Password interface{} // 密码
Fullname interface{} // 真实姓名
Mobile interface{} // 手机号
Tel interface{} // 联系电话
Remark interface{} // 备注
Email interface{} // 邮箱地址
EmailIsVerified interface{} // 邮箱是否已验证
AvatarFileId interface{} // 头像文件ID
CreatedAt interface{} // 创建时间
Day interface{} // YYYYMMDD
UpdatedAt interface{} // 修改时间
State interface{} // 状态
Source interface{} // 来源
ClusterId interface{} // 集群ID
Features interface{} // 允许操作的特征
RegisteredIP interface{} // 注册使用的IP
IsRejected interface{} // 是否已拒绝
RejectReason interface{} // 拒绝理由
IsVerified interface{} // 是否验证通过
RequirePlans interface{} // 是否需要购买套餐
Id any // ID
IsOn any // 是否启用
Username any // 用户名
Password any // 密码
Fullname any // 真实姓名
Mobile any // 手机号
Tel any // 联系电话
Remark any // 备注
Email any // 邮箱地址
EmailIsVerified any // 邮箱是否已验证
AvatarFileId any // 头像文件ID
CreatedAt any // 创建时间
Day any // YYYYMMDD
UpdatedAt any // 修改时间
State any // 状态
Source any // 来源
ClusterId any // 集群ID
Features any // 允许操作的特征
RegisteredIP any // 注册使用的IP
IsRejected any // 是否已拒绝
RejectReason any // 拒绝理由
IsVerified any // 是否验证通过
RequirePlans any // 是否需要购买套餐
Modules any // 用户模块
}
func NewUserOperator() *UserOperator {

View File

@@ -1 +1,21 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
)
// DecodeModules 解析模块
func (this *User) DecodeModules() []string {
if len(this.Modules) == 0 {
return nil
}
var result = []string{}
err := json.Unmarshal(this.Modules, &result)
if err != nil {
remotelogs.Error("User.DecodeModules", err.Error())
}
return result
}

View File

@@ -300,7 +300,7 @@ func (this *UserNodeDAO) CountAllEnabledUserNodesWithSSLPolicyIds(tx *dbs.Tx, ss
if len(sslPolicyIds) == 0 {
return
}
policyStringIds := []string{}
var policyStringIds = []string{}
for _, policyId := range sslPolicyIds {
policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10))
}
@@ -310,3 +310,21 @@ func (this *UserNodeDAO) CountAllEnabledUserNodesWithSSLPolicyIds(tx *dbs.Tx, ss
Param("policyIds", strings.Join(policyStringIds, ",")).
Count()
}
// FindUserNodeAccessAddr 获取用户节点访问地址
func (this *UserNodeDAO) FindUserNodeAccessAddr(tx *dbs.Tx) (string, error) {
nodes, err := this.ListEnabledUserNodes(tx, 0, 100)
if err != nil {
return "", err
}
for _, node := range nodes {
addrs, err := node.DecodeAccessAddrStrings()
if err != nil {
continue
}
if len(addrs) > 0 {
return addrs[0], nil
}
}
return "", nil
}

View File

@@ -22,21 +22,21 @@ type UserNode struct {
}
type UserNodeOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
UniqueId interface{} // 唯一ID
Secret interface{} // 密钥
Name interface{} // 名称
Description interface{} // 描述
Http interface{} // 监听的HTTP配置
Https interface{} // 监听的HTTPS配置
AccessAddrs interface{} // 外部访问地址
Order interface{} // 排序
State interface{} // 状态
CreatedAt interface{} // 创建时间
AdminId interface{} // 管理员ID
Weight interface{} // 权重
Status interface{} // 运行状态
Id any // ID
IsOn any // 是否启用
UniqueId any // 唯一ID
Secret any // 密钥
Name any // 名称
Description any // 描述
Http any // 监听的HTTP配置
Https any // 监听的HTTPS配置
AccessAddrs any // 外部访问地址
Order any // 排序
State any // 状态
CreatedAt any // 创建时间
AdminId any // 管理员ID
Weight any // 权重
Status any // 运行状态
}
func NewUserNodeOperator() *UserNodeOperator {

View File

@@ -35,3 +35,37 @@ func QuoteLikePrefix(keyword string) string {
func QuoteLikeSuffix(keyword string) string {
return "%" + QuoteLikeKeyword(keyword)
}
// SetGlobalVarMin 设置变量最小值
func SetGlobalVarMin(db *dbs.DB, variableName string, minValue int) error {
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name=?", variableName)
if err != nil {
return err
}
if len(result) == 0 {
return nil
}
var oldValue = result.GetInt("Value")
if oldValue > 0 /** 小于等于0通常表示不限制 **/ && oldValue < minValue {
_, err = db.Exec("SET GLOBAL "+variableName+"=?", minValue)
return err
}
return nil
}
// SetGlobalVarMax 设置变量最大值
func SetGlobalVarMax(db *dbs.DB, variableName string, maxValue int) error {
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name=?", variableName)
if err != nil {
return err
}
if len(result) == 0 {
return nil
}
var oldValue = result.GetInt("Value")
if oldValue > maxValue {
_, err = db.Exec("SET GLOBAL "+variableName+"=?", maxValue)
return err
}
return nil
}

View File

@@ -0,0 +1,21 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
import (
"errors"
"github.com/iwind/TeaGo/types"
)
type BaseResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (this *BaseResponse) IsValid() bool {
return this.Code == 200
}
func (this *BaseResponse) Error() error {
return errors.New("code: " + types.String(this.Code) + ", message: " + this.Message)
}

View File

@@ -0,0 +1,11 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type CreateNSRecordResponse struct {
BaseResponse
Data struct {
NSRecordId int64 `json:"nsRecordId"`
} `json:"data"`
}

View File

@@ -0,0 +1,14 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type FindAllNSRoutesResponse struct {
BaseResponse
Data struct {
NSRoutes []struct {
Name string `json:"name"`
Code string `json:"code"`
} `json:"nsRoutes"`
} `json:"data"`
}

View File

@@ -0,0 +1,14 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type FindDomainWithNameResponse struct {
BaseResponse
Data struct {
NSDomain struct {
Id int64 `json:"id"`
Name string `json:"name"`
}
} `json:"data"`
}

View File

@@ -0,0 +1,21 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type FindNSRecordWithNameAndTypeResponse struct {
BaseResponse
Data struct {
NSRecord struct {
Id int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
TTL int32 `json:"ttl"`
NSRoutes []struct {
Name string `json:"name"`
Code string `json:"code"`
} `json:"nsRoutes"`
} `json:"nsRecord"`
}
}

View File

@@ -0,0 +1,12 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type GetAPIAccessToken struct {
BaseResponse
Data struct {
Token string `json:"token"`
ExpiresAt int64 `json:"expiresAt"`
} `json:"data"`
}

View File

@@ -0,0 +1,8 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type ResponseInterface interface {
IsValid() bool
Error() error
}

View File

@@ -0,0 +1,16 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type ListNSDomainsResponse struct {
BaseResponse
Data struct {
NSDomains []struct {
Id int64 `json:"id"`
Name string `json:"name"`
IsOn bool `json:"isOn"`
IsDeleted bool `json:"isDeleted"`
} `json:"nsDomains"`
} `json:"data"`
}

View File

@@ -0,0 +1,21 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type ListNSRecordsResponse struct {
BaseResponse
Data struct {
NSRecords []struct {
Id int64 `json:"id"`
Name string `json:"name"`
Value string `json:"value"`
TTL int32 `json:"ttl"`
Type string `json:"type"`
NSRoutes []struct {
Name string `json:"name"`
Code string `json:"code"`
} `json:"nsRoutes"`
} `json:"nsRecords"`
} `json:"data"`
}

View File

@@ -0,0 +1,7 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type SuccessResponse struct {
BaseResponse
}

View File

@@ -0,0 +1,7 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package edgeapi
type UpdateNSRecordResponse struct {
BaseResponse
}

View File

@@ -55,8 +55,8 @@ func (this *DNSPodProvider) Auth(params maps.Map) error {
// GetDomains 获取所有域名列表
func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
offset := 0
size := 100
var offset = 0
var size = 100
for {
domainsResp, err := this.post("/Domain.List", map[string]string{
@@ -68,7 +68,7 @@ func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
}
offset += size
domainsSlice := domainsResp.GetSlice("domains")
var domainsSlice = domainsResp.GetSlice("domains")
if len(domainsSlice) == 0 {
break
}
@@ -79,8 +79,8 @@ func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
}
// 检查是否到头
info := domainsResp.GetMap("info")
recordTotal := info.GetInt("record_total")
var info = domainsResp.GetMap("info")
var recordTotal = info.GetInt("all_total")
if offset >= recordTotal {
break
}
@@ -90,8 +90,8 @@ func (this *DNSPodProvider) GetDomains() (domains []string, err error) {
// GetRecords 获取域名列表
func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
offset := 0
size := 100
var offset = 0
var size = 100
for {
recordsResp, err := this.post("/Record.List", map[string]string{
"domain": domain,
@@ -104,7 +104,7 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Recor
offset += size
// 记录
recordSlice := recordsResp.GetSlice("records")
var recordSlice = recordsResp.GetSlice("records")
for _, record := range recordSlice {
recordMap := maps.NewMap(record)
records = append(records, &dnstypes.Record{
@@ -118,8 +118,8 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Recor
}
// 检查是否到头
info := recordsResp.GetMap("info")
recordTotal := info.GetInt("record_total")
var info = recordsResp.GetMap("info")
var recordTotal = info.GetInt("record_total")
if offset >= recordTotal {
break
}

View File

@@ -0,0 +1,441 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dnsclients
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/edgeapi"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"io"
"net/http"
"regexp"
"strings"
"time"
)
var edgeDNSHTTPClient = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
type EdgeDNSAPIProvider struct {
host string
accessKeyId string
accessKeySecret string
role string // admin | user
accessToken string
accessTokenExpiresAt int64
}
// Auth 认证
func (this *EdgeDNSAPIProvider) Auth(params maps.Map) error {
this.role = params.GetString("role")
this.host = params.GetString("host")
this.accessKeyId = params.GetString("accessKeyId")
this.accessKeySecret = params.GetString("accessKeySecret")
if len(this.role) == 0 {
this.role = "user"
}
if len(this.host) == 0 {
return errors.New("'host' should not be empty")
}
if !regexp.MustCompile(`^(?i)(http|https):`).MatchString(this.host) {
this.host = "http://" + this.host
}
if len(this.accessKeyId) == 0 {
return errors.New("'accessKeyId' should not be empty")
}
if len(this.accessKeySecret) == 0 {
return errors.New("'accessKeySecret' should not be empty")
}
return nil
}
// GetDomains 获取所有域名列表
func (this *EdgeDNSAPIProvider) GetDomains() (domains []string, err error) {
var offset = 0
var size = 100
for {
var resp = &edgeapi.ListNSDomainsResponse{}
err = this.doAPI("/NSDomainService/ListNSDomains", map[string]any{
"offset": offset,
"size": size,
}, resp)
if err != nil {
return
}
for _, domain := range resp.Data.NSDomains {
domains = append(domains, domain.Name)
}
if len(resp.Data.NSDomains) < size {
break
}
offset += size
}
return
}
// GetRecords 获取域名解析记录列表
func (this *EdgeDNSAPIProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
var domainResp = &edgeapi.FindDomainWithNameResponse{}
err = this.doAPI("/NSDomainService/FindNSDomainWithName", map[string]any{
"name": domain,
}, domainResp)
if err != nil {
return nil, err
}
var domainId = domainResp.Data.NSDomain.Id
if domainId == 0 {
return nil, nil
}
var offset = 0
var size = 100
for {
var recordsResp = &edgeapi.ListNSRecordsResponse{}
err = this.doAPI("/NSRecordService/ListNSRecords", map[string]any{
"nsDomainId": domainId,
"offset": offset,
"size": size,
}, recordsResp)
if err != nil {
return nil, err
}
var nsRecords = recordsResp.Data.NSRecords
for _, record := range nsRecords {
var routeCode = this.DefaultRoute()
if len(record.NSRoutes) > 0 {
routeCode = record.NSRoutes[0].Code
}
records = append(records, &dnstypes.Record{
Id: types.String(record.Id),
Name: record.Name,
Type: record.Type,
Value: record.Value,
Route: routeCode,
TTL: record.TTL,
})
}
if len(nsRecords) < size {
break
}
offset += size
}
return
}
// GetRoutes 读取域名支持的线路数据
func (this *EdgeDNSAPIProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
// default
routes = append(routes, &dnstypes.Route{
Name: "默认线路",
Code: this.DefaultRoute(),
})
// 世界区域
{
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
err = this.doAPI("/NSRouteService/FindAllDefaultWorldRegionRoutes", map[string]any{}, routesResp)
if err != nil {
return nil, err
}
for _, route := range routesResp.Data.NSRoutes {
routes = append(routes, &dnstypes.Route{
Name: route.Name,
Code: route.Code,
})
}
}
// 中国省份
{
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
err = this.doAPI("/NSRouteService/FindAllDefaultChinaProvinceRoutes", map[string]any{}, routesResp)
if err != nil {
return nil, err
}
for _, route := range routesResp.Data.NSRoutes {
routes = append(routes, &dnstypes.Route{
Name: route.Name,
Code: route.Code,
})
}
}
// ISP
{
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
err = this.doAPI("/NSRouteService/FindAllDefaultISPRoutes", map[string]any{}, routesResp)
if err != nil {
return nil, err
}
for _, route := range routesResp.Data.NSRoutes {
routes = append(routes, &dnstypes.Route{
Name: route.Name,
Code: route.Code,
})
}
}
// 自定义
{
var routesResp = &edgeapi.FindAllNSRoutesResponse{}
err = this.doAPI("/NSRouteService/FindAllNSRoutes", map[string]any{}, routesResp)
if err != nil {
return nil, err
}
for _, route := range routesResp.Data.NSRoutes {
routes = append(routes, &dnstypes.Route{
Name: route.Name,
Code: route.Code,
})
}
}
return
}
// QueryRecord 查询单个记录
func (this *EdgeDNSAPIProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
var domainResp = &edgeapi.FindDomainWithNameResponse{}
err := this.doAPI("/NSDomainService/FindNSDomainWithName", map[string]any{
"name": domain,
}, domainResp)
if err != nil {
return nil, err
}
var domainId = domainResp.Data.NSDomain.Id
if domainId == 0 {
return nil, errors.New("can not find domain '" + domain + "'")
}
var recordResp = &edgeapi.FindNSRecordWithNameAndTypeResponse{}
err = this.doAPI("/NSRecordService/FindNSRecordWithNameAndType", map[string]any{
"nsDomainId": domainId,
"name": name,
"type": recordType,
}, recordResp)
if err != nil {
return nil, err
}
var record = recordResp.Data.NSRecord
if record.Id <= 0 {
return nil, nil
}
var routeCode = this.DefaultRoute()
if len(record.NSRoutes) > 0 {
routeCode = record.NSRoutes[0].Code
}
return &dnstypes.Record{
Id: types.String(record.Id),
Name: record.Name,
Type: record.Type,
Value: record.Value,
Route: routeCode,
TTL: record.TTL,
}, nil
}
// AddRecord 设置记录
func (this *EdgeDNSAPIProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
var domainResp = &edgeapi.FindDomainWithNameResponse{}
err := this.doAPI("/NSDomainService/FindNSDomainWithName", map[string]any{
"name": domain,
}, domainResp)
if err != nil {
return err
}
var domainId = domainResp.Data.NSDomain.Id
if domainId == 0 {
return errors.New("can not find domain '" + domain + "'")
}
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
newRecord.Value += "."
}
var createResp = &edgeapi.CreateNSRecordResponse{}
var routes = []string{}
if len(newRecord.Route) > 0 {
routes = []string{newRecord.Route}
}
err = this.doAPI("/NSRecordService/CreateNSRecord", map[string]any{
"nsDomainId": domainId,
"name": newRecord.Name,
"type": strings.ToUpper(newRecord.Type),
"value": newRecord.Value,
"ttl": newRecord.TTL,
"nsRouteCodes": routes,
}, createResp)
if err != nil {
return err
}
newRecord.Id = types.String(createResp.Data.NSRecordId)
return nil
}
// UpdateRecord 修改记录
func (this *EdgeDNSAPIProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
newRecord.Value += "."
}
var createResp = &edgeapi.UpdateNSRecordResponse{}
var routes = []string{}
if len(newRecord.Route) > 0 {
routes = []string{newRecord.Route}
}
err := this.doAPI("/NSRecordService/UpdateNSRecord", map[string]any{
"nsRecordId": types.Int64(record.Id),
"name": newRecord.Name,
"type": strings.ToUpper(newRecord.Type),
"value": newRecord.Value,
"ttl": newRecord.TTL,
"nsRouteCodes": routes,
"isOn": true, // important
}, createResp)
return err
}
// DeleteRecord 删除记录
func (this *EdgeDNSAPIProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
var resp = &edgeapi.SuccessResponse{}
err := this.doAPI("/NSRecordService/DeleteNSRecord", map[string]any{
"nsRecordId": types.Int64(record.Id),
}, resp)
return err
}
// DefaultRoute 默认线路
func (this *EdgeDNSAPIProvider) DefaultRoute() string {
return "default"
}
func (this *EdgeDNSAPIProvider) doAPI(path string, params map[string]any, respPtr edgeapi.ResponseInterface) error {
accessToken, err := this.getToken()
if err != nil {
return err
}
paramsJSON, err := json.Marshal(params)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, this.host+path, bytes.NewReader(paramsJSON))
if err != nil {
return err
}
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
req.Header.Set("X-Edge-Access-Token", accessToken)
resp, err := edgeDNSHTTPClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New("invalid response status code '" + types.String(resp.StatusCode) + "'")
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(data, respPtr)
if err != nil {
return errors.New("decode response failed: " + err.Error() + ", JSON: " + string(data))
}
if !respPtr.IsValid() {
return respPtr.Error()
}
return err
}
func (this *EdgeDNSAPIProvider) getToken() (string, error) {
if len(this.accessToken) > 0 && this.accessTokenExpiresAt > time.Now().Unix()+600 /** 600秒是防止当前服务器和API服务器之间有时间差 **/ {
return this.accessToken, nil
}
var params = maps.Map{
"type": this.role,
"accessKeyId": this.accessKeyId,
"accessKey": this.accessKeySecret,
}
paramsJSON, err := json.Marshal(params)
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPost, this.host+"/APIAccessTokenService/getAPIAccessToken", bytes.NewReader(paramsJSON))
if err != nil {
return "", err
}
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
resp, err := edgeDNSHTTPClient.Do(req)
if err != nil {
return "", err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
return "", errors.New("invalid response code '" + types.String(resp.StatusCode) + "'")
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var tokenResp = &edgeapi.GetAPIAccessToken{}
err = json.Unmarshal(data, tokenResp)
if err != nil {
return "", err
}
if tokenResp.Code != 200 {
return "", errors.New("invalid code '" + types.String(tokenResp.Code) + "', message: " + tokenResp.Message)
}
this.accessToken = tokenResp.Data.Token
this.accessTokenExpiresAt = tokenResp.Data.ExpiresAt
return this.accessToken, nil
}

View File

@@ -0,0 +1,163 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dnsclients_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"testing"
)
const edgeDNSAPIDomainName = "hello2.com"
func TestEdgeDNSAPIProvider_GetDomains(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
domains, err := provider.GetDomains()
if err != nil {
t.Fatal(err)
}
t.Log("domains:", domains)
}
func TestEdgeDNSAPIProvider_GetRecords(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
records, err := provider.GetRecords(edgeDNSAPIDomainName)
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(records, t)
}
func TestEdgeDNSAPIProvider_GetRoutes(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
routes, err := provider.GetRoutes(edgeDNSAPIDomainName)
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(routes, t)
}
func TestEdgeDNSAPIProvider_QueryRecord(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
record, err := provider.QueryRecord(edgeDNSAPIDomainName, "cdn", dnstypes.RecordTypeA)
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(record)
}
func TestEdgeDNSAPIProvider_AddRecord(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
err = provider.AddRecord(edgeDNSAPIDomainName, &dnstypes.Record{
Id: "",
Name: "example",
Type: dnstypes.RecordTypeA,
Value: "10.0.0.1",
Route: "china:province:beijing",
TTL: 300,
})
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestEdgeDNSAPIProvider_UpdateRecord(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
record, err := provider.QueryRecord(edgeDNSAPIDomainName, "cdn", dnstypes.RecordTypeA)
if err != nil {
t.Fatal(err)
}
if record == nil {
t.Log("not found record")
return
}
//record.Id = ""
err = provider.UpdateRecord(edgeDNSAPIDomainName, record, &dnstypes.Record{
Id: "",
Name: record.Name,
Type: record.Type,
Value: "127.0.0.3",
Route: record.Route,
TTL: 30,
})
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestEdgeDNSAPIProvider_DeleteRecord(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
record, err := provider.QueryRecord(edgeDNSAPIDomainName, "example", "A")
if err != nil {
t.Fatal(err)
}
if record == nil {
t.Log("not found")
return
}
err = provider.DeleteRecord(edgeDNSAPIDomainName, &dnstypes.Record{
Id: record.Id,
Name: "example",
Type: "A",
Value: "",
Route: "",
})
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestEdgeDNSAPIProvider_DefaultRoute(t *testing.T) {
provider, err := testEdgeDNSAPIProvider()
if err != nil {
t.Fatal(err)
}
t.Log(provider.DefaultRoute())
}
func testEdgeDNSAPIProvider() (dnsclients.ProviderInterface, error) {
provider := &dnsclients.EdgeDNSAPIProvider{}
err := provider.Auth(maps.Map{
"role": "user",
"host": "http://127.0.0.1:8004",
"accessKeyId": "JOvsyXIFqkQbh5kl",
"accessKeySecret": "t0RY8YO3R58VbJJNp0RqKw9KWNpObwtE",
})
if err != nil {
return nil, err
}
return provider, nil
}

View File

@@ -1,65 +0,0 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dnsclients
import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/iwind/TeaGo/maps"
)
type UserEdgeDNSProvider struct {
}
// Auth 认证
func (this *UserEdgeDNSProvider) Auth(params maps.Map) error {
// TODO
return nil
}
// GetDomains 获取所有域名列表
func (this *UserEdgeDNSProvider) GetDomains() (domains []string, err error) {
// TODO
return
}
// GetRecords 获取域名解析记录列表
func (this *UserEdgeDNSProvider) GetRecords(domain string) (records []*dnstypes.Record, err error) {
// TODO
return
}
// GetRoutes 读取域名支持的线路数据
func (this *UserEdgeDNSProvider) GetRoutes(domain string) (routes []*dnstypes.Route, err error) {
// TODO
return
}
// QueryRecord 查询单个记录
func (this *UserEdgeDNSProvider) QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error) {
// TODO
return nil, nil
}
// AddRecord 设置记录
func (this *UserEdgeDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
// TODO
return nil
}
// UpdateRecord 修改记录
func (this *UserEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.Record, newRecord *dnstypes.Record) error {
// TODO
return nil
}
// DeleteRecord 删除记录
func (this *UserEdgeDNSProvider) DeleteRecord(domain string, record *dnstypes.Record) error {
// TODO
return nil
}
// DefaultRoute 默认线路
func (this *UserEdgeDNSProvider) DefaultRoute() string {
// TODO
return ""
}

Some files were not shown because too many files have changed in this diff Show More