mirror of
https://github.com/amitbet/vncproxy.git
synced 2025-09-09 17:09:55 +00:00
created proxy and listeners to connect recorder and server-client cross writing
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"vncproxy/common"
|
||||
@@ -37,6 +39,11 @@ type ServerConn struct {
|
||||
// SetPixelFormat method.
|
||||
pixelFormat *common.PixelFormat
|
||||
|
||||
// a consumer for the parsed messages, to allow for recording and proxy
|
||||
Listener common.SegmentConsumer
|
||||
|
||||
SessionId string
|
||||
|
||||
quit chan struct{}
|
||||
}
|
||||
|
||||
@@ -44,6 +51,28 @@ type ServerConn struct {
|
||||
// return c.br.UnreadByte()
|
||||
// }
|
||||
|
||||
func NewServerConn(c io.ReadWriter, cfg *ServerConfig) (*ServerConn, error) {
|
||||
// if cfg.ClientMessageCh == nil {
|
||||
// return nil, fmt.Errorf("ClientMessageCh nil")
|
||||
// }
|
||||
|
||||
if len(cfg.ClientMessages) == 0 {
|
||||
return nil, fmt.Errorf("ClientMessage 0")
|
||||
}
|
||||
|
||||
return &ServerConn{
|
||||
c: c,
|
||||
//br: bufio.NewReader(c),
|
||||
//bw: bufio.NewWriter(c),
|
||||
cfg: cfg,
|
||||
quit: make(chan struct{}),
|
||||
encodings: cfg.Encodings,
|
||||
pixelFormat: cfg.PixelFormat,
|
||||
fbWidth: cfg.Width,
|
||||
fbHeight: cfg.Height,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *ServerConn) Conn() io.ReadWriter {
|
||||
return c.c
|
||||
}
|
||||
@@ -72,7 +101,7 @@ func (c *ServerConn) SetProtoVersion(pv string) {
|
||||
// }
|
||||
|
||||
func (c *ServerConn) Close() error {
|
||||
return c.c.(io.ReadWriteCloser).Close()
|
||||
return c.c.(io.ReadWriteCloser).Close()
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -104,7 +133,7 @@ func (c *ServerConn) SetColorMap(cm *common.ColorMap) {
|
||||
func (c *ServerConn) DesktopName() string {
|
||||
return c.desktopName
|
||||
}
|
||||
func (c *ServerConn) PixelFormat() *common.PixelFormat {
|
||||
func (c *ServerConn) CurrentPixelFormat() *common.PixelFormat {
|
||||
return c.pixelFormat
|
||||
}
|
||||
func (c *ServerConn) SetDesktopName(name string) {
|
||||
@@ -134,3 +163,71 @@ func (c *ServerConn) SetWidth(w uint16) {
|
||||
func (c *ServerConn) SetHeight(h uint16) {
|
||||
c.fbHeight = h
|
||||
}
|
||||
|
||||
func (c *ServerConn) handle() error {
|
||||
//var err error
|
||||
//var wg sync.WaitGroup
|
||||
|
||||
//defer c.Close()
|
||||
|
||||
//create a map of all message types
|
||||
clientMessages := make(map[common.ClientMessageType]common.ClientMessage)
|
||||
for _, m := range c.cfg.ClientMessages {
|
||||
clientMessages[m.Type()] = m
|
||||
}
|
||||
//wg.Add(2)
|
||||
|
||||
// server
|
||||
go func() error {
|
||||
//defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case msg := <-c.cfg.ServerMessageCh:
|
||||
fmt.Printf("%v", msg)
|
||||
// if err = msg.Write(c); err != nil {
|
||||
// return err
|
||||
// }
|
||||
case <-c.quit:
|
||||
c.Close()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// client
|
||||
//go func() error {
|
||||
//defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-c.quit:
|
||||
return nil
|
||||
default:
|
||||
var messageType common.ClientMessageType
|
||||
if err := binary.Read(c, binary.BigEndian, &messageType); err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return err
|
||||
}
|
||||
msg, ok := clientMessages[messageType]
|
||||
if !ok {
|
||||
return fmt.Errorf("unsupported message-type: %v", messageType)
|
||||
|
||||
}
|
||||
parsedMsg, err := msg.Read(c)
|
||||
seg := &common.RfbSegment{
|
||||
SegmentType: common.SegmentFullyParsedClientMessage,
|
||||
Message: parsedMsg,
|
||||
}
|
||||
c.Listener.Consume(seg)
|
||||
if err != nil {
|
||||
fmt.Printf("srv err %s\n", err.Error())
|
||||
return err
|
||||
}
|
||||
fmt.Printf("message:%s, %v\n", parsedMsg.Type(), parsedMsg)
|
||||
//c.cfg.ClientMessageCh <- parsedMsg
|
||||
}
|
||||
}
|
||||
//}()
|
||||
|
||||
//wg.Wait()
|
||||
//return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user