mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-27 12:39:01 +08:00
fix: 主机连接、数据库连接适配 IPv6 (#2832)
This commit is contained in:
parent
fbc34c2847
commit
8adca0efa2
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user