ssh_tunnel

package
v0.12.6 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthType

type AuthType int

AuthType is the type of authentication to use for SSH.

const (
	// AuthTypeKeyFile uses the keys from a SSH key file read from the system.
	AuthTypeKeyFile AuthType = iota
	// AuthTypeEncryptedKeyFile uses the keys from an encrypted SSH key file read from the system.
	AuthTypeEncryptedKeyFile
	// AuthTypeKeyReader uses the keys from a SSH key reader.
	AuthTypeKeyReader
	// AuthTypeEncryptedKeyReader uses the keys from an encrypted SSH key reader.
	AuthTypeEncryptedKeyReader
	// AuthTypePassword uses a password directly.
	AuthTypePassword
	// AuthTypeSSHServer will use registered users in the ssh-server.
	AuthTypeSSHServer
	// AuthTypeAuto tries to get the authentication method automatically. See SSHTun.Start for details on
	// this.
	AuthTypeAuto
)

type ConnectionState

type ConnectionState int

ConnectionState represents the state of the SSH tunnel. It's returned to an optional function provided to SetConnState.

const (
	// StateStopped represents a stopped tunnel. A call to Start will make the state to transition to StateStarting.
	StateStopped ConnectionState = iota

	// StateStarting represents a tunnel initializing and preparing to listen for connections.
	// A successful initialization will make the state to transition to StateStarted, otherwise it will transition to StateStopped.
	StateStarting

	// StateStarted represents a tunnel ready to accept connections.
	// A call to stop or an error will make the state to transition to StateStopped.
	StateStarted
)

type Endpoint

type Endpoint struct {
	// contains filtered or unexported fields
}

func NewTCPEndpoint

func NewTCPEndpoint(host string, port int) *Endpoint

func NewUnixEndpoint

func NewUnixEndpoint(socket string) *Endpoint

func (*Endpoint) String

func (e *Endpoint) String() string

func (*Endpoint) Type

func (e *Endpoint) Type() string

type SshTunnel

type SshTunnel struct {
	Server *Endpoint

	SshConfig *ssh.ClientConfig
	SshClient *ssh.Client
	// contains filtered or unexported fields
}

SshTunnel represents a SSH tunnel

func New

func New(localPort int, server string, remotePort int) *SshTunnel

New creates a new SSH tunnel to the specified server redirecting a port on local localhost to a port on remote localhost. By default the SSH connection is made to port 22 as root and using automatic detection of the authentication method (see Start for details on this). Calling SetPassword will change the authentication to password based. Calling SetKeyFile will change the authentication to keyfile based.. The SSH user and port can be changed with SetUser and SetPort. The local and remote hosts can be changed to something different than localhost with SetLocalEndpoint and SetRemoteEndpoint. The states of the tunnel can be received through a callback function with SetConnState. The states of the tunneled connections can be received through a callback function with SetTunneledConnState.

func NewUnix

func NewUnix(localUnixSocket string, server string, remoteUnixSocket string) *SshTunnel

NewUnix does the same as New but using unix sockets.

func (*SshTunnel) InitSSHConfig added in v0.7.0

func (tun *SshTunnel) InitSSHConfig() (*ssh.ClientConfig, error)

func (*SshTunnel) SetConnState

func (tun *SshTunnel) SetConnState(connStateFun func(*SshTunnel, ConnectionState))

SetConnState specifies an optional callback function that is called when a SSH tunnel changes state. See the ConnState type and associated constants for details.

func (*SshTunnel) SetEncryptedKeyFile

func (tun *SshTunnel) SetEncryptedKeyFile(file string, password string)

SetEncryptedKeyFile changes the authentication to encrypted key-based and uses the specified file and password. Leaving the file empty defaults to the default Linux private key locations: `~/.ssh/id_rsa`, `~/.ssh/id_dsa`, `~/.ssh/id_ecdsa`, `~/.ssh/id_ecdsa_sk`, `~/.ssh/id_ed25519` and `~/.ssh/id_ed25519_sk`.

func (*SshTunnel) SetEncryptedKeyReader

func (tun *SshTunnel) SetEncryptedKeyReader(reader io.Reader, password string)

SetEncryptedKeyReader changes the authentication to encrypted key-based and uses the specified reader and password.

func (*SshTunnel) SetKeyFile

func (tun *SshTunnel) SetKeyFile(file string)

SetKeyFile changes the authentication to key-based and uses the specified file. Leaving the file empty defaults to the default Linux private key locations: `~/.ssh/id_rsa`, `~/.ssh/id_dsa`, `~/.ssh/id_ecdsa`, `~/.ssh/id_ecdsa_sk`, `~/.ssh/id_ed25519` and `~/.ssh/id_ed25519_sk`.

func (*SshTunnel) SetKeyReader

func (tun *SshTunnel) SetKeyReader(reader io.Reader)

SetKeyReader changes the authentication to key-based and uses the specified reader.

func (*SshTunnel) SetLocalEndpoint

func (tun *SshTunnel) SetLocalEndpoint(endpoint *Endpoint)

SetLocalEndpoint sets the local endpoint to redirect.

func (*SshTunnel) SetLocalHost

func (tun *SshTunnel) SetLocalHost(host string)

SetLocalHost sets the local host to redirect (defaults to localhost).

func (*SshTunnel) SetPassword

func (tun *SshTunnel) SetPassword(password string)

SetPassword changes the authentication to password-based and uses the specified password.

func (*SshTunnel) SetPort

func (tun *SshTunnel) SetPort(port int)

SetPort changes the port where the SSH connection will be made.

func (*SshTunnel) SetRemoteEndpoint

func (tun *SshTunnel) SetRemoteEndpoint(endpoint *Endpoint)

SetRemoteEndpoint sets the remote endpoint to redirect.

func (*SshTunnel) SetRemoteHost

func (tun *SshTunnel) SetRemoteHost(host string)

SetRemoteHost sets the remote host to redirect (defaults to localhost).

func (*SshTunnel) SetSSHServer

func (tun *SshTunnel) SetSSHServer()

SetSSHServer changes the authentication to ssh-server.

func (*SshTunnel) SetTimeout

func (tun *SshTunnel) SetTimeout(timeout time.Duration)

SetTimeout sets the connection timeouts (defaults to 15 seconds).

func (*SshTunnel) SetTunneledConnState

func (tun *SshTunnel) SetTunneledConnState(tunneledConnStateFun func(*SshTunnel, *TunneledConnectionState))

SetTunneledConnState specifies an optional callback function that is called when the underlying tunneled connections change state.

func (*SshTunnel) SetUser

func (tun *SshTunnel) SetUser(user string)

SetUser changes the user used to make the SSH connection.

func (*SshTunnel) Start

func (tun *SshTunnel) Start(ctx context.Context) error

Start starts the SSH tunnel. It can be stopped by calling `Stop` or cancelling its context. This call will block until the tunnel is stopped either calling those methods or by an error. Note on SSH authentication: in case the tunnel's authType is set to AuthTypeAuto the following will happen: The default key files will be used, if that doesn't succeed it will try to use the SSH server. If that fails the whole authentication fails. That means if you want to use password or encrypted key file authentication, you have to specify that explicitly.

func (*SshTunnel) Stop

func (tun *SshTunnel) Stop()

Stop closes all connections and makes Start exit gracefuly.

type TunneledConnectionState

type TunneledConnectionState struct {
	// From is the address initating the connection.
	From string
	// Info holds a message with info on the state of the connection (useful for debug purposes).
	Info string
	// Error holds an error on the connection or nil if the connection is successful.
	Error error
	// Ready indicates if the connection is established.
	Ready bool
	// Closed indicates if the coonnection is closed.
	Closed bool
}

TunneledConnectionState represents the state of the final connections made through the tunnel.

func (*TunneledConnectionState) String

func (s *TunneledConnectionState) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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