From 5534087cd88d9613460f69ed8c01ed2db76bfde5 Mon Sep 17 00:00:00 2001 From: r0zbeh Date: Thu, 21 Sep 2023 03:53:40 +0330 Subject: [PATCH] new files --- v2ui/db.go | 28 +++++++++++++++++++++++++++ v2ui/models.go | 41 ++++++++++++++++++++++++++++++++++++++++ v2ui/v2ui.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 v2ui/db.go create mode 100644 v2ui/models.go create mode 100644 v2ui/v2ui.go diff --git a/v2ui/db.go b/v2ui/db.go new file mode 100644 index 0000000..2745b9d --- /dev/null +++ b/v2ui/db.go @@ -0,0 +1,28 @@ +package v2ui + +import ( + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +var v2db *gorm.DB + +func initDB(dbPath string) error { + c := &gorm.Config{ + Logger: logger.Discard, + } + var err error + v2db, err = gorm.Open(sqlite.Open(dbPath), c) + if err != nil { + return err + } + + return nil +} + +func getV2Inbounds() ([]*V2Inbound, error) { + inbounds := make([]*V2Inbound, 0) + err := v2db.Model(V2Inbound{}).Find(&inbounds).Error + return inbounds, err +} diff --git a/v2ui/models.go b/v2ui/models.go new file mode 100644 index 0000000..9ac50ed --- /dev/null +++ b/v2ui/models.go @@ -0,0 +1,41 @@ +package v2ui + +import "x-ui/database/model" + +type V2Inbound struct { + Id int `gorm:"primaryKey;autoIncrement"` + Port int `gorm:"unique"` + Listen string + Protocol string + Settings string + StreamSettings string + Tag string `gorm:"unique"` + Sniffing string + Remark string + Up int64 + Down int64 + Enable bool +} + +func (i *V2Inbound) TableName() string { + return "inbound" +} + +func (i *V2Inbound) ToInbound(userId int) *model.Inbound { + return &model.Inbound{ + UserId: userId, + Up: i.Up, + Down: i.Down, + Total: 0, + Remark: i.Remark, + Enable: i.Enable, + ExpiryTime: 0, + Listen: i.Listen, + Port: i.Port, + Protocol: model.Protocol(i.Protocol), + Settings: i.Settings, + StreamSettings: i.StreamSettings, + Tag: i.Tag, + Sniffing: i.Sniffing, + } +} diff --git a/v2ui/v2ui.go b/v2ui/v2ui.go new file mode 100644 index 0000000..57d673c --- /dev/null +++ b/v2ui/v2ui.go @@ -0,0 +1,51 @@ +package v2ui + +import ( + "fmt" + "x-ui/config" + "x-ui/database" + "x-ui/database/model" + "x-ui/util/common" + "x-ui/web/service" +) + +func MigrateFromV2UI(dbPath string) error { + err := initDB(dbPath) + if err != nil { + return common.NewError("init v2-ui database failed:", err) + } + err = database.InitDB(config.GetDBPath()) + if err != nil { + return common.NewError("init x-ui database failed:", err) + } + + v2Inbounds, err := getV2Inbounds() + if err != nil { + return common.NewError("get v2-ui inbounds failed:", err) + } + if len(v2Inbounds) == 0 { + fmt.Println("migrate v2-ui inbounds success: 0") + return nil + } + + userService := service.UserService{} + user, err := userService.GetFirstUser() + if err != nil { + return common.NewError("get x-ui user failed:", err) + } + + inbounds := make([]*model.Inbound, 0) + for _, v2inbound := range v2Inbounds { + inbounds = append(inbounds, v2inbound.ToInbound(user.Id)) + } + + inboundService := service.InboundService{} + err = inboundService.AddInbounds(inbounds) + if err != nil { + return common.NewError("add x-ui inbounds failed:", err) + } + + fmt.Println("migrate v2-ui inbounds success:", len(inbounds)) + + return nil +}