denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 501 forks source link

Question: Connect via SSH Tunneling #665

Closed iambudi closed 3 years ago

iambudi commented 3 years ago

Hi All,

In Go MySQL driver we can use mysql.RegisterDialContext() to connect to MySQL database host via *ssh.Client tunneling.

Any guidance on where to use it in Go MSSQL?

Thanks.

iambudi commented 3 years ago

Well found it after digging the code. Please correct me if this is the proper way to set custom dialer?

// Implementation of Dialer interface
type SSHTunnelDialer struct {
    SSHClient *ssh.Client
}

func (d SSHTunnelDialer) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
    log.Println("Dialing mssql server via tunnel...")
    return d.SSHClient.Dial(network, addr)
}

connector, _ := mssql.NewConnector("sqlserver://my-dsn-connection")
connector.Dialer = SSHTunnelDialer{} 
db := sql.OpenDB(connector)