From f0367b32fd524547b00b81c53c16a8b58f70840f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?MisakaNo=20=E3=81=AE=20=E5=B0=8F=E7=A0=B4=E7=AB=99?= <122191366+Misaka-blog@users.noreply.github.com> Date: Mon, 15 May 2023 18:35:10 +0800 Subject: [PATCH] Add import database (#58) * fix * Bump google.golang.org/grpc from 1.54.0 to 1.55.0 (#40) Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.54.0 to 1.55.0. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.54.0...v1.55.0) --- updated-dependencies: - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * add * warp * fix * fix * Bump github.com/shirou/gopsutil/v3 from 3.23.3 to 3.23.4 (#43) Bumps [github.com/shirou/gopsutil/v3](https://github.com/shirou/gopsutil) from 3.23.3 to 3.23.4. - [Release notes](https://github.com/shirou/gopsutil/releases) - [Commits](https://github.com/shirou/gopsutil/compare/v3.23.3...v3.23.4) --- updated-dependencies: - dependency-name: github.com/shirou/gopsutil/v3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * fix * project info * optimize * Bump gorm.io/gorm from 1.25.0 to 1.25.1 (#45) Bumps [gorm.io/gorm](https://github.com/go-gorm/gorm) from 1.25.0 to 1.25.1. - [Release notes](https://github.com/go-gorm/gorm/releases) - [Commits](https://github.com/go-gorm/gorm/compare/v1.25.0...v1.25.1) --- updated-dependencies: - dependency-name: gorm.io/gorm dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Show x-ui log on panel * add * change docker source * fix * show xray config * Fix ciphersuites bug * fix * modify * Add 386 support * v0.3.3.10-test * import database --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- config/version | 2 +- database/db.go | 19 ++++++-- web/controller/server.go | 23 +++++++++ web/html/xui/index.html | 83 +++++++++++++++++++++++++++++++ web/service/server.go | 103 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 226 insertions(+), 4 deletions(-) diff --git a/config/version b/config/version index 5eea348..2c7d265 100644 --- a/config/version +++ b/config/version @@ -1 +1 @@ -v0.3.3.10-test \ No newline at end of file +v0.3.3.11 \ No newline at end of file diff --git a/database/db.go b/database/db.go index 3273874..bb93013 100644 --- a/database/db.go +++ b/database/db.go @@ -1,14 +1,17 @@ package database import ( - "gorm.io/driver/sqlite" - "gorm.io/gorm" - "gorm.io/gorm/logger" + "bytes" + "io" "io/fs" "os" "path" "x-ui/config" "x-ui/database/model" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" ) var db *gorm.DB @@ -87,3 +90,13 @@ func GetDB() *gorm.DB { func IsNotFound(err error) bool { return err == gorm.ErrRecordNotFound } + +func IsSQLiteDB(file io.Reader) (bool, error) { + signature := []byte("SQLite format 3\x00") + buf := make([]byte, len(signature)) + _, err := file.Read(buf) + if err != nil { + return false, err + } + return bytes.Equal(buf, signature), nil +} diff --git a/web/controller/server.go b/web/controller/server.go index 79599d8..a78215d 100644 --- a/web/controller/server.go +++ b/web/controller/server.go @@ -62,6 +62,7 @@ func (a *ServerController) initRouter(g *gin.RouterGroup) { g.POST("/installGeosite/:version", a.installGeosite) g.GET("/getDatabase", a.getDatabase) g.POST("/getConfigJson", a.getConfigJson) + g.POST("/importDatabase", a.importDatabase) } func (a *ServerController) refreshStatus() { @@ -226,3 +227,25 @@ func (a *ServerController) getConfigJson(c *gin.Context) { } jsonObj(c, configJson, nil) } + +func (a *ServerController) importDatabase(c *gin.Context) { + // Get the file from the request body + file, _, err := c.Request.FormFile("db") + if err != nil { + jsonMsg(c, "Error reading db file", err) + return + } + defer file.Close() + // Always restart Xray before return + defer a.serverService.RestartXrayService() + defer func() { + a.lastGetStatusTime = time.Now() + }() + // Import it + err = a.serverService.ImportDatabase(file) + if err != nil { + jsonMsg(c, "", err) + return + } + jsonObj(c, "Import DB", nil) +} \ No newline at end of file diff --git a/web/html/xui/index.html b/web/html/xui/index.html index a84934b..a126163 100644 --- a/web/html/xui/index.html +++ b/web/html/xui/index.html @@ -86,6 +86,7 @@ 版本切换 查看日志 查看配置文件 + 备份还原 @@ -217,6 +218,22 @@

点击你想更新的版本

+ +

+ + [[ backupModal.description ]] +

+ + + [[ backupModal.exportText ]] + + + [[ backupModal.importText ]] + + +
+

点击你想更新的版本

@@ -260,6 +277,29 @@

点击你想更新的版本

{{template "textModal"}}