This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
package clients
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
type ClientAgentDAO dbs.DAO
func NewClientAgentDAO() *ClientAgentDAO {
return dbs.NewDAO(&ClientAgentDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeClientAgents",
Model: new(ClientAgent),
PkName: "id",
},
}).(*ClientAgentDAO)
}
var SharedClientAgentDAO *ClientAgentDAO
func init() {
dbs.OnReady(func() {
SharedClientAgentDAO = NewClientAgentDAO()
})
}
// FindClientAgentName 根据主键查找名称
func (this *ClientAgentDAO) FindClientAgentName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// FindAgent 查找Agent
func (this *ClientAgentDAO) FindAgent(tx *dbs.Tx, agentId int64) (*ClientAgent, error) {
if agentId <= 0 {
return nil, nil
}
one, err := this.Query(tx).
Pk(agentId).
Find()
if err != nil || one == nil {
return nil, err
}
return one.(*ClientAgent), nil
}
// FindAgentIdWithCode 根据代号查找ID
func (this *ClientAgentDAO) FindAgentIdWithCode(tx *dbs.Tx, code string) (int64, error) {
return this.Query(tx).
ResultPk().
Attr("code", code).
FindInt64Col(0)
}
// FindAgentNameWithCode 根据代号查找Agent名称
func (this *ClientAgentDAO) FindAgentNameWithCode(tx *dbs.Tx, code string) (string, error) {
return this.Query(tx).
Result("name").
Attr("code", code).
FindStringCol("")
}
// UpdateAgentCountIPs 修改Agent拥有的IP数量
func (this *ClientAgentDAO) UpdateAgentCountIPs(tx *dbs.Tx, agentId int64, countIPs int64) error {
return this.Query(tx).
Pk(agentId).
Set("countIPs", countIPs).
UpdateQuickly()
}
// FindAllAgents 查找所有Agents
func (this *ClientAgentDAO) FindAllAgents(tx *dbs.Tx) (result []*ClientAgent, err error) {
_, err = this.Query(tx).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// FindAllNSAgents 查找所有DNS可以使用的Agents
func (this *ClientAgentDAO) FindAllNSAgents(tx *dbs.Tx) (result []*ClientAgent, err error) {
// 注意允许NS使用所有的Agent不管有没有IP数据
_, err = this.Query(tx).
Result("id", "name", "code").
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}

View File

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

View File

@@ -0,0 +1,105 @@
package clients
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
// TODO 需要定时对所有IP的PTR进行检查剔除已经变更的IP
type ClientAgentIPDAO dbs.DAO
func NewClientAgentIPDAO() *ClientAgentIPDAO {
return dbs.NewDAO(&ClientAgentIPDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeClientAgentIPs",
Model: new(ClientAgentIP),
PkName: "id",
},
}).(*ClientAgentIPDAO)
}
var SharedClientAgentIPDAO *ClientAgentIPDAO
func init() {
dbs.OnReady(func() {
SharedClientAgentIPDAO = NewClientAgentIPDAO()
})
}
// CreateIP 写入IP
func (this *ClientAgentIPDAO) CreateIP(tx *dbs.Tx, agentId int64, ip string, ptr string) error {
// 检查数据有效性
if agentId <= 0 || len(ip) == 0 {
return nil
}
// 限制ptr长度
if len(ptr) > 100 {
ptr = ptr[:100]
}
// 检查是否存在
exists, err := this.Query(tx).
Attr("agentId", agentId).
Attr("ip", ip).
Exist()
if err != nil {
return err
}
if exists {
return nil
}
var op = NewClientAgentIPOperator()
op.AgentId = agentId
op.IP = ip
op.Ptr = ptr
err = this.Save(tx, op)
if err != nil {
// 忽略duplicate错误
if models.CheckSQLDuplicateErr(err) {
return nil
}
return err
}
// 更新Agent IP数量
countIPs, err := this.CountAgentIPs(tx, agentId)
if err != nil {
return err
}
err = SharedClientAgentDAO.UpdateAgentCountIPs(tx, agentId, countIPs)
if err != nil {
return err
}
return nil
}
// ListIPsAfterId 列出某个ID之后的IP
func (this *ClientAgentIPDAO) ListIPsAfterId(tx *dbs.Tx, id int64, size int64) (result []*ClientAgentIP, err error) {
if id < 0 {
id = 0
}
_, err = this.Query(tx).
Result("id", "ip", "ptr", "agentId").
Gt("id", id).
AscPk().
Limit(size). // 限制单次读取个数
Slice(&result).
FindAll()
return
}
// CountAgentIPs 计算Agent IP数量
func (this *ClientAgentIPDAO) CountAgentIPs(tx *dbs.Tx, agentId int64) (int64, error) {
return this.Query(tx).
Attr("agentId", agentId).
Count()
}

View File

@@ -0,0 +1,16 @@
package clients_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"testing"
)
func TestClientAgentIPDAO_CreateIP(t *testing.T) {
var dao = clients.NewClientAgentIPDAO()
err := dao.CreateIP(nil, 1, "127.0.0.1", "")
if err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,20 @@
package clients
// ClientAgentIP Agent IP
type ClientAgentIP struct {
Id uint64 `field:"id"` // ID
AgentId uint32 `field:"agentId"` // Agent ID
IP string `field:"ip"` // IP地址
Ptr string `field:"ptr"` // PTR值
}
type ClientAgentIPOperator struct {
Id any // ID
AgentId any // Agent ID
IP any // IP地址
Ptr any // PTR值
}
func NewClientAgentIPOperator() *ClientAgentIPOperator {
return &ClientAgentIPOperator{}
}

View File

@@ -0,0 +1 @@
package clients

View File

@@ -0,0 +1,24 @@
package clients
// ClientAgent Agent库
type ClientAgent struct {
Id uint32 `field:"id"` // ID
Name string `field:"name"` // 名称
Code string `field:"code"` // 代号
Description string `field:"description"` // 介绍
Order uint32 `field:"order"` // 排序
CountIPs uint32 `field:"countIPs"` // IP数量
}
type ClientAgentOperator struct {
Id any // ID
Name any // 名称
Code any // 代号
Description any // 介绍
Order any // 排序
CountIPs any // IP数量
}
func NewClientAgentOperator() *ClientAgentOperator {
return &ClientAgentOperator{}
}

View File

@@ -0,0 +1,6 @@
package clients
// NSRouteCode NS线路代号
func (this *ClientAgent) NSRouteCode() string {
return "agent:" + this.Code
}

View File

@@ -0,0 +1,155 @@
package clients
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
const (
ClientBrowserStateEnabled = 1 // 已启用
ClientBrowserStateDisabled = 0 // 已禁用
)
func init() {
dbs.OnReadyDone(func() {
// 清理数据任务
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
goman.New(func() {
for range ticker.C {
err := SharedClientBrowserDAO.Clean(nil, 7) // 只保留N天
if err != nil {
remotelogs.Error("SharedClientBrowserDAO", "clean expired data failed: "+err.Error())
}
}
})
})
}
type ClientBrowserDAO dbs.DAO
func NewClientBrowserDAO() *ClientBrowserDAO {
return dbs.NewDAO(&ClientBrowserDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeClientBrowsers",
Model: new(ClientBrowser),
PkName: "id",
},
}).(*ClientBrowserDAO)
}
var SharedClientBrowserDAO *ClientBrowserDAO
func init() {
dbs.OnReady(func() {
SharedClientBrowserDAO = NewClientBrowserDAO()
})
}
// EnableClientBrowser 启用条目
func (this *ClientBrowserDAO) EnableClientBrowser(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientBrowserStateEnabled).
Update()
return err
}
// DisableClientBrowser 禁用条目
func (this *ClientBrowserDAO) DisableClientBrowser(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientBrowserStateDisabled).
Update()
return err
}
// FindEnabledClientBrowser 查找启用中的条目
func (this *ClientBrowserDAO) FindEnabledClientBrowser(tx *dbs.Tx, id int64) (*ClientBrowser, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", ClientBrowserStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*ClientBrowser), err
}
// FindClientBrowserName 根据主键查找名称
func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreateBrowserIfNotExists 创建浏览器信息
func (this *ClientBrowserDAO) CreateBrowserIfNotExists(tx *dbs.Tx, browserName string) error {
const maxlength = 50
if len(browserName) > maxlength {
browserName = browserName[:50]
}
// 检查缓存
var cacheKey = "clientBrowser:" + browserName
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
if cacheItem != nil {
return nil
}
// 检查是否已经存在
// 不需要加状态条件
browserId, err := this.Query(tx).
Attr("name", browserName).
ResultPk().
FindInt64Col(0)
if err != nil {
return err
}
if browserId > 0 {
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
return this.Query(tx).
Pk(browserId).
Set("createdDay", timeutil.Format("Ymd")).
UpdateQuickly()
}
// 如果不存在,则创建之
var op = NewClientBrowserOperator()
op.Name = browserName
op.CreatedDay = timeutil.Format("Ymd")
op.State = ClientBrowserStateEnabled
browserId, err = this.SaveInt64(tx, op)
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
return nil
}
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
if browserId > 0 {
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
}
return err
}
// Clean 清理
func (this *ClientBrowserDAO) Clean(tx *dbs.Tx, days int) error {
if days <= 0 {
days = 30
}
return this.Query(tx).
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
DeleteQuickly()
}

View File

@@ -0,0 +1,33 @@
package clients_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
_ "github.com/go-sql-driver/mysql"
"testing"
)
func TestClientBrowserDAO_CreateBrowser(t *testing.T) {
var dao = clients.NewClientBrowserDAO()
err := dao.CreateBrowserIfNotExists(nil, "Hello")
if err != nil {
t.Fatal(err)
}
err = dao.CreateBrowserIfNotExists(nil, "Hello")
if err != nil {
t.Fatal(err)
}
err = dao.CreateBrowserIfNotExists(nil, "Hello")
if err != nil {
t.Fatal(err)
}
}
func TestClientBrowserDAO_Clean(t *testing.T) {
var dao = clients.NewClientBrowserDAO()
err := dao.Clean(nil, 30)
if err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,24 @@
package clients
import "github.com/iwind/TeaGo/dbs"
// ClientBrowser 终端浏览器信息
type ClientBrowser struct {
Id uint64 `field:"id"` // ID
Name string `field:"name"` // 浏览器名称
Codes dbs.JSON `field:"codes"` // 代号
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
State uint8 `field:"state"` // 状态
}
type ClientBrowserOperator struct {
Id any // ID
Name any // 浏览器名称
Codes any // 代号
CreatedDay any // 创建日期YYYYMMDD
State any // 状态
}
func NewClientBrowserOperator() *ClientBrowserOperator {
return &ClientBrowserOperator{}
}

View File

@@ -0,0 +1 @@
package clients

View File

@@ -0,0 +1,154 @@
package clients
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
const (
ClientSystemStateEnabled = 1 // 已启用
ClientSystemStateDisabled = 0 // 已禁用
)
func init() {
dbs.OnReadyDone(func() {
// 清理数据任务
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
goman.New(func() {
for range ticker.C {
err := SharedClientSystemDAO.Clean(nil, 7) // 只保留N天
if err != nil {
remotelogs.Error("SharedClientSystemDAO", "clean expired data failed: "+err.Error())
}
}
})
})
}
type ClientSystemDAO dbs.DAO
func NewClientSystemDAO() *ClientSystemDAO {
return dbs.NewDAO(&ClientSystemDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeClientSystems",
Model: new(ClientSystem),
PkName: "id",
},
}).(*ClientSystemDAO)
}
var SharedClientSystemDAO *ClientSystemDAO
func init() {
dbs.OnReady(func() {
SharedClientSystemDAO = NewClientSystemDAO()
})
}
// EnableClientSystem 启用条目
func (this *ClientSystemDAO) EnableClientSystem(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientSystemStateEnabled).
Update()
return err
}
// DisableClientSystem 禁用条目
func (this *ClientSystemDAO) DisableClientSystem(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientSystemStateDisabled).
Update()
return err
}
// FindEnabledClientSystem 查找启用中的条目
func (this *ClientSystemDAO) FindEnabledClientSystem(tx *dbs.Tx, id int64) (*ClientSystem, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", ClientSystemStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*ClientSystem), err
}
// FindClientSystemName 根据主键查找名称
func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreateSystemIfNotExists 创建系统信息
func (this *ClientSystemDAO) CreateSystemIfNotExists(tx *dbs.Tx, systemName string) error {
const maxlength = 50
if len(systemName) > maxlength {
systemName = systemName[:50]
}
// 检查缓存
var cacheKey = "clientSystem:" + systemName
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
if cacheItem != nil {
return nil
}
// 检查是否已经存在
// 不需要加状态条件
systemId, err := this.Query(tx).
Attr("name", systemName).
ResultPk().
FindInt64Col(0)
if err != nil {
return err
}
if systemId > 0 {
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
return this.Query(tx).
Pk(systemId).
Set("createdDay", timeutil.Format("Ymd")).
UpdateQuickly()
}
var op = NewClientSystemOperator()
op.Name = systemName
op.CreatedDay = timeutil.Format("Ymd")
op.State = ClientSystemStateEnabled
systemId, err = this.SaveInt64(tx, op)
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
return nil
}
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
if systemId > 0 {
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
}
return err
}
// Clean 清理
func (this *ClientSystemDAO) Clean(tx *dbs.Tx, days int) error {
if days <= 0 {
days = 30
}
return this.Query(tx).
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
DeleteQuickly()
}

View File

@@ -0,0 +1,31 @@
package clients_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
_ "github.com/go-sql-driver/mysql"
"testing"
)
func TestClientSystemDAO_CreateSystemIfNotExists(t *testing.T) {
var dao = clients.NewClientSystemDAO()
{
err := dao.CreateSystemIfNotExists(nil, "Mac OS X")
if err != nil {
t.Fatal(err)
}
}
{
err := dao.CreateSystemIfNotExists(nil, "Mac OS X 2")
if err != nil {
t.Fatal(err)
}
}
}
func TestClientSystemDAO_Clean(t *testing.T) {
var dao = clients.NewClientSystemDAO()
err := dao.Clean(nil, 30)
if err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,24 @@
package clients
import "github.com/iwind/TeaGo/dbs"
// ClientSystem 终端操作系统信息
type ClientSystem struct {
Id uint64 `field:"id"` // ID
Name string `field:"name"` // 系统名称
Codes dbs.JSON `field:"codes"` // 代号
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
State uint8 `field:"state"` // 状态
}
type ClientSystemOperator struct {
Id any // ID
Name any // 系统名称
Codes any // 代号
CreatedDay any // 创建日期YYYYMMDD
State any // 状态
}
func NewClientSystemOperator() *ClientSystemOperator {
return &ClientSystemOperator{}
}

View File

@@ -0,0 +1 @@
package clients