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
|
|
|
)
|
|
|
|
|
|
|
|
var _ Conn = (*ServerConn)(nil)
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Config returns config for server conn
|
2017-06-26 14:16:03 +03:00
|
|
|
func (c *ServerConn) Config() interface{} {
|
|
|
|
return c.cfg
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Conn returns underlining server net.Conn
|
2017-06-13 01:52:07 +03:00
|
|
|
func (c *ServerConn) Conn() net.Conn {
|
|
|
|
return c.c
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Wait waits connection to close
|
2017-06-29 00:09:31 +03:00
|
|
|
func (c *ServerConn) Wait() {
|
|
|
|
<-c.quit
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetEncodings ??? sets server connection encodings
|
2017-06-13 01:52:07 +03:00
|
|
|
func (c *ServerConn) SetEncodings(encs []EncodingType) error {
|
|
|
|
encodings := make(map[EncodingType]Encoding)
|
|
|
|
for _, enc := range c.cfg.Encodings {
|
|
|
|
encodings[enc.Type()] = enc
|
|
|
|
}
|
|
|
|
for _, encType := range encs {
|
|
|
|
if enc, ok := encodings[encType]; ok {
|
|
|
|
c.encodings = append(c.encodings, enc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetProtoVersion ??? sets proto version
|
2017-06-13 01:52:07 +03:00
|
|
|
func (c *ServerConn) SetProtoVersion(pv string) {
|
|
|
|
c.protocol = pv
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Flush buffered data to server conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) Flush() error {
|
|
|
|
return c.bw.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Close closing server conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) Close() error {
|
2017-06-29 00:09:31 +03:00
|
|
|
if c.quit != nil {
|
|
|
|
close(c.quit)
|
|
|
|
c.quit = nil
|
|
|
|
}
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.c.Close()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read reads data from net.Conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) Read(buf []byte) (int, error) {
|
|
|
|
return c.br.Read(buf)
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write writes data to net.Conn, must be Flashed
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) Write(buf []byte) (int, error) {
|
|
|
|
return c.bw.Write(buf)
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ColorMap returns server connection color map
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ServerConn) ColorMap() ColorMap {
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.colorMap
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetColorMap sets connection color map
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ServerConn) 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 *ServerConn) DesktopName() []byte {
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.desktopName
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// PixelFormat return connection pixel format
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ServerConn) PixelFormat() PixelFormat {
|
2017-06-12 14:11:23 +03:00
|
|
|
return c.pixelFormat
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// SetDesktopName sets connection desktop name
|
2017-06-26 14:16:03 +03:00
|
|
|
func (c *ServerConn) SetDesktopName(name []byte) {
|
2017-07-04 14:24:41 +03:00
|
|
|
c.desktopName = name
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// SetPixelFormat sets pixel format for server conn
|
2017-06-30 00:24:39 +03:00
|
|
|
func (c *ServerConn) 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 connection encodings
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) Encodings() []Encoding {
|
|
|
|
return c.encodings
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// Width returns framebuffer width
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) Width() uint16 {
|
|
|
|
return c.fbWidth
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// Height returns framebuffer height
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) 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 *ServerConn) Protocol() string {
|
|
|
|
return c.protocol
|
|
|
|
}
|
|
|
|
|
2017-07-05 16:08:27 +03:00
|
|
|
// SecurityHandler returns security handler
|
|
|
|
func (c *ServerConn) SecurityHandler() SecurityHandler {
|
|
|
|
return c.securityHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSecurityHandler sets security handler
|
|
|
|
func (c *ServerConn) SetSecurityHandler(sechandler SecurityHandler) error {
|
|
|
|
c.securityHandler = sechandler
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetWidth sets framebuffer width
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) SetWidth(w uint16) {
|
2017-07-04 01:36:10 +03:00
|
|
|
// TODO send desktopsize pseudo encoding
|
2017-06-12 14:11:23 +03:00
|
|
|
c.fbWidth = w
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
// SetHeight sets framebuffer height
|
2017-06-12 14:11:23 +03:00
|
|
|
func (c *ServerConn) SetHeight(h uint16) {
|
2017-07-04 01:36:10 +03:00
|
|
|
// TODO send desktopsize pseudo encoding
|
2017-06-12 14:11:23 +03:00
|
|
|
c.fbHeight = h
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ServerConn underlining server conn
|
2017-06-12 14:11:23 +03:00
|
|
|
type ServerConn struct {
|
|
|
|
c net.Conn
|
|
|
|
cfg *ServerConfig
|
|
|
|
br *bufio.Reader
|
|
|
|
bw *bufio.Writer
|
|
|
|
protocol string
|
|
|
|
// 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
|
|
|
|
|
2017-07-05 16:08:27 +03:00
|
|
|
securityHandler SecurityHandler
|
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
// Height of the frame buffer in pixels, sent to the client.
|
|
|
|
fbHeight uint16
|
|
|
|
|
|
|
|
// Width of the frame buffer in pixels, sent to the client.
|
|
|
|
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-29 00:09:31 +03:00
|
|
|
|
|
|
|
quit chan struct{}
|
2017-06-26 14:16:03 +03:00
|
|
|
}
|
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
|
|
|
// DefaultServerHandlers uses default handlers for hanshake
|
2017-07-04 14:24:41 +03:00
|
|
|
DefaultServerHandlers = []Handler{
|
2017-06-26 14:16:03 +03:00
|
|
|
&DefaultServerVersionHandler{},
|
|
|
|
&DefaultServerSecurityHandler{},
|
|
|
|
&DefaultServerClientInitHandler{},
|
|
|
|
&DefaultServerServerInitHandler{},
|
|
|
|
&DefaultServerMessageHandler{},
|
|
|
|
}
|
|
|
|
)
|
2017-06-12 14:11:23 +03:00
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ServerConfig config struct
|
2017-06-12 14:11:23 +03:00
|
|
|
type ServerConfig struct {
|
2017-07-04 14:24:41 +03:00
|
|
|
Handlers []Handler
|
2017-06-26 14:16:03 +03:00
|
|
|
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
|
2017-07-04 14:24:41 +03:00
|
|
|
Messages []ClientMessage
|
2017-06-26 14:16:03 +03:00
|
|
|
DesktopName []byte
|
|
|
|
Height uint16
|
|
|
|
Width uint16
|
2017-06-30 00:24:39 +03:00
|
|
|
ErrorCh chan error
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// NewServerConn returns new Server connection fron net.Conn
|
2017-06-12 14:11:23 +03:00
|
|
|
func NewServerConn(c net.Conn, cfg *ServerConfig) (*ServerConn, error) {
|
|
|
|
return &ServerConn{
|
|
|
|
c: c,
|
|
|
|
br: bufio.NewReader(c),
|
|
|
|
bw: bufio.NewWriter(c),
|
|
|
|
cfg: cfg,
|
2017-06-26 14:16:03 +03:00
|
|
|
desktopName: cfg.DesktopName,
|
2017-06-12 14:11:23 +03:00
|
|
|
encodings: cfg.Encodings,
|
|
|
|
pixelFormat: cfg.PixelFormat,
|
2017-06-13 01:52:07 +03:00
|
|
|
fbWidth: cfg.Width,
|
|
|
|
fbHeight: cfg.Height,
|
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
|
|
|
// Serve serves requests from net.Listener using ServerConfig
|
2017-06-12 14:11:23 +03:00
|
|
|
func Serve(ctx context.Context, ln net.Listener, cfg *ServerConfig) error {
|
|
|
|
for {
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
conn, err := NewServerConn(c, cfg)
|
|
|
|
if err != nil {
|
2017-06-30 00:24:39 +03:00
|
|
|
cfg.ErrorCh <- err
|
2017-06-12 14:11:23 +03:00
|
|
|
continue
|
|
|
|
}
|
2017-06-30 00:24:39 +03:00
|
|
|
|
2017-06-26 14:16:03 +03:00
|
|
|
if len(cfg.Handlers) == 0 {
|
|
|
|
cfg.Handlers = DefaultServerHandlers
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-06-30 00:24:39 +03:00
|
|
|
handlerLoop:
|
2017-06-26 14:16:03 +03:00
|
|
|
for _, h := range cfg.Handlers {
|
|
|
|
if err := h.Handle(conn); err != nil {
|
2017-06-30 00:24:39 +03:00
|
|
|
cfg.ErrorCh <- err
|
2017-06-26 14:16:03 +03:00
|
|
|
conn.Close()
|
2017-06-30 00:24:39 +03:00
|
|
|
break handlerLoop
|
2017-06-26 14:16:03 +03:00
|
|
|
}
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultServerMessageHandler default package handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultServerMessageHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle handles messages from clients
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultServerMessageHandler) Handle(c Conn) error {
|
|
|
|
cfg := c.Config().(*ServerConfig)
|
2017-06-12 14:11:23 +03:00
|
|
|
var err error
|
2017-06-13 01:52:07 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
defer c.Close()
|
|
|
|
clientMessages := make(map[ClientMessageType]ClientMessage)
|
2017-07-04 14:24:41 +03:00
|
|
|
for _, m := range cfg.Messages {
|
2017-06-12 14:11:23 +03:00
|
|
|
clientMessages[m.Type()] = m
|
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
wg.Add(2)
|
|
|
|
|
2017-06-26 17:31:00 +03:00
|
|
|
quit := make(chan struct{})
|
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
// server
|
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 17:31:00 +03:00
|
|
|
case <-quit:
|
|
|
|
return
|
2017-06-26 14:16:03 +03:00
|
|
|
case msg := <-cfg.ServerMessageCh:
|
2017-06-13 01:52:07 +03:00
|
|
|
if err = msg.Write(c); err != nil {
|
2017-06-30 00:24:39 +03:00
|
|
|
cfg.ErrorCh <- err
|
2017-06-29 00:09:31 +03:00
|
|
|
if quit != nil {
|
|
|
|
close(quit)
|
|
|
|
quit = nil
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
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
|
|
|
}()
|
|
|
|
|
|
|
|
// client
|
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 17:31:00 +03:00
|
|
|
case <-quit:
|
|
|
|
return
|
2017-06-13 01:52:07 +03:00
|
|
|
default:
|
|
|
|
var messageType ClientMessageType
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &messageType); err != nil {
|
2017-06-30 00:24:39 +03:00
|
|
|
cfg.ErrorCh <- err
|
2017-06-29 00:09:31 +03:00
|
|
|
if quit != nil {
|
|
|
|
close(quit)
|
|
|
|
quit = nil
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
return
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
|
|
|
msg, ok := clientMessages[messageType]
|
|
|
|
if !ok {
|
2017-06-30 00:24:39 +03:00
|
|
|
cfg.ErrorCh <- fmt.Errorf("unsupported message-type: %v", messageType)
|
2017-06-26 17:31:00 +03:00
|
|
|
close(quit)
|
2017-06-26 14:16:03 +03:00
|
|
|
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-30 00:24:39 +03:00
|
|
|
cfg.ErrorCh <- err
|
2017-06-29 00:09:31 +03:00
|
|
|
if quit != nil {
|
|
|
|
close(quit)
|
|
|
|
quit = nil
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
return
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
cfg.ClientMessageCh <- parsedMsg
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-06-12 14:11:23 +03:00
|
|
|
return nil
|
|
|
|
}
|