-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |