Go 通过socks5代理 访问mysql
原文链接: Go 通过socks5代理 访问mysql
注册 mysql 的 ssh 代理
func main(){
var dsn = "root:123456@mysql_ssh(localhost:3306)/shizhi/?parseTime=true&loc=Local"
mysql.RegisterDial("mysql_ssh", func(addr string) (conn net.Conn, err error) {
passAuth := []ssh.AuthMethod{ssh.Password("password")}
client, err := ssh.Dial("tcp", "127.0.0.1:22", &ssh.ClientConfig{
User: "root",
Auth: passAuth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
return
}
conn, err = client.Dial("tcp", addr)
if err != nil {
return
}
return
})
db, err := sql.Open("mysql", dsn )
if err != nil {
return
}
}
用 Golang 通过 ssh 访问 Mysql
func main() {
config := ssh.Default.WithPassword("HR2018!!").WithHost("192.168.5.157")
client, err := ssh.New(config)
// client, err := ssh.NewClient("localhost", "22", "root", "ubuntu")
if err != nil {
panic(err)
}
defer client.Close()
fmt.Println(client.Output("id"))
// 1. 注册自定义的Dial
// Now we register the ViaSSHDialer with the ssh connection as a parameter
mysql.RegisterDial("mysql+ssh", func(addr string) (net.Conn, error) {
return client.SSHClient.Dial("tcp", addr)
})
dbUser := "root" // DB username
dbPass := "" // DB Password
dbHost := "localhost:3306" // DB Hostname/IP
dbName := "shizhi" // Database name
// 2. 使用自定义的 Dial
// And now we can use our new driver with the regular mysql connection string tunneled through the SSH connection
if db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@mysql+ssh(%s)/%s", dbUser, dbPass, dbHost, dbName)); err == nil {
if rows, err := db.Query("SELECT user, host FROM mysql.user "); err == nil {
for rows.Next() {
var id string
var name string
rows.Scan(&id, &name)
fmt.Printf("ID: %s\tName: %s\n", id, name)
}
rows.Close()
} else {
fmt.Printf("Failure: %s", err.Error())
}
db.Close()
fmt.Printf("Successfully connected to the db\n")
}
}
用 Golang 通过 Proxy 访问 Mysql
// all_proxy=socks5://127.0.0.1:1080
import (
"database/sql"
"net"
"golang.org/x/net/proxy"
"github.com/go-sql-driver/mysql"
)
func main() {
dialer := proxy.FromEnvironment()
mysql.RegisterDial("tcp", func(network string) (net.Conn, error) {
return dialer.Dial("tcp", network)
})
db, err := sql.Open("mysql", dsn)
....
}
用 Golang 通过 SOCKS5 访问 Mysql
//InitDB starts the db connection
func InitDB(c *Config) (*sqlx.DB, error) {
fmt.Println(c.DBsocks)
fixieData := strings.Split(c.DBsocks, "@")
fixieAddr := fixieData[1]
authData := strings.Split(fixieData[0], ":")
auth := proxy.Auth{
User: authData[0],
Password: authData[1],
}
dialer, err := proxy.SOCKS5("tcp", fixieAddr, &auth, proxy.Direct)
if err != nil {
fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
os.Exit(1)
}
mysql.RegisterDial("fixieDial", func(addr string) (net.Conn, error) {
return dialer.Dial("tcp", addr)
})
cnx := fmt.Sprintf("%s:%s@fixieDial(%s:3306)/%s?allowOldPasswords=true",
c.DBuser, c.DBpass, c.DBhost, c.DBname)
fmt.Println(cnx)
db, err := sqlx.Connect("mysql", cnx)
if err != nil {
fmt.Println("Could not connect")
log.Fatalln(err)
return nil, err
}
fmt.Println("Connection to db succeed")
return db, nil
}