utils

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2021 License: MIT Imports: 31 Imported by: 2

Documentation

Overview

Package utils implements utilities used across different areas of the sish application. There are utility functions that help with overall state management and are core to the application.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Filter is the IPFilter used to block connections.
	Filter *ipfilter.IPFilter
)

Functions

func CheckPort

func CheckPort(port uint32, portRanges string) (uint32, error)

CheckPort verifies if a port exists within the port range. It will return 0 and an error if not (0 allows the kernel to select) the port.

func CommaSplitFields

func CommaSplitFields(c rune) bool

CommaSplitFields is a function used by strings.FieldsFunc to split around commas.

func CopyBoth

func CopyBoth(writer net.Conn, reader io.ReadWriteCloser)

CopyBoth copies betwen a reader and writer and will cleanup each.

func GetRandomPortInRange

func GetRandomPortInRange(portRange string) uint32

GetRandomPortInRange returns a random port in the provided range. The port range is a comma separated list of ranges or ports.

func GetSSHConfig

func GetSSHConfig() *ssh.ServerConfig

GetSSHConfig Returns an SSH config for the ssh muxer. It handles auth and storing user connection information.

func LoadProxyProtoConfig added in v1.0.10

func LoadProxyProtoConfig(l *proxyproto.Listener)

LoadProxyProtoConfig will load the timeouts and policies for the proxy protocol.

func RandStringBytesMaskImprSrc

func RandStringBytesMaskImprSrc(n int) string

RandStringBytesMaskImprSrc creates a random string of length n https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang

func Setup

func Setup(logWriter io.Writer)

Setup main utils. This initializes, whitelists, blacklists, and log writers.

func WatchCerts

func WatchCerts()

WatchCerts watches ssh keys for changes and will load them.

Types

type AliasHolder

type AliasHolder struct {
	AliasHost      string
	SSHConnections *sync.Map
	Balancer       *roundrobin.RoundRobin
}

AliasHolder holds alias and connection info. SSHConnections is a map[string]*SSHConnection.

func GetOpenAlias

func GetOpenAlias(addr string, port string, state *State, sshConn *SSHConnection) (string, *AliasHolder)

GetOpenAlias returns open aliases or a random one if it is not enabled. If load balancing is enabled, it will return the requested alias.

type HTTPHolder

type HTTPHolder struct {
	HTTPHost       string
	Scheme         string
	SSHConnections *sync.Map
	Forward        *forward.Forwarder
	Balancer       *roundrobin.RoundRobin
}

HTTPHolder holds proxy and connection info. SSHConnections is a map[string]*SSHConnection.

func GetOpenHost

func GetOpenHost(addr string, state *State, sshConn *SSHConnection) (string, *HTTPHolder)

GetOpenHost returns an open host or a random host if that one is unavailable. If load balancing is enabled, it will return the requested domain.

type IdleTimeoutConn

type IdleTimeoutConn struct {
	Conn net.Conn
}

IdleTimeoutConn handles the connection with a context deadline. code adapted from https://qiita.com/kwi/items/b38d6273624ad3f6ae79

func (IdleTimeoutConn) Read

func (i IdleTimeoutConn) Read(buf []byte) (int, error)

Read is needed to implement the reader part.

func (IdleTimeoutConn) Write

func (i IdleTimeoutConn) Write(buf []byte) (int, error)

Write is needed to implement the writer part.

type ListenerHolder

type ListenerHolder struct {
	net.Listener
	ListenAddr string
	Type       ListenerType
	SSHConn    *SSHConnection
}

ListenerHolder represents a generic listener.

type ListenerType

type ListenerType int

ListenerType represents any listener sish supports.

const (
	// AliasListener represents a tcp alias.
	AliasListener ListenerType = iota

	// HTTPListener represents a HTTP proxy.
	HTTPListener

	// TCPListener represents a generic tcp listener.
	TCPListener

	// ProcessListener represents a process specific listener.
	ProcessListener
)

type LogWriter

type LogWriter struct {
	TimeFmt     string
	MultiWriter io.Writer
}

LogWriter represents a writer that is used for writing logs in multiple locations.

func (LogWriter) Write

func (w LogWriter) Write(bytes []byte) (int, error)

Write implements the write function for the LogWriter. It will add a time in a specific format to logs.

type SSHConnection

type SSHConnection struct {
	SSHConn        *ssh.ServerConn
	Listeners      *sync.Map
	Closed         *sync.Once
	Close          chan bool
	Messages       chan string
	ProxyProto     byte
	Session        chan bool
	CleanupHandler bool
	SetupLock      *sync.Mutex
}

SSHConnection handles state for a SSHConnection. It wraps an ssh.ServerConn and allows us to pass other state around the application. Listeners is a map[string]net.Listener.

func (*SSHConnection) CleanUp

func (s *SSHConnection) CleanUp(state *State)

CleanUp closes all allocated resources for a SSH session and cleans them up.

func (*SSHConnection) SendMessage

func (s *SSHConnection) SendMessage(message string, block bool)

SendMessage sends a console message to the connection. If block is true, it will block until the message is sent. If it is false, it will try to send the message 5 times, waiting 100ms each time.

type State

type State struct {
	Console        *WebConsole
	SSHConnections *sync.Map
	Listeners      *sync.Map
	HTTPListeners  *sync.Map
	AliasListeners *sync.Map
	TCPListeners   *sync.Map
	IPFilter       *ipfilter.IPFilter
	LogWriter      io.Writer
}

State handles overall state. It retains mutexed maps for various datastructures and shared objects. SSHConnections is a map[string]*SSHConnection. Listeners is a map[string]net.Listener. HTTPListeners is a map[string]HTTPHolder. AliasListeners is a map[string]AliasHolder. TCPListeners is a map[string]TCPHolder.

func NewState

func NewState() *State

NewState returns a new State struct.

type TCPHolder

type TCPHolder struct {
	TCPHost        string
	Listener       net.Listener
	SSHConnections *sync.Map
	Balancer       *roundrobin.RoundRobin
}

TCPHolder holds proxy and connection info. SSHConnections is a map[string]*SSHConnection.

func GetOpenPort

func GetOpenPort(addr string, port uint32, state *State, sshConn *SSHConnection) (string, uint32, *TCPHolder)

GetOpenPort returns open ports that can be bound. It verifies the host to bind the port to and attempts to listen to the port to ensure it is open. If load balancing is enabled, it will return the port if used.

func (*TCPHolder) Handle

func (tH *TCPHolder) Handle(state *State)

Handle will copy connections from one handler to a roundrobin server.

type WebClient

type WebClient struct {
	Conn    *websocket.Conn
	Console *WebConsole
	Send    chan []byte
	Route   string
}

WebClient represents a primitive web console client. It maintains references that allow us to communicate and track a client connection.

func (*WebClient) Handle

func (c *WebClient) Handle()

Handle is the only place socket reads and writes happen.

type WebConsole

type WebConsole struct {
	Clients     *sync.Map
	RouteTokens *sync.Map
	State       *State
}

WebConsole represents the data structure that stores web console client information. Clients is a map[string][]*WebClient. RouteTokens is a map[string]string.

func NewWebConsole

func NewWebConsole() *WebConsole

NewWebConsole sets up the WebConsole.

func (*WebConsole) AddClient

func (c *WebConsole) AddClient(route string, w *WebClient)

AddClient adds a client to the console route.

func (*WebConsole) AddRoute

func (c *WebConsole) AddRoute(route string, token string)

AddRoute adds a route token to the console.

func (*WebConsole) BroadcastRoute

func (c *WebConsole) BroadcastRoute(route string, message []byte)

BroadcastRoute sends a message to all clients on a route.

func (*WebConsole) HandleClients

func (c *WebConsole) HandleClients(hostname string, g *gin.Context)

HandleClients handles returning all connected SSH clients. This will also go through all of the forwarded connections for the SSH client and return them.

func (*WebConsole) HandleDisconnectClient

func (c *WebConsole) HandleDisconnectClient(hostname string, g *gin.Context)

HandleDisconnectClient handles the disconnection request for a SSH client.

func (*WebConsole) HandleDisconnectRoute

func (c *WebConsole) HandleDisconnectRoute(hostname string, g *gin.Context)

HandleDisconnectRoute handles the disconnection request for a forwarded route.

func (*WebConsole) HandleRequest

func (c *WebConsole) HandleRequest(hostname string, hostIsRoot bool, g *gin.Context)

HandleRequest handles an incoming web request, handles auth, and then routes it.

func (*WebConsole) HandleTemplate

func (c *WebConsole) HandleTemplate(hostname string, hostIsRoot bool, userIsAdmin bool, g *gin.Context)

HandleTemplate handles rendering the console templates.

func (*WebConsole) HandleWebSocket

func (c *WebConsole) HandleWebSocket(hostname string, g *gin.Context)

HandleWebSocket handles the websocket route.

func (*WebConsole) RemoveClient

func (c *WebConsole) RemoveClient(route string, w *WebClient)

RemoveClient removes a client from the console route.

func (*WebConsole) RemoveRoute

func (c *WebConsole) RemoveRoute(route string)

RemoveRoute removes a route token from the console.

func (*WebConsole) RouteExists

func (c *WebConsole) RouteExists(route string) bool

RouteExists check if a route token exists.

func (*WebConsole) RouteToken

func (c *WebConsole) RouteToken(route string) (string, bool)

RouteToken returns the route token for a specific route.

Jump to

Keyboard shortcuts

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