优化表结构,重构模型,重新实现基于白银网关的提取节点流程

This commit is contained in:
2025-11-24 18:44:06 +08:00
parent 9a574f55cb
commit cb2a963a37
142 changed files with 6528 additions and 5808 deletions

View File

@@ -28,23 +28,103 @@ func newChannel(db *gorm.DB, opts ...gen.DOOption) channel {
tableName := _channel.channelDo.TableName()
_channel.ALL = field.NewAsterisk(tableName)
_channel.ID = field.NewInt32(tableName, "id")
_channel.CreatedAt = field.NewTime(tableName, "created_at")
_channel.UpdatedAt = field.NewTime(tableName, "updated_at")
_channel.DeletedAt = field.NewField(tableName, "deleted_at")
_channel.UserID = field.NewInt32(tableName, "user_id")
_channel.ProxyID = field.NewInt32(tableName, "proxy_id")
_channel.EdgeID = field.NewInt32(tableName, "edge_id")
_channel.ResourceID = field.NewInt32(tableName, "resource_id")
_channel.ProxyHost = field.NewString(tableName, "proxy_host")
_channel.ProxyPort = field.NewInt32(tableName, "proxy_port")
_channel.EdgeHost = field.NewString(tableName, "edge_host")
_channel.Protocol = field.NewInt32(tableName, "protocol")
_channel.AuthIP = field.NewBool(tableName, "auth_ip")
_channel.Whitelists = field.NewString(tableName, "whitelists")
_channel.AuthPass = field.NewBool(tableName, "auth_pass")
_channel.ProxyID = field.NewInt32(tableName, "proxy_id")
_channel.BatchNo = field.NewString(tableName, "batch_no")
_channel.Port = field.NewUint16(tableName, "port")
_channel.EdgeID = field.NewInt32(tableName, "edge_id")
_channel.FilterISP = field.NewInt(tableName, "filter_isp")
_channel.FilterProv = field.NewString(tableName, "filter_prov")
_channel.FilterCity = field.NewString(tableName, "filter_city")
_channel.IP = field.NewField(tableName, "ip")
_channel.Whitelists = field.NewField(tableName, "whitelists")
_channel.Username = field.NewString(tableName, "username")
_channel.Password = field.NewString(tableName, "password")
_channel.Expiration = field.NewField(tableName, "expiration")
_channel.CreatedAt = field.NewField(tableName, "created_at")
_channel.UpdatedAt = field.NewField(tableName, "updated_at")
_channel.DeletedAt = field.NewField(tableName, "deleted_at")
_channel.ExpiredAt = field.NewTime(tableName, "expired_at")
_channel.User = channelBelongsToUser{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Admin: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Admin", "models.Admin"),
},
}
_channel.Resource = channelBelongsToResource{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Resource", "models.Resource"),
User: struct {
field.RelationField
}{
RelationField: field.NewRelation("Resource.User", "models.User"),
},
Short: struct {
field.RelationField
}{
RelationField: field.NewRelation("Resource.Short", "models.ResourceShort"),
},
Long: struct {
field.RelationField
}{
RelationField: field.NewRelation("Resource.Long", "models.ResourceLong"),
},
}
_channel.Proxy = channelBelongsToProxy{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Proxy", "models.Proxy"),
Channels: struct {
field.RelationField
User struct {
field.RelationField
}
Resource struct {
field.RelationField
}
Proxy struct {
field.RelationField
}
Edge struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Proxy.Channels", "models.Channel"),
User: struct {
field.RelationField
}{
RelationField: field.NewRelation("Proxy.Channels.User", "models.User"),
},
Resource: struct {
field.RelationField
}{
RelationField: field.NewRelation("Proxy.Channels.Resource", "models.Resource"),
},
Proxy: struct {
field.RelationField
}{
RelationField: field.NewRelation("Proxy.Channels.Proxy", "models.Proxy"),
},
Edge: struct {
field.RelationField
}{
RelationField: field.NewRelation("Proxy.Channels.Edge", "models.Edge"),
},
},
}
_channel.Edge = channelBelongsToEdge{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Edge", "models.Edge"),
}
_channel.fillFieldMap()
@@ -55,24 +135,31 @@ type channel struct {
channelDo
ALL field.Asterisk
ID field.Int32 // 通道ID
UserID field.Int32 // 用户ID
ProxyID field.Int32 // 代理ID
EdgeID field.Int32 // 节点ID
ResourceID field.Int32 // 套餐ID
ProxyHost field.String // 代理地址
ProxyPort field.Int32 // 转发端口
EdgeHost field.String // 节点地址
Protocol field.Int32 // 协议类型1-http2-https3-socks5
AuthIP field.Bool // IP认证
Whitelists field.String // IP白名单逗号分隔
AuthPass field.Bool // 密码认证
Username field.String // 用户名
Password field.String // 密码
Expiration field.Field // 过期时间
CreatedAt field.Field // 创建时间
UpdatedAt field.Field // 更新时间
DeletedAt field.Field // 删除时间
ID field.Int32
CreatedAt field.Time
UpdatedAt field.Time
DeletedAt field.Field
UserID field.Int32
ResourceID field.Int32
ProxyID field.Int32
BatchNo field.String
Port field.Uint16
EdgeID field.Int32
FilterISP field.Int
FilterProv field.String
FilterCity field.String
IP field.Field
Whitelists field.Field
Username field.String
Password field.String
ExpiredAt field.Time
User channelBelongsToUser
Resource channelBelongsToResource
Proxy channelBelongsToProxy
Edge channelBelongsToEdge
fieldMap map[string]field.Expr
}
@@ -90,23 +177,23 @@ func (c channel) As(alias string) *channel {
func (c *channel) updateTableName(table string) *channel {
c.ALL = field.NewAsterisk(table)
c.ID = field.NewInt32(table, "id")
c.CreatedAt = field.NewTime(table, "created_at")
c.UpdatedAt = field.NewTime(table, "updated_at")
c.DeletedAt = field.NewField(table, "deleted_at")
c.UserID = field.NewInt32(table, "user_id")
c.ProxyID = field.NewInt32(table, "proxy_id")
c.EdgeID = field.NewInt32(table, "edge_id")
c.ResourceID = field.NewInt32(table, "resource_id")
c.ProxyHost = field.NewString(table, "proxy_host")
c.ProxyPort = field.NewInt32(table, "proxy_port")
c.EdgeHost = field.NewString(table, "edge_host")
c.Protocol = field.NewInt32(table, "protocol")
c.AuthIP = field.NewBool(table, "auth_ip")
c.Whitelists = field.NewString(table, "whitelists")
c.AuthPass = field.NewBool(table, "auth_pass")
c.ProxyID = field.NewInt32(table, "proxy_id")
c.BatchNo = field.NewString(table, "batch_no")
c.Port = field.NewUint16(table, "port")
c.EdgeID = field.NewInt32(table, "edge_id")
c.FilterISP = field.NewInt(table, "filter_isp")
c.FilterProv = field.NewString(table, "filter_prov")
c.FilterCity = field.NewString(table, "filter_city")
c.IP = field.NewField(table, "ip")
c.Whitelists = field.NewField(table, "whitelists")
c.Username = field.NewString(table, "username")
c.Password = field.NewString(table, "password")
c.Expiration = field.NewField(table, "expiration")
c.CreatedAt = field.NewField(table, "created_at")
c.UpdatedAt = field.NewField(table, "updated_at")
c.DeletedAt = field.NewField(table, "deleted_at")
c.ExpiredAt = field.NewTime(table, "expired_at")
c.fillFieldMap()
@@ -123,37 +210,404 @@ func (c *channel) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (c *channel) fillFieldMap() {
c.fieldMap = make(map[string]field.Expr, 18)
c.fieldMap = make(map[string]field.Expr, 22)
c.fieldMap["id"] = c.ID
c.fieldMap["user_id"] = c.UserID
c.fieldMap["proxy_id"] = c.ProxyID
c.fieldMap["edge_id"] = c.EdgeID
c.fieldMap["resource_id"] = c.ResourceID
c.fieldMap["proxy_host"] = c.ProxyHost
c.fieldMap["proxy_port"] = c.ProxyPort
c.fieldMap["edge_host"] = c.EdgeHost
c.fieldMap["protocol"] = c.Protocol
c.fieldMap["auth_ip"] = c.AuthIP
c.fieldMap["whitelists"] = c.Whitelists
c.fieldMap["auth_pass"] = c.AuthPass
c.fieldMap["username"] = c.Username
c.fieldMap["password"] = c.Password
c.fieldMap["expiration"] = c.Expiration
c.fieldMap["created_at"] = c.CreatedAt
c.fieldMap["updated_at"] = c.UpdatedAt
c.fieldMap["deleted_at"] = c.DeletedAt
c.fieldMap["user_id"] = c.UserID
c.fieldMap["resource_id"] = c.ResourceID
c.fieldMap["proxy_id"] = c.ProxyID
c.fieldMap["batch_no"] = c.BatchNo
c.fieldMap["port"] = c.Port
c.fieldMap["edge_id"] = c.EdgeID
c.fieldMap["filter_isp"] = c.FilterISP
c.fieldMap["filter_prov"] = c.FilterProv
c.fieldMap["filter_city"] = c.FilterCity
c.fieldMap["ip"] = c.IP
c.fieldMap["whitelists"] = c.Whitelists
c.fieldMap["username"] = c.Username
c.fieldMap["password"] = c.Password
c.fieldMap["expired_at"] = c.ExpiredAt
}
func (c channel) clone(db *gorm.DB) channel {
c.channelDo.ReplaceConnPool(db.Statement.ConnPool)
c.User.db = db.Session(&gorm.Session{Initialized: true})
c.User.db.Statement.ConnPool = db.Statement.ConnPool
c.Resource.db = db.Session(&gorm.Session{Initialized: true})
c.Resource.db.Statement.ConnPool = db.Statement.ConnPool
c.Proxy.db = db.Session(&gorm.Session{Initialized: true})
c.Proxy.db.Statement.ConnPool = db.Statement.ConnPool
c.Edge.db = db.Session(&gorm.Session{Initialized: true})
c.Edge.db.Statement.ConnPool = db.Statement.ConnPool
return c
}
func (c channel) replaceDB(db *gorm.DB) channel {
c.channelDo.ReplaceDB(db)
c.User.db = db.Session(&gorm.Session{})
c.Resource.db = db.Session(&gorm.Session{})
c.Proxy.db = db.Session(&gorm.Session{})
c.Edge.db = db.Session(&gorm.Session{})
return c
}
type channelBelongsToUser struct {
db *gorm.DB
field.RelationField
Admin struct {
field.RelationField
}
}
func (a channelBelongsToUser) Where(conds ...field.Expr) *channelBelongsToUser {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a channelBelongsToUser) WithContext(ctx context.Context) *channelBelongsToUser {
a.db = a.db.WithContext(ctx)
return &a
}
func (a channelBelongsToUser) Session(session *gorm.Session) *channelBelongsToUser {
a.db = a.db.Session(session)
return &a
}
func (a channelBelongsToUser) Model(m *models.Channel) *channelBelongsToUserTx {
return &channelBelongsToUserTx{a.db.Model(m).Association(a.Name())}
}
func (a channelBelongsToUser) Unscoped() *channelBelongsToUser {
a.db = a.db.Unscoped()
return &a
}
type channelBelongsToUserTx struct{ tx *gorm.Association }
func (a channelBelongsToUserTx) Find() (result *models.User, err error) {
return result, a.tx.Find(&result)
}
func (a channelBelongsToUserTx) Append(values ...*models.User) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a channelBelongsToUserTx) Replace(values ...*models.User) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a channelBelongsToUserTx) Delete(values ...*models.User) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a channelBelongsToUserTx) Clear() error {
return a.tx.Clear()
}
func (a channelBelongsToUserTx) Count() int64 {
return a.tx.Count()
}
func (a channelBelongsToUserTx) Unscoped() *channelBelongsToUserTx {
a.tx = a.tx.Unscoped()
return &a
}
type channelBelongsToResource struct {
db *gorm.DB
field.RelationField
User struct {
field.RelationField
}
Short struct {
field.RelationField
}
Long struct {
field.RelationField
}
}
func (a channelBelongsToResource) Where(conds ...field.Expr) *channelBelongsToResource {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a channelBelongsToResource) WithContext(ctx context.Context) *channelBelongsToResource {
a.db = a.db.WithContext(ctx)
return &a
}
func (a channelBelongsToResource) Session(session *gorm.Session) *channelBelongsToResource {
a.db = a.db.Session(session)
return &a
}
func (a channelBelongsToResource) Model(m *models.Channel) *channelBelongsToResourceTx {
return &channelBelongsToResourceTx{a.db.Model(m).Association(a.Name())}
}
func (a channelBelongsToResource) Unscoped() *channelBelongsToResource {
a.db = a.db.Unscoped()
return &a
}
type channelBelongsToResourceTx struct{ tx *gorm.Association }
func (a channelBelongsToResourceTx) Find() (result *models.Resource, err error) {
return result, a.tx.Find(&result)
}
func (a channelBelongsToResourceTx) Append(values ...*models.Resource) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a channelBelongsToResourceTx) Replace(values ...*models.Resource) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a channelBelongsToResourceTx) Delete(values ...*models.Resource) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a channelBelongsToResourceTx) Clear() error {
return a.tx.Clear()
}
func (a channelBelongsToResourceTx) Count() int64 {
return a.tx.Count()
}
func (a channelBelongsToResourceTx) Unscoped() *channelBelongsToResourceTx {
a.tx = a.tx.Unscoped()
return &a
}
type channelBelongsToProxy struct {
db *gorm.DB
field.RelationField
Channels struct {
field.RelationField
User struct {
field.RelationField
}
Resource struct {
field.RelationField
}
Proxy struct {
field.RelationField
}
Edge struct {
field.RelationField
}
}
}
func (a channelBelongsToProxy) Where(conds ...field.Expr) *channelBelongsToProxy {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a channelBelongsToProxy) WithContext(ctx context.Context) *channelBelongsToProxy {
a.db = a.db.WithContext(ctx)
return &a
}
func (a channelBelongsToProxy) Session(session *gorm.Session) *channelBelongsToProxy {
a.db = a.db.Session(session)
return &a
}
func (a channelBelongsToProxy) Model(m *models.Channel) *channelBelongsToProxyTx {
return &channelBelongsToProxyTx{a.db.Model(m).Association(a.Name())}
}
func (a channelBelongsToProxy) Unscoped() *channelBelongsToProxy {
a.db = a.db.Unscoped()
return &a
}
type channelBelongsToProxyTx struct{ tx *gorm.Association }
func (a channelBelongsToProxyTx) Find() (result *models.Proxy, err error) {
return result, a.tx.Find(&result)
}
func (a channelBelongsToProxyTx) Append(values ...*models.Proxy) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a channelBelongsToProxyTx) Replace(values ...*models.Proxy) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a channelBelongsToProxyTx) Delete(values ...*models.Proxy) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a channelBelongsToProxyTx) Clear() error {
return a.tx.Clear()
}
func (a channelBelongsToProxyTx) Count() int64 {
return a.tx.Count()
}
func (a channelBelongsToProxyTx) Unscoped() *channelBelongsToProxyTx {
a.tx = a.tx.Unscoped()
return &a
}
type channelBelongsToEdge struct {
db *gorm.DB
field.RelationField
}
func (a channelBelongsToEdge) Where(conds ...field.Expr) *channelBelongsToEdge {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a channelBelongsToEdge) WithContext(ctx context.Context) *channelBelongsToEdge {
a.db = a.db.WithContext(ctx)
return &a
}
func (a channelBelongsToEdge) Session(session *gorm.Session) *channelBelongsToEdge {
a.db = a.db.Session(session)
return &a
}
func (a channelBelongsToEdge) Model(m *models.Channel) *channelBelongsToEdgeTx {
return &channelBelongsToEdgeTx{a.db.Model(m).Association(a.Name())}
}
func (a channelBelongsToEdge) Unscoped() *channelBelongsToEdge {
a.db = a.db.Unscoped()
return &a
}
type channelBelongsToEdgeTx struct{ tx *gorm.Association }
func (a channelBelongsToEdgeTx) Find() (result *models.Edge, err error) {
return result, a.tx.Find(&result)
}
func (a channelBelongsToEdgeTx) Append(values ...*models.Edge) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a channelBelongsToEdgeTx) Replace(values ...*models.Edge) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a channelBelongsToEdgeTx) Delete(values ...*models.Edge) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a channelBelongsToEdgeTx) Clear() error {
return a.tx.Clear()
}
func (a channelBelongsToEdgeTx) Count() int64 {
return a.tx.Count()
}
func (a channelBelongsToEdgeTx) Unscoped() *channelBelongsToEdgeTx {
a.tx = a.tx.Unscoped()
return &a
}
type channelDo struct{ gen.DO }
func (c channelDo) Debug() *channelDo {