sshtunnel

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: MIT Imports: 7 Imported by: 0

README

🚇 sshtunnel

Ultra simple SSH tunnelling for Go programs.

Fork notes

This is a fork of elliotchance/sshtunnel at v1.4.0:

  • the API is backwards incompatible!
  • the API will break again.

To make this more evident, the Go module name is now github.com/marco-m/sshtunnel.

Installation

go get -u github.com/marco-m/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 an ephemeral port, which can be read
   // either from listener.Addr or from tunnel.Local.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)

listener, err := tunnel.Listen()
if err != nil {
    panic(err)
}
// After having called tunnel.Listen(), there is no need to sleep, the port
// is already allocated and bound. The address is available at
// listener.Addr().String(), or the port at tunnel.Local.Port.  

// Start the server in the background.
go tunnel.Serve(listener)

// 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 SSHAgent added in v1.5.0

func SSHAgent() ssh.AuthMethod

Types

type Endpoint

type Endpoint struct {
	Host string
	Port int
	User string
}

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 Endpoint,
	remote Endpoint,
	auth ssh.AuthMethod,
	localport int,
) *SSHTunnel

NewSSHTunnel creates a new single-use tunnel. Supplying 0 for localport will use an ephemeral port.

func (*SSHTunnel) Close added in v1.5.0

func (tunnel *SSHTunnel) Close()

func (*SSHTunnel) Listen added in v1.5.0

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

func (*SSHTunnel) Serve added in v1.5.0

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

Jump to

Keyboard shortcuts

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