mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-05 17:46:37 +08:00
fix(backend): normalize strategy data and proxy ip parsing
This commit is contained in:
@@ -719,7 +719,7 @@ func (r *Repository) ListForwards() ([]map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
rows, err := r.db.Query(`
|
||||
SELECT f.id, f.user_id, f.user_name, f.name, f.tunnel_id, COALESCE(t.name, ''), f.remote_addr, f.strategy,
|
||||
SELECT f.id, f.user_id, f.user_name, f.name, f.tunnel_id, COALESCE(t.name, ''), f.remote_addr, COALESCE(f.strategy, 'fifo'),
|
||||
f.in_flow, f.out_flow, f.created_time, f.status, f.inx
|
||||
FROM forward f
|
||||
LEFT JOIN tunnel t ON t.id = f.tunnel_id
|
||||
@@ -1297,7 +1297,7 @@ func bootstrapSchema(db *store.DB, schemaSQL, seedSQL string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const currentSchemaVersion = 1
|
||||
const currentSchemaVersion = 2
|
||||
|
||||
func getSchemaVersion(db *store.DB) int {
|
||||
_, _ = db.Exec(`CREATE TABLE IF NOT EXISTS schema_version (version INTEGER NOT NULL DEFAULT 0)`)
|
||||
@@ -1367,6 +1367,27 @@ func migrateSchema(db *store.DB) error {
|
||||
}
|
||||
}
|
||||
|
||||
normalizeStrategy := func(table, defaultValue string) error {
|
||||
_, err := db.Exec(fmt.Sprintf("UPDATE %s SET strategy = ? WHERE strategy IS NULL", table), defaultValue)
|
||||
if err != nil {
|
||||
if isMissingTableError(db.Dialect(), err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("normalize %s.strategy: %w", table, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := normalizeStrategy("forward", "fifo"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := normalizeStrategy("chain_tunnel", "round"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := normalizeStrategy("peer_share_runtime", "round"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if db.Dialect() == store.DialectPostgres {
|
||||
if err := ensurePostgresIDDefaults(db); err != nil {
|
||||
return err
|
||||
@@ -1537,6 +1558,17 @@ func isMissingColumnError(dialect store.Dialect, err error) bool {
|
||||
return strings.Contains(msg, "no such column")
|
||||
}
|
||||
|
||||
func isMissingTableError(dialect store.Dialect, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
if dialect == store.DialectPostgres {
|
||||
return strings.Contains(msg, "relation") && strings.Contains(msg, "does not exist")
|
||||
}
|
||||
return strings.Contains(msg, "no such table")
|
||||
}
|
||||
|
||||
func (r *Repository) CreatePeerShare(share *PeerShare) error {
|
||||
if r == nil || r.db == nil {
|
||||
return errors.New("repository not initialized")
|
||||
|
||||
Reference in New Issue
Block a user