mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-11-17 01:44:23 +08:00
fix(relay): authenticate endpoints and fail closed on missing routes (#569)
* fix(relay): authenticate endpoints and fail closed on missing routes * fix(tunnel): retain chains until receiver replacement is acknowledged * docs: record final relay security review and validation
This commit is contained in:
@@ -175,6 +175,7 @@ type StatisticsFlow struct {
|
||||
func (StatisticsFlow) TableName() string { return "statistics_flow" }
|
||||
|
||||
type Tunnel struct {
|
||||
RelayAuthMode string `gorm:"column:relay_auth_mode;type:varchar(20);not null;default:legacy"`
|
||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||
Name string `gorm:"type:varchar(100);not null"`
|
||||
TrafficRatio float64 `gorm:"column:traffic_ratio;not null;default:1.0"`
|
||||
@@ -354,24 +355,26 @@ type PeerShare struct {
|
||||
func (PeerShare) TableName() string { return "peer_share" }
|
||||
|
||||
type PeerShareRuntime struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||
ShareID int64 `gorm:"column:share_id;not null;index:idx_peer_share_runtime_share_node_status"`
|
||||
NodeID int64 `gorm:"column:node_id;not null;index:idx_peer_share_runtime_share_node_status"`
|
||||
ReservationID string `gorm:"column:reservation_id;type:text;not null;uniqueIndex"`
|
||||
ResourceKey string `gorm:"column:resource_key;type:text;not null;uniqueIndex"`
|
||||
BindingID string `gorm:"column:binding_id;type:text;not null;default:'';index:idx_peer_share_runtime_binding_id"`
|
||||
Role string `gorm:"type:text;not null;default:''"`
|
||||
ChainName string `gorm:"column:chain_name;type:text;not null;default:''"`
|
||||
ServiceName string `gorm:"column:service_name;type:text;not null;default:''"`
|
||||
Protocol string `gorm:"type:text;not null;default:'tls'"`
|
||||
Strategy string `gorm:"type:text;not null;default:'round'"`
|
||||
Port int `gorm:"not null;default:0"`
|
||||
Target string `gorm:"type:text;not null;default:''"`
|
||||
Applied int `gorm:"not null;default:0"`
|
||||
ReleasePending int `gorm:"column:release_pending;not null;default:0"`
|
||||
Status int `gorm:"not null;default:1;index:idx_peer_share_runtime_share_node_status"`
|
||||
CreatedTime int64 `gorm:"column:created_time;not null"`
|
||||
UpdatedTime int64 `gorm:"column:updated_time;not null"`
|
||||
RelayAuthVersion int `gorm:"column:relay_auth_version;not null;default:0" json:"-"`
|
||||
RelayAuth string `gorm:"column:relay_auth;type:text;not null;default:''" json:"-"`
|
||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||
ShareID int64 `gorm:"column:share_id;not null;index:idx_peer_share_runtime_share_node_status"`
|
||||
NodeID int64 `gorm:"column:node_id;not null;index:idx_peer_share_runtime_share_node_status"`
|
||||
ReservationID string `gorm:"column:reservation_id;type:text;not null;uniqueIndex"`
|
||||
ResourceKey string `gorm:"column:resource_key;type:text;not null;uniqueIndex"`
|
||||
BindingID string `gorm:"column:binding_id;type:text;not null;default:'';index:idx_peer_share_runtime_binding_id"`
|
||||
Role string `gorm:"type:text;not null;default:''"`
|
||||
ChainName string `gorm:"column:chain_name;type:text;not null;default:''"`
|
||||
ServiceName string `gorm:"column:service_name;type:text;not null;default:''"`
|
||||
Protocol string `gorm:"type:text;not null;default:'tls'"`
|
||||
Strategy string `gorm:"type:text;not null;default:'round'"`
|
||||
Port int `gorm:"not null;default:0"`
|
||||
Target string `gorm:"type:text;not null;default:''" json:"-"`
|
||||
Applied int `gorm:"not null;default:0"`
|
||||
ReleasePending int `gorm:"column:release_pending;not null;default:0"`
|
||||
Status int `gorm:"not null;default:1;index:idx_peer_share_runtime_share_node_status"`
|
||||
CreatedTime int64 `gorm:"column:created_time;not null"`
|
||||
UpdatedTime int64 `gorm:"column:updated_time;not null"`
|
||||
}
|
||||
|
||||
func (PeerShareRuntime) TableName() string { return "peer_share_runtime" }
|
||||
@@ -464,6 +467,7 @@ type NodeBackup struct {
|
||||
}
|
||||
|
||||
type TunnelBackup struct {
|
||||
RelayAuthMode string `json:"relayAuthMode,omitempty"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
TrafficRatio float64 `json:"trafficRatio"`
|
||||
@@ -621,6 +625,7 @@ type ForwardRecord struct {
|
||||
|
||||
// TunnelRecord is a minimal tunnel view used by control plane.
|
||||
type TunnelRecord struct {
|
||||
RelayAuthMode string
|
||||
ID int64
|
||||
Type int
|
||||
Status int
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package model
|
||||
|
||||
// RelayEndpointAuth is private desired state, never an API or backup DTO.
|
||||
// Retained endpoint identities keep their credentials across topology edits.
|
||||
type RelayEndpointAuth struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"-"`
|
||||
TunnelID int64 `gorm:"not null;uniqueIndex:idx_relay_endpoint" json:"-"`
|
||||
NodeID int64 `gorm:"not null;uniqueIndex:idx_relay_endpoint" json:"-"`
|
||||
ChainType int `gorm:"not null;uniqueIndex:idx_relay_endpoint" json:"-"`
|
||||
Version int `gorm:"not null" json:"-"`
|
||||
Username string `gorm:"type:text;not null" json:"-"`
|
||||
Password string `gorm:"type:text;not null" json:"-"`
|
||||
}
|
||||
|
||||
func (RelayEndpointAuth) TableName() string { return "relay_endpoint_auth" }
|
||||
@@ -0,0 +1,13 @@
|
||||
package model
|
||||
|
||||
// TunnelRuntimeCleanup retains ownership until an obsolete local runtime is
|
||||
// acknowledged removed. It intentionally survives deletion of its tunnel.
|
||||
type TunnelRuntimeCleanup struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||
TunnelID int64 `gorm:"not null;uniqueIndex:idx_tunnel_cleanup"`
|
||||
NodeID int64 `gorm:"not null;uniqueIndex:idx_tunnel_cleanup"`
|
||||
Kind string `gorm:"type:varchar(16);not null;uniqueIndex:idx_tunnel_cleanup"`
|
||||
Port int `gorm:"not null;default:0;uniqueIndex:idx_tunnel_cleanup"`
|
||||
}
|
||||
|
||||
func (TunnelRuntimeCleanup) TableName() string { return "tunnel_runtime_cleanup" }
|
||||
@@ -299,6 +299,8 @@ func autoMigrateAll(db *gorm.DB) error {
|
||||
&model.SpeedLimit{},
|
||||
&model.StatisticsFlow{},
|
||||
&model.Tunnel{},
|
||||
&model.RelayEndpointAuth{},
|
||||
&model.TunnelRuntimeCleanup{},
|
||||
&model.ChainTunnel{},
|
||||
&model.UserTunnel{},
|
||||
&model.TunnelGroup{},
|
||||
@@ -1252,7 +1254,8 @@ func (r *Repository) ListTunnels() ([]map[string]interface{}, error) {
|
||||
tunnelMap[t.ID] = map[string]interface{}{
|
||||
"id": t.ID, "inx": t.Inx, "name": t.Name,
|
||||
"type": t.Type, "flow": t.Flow, "trafficRatio": t.TrafficRatio,
|
||||
"status": t.Status, "createdTime": t.CreatedTime,
|
||||
"relayAuthMode": t.RelayAuthMode,
|
||||
"status": t.Status, "createdTime": t.CreatedTime,
|
||||
"inIp": nullableString(t.InIP),
|
||||
"ipPreference": t.IPPreference,
|
||||
"probeTargetHost": t.ProbeTargetHost,
|
||||
@@ -1616,8 +1619,10 @@ func (r *Repository) UpdatePeerShareRuntime(item *model.PeerShareRuntime) error
|
||||
return errors.New("runtime item is nil")
|
||||
}
|
||||
return r.db.Model(&model.PeerShareRuntime{}).Where("id = ?", item.ID).Updates(map[string]interface{}{
|
||||
"reservation_id": item.ReservationID,
|
||||
"binding_id": item.BindingID, "role": item.Role,
|
||||
"reservation_id": item.ReservationID,
|
||||
"relay_auth": item.RelayAuth,
|
||||
"relay_auth_version": item.RelayAuthVersion,
|
||||
"binding_id": item.BindingID, "role": item.Role,
|
||||
"chain_name": item.ChainName, "service_name": item.ServiceName,
|
||||
"protocol": item.Protocol, "strategy": item.Strategy,
|
||||
"port": item.Port, "target": item.Target,
|
||||
@@ -2161,7 +2166,8 @@ func (r *Repository) exportTunnels() ([]model.TunnelBackup, error) {
|
||||
out := make([]model.TunnelBackup, 0, len(tunnels))
|
||||
for _, t := range tunnels {
|
||||
b := model.TunnelBackup{
|
||||
ID: t.ID, Name: t.Name, TrafficRatio: t.TrafficRatio,
|
||||
RelayAuthMode: t.RelayAuthMode,
|
||||
ID: t.ID, Name: t.Name, TrafficRatio: t.TrafficRatio,
|
||||
Type: t.Type, Protocol: t.Protocol, Flow: t.Flow,
|
||||
CreatedTime: t.CreatedTime, UpdatedTime: t.UpdatedTime,
|
||||
Status: t.Status, Inx: t.Inx, IPPreference: t.IPPreference,
|
||||
@@ -2595,6 +2601,18 @@ func importNodes(tx *gorm.DB, nodes []model.NodeBackup, now int64) (int, error)
|
||||
func importTunnels(tx *gorm.DB, tunnels []model.TunnelBackup, now int64) (int, error) {
|
||||
count := 0
|
||||
for _, t := range tunnels {
|
||||
if t.RelayAuthMode != "" && t.RelayAuthMode != "legacy" {
|
||||
return count, errors.New("authenticated relay tunnels require a full database restore; configuration backup has no private relay credentials")
|
||||
}
|
||||
var existing model.Tunnel
|
||||
lookupErr := tx.First(&existing, t.ID).Error
|
||||
if lookupErr != nil && !errors.Is(lookupErr, gorm.ErrRecordNotFound) {
|
||||
return count, lookupErr
|
||||
}
|
||||
if lookupErr == nil && existing.RelayAuthMode == "required" {
|
||||
return count, errors.New("configuration import cannot replace an authenticated relay tunnel")
|
||||
}
|
||||
|
||||
item := model.Tunnel{
|
||||
ID: t.ID,
|
||||
Name: t.Name,
|
||||
|
||||
@@ -202,6 +202,13 @@ func (r *Repository) ListUsedPortsOnNode(nodeID int64) ([]int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
pendingPorts, err := r.PendingTunnelCleanupPortsTx(nil, nodeID, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, port := range pendingPorts {
|
||||
used[port] = struct{}{}
|
||||
}
|
||||
result := make([]int, 0, len(used))
|
||||
for p := range used {
|
||||
result = append(result, p)
|
||||
|
||||
@@ -272,6 +272,7 @@ func (r *Repository) GetTunnelRecord(tunnelID int64) (*model.TunnelRecord, error
|
||||
return nil, err
|
||||
}
|
||||
tr := model.TunnelRecord{
|
||||
RelayAuthMode: t.RelayAuthMode,
|
||||
ID: t.ID,
|
||||
Type: t.Type,
|
||||
Status: t.Status,
|
||||
|
||||
@@ -584,6 +584,13 @@ func (r *Repository) pickNodePortTx(tx *gorm.DB, nodeID int64, allocated map[int
|
||||
}
|
||||
}
|
||||
|
||||
pendingPorts, err := r.PendingTunnelCleanupPortsTx(tx, nodeID, excludeTunnelID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, port := range pendingPorts {
|
||||
used[port] = struct{}{}
|
||||
}
|
||||
var available []int
|
||||
for _, candidate := range candidates {
|
||||
if candidate <= 0 {
|
||||
@@ -629,6 +636,9 @@ func (r *Repository) DeleteTunnelCascade(tunnelID int64) error {
|
||||
return errors.New("repository not initialized")
|
||||
}
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := r.queueDeletedTunnelRuntimeTx(tx, tunnelID); err != nil {
|
||||
return err
|
||||
}
|
||||
forwardIDs := tx.Model(&model.Forward{}).Select("id").Where("tunnel_id = ?", tunnelID)
|
||||
if err := tx.Where("forward_id IN (?)", forwardIDs).Delete(&model.ForwardPort{}).Error; err != nil {
|
||||
return err
|
||||
@@ -645,6 +655,9 @@ func (r *Repository) DeleteTunnelCascade(tunnelID int64) error {
|
||||
if err := tx.Where("tunnel_id = ?", tunnelID).Delete(&model.FederationTunnelBinding{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("tunnel_id = ?", tunnelID).Delete(&model.RelayEndpointAuth{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Where("id = ?", tunnelID).Delete(&model.Tunnel{}).Error
|
||||
})
|
||||
}
|
||||
@@ -904,6 +917,13 @@ func (r *Repository) GetUsedPortsOnNodeAsMap(nodeID int64) (map[int]bool, error)
|
||||
for _, p := range chainPorts {
|
||||
used[p] = true
|
||||
}
|
||||
pendingPorts, err := r.PendingTunnelCleanupPortsTx(nil, nodeID, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, port := range pendingPorts {
|
||||
used[port] = true
|
||||
}
|
||||
return used, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go-backend/internal/relaysecurity"
|
||||
"go-backend/internal/store/model"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// RelayEndpointCredentials loads existing state, creating only when explicitly
|
||||
// requested by a new endpoint operation. Reconnects must never recreate a secret.
|
||||
func (r *Repository) RelayEndpointCredentials(tx *gorm.DB, tunnelID, nodeID int64, role int, create bool) (*relaysecurity.Credentials, error) {
|
||||
db := tx
|
||||
if db == nil {
|
||||
db = r.db
|
||||
}
|
||||
var row model.RelayEndpointAuth
|
||||
find := func() error {
|
||||
return db.Where("tunnel_id = ? AND node_id = ? AND chain_type = ?", tunnelID, nodeID, role).First(&row).Error
|
||||
}
|
||||
err := find()
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) && create {
|
||||
var existing int64
|
||||
if e := db.Model(&model.ChainTunnel{}).Where("tunnel_id = ? AND node_id = ? AND chain_type = ?", tunnelID, nodeID, role).Count(&existing).Error; e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if existing > 0 {
|
||||
var tunnel model.Tunnel
|
||||
if e := db.First(&tunnel, tunnelID).Error; e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if tunnel.Type == 2 || tunnel.RelayAuthMode == relaysecurity.Required {
|
||||
return nil, errors.New("required relay endpoint credentials unavailable; refusing regeneration")
|
||||
}
|
||||
}
|
||||
|
||||
c, e := relaysecurity.New()
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
row = model.RelayEndpointAuth{TunnelID: tunnelID, NodeID: nodeID, ChainType: role, Version: c.Version, Username: c.Username, Password: c.Password}
|
||||
if e = db.Clauses(clause.OnConflict{DoNothing: true}).Create(&row).Error; e != nil {
|
||||
return nil, e
|
||||
}
|
||||
row = model.RelayEndpointAuth{}
|
||||
err = find()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.New("required relay endpoint credentials unavailable")
|
||||
}
|
||||
c := &relaysecurity.Credentials{Version: row.Version, Username: row.Username, Password: row.Password}
|
||||
if err := c.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *Repository) SetTunnelRelayAuthModeTx(tx *gorm.DB, tunnelID int64, mode string) error {
|
||||
if mode != relaysecurity.Required && mode != relaysecurity.Legacy {
|
||||
return errors.New("invalid relay authentication mode")
|
||||
}
|
||||
return tx.Model(&model.Tunnel{}).Where("id = ?", tunnelID).Update("relay_auth_mode", mode).Error
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"go-backend/internal/store/model"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func (r *Repository) QueueTunnelRuntimeCleanupTx(tx *gorm.DB, items []model.TunnelRuntimeCleanup) error {
|
||||
for _, item := range items {
|
||||
var node model.Node
|
||||
if err := tx.Select("id", "is_remote").Where("id = ?", item.NodeID).First(&node).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if node.IsRemote == 1 {
|
||||
continue
|
||||
}
|
||||
if err := tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "tunnel_id"}, {Name: "node_id"}, {Name: "kind"}, {Name: "port"}}, DoNothing: true}).Create(&item).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (r *Repository) ListTunnelRuntimeCleanup(tunnelID int64) ([]model.TunnelRuntimeCleanup, error) {
|
||||
var items []model.TunnelRuntimeCleanup
|
||||
q := r.db.Model(&model.TunnelRuntimeCleanup{})
|
||||
if tunnelID > 0 {
|
||||
q = q.Where("tunnel_id = ?", tunnelID)
|
||||
}
|
||||
err := q.Order("tunnel_id, node_id, kind DESC").Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
func (r *Repository) DeleteTunnelRuntimeCleanup(id int64) error {
|
||||
return r.db.Where("id = ?", id).Delete(&model.TunnelRuntimeCleanup{}).Error
|
||||
}
|
||||
func (r *Repository) PendingTunnelCleanupPortsTx(tx *gorm.DB, nodeID, excludeTunnelID int64) ([]int, error) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
q := tx.Model(&model.TunnelRuntimeCleanup{}).Where("node_id = ? AND kind = ? AND port > 0", nodeID, "service")
|
||||
if excludeTunnelID > 0 {
|
||||
q = q.Where("tunnel_id != ?", excludeTunnelID)
|
||||
}
|
||||
var ports []int
|
||||
err := q.Pluck("port", &ports).Error
|
||||
return ports, err
|
||||
}
|
||||
|
||||
func (r *Repository) queueDeletedTunnelRuntimeTx(tx *gorm.DB, tunnelID int64) error {
|
||||
var tunnels []model.Tunnel
|
||||
if err := tx.Where("id = ? AND type = ?", tunnelID, 2).Find(&tunnels).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if len(tunnels) == 0 {
|
||||
return nil
|
||||
}
|
||||
var rows []model.ChainTunnel
|
||||
if err := tx.Where("tunnel_id = ?", tunnelID).Find(&rows).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var items []model.TunnelRuntimeCleanup
|
||||
for _, row := range rows {
|
||||
if row.ChainType == "2" || row.ChainType == "3" {
|
||||
items = append(items, model.TunnelRuntimeCleanup{TunnelID: tunnelID, NodeID: row.NodeID, Kind: "service", Port: int(row.Port.Int64)})
|
||||
}
|
||||
if row.ChainType == "1" || row.ChainType == "2" {
|
||||
items = append(items, model.TunnelRuntimeCleanup{TunnelID: tunnelID, NodeID: row.NodeID, Kind: "chain"})
|
||||
}
|
||||
}
|
||||
return r.QueueTunnelRuntimeCleanupTx(tx, items)
|
||||
}
|
||||
Reference in New Issue
Block a user