2017-06-12 14:11:23 +03:00
|
|
|
package vnc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-06-13 01:52:07 +03:00
|
|
|
"sync"
|
2017-06-12 14:11:23 +03:00
|
|
|
)
|
|
|
|
|
2017-06-26 14:16:03 +03:00
|
|
|
var (
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultClientHandlers represents default client handlers
|
|
|
|
DefaultClientHandlers = []ClientHandler{
|
2017-06-26 14:16:03 +03:00
|
|
|
&DefaultClientVersionHandler{},
|
|
|
|
&DefaultClientSecurityHandler{},
|
|
|
|
&DefaultClientClientInitHandler{},
|
|
|
|
&DefaultClientServerInitHandler{},
|
2017-06-29 00:09:31 +03:00
|
|
|
&DefaultClientMessageHandler{},
|
2017-06-26 14:16:03 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Connect handshake with remote server using underlining net.Conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func Connect(ctx context.Context, c net.Conn, cfg *ClientConfig) (*ClientConn, error) {
|
|
|
|
conn, err := NewClientConn(c, cfg)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2017-06-26 14:16:03 +03:00
|
|
|
cfg.ErrorCh <- err
|
2017-06-12 14:11:23 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-06-26 14:16:03 +03:00
|
|
|
if len(cfg.Handlers) == 0 {
|
|
|
|
cfg.Handlers = DefaultClientHandlers
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-06-26 14:16:03 +03:00
|
|
|
for _, h := range cfg.Handlers {
|
|
|
|
if err := h.Handle(conn); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
cfg.ErrorCh <- err
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Conn = (*ClientConn)(nil)
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Config returns connection config
|
2017-06-26 14:16:03 +03:00
|
|
|
func (c *ClientConn) Config() interface{} {
|
|
|
|
return c.cfg
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Wait waiting for connection close
|
2017-06-29 00:09:31 +03:00
|
|
|
func (c *ClientConn) Wait() {
|
|
|
|
<-c.quit
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Conn return underlining net.Conn
|
2017-06-13 01:52:07 +03:00
|
|
|
func (c *ClientConn) Conn() net.Conn {
|
|
|
|
return c.c
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetProtoVersion sets proto version
|
2017-06-13 01:52:07 +03:00
|
|
|
func (c *ClientConn) SetProtoVersion(pv string) {
|
|
|
|
c.protocol = pv
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetEncodings write SetEncodings message
|
2017-06-13 01:52:07 +03:00
|
|
|
func (c *ClientConn) SetEncodings(encs []EncodingType) error {
|
|
|
|
|
|
|
|
msg := &SetEncodings{
|
|
|
|
EncNum: uint16(len(encs)),
|
|
|
|
Encodings: encs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg.Write(c)
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Flush flushes data to conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Flush() error {
|
|
|
|
return c.bw.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Close closing conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Close() error {
|
2017-06-29 00:09:31 +03:00
|
|
|
if c.quit != nil {
|
|
|
|
close(c.quit)
|
|
|
|
c.quit = nil
|
|
|
|
}
|
2017-06-29 15:19:04 +03:00
|
|
|
if c.quitCh != nil {
|
|
|
|
close(c.quitCh)
|
|
|
|
}
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.c.Close()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read reads data from conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Read(buf []byte) (int, error) {
|
|
|
|
return c.br.Read(buf)
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write data to conn must be Flushed
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Write(buf []byte) (int, error) {
|
|
|
|
return c.bw.Write(buf)
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ColorMap returns color map
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ClientConn) ColorMap() ColorMap {
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.colorMap
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetColorMap sets color map
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ClientConn) SetColorMap(cm ColorMap) {
|
2017-06-12 14:11:23 +03:00
|
|
|
c.colorMap = cm
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// DesktopName returns connection desktop name
|
2017-06-26 14:16:03 +03:00
|
|
|
func (c *ClientConn) DesktopName() []byte {
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.desktopName
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// PixelFormat returns connection pixel format
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ClientConn) PixelFormat() PixelFormat {
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.pixelFormat
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// SetDesktopName sets desktop name
|
2017-06-26 14:16:03 +03:00
|
|
|
func (c *ClientConn) SetDesktopName(name []byte) {
|
2017-06-29 15:19:04 +03:00
|
|
|
copy(c.desktopName, name)
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// SetPixelFormat sets pixel format
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ClientConn) SetPixelFormat(pf PixelFormat) error {
|
2017-06-13 01:52:07 +03:00
|
|
|
c.pixelFormat = pf
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// Encodings returns client encodings
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Encodings() []Encoding {
|
|
|
|
return c.encodings
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// Width returns width
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Width() uint16 {
|
|
|
|
return c.fbWidth
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// Height returns height
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Height() uint16 {
|
|
|
|
return c.fbHeight
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// Protocol returns protocol
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ClientConn) Protocol() string {
|
|
|
|
return c.protocol
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// SetWidth sets width of client conn
|
|
|
|
func (c *ClientConn) SetWidth(width uint16) {
|
|
|
|
c.fbWidth = width
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// SetHeight sets height of client conn
|
|
|
|
func (c *ClientConn) SetHeight(height uint16) {
|
|
|
|
c.fbHeight = height
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// The ClientConn type holds client connection information
|
2017-06-12 14:11:23 +03:00
|
|
|
type ClientConn struct {
|
|
|
|
c net.Conn
|
|
|
|
br *bufio.Reader
|
|
|
|
bw *bufio.Writer
|
|
|
|
cfg *ClientConfig
|
|
|
|
protocol string
|
2017-07-04 01:36:10 +03:00
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
// If the pixel format uses a color map, then this is the color
|
|
|
|
// map that is used. This should not be modified directly, since
|
|
|
|
// the data comes from the server.
|
|
|
|
// Definition in §5 - Representation of Pixel Data.
|
2017-06-30 00:24:39 +03:00
|
|
|
colorMap ColorMap
|
2017-06-12 14:11:23 +03:00
|
|
|
|
|
|
|
// Name associated with the desktop, sent from the server.
|
2017-06-26 14:16:03 +03:00
|
|
|
desktopName []byte
|
2017-06-12 14:11:23 +03:00
|
|
|
|
|
|
|
// Encodings supported by the client. This should not be modified
|
|
|
|
// directly. Instead, SetEncodings() should be used.
|
|
|
|
encodings []Encoding
|
|
|
|
|
|
|
|
// Height of the frame buffer in pixels, sent from the server.
|
|
|
|
fbHeight uint16
|
|
|
|
|
|
|
|
// Width of the frame buffer in pixels, sent from the server.
|
|
|
|
fbWidth uint16
|
|
|
|
|
|
|
|
// The pixel format associated with the connection. This shouldn't
|
|
|
|
// be modified. If you wish to set a new pixel format, use the
|
|
|
|
// SetPixelFormat method.
|
2017-06-30 00:24:39 +03:00
|
|
|
pixelFormat PixelFormat
|
2017-06-12 14:11:23 +03:00
|
|
|
|
2017-06-26 14:16:03 +03:00
|
|
|
quitCh chan struct{}
|
2017-06-29 00:09:31 +03:00
|
|
|
quit chan struct{}
|
2017-06-26 14:16:03 +03:00
|
|
|
errorCh chan error
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// NewClientConn creates new client conn using config
|
2017-06-12 14:11:23 +03:00
|
|
|
func NewClientConn(c net.Conn, cfg *ClientConfig) (*ClientConn, error) {
|
|
|
|
if len(cfg.Encodings) == 0 {
|
|
|
|
return nil, fmt.Errorf("client can't handle encodings")
|
|
|
|
}
|
|
|
|
return &ClientConn{
|
|
|
|
c: c,
|
|
|
|
cfg: cfg,
|
|
|
|
br: bufio.NewReader(c),
|
|
|
|
bw: bufio.NewWriter(c),
|
|
|
|
encodings: cfg.Encodings,
|
2017-06-26 14:16:03 +03:00
|
|
|
quitCh: cfg.QuitCh,
|
|
|
|
errorCh: cfg.ErrorCh,
|
2017-06-12 14:11:23 +03:00
|
|
|
pixelFormat: cfg.PixelFormat,
|
2017-06-29 00:09:31 +03:00
|
|
|
quit: make(chan struct{}),
|
2017-06-12 14:11:23 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultClientMessageHandler represents default client message handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultClientMessageHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle handles server messages.
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultClientMessageHandler) Handle(c Conn) error {
|
|
|
|
cfg := c.Config().(*ClientConfig)
|
2017-06-12 14:11:23 +03:00
|
|
|
var err error
|
2017-06-13 01:52:07 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
2017-06-12 14:11:23 +03:00
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
serverMessages := make(map[ServerMessageType]ServerMessage)
|
2017-06-26 14:16:03 +03:00
|
|
|
for _, m := range cfg.ServerMessages {
|
2017-06-12 14:11:23 +03:00
|
|
|
serverMessages[m.Type()] = m
|
|
|
|
}
|
|
|
|
|
2017-06-26 14:16:03 +03:00
|
|
|
go func() {
|
2017-06-13 01:52:07 +03:00
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
2017-06-26 14:16:03 +03:00
|
|
|
case msg := <-cfg.ClientMessageCh:
|
2017-06-13 01:52:07 +03:00
|
|
|
if err = msg.Write(c); err != nil {
|
2017-06-26 14:16:03 +03:00
|
|
|
cfg.ErrorCh <- err
|
|
|
|
return
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
}()
|
|
|
|
|
2017-06-26 14:16:03 +03:00
|
|
|
go func() {
|
2017-06-13 01:52:07 +03:00
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
var messageType ServerMessageType
|
|
|
|
if err = binary.Read(c, binary.BigEndian, &messageType); err != nil {
|
2017-06-26 14:16:03 +03:00
|
|
|
cfg.ErrorCh <- err
|
|
|
|
return
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
|
|
|
msg, ok := serverMessages[messageType]
|
|
|
|
if !ok {
|
2017-06-26 14:16:03 +03:00
|
|
|
err = fmt.Errorf("unknown message-type: %v", messageType)
|
|
|
|
cfg.ErrorCh <- err
|
|
|
|
return
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-06-13 16:20:35 +03:00
|
|
|
parsedMsg, err := msg.Read(c)
|
|
|
|
if err != nil {
|
2017-06-26 14:16:03 +03:00
|
|
|
cfg.ErrorCh <- err
|
|
|
|
return
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
cfg.ServerMessageCh <- parsedMsg
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
}()
|
2017-06-26 14:16:03 +03:00
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
wg.Wait()
|
2017-06-26 14:16:03 +03:00
|
|
|
return nil
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A ClientConfig structure is used to configure a ClientConn. After
|
|
|
|
// one has been passed to initialize a connection, it must not be modified.
|
|
|
|
type ClientConfig struct {
|
2017-06-26 14:16:03 +03:00
|
|
|
Handlers []ClientHandler
|
|
|
|
SecurityHandlers []SecurityHandler
|
|
|
|
Encodings []Encoding
|
2017-06-30 00:24:39 +03:00
|
|
|
PixelFormat PixelFormat
|
|
|
|
ColorMap ColorMap
|
2017-06-26 14:16:03 +03:00
|
|
|
ClientMessageCh chan ClientMessage
|
|
|
|
ServerMessageCh chan ServerMessage
|
|
|
|
Exclusive bool
|
|
|
|
ServerMessages []ServerMessage
|
|
|
|
QuitCh chan struct{}
|
|
|
|
ErrorCh chan error
|
2017-06-29 00:09:31 +03:00
|
|
|
quit chan struct{}
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|