|
@@ -91,6 +91,9 @@ func initModels() error {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ if err := dropLegacyInboundPortUnique(); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
if err := migrateHostVerifyPeerCertByNameColumn(); err != nil {
|
|
if err := migrateHostVerifyPeerCertByNameColumn(); err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -118,6 +121,12 @@ func initModels() error {
|
|
|
if err := migrateLegacySocksInboundsToMixed(); err != nil {
|
|
if err := migrateLegacySocksInboundsToMixed(); err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
+ if err := migrateShadowsocksRemovedCiphers(); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := migrateVmessRemovedSecurities(); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
if IsPostgres() {
|
|
if IsPostgres() {
|
|
|
if err := resyncPostgresSequences(db, models); err != nil {
|
|
if err := resyncPostgresSequences(db, models); err != nil {
|
|
|
log.Printf("Error resyncing postgres sequences: %v", err)
|
|
log.Printf("Error resyncing postgres sequences: %v", err)
|
|
@@ -160,6 +169,121 @@ func dropLegacyForeignKeys() error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type sqliteIndexListRow struct {
|
|
|
|
|
+ Name string `gorm:"column:name"`
|
|
|
|
|
+ Unique int `gorm:"column:unique"`
|
|
|
|
|
+ Origin string `gorm:"column:origin"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func sqliteUniquePortIndexes() (autoIndexes, explicitIndexes []string, err error) {
|
|
|
|
|
+ var list []sqliteIndexListRow
|
|
|
|
|
+ if err = db.Raw(`PRAGMA index_list('inbounds')`).Scan(&list).Error; err != nil {
|
|
|
|
|
+ return nil, nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, idx := range list {
|
|
|
|
|
+ if idx.Unique != 1 {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ var cols []struct {
|
|
|
|
|
+ Name string `gorm:"column:name"`
|
|
|
|
|
+ }
|
|
|
|
|
+ if err = db.Raw(`PRAGMA index_info("` + idx.Name + `")`).Scan(&cols).Error; err != nil {
|
|
|
|
|
+ return nil, nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(cols) != 1 || cols[0].Name != "port" {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ if idx.Origin == "c" {
|
|
|
|
|
+ explicitIndexes = append(explicitIndexes, idx.Name)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ autoIndexes = append(autoIndexes, idx.Name)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return autoIndexes, explicitIndexes, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// dropLegacyInboundPortUnique removes the pre-multi-node UNIQUE on inbounds.port,
|
|
|
|
|
+// which AutoMigrate never drops and which blocks cross-node port reuse on old SQLite DBs.
|
|
|
|
|
+func dropLegacyInboundPortUnique() error {
|
|
|
|
|
+ if IsPostgres() {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ autoIndexes, explicitIndexes, err := sqliteUniquePortIndexes()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, name := range explicitIndexes {
|
|
|
|
|
+ if err := db.Exec(`DROP INDEX IF EXISTS "` + name + `"`).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(autoIndexes) == 0 {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ log.Printf("Rebuilding inbounds table to drop the legacy UNIQUE constraint on port")
|
|
|
|
|
+ return rebuildInboundsWithoutInlineUniquePort()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func sqliteTableColumns(tx *gorm.DB, table string) ([]string, error) {
|
|
|
|
|
+ var rows []struct {
|
|
|
|
|
+ Name string `gorm:"column:name"`
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := tx.Raw(`PRAGMA table_info("` + table + `")`).Scan(&rows).Error; err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ cols := make([]string, 0, len(rows))
|
|
|
|
|
+ for _, r := range rows {
|
|
|
|
|
+ cols = append(cols, r.Name)
|
|
|
|
|
+ }
|
|
|
|
|
+ return cols, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func rebuildInboundsWithoutInlineUniquePort() error {
|
|
|
|
|
+ return db.Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
+ var list []sqliteIndexListRow
|
|
|
|
|
+ if err := tx.Raw(`PRAGMA index_list('inbounds')`).Scan(&list).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, idx := range list {
|
|
|
|
|
+ if idx.Origin != "c" {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := tx.Exec(`DROP INDEX IF EXISTS "` + idx.Name + `"`).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := tx.Exec(`ALTER TABLE inbounds RENAME TO inbounds_legacy_rebuild`).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := tx.Migrator().CreateTable(&model.Inbound{}); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ newCols, err := sqliteTableColumns(tx, "inbounds")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ oldCols, err := sqliteTableColumns(tx, "inbounds_legacy_rebuild")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ oldSet := make(map[string]struct{}, len(oldCols))
|
|
|
|
|
+ for _, c := range oldCols {
|
|
|
|
|
+ oldSet[c] = struct{}{}
|
|
|
|
|
+ }
|
|
|
|
|
+ shared := make([]string, 0, len(newCols))
|
|
|
|
|
+ for _, c := range newCols {
|
|
|
|
|
+ if _, ok := oldSet[c]; ok {
|
|
|
|
|
+ shared = append(shared, `"`+c+`"`)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ colList := strings.Join(shared, ", ")
|
|
|
|
|
+ if err := tx.Exec(`INSERT INTO inbounds (` + colList + `) SELECT ` + colList + ` FROM inbounds_legacy_rebuild`).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ return tx.Exec(`DROP TABLE inbounds_legacy_rebuild`).Error
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func migrateHostVerifyPeerCertByNameColumn() error {
|
|
func migrateHostVerifyPeerCertByNameColumn() error {
|
|
|
if !db.Migrator().HasColumn(&model.Host{}, "verify_peer_cert_by_name") {
|
|
if !db.Migrator().HasColumn(&model.Host{}, "verify_peer_cert_by_name") {
|
|
|
return nil
|
|
return nil
|
|
@@ -636,6 +760,125 @@ func migrateLegacySocksInboundsToMixed() error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// migrateShadowsocksRemovedCiphers rewrites shadowsocks inbounds still using
|
|
|
|
|
+// the "none"/"plain" ciphers that xray-core v26.7.11 removed; one such row
|
|
|
|
|
+// makes the whole generated config unbuildable and keeps xray from starting.
|
|
|
|
|
+func migrateShadowsocksRemovedCiphers() error {
|
|
|
|
|
+ var inbounds []model.Inbound
|
|
|
|
|
+ if err := db.Where("protocol = ?", model.Shadowsocks).Find(&inbounds).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ migrated := int64(0)
|
|
|
|
|
+ for _, inbound := range inbounds {
|
|
|
|
|
+ if strings.TrimSpace(inbound.Settings) == "" {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ var settings map[string]any
|
|
|
|
|
+ if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ changed := false
|
|
|
|
|
+ if method, _ := settings["method"].(string); method != "" {
|
|
|
|
|
+ if replacement, removed := model.ReplaceRemovedShadowsocksCipher(method); removed {
|
|
|
|
|
+ settings["method"] = replacement
|
|
|
|
|
+ changed = true
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if clients, ok := settings["clients"].([]any); ok {
|
|
|
|
|
+ for i := range clients {
|
|
|
|
|
+ cm, ok := clients[i].(map[string]any)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ method, _ := cm["method"].(string)
|
|
|
|
|
+ if replacement, removed := model.ReplaceRemovedShadowsocksCipher(method); removed {
|
|
|
|
|
+ cm["method"] = replacement
|
|
|
|
|
+ clients[i] = cm
|
|
|
|
|
+ changed = true
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if !changed {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ newSettings, err := json.MarshalIndent(settings, "", " ")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Printf("migrateShadowsocksRemovedCiphers: skip inbound %d (marshal failed): %v", inbound.Id, err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := db.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
|
|
|
|
|
+ Update("settings", string(newSettings)).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ migrated++
|
|
|
|
|
+ }
|
|
|
|
|
+ if migrated > 0 {
|
|
|
|
|
+ log.Printf("Rewrote removed shadowsocks cipher on %d inbound(s)", migrated)
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// migrateVmessRemovedSecurities rewrites the vmess "none"/"zero" security
|
|
|
|
|
+// values that xray-core v26.7.11 removed to "auto" (what the core now treats
|
|
|
|
|
+// them as), on both the clients column and each vmess inbound's settings.
|
|
|
|
|
+func migrateVmessRemovedSecurities() error {
|
|
|
|
|
+ res := db.Exec("UPDATE clients SET security = 'auto' WHERE security IN ('none', 'zero')")
|
|
|
|
|
+ if res.Error != nil {
|
|
|
|
|
+ log.Printf("Error migrating removed vmess security values on clients: %v", res.Error)
|
|
|
|
|
+ return res.Error
|
|
|
|
|
+ }
|
|
|
|
|
+ if res.RowsAffected > 0 {
|
|
|
|
|
+ log.Printf("Migrated %d client(s) off removed vmess security values", res.RowsAffected)
|
|
|
|
|
+ }
|
|
|
|
|
+ var inbounds []model.Inbound
|
|
|
|
|
+ if err := db.Where("protocol = ?", model.VMESS).Find(&inbounds).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ migrated := int64(0)
|
|
|
|
|
+ for _, inbound := range inbounds {
|
|
|
|
|
+ if strings.TrimSpace(inbound.Settings) == "" {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ var settings map[string]any
|
|
|
|
|
+ if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ clients, ok := settings["clients"].([]any)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ changed := false
|
|
|
|
|
+ for i := range clients {
|
|
|
|
|
+ cm, ok := clients[i].(map[string]any)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ if security, _ := cm["security"].(string); security == "none" || security == "zero" {
|
|
|
|
|
+ cm["security"] = "auto"
|
|
|
|
|
+ clients[i] = cm
|
|
|
|
|
+ changed = true
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if !changed {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ newSettings, err := json.MarshalIndent(settings, "", " ")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Printf("migrateVmessRemovedSecurities: skip inbound %d (marshal failed): %v", inbound.Id, err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := db.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
|
|
|
|
|
+ Update("settings", string(newSettings)).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ migrated++
|
|
|
|
|
+ }
|
|
|
|
|
+ if migrated > 0 {
|
|
|
|
|
+ log.Printf("Rewrote removed vmess security values on %d inbound(s)", migrated)
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// normalizeInboundSubSortIndex lifts sub_sort_index values below the 1-based
|
|
// normalizeInboundSubSortIndex lifts sub_sort_index values below the 1-based
|
|
|
// minimum (rows written by builds that defaulted the column to 0, or by nodes
|
|
// minimum (rows written by builds that defaulted the column to 0, or by nodes
|
|
|
// predating the field) so they cannot sort ahead of explicitly ranked inbounds.
|
|
// predating the field) so they cannot sort ahead of explicitly ranked inbounds.
|
|
@@ -810,7 +1053,7 @@ func runSeeders(isUsersEmpty bool) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if empty && isUsersEmpty {
|
|
if empty && isUsersEmpty {
|
|
|
- seeders := []string{"UserPasswordHash", "ClientsTable", "InboundClientsArrayFix", "InboundClientTgIdFix", "InboundClientSubIdFix", "FreedomFinalRulesReverseFix", "ApiTokensHash", "LegacyProxySettingsCleanup", "WireguardPeersToClients", "MtprotoSecretsToClients"}
|
|
|
|
|
|
|
+ seeders := []string{"UserPasswordHash", "ClientsTable", "InboundClientsArrayFix", "InboundClientTgIdFix", "InboundClientSubIdFix", "FreedomFinalRulesReverseFix", "ApiTokensHash", "LegacyProxySettingsCleanup", "WireguardPeersToClients", "MtprotoSecretsToClients", "NodeInboundsAdopted"}
|
|
|
for _, name := range seeders {
|
|
for _, name := range seeders {
|
|
|
if err := db.Create(&model.HistoryOfSeeders{SeederName: name}).Error; err != nil {
|
|
if err := db.Create(&model.HistoryOfSeeders{SeederName: name}).Error; err != nil {
|
|
|
return err
|
|
return err
|
|
@@ -903,6 +1146,12 @@ func runSeeders(isUsersEmpty bool) error {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if !slices.Contains(seedersHistory, "NodeInboundsAdopted") {
|
|
|
|
|
+ if err := seedNodeInboundsAdopted(); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if err := seedHostsFromExternalProxy(); err != nil {
|
|
if err := seedHostsFromExternalProxy(); err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -937,6 +1186,17 @@ func runSeeders(isUsersEmpty bool) error {
|
|
|
return normalizeSettingPaths()
|
|
return normalizeSettingPaths()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// seedNodeInboundsAdopted keeps the pre-existing reconcile behavior for nodes
|
|
|
|
|
+// that were already syncing before the inbounds_adopted_at gate was introduced.
|
|
|
|
|
+func seedNodeInboundsAdopted() error {
|
|
|
|
|
+ if err := db.Model(&model.Node{}).
|
|
|
|
|
+ Where("inbounds_adopted_at = 0").
|
|
|
|
|
+ Update("inbounds_adopted_at", time.Now().Unix()).Error; err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ return db.Create(&model.HistoryOfSeeders{SeederName: "NodeInboundsAdopted"}).Error
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func seedHostGroupIds() error {
|
|
func seedHostGroupIds() error {
|
|
|
var history []string
|
|
var history []string
|
|
|
if err := db.Model(&model.HistoryOfSeeders{}).Pluck("seeder_name", &history).Error; err != nil {
|
|
if err := db.Model(&model.HistoryOfSeeders{}).Pluck("seeder_name", &history).Error; err != nil {
|