* some debugging and fixing in the proxy (still needs some work)

* added a logger and stringers for message types
This commit is contained in:
betzalel
2017-07-11 16:50:06 +03:00
parent 092f92264a
commit 18bef62b79
28 changed files with 1340 additions and 726 deletions

View File

@@ -8,6 +8,7 @@ import (
"net"
"unicode"
"vncproxy/common"
"vncproxy/logger"
)
// A ServerMessage implements a message sent from the server to the client.
@@ -52,7 +53,7 @@ type ClientConn struct {
// SetPixelFormat method.
PixelFormat common.PixelFormat
Listener common.SegmentConsumer
Listeners *common.MultiListener
}
// A ClientConfig structure is used to configure a ClientConn. After
@@ -80,20 +81,26 @@ type ClientConfig struct {
ServerMessages []common.ServerMessage
}
func Client(c net.Conn, cfg *ClientConfig) (*ClientConn, error) {
func NewClientConn(c net.Conn, cfg *ClientConfig) (*ClientConn, error) {
conn := &ClientConn{
conn: c,
config: cfg,
conn: c,
config: cfg,
Listeners: &common.MultiListener{},
}
return conn, nil
}
func (conn *ClientConn) Connect() error {
if err := conn.handshake(); err != nil {
logger.Errorf("ClientConn.Connect error: %v", err)
conn.Close()
return nil, err
return err
}
go conn.mainLoop()
return conn, nil
return nil
}
func (c *ClientConn) Close() error {
@@ -360,11 +367,12 @@ func (c *ClientConn) handshake() error {
// 7.1.2 Security Handshake from server
var numSecurityTypes uint8
if err = binary.Read(c.conn, binary.BigEndian, &numSecurityTypes); err != nil {
return fmt.Errorf("Error reading security types: %v", err)
return err
}
if numSecurityTypes == 0 {
return fmt.Errorf("no security types: %s", c.readErrorReason())
return fmt.Errorf("Error: no security types: %s", c.readErrorReason())
}
securityTypes := make([]uint8, numSecurityTypes)
@@ -455,9 +463,8 @@ FindAuth:
PixelFormat: c.PixelFormat,
}
rfbSeg := &common.RfbSegment{SegmentType: common.SegmentServerInitMessage, Message: &srvInit}
c.Listener.Consume(rfbSeg)
return nil
return c.Listeners.Consume(rfbSeg)
}
// mainLoop reads messages sent from the server and routes them to the
@@ -465,7 +472,7 @@ FindAuth:
func (c *ClientConn) mainLoop() {
defer c.Close()
reader := &common.RfbReadHelper{Reader: c.conn, Listener: c.Listener}
reader := &common.RfbReadHelper{Reader: c.conn, Listeners: c.Listeners}
// Build the map of available server messages
typeMap := make(map[uint8]common.ServerMessage)
@@ -486,6 +493,10 @@ func (c *ClientConn) mainLoop() {
}
}
defer func(){
logger.Warn("ClientConn.MainLoop: exiting!")
}()
for {
var messageType uint8
if err := binary.Read(c.conn, binary.BigEndian, &messageType); err != nil {
@@ -497,6 +508,7 @@ func (c *ClientConn) mainLoop() {
// Unsupported message type! Bad!
break
}
logger.Debugf("ClientConn.MainLoop: got ServerMessage:%s", common.ServerMessageType(messageType))
reader.SendMessageSeparator(common.ServerMessageType(messageType))
reader.PublishBytes([]byte{byte(messageType)})
@@ -504,6 +516,7 @@ func (c *ClientConn) mainLoop() {
if err != nil {
break
}
logger.Debugf("ClientConn.MainLoop: read & parsed ServerMessage:%s, %v", parsedMsg.Type(), parsedMsg)
if c.config.ServerMessageCh == nil {
continue

View File

@@ -29,7 +29,7 @@ func readPixelFormat(r io.Reader, result *common.PixelFormat) error {
if pfBoolByte != 0 {
// Big endian is true
result.BigEndian = true
result.BigEndian = 1
}
if err := binary.Read(brPF, binary.BigEndian, &pfBoolByte); err != nil {
@@ -38,7 +38,7 @@ func readPixelFormat(r io.Reader, result *common.PixelFormat) error {
if pfBoolByte != 0 {
// True Color is true. So we also have to read all the color max & shifts.
result.TrueColor = true
result.TrueColor = 1
if err := binary.Read(brPF, binary.BigEndian, &result.RedMax); err != nil {
return err
@@ -82,7 +82,7 @@ func writePixelFormat(format *common.PixelFormat) ([]byte, error) {
}
var boolByte byte
if format.BigEndian {
if format.BigEndian == 1 {
boolByte = 1
} else {
boolByte = 0
@@ -93,7 +93,7 @@ func writePixelFormat(format *common.PixelFormat) ([]byte, error) {
return nil, err
}
if format.TrueColor {
if format.TrueColor == 1 {
boolByte = 1
} else {
boolByte = 0
@@ -106,7 +106,7 @@ func writePixelFormat(format *common.PixelFormat) ([]byte, error) {
// If we have true color enabled then we have to fill in the rest of the
// structure with the color values.
if format.TrueColor {
if format.TrueColor == 1 {
if err := binary.Write(&buf, binary.BigEndian, format.RedMax); err != nil {
return nil, err
}

View File

@@ -7,6 +7,8 @@ import (
"io"
"vncproxy/common"
"vncproxy/encodings"
"vncproxy/logger"
"strings"
)
// FramebufferUpdateMessage consists of a sequence of rectangles of
@@ -49,13 +51,13 @@ func (fbm *FramebufferUpdateMessage) Read(c common.IClientConn, r *common.RfbRea
// We must always support the raw encoding
rawEnc := new(encodings.RawEncoding)
encMap[rawEnc.Type()] = rawEnc
fmt.Printf("numrects= %d\n", numRects)
logger.Debugf("numrects= %d", numRects)
rects := make([]common.Rectangle, numRects)
for i := uint16(0); i < numRects; i++ {
fmt.Printf("###############rect################: %d\n", i)
logger.Debugf("###############rect################: %d\n", i)
var encodingType int32
var encodingTypeInt int32
r.SendRectSeparator(-1)
rect := &rects[i]
data := []interface{}{
@@ -63,34 +65,45 @@ func (fbm *FramebufferUpdateMessage) Read(c common.IClientConn, r *common.RfbRea
&rect.Y,
&rect.Width,
&rect.Height,
&encodingType,
&encodingTypeInt,
}
for _, val := range data {
if err := binary.Read(r, binary.BigEndian, val); err != nil {
fmt.Printf("err: %v\n", err)
logger.Errorf("err: %v", err)
return nil, err
}
}
jBytes, _ := json.Marshal(data)
fmt.Printf("rect hdr data: %s\n", string(jBytes))
//fmt.Printf(" encoding type: %d", encodingType)
enc, ok := encMap[encodingType]
if !ok {
return nil, fmt.Errorf("unsupported encoding type: %d\n", encodingType)
encType := common.EncodingType(encodingTypeInt)
logger.Debugf("rect hdr data: enctype=%s, data: %s\n", encType, string(jBytes))
enc, supported := encMap[encodingTypeInt]
if supported {
var err error
rect.Enc, err = enc.Read(c.CurrentPixelFormat(), rect, r)
if err != nil {
return nil, err
}
} else {
if strings.Contains(encType.String(), "Pseudo") {
rect.Enc = &encodings.PseudoEncoding{encodingTypeInt}
} else {
return nil, fmt.Errorf("unsupported encoding type: %d, %s", encodingTypeInt, encType)
}
}
var err error
rect.Enc, err = enc.Read(c.CurrentPixelFormat(), rect, r)
if err != nil {
return nil, err
}
}
return &FramebufferUpdateMessage{rects}, nil
}
// SetColorMapEntriesMessage is sent by the server to set values into
// the color map. This message will automatically update the color map
// for the associated connection, but contains the color change data