fix: 主机连接、数据库连接适配 IPv6 (#2832)

This commit is contained in:
ssongliu 2023-11-07 14:09:28 +08:00 committed by GitHub
parent fbc34c2847
commit 8adca0efa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -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 {

View File

@ -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
}