From a359bff5313972b7de8c236b7ecea4f94f7caf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sat, 6 Nov 2021 18:35:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E8=A3=85=E6=97=B6=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=B8=8A=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E7=9A=84MySQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/actions/default/setup/detectDB.go | 61 +++++++++++++++++++ internal/web/actions/default/setup/init.go | 1 + web/views/@default/setup/index.html | 9 +-- web/views/@default/setup/index.js | 12 ++++ 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 internal/web/actions/default/setup/detectDB.go diff --git a/internal/web/actions/default/setup/detectDB.go b/internal/web/actions/default/setup/detectDB.go new file mode 100644 index 00000000..5db279e0 --- /dev/null +++ b/internal/web/actions/default/setup/detectDB.go @@ -0,0 +1,61 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package setup + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/configutils" + "github.com/iwind/TeaGo/dbs" + "github.com/iwind/TeaGo/maps" + "net" + "strings" + "time" +) + +// DetectDBAction 尝试从本地服务器中发现MySQL +type DetectDBAction struct { + actionutils.ParentAction +} + +func (this *DetectDBAction) RunPost(params struct{}) { + var localHost = "" + var localPort = "" + var localUsername = "" + var localPassword = "" + + // 本地的3306端口是否可以连接 + conn, err := net.DialTimeout("tcp", "127.0.0.1:3306", 3*time.Second) + if err == nil { + _ = conn.Close() + localHost = "127.0.0.1" + localPort = "3306" + + var username = "root" + for _, pass := range []string{"", "123456", "654321", "Aa_123456"} { + db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{ + Driver: "mysql", + Dsn: username + ":" + pass + "@tcp(" + configutils.QuoteIP(localHost) + ":" + localPort + ")/edges11111", + Prefix: "", + }) + if err == nil { + err = db.Raw().Ping() + _ = db.Close() + + if err == nil || strings.Contains(err.Error(), "Error 1049") { + localUsername = username + localPassword = pass + break + } + } + } + } + + this.Data["localDB"] = maps.Map{ + "host": localHost, + "port": localPort, + "username": localUsername, + "password": localPassword, + } + + this.Success() +} diff --git a/internal/web/actions/default/setup/init.go b/internal/web/actions/default/setup/init.go index 0ff4ddc3..18b43509 100644 --- a/internal/web/actions/default/setup/init.go +++ b/internal/web/actions/default/setup/init.go @@ -13,6 +13,7 @@ func init() { Post("/validateAdmin", new(ValidateAdminAction)). Post("/install", new(InstallAction)). Post("/status", new(StatusAction)). + Post("/detectDB", new(DetectDBAction)). EndAll() }) } diff --git a/web/views/@default/setup/index.html b/web/views/@default/setup/index.html index 9a131666..4d5b8875 100644 --- a/web/views/@default/setup/index.html +++ b/web/views/@default/setup/index.html @@ -149,13 +149,14 @@ MySQL主机地址 * - + +

已经自动填入从当前服务器发现的MySQL数据库信息。

数据库连接端口 * - + @@ -168,14 +169,14 @@ 连接用户名 * - +

此用户需要可以在数据库中有操作数据和创建数据表的权限。

连接密码 - +

连接数据库所需密码,没有密码的话就不需要填写。

diff --git a/web/views/@default/setup/index.js b/web/views/@default/setup/index.js index 79cae444..19e4fbc4 100644 --- a/web/views/@default/setup/index.js +++ b/web/views/@default/setup/index.js @@ -34,6 +34,7 @@ Tea.context(function () { this.apiSuccess = function (resp) { this.step = this.STEP_DB + this.detectDB() this.apiNodeInfo = resp.data.apiNode if (this.apiNodeMode == "new") { @@ -56,7 +57,18 @@ Tea.context(function () { // 数据库 this.dbInfo = {} + this.localDB = {"host": "", "port": "", "username": "", "port": ""} + this.localDBHost = "" this.dbRequesting = false + + this.detectDB = function () { + this.$post(".detectDB") + .success(function (resp) { + this.localDB = resp.data.localDB + this.localDBHost = this.localDB.host + }) + } + this.dbSubmit = function () { this.dbRequesting = true }