peers

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 18 Imported by: 1

Documentation

Overview

this file define the type and functions that serve as the clients data base

This file contains the Pane type and all associated functions

This file holds the struct and code to communicate with remote peers over webrtc data channels.

Index

Constants

View Source
const OutBufSize = 4096

Variables

View Source
var (
	// Peers holds all the peers (connected and disconnected)
	Peers map[string]*Peer
	// Payload holds the client's payload
	Payload []byte
	// WebRTCAPI is the gateway to webrtc calls
	WebRTCAPI *webrtc.API

	CDB = NewClientsDB()
)
View Source
var Panes = NewPanesDB()

Panes is an array that hol;ds all the panes

Functions

func CompressFP

func CompressFP(hex string) string

func DecodeOffer

func DecodeOffer(dst interface{}, src []byte) error

DecodeOffer decodes the input from base64

func EncodeOffer

func EncodeOffer(dst []byte, obj interface{}) (int, error)

EncodeOffer encodes the input in base64

func ExecCommand

func ExecCommand(command []string, env map[string]string, ws *pty.Winsize, pID int, fp string) (*exec.Cmd, io.ReadWriteCloser, error)

ExecCommand in ahelper function for executing a command

func ExtractFP

func ExtractFP(certificate *webrtc.Certificate) (string, error)

func GetFingerprint

func GetFingerprint(offer *webrtc.SessionDescription) (string, error)

GetFingerprint extract the fingerprints from a client's offer and returns a compressed fingerprint

func ParseWinsize

func ParseWinsize(s string) (*pty.Winsize, error)

ParseWinsize gets a string in the format of "24x80" and returns a Winsize

func SetLastPeer added in v1.4.0

func SetLastPeer(p *Peer)

SetLastPeer sets the most recent peer

func Shutdown

func Shutdown()

Shutdown is called when it's time to go.Sweet dreams.

Types

type AckArgs

type AckArgs struct {
	// Ref holds the message id the error refers to or 0 for system errors
	Ref  int    `json:"ref"`
	Body string `json:"body,omitempty"`
}

AckArgs is a type to hold the args for an Ack message

type AddPaneArgs

type AddPaneArgs struct {
	Command []string `json:"command"`
	Rows    uint16   `json:"rows, omitempty"`
	Cols    uint16   `json:"cols, omitempty"`
	X       uint16   `json:"x, omitempty"`
	Y       uint16   `json:"y, omitempty"`
	Parent  int      `json:"parent,omitempty"`
}

type Buffer

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

Buffer is a used to represet a fixed size buffer with markers

func NewBuffer

func NewBuffer(size int) *Buffer

NewBuffer creates and returns a new buffer of a given size

func (*Buffer) Add

func (buffer *Buffer) Add(b []byte)

Add adds a slice of bytes to the buffer

func (*Buffer) GetSinceMarker

func (buffer *Buffer) GetSinceMarker(id int) []byte

GetSinceMarker returns a byte slice with all the accumlated data since a given marker id and deltes the marker. If the marker is too ancient cycle or id is -1 then all the buffer's data is returned.

func (*Buffer) Mark

func (buffer *Buffer) Mark(id int)

Mark adds a new marker in the next buffer position

type CTRLMessage

type CTRLMessage struct {
	// Time is in msec since EPOCH
	Time int64       `json:"time"`
	Ref  int         `json:"message_id"`
	Type string      `json:"type"`
	Args interface{} `json:"args"` // TODO: add omitempty
}

CTRLMessage type holds control messages passed over the control channel

type Client

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

Client ties together the dta channel, its peer and the pane

type ClientsDB

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

ClientsDB represents a data channels data base

func NewClientsDB

func NewClientsDB() *ClientsDB

NewClientsDB return new data channels data base

func (*ClientsDB) Add

func (db *ClientsDB) Add(dc *webrtc.DataChannel, pane *Pane, peer *Peer) *Client

Add adds a Client to the db

func (*ClientsDB) All4Pane

func (db *ClientsDB) All4Pane(pane *Pane) []*Client

All4Pane returns

func (*ClientsDB) All4Peer

func (db *ClientsDB) All4Peer(peer *Peer) []*Client

All4Peer returns a slice with all the clients of a given peer

func (*ClientsDB) Delete

func (db *ClientsDB) Delete(c *Client) error

Delete removes a client from the database

func (*ClientsDB) Len

func (db *ClientsDB) Len() int

Len returns how many clients are in the data base

type Conf

type Conf struct {
	AckTimeout        time.Duration
	Certificate       *webrtc.Certificate
	DisconnectTimeout time.Duration
	Env               map[string]string
	FailedTimeout     time.Duration
	GatheringTimeout  time.Duration
	GetICEServers     func() ([]webrtc.ICEServer, error)
	GetWelcome        func() string
	KeepAliveInterval time.Duration
	Logger            *zap.SugaredLogger
	OnCTRLMsg         func(*Peer, CTRLMessage, json.RawMessage)
	OnStateChange     func(*Peer, webrtc.PeerConnectionState)
	PortMax           uint16
	PortMin           uint16
	RunCommand        RunCommandInterface
	WebrtcSetting     *webrtc.SettingEngine
}

type NAckArgs

type NAckArgs struct {
	// Desc hold the textual desciption of the error
	Desc string `json:"desc"`
	// Ref holds the message id the error refers to or 0 for system errors
	Ref int `json:"ref"`
}

NAckArgs is a type that holds the args for an error message

type Pane

type Pane struct {
	sync.Mutex
	ID int

	// C holds the exectuted command
	C         *exec.Cmd
	IsRunning bool
	TTY       io.ReadWriteCloser
	Buffer    *Buffer
	Ws        *pty.Winsize
	// contains filtered or unexported fields
}

Pane type hold a command, a pseudo tty and the connected data channels

func NewPane

func NewPane(peer *Peer, ws *pty.Winsize, parent int) (*Pane, error)

NewPane opens a new pane

func (*Pane) Kill

func (pane *Pane) Kill()

Kill takes a pane to the sands of Rishon and buries it

func (*Pane) OnMessage

func (pane *Pane) OnMessage(msg webrtc.DataChannelMessage)

OnMessage is called when a new client message is recieved

func (*Pane) ReadLoop

func (pane *Pane) ReadLoop()

ReadLoop reads the tty and send data it finds to the open data channels

func (*Pane) Resize

func (pane *Pane) Resize(ws *pty.Winsize)

Resize is used to resize the pane's tty. the function does nothing if it's given a nil size or the current size

func (*Pane) Restore

func (pane *Pane) Restore(d *webrtc.DataChannel, marker int)

Restore restore the screen or buffer. If the peer has a marker data will be read from the buffer and sent over. If no marker, Restore uses our headless terminal emulator to restore the screen.

func (*Pane) Run added in v1.3.0

func (pane *Pane) Run(command []string) error

start starts the command and pty

type PanesDB

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

PanesDB is use to store and access the panes' database

func NewPanesDB

func NewPanesDB() *PanesDB

NewPanesDB return a new database

func (*PanesDB) Add

func (pd *PanesDB) Add(p *Pane)

Add adds a new pane to the database

func (*PanesDB) All

func (pd *PanesDB) All() []*Pane

All returns a slice with all the panes in the database

func (*PanesDB) Delete

func (pd *PanesDB) Delete(id int) error

Delete delets a pane from the db

func (*PanesDB) Get

func (pd *PanesDB) Get(id int) *Pane

Get retrieves a pane from the database based on id

type Peer

type Peer struct {
	sync.Mutex

	FP          string
	Token       string
	LastContact *time.Time
	LastRef     int
	PC          *webrtc.PeerConnection

	Marker int

	Conf *Conf
	// contains filtered or unexported fields
}

Peer is a type used to remember a client.

func GetActivePeer added in v1.4.0

func GetActivePeer() *Peer

GetActivePeer returns the last active peer or nil if no peer has been active for a while

func NewPeer

func NewPeer(fp string, conf *Conf) (*Peer, error)

NewPeer funcions starts listening to incoming peer connection from a remote

func (*Peer) AddCandidate

func (peer *Peer) AddCandidate(candidate webrtc.ICECandidateInit) error

Peer.AddCandidate adds a new ICE candidate to the peer

func (*Peer) Broadcast added in v1.2.0

func (peer *Peer) Broadcast(typ string, args interface{}) error

func (*Peer) Close added in v1.3.0

func (peer *Peer) Close()

func (*Peer) GetOrCreatePane

func (peer *Peer) GetOrCreatePane(d *webrtc.DataChannel) (*Pane, error)

GetOrCreatePane gets a data channel and creates an associated pane The function parses the label to figure out what it needs to exec:

the command to run and rows & cols of the pseudo tty.

returns a nil when it fails to parse the channel name or when the name is '%' used for command & control channel.

label examples:

     simple form with no pty: `echo,Hello world`
		to start bash: `24x80,bash`
		to reconnect to pane id 123: `>123`

func (*Peer) Listen

func (peer *Peer) Listen(offer webrtc.SessionDescription) (*webrtc.SessionDescription, error)

Listen get's a client offer, starts listens to it and returns an answear

func (*Peer) OnChannelReq

func (peer *Peer) OnChannelReq(d *webrtc.DataChannel)

OnChannelReq starts the cdc channel. Upon establishing the connection, the client opens this channel with the api version he uses

func (*Peer) Reconnect

func (peer *Peer) Reconnect(d *webrtc.DataChannel, id int) (*Pane, error)

Reconnect reconnects to a pane and restore the screen/buffer buffer from that marker if not we use our headless terminal emulator to send over the current screen.

func (*Peer) SendAck

func (peer *Peer) SendAck(cm CTRLMessage, body string) error

SendAck sends an ack for a given control message

func (*Peer) SendControlMessage added in v1.3.0

func (peer *Peer) SendControlMessage(typ string, args interface{}) error

SendControlMessage sends a control message to the client

func (*Peer) SendControlMessageAndWait added in v1.4.0

func (peer *Peer) SendControlMessageAndWait(typ string, args interface{}) (string, error)

SendControlMessage sends a control message and wait for ack

func (*Peer) SendMessage added in v1.3.0

func (peer *Peer) SendMessage(msg []byte) error

SendMessage marshales a message and sends it over the cdc

func (*Peer) SendNack

func (peer *Peer) SendNack(cm CTRLMessage, desc string) error

SendNack sends an nack for a given control message

type PtyMuxInterface

type PtyMuxInterface interface {
	Start(c *exec.Cmd) (pty *os.File, err error)
	StartWithSize(c *exec.Cmd, sz *pty.Winsize) (pty *os.File, err error)
}
var PtyMux PtyMuxInterface

type PtyMuxType

type PtyMuxType struct{}

func (PtyMuxType) Start

func (pm PtyMuxType) Start(c *exec.Cmd) (*os.File, error)

func (PtyMuxType) StartWithSize

func (pm PtyMuxType) StartWithSize(c *exec.Cmd, sz *pty.Winsize) (*os.File, error)

type ReconnectPaneArgs

type ReconnectPaneArgs struct {
	ID int `json:"id"`
}

type ResizeArgs

type ResizeArgs struct {
	PaneID int    `json:"pane_id"`
	Sx     uint16 `json:"sx"`
	Sy     uint16 `json:"sy"`
}

ResizeArgs is a type that holds the argumnet to the resize pty command ResizeArgs is a type that holds the argumnet to the resize pty command

type RestoreArgs

type RestoreArgs struct {
	Marker int `json:"marker"`
}

RestoreArgs is a type that holds client's authentication arguments.

type RunCommandInterface

type RunCommandInterface func([]string, map[string]string, *pty.Winsize, int, string) (*exec.Cmd, io.ReadWriteCloser, error)

RunCommandInterface is an interface for a function that runs a command

type SetClipboardArgs added in v1.4.0

type SetClipboardArgs struct {
	Data     string `json:"data"`
	MimeType string `json:"mimetype"`
}

type SetPayloadArgs

type SetPayloadArgs struct {
	// Ref holds the message id the error refers to or 0 for system errors
	Payload json.RawMessage `json:"payload"`
}

SetPayloadArgs is a type to hold the args for a set_payload type of a message

Jump to

Keyboard shortcuts

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