From 8adca0efa27b5b07f2ada4db0bb357c4d8d0ff43 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:09:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=BB=E6=9C=BA=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E3=80=81=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E9=80=82?= =?UTF-8?q?=E9=85=8D=20IPv6=20(#2832)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/mysql/client.go | 4 ++++ backend/utils/ssh/ssh.go | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/utils/mysql/client.go b/backend/utils/mysql/client.go index f52651bcb..b2ca2a35b 100644 --- a/backend/utils/mysql/client.go +++ b/backend/utils/mysql/client.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "strings" "time" "github.com/1Panel-dev/1Panel/backend/buserr" @@ -31,6 +32,9 @@ func NewMysqlClient(conn client.DBInfo) (MysqlClient, error) { return client.NewLocal(connArgs, conn.Address, conn.Password, conn.Database), nil } + if strings.Contains(conn.Address, ":") { + conn.Address = fmt.Sprintf("[%s]", conn.Address) + } connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", conn.Username, conn.Password, conn.Address, conn.Port) db, err := sql.Open("mysql", connArgs) if err != nil { diff --git a/backend/utils/ssh/ssh.go b/backend/utils/ssh/ssh.go index bbe6d2377..f487182d4 100644 --- a/backend/utils/ssh/ssh.go +++ b/backend/utils/ssh/ssh.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io" + "strings" "sync" "time" @@ -26,6 +27,9 @@ type ConnInfo struct { } func (c *ConnInfo) NewClient() (*ConnInfo, error) { + if strings.Contains(c.Addr, ":") { + c.Addr = fmt.Sprintf("[%s]", c.Addr) + } config := &gossh.ClientConfig{} config.SetDefaults() addr := fmt.Sprintf("%s:%d", c.Addr, c.Port) @@ -45,7 +49,11 @@ func (c *ConnInfo) NewClient() (*ConnInfo, error) { config.Timeout = c.DialTimeOut config.HostKeyCallback = gossh.InsecureIgnoreHostKey() - client, err := gossh.Dial("tcp", addr, config) + proto := "tcp" + if strings.Contains(c.Addr, ":") { + proto = "tcp6" + } + client, err := gossh.Dial(proto, addr, config) if nil != err { return c, err }