sshtun

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

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

Go to latest
Published: Dec 8, 2018 License: GPL-3.0 Imports: 10 Imported by: 0

README

sshtun

GoDoc

sshtun is a Go package that provides a SSH tunnel with port forwarding supporting both password and key auth methods.

By default it reads the default linux ssh private key location $HOME/.ssh/id_rsa, but other key can be specified.

Installation

go get github.com/rgzr/sshtun

Example

package main

import (
    "log"
    "time"

    "github.com/rgzr/sshtun"
)

func main() {
    // We want to connect to port 8080 on our machine to access port 80 on my.super.host.com
    sshTun := sshtun.New(8080, "my.super.host.com", 80)

    // We enable debug messages to see what happens
    sshTun.SetDebug(true)

    // We set a callback to know when the tunnel is ready
    sshTun.SetConnState(func(tun *sshtun.SSHTun, state sshtun.ConnState) {
        switch state {
        case sshtun.StateStarting:
            log.Printf("STATE is Starting")
        case sshtun.StateStarted:
            log.Printf("STATE is Started")
        case sshtun.StateStopped:
            log.Printf("STATE is Stopped")
        }
    })

    // We start the tunnel (and restart it every time it is stopped)
    go func() {
        for {
            if err := sshTun.Start(); err != nil {
                log.Printf("SSH tunnel stopped: %s", err.Error())
                time.Sleep(time.Second) // don't flood if there's a start error :)
            }
        }
    }()

    // We stop the tunnel every 20 seconds (just to see what happens)
    for {
        time.Sleep(time.Second * time.Duration(20))
        log.Println("Lets stop the SSH tunnel...")
        sshTun.Stop()
    }
}

Documentation

Overview

Package sshtun provides a SSH tunnel with port forwarding. By default it reads the default linux ssh private key location ($HOME/.ssh/id_rsa).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnState

type ConnState int

ConnState 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 ConnState = 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 {
	Host       string
	Port       int
	UnixSocket string
}

type SSHTun

type SSHTun struct {
	*sync.Mutex
	// contains filtered or unexported fields
}

SSHTun represents a SSH tunnel

func New

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

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 the default linux private key location ($HOME/.ssh/id_rsa). Calling SetPassword will change the authentication to password based and with SetKeyFile another key can be specified. 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 SetLocalHost and SetRemoteHost. The states of the tunnel can be received throgh a callback function with SetConnState.

func NewUnix

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

func (*SSHTun) SetConnState

func (tun *SSHTun) SetConnState(connStateFun func(*SSHTun, ConnState))

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 (*SSHTun) SetDebug

func (tun *SSHTun) SetDebug(debug bool)

SetDebug enables or disables log messages (disabled by default).

func (*SSHTun) SetKeyFile

func (tun *SSHTun) SetKeyFile(file string)

SetKeyFile changes the authentication to key-based and uses the specified file. Leaving it empty defaults to the default linux private key location ($HOME/.ssh/id_rsa).

func (*SSHTun) SetLocalHost

func (tun *SSHTun) SetLocalHost(host string)

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

func (*SSHTun) SetPassword

func (tun *SSHTun) SetPassword(password string)

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

func (*SSHTun) SetPort

func (tun *SSHTun) SetPort(port int)

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

func (*SSHTun) SetRemoteHost

func (tun *SSHTun) SetRemoteHost(host string)

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

func (*SSHTun) SetTimeout

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

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

func (*SSHTun) SetUser

func (tun *SSHTun) SetUser(user string)

SetUser changes the user used to make the SSH connection.

func (*SSHTun) Start

func (tun *SSHTun) Start() error

Start starts the SSH tunnel. After this call, all Set* methods will have no effect until Close is called.

func (*SSHTun) Stop

func (tun *SSHTun) Stop()

Stop closes the SSH tunnel and its connections. After this call all Set* methods will have effect and Start can be called again.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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