Files
Meshray-Manager/internal/model/models.go
T
2026-06-30 15:14:37 +08:00

364 lines
22 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package model
import (
"time"
)
// Network 组网模型 - 使用雪花算法生成 ID(uint64)
type Network struct {
ID uint64 `gorm:"primaryKey;type:bigint" json:"id"` // 雪花算法 ID
Name string `gorm:"type:varchar(64);not null;uniqueIndex" json:"name"`
SubnetIPv4 string `gorm:"type:varchar(18);not null" json:"subnet_ipv4"`
SubnetIPv6 string `gorm:"type:varchar(43)" json:"subnet_ipv6,omitempty"`
Mode string `gorm:"type:varchar(16);not null;default:'enhanced';index" json:"mesh_mode"` // native | enhanced
WGMode string `gorm:"type:varchar(16);not null;default:'userspace'" json:"wg_mode"` // kernel | userspace
PolicyID uint64 `gorm:"type:bigint;index" json:"policy_id"`
DHCPEnabled bool `gorm:"default:true" json:"dhcp_enabled"`
TunEnabled bool `gorm:"default:true" json:"tun_enabled"`
TunName string `gorm:"type:varchar(16);default:'meshray-tun'" json:"tun_name"`
MTU int `gorm:"default:1420" json:"mtu"`
Password string `gorm:"type:varchar(128)" json:"-"` // AES-256-GCM 加密存储
Status string `gorm:"type:varchar(16);default:'running';index" json:"status"` // running | stopped | error
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
// 关联字段
Devices []Device `gorm:"foreignKey:NetworkID" json:"devices,omitempty"`
// DDNS 同步字段
DDNSEnabled bool `gorm:"default:false" json:"ddns_enabled"`
DDNSServiceID string `gorm:"type:varchar(36);index" json:"ddns_service_id"` // ExternalService.ID
DDNSUsageID string `gorm:"type:varchar(36);index" json:"ddns_usage_id"` // DDNSUsage.ID
DDNSPrefix string `gorm:"type:varchar(255)" json:"ddns_prefix"` // TXT 记录前缀(如:_meshray.ABC123
}
// Device 设备模型 - 使用雪花算法生成 ID(uint64)
type Device struct {
ID uint64 `gorm:"primaryKey;type:bigint" json:"id"` // 雪花算法 ID
NetworkID uint64 `gorm:"type:bigint;not null;index:idx_device_network" json:"network_id"` // 所属网络 ID
Name string `gorm:"type:varchar(64);not null" json:"name"`
VirtualIP string `gorm:"type:varchar(45);not null" json:"virtual_ip"`
PublicKey string `gorm:"type:varchar(64);not null" json:"public_key"`
PresharedKey string `gorm:"type:varchar(64)" json:"-"`
Endpoint string `gorm:"type:varchar(261)" json:"endpoint,omitempty"` // 原始公网 Endpointhost:port),用于增强模式回退
IsRelayCapable bool `gorm:"default:false" json:"is_relay_capable"` // 是否作为 Mesh 中继节点
Status string `gorm:"type:varchar(16);default:'offline';index" json:"status"` // online | offline
LastSeen time.Time `json:"last_seen"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
}
// Policy 传输策略模型
type Policy struct {
ID uint64 `gorm:"primaryKey;type:bigint" json:"id"` // Snowflake ID, matches Network.PolicyID
Name string `gorm:"size:255;not null;index" json:"name"`
Type string `gorm:"size:32;not null;default:'custom'" json:"type"` // system | custom
Description string `gorm:"size:512" json:"description"`
LayerConfig string `gorm:"type:text" json:"layer_config"` // JSON 格式存储 9 层链路配置
GlobalParams string `gorm:"type:text" json:"global_params"` // JSON 格式存储全局参数
Enabled bool `gorm:"default:true" json:"enabled"`
IsDefault bool `gorm:"default:false" json:"is_default"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TURNAuthType TURN 鉴权方式
type TURNAuthType string
const (
TURNAuthCredential TURNAuthType = "credential" // Username + PasswordRFC 5766,自建 coturn
TURNAuthToken TURNAuthType = "token" // 临时 Token(商业 TURN 服务:Twilio、Xirsys 等)
TURNAuthSecret TURNAuthType = "secret" // Shared Secret(由信令服务器签发临时凭证)
)
// Service 服务模型 - 前端用户视角的服务实例
type Service struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
Name string `gorm:"type:varchar(64);not null" json:"name"`
Type string `gorm:"type:varchar(8);not null;index" json:"type"` // "tun"/"turn"/"ddns"
Address string `gorm:"type:varchar(255);not null" json:"address"`
Port int `gorm:"not null" json:"port"`
Protocols []string `gorm:"type:text;serializer:json" json:"protocols"` // JSON: ["udp","tcp","tls","ws","wss"]
AuthType string `gorm:"type:varchar(16)" json:"auth_type,omitempty"`
AuthUsername string `gorm:"type:varchar(128)" json:"username,omitempty"`
AuthPassword string `gorm:"type:varchar(128)" json:"password,omitempty"` // 仅用于输入,存储时仍需加密处理
Token string `gorm:"type:text" json:"token,omitempty"`
SharedSecret string `gorm:"type:varchar(128)" json:"shared_secret,omitempty"`
MTU int `gorm:"default:1420" json:"mtu,omitempty"`
Provider string `gorm:"type:varchar(32)" json:"provider,omitempty"`
Domain string `gorm:"type:varchar(255)" json:"domain,omitempty"`
RecordType string `gorm:"type:varchar(8)" json:"record_type,omitempty"`
// DDNS 全功能模式字段
ConfigMode string `gorm:"type:varchar(16);default:'infrastructure'" json:"config_mode"` // "infrastructure" | "fullservice"
DDNSConfigID string `gorm:"type:varchar(36)" json:"ddns_config_id,omitempty"` // 关联的 DDNS 配置 ID(全功能模式)
Subdomain string `gorm:"type:varchar(255)" json:"subdomain,omitempty"` // 主机记录(全功能模式)
TargetIP string `gorm:"type:varchar(64)" json:"target_ip,omitempty"` // 目标 IP(全功能模式)
TXTRecordName string `gorm:"type:varchar(255)" json:"txt_record_name,omitempty"` // TXT 记录名称(全功能模式)
TXTValue string `gorm:"type:text" json:"txt_value,omitempty"` // TXT 记录值(全功能模式)
CNAMETarget string `gorm:"type:varchar(255)" json:"cname_target,omitempty"` // CNAME 目标域名(全功能模式)
TTL int `gorm:"default:600" json:"ttl,omitempty"` // TTL(全功能模式)
Status string `gorm:"type:varchar(16);default:'active'" json:"status"` // active | inactive
Reachable bool `gorm:"-" json:"reachable"` // 连通性状态
Latency int `gorm:"-" json:"latency"` // 延迟
WSEnabled bool `gorm:"default:false" json:"ws_enabled"`
IndependentWS string `gorm:"type:text" json:"independent_ws,omitempty"` // JSON: {address, port, wss}
Enabled bool `gorm:"default:true;index" json:"enabled"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
// ExternalService 外部服务模型 - 统一管理所有外部依赖服务
// 按照 README 6.4 节定义,通过 category + serviceType + config JSON 实现无限扩展
type ExternalService struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
Category string `gorm:"type:varchar(32);not null;index" json:"category"` // "networking" / "dns" / "security" / "gateway" / "automation"
ServiceType string `gorm:"type:varchar(64);not null;index" json:"service_type"` // stun_server / turn_server / ddns_aliyun / ssl_acme 等
Name string `gorm:"type:varchar(64);not null" json:"name"`
Enabled bool `gorm:"default:true;index" json:"enabled"`
Address string `gorm:"type:varchar(255)" json:"address,omitempty"` // 服务器地址(可选)
Port int `json:"port,omitempty"` // 端口(可选)
Config string `gorm:"type:text;not null" json:"config"` // JSON,结构由 serviceType 决定(敏感字段加密)
Status string `gorm:"type:varchar(16);default:'unknown';index" json:"status"` // reachable / unreachable / unknown
LastTestAt *time.Time `json:"last_test_at,omitempty"` // 最后测试时间
LatencyMs int `gorm:"default:0" json:"latency_ms"` // 延迟(ms
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
// SystemSetting 系统设置模型 - 单例模式,全局只有一条记录
type SystemSetting struct {
ID uint `gorm:"primaryKey;type:bigint" json:"id"` // 固定为 1
ServerIP string `gorm:"type:varchar(45)" json:"server_ip"` // 服务端公网 IP
ServerPort int `gorm:"default:51820" json:"server_port"` // WireGuard 监听端口
ServerPublicKey string `gorm:"type:varchar(64)" json:"server_public_key,omitempty"` // WireGuard 服务端公钥
DDNSDomain string `gorm:"type:varchar(255)" json:"ddns_domain"` // DDNS 域名(如 mesh.example.com
TURNMode string `gorm:"type:varchar(16);default:'auto'" json:"turn_mode"` // auto/manual
TURNURL string `gorm:"type:varchar(255)" json:"turn_url"` // TURN 服务器 URL(手动模式)
TURNUsername string `gorm:"type:varchar(128)" json:"turn_username,omitempty"` // TURN 用户名
TURNPassword string `gorm:"type:varchar(128)" json:"-"` // TURN 密码(加密存储)
LogLevel string `gorm:"type:varchar(16);default:'info'" json:"log_level"` // debug/info/warn/error
LogFormat string `gorm:"type:varchar(16);default:'console'" json:"log_format"` // console/json
MaxBackups int `gorm:"default:7" json:"max_backups"` // 日志最大保留份数
MaxAge int `gorm:"default:30" json:"max_age"` // 日志最大保留天数
Theme string `gorm:"type:varchar(32);default:'light'" json:"theme"` // light/dark/auto
Language string `gorm:"type:varchar(16);default:'zh-CN'" json:"language"` // zh-CN/en-US
CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
}
// DDNSProvider DDNS 服务商配置模型(配置层)
type DDNSProvider struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
Name string `gorm:"type:varchar(64);not null" json:"name"` // 服务商名称,如 "Cloudflare-Mesh"
Provider string `gorm:"type:varchar(32);not null" json:"provider"` // cloudflare, aliyun, tencent
Domain string `gorm:"type:varchar(255);not null" json:"domain"` // 主域名
AccessKey string `gorm:"type:varchar(255)" json:"-"` // 加密存储
SecretKey string `gorm:"type:text" json:"-"` // 加密存储
Config string `gorm:"type:text" json:"config"` // JSON 扩展配置
Enabled bool `gorm:"default:true" json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// DDNSUsageType DDNS 用途类型
type DDNSUsageType string
const (
DDNSUsageMeshSeedSync DDNSUsageType = "meshseed_sync" // MeshSeed 同步(TXT 记录)
DDNSUsageIPResolve DDNSUsageType = "ip_resolve" // IP 动态解析(A/AAAA 记录)
DDNSUsageDeviceBind DDNSUsageType = "device_bind" // 设备域名绑定(CNAME 记录)
)
// DDNSUsage DDNS 用途定义模型(应用层)
type DDNSUsage struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
ProviderID string `gorm:"type:varchar(36);not null;index" json:"provider_id"` // 关联的 Provider(即 ExternalService.ID
UsageType DDNSUsageType `gorm:"type:varchar(32);not null" json:"usage_type"`
RecordType string `gorm:"type:varchar(8);not null" json:"record_type"` // TXT, A, AAAA, CNAME
RecordPrefix string `gorm:"type:varchar(255);not null" json:"record_prefix"` // 记录前缀
Description string `gorm:"type:varchar(512)" json:"description"`
IsExclusive bool `gorm:"default:false" json:"is_exclusive"` // 是否独占
PrefixMode string `gorm:"type:varchar(16);not null;default:'auto'" json:"prefix_mode"` // "auto" | "custom"
CreatedAt time.Time `json:"created_at"`
// 关联关系
Provider *DDNSProvider `gorm:"foreignKey:ProviderID" json:"provider,omitempty"`
}
// NetworkDDNSBinding 网络 DDNS 绑定模型(使用层)
type NetworkDDNSBinding struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
NetworkID uint64 `gorm:"type:bigint;not null;uniqueIndex" json:"network_id"` // 每个网络只能绑定一个
UsageID string `gorm:"type:varchar(36);not null" json:"usage_id"` // 选择的用途
ProviderID string `gorm:"type:varchar(36);not null" json:"provider_id"` // 冗余存储
Status string `gorm:"type:varchar(16);default:'active'" json:"status"` // active, sync_pending, sync_failed
LastSyncAt *time.Time `json:"last_sync_at"`
SyncMessage string `gorm:"type:text" json:"sync_message"` // 同步结果/错误信息
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 关联字段
Network *Network `gorm:"foreignKey:NetworkID" json:"network,omitempty"`
Usage *DDNSUsage `gorm:"foreignKey:UsageID" json:"usage,omitempty"`
}
// MeshSeed 组网凭证模型
type MeshSeed struct {
ID uint `gorm:"primaryKey" json:"id"`
SeedID string `gorm:"size:64;not null;uniqueIndex" json:"seed_id"` // 16 字节随机 Base64
NetworkID uint64 `gorm:"type:bigint;not null;index" json:"network_id"` // ✅ 与 Network.ID 类型一致
Network *Network `gorm:"foreignKey:NetworkID" json:"network,omitempty"`
JoinToken string `gorm:"size:512;not null" json:"join_token"` // Base64 编码的完整 MeshSeed
Signature string `gorm:"size:512;not null" json:"signature"` // Ed25519 签名
IssuerNodeID string `gorm:"size:255;not null" json:"issuer_node_id"`
MaxUses int `gorm:"default:10" json:"max_uses"`
UsedCount int `gorm:"default:0" json:"used_count"`
ExpiresAt time.Time `gorm:"index" json:"expires_at"`
DDNSEnabled bool `gorm:"default:false" json:"ddns_enabled"`
UpdateVersion int `gorm:"default:0" json:"update_version"`
DDNSDomain string `gorm:"size:255" json:"ddns_domain"`
Revoked bool `gorm:"default:false" json:"revoked"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// SecurityKey 安全密钥模型(用于存储 Ed25519 签名密钥等)
type SecurityKey struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"size:64;not null;uniqueIndex" json:"name"` // 密钥名称,如 "meshseed_signing"
Value string `gorm:"size:512;not null" json:"-"` // Base64 编码的密钥值
Algorithm string `gorm:"size:32;not null" json:"algorithm"` // 算法类型:ed25519, rsa 等
Purpose string `gorm:"size:128" json:"purpose"` // 用途描述
CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
}
// PendingJoin 待加入审核模型
type PendingJoin struct {
ID uint `gorm:"primaryKey" json:"id"`
SeedID string `gorm:"size:64;not null;index" json:"seed_id"`
DeviceName string `gorm:"size:255;not null" json:"device_name"`
RequestIP string `gorm:"size:64" json:"request_ip"`
Status string `gorm:"size:32;default:'pending'" json:"status"` // pending | approved | rejected
ExpireAt time.Time `gorm:"index" json:"expire_at"` // 72 小时超时
ApprovedAt *time.Time `json:"approved_at"`
RejectedAt *time.Time `json:"rejected_at"`
Reason string `gorm:"size:512" json:"reason"` // 拒绝原因
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// AlertRule 告警规则模型
type AlertRule struct {
ID uint `gorm:"primaryKey" json:"id"`
Type string `gorm:"size:32;not null;index" json:"type"` // resource | link | service
Name string `gorm:"size:255;not null" json:"name"`
Threshold float64 `gorm:"not null" json:"threshold"`
Operator string `gorm:"size:8;not null" json:"operator"` // gt | lt | eq
NotifyEmail bool `gorm:"default:false" json:"notify_email"`
NotifyWebhook bool `gorm:"default:false" json:"notify_webhook"`
WebhookURL string `gorm:"size:512" json:"webhook_url"`
Enabled bool `gorm:"default:true" json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// User 用户模型
type User struct {
ID uint `gorm:"primaryKey" json:"id"`
Username string `gorm:"size:64;not null;uniqueIndex" json:"username"`
PasswordHash string `gorm:"size:255;not null" json:"-"` // bcrypt 加密存储
Email string `gorm:"size:255" json:"email"`
Role string `gorm:"size:32;default:'user'" json:"role"` // admin | user
Status string `gorm:"size:32;default:'active'" json:"status"` // active | banned
LastLoginAt time.Time `json:"last_login_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// AuditLog 审计日志模型
type AuditLog struct {
ID uint `gorm:"primaryKey" json:"id"`
Action string `gorm:"size:64;not null;index" json:"action"` // create_network | delete_network | generate_seed | etc.
OperatorIP string `gorm:"size:64;not null" json:"operator_ip"`
UserAgent string `gorm:"size:512" json:"user_agent"`
Detail string `gorm:"type:text" json:"detail"` // JSON 格式存储详细信息
CreatedAt time.Time `gorm:"index" json:"created_at"`
}
// SystemConfig 系统配置模型(全局唯一)
type SystemConfig struct {
ID uint `gorm:"primaryKey" json:"id"`
Key string `gorm:"size:64;not null;uniqueIndex" json:"key"` // 配置键,如 "wg_mode"
Value string `gorm:"size:255;not null" json:"value"` // 配置值,如 "auto" | "kernel" | "userspace"
UpdatedAt time.Time `json:"updated_at"`
}
// Notification 通知模型
type Notification struct {
ID uint `gorm:"primaryKey" json:"id"`
UserID uint `gorm:"not null;index" json:"user_id"` // 接收用户 ID
Type string `gorm:"size:32;not null;index" json:"type"` // alert/system/update/ddns
Priority int `gorm:"default:2;index" json:"priority"` // 1=low, 2=medium, 3=high
Title string `gorm:"size:255;not null" json:"title"`
Message string `gorm:"type:text;not null" json:"message"`
Data string `gorm:"type:text" json:"data,omitempty"` // JSON 格式额外数据
IsRead bool `gorm:"default:false;index" json:"is_read"` // 是否已读
ReadAt *time.Time `json:"read_at,omitempty"` // 阅读时间
CreatedAt time.Time `gorm:"autoCreateTime;index" json:"created_at"` // 创建时间
}
// DDNSConfig DDNS 配置模型
type DDNSConfig struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
Provider string `gorm:"type:varchar(32);not null" json:"provider"` // aliyun | tencent
AccessKey string `gorm:"type:varchar(128);not null" json:"accessKey"`
SecretKey string `gorm:"type:varchar(128);not null" json:"-"` // AES-256-GCM 加密存储
Domain string `gorm:"type:varchar(255);not null" json:"domain"`
TXTRecordName string `gorm:"type:varchar(255)" json:"txtRecordName"` // 自动生成
SyncMode string `gorm:"type:varchar(16);default:'auto'" json:"syncMode"` // auto | manual
RetryCount int `gorm:"default:10" json:"retryCount"`
RetryInterval int `gorm:"default:300" json:"retryInterval"` // 秒
Enabled bool `gorm:"default:true" json:"enabled"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
}
// NetworkMemberRole 网络成员角色
type NetworkMemberRole string
const (
RoleOwner NetworkMemberRole = "owner" // 创建者,拥有所有权限
RoleMember NetworkMemberRole = "member" // 加入者,只能使用网络
)
// NetworkMember 网络成员表 - 管理网络成员及其角色
type NetworkMember struct {
ID uint64 `gorm:"primaryKey;type:bigint" json:"id"`
NetworkID uint64 `gorm:"type:bigint;not null;uniqueIndex:idx_network_user" json:"networkID"`
UserID uint64 `gorm:"type:bigint;not null;uniqueIndex:idx_network_user" json:"userID"`
Role NetworkMemberRole `gorm:"type:varchar(16);not null;default:'member'" json:"role"` // owner | member
JoinedAt time.Time `gorm:"autoCreateTime" json:"joinedAt"`
// 关联
Network *Network `gorm:"foreignKey:NetworkID" json:"network,omitempty"`
User *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}
// TableName 指定表名
func (NetworkMember) TableName() string {
return "network_members"
}
// Permission 权限类型
type Permission string
const (
PermManageNetwork Permission = "manage_network" // 管理网络配置
PermAddPeer Permission = "add_peer" // 添加节点
PermRemovePeer Permission = "remove_peer" // 移除节点
PermKickMember Permission = "kick_member" // 踢出成员
PermUpdateConfig Permission = "update_config" // 更新组网参数
PermDeleteNetwork Permission = "delete_network" // 解散网络
PermLeaveNetwork Permission = "leave_network" // 退出网络
)