sshtunnel

package module
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 9 Imported by: 0

README

🚇 sshtunnel

Ultra simple SSH tunnelling for Go programs.

Installation

go get -u github.com/elliotchance/sshtunnel

Or better with dep:

dep ensure -add github.com/elliotchance/sshtunnel

Example

// Setup the tunnel, but do not yet start it yet.
tunnel := sshtunnel.NewSSHTunnel(
   // User and host of tunnel server, it will default to port 22
   // if not specified.
   "ec2-user@jumpbox.us-east-1.mydomain.com",

   // Pick ONE of the following authentication methods:
   sshtunnel.PrivateKeyFile("path/to/private/key.pem"), // 1. private key
   ssh.Password("password"),                            // 2. password
   sshtunnel.SSHAgent(),                                // 3. ssh-agent

   // The destination host and port of the actual server.
   "dqrsdfdssdfx.us-east-1.redshift.amazonaws.com:5439",
   
   // The local port you want to bind the remote port to.
   // Specifying "0" will lead to a random port.
   "8443",
)

// You can provide a logger for debugging, or remove this line to
// make it silent.
tunnel.Log = log.New(os.Stdout, "", log.Ldate | log.Lmicroseconds)

// Start the server in the background. You will need to wait a
// small amount of time for it to bind to the localhost port
// before you can start sending connections.
go tunnel.Start()
time.Sleep(100 * time.Millisecond)

// NewSSHTunnel will bind to a random port so that you can have
// multiple SSH tunnels available. The port is available through:
//   tunnel.Local.Port

// You can use any normal Go code to connect to the destination server
// through localhost. You may need to use 127.0.0.1 for some libraries.
//
// Here is an example of connecting to a PostgreSQL server:
conn := fmt.Sprintf("host=127.0.0.1 port=%d username=foo", tunnel.Local.Port)
db, err := sql.Open("postgres", conn)

// ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrivateKeyFile

func PrivateKeyFile(file string) ssh.AuthMethod

func PrivateKeyFileWithPassphrase

func PrivateKeyFileWithPassphrase(file string, passphrase string) ssh.AuthMethod

All credits go to the author fagnercarvalho

func SSHAgent

func SSHAgent() ssh.AuthMethod

Types

type Endpoint

type Endpoint struct {
	Host string
	Port int
	User string
}

func NewEndpoint

func NewEndpoint(s string) (*Endpoint, error)

NewEndpoint creates an Endpoint from a string that contains a user, host and port. Both User and Port are optional (depending on context). The host can be a domain name, IPv4 address or IPv6 address. If it's an IPv6, it must be enclosed in square brackets

func (*Endpoint) String

func (endpoint *Endpoint) String() string

type SSHTunnel

type SSHTunnel struct {
	Local                 *Endpoint
	Server                *Endpoint
	Remote                *Endpoint
	Config                *ssh.ClientConfig
	Log                   logger
	Conns                 []net.Conn
	SvrConns              []*ssh.Client
	MaxConnectionAttempts int
	// contains filtered or unexported fields
}

func NewSSHTunnel

func NewSSHTunnel(tunnel string, auth ssh.AuthMethod, destination string, localport string) (*SSHTunnel, error)

NewSSHTunnel creates a new single-use tunnel. Supplying "0" for localport will use a random port.

func (*SSHTunnel) Close

func (tunnel *SSHTunnel) Close()

func (*SSHTunnel) Listen

func (t *SSHTunnel) Listen() (net.Listener, error)

func (*SSHTunnel) Serve

func (tunnel *SSHTunnel) Serve(listener net.Listener) error

func (*SSHTunnel) Start

func (t *SSHTunnel) Start() error

Jump to

Keyboard shortcuts

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