client

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: GPL-3.0 Imports: 25 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

client.go

Package client implements the RDP client, and carries the RFB (VNC) client this fork inherited from upstream alongside it.

The VNC client (VncClient) is exercised against a real server by scripts/vnc-dev.sh, which starts TigerVNC's Xtigervnc and runs the live tests in protocol/rfb. Its clipboard is implemented in both directions: text the server cuts arrives as the "clipboard-text" event, and SetClipboardText publishes with ClientCutText.

Index

Constants

View Source
const (
	CLIP_OFF = 0
	CLIP_IN  = 0x1
	CLIP_OUT = 0x2
)

Clipboard directions accepted by the (currently inert) Setting.SetClipboard.

View Source
const (
	TC_RDP = 0
	TC_VNC = 1
)

Transport kinds accepted by NewClient: TC_RDP for RDP, TC_VNC for VNC (RFB).

View Source
const DefaultLoginTimeout = 30 * time.Second

DefaultLoginTimeout bounds how long Login waits for the RDP session to become ready after the transport handshake succeeds.

Variables

This section is empty.

Functions

func Bpp

func Bpp(bp uint16) int

Bpp converts a bit depth to a byte count by dividing by eight: 16 to 2, 32 to 4. It truncates, so 15 bpp gives 1 rather than 2.

Types

type Bitmap

type Bitmap struct {
	DestLeft     int    `json:"destLeft"`
	DestTop      int    `json:"destTop"`
	DestRight    int    `json:"destRight"`
	DestBottom   int    `json:"destBottom"`
	Width        int    `json:"width"`
	Height       int    `json:"height"`
	BitsPerPixel int    `json:"bitsPerPixel"`
	IsCompress   bool   `json:"isCompress"`
	Data         []byte `json:"data"`
}

Bitmap is one decoded rectangle of the remote screen.

The rectangle is [DestLeft, DestRight) x [DestTop, DestBottom) on the desktop; Width and Height are the size of the source data and are usually equal to the destination size. Data is top-down BGRA (B first) at BitsPerPixel/8 bytes per pixel when IsCompress is false, which is how OnBitmap delivers RDP updates.

type Client

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

Client is one session with an RDP or VNC server. It picks a protocol-specific implementation from the type given to NewClient (RdpClient or VncClient) and exposes one API for input, events, the clipboard and the framebuffer.

A Client is safe for concurrent use. Build one with NewClient; the zero value is not usable. Nothing is sent until Login is called.

func NewClient

func NewClient(host, user, passwd string, t int, s *Setting) *Client

NewClient returns an unconnected session for host, which must be "address:port". t selects the transport: TC_RDP for RDP, TC_VNC for VNC. A nil s is replaced by NewSetting, so the common call is NewClient(host, user, pass, TC_RDP, nil).

No connection is made here. Call Login to connect, and Close when done.

func (*Client) Close

func (c *Client) Close()

Close ends the session and closes the underlying connection. It is safe to call more than once and on a client that never connected, and it is safe to call concurrently with input methods, which become no-ops once the connection is gone.

func (*Client) KeyDown

func (c *Client) KeyDown(sc int, name string)

KeyDown presses the key whose PC set 1 scancode is sc, with the same extended key handling as KeyUp. name is ignored.

func (*Client) KeyUp

func (c *Client) KeyUp(sc int, name string)

KeyUp releases the key whose PC set 1 scancode is sc. A value in 0xE000..0xE0FF is sent as an extended key: the low byte is the scancode and the 0xE0 prefix becomes the extended flag. name is accepted for source compatibility and ignored; the scancode is what reaches the server.

func (*Client) Login

func (c *Client) Login() error

Login connects and waits until the session is ready (or fails/times out).

func (*Client) LoginContext

func (c *Client) LoginContext(ctx context.Context) error

LoginContext is Login with an explicit context. It returns only once the server has completed the connection sequence and the session is ready, or when the handshake fails, the connection is closed, or ctx expires.

func (*Client) MouseDown

func (c *Client) MouseDown(button, x, y int)

MouseDown presses button 0 (left), 1 (middle) or 2 (right) at (x, y), with the same numbering as MouseUp.

func (*Client) MouseMove

func (c *Client) MouseMove(x, y int)

MouseMove moves the pointer to (x, y) in screen pixels from the top left.

func (*Client) MouseUp

func (c *Client) MouseUp(button, x, y int)

MouseUp releases button 0 (left), 1 (middle) or 2 (right) at (x, y). Any other value only moves the pointer. The numbering is the sender's, not the protocol's.

func (*Client) MouseWheel

func (c *Client) MouseWheel(scroll, x, y int)

MouseWheel turns the wheel by scroll notches at (x, y); positive is up (away from the user) and the magnitude is clamped to the protocol's 9 bit field.

func (*Client) OnBitmap

func (c *Client) OnBitmap(f func([]Bitmap))

OnBitmap registers f to be called with each bitmap update, already expanded and converted to BGRA at 32 bpp where the source was compressed. f may see the same underlying data reused by the next update, so copy anything it keeps. For VNC the Bitmap rectangles carry the VNC pixel format instead.

func (*Client) OnClipboardText

func (c *Client) OnClipboardText(f func(text string))

OnClipboardText reports text the server has put on the clipboard. It only fires when the clipboard channel is enabled.

func (*Client) OnClose

func (c *Client) OnClose(f func())

OnClose registers f to be called when the connection to the server is gone. Like the other handlers it may be registered before Login.

func (*Client) OnError

func (c *Client) OnError(f func(e error))

OnError registers f to be called with a handshake or transport error. Handlers may be registered before Login; they are kept and delivered once the session layers exist.

func (*Client) OnOrdersFrame added in v0.3.0

func (c *Client) OnOrdersFrame(f func(dirty image.Rectangle))

OnOrdersFrame reports that a batch of drawing orders changed the screen, giving the area that changed. The pixels are in Client.Screen.

func (*Client) OnPointer

func (c *Client) OnPointer(f func(p *pdu.PointerDataPDU))

OnPointer reports slow-path pointer updates (PDUTYPE2_POINTER), which carry either a new position or a pointer shape.

func (*Client) OnPointerPosition

func (c *Client) OnPointerPosition(f func(x, y int))

OnPointerPosition reports the server-side pointer position (fast-path FASTPATH_UPDATETYPE_PTR_POSITION). It is useful to confirm that mouse input reached the remote session.

func (*Client) OnReady

func (c *Client) OnReady(f func())

OnReady registers f to be called once the session is negotiated and the server is about to send its first update. It fires before any bitmap or order callback.

func (*Client) OnSuccess

func (c *Client) OnSuccess(f func())

OnSuccess registers f to be called when the server accepts the credentials, before the session is ready to draw.

func (*Client) OnSurfaceFrame

func (c *Client) OnSurfaceFrame(f func(frameID uint32, surfaces []*rdpgfx.Surface))

OnSurfaceFrame reports each completed EGFX frame and the surfaces it touched. The pixel data lives on the surfaces, so callers that need it should copy what they need before returning.

func (*Client) OnSurfaceReset

func (c *Client) OnSurfaceReset(f func(width, height int))

OnSurfaceReset reports the desktop size an EGFX server will composite into, which also means any previously known surfaces are gone.

func (*Client) RequestClipboardText

func (c *Client) RequestClipboardText() error

RequestClipboardText asks the server for the clipboard text it last announced. OnClipboardText normally triggers this by itself, but a server can announce a format before it is able to serve it, in which case an explicit request made later is what gets the data.

func (*Client) Screen added in v0.3.0

func (c *Client) Screen() *orders.Screen

Screen returns the framebuffer that drawing orders are rendered into, or nil when Setting.EnableOrders was not set. It holds BGRA pixels, top down, and is safe to read while the connection is running.

func (*Client) SetClipboardText

func (c *Client) SetClipboardText(text string) error

SetClipboardText publishes text on the shared clipboard, so a paste on the server pastes it. It only does anything when the clipboard channel is enabled.

type Control

type Control interface {
	Login(host, user, passwd string, width, height int) error
	KeyUp(sc int, name string)
	KeyDown(sc int, name string)
	MouseMove(x, y int)
	MouseWheel(scroll, x, y int)
	MouseUp(button int, x, y int)
	MouseDown(button int, x, y int)
	On(event string, msg interface{})
	SetClipboardText(text string) error
	RequestClipboardText() error
	Close()
}

Control is the small surface both clients implement, so that Client can hold either one. Callers normally use Client rather than this.

type RdpClient

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

RdpClient is the RDP implementation of Control. NewClient builds one (stored unexported) for TC_RDP; callers interact with it through Client, which forwards input, events and the clipboard to it.

func (*RdpClient) Close

func (c *RdpClient) Close()

Close closes the underlying transport. It is safe on a partially built client and more than once; input methods become no-ops afterwards.

func (*RdpClient) KeyDown

func (c *RdpClient) KeyDown(sc int, name string)

KeyDown presses sc. See Client.KeyUp for the scancode and extended key conventions. name is ignored.

func (*RdpClient) KeyUp

func (c *RdpClient) KeyUp(sc int, name string)

KeyUp releases sc. See Client.KeyUp for the scancode and extended key conventions. name is ignored.

func (*RdpClient) Login

func (c *RdpClient) Login(host, user, pwd string, width, height int) error

Login dials host, builds the protocol stack (TPKT/X.224/MCS/security/PDU) and starts the connection. user may be "user", "DOMAIN\user" or "domain/user": the domain is split off and sent separately.

Login returns once the connection has been established, which is before the session is usable. The session becomes ready asynchronously and is reported on the "ready" event; errors and closure arrive on "error" and "close". Use Client.Login or Client.LoginContext to connect and wait for readiness in one call.

func (*RdpClient) MouseDown

func (c *RdpClient) MouseDown(button int, x, y int)

MouseDown presses button 0 (left), 1 (middle) or 2 (right) at (x, y).

func (*RdpClient) MouseMove

func (c *RdpClient) MouseMove(x, y int)

MouseMove moves the pointer to (x, y) in screen pixels from the top left.

func (*RdpClient) MouseUp

func (c *RdpClient) MouseUp(button int, x, y int)

MouseUp releases button 0 (left), 1 (middle) or 2 (right) at (x, y).

func (*RdpClient) MouseWheel

func (c *RdpClient) MouseWheel(scroll, x, y int)

MouseWheel turns the wheel by scroll notches at (x, y); positive is up.

func (*RdpClient) On

func (c *RdpClient) On(event string, f interface{})

On registers a handler for an event. It may be called before Login, in which case handlers are buffered and replayed once the layer that produces them exists; this ordering is what lets a caller register handlers and then call Login without losing the "ready" event.

Event names: "ready", "success", "error" (error), "close", "bitmap" ([]pdu.BitmapData), "surface-bits" (*pdu.SurfaceBitsCommand), "orders" ([]pdu.OrderPdu), "orders-frame" (image.Rectangle), "pointer" (*pdu.PointerDataPDU), "pointer-position" (*pdu.FastPathPointerPositionPDU), "clipboard-text" (string), and "gfx-frame"/"gfx-reset" from the graphics channel. f must have the type the event carries or the receive will panic.

func (*RdpClient) RequestClipboardText

func (c *RdpClient) RequestClipboardText() error

RequestClipboardText asks the server for its clipboard text.

func (*RdpClient) SetClipboardText

func (c *RdpClient) SetClipboardText(text string) error

SetClipboardText publishes text on the shared clipboard.

type Setting

type Setting struct {
	// Width and Height are the desktop size requested from the server, in
	// pixels. They are sent in the MCS connect sequence and must be positive;
	// NewSetting uses 1024x768.
	Width  int
	Height int

	// Protocol selects the security path: "tls" (the default) or "ssl" for
	// TLS, "nla", "hybrid" or "credssp" for CredSSP, and "rdp" or "standard"
	// for Standard RDP Security with no TLS. The empty string means TLS, and an
	// unrecognised value falls back to TLS with a warning rather than failing.
	Protocol string

	// Timeout bounds the whole of Login, from dial to ready. Zero or negative
	// means DefaultLoginTimeout (30s).
	Timeout time.Duration

	// LogLevel is applied to the package-global glog logger by NewClient. The
	// default is glog.INFO; glog.NONE silences the library.
	LogLevel glog.LEVEL

	// NoFastPathInput forces keyboard/mouse input over the slow path even
	// when the server advertised fast-path input. Useful for debugging.
	NoFastPathInput bool

	// EnableEGFX opens the dynamic virtual channel needed for the RDP
	// Graphics Pipeline Extension, so the server can composite surfaces and
	// send them as compressed bitmaps instead of bitmap updates.
	//
	// This is off by default: it has not been exercised against a server that
	// speaks EGFX, and enabling it changes how the server draws the session.
	EnableEGFX bool

	// EnableClipboard opens the clipboard channel so text can be exchanged
	// with the server. Use Client.OnClipboardText and Client.SetClipboardText
	// to bridge it to the local clipboard.
	EnableClipboard bool

	// EnableOrders advertises support for the MEMBLT drawing order and renders
	// the drawing orders that follow, into a framebuffer reachable through
	// Client.Screen.
	//
	// It is off by default because it changes how the server draws the session
	// and it cannot mix: once MEMBLT is advertised the server stops sending
	// bitmap updates and puts everything through the bitmap cache instead. With
	// it off the session is drawn from bitmap updates, which is correct, just
	// larger on the wire.
	EnableOrders bool
}

Setting configures a session. NewSetting returns one with usable defaults; the fields below are only read when Login is called, so they may be changed until then.

func NewSetting

func NewSetting() *Setting

NewSetting returns a Setting with the default desktop size (1024x768), the default login timeout and INFO logging.

func (*Setting) SetClipboard

func (s *Setting) SetClipboard(c int)

SetClipboard is inert: it does nothing. Use Setting.EnableClipboard to open the clipboard channel.

func (*Setting) SetLogLevel

func (s *Setting) SetLogLevel()

SetLogLevel pushes LogLevel into the package-global glog logger. NewClient already does this, so it only needs calling if the level is changed after the client is built.

func (*Setting) SetRequestedProtocol

func (s *Setting) SetRequestedProtocol(p uint32)

SetRequestedProtocol is inert: it does nothing. Set Setting.Protocol instead, which is read when Login negotiates the security path.

type VncClient

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

VncClient is the RFB (VNC) client. It is exercised against a real server by scripts/vnc-dev.sh, which starts TigerVNC and runs the live tests: a frame is received and parsed, and the clipboard works in both directions. Raw and CopyRect encodings only, and RequestClipboardText is not implemented.

Login blocks until the handshake completes, fails, or the configured timeout expires, so it is safe to register handlers and drive input afterwards; before it returns those calls do nothing rather than panic.

func (*VncClient) Close

func (c *VncClient) Close()

Close implements the Control interface. It does nothing until Login has completed.

func (*VncClient) KeyDown

func (c *VncClient) KeyDown(sc int, name string)

KeyDown implements the Control interface. It does nothing until Login has completed.

func (*VncClient) KeyUp

func (c *VncClient) KeyUp(sc int, name string)

KeyUp implements the Control interface. It does nothing until Login has completed.

func (*VncClient) Login

func (c *VncClient) Login(host, user, pwd string, width, height int) error

Login dials the server and returns only once the RFB handshake has completed, the handshake has failed, or the timeout expires. The handshake is event driven, so it is driven to a result the caller can see instead of returning while it is still running. scripts/vnc-dev.sh runs this against a real server.

func (*VncClient) MouseDown

func (c *VncClient) MouseDown(button int, x, y int)

MouseDown implements the Control interface. It does nothing until Login has completed.

func (*VncClient) MouseMove

func (c *VncClient) MouseMove(x, y int)

MouseMove implements the Control interface. It does nothing until Login has completed.

func (*VncClient) MouseUp

func (c *VncClient) MouseUp(button int, x, y int)

MouseUp implements the Control interface. It does nothing until Login has completed.

func (*VncClient) MouseWheel

func (c *VncClient) MouseWheel(scroll, x, y int)

MouseWheel implements the Control interface. It does nothing until Login has completed.

func (*VncClient) On

func (c *VncClient) On(event string, f interface{})

On registers a listener. A listener registered before Login is held until there is a connection to attach it to, so a caller that registers before connecting still receives events.

func (*VncClient) RequestClipboardText

func (c *VncClient) RequestClipboardText() error

RequestClipboardText is not supported by RFB and says so. Unlike the RDP clipboard channel, RFB has no client message that asks for the server's cut text: the server sends ServerCutText whenever its clipboard changes, and the only client-to-server message, ClientCutText, publishes rather than requests. Text therefore arrives through OnClipboardText with no request behind it.

func (*VncClient) SetClipboardText

func (c *VncClient) SetClipboardText(text string) error

SetClipboardText publishes text on the server's clipboard. RFB carries it as one ClientCutText message. There is no acknowledgement, so a nil error only means the bytes were written to the socket.

Jump to

Keyboard shortcuts

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