tunnel

package module
v0.0.0-...-8a8c1eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

postgres-ssh-proxy

example of how to use SSH proxy for Postgres connection

import "github.com/qwertmax/postgres-ssh-proxy"

func main(){
	listener, err := tunnel.SetupSSHTunnel(tunnel.SSHTunnelConfig{
		SSHAddress:        "sshServer:22",
		SSHUser:           "user",
		SSHPrivateKeyPath: "key.pem",
		LocalEndpoint:     "localhost:0",
		RemoteEndpoint:    "remoteHost:5432",
	})
	if err != nil {
		log.Fatalf("Failed to start SSH tunnel: %v", err)
	}
	defer listener.Close()
	localAddr := listener.Addr().(*net.TCPAddr)

	ctx := context.TODO()
	connString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", "pgUser", "pgPassword", localAddr.IP, localAddr.Port, "dbName")
	pgConn, err := pgx.Connect(ctx, connString)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
		os.Exit(1)
	}
	defer pgConn.Close(ctx)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupSSHTunnel

func SetupSSHTunnel(config SSHTunnelConfig) (net.Listener, error)

Types

type SSHTunnelConfig

type SSHTunnelConfig struct {
	SSHAddress        string
	SSHUser           string
	SSHPrivateKeyPath string
	LocalEndpoint     string
	RemoteEndpoint    string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL