mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-06 07:25:48 +08:00
智能DNS初步支持搜索引擎线路
This commit is contained in:
98
internal/db/models/clients/client_agent_dao.go
Normal file
98
internal/db/models/clients/client_agent_dao.go
Normal 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
|
||||
}
|
||||
6
internal/db/models/clients/client_agent_dao_test.go
Normal file
6
internal/db/models/clients/client_agent_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
105
internal/db/models/clients/client_agent_ip_dao.go
Normal file
105
internal/db/models/clients/client_agent_ip_dao.go
Normal 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", "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()
|
||||
}
|
||||
16
internal/db/models/clients/client_agent_ip_dao_test.go
Normal file
16
internal/db/models/clients/client_agent_ip_dao_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
20
internal/db/models/clients/client_agent_ip_model.go
Normal file
20
internal/db/models/clients/client_agent_ip_model.go
Normal 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{}
|
||||
}
|
||||
1
internal/db/models/clients/client_agent_ip_model_ext.go
Normal file
1
internal/db/models/clients/client_agent_ip_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package clients
|
||||
24
internal/db/models/clients/client_agent_model.go
Normal file
24
internal/db/models/clients/client_agent_model.go
Normal 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{}
|
||||
}
|
||||
6
internal/db/models/clients/client_agent_model_ext.go
Normal file
6
internal/db/models/clients/client_agent_model_ext.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package clients
|
||||
|
||||
// NSRouteCode NS线路代号
|
||||
func (this *ClientAgent) NSRouteCode() string {
|
||||
return "agent:" + this.Code
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientBrowserDAO_CreateBrowser(t *testing.T) {
|
||||
var dao = models.NewClientBrowserDAO()
|
||||
var dao = clients.NewClientBrowserDAO()
|
||||
err := dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -25,7 +25,7 @@ func TestClientBrowserDAO_CreateBrowser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClientBrowserDAO_Clean(t *testing.T) {
|
||||
var dao = models.NewClientBrowserDAO()
|
||||
var dao = clients.NewClientBrowserDAO()
|
||||
err := dao.Clean(nil, 30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientSystemDAO_CreateSystemIfNotExists(t *testing.T) {
|
||||
var dao = models.NewClientSystemDAO()
|
||||
var dao = clients.NewClientSystemDAO()
|
||||
{
|
||||
err := dao.CreateSystemIfNotExists(nil, "Mac OS X")
|
||||
if err != nil {
|
||||
@@ -23,7 +23,7 @@ func TestClientSystemDAO_CreateSystemIfNotExists(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClientSystemDAO_Clean(t *testing.T) {
|
||||
var dao = models.NewClientSystemDAO()
|
||||
var dao = clients.NewClientSystemDAO()
|
||||
err := dao.Clean(nil, 30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user