Advertisement
Guest User

Untitled

a guest
Nov 7th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "database/sql/driver"
  6. "fmt"
  7. "net"
  8. "os"
  9. "time"
  10.  
  11. "github.com/lib/pq"
  12. "golang.org/x/crypto/ssh"
  13. "golang.org/x/crypto/ssh/agent"
  14. )
  15.  
  16. type ViaSSHDialer struct {
  17. client *ssh.Client
  18. }
  19.  
  20. func (self *ViaSSHDialer) Open(s string) (_ driver.Conn, err error) {
  21. return pq.DialOpen(self, s)
  22. }
  23.  
  24. func (self *ViaSSHDialer) Dial(network, address string) (net.Conn, error) {
  25. return self.client.Dial(network, address)
  26. }
  27.  
  28. func (self *ViaSSHDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
  29. return self.client.Dial(network, address)
  30. }
  31.  
  32. func main() {
  33.  
  34. sshHost := "example.com" // SSH Server Hostname/IP
  35. sshPort := 22 // SSH Port
  36. sshUser := "ssh-user" // SSH Username
  37. sshPass := "ssh-pass" // Empty string for no password
  38. dbUser := "user" // DB username
  39. dbPass := "password" // DB Password
  40. dbHost := "localhost" // DB Hostname/IP
  41. dbName := "database" // Database name
  42.  
  43. var agentClient *agent.Client
  44. // Establish a connection to the local ssh-agent
  45. if conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
  46. defer conn.Close()
  47.  
  48. // Create a new instance of the ssh agent
  49. agentClient = agent.NewClient(conn)
  50. }
  51.  
  52. // The client configuration with configuration option to use the ssh-agent
  53. sshConfig := &ssh.ClientConfig{
  54. User: sshUser,
  55. Auth: []ssh.AuthMethod{},
  56. }
  57.  
  58. // When the agentClient connection succeeded, add them as AuthMethod
  59. if agentClient != nil {
  60. sshConfig.Auth = append(sshConfig.Auth, ssh.PublicKeysCallback(agentClient.Signers))
  61. }
  62. // When there's a non empty password add the password AuthMethod
  63. if sshPass != "" {
  64. sshConfig.Auth = append(sshConfig.Auth, ssh.PasswordCallback(func() (string, error) {
  65. return sshPass, nil
  66. }))
  67. }
  68.  
  69. // Connect to the SSH Server
  70. if sshcon, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", sshHost, sshPort), sshConfig); err == nil {
  71. defer sshcon.Close()
  72.  
  73. // Now we register the ViaSSHDialer with the ssh connection as a parameter
  74. sql.Register("postgres+ssh", &ViaSSHDialer{sshcon})
  75.  
  76. // And now we can use our new driver with the regular postgres connection string tunneled through the SSH connection
  77. if db, err := sql.Open("postgres+ssh", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", dbUser, dbPass, dbHost, dbName)); err == nil {
  78.  
  79. fmt.Printf("Successfully connected to the db\n")
  80.  
  81. if rows, err := db.Query("SELECT id, name FROM table ORDER BY id"); err == nil {
  82. for rows.Next() {
  83. var id int64
  84. var name string
  85. rows.Scan(&id, &name)
  86. fmt.Printf("ID: %d Name: %s\n", id, name)
  87. }
  88. rows.Close()
  89. }
  90.  
  91. db.Close()
  92.  
  93. } else {
  94.  
  95. fmt.Printf("Failed to connect to the db: %s\n", err.Error())
  96. }
  97.  
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement