diff --git a/.idea/misc.xml b/.idea/misc.xml index ce2a63a..8aa70d1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,30 +1,7 @@ - - - - - - - - 1.8 - - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 14ccde0..0648d57 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,30 +2,12 @@ - - - - - - - - - - - - - - - - + - + + + - - - - - + + @@ -53,100 +38,97 @@ - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -155,8 +137,8 @@ - - + + @@ -173,19 +155,6 @@ - r - rfhc - rfh - rfher - rfhera - rfherad - rferad - rfberad - rfbrad - rfbread - rfbreadh - rfbreadhel - rfbreadhelper rfbreadhelper{ mainLoop NewServerConn @@ -203,6 +172,19 @@ c. upgrade read + EncodingType + loop + BellMessage + ServerCutTextMessage + SetEncodings + enc + fra + FramebufferUpdate + e + encodin + encoding + tight + conn. @@ -216,11 +198,9 @@ @@ -240,6 +224,9 @@ true DEFINITION_ORDER + + + @@ -269,15 +278,12 @@ - + - - - @@ -289,10 +295,6 @@ @@ -304,11 +306,11 @@ - - @@ -322,46 +324,18 @@ + + + + + + + + + + + + + + + + + + + + - + @@ -439,7 +457,7 @@ - + @@ -913,6 +931,8 @@ + + @@ -949,7 +969,7 @@ - @@ -964,41 +984,68 @@ - - - - - - - - - + + + - - + - - - - - + + + + + + + + + + + + + + + + - - - - + + + - - + - + + + @@ -1041,7 +1088,7 @@ file://$PROJECT_DIR$/common/encoding.go - 164 + 162 @@ -1124,13 +1171,39 @@ 211 + + file://$PROJECT_DIR$/tee-listeners/recorder.go + 115 + + + file://$PROJECT_DIR$/tee-listeners/recorder.go + 22 + + + file://$PROJECT_DIR$/client/client-conn.go + 450 + + + file://$PROJECT_DIR$/tee-listeners/recorder.go + 27 + - \ No newline at end of file diff --git a/client/client-conn.go b/client/client-conn.go index ec8b75e..522d554 100644 --- a/client/client-conn.go +++ b/client/client-conn.go @@ -8,7 +8,6 @@ import ( "net" "unicode" "vncproxy/common" - "vncproxy/tee-listeners" ) // A ServerMessage implements a message sent from the server to the client. @@ -21,11 +20,11 @@ type ClientAuth interface { // Handshake is called when the authentication handshake should be // performed, as part of the general RFB handshake. (see 7.2.1) - Handshake(net.Conn) error + Handshake(io.ReadWriteCloser) error } type ClientConn struct { - conn net.Conn + conn io.ReadWriteCloser //c net.Conn config *ClientConfig @@ -77,6 +76,7 @@ type ClientConfig struct { // This only needs to contain NEW server messages, and doesn't // need to explicitly contain the RFC-required messages. ServerMessages []common.ServerMessage + Listener common.SegmentConsumer } func Client(c net.Conn, cfg *ClientConfig) (*ClientConn, error) { @@ -438,6 +438,15 @@ FindAuth: } c.DesktopName = string(nameBytes) + srvInit := common.ServerInit{ + NameLength: nameLength, + NameText: nameBytes, + FBHeight: c.FrameBufferHeight, + FBWidth: c.FrameBufferWidth, + PixelFormat: c.PixelFormat, + } + rfbSeg := &common.RfbSegment{SegmentType: common.SegmentServerInitMessage, Message: &srvInit} + c.config.Listener.Consume(rfbSeg) return nil } @@ -446,9 +455,8 @@ FindAuth: // proper channels for users of the client to read. func (c *ClientConn) mainLoop() { defer c.Close() - rec := listeners.NewRecorder("/Users/amitbet/recording.rbs", c.DesktopName, c.FrameBufferWidth, c.FrameBufferHeight) - reader := &common.RfbReadHelper{Reader: c.conn, Listener: rec} + reader := &common.RfbReadHelper{Reader: c.conn, Listener: c.config.Listener} // Build the map of available server messages typeMap := make(map[uint8]common.ServerMessage) diff --git a/client/client_auth.go b/client/client_auth.go index 8a59857..7af6c06 100644 --- a/client/client_auth.go +++ b/client/client_auth.go @@ -1,13 +1,11 @@ package client import ( - "net" - "crypto/des" "encoding/binary" + "io" ) - // ClientAuthNone is the "none" authentication. See 7.2.1 type ClientAuthNone byte @@ -15,7 +13,7 @@ func (*ClientAuthNone) SecurityType() uint8 { return 1 } -func (*ClientAuthNone) Handshake(net.Conn) error { +func (*ClientAuthNone) Handshake(closer io.ReadWriteCloser) error { return nil } @@ -28,7 +26,7 @@ func (p *PasswordAuth) SecurityType() uint8 { return 2 } -func (p *PasswordAuth) Handshake(c net.Conn) error { +func (p *PasswordAuth) Handshake(c io.ReadWriteCloser) error { randomValue := make([]uint8, 16) if err := binary.Read(c, binary.BigEndian, &randomValue); err != nil { return err @@ -36,7 +34,7 @@ func (p *PasswordAuth) Handshake(c net.Conn) error { crypted, err := p.encrypt(p.Password, randomValue) - if (err != nil) { + if err != nil { return err } @@ -87,7 +85,7 @@ func (p *PasswordAuth) reverseBits(b byte) byte { } func (p *PasswordAuth) encrypt(key string, bytes []byte) ([]byte, error) { - keyBytes := []byte{0,0,0,0,0,0,0,0} + keyBytes := []byte{0, 0, 0, 0, 0, 0, 0, 0} if len(key) > 8 { key = key[:8] diff --git a/common/readers.go b/common/readers.go index fc1d4b6..3126b6c 100644 --- a/common/readers.go +++ b/common/readers.go @@ -12,6 +12,9 @@ const ( SegmentBytes SegmentType = iota SegmentMessageSeparator SegmentRectSeparator + SegmentFullyParsedClientMessage + SegmentFullyParsedServerMessage + SegmentServerInitMessage ) type SegmentType int @@ -20,7 +23,9 @@ type RfbSegment struct { Bytes []byte SegmentType SegmentType UpcomingObjectType int + Message interface{} } + type SegmentConsumer interface { Consume(*RfbSegment) error } diff --git a/common/server-message.go b/common/server-message.go index d82d298..0afa0c2 100644 --- a/common/server-message.go +++ b/common/server-message.go @@ -23,3 +23,10 @@ const ( Bell ServerCutText ) + +type ServerInit struct { + FBWidth, FBHeight uint16 + PixelFormat PixelFormat + NameLength uint32 + NameText []byte +} diff --git a/encodings/enc-tight.go b/encodings/enc-tight.go index 9e11ce4..a708fb9 100644 --- a/encodings/enc-tight.go +++ b/encodings/enc-tight.go @@ -1,327 +1,325 @@ -package encodings - -import ( - "errors" - "fmt" - "vncproxy/common" -) - -var TightMinToCompress int = 12 - -const ( - TightExplicitFilter = 0x04 - TightFill = 0x08 - TightJpeg = 0x09 - TightPNG = 0x10 - - TightFilterCopy = 0x00 - TightFilterPalette = 0x01 - TightFilterGradient = 0x02 -) - -type TightEncoding struct { - //output io.Writer - logger common.Logger -} - -// func (t *TightEncoding) SetOutput(output io.Writer) { -// t.output = output -// } - -func (*TightEncoding) Type() int32 { - return 7 -} - -// func ReadAndRecBytes(conn io.Reader, rec io.Writer, count int) ([]byte, error) { -// buf, err := readBytes(conn, count) -// rec.Write(buf) -// return buf, err -// } -// func ReadAndRecUint8(conn io.Reader, rec io.Writer) (uint8, error) { -// myUint, err := readUint8(conn) -// buf := make([]byte, 1) -// buf[0] = byte(myUint) // cast int8 to byte -// rec.Write(buf) -// return myUint, err -// } - -// func ReadAndRecUint16(conn io.Reader, rec io.Writer) (uint16, error) { -// myUint, err := readUint16(conn) -// buf := make([]byte, 2) -// //buf[0] = byte(myUint) // cast int8 to byte -// //var i int16 = 41 -// //b := make([]byte, 2) -// binary.LittleEndian.PutUint16(buf, uint16(myUint)) - -// rec.Write(buf) -// return myUint, err -// } - -func calcTightBytePerPixel(pf *common.PixelFormat) int { - bytesPerPixel := int(pf.BPP / 8) - - var bytesPerPixelTight int - if 24 == pf.Depth && 32 == pf.BPP { - bytesPerPixelTight = 3 - } else { - bytesPerPixelTight = bytesPerPixel - } - return bytesPerPixelTight -} - -func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) { - bytesPixel := calcTightBytePerPixel(pixelFmt) - //conn := common.RfbReadHelper{Reader:reader} - //conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat} - - //var subencoding uint8 - compctl, err := r.ReadUint8() - if err != nil { - fmt.Printf("error in handling tight encoding: %v\n", err) - return nil, err - } - fmt.Printf("bytesPixel= %d, subencoding= %d\n", bytesPixel, compctl) - - //move it to position (remove zlib flush commands) - compType := compctl >> 4 & 0x0F - - fmt.Printf("afterSHL:%d\n", compType) - switch compType { - case TightFill: - fmt.Printf("reading fill size=%d\n", bytesPixel) - //read color - r.ReadBytes(int(bytesPixel)) - //byt, _ := r.ReadBytes(3) - //fmt.Printf(">>>>>>>>>TightFillBytes=%v", byt) - return t, nil - case TightJpeg: - if pixelFmt.BPP == 8 { - return nil, errors.New("Tight encoding: JPEG is not supported in 8 bpp mode") - } - - len, err := r.ReadCompactLen() - if err != nil { - return nil, err - } - fmt.Printf("reading jpeg size=%d\n", len) - r.ReadBytes(len) - return t, nil - default: - - if compType > TightJpeg { - fmt.Println("Compression control byte is incorrect!") - } - - handleTightFilters(compctl, pixelFmt, rect, r) - return t, nil - } -} - -func handleTightFilters(subencoding uint8, pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) { - //conn := common.RfbReadHelper{Reader:reader} - var FILTER_ID_MASK uint8 = 0x40 - //var STREAM_ID_MASK uint8 = 0x30 - - //decoderId := (subencoding & STREAM_ID_MASK) >> 4 - var filterid uint8 - var err error - - if (subencoding & FILTER_ID_MASK) > 0 { // filter byte presence - filterid, err = r.ReadUint8() - if err != nil { - fmt.Printf("error in handling tight encoding, reading filterid: %v\n", err) - return - } - fmt.Printf("read filter: %d\n", filterid) - } - - //var numColors uint8 - bytesPixel := calcTightBytePerPixel(pixelFmt) - - fmt.Printf("filter: %d\n", filterid) - // if rfb.rec != null { - // rfb.rec.writeByte(filter_id) - // } - lengthCurrentbpp := int(bytesPixel) * int(rect.Width) * int(rect.Height) - - switch filterid { - case TightFilterPalette: //PALETTE_FILTER - - colorCount, err := r.ReadUint8() - paletteSize := colorCount + 1 // add one more - fmt.Printf("----PALETTE_FILTER: paletteSize=%d bytesPixel=%d\n", paletteSize, bytesPixel) - //complete palette - r.ReadBytes(int(paletteSize) * bytesPixel) - - var dataLength int - if paletteSize == 2 { - dataLength = int(rect.Height) * ((int(rect.Width) + 7) / 8) - } else { - dataLength = int(rect.Width * rect.Height) - } - _, err = r.ReadTightData(dataLength) - if err != nil { - fmt.Printf("error in handling tight encoding, Reading Palette: %v\n", err) - return - } - case TightFilterGradient: //GRADIENT_FILTER - fmt.Printf("----GRADIENT_FILTER: bytesPixel=%d\n", bytesPixel) - //useGradient = true - fmt.Printf("usegrad: %d\n", filterid) - r.ReadTightData(lengthCurrentbpp) - case TightFilterCopy: //BASIC_FILTER - fmt.Printf("----BASIC_FILTER: bytesPixel=%d\n", bytesPixel) - r.ReadTightData(lengthCurrentbpp) - default: - fmt.Printf("Bad tight filter id: %d\n", filterid) - return - } - - //////////// - - // if numColors == 0 && bytesPixel == 4 { - // rowSize1 *= 3 - // } - // rowSize := (int(rect.Width)*bitsPixel + 7) / 8 - // dataSize := int(rect.Height) * rowSize - - // dataSize1 := rect.Height * rowSize1 - // fmt.Printf("datasize: %d, origDatasize: %d", dataSize, dataSize1) - // // Read, optionally uncompress and decode data. - // if int(dataSize1) < TightMinToCompress { - // // Data size is small - not compressed with zlib. - // if numColors != 0 { - // // Indexed colors. - // //indexedData := make([]byte, dataSize) - // readBytes(conn.c, int(dataSize1)) - // //readFully(indexedData); - // // if (rfb.rec != null) { - // // rfb.rec.write(indexedData); - // // } - // // if (numColors == 2) { - // // // Two colors. - // // if (bytesPixel == 1) { - // // decodeMonoData(x, y, w, h, indexedData, palette8); - // // } else { - // // decodeMonoData(x, y, w, h, indexedData, palette24); - // // } - // // } else { - // // // 3..255 colors (assuming bytesPixel == 4). - // // int i = 0; - // // for (int dy = y; dy < y + h; dy++) { - // // for (int dx = x; dx < x + w; dx++) { - // // pixels24[dy * rfb.framebufferWidth + dx] = palette24[indexedData[i++] & 0xFF]; - // // } - // // } - // // } - // } else if useGradient { - // // "Gradient"-processed data - // //buf := make ( []byte,w * h * 3); - // dataByteCount := int(3) * int(rect.Width) * int(rect.Height) - // readBytes(conn.c, dataByteCount) - // // rfb.readFully(buf); - // // if (rfb.rec != null) { - // // rfb.rec.write(buf); - // // } - // // decodeGradientData(x, y, w, h, buf); - // } else { - // // Raw truecolor data. - // dataByteCount := int(bytesPixel) * int(rect.Width) * int(rect.Height) - // readBytes(conn.c, dataByteCount) - - // // if (bytesPixel == 1) { - // // for (int dy = y; dy < y + h; dy++) { - - // // rfb.readFully(pixels8, dy * rfb.framebufferWidth + x, w); - // // if (rfb.rec != null) { - // // rfb.rec.write(pixels8, dy * rfb.framebufferWidth + x, w); - // // } - // // } - // // } else { - // // byte[] buf = new byte[w * 3]; - // // int i, offset; - // // for (int dy = y; dy < y + h; dy++) { - // // rfb.readFully(buf); - // // if (rfb.rec != null) { - // // rfb.rec.write(buf); - // // } - // // offset = dy * rfb.framebufferWidth + x; - // // for (i = 0; i < w; i++) { - // // pixels24[offset + i] = (buf[i * 3] & 0xFF) << 16 | (buf[i * 3 + 1] & 0xFF) << 8 | (buf[i * 3 + 2] & 0xFF); - // // } - // // } - // // } - // } - // } else { - // // Data was compressed with zlib. - // zlibDataLen, err := readCompactLen(conn.c) - // fmt.Printf("compactlen=%d\n", zlibDataLen) - // if err != nil { - // return nil, err - // } - // //byte[] zlibData = new byte[zlibDataLen]; - // //rfb.readFully(zlibData); - // readBytes(conn.c, zlibDataLen) - - // // if (rfb.rec != null) { - // // rfb.rec.write(zlibData); - // // } - // // int stream_id = comp_ctl & 0x03; - // // if (tightInflaters[stream_id] == null) { - // // tightInflaters[stream_id] = new Inflater(); - // // } - // // Inflater myInflater = tightInflaters[stream_id]; - // // myInflater.setInput(zlibData); - // // byte[] buf = new byte[dataSize]; - // // myInflater.inflate(buf); - // // if (rfb.rec != null && !rfb.recordFromBeginning) { - // // rfb.recordCompressedData(buf); - // // } - - // // if (numColors != 0) { - // // // Indexed colors. - // // if (numColors == 2) { - // // // Two colors. - // // if (bytesPixel == 1) { - // // decodeMonoData(x, y, w, h, buf, palette8); - // // } else { - // // decodeMonoData(x, y, w, h, buf, palette24); - // // } - // // } else { - // // // More than two colors (assuming bytesPixel == 4). - // // int i = 0; - // // for (int dy = y; dy < y + h; dy++) { - // // for (int dx = x; dx < x + w; dx++) { - // // pixels24[dy * rfb.framebufferWidth + dx] = palette24[buf[i++] & 0xFF]; - // // } - // // } - // // } - // // } else if (useGradient) { - // // // Compressed "Gradient"-filtered data (assuming bytesPixel == 4). - // // decodeGradientData(x, y, w, h, buf); - // // } else { - // // // Compressed truecolor data. - // // if (bytesPixel == 1) { - // // int destOffset = y * rfb.framebufferWidth + x; - // // for (int dy = 0; dy < h; dy++) { - // // System.arraycopy(buf, dy * w, pixels8, destOffset, w); - // // destOffset += rfb.framebufferWidth; - // // } - // // } else { - // // int srcOffset = 0; - // // int destOffset, i; - // // for (int dy = 0; dy < h; dy++) { - // // myInflater.inflate(buf); - // // destOffset = (y + dy) * rfb.framebufferWidth + x; - // // for (i = 0; i < w; i++) { - // // pixels24[destOffset + i] = (buf[srcOffset] & 0xFF) << 16 | (buf[srcOffset + 1] & 0xFF) << 8 - // // | (buf[srcOffset + 2] & 0xFF); - // // srcOffset += 3; - // // } - // // } - // // } - // // } - // } - - return -} +package encodings + +import ( + "errors" + "fmt" + "vncproxy/common" +) + +var TightMinToCompress int = 12 + +const ( + TightExplicitFilter = 0x04 + TightFill = 0x08 + TightJpeg = 0x09 + TightPNG = 0x10 + + TightFilterCopy = 0x00 + TightFilterPalette = 0x01 + TightFilterGradient = 0x02 +) + +type TightEncoding struct { + //output io.Writer + logger common.Logger +} + +// func (t *TightEncoding) SetOutput(output io.Writer) { +// t.output = output +// } + +func (*TightEncoding) Type() int32 { return int32(common.EncTight) } + +// func ReadAndRecBytes(conn io.Reader, rec io.Writer, count int) ([]byte, error) { +// buf, err := readBytes(conn, count) +// rec.Write(buf) +// return buf, err +// } +// func ReadAndRecUint8(conn io.Reader, rec io.Writer) (uint8, error) { +// myUint, err := readUint8(conn) +// buf := make([]byte, 1) +// buf[0] = byte(myUint) // cast int8 to byte +// rec.Write(buf) +// return myUint, err +// } + +// func ReadAndRecUint16(conn io.Reader, rec io.Writer) (uint16, error) { +// myUint, err := readUint16(conn) +// buf := make([]byte, 2) +// //buf[0] = byte(myUint) // cast int8 to byte +// //var i int16 = 41 +// //b := make([]byte, 2) +// binary.LittleEndian.PutUint16(buf, uint16(myUint)) + +// rec.Write(buf) +// return myUint, err +// } + +func calcTightBytePerPixel(pf *common.PixelFormat) int { + bytesPerPixel := int(pf.BPP / 8) + + var bytesPerPixelTight int + if 24 == pf.Depth && 32 == pf.BPP { + bytesPerPixelTight = 3 + } else { + bytesPerPixelTight = bytesPerPixel + } + return bytesPerPixelTight +} + +func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) { + bytesPixel := calcTightBytePerPixel(pixelFmt) + //conn := common.RfbReadHelper{Reader:reader} + //conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat} + + //var subencoding uint8 + compctl, err := r.ReadUint8() + if err != nil { + fmt.Printf("error in handling tight encoding: %v\n", err) + return nil, err + } + fmt.Printf("bytesPixel= %d, subencoding= %d\n", bytesPixel, compctl) + + //move it to position (remove zlib flush commands) + compType := compctl >> 4 & 0x0F + + fmt.Printf("afterSHL:%d\n", compType) + switch compType { + case TightFill: + fmt.Printf("reading fill size=%d\n", bytesPixel) + //read color + r.ReadBytes(int(bytesPixel)) + //byt, _ := r.ReadBytes(3) + //fmt.Printf(">>>>>>>>>TightFillBytes=%v", byt) + return t, nil + case TightJpeg: + if pixelFmt.BPP == 8 { + return nil, errors.New("Tight encoding: JPEG is not supported in 8 bpp mode") + } + + len, err := r.ReadCompactLen() + if err != nil { + return nil, err + } + fmt.Printf("reading jpeg size=%d\n", len) + r.ReadBytes(len) + return t, nil + default: + + if compType > TightJpeg { + fmt.Println("Compression control byte is incorrect!") + } + + handleTightFilters(compctl, pixelFmt, rect, r) + return t, nil + } +} + +func handleTightFilters(subencoding uint8, pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) { + //conn := common.RfbReadHelper{Reader:reader} + var FILTER_ID_MASK uint8 = 0x40 + //var STREAM_ID_MASK uint8 = 0x30 + + //decoderId := (subencoding & STREAM_ID_MASK) >> 4 + var filterid uint8 + var err error + + if (subencoding & FILTER_ID_MASK) > 0 { // filter byte presence + filterid, err = r.ReadUint8() + if err != nil { + fmt.Printf("error in handling tight encoding, reading filterid: %v\n", err) + return + } + fmt.Printf("read filter: %d\n", filterid) + } + + //var numColors uint8 + bytesPixel := calcTightBytePerPixel(pixelFmt) + + fmt.Printf("filter: %d\n", filterid) + // if rfb.rec != null { + // rfb.rec.writeByte(filter_id) + // } + lengthCurrentbpp := int(bytesPixel) * int(rect.Width) * int(rect.Height) + + switch filterid { + case TightFilterPalette: //PALETTE_FILTER + + colorCount, err := r.ReadUint8() + paletteSize := colorCount + 1 // add one more + fmt.Printf("----PALETTE_FILTER: paletteSize=%d bytesPixel=%d\n", paletteSize, bytesPixel) + //complete palette + r.ReadBytes(int(paletteSize) * bytesPixel) + + var dataLength int + if paletteSize == 2 { + dataLength = int(rect.Height) * ((int(rect.Width) + 7) / 8) + } else { + dataLength = int(rect.Width * rect.Height) + } + _, err = r.ReadTightData(dataLength) + if err != nil { + fmt.Printf("error in handling tight encoding, Reading Palette: %v\n", err) + return + } + case TightFilterGradient: //GRADIENT_FILTER + fmt.Printf("----GRADIENT_FILTER: bytesPixel=%d\n", bytesPixel) + //useGradient = true + fmt.Printf("usegrad: %d\n", filterid) + r.ReadTightData(lengthCurrentbpp) + case TightFilterCopy: //BASIC_FILTER + fmt.Printf("----BASIC_FILTER: bytesPixel=%d\n", bytesPixel) + r.ReadTightData(lengthCurrentbpp) + default: + fmt.Printf("Bad tight filter id: %d\n", filterid) + return + } + + //////////// + + // if numColors == 0 && bytesPixel == 4 { + // rowSize1 *= 3 + // } + // rowSize := (int(rect.Width)*bitsPixel + 7) / 8 + // dataSize := int(rect.Height) * rowSize + + // dataSize1 := rect.Height * rowSize1 + // fmt.Printf("datasize: %d, origDatasize: %d", dataSize, dataSize1) + // // Read, optionally uncompress and decode data. + // if int(dataSize1) < TightMinToCompress { + // // Data size is small - not compressed with zlib. + // if numColors != 0 { + // // Indexed colors. + // //indexedData := make([]byte, dataSize) + // readBytes(conn.c, int(dataSize1)) + // //readFully(indexedData); + // // if (rfb.rec != null) { + // // rfb.rec.write(indexedData); + // // } + // // if (numColors == 2) { + // // // Two colors. + // // if (bytesPixel == 1) { + // // decodeMonoData(x, y, w, h, indexedData, palette8); + // // } else { + // // decodeMonoData(x, y, w, h, indexedData, palette24); + // // } + // // } else { + // // // 3..255 colors (assuming bytesPixel == 4). + // // int i = 0; + // // for (int dy = y; dy < y + h; dy++) { + // // for (int dx = x; dx < x + w; dx++) { + // // pixels24[dy * rfb.framebufferWidth + dx] = palette24[indexedData[i++] & 0xFF]; + // // } + // // } + // // } + // } else if useGradient { + // // "Gradient"-processed data + // //buf := make ( []byte,w * h * 3); + // dataByteCount := int(3) * int(rect.Width) * int(rect.Height) + // readBytes(conn.c, dataByteCount) + // // rfb.readFully(buf); + // // if (rfb.rec != null) { + // // rfb.rec.write(buf); + // // } + // // decodeGradientData(x, y, w, h, buf); + // } else { + // // Raw truecolor data. + // dataByteCount := int(bytesPixel) * int(rect.Width) * int(rect.Height) + // readBytes(conn.c, dataByteCount) + + // // if (bytesPixel == 1) { + // // for (int dy = y; dy < y + h; dy++) { + + // // rfb.readFully(pixels8, dy * rfb.framebufferWidth + x, w); + // // if (rfb.rec != null) { + // // rfb.rec.write(pixels8, dy * rfb.framebufferWidth + x, w); + // // } + // // } + // // } else { + // // byte[] buf = new byte[w * 3]; + // // int i, offset; + // // for (int dy = y; dy < y + h; dy++) { + // // rfb.readFully(buf); + // // if (rfb.rec != null) { + // // rfb.rec.write(buf); + // // } + // // offset = dy * rfb.framebufferWidth + x; + // // for (i = 0; i < w; i++) { + // // pixels24[offset + i] = (buf[i * 3] & 0xFF) << 16 | (buf[i * 3 + 1] & 0xFF) << 8 | (buf[i * 3 + 2] & 0xFF); + // // } + // // } + // // } + // } + // } else { + // // Data was compressed with zlib. + // zlibDataLen, err := readCompactLen(conn.c) + // fmt.Printf("compactlen=%d\n", zlibDataLen) + // if err != nil { + // return nil, err + // } + // //byte[] zlibData = new byte[zlibDataLen]; + // //rfb.readFully(zlibData); + // readBytes(conn.c, zlibDataLen) + + // // if (rfb.rec != null) { + // // rfb.rec.write(zlibData); + // // } + // // int stream_id = comp_ctl & 0x03; + // // if (tightInflaters[stream_id] == null) { + // // tightInflaters[stream_id] = new Inflater(); + // // } + // // Inflater myInflater = tightInflaters[stream_id]; + // // myInflater.setInput(zlibData); + // // byte[] buf = new byte[dataSize]; + // // myInflater.inflate(buf); + // // if (rfb.rec != null && !rfb.recordFromBeginning) { + // // rfb.recordCompressedData(buf); + // // } + + // // if (numColors != 0) { + // // // Indexed colors. + // // if (numColors == 2) { + // // // Two colors. + // // if (bytesPixel == 1) { + // // decodeMonoData(x, y, w, h, buf, palette8); + // // } else { + // // decodeMonoData(x, y, w, h, buf, palette24); + // // } + // // } else { + // // // More than two colors (assuming bytesPixel == 4). + // // int i = 0; + // // for (int dy = y; dy < y + h; dy++) { + // // for (int dx = x; dx < x + w; dx++) { + // // pixels24[dy * rfb.framebufferWidth + dx] = palette24[buf[i++] & 0xFF]; + // // } + // // } + // // } + // // } else if (useGradient) { + // // // Compressed "Gradient"-filtered data (assuming bytesPixel == 4). + // // decodeGradientData(x, y, w, h, buf); + // // } else { + // // // Compressed truecolor data. + // // if (bytesPixel == 1) { + // // int destOffset = y * rfb.framebufferWidth + x; + // // for (int dy = 0; dy < h; dy++) { + // // System.arraycopy(buf, dy * w, pixels8, destOffset, w); + // // destOffset += rfb.framebufferWidth; + // // } + // // } else { + // // int srcOffset = 0; + // // int destOffset, i; + // // for (int dy = 0; dy < h; dy++) { + // // myInflater.inflate(buf); + // // destOffset = (y + dy) * rfb.framebufferWidth + x; + // // for (i = 0; i < w; i++) { + // // pixels24[destOffset + i] = (buf[srcOffset] & 0xFF) << 16 | (buf[srcOffset + 1] & 0xFF) << 8 + // // | (buf[srcOffset + 2] & 0xFF); + // // srcOffset += 3; + // // } + // // } + // // } + // // } + // } + + return +} diff --git a/main.go b/main.go index ef10d3b..459de17 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,10 @@ import ( "fmt" "net" "time" + "vncproxy/client" "vncproxy/common" "vncproxy/encodings" - "vncproxy/client" + listeners "vncproxy/tee-listeners" ) func main() { @@ -21,11 +22,18 @@ func main() { authArr := []client.ClientAuth{&client.PasswordAuth{Password: "Ch_#!T@8"}, &noauth} vncSrvMessagesChan := make(chan common.ServerMessage) + + rec := listeners.NewRecorder("c:/Users/betzalel/recording.rbs") + + split := &listeners.MultiListener{} + split.AddListener(rec) + clientConn, err := client.Client(nc, &client.ClientConfig{ Auth: authArr, ServerMessageCh: vncSrvMessagesChan, Exclusive: true, + Listener: split, }) if err != nil { @@ -37,6 +45,7 @@ func main() { // } tight := encodings.TightEncoding{} + tightPng := encodings.TightPngEncoding{} //rre := encodings.RREEncoding{} //zlib := encodings.ZLibEncoding{} //zrle := encodings.ZRLEEncoding{} @@ -47,7 +56,7 @@ func main() { // defer file.Close() //tight.SetOutput(file) - clientConn.SetEncodings([]common.Encoding{&cpyRect, &tight}) + clientConn.SetEncodings([]common.Encoding{&cpyRect, &tightPng, &tight}) go func() { for { diff --git a/server/handlers.go b/server/handlers.go index 0423cb4..fa2e0ec 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -3,6 +3,7 @@ package server import ( "encoding/binary" "fmt" + "vncproxy/common" "io" ) @@ -126,7 +127,7 @@ func ServerSecurityHandler(cfg *ServerConfig, c *ServerConn) error { } func ServerServerInitHandler(cfg *ServerConfig, c *ServerConn) error { - srvInit := &ServerInit{ + srvInit := &common.ServerInit{ FBWidth: c.Width(), FBHeight: c.Height(), PixelFormat: *c.PixelFormat(), diff --git a/server/server.go b/server/server.go index bf0d617..bd7087f 100644 --- a/server/server.go +++ b/server/server.go @@ -19,12 +19,7 @@ var DefaultClientMessages = []common.ClientMessage{ &ClientCutText{}, } -type ServerInit struct { - FBWidth, FBHeight uint16 - PixelFormat common.PixelFormat - NameLength uint32 - NameText []byte -} + //var _ Conn = (*ServerConn)(nil) diff --git a/tee-listeners/recorder.go b/tee-listeners/recorder.go index 9d3fbd0..a10b93b 100644 --- a/tee-listeners/recorder.go +++ b/tee-listeners/recorder.go @@ -8,22 +8,25 @@ import ( "os" "time" "vncproxy/common" + "vncproxy/server" ) type Recorder struct { //common.BytesListener - RBSFileName string - writer *os.File - logger common.Logger - startTime int - buffer bytes.Buffer + RBSFileName string + writer *os.File + logger common.Logger + startTime int + buffer bytes.Buffer + serverInitMessage *common.ServerInit + sessionStartWritten bool } func getNowMillisec() int { return int(time.Now().UnixNano() / int64(time.Millisecond)) } -func NewRecorder(saveFilePath string, desktopName string, fbWidth uint16, fbHeight uint16) *Recorder { +func NewRecorder(saveFilePath string) *Recorder { //delete file if it exists if _, err := os.Stat(saveFilePath); err == nil { os.Remove(saveFilePath) @@ -33,16 +36,25 @@ func NewRecorder(saveFilePath string, desktopName string, fbWidth uint16, fbHeig var err error rec.writer, err = os.OpenFile(saveFilePath, os.O_RDWR|os.O_CREATE, 0755) - rec.writeStartSession(desktopName, fbWidth, fbHeight) - if err != nil { fmt.Printf("unable to open file: %s, error: %v", saveFilePath, err) return nil } - return &rec } +// func (rec *Recorder) startSession(desktopName string, fbWidth uint16, fbHeight uint16) error { + +// err := rec.writeStartSession(desktopName, fbWidth, fbHeight) + +// if err != nil { +// fmt.Printf("Recorder was unable to write StartSession to file error: %v", err) +// return nil +// } + +// return nil +// } + const versionMsg_3_3 = "RFB 003.003\n" const versionMsg_3_7 = "RFB 003.007\n" const versionMsg_3_8 = "RFB 003.008\n" @@ -61,7 +73,11 @@ const ( // // df.write("FBS 001.000\n".getBytes()); // } -func (r *Recorder) writeStartSession(desktopName string, framebufferWidth uint16, framebufferHeight uint16) error { +func (r *Recorder) writeStartSession(initMsg *common.ServerInit) error { + + desktopName := string(initMsg.NameText) + framebufferWidth := initMsg.FBWidth + framebufferHeight := initMsg.FBHeight //write rfb header information (the only part done without the [size|data|timestamp] block wrapper) r.buffer.WriteString("FBS 001.000\n") @@ -90,6 +106,9 @@ func (r *Recorder) writeStartSession(desktopName string, framebufferWidth uint16 func (r *Recorder) Consume(data *common.RfbSegment) error { switch data.SegmentType { case common.SegmentMessageSeparator: + if !r.sessionStartWritten { + r.writeStartSession(r.serverInitMessage) + } switch common.ServerMessageType(data.UpcomingObjectType) { case common.FramebufferUpdate: r.writeToDisk() @@ -105,6 +124,18 @@ func (r *Recorder) Consume(data *common.RfbSegment) error { case common.SegmentBytes: _, err := r.buffer.Write(data.Bytes) return err + case common.SegmentServerInitMessage: + r.serverInitMessage = data.Message.(*common.ServerInit) + case common.SegmentFullyParsedClientMessage: + clientMsg := data.Message.(common.ClientMessage) + switch clientMsg.Type() { + + case common.SetPixelFormatMsgType: + clientMsg := data.Message.(*server.SetPixelFormat) + r.serverInitMessage.PixelFormat = clientMsg.PF + default: + return errors.New("unknown client message type:" + string(data.UpcomingObjectType)) + } default: return errors.New("undefined RfbSegment type") diff --git a/tee-listeners/pass-to.go b/tee-listeners/write-to.go similarity index 77% rename from tee-listeners/pass-to.go rename to tee-listeners/write-to.go index 5e6b7ce..9f98c7a 100644 --- a/tee-listeners/pass-to.go +++ b/tee-listeners/write-to.go @@ -6,11 +6,11 @@ import ( "vncproxy/common" ) -type PassListener struct { +type WriteToListener struct { io.Writer } -func (p *PassListener) Consume(seg *common.RfbSegment) error { +func (p *WriteToListener) Consume(seg *common.RfbSegment) error { switch seg.SegmentType { case common.SegmentMessageSeparator: case common.SegmentRectSeparator: