2017-06-12 14:11:23 +03:00
|
|
|
package vnc
|
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Handler represents handler of handshake
|
|
|
|
type Handler interface {
|
2017-07-03 23:57:45 +03:00
|
|
|
Handle(Conn) error
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ProtoVersionLength protocol version length
|
2017-06-13 01:52:07 +03:00
|
|
|
const ProtoVersionLength = 12
|
|
|
|
|
|
|
|
const (
|
2017-07-04 01:36:10 +03:00
|
|
|
// ProtoVersionUnknown unknown version
|
2017-06-13 01:52:07 +03:00
|
|
|
ProtoVersionUnknown = ""
|
2017-07-04 01:36:10 +03:00
|
|
|
// ProtoVersion33 sets if proto 003.003
|
|
|
|
ProtoVersion33 = "RFB 003.003\n"
|
|
|
|
// ProtoVersion38 sets if proto 003.008
|
|
|
|
ProtoVersion38 = "RFB 003.008\n"
|
2017-06-13 01:52:07 +03:00
|
|
|
)
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ParseProtoVersion parse protocol version
|
2017-06-13 01:52:07 +03:00
|
|
|
func ParseProtoVersion(pv []byte) (uint, uint, error) {
|
|
|
|
var major, minor uint
|
|
|
|
|
|
|
|
if len(pv) < ProtoVersionLength {
|
|
|
|
return 0, 0, fmt.Errorf("ProtocolVersion message too short (%v < %v)", len(pv), ProtoVersionLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := fmt.Sscanf(string(pv), "RFB %d.%d\n", &major, &minor)
|
|
|
|
if l != 2 {
|
2017-07-04 01:36:10 +03:00
|
|
|
return 0, 0, fmt.Errorf("error parsing protocol version")
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return major, minor, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultClientVersionHandler represents default handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultClientVersionHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide version handler for client side
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultClientVersionHandler) Handle(c Conn) error {
|
2017-06-13 01:52:07 +03:00
|
|
|
var version [ProtoVersionLength]byte
|
|
|
|
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &version); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
major, minor, err := ParseProtoVersion(version[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pv := ProtoVersionUnknown
|
|
|
|
if major == 3 {
|
|
|
|
if minor >= 8 {
|
|
|
|
pv = ProtoVersion38
|
|
|
|
} else if minor >= 3 {
|
|
|
|
pv = ProtoVersion38
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pv == ProtoVersionUnknown {
|
|
|
|
return fmt.Errorf("ProtocolVersion handshake failed; unsupported version '%v'", string(version[:]))
|
|
|
|
}
|
2017-06-20 00:55:32 +03:00
|
|
|
c.SetProtoVersion(string(version[:]))
|
2017-06-13 01:52:07 +03:00
|
|
|
|
|
|
|
if err := binary.Write(c, binary.BigEndian, []byte(pv)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultServerVersionHandler represents default server handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultServerVersionHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide server version handler
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultServerVersionHandler) Handle(c Conn) error {
|
2017-06-13 01:52:07 +03:00
|
|
|
var version [ProtoVersionLength]byte
|
|
|
|
if err := binary.Write(c, binary.BigEndian, []byte(ProtoVersion38)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &version); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
major, minor, err := ParseProtoVersion(version[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pv := ProtoVersionUnknown
|
|
|
|
if major == 3 {
|
|
|
|
if minor >= 8 {
|
|
|
|
pv = ProtoVersion38
|
|
|
|
} else if minor >= 3 {
|
|
|
|
pv = ProtoVersion33
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pv == ProtoVersionUnknown {
|
|
|
|
return fmt.Errorf("ProtocolVersion handshake failed; unsupported version '%v'", string(version[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetProtoVersion(pv)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultClientSecurityHandler used for client security handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultClientSecurityHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide client side security handler
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultClientSecurityHandler) Handle(c Conn) error {
|
|
|
|
cfg := c.Config().(*ClientConfig)
|
2017-06-13 01:52:07 +03:00
|
|
|
var numSecurityTypes uint8
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &numSecurityTypes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
secTypes := make([]SecurityType, numSecurityTypes)
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &secTypes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-20 00:55:32 +03:00
|
|
|
var secType SecurityHandler
|
|
|
|
for _, st := range cfg.SecurityHandlers {
|
|
|
|
for _, sc := range secTypes {
|
|
|
|
if st.Type() == sc {
|
|
|
|
secType = st
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
if err := binary.Write(c, binary.BigEndian, cfg.SecurityHandlers[0].Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-20 00:55:32 +03:00
|
|
|
err := secType.Auth(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
var authCode uint32
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &authCode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if authCode == 1 {
|
|
|
|
var reasonLength uint32
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &reasonLength); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reasonText := make([]byte, reasonLength)
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &reasonText); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s", reasonText)
|
|
|
|
}
|
2017-07-05 16:08:27 +03:00
|
|
|
c.SetSecurityHandler(secType)
|
2017-06-12 14:11:23 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultServerSecurityHandler used for server security handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultServerSecurityHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide server side security handler
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultServerSecurityHandler) Handle(c Conn) error {
|
|
|
|
cfg := c.Config().(*ServerConfig)
|
2017-06-13 01:52:07 +03:00
|
|
|
if err := binary.Write(c, binary.BigEndian, uint8(len(cfg.SecurityHandlers))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sectype := range cfg.SecurityHandlers {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, sectype.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var secType SecurityType
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &secType); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
secTypes := make(map[SecurityType]SecurityHandler)
|
|
|
|
for _, sType := range cfg.SecurityHandlers {
|
|
|
|
secTypes[sType.Type()] = sType
|
|
|
|
}
|
|
|
|
|
|
|
|
sType, ok := secTypes[secType]
|
|
|
|
if !ok {
|
2017-07-03 23:57:45 +03:00
|
|
|
return fmt.Errorf("security type %d not implemented", secType)
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var authCode uint32
|
|
|
|
authErr := sType.Auth(c)
|
|
|
|
if authErr != nil {
|
|
|
|
authCode = uint32(1)
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, authCode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-10 23:16:47 +03:00
|
|
|
|
|
|
|
if authErr == nil {
|
2017-06-18 19:42:41 +03:00
|
|
|
if err := c.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-10 23:16:47 +03:00
|
|
|
c.SetSecurityHandler(sType)
|
|
|
|
return nil
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-07-10 23:16:47 +03:00
|
|
|
if err := binary.Write(c, binary.BigEndian, uint32(len(authErr.Error()))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, []byte(authErr.Error())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return authErr
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultClientServerInitHandler default client server init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultClientServerInitHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide default server init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultClientServerInitHandler) Handle(c Conn) error {
|
2017-07-04 14:24:41 +03:00
|
|
|
var err error
|
2017-06-29 15:17:15 +03:00
|
|
|
srvInit := ServerInit{}
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &srvInit.FBWidth); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 14:24:41 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &srvInit.FBHeight); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 14:24:41 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &srvInit.PixelFormat); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 14:24:41 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &srvInit.NameLength); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-30 16:13:01 +03:00
|
|
|
srvInit.NameText = make([]byte, srvInit.NameLength)
|
2017-07-04 14:24:41 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &srvInit.NameText); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
c.SetDesktopName(srvInit.NameText)
|
2017-07-04 14:24:41 +03:00
|
|
|
if c.Protocol() == "aten1" {
|
2017-07-03 23:57:45 +03:00
|
|
|
c.SetWidth(800)
|
|
|
|
c.SetHeight(600)
|
2017-06-30 16:13:01 +03:00
|
|
|
c.SetPixelFormat(NewPixelFormatAten())
|
|
|
|
} else {
|
2017-07-03 23:57:45 +03:00
|
|
|
c.SetWidth(srvInit.FBWidth)
|
|
|
|
c.SetHeight(srvInit.FBHeight)
|
2017-06-30 16:13:01 +03:00
|
|
|
c.SetPixelFormat(srvInit.PixelFormat)
|
|
|
|
}
|
2017-07-04 14:24:41 +03:00
|
|
|
if c.Protocol() == "aten1" {
|
2017-06-29 00:09:31 +03:00
|
|
|
ikvm := struct {
|
|
|
|
_ [8]byte
|
|
|
|
IKVMVideoEnable uint8
|
|
|
|
IKVMKMEnable uint8
|
|
|
|
IKVMKickEnable uint8
|
|
|
|
VUSBEnable uint8
|
|
|
|
}{}
|
2017-07-04 14:24:41 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &ikvm); err != nil {
|
2017-06-29 00:09:31 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 14:24:41 +03:00
|
|
|
}
|
|
|
|
/*
|
2017-06-29 00:09:31 +03:00
|
|
|
caps := struct {
|
|
|
|
ServerMessagesNum uint16
|
|
|
|
ClientMessagesNum uint16
|
|
|
|
EncodingsNum uint16
|
|
|
|
_ [2]byte
|
|
|
|
}{}
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &caps); err != nil {
|
2017-06-26 14:16:03 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-06-29 00:09:31 +03:00
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
caps.ServerMessagesNum = uint16(1)
|
2017-06-29 00:09:31 +03:00
|
|
|
var item [16]byte
|
|
|
|
for i := uint16(0); i < caps.ServerMessagesNum; i++ {
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &item); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-03 23:57:45 +03:00
|
|
|
fmt.Printf("server message cap %s\n", item)
|
2017-06-29 00:09:31 +03:00
|
|
|
}
|
2017-07-03 23:57:45 +03:00
|
|
|
|
2017-06-29 00:09:31 +03:00
|
|
|
for i := uint16(0); i < caps.ClientMessagesNum; i++ {
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &item); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("client message cap %s\n", item)
|
|
|
|
}
|
|
|
|
for i := uint16(0); i < caps.EncodingsNum; i++ {
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &item); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("encoding cap %s\n", item)
|
|
|
|
}
|
2017-07-03 23:57:45 +03:00
|
|
|
// var pad [1]byte
|
|
|
|
// if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2017-07-04 14:24:41 +03:00
|
|
|
}*/
|
2017-06-12 14:11:23 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultServerServerInitHandler default server server init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultServerServerInitHandler struct{}
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide default server server init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultServerServerInitHandler) Handle(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, c.Width()); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
if err := binary.Write(c, binary.BigEndian, c.Height()); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
if err := binary.Write(c, binary.BigEndian, c.PixelFormat()); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
if err := binary.Write(c, binary.BigEndian, uint32(len(c.DesktopName()))); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-06-26 14:16:03 +03:00
|
|
|
if err := binary.Write(c, binary.BigEndian, []byte(c.DesktopName())); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultClientClientInitHandler default client client init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultClientClientInitHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide default client client init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultClientClientInitHandler) Handle(c Conn) error {
|
|
|
|
cfg := c.Config().(*ClientConfig)
|
2017-06-13 16:20:35 +03:00
|
|
|
var shared uint8
|
|
|
|
if cfg.Exclusive {
|
|
|
|
shared = 0
|
|
|
|
} else {
|
|
|
|
shared = 1
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, shared); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// DefaultServerClientInitHandler default server client init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
type DefaultServerClientInitHandler struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Handle provide default server client init handler
|
2017-06-26 14:16:03 +03:00
|
|
|
func (*DefaultServerClientInitHandler) Handle(c Conn) error {
|
2017-06-13 01:52:07 +03:00
|
|
|
var shared uint8
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &shared); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
/* TODO
|
|
|
|
if shared != 1 {
|
|
|
|
c.SetShared(false)
|
|
|
|
}
|
|
|
|
*/
|
2017-06-12 14:11:23 +03:00
|
|
|
return nil
|
|
|
|
}
|