general refactoring + fixed setcursorpseudo

This commit is contained in:
amit bezalel
2017-07-20 02:01:40 +03:00
parent 662e8393e9
commit 2d87ae5773
38 changed files with 1082 additions and 1381 deletions

View File

@@ -6,13 +6,6 @@ import (
"vncproxy/common"
)
// SetPixelFormat holds the wire format message.
type SetPixelFormat struct {
_ [3]byte // padding
PF common.PixelFormat // pixel-format
_ [3]byte // padding after pixel format
}
// Key represents a VNC key press.
type Key uint32
@@ -21,11 +14,18 @@ type Key uint32
// Keys is a slice of Key values.
type Keys []Key
func (*SetPixelFormat) Type() common.ClientMessageType {
// MsgSetPixelFormat holds the wire format message.
type MsgSetPixelFormat struct {
_ [3]byte // padding
PF common.PixelFormat // pixel-format
_ [3]byte // padding after pixel format
}
func (*MsgSetPixelFormat) Type() common.ClientMessageType {
return common.SetPixelFormatMsgType
}
func (msg *SetPixelFormat) Write(c io.Writer) error {
func (msg *MsgSetPixelFormat) Write(c io.Writer) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
@@ -43,27 +43,27 @@ func (msg *SetPixelFormat) Write(c io.Writer) error {
return nil
}
func (*SetPixelFormat) Read(c io.Reader) (common.ClientMessage, error) {
msg := SetPixelFormat{}
func (*MsgSetPixelFormat) Read(c io.Reader) (common.ClientMessage, error) {
msg := MsgSetPixelFormat{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
// SetEncodings holds the wire format message, sans encoding-type field.
type SetEncodings struct {
// MsgSetEncodings holds the wire format message, sans encoding-type field.
type MsgSetEncodings struct {
_ [1]byte // padding
EncNum uint16 // number-of-encodings
Encodings []common.EncodingType
}
func (*SetEncodings) Type() common.ClientMessageType {
func (*MsgSetEncodings) Type() common.ClientMessageType {
return common.SetEncodingsMsgType
}
func (*SetEncodings) Read(c io.Reader) (common.ClientMessage, error) {
msg := SetEncodings{}
func (*MsgSetEncodings) Read(c io.Reader) (common.ClientMessage, error) {
msg := MsgSetEncodings{}
var pad [1]byte
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
return nil, err
@@ -79,11 +79,11 @@ func (*SetEncodings) Read(c io.Reader) (common.ClientMessage, error) {
}
msg.Encodings = append(msg.Encodings, enc)
}
c.(common.ServerConn).SetEncodings(msg.Encodings)
c.(common.IServerConn).SetEncodings(msg.Encodings)
return &msg, nil
}
func (msg *SetEncodings) Write(c io.Writer) error {
func (msg *MsgSetEncodings) Write(c io.Writer) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
@@ -107,26 +107,26 @@ func (msg *SetEncodings) Write(c io.Writer) error {
return nil
}
// FramebufferUpdateRequest holds the wire format message.
type FramebufferUpdateRequest struct {
// MsgFramebufferUpdateRequest holds the wire format message.
type MsgFramebufferUpdateRequest struct {
Inc uint8 // incremental
X, Y uint16 // x-, y-position
Width, Height uint16 // width, height
}
func (*FramebufferUpdateRequest) Type() common.ClientMessageType {
func (*MsgFramebufferUpdateRequest) Type() common.ClientMessageType {
return common.FramebufferUpdateRequestMsgType
}
func (*FramebufferUpdateRequest) Read(c io.Reader) (common.ClientMessage, error) {
msg := FramebufferUpdateRequest{}
func (*MsgFramebufferUpdateRequest) Read(c io.Reader) (common.ClientMessage, error) {
msg := MsgFramebufferUpdateRequest{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
func (msg *FramebufferUpdateRequest) Write(c io.Writer) error {
func (msg *MsgFramebufferUpdateRequest) Write(c io.Writer) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
@@ -136,26 +136,26 @@ func (msg *FramebufferUpdateRequest) Write(c io.Writer) error {
return nil
}
// KeyEvent holds the wire format message.
type KeyEvent struct {
// MsgKeyEvent holds the wire format message.
type MsgKeyEvent struct {
Down uint8 // down-flag
_ [2]byte // padding
Key Key // key
}
func (*KeyEvent) Type() common.ClientMessageType {
func (*MsgKeyEvent) Type() common.ClientMessageType {
return common.KeyEventMsgType
}
func (*KeyEvent) Read(c io.Reader) (common.ClientMessage, error) {
msg := KeyEvent{}
func (*MsgKeyEvent) Read(c io.Reader) (common.ClientMessage, error) {
msg := MsgKeyEvent{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
func (msg *KeyEvent) Write(c io.Writer) error {
func (msg *MsgKeyEvent) Write(c io.Writer) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
@@ -166,24 +166,24 @@ func (msg *KeyEvent) Write(c io.Writer) error {
}
// PointerEventMessage holds the wire format message.
type PointerEvent struct {
type MsgPointerEvent struct {
Mask uint8 // button-mask
X, Y uint16 // x-, y-position
}
func (*PointerEvent) Type() common.ClientMessageType {
func (*MsgPointerEvent) Type() common.ClientMessageType {
return common.PointerEventMsgType
}
func (*PointerEvent) Read(c io.Reader) (common.ClientMessage, error) {
msg := PointerEvent{}
func (*MsgPointerEvent) Read(c io.Reader) (common.ClientMessage, error) {
msg := MsgPointerEvent{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
func (msg *PointerEvent) Write(c io.Writer) error {
func (msg *MsgPointerEvent) Write(c io.Writer) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
@@ -193,14 +193,14 @@ func (msg *PointerEvent) Write(c io.Writer) error {
return nil
}
type ClientFence struct {
type MsgClientFence struct {
}
func (*ClientFence) Type() common.ClientMessageType {
func (*MsgClientFence) Type() common.ClientMessageType {
return common.ClientFenceMsgType
}
func (cf *ClientFence) Read(c io.Reader) (common.ClientMessage, error) {
func (cf *MsgClientFence) Read(c io.Reader) (common.ClientMessage, error) {
bytes := make([]byte, 3)
c.Read(bytes)
if _, err := c.Read(bytes); err != nil {
@@ -223,23 +223,23 @@ func (cf *ClientFence) Read(c io.Reader) (common.ClientMessage, error) {
return cf, nil
}
func (msg *ClientFence) Write(c io.Writer) error {
func (msg *MsgClientFence) Write(c io.Writer) error {
panic("not implemented!")
}
// ClientCutText holds the wire format message, sans the text field.
type ClientCutText struct {
// MsgClientCutText holds the wire format message, sans the text field.
type MsgClientCutText struct {
_ [3]byte // padding
Length uint32 // length
Text []byte
}
func (*ClientCutText) Type() common.ClientMessageType {
func (*MsgClientCutText) Type() common.ClientMessageType {
return common.ClientCutTextMsgType
}
func (*ClientCutText) Read(c io.Reader) (common.ClientMessage, error) {
msg := ClientCutText{}
func (*MsgClientCutText) Read(c io.Reader) (common.ClientMessage, error) {
msg := MsgClientCutText{}
var pad [3]byte
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
return nil, err
@@ -256,7 +256,7 @@ func (*ClientCutText) Read(c io.Reader) (common.ClientMessage, error) {
return &msg, nil
}
func (msg *ClientCutText) Write(c io.Writer) error {
func (msg *MsgClientCutText) Write(c io.Writer) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}

View File

@@ -49,7 +49,7 @@ const (
type SecurityHandler interface {
Type() SecurityType
SubType() SecuritySubType
Auth(common.ServerConn) error
Auth(common.IServerConn) error
}
// type ClientAuthNone struct{}
@@ -62,7 +62,7 @@ type SecurityHandler interface {
// return SecSubTypeUnknown
// }
// func (*ClientAuthNone) Auth(conn common.ServerConn) error {
// func (*ClientAuthNone) Auth(conn common.IServerConn) error {
// return nil
// }
@@ -73,7 +73,7 @@ func (*ServerAuthNone) Type() SecurityType {
return SecTypeNone
}
func (*ServerAuthNone) Auth(c common.ServerConn) error {
func (*ServerAuthNone) Auth(c common.IServerConn) error {
return nil
}
@@ -95,7 +95,7 @@ func (*ServerAuthNone) SubType() SecuritySubType {
// Password []byte
// }
// func (auth *ClientAuthVeNCrypt02Plain) Auth(c common.ServerConn) error {
// func (auth *ClientAuthVeNCrypt02Plain) Auth(c common.IServerConn) error {
// if err := binary.Write(c, binary.BigEndian, []uint8{0, 2}); err != nil {
// return err
// }
@@ -198,7 +198,7 @@ func (*ServerAuthVNC) SubType() SecuritySubType {
const AUTH_FAIL = "Authentication Failure"
func (auth *ServerAuthVNC) Auth(c common.ServerConn) error {
func (auth *ServerAuthVNC) Auth(c common.IServerConn) error {
buf := make([]byte, 8+len([]byte(AUTH_FAIL)))
rand.Read(buf[:16]) // Random 16 bytes in buf
sndsz, err := c.Write(buf[:16])
@@ -290,7 +290,7 @@ func fixDesKey(key string) []byte {
// return SecSubTypeUnknown
// }
// func (auth *ClientAuthVNC) Auth(c common.ServerConn) error {
// func (auth *ClientAuthVNC) Auth(c common.IServerConn) error {
// if len(auth.Password) == 0 {
// return fmt.Errorf("Security Handshake failed; no password provided for VNCAuth.")
// }

View File

@@ -18,15 +18,14 @@ type ServerConn struct {
// 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.
colorMap *common.ColorMap
// Name associated with the desktop, sent from the server.
desktopName string
// Encodings supported by the client. This should not be modified
// directly. Instead, SetEncodings() should be used.
encodings []common.Encoding
// directly. Instead, MsgSetEncodings() should be used.
encodings []common.IEncoding
// Height of the frame buffer in pixels, sent to the client.
fbHeight uint16
@@ -47,7 +46,7 @@ type ServerConn struct {
quit chan struct{}
}
// func (c *ServerConn) UnreadByte() error {
// func (c *IServerConn) UnreadByte() error {
// return c.br.UnreadByte()
// }
@@ -79,7 +78,7 @@ func (c *ServerConn) Conn() io.ReadWriter {
}
func (c *ServerConn) SetEncodings(encs []common.EncodingType) error {
encodings := make(map[int32]common.Encoding)
encodings := make(map[int32]common.IEncoding)
for _, enc := range c.cfg.Encodings {
encodings[enc.Type()] = enc
}
@@ -129,7 +128,7 @@ func (c *ServerConn) SetPixelFormat(pf *common.PixelFormat) error {
c.pixelFormat = pf
return nil
}
func (c *ServerConn) Encodings() []common.Encoding {
func (c *ServerConn) Encodings() []common.IEncoding {
return c.encodings
}
func (c *ServerConn) Width() uint16 {
@@ -169,12 +168,12 @@ func (c *ServerConn) handle() error {
default:
var messageType common.ClientMessageType
if err := binary.Read(c, binary.BigEndian, &messageType); err != nil {
logger.Errorf("ServerConn.handle error: %v", err)
logger.Errorf("IServerConn.handle error: %v", err)
return err
}
msg, ok := clientMessages[messageType]
if !ok {
return fmt.Errorf("ServerConn.Handle: unsupported message-type: %v", messageType)
return fmt.Errorf("IServerConn.Handle: unsupported message-type: %v", messageType)
}
parsedMsg, err := msg.Read(c)
@@ -184,7 +183,7 @@ func (c *ServerConn) handle() error {
case common.SetPixelFormatMsgType:
// update pixel format
logger.Debugf("ClientUpdater.Consume: updating pixel format")
pixFmtMsg := parsedMsg.(*SetPixelFormat)
pixFmtMsg := parsedMsg.(*MsgSetPixelFormat)
c.SetPixelFormat(&pixFmtMsg.PF)
if pixFmtMsg.PF.TrueColor != 0 {
c.SetColorMap(&common.ColorMap{})
@@ -197,7 +196,7 @@ func (c *ServerConn) handle() error {
return err
}
logger.Infof("ServerConn.Handle got ClientMessage: %s, %v", parsedMsg.Type(), parsedMsg)
logger.Infof("IServerConn.Handle got ClientMessage: %s, %v", parsedMsg.Type(), parsedMsg)
//TODO: treat set encodings by allowing only supported encoding in proxy configurations
//// if parsedMsg.Type() == common.SetEncodingsMsgType{
//// c.cfg.Encodings
@@ -209,7 +208,7 @@ func (c *ServerConn) handle() error {
}
err = c.Listeners.Consume(seg)
if err != nil {
logger.Errorf("ServerConn.Handle: listener consume err %s", err.Error())
logger.Errorf("IServerConn.Handle: listener consume err %s", err.Error())
return err
}
}

View File

@@ -9,12 +9,12 @@ import (
)
var DefaultClientMessages = []common.ClientMessage{
&SetPixelFormat{},
&SetEncodings{},
&FramebufferUpdateRequest{},
&KeyEvent{},
&PointerEvent{},
&ClientCutText{},
&MsgSetPixelFormat{},
&MsgSetEncodings{},
&MsgFramebufferUpdateRequest{},
&MsgKeyEvent{},
&MsgPointerEvent{},
&MsgClientCutText{},
}
// FramebufferUpdate holds a FramebufferUpdate wire format message.
@@ -28,7 +28,7 @@ type ServerHandler func(*ServerConfig, *ServerConn) error
type ServerConfig struct {
SecurityHandlers []SecurityHandler
Encodings []common.Encoding
Encodings []common.IEncoding
PixelFormat *common.PixelFormat
ColorMap *common.ColorMap
ClientMessages []common.ClientMessage

View File

@@ -20,13 +20,13 @@ func TestServer(t *testing.T) {
cfg := &ServerConfig{
//SecurityHandlers: []SecurityHandler{&ServerAuthNone{}, &ServerAuthVNC{}},
SecurityHandlers: []SecurityHandler{&ServerAuthVNC{"Ch_#!T@8"}},
Encodings: []common.Encoding{&encodings.RawEncoding{}, &encodings.TightEncoding{}, &encodings.CopyRectEncoding{}},
Encodings: []common.IEncoding{&encodings.RawEncoding{}, &encodings.TightEncoding{}, &encodings.CopyRectEncoding{}},
PixelFormat: common.NewPixelFormat(32),
ClientMessages: DefaultClientMessages,
DesktopName: []byte("workDesk"),
Height: uint16(768),
Width: uint16(1024),
NewConnHandler: newServerConnHandler,
ClientMessages: DefaultClientMessages,
DesktopName: []byte("workDesk"),
Height: uint16(768),
Width: uint16(1024),
NewConnHandler: newServerConnHandler,
}
url := "http://localhost:8091/"
go WsServe(url, cfg)

View File

@@ -15,17 +15,9 @@ type WsServer struct {
type WsHandler func(io.ReadWriter, *ServerConfig, string)
// func checkOrigin(config *websocket.Config, req *http.Request) (err error) {
// config.Origin, err = websocket.Origin(config, req)
// if err == nil && config.Origin == nil {
// return fmt.Errorf("null origin")
// }
// return err
// }
// This example demonstrates a trivial echo server.
func (wsServer *WsServer) Listen(urlStr string, handlerFunc WsHandler) {
//http.Handle("/", websocket.Handler(EchoHandler))
if urlStr == "" {
urlStr = "/"
}
@@ -34,17 +26,6 @@ func (wsServer *WsServer) Listen(urlStr string, handlerFunc WsHandler) {
logger.Errorf("error while parsing url: ", err)
}
// http.HandleFunc(url.Path,
// func(w http.ResponseWriter, req *http.Request) {
// sessionId := req.URL.Query().Get("sessionId")
// s := websocket.Server{Handshake: checkOrigin, Handler: websocket.Handler(
// func(ws *websocket.ServerConn) {
// ws.PayloadType = websocket.BinaryFrame
// handlerFunc(ws, wsServer.cfg, sessionId)
// })}
// s.ServeHTTP(w, req)
// })
http.Handle(url.Path, websocket.Handler(
func(ws *websocket.Conn) {
path := ws.Request().URL.Path

View File

@@ -1 +0,0 @@
package server