diff --git a/main.go b/main.go index 91b9e545..7fdfeeb8 100644 --- a/main.go +++ b/main.go @@ -203,6 +203,19 @@ func updateSetting(port int, username string, password string) { } } +func migrateDb() { + inboundService := service.InboundService{} + + err := database.InitDB(config.GetDBPath()) + if err != nil { + log.Fatal(err) + } + fmt.Println("Start migrating database...") + inboundService.MigrationRequirements() + inboundService.RemoveOrphanedTraffics() + fmt.Println("Migration done!") +} + func main() { if len(os.Args) < 2 { runWebServer() @@ -245,6 +258,7 @@ func main() { fmt.Println("Commands:") fmt.Println(" run run web panel") fmt.Println(" v2-ui migrate form v2-ui") + fmt.Println(" migrate migrate form other/old x-ui") fmt.Println(" setting set settings") } @@ -262,6 +276,8 @@ func main() { return } runWebServer() + case "migrate": + migrateDb() case "v2-ui": err := v2uiCmd.Parse(os.Args[2:]) if err != nil { diff --git a/web/service/inbound.go b/web/service/inbound.go index 344c3910..2c9fea12 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -730,3 +730,44 @@ func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) } return inbounds, nil } + +func (s *InboundService) MigrationRequirements() { + db := database.GetDB() + var inbounds []*model.Inbound + err := db.Model(model.Inbound{}).Where("protocol IN (?)", []string{"vmess", "vless", "trojan"}).Find(&inbounds).Error + if err != nil && err != gorm.ErrRecordNotFound { + return + } + for inbound_index := range inbounds { + settings := map[string]interface{}{} + json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings) + clients, ok := settings["clients"].([]interface{}) + if ok { + var newClients []interface{} + for client_index := range clients { + c := clients[client_index].(map[string]interface{}) + + // Add email='' if it is not exists + if _, ok := c["email"]; !ok { + c["email"] = "" + } + + // Remove "flow": "xtls-rprx-direct" + if _, ok := c["flow"]; ok { + if c["flow"] == "xtls-rprx-direct" { + c["flow"] = "" + } + } + newClients = append(newClients, interface{}(c)) + } + settings["clients"] = newClients + modifiedSettings, err := json.MarshalIndent(settings, "", " ") + if err != nil { + return + } + + inbounds[inbound_index].Settings = string(modifiedSettings) + } + } + db.Save(inbounds) +} diff --git a/web/service/xray.go b/web/service/xray.go index 9c23f7a4..6008a493 100644 --- a/web/service/xray.go +++ b/web/service/xray.go @@ -69,7 +69,6 @@ func (s *XrayService) GetXrayConfig() (*xray.Config, error) { } s.inboundService.DisableInvalidClients() - s.inboundService.RemoveOrphanedTraffics() inbounds, err := s.inboundService.GetAllInbounds() if err != nil { @@ -124,7 +123,7 @@ func (s *XrayService) GetXrayConfig() (*xray.Config, error) { } settings["clients"] = final_clients - modifiedSettings, err := json.Marshal(settings) + modifiedSettings, err := json.MarshalIndent(settings, "", " ") if err != nil { return nil, err }