mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-11 03:36:37 +08:00
fix: preserve probe targets in backups
This commit is contained in:
@@ -417,6 +417,8 @@ type TunnelBackup struct {
|
|||||||
InIP string `json:"inIp,omitempty"`
|
InIP string `json:"inIp,omitempty"`
|
||||||
Inx int `json:"inx"`
|
Inx int `json:"inx"`
|
||||||
IPPreference string `json:"ipPreference,omitempty"`
|
IPPreference string `json:"ipPreference,omitempty"`
|
||||||
|
ProbeTargetHost string `json:"probeTargetHost,omitempty"`
|
||||||
|
ProbeTargetPort int `json:"probeTargetPort,omitempty"`
|
||||||
ChainTunnels []ChainTunnelBackup `json:"chainTunnels,omitempty"`
|
ChainTunnels []ChainTunnelBackup `json:"chainTunnels,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2063,6 +2063,7 @@ func (r *Repository) exportTunnels() ([]model.TunnelBackup, error) {
|
|||||||
Type: t.Type, Protocol: t.Protocol, Flow: t.Flow,
|
Type: t.Type, Protocol: t.Protocol, Flow: t.Flow,
|
||||||
CreatedTime: t.CreatedTime, UpdatedTime: t.UpdatedTime,
|
CreatedTime: t.CreatedTime, UpdatedTime: t.UpdatedTime,
|
||||||
Status: t.Status, Inx: t.Inx, IPPreference: t.IPPreference,
|
Status: t.Status, Inx: t.Inx, IPPreference: t.IPPreference,
|
||||||
|
ProbeTargetHost: t.ProbeTargetHost, ProbeTargetPort: t.ProbeTargetPort,
|
||||||
}
|
}
|
||||||
if t.InIP.Valid {
|
if t.InIP.Valid {
|
||||||
b.InIP = t.InIP.String
|
b.InIP = t.InIP.String
|
||||||
@@ -2470,11 +2471,13 @@ func importTunnels(tx *gorm.DB, tunnels []model.TunnelBackup, now int64) (int, e
|
|||||||
InIP: sql.NullString{String: t.InIP, Valid: true},
|
InIP: sql.NullString{String: t.InIP, Valid: true},
|
||||||
Inx: t.Inx,
|
Inx: t.Inx,
|
||||||
IPPreference: t.IPPreference,
|
IPPreference: t.IPPreference,
|
||||||
|
ProbeTargetHost: t.ProbeTargetHost,
|
||||||
|
ProbeTargetPort: t.ProbeTargetPort,
|
||||||
}
|
}
|
||||||
err := tx.Clauses(clause.OnConflict{
|
err := tx.Clauses(clause.OnConflict{
|
||||||
Columns: []clause.Column{{Name: "id"}},
|
Columns: []clause.Column{{Name: "id"}},
|
||||||
DoUpdates: clause.AssignmentColumns([]string{
|
DoUpdates: clause.AssignmentColumns([]string{
|
||||||
"name", "traffic_ratio", "type", "protocol", "flow", "updated_time", "status", "in_ip", "inx", "ip_preference",
|
"name", "traffic_ratio", "type", "protocol", "flow", "updated_time", "status", "in_ip", "inx", "ip_preference", "probe_target_host", "probe_target_port",
|
||||||
}),
|
}),
|
||||||
}).Create(&item).Error
|
}).Create(&item).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBackupRoundTripsTunnelProbeTarget(t *testing.T) {
|
||||||
|
source, err := Open(filepath.Join(t.TempDir(), "source.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open source repo: %v", err)
|
||||||
|
}
|
||||||
|
defer source.Close()
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := source.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(id, name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx, probe_target_host, probe_target_port)
|
||||||
|
VALUES(20, 'backup-target', 1, 2, 'tls', 1, ?, ?, 1, '', 1, 'speed.example.com', 8443)
|
||||||
|
`, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert source tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backup, err := source.ExportAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("export backup: %v", err)
|
||||||
|
}
|
||||||
|
if len(backup.Tunnels) != 1 {
|
||||||
|
t.Fatalf("expected one exported tunnel, got %d", len(backup.Tunnels))
|
||||||
|
}
|
||||||
|
if backup.Tunnels[0].ProbeTargetHost != "speed.example.com" || backup.Tunnels[0].ProbeTargetPort != 8443 {
|
||||||
|
t.Fatalf("unexpected exported probe target: %+v", backup.Tunnels[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
dest, err := Open(filepath.Join(t.TempDir(), "dest.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open dest repo: %v", err)
|
||||||
|
}
|
||||||
|
defer dest.Close()
|
||||||
|
|
||||||
|
result, err := dest.Import(backup, []string{"tunnels"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("import backup: %v", err)
|
||||||
|
}
|
||||||
|
if result.TunnelsImported != 1 {
|
||||||
|
t.Fatalf("expected one imported tunnel, got %d", result.TunnelsImported)
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := dest.ListTunnels()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("list imported tunnels: %v", err)
|
||||||
|
}
|
||||||
|
if len(items) != 1 {
|
||||||
|
t.Fatalf("expected one imported tunnel item, got %d", len(items))
|
||||||
|
}
|
||||||
|
if items[0]["probeTargetHost"] != "speed.example.com" || items[0]["probeTargetPort"] != 8443 {
|
||||||
|
t.Fatalf("unexpected imported probe target: %+v", items[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user