sshbox

package module
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: MIT Imports: 31 Imported by: 0

README

SSHBox

This is a library for ease of native ssh with https://pkg.go.dev/golang.org/x/crypto/ssh . This help you to:

  • Create tunnels on ssh server
  • Create reverse tunnels on ssh server
  • Create socks5 server on ssh server, you can also have dns resolution from nameserver on ssh server which let you set socks5h server
  • Gateway(s) creation for accessing ssh server in chainable way
  • Have an interactive shell on ssh server

Note: Use https://pkg.go.dev/golang.org/x/crypto/ssh make the library totally standalone from ssh command line from a linux server. This liberate you from having putty on windows for example.

Usage

package main

import (
	"github.com/ArthurHlt/sshbox"
)

func main() {
	sb, err := sshbox.NewSSHBox(sshbox.SSHConf{
		Host:      "url.com",
		User:       "root",
		Password:   "a password",
		NoSSHAgent: true,
	})
	if err != nil {
		panic(err)
	}

	// create tunnels
	// this will let you call access to something running on port 8080 in your ssh server on port 8080 on localhost
	// if reverse is true, this is inverted, ssh server will access to something running locally on port 8080
	go sb.StartTunnels([]*sshbox.TunnelTarget{
		{
			Network:    "tcp",
			RemoteHost: "127.0.0.1",
			RemotePort: 8080,
			LocalPort:  8080,
			Reverse:    false,
		},
	})
	// Create a socks5 server on udp and tcp
	// you can now use with env var https_proxy=socks5h://localhost:9090 and http_proxy=socks5h://localhost:9090
	go sb.StartSocksServer(9090, "tcp")
	go sb.StartSocksServer(9090, "udp")
	// This will open a shell on ssh server
	interact := sshbox.NewInteractiveSSH(sb)
	panic(interact.Interactive())
	// panic(sb.StartSocksServer(9090, "tcp"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultErrorMatcher added in v0.4.1

func DefaultErrorMatcher(content []byte) bool

func DefaultPromptMatcher added in v0.4.1

func DefaultPromptMatcher(line []byte) bool

func DefaultSanitizePromptLine added in v0.6.1

func DefaultSanitizePromptLine(line []byte) []byte

func DefaultSshClientFactory

func DefaultSshClientFactory(conf SSHConf) (*ssh.Client, error)

func MakeSessionNoTerminal added in v0.4.0

func MakeSessionNoTerminal(client *ssh.Client, opts ...SSHSessionOptions) (*ssh.Session, error)

func NameResolverFactoryTunnels added in v0.1.4

func NameResolverFactoryTunnels(dnsservers []string) func(sshBox *SSHBox) (NameResolver, error)

func NewNameResolverSimple

func NewNameResolverSimple(servers []string) *nameResolverSimple

func OptNameResolverFactory

func OptNameResolverFactory(nameResolverFactory NameResolverFactory) func(box *SSHBox) error

func OptSSHClientFactory

func OptSSHClientFactory(factory SshClientFactory) func(box *SSHBox) error

func OptSocksConf

func OptSocksConf(conf *socks5.Config) func(box *SSHBox) error

func SetLogger added in v0.2.0

func SetLogger(newLogger *logrus.Logger)

func WithErrorMatcher added in v0.5.0

func WithErrorMatcher(errorMatcher func(content []byte) bool) commanderSessionOptions

WithErrorMatcher option to set the error matcher

func WithPromptMatcher added in v0.5.0

func WithPromptMatcher(promptMatcher func(line []byte) bool) commanderSessionOptions

WithErrorMatcher option to set the prompt matcher

func WithSanitizePromptLine added in v0.6.1

func WithSanitizePromptLine(sanitizePromptLine func(line []byte) []byte) commanderSessionOptions

func WithSeparator added in v0.6.2

func WithSeparator(separator []byte) commanderSessionOptions

WithSeparator option to set a separator content

func WithSessionOptions added in v0.5.0

func WithSessionOptions(opts ...SSHSessionOptions) commanderSessionOptions

WithSessionOptions option to add options to the session

func WithSubSystem added in v0.5.0

func WithSubSystem(subsystem string) commanderSessionOptions

WithSubSystem option to use subsystem instead of shell

Types

type CommanderSSH added in v0.4.0

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

CommanderSSH let you run commands on a remote host and getting the output back It will create a session each time a command is run which mean that context is not persisted between commands

func NewCommanderSSH added in v0.4.0

func NewCommanderSSH(sshBox *SSHBox) *CommanderSSH

func (*CommanderSSH) CombinedOutput added in v0.4.0

func (c *CommanderSSH) CombinedOutput(cmd string, opts ...SSHSessionOptions) ([]byte, error)

func (*CommanderSSH) Run added in v0.4.0

func (c *CommanderSSH) Run(cmd string, opts ...SSHSessionOptions) (stdout []byte, stderr []byte, err error)

type CommanderSession added in v0.4.0

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

CommanderSession let you run multiple commands on a remote host and getting the output back on a single session which means that context is persisted between commands but output is buffered and split by a promptMatcher which is often the prompt

func NewCommanderSession added in v0.4.0

func NewCommanderSession(client *ssh.Client, opts ...commanderSessionOptions) (*CommanderSession, error)

NewCommanderSession creates a new commander session

func (*CommanderSession) Close added in v0.4.0

func (c *CommanderSession) Close() error

func (*CommanderSession) Run added in v0.4.0

func (c *CommanderSession) Run(cmd string) ([]byte, error)

func (*CommanderSession) SetMatcher added in v0.4.0

func (c *CommanderSession) SetMatcher(matcher func(line []byte) bool)

type DnsConfig

type DnsConfig struct {
	Servers    []string // servers to use
	Search     []string // suffixes to append to local name
	Ndots      int      // number of dots in name to trigger absolute lookup
	Timeout    int      // seconds before giving up on packet
	Attempts   int      // lost packets before giving up on server
	Rotate     bool     // round robin among servers
	UnknownOpt bool     // anything unknown was encountered
	Lookup     []string // OpenBSD top-level database "lookup" order
	Err        error    // any error that occurs during open of resolv.conf
}

func DnsConfFromSSH

func DnsConfFromSSH(sshBox *SSHBox) (*DnsConfig, error)

type Emitter

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

func NewEmitter

func NewEmitter() *Emitter

func (*Emitter) EmitClosedSsh added in v0.6.0

func (em *Emitter) EmitClosedSsh()

func (*Emitter) EmitStopSocks

func (em *Emitter) EmitStopSocks()

func (*Emitter) EmitStopSsh

func (em *Emitter) EmitStopSsh()

func (*Emitter) EmitStopTunnels

func (em *Emitter) EmitStopTunnels()

func (*Emitter) ListenersStartTunnels

func (em *Emitter) ListenersStartTunnels() []<-chan emitter.Event

func (*Emitter) ListenersStopSocks

func (em *Emitter) ListenersStopSocks() []<-chan emitter.Event

func (*Emitter) ListenersStopSsh

func (em *Emitter) ListenersStopSsh() []<-chan emitter.Event

func (*Emitter) ListenersStopTunnels

func (em *Emitter) ListenersStopTunnels() []<-chan emitter.Event

func (*Emitter) OffClosedSsh added in v0.6.0

func (em *Emitter) OffClosedSsh(events ...<-chan emitter.Event)

func (*Emitter) OffStartTunnels

func (em *Emitter) OffStartTunnels(events ...<-chan emitter.Event)

func (*Emitter) OffStopSocks

func (em *Emitter) OffStopSocks(events ...<-chan emitter.Event)

func (*Emitter) OffStopSsh

func (em *Emitter) OffStopSsh(events ...<-chan emitter.Event)

func (*Emitter) OffStopTunnels

func (em *Emitter) OffStopTunnels(events ...<-chan emitter.Event)

func (*Emitter) OnClosedSsh added in v0.6.0

func (em *Emitter) OnClosedSsh() <-chan emitter.Event

func (*Emitter) OnStartTunnels

func (em *Emitter) OnStartTunnels() <-chan emitter.Event

func (*Emitter) OnStopSocks

func (em *Emitter) OnStopSocks() <-chan emitter.Event

func (*Emitter) OnStopSsh

func (em *Emitter) OnStopSsh() <-chan emitter.Event

func (*Emitter) OnStopTunnels

func (em *Emitter) OnStopTunnels() <-chan emitter.Event

func (*Emitter) ToError

func (em *Emitter) ToError(evt emitter.Event) error

type ErrLoad

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

func (ErrLoad) Error

func (e ErrLoad) Error() string

type GatewayInfo

type GatewayInfo struct {
	SrcSSHUri  string
	LocalPort  int
	RemoteHost string
	RemotePort int
}

type Gateways

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

func NewGateways

func NewGateways(gateways []*SSHConf) *Gateways

func (*Gateways) Close added in v0.6.0

func (g *Gateways) Close()

func (*Gateways) RunGateways

func (g *Gateways) RunGateways(toHost string) (string, error)

type InteractiveSSH

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

func NewInteractiveSSH

func NewInteractiveSSH(sshBox *SSHBox) *InteractiveSSH

func (*InteractiveSSH) Interactive

func (c *InteractiveSSH) Interactive(sessOpts ...SSHSessionOptions) error

func (*InteractiveSSH) InteractiveSession

func (c *InteractiveSSH) InteractiveSession(commands []string, terminalRequest TTYRequest, sessOpts ...SSHSessionOptions) error

func (*InteractiveSSH) InteractiveSubSystem added in v0.5.0

func (c *InteractiveSSH) InteractiveSubSystem(subsystem string, sessOpts ...SSHSessionOptions) error

func (*InteractiveSSH) RunCmd

func (c *InteractiveSSH) RunCmd(cmd []string, sessOpts ...SSHSessionOptions) error

func (*InteractiveSSH) Stop

func (c *InteractiveSSH) Stop() error

type NameResolver

type NameResolver interface {
	Resolve(ctx netctx.Context, name string) (context.Context, net.IP, error)
}

func NameResolverFactorySSH

func NameResolverFactorySSH(sshBox *SSHBox) (NameResolver, error)

type NameResolverFactory

type NameResolverFactory func(sshBox *SSHBox) (NameResolver, error)

type PublicKeys

type PublicKeys struct {
	User   string
	Signer ssh.Signer
}

PublicKeys implements AuthMethod by using the given key pairs.

func NewPublicKeys

func NewPublicKeys(pemBytes []byte, passphrase string) (*PublicKeys, error)

func NewPublicKeysFromFile

func NewPublicKeysFromFile(pemFile, passphrase string) (*PublicKeys, error)

type SSHBox

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

func NewSSHBox

func NewSSHBox(config SSHConf, opts ...SSHBoxOptions) (*SSHBox, error)

func (*SSHBox) Close added in v0.6.0

func (t *SSHBox) Close()

func (SSHBox) Emitter

func (t SSHBox) Emitter() *Emitter

func (*SSHBox) HandleRTunnelClient added in v0.1.2

func (t *SSHBox) HandleRTunnelClient(client net.Conn, target *TunnelTarget)

func (*SSHBox) HandleTunnelClient added in v0.1.2

func (t *SSHBox) HandleTunnelClient(client net.Conn, target *TunnelTarget)

func (*SSHBox) SSHClient

func (t *SSHBox) SSHClient() *ssh.Client

func (*SSHBox) SetNameResolverFactory added in v0.1.3

func (t *SSHBox) SetNameResolverFactory(nrf NameResolverFactory)

func (*SSHBox) StartSocksServer

func (t *SSHBox) StartSocksServer(port int, network string) error

func (*SSHBox) StartTunnels

func (t *SSHBox) StartTunnels(tunnelTargets []*TunnelTarget) error

func (*SSHBox) StopSocksServer

func (t *SSHBox) StopSocksServer()

func (*SSHBox) StopTunnelsServer

func (t *SSHBox) StopTunnelsServer()

type SSHBoxOptions

type SSHBoxOptions func(sshBox *SSHBox) error

type SSHConf

type SSHConf struct {
	Host               string
	User               string
	Password           string
	PrivateKey         string
	Passphrase         string
	HostKeyFingerprint string
	SSHAuthSock        *string
	NoSSHAgent         bool
}

func (*SSHConf) CheckAndFill

func (c *SSHConf) CheckAndFill() error

func (SSHConf) String added in v0.2.0

func (c SSHConf) String() string

type SSHSessionOptions added in v0.4.0

type SSHSessionOptions func(session *ssh.Session) error

type SShInGateways added in v0.6.0

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

func NewSShInGateways added in v0.6.0

func NewSShInGateways(sshConf *SSHConf, gatewaysConf []*SSHConf) (*SShInGateways, error)

func (*SShInGateways) Close added in v0.6.0

func (S *SShInGateways) Close()

func (*SShInGateways) SSHBox added in v0.6.0

func (S *SShInGateways) SSHBox() *SSHBox

type SshClientFactory

type SshClientFactory func(conf SSHConf) (*ssh.Client, error)

type TTYRequest

type TTYRequest int
const (
	RequestTTYAuto TTYRequest = iota
	RequestTTYNo
	RequestTTYYes
	RequestTTYForce
)

type TerminalError added in v0.4.1

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

func IsTerminalError added in v0.4.1

func IsTerminalError(err error) (*TerminalError, bool)

func (TerminalError) Error added in v0.4.1

func (e TerminalError) Error() string

type TunnelTarget

type TunnelTarget struct {
	Network    string
	RemoteHost string
	RemotePort int
	LocalPort  int
	Reverse    bool
}

func DNSServerToTunnel

func DNSServerToTunnel(dnsservers []string) ([]*TunnelTarget, error)

func (*TunnelTarget) CheckAndFill

func (c *TunnelTarget) CheckAndFill() error

func (TunnelTarget) String added in v0.2.0

func (c TunnelTarget) String() string

type TunnelTargets

type TunnelTargets []*TunnelTarget

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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