mirror of
https://github.com/amitbet/vncproxy.git
synced 2025-09-23 10:28:54 +00:00
made vnc replay-to-client work!
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package encodings
|
||||
|
||||
import "vncproxy/common"
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
type CopyRectEncoding struct {
|
||||
//Colors []Color
|
||||
@@ -11,6 +15,18 @@ type CopyRectEncoding struct {
|
||||
func (z *CopyRectEncoding) Type() int32 {
|
||||
return 1
|
||||
}
|
||||
func (z *CopyRectEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
binary.Write(w, binary.BigEndian, z.copyRectSrcX)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
binary.Write(w, binary.BigEndian, z.copyRectSrcY)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return 4, nil
|
||||
}
|
||||
|
||||
func (z *CopyRectEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
z.copyRectSrcX, _ = r.ReadUint16()
|
||||
z.copyRectSrcY, _ = r.ReadUint16()
|
||||
|
@@ -1,10 +1,15 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
type CoRREEncoding struct {
|
||||
numSubRects uint32
|
||||
backgroundColor []byte
|
||||
subRectData []byte
|
||||
//Colors []Color
|
||||
}
|
||||
|
||||
@@ -12,35 +17,45 @@ func (z *CoRREEncoding) Type() int32 {
|
||||
return 4
|
||||
}
|
||||
|
||||
func (z *CoRREEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
binary.Write(w, binary.BigEndian, z.numSubRects)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
w.Write(z.backgroundColor)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
w.Write(z.subRectData)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
b := len(z.backgroundColor) + len(z.subRectData) + 4
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (z *CoRREEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
bytesPerPixel := int(pixelFmt.BPP / 8)
|
||||
numOfSubrectangles, _ := r.ReadUint32()
|
||||
|
||||
//read whole rect background color
|
||||
r.ReadBytes(bytesPerPixel)
|
||||
|
||||
//read all individual rects (color=BPP + x=16b + y=16b + w=16b + h=16b)
|
||||
_, err := r.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 4))
|
||||
|
||||
numOfSubrectangles, err := r.ReadUint32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
z.numSubRects = numOfSubrectangles
|
||||
|
||||
//read whole-rect background color
|
||||
z.backgroundColor, err = r.ReadBytes(bytesPerPixel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//read all individual rects (color=BPP + x=16b + y=16b + w=16b + h=16b)
|
||||
z.subRectData, err = r.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 4))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return z, nil
|
||||
|
||||
//int nSubrects = rfb.readU32();
|
||||
|
||||
//byte[] bg_buf = new byte[bytesPerPixel];
|
||||
//rfb.readFully(bytesPerPixel);
|
||||
//Color pixel;
|
||||
// if (bytesPixel == 1) {
|
||||
// pixel = colors[bg_buf[0] & 0xFF];
|
||||
// } else {
|
||||
// pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF, bg_buf[0] & 0xFF);
|
||||
// }
|
||||
// memGraphics.setColor(pixel);
|
||||
// memGraphics.fillRect(x, y, w, h);
|
||||
|
||||
// byte[] buf = new byte[nSubrects * (bytesPixel + 4)];
|
||||
// rfb.readFully(buf);
|
||||
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
"vncproxy/logger"
|
||||
)
|
||||
@@ -15,14 +16,23 @@ const (
|
||||
|
||||
type HextileEncoding struct {
|
||||
//Colors []Color
|
||||
bytes []byte
|
||||
}
|
||||
|
||||
func (z *HextileEncoding) Type() int32 {
|
||||
return 5
|
||||
}
|
||||
func (z *HextileEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
return w.Write(z.bytes)
|
||||
}
|
||||
func (z *HextileEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
bytesPerPixel := int(pixelFmt.BPP) / 8
|
||||
|
||||
r.StartByteCollection()
|
||||
defer func() {
|
||||
z.bytes = r.EndByteCollection()
|
||||
}()
|
||||
|
||||
for ty := rect.Y; ty < rect.Y+rect.Height; ty += 16 {
|
||||
th := 16
|
||||
if rect.Y+rect.Height-ty < 16 {
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package encodings
|
||||
|
||||
import "vncproxy/common"
|
||||
import (
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
type PseudoEncoding struct {
|
||||
Typ int32
|
||||
@@ -9,7 +12,9 @@ type PseudoEncoding struct {
|
||||
func (pe *PseudoEncoding) Type() int32 {
|
||||
return pe.Typ
|
||||
}
|
||||
|
||||
func (z *PseudoEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
return 0, nil
|
||||
}
|
||||
func (pe *PseudoEncoding) Read(*common.PixelFormat, *common.Rectangle, *common.RfbReadHelper) (common.Encoding, error) {
|
||||
return pe, nil
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
@@ -9,12 +11,15 @@ import (
|
||||
// See RFC 6143 Section 7.7.1
|
||||
type RawEncoding struct {
|
||||
//Colors []Color
|
||||
bytes []byte
|
||||
}
|
||||
|
||||
func (*RawEncoding) Type() int32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (z *RawEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
return w.Write(z.bytes)
|
||||
}
|
||||
func (*RawEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
|
||||
//conn := common.RfbReadHelper{Reader:r}
|
||||
@@ -27,10 +32,11 @@ func (*RawEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r
|
||||
// }
|
||||
|
||||
//colors := make([]vnc.Color, int(rect.Height)*int(rect.Width))
|
||||
|
||||
bytes := &bytes.Buffer{}
|
||||
for y := uint16(0); y < rect.Height; y++ {
|
||||
for x := uint16(0); x < rect.Width; x++ {
|
||||
if _, err := r.ReadBytes(bytesPerPixel); err != nil {
|
||||
if bts, err := r.ReadBytes(bytesPerPixel); err != nil {
|
||||
StoreBytes(bytes, bts)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -54,5 +60,5 @@ func (*RawEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r
|
||||
}
|
||||
}
|
||||
|
||||
return &RawEncoding{}, nil
|
||||
return &RawEncoding{bytes.Bytes()}, nil
|
||||
}
|
||||
|
@@ -1,25 +1,57 @@
|
||||
package encodings
|
||||
|
||||
import "vncproxy/common"
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
type RREEncoding struct {
|
||||
//Colors []Color
|
||||
numSubRects uint32
|
||||
backgroundColor []byte
|
||||
subRectData []byte
|
||||
}
|
||||
|
||||
func (z *RREEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
binary.Write(w, binary.BigEndian, z.numSubRects)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
w.Write(z.backgroundColor)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
w.Write(z.subRectData)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
b := len(z.backgroundColor) + len(z.subRectData) + 4
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (z *RREEncoding) Type() int32 {
|
||||
return 2
|
||||
}
|
||||
func (z *RREEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := common.RfbReadHelper{Reader:r}
|
||||
bytesPerPixel := int(pixelFmt.BPP / 8)
|
||||
numOfSubrectangles, _ := r.ReadUint32()
|
||||
numOfSubrectangles, err := r.ReadUint32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
z.numSubRects = numOfSubrectangles
|
||||
|
||||
//read whole rect background color
|
||||
r.ReadBytes(bytesPerPixel)
|
||||
//read whole-rect background color
|
||||
z.backgroundColor, err = r.ReadBytes(bytesPerPixel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//read all individual rects (color=bytesPerPixel + x=16b + y=16b + w=16b + h=16b)
|
||||
_, err := r.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 8)) // x+y+w+h=8 bytes
|
||||
|
||||
z.subRectData, err = r.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 8)) // x+y+w+h=8 bytes
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -1,9 +1,12 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
"vncproxy/logger"
|
||||
)
|
||||
|
||||
var TightMinToCompress int = 12
|
||||
@@ -20,6 +23,7 @@ const (
|
||||
)
|
||||
|
||||
type TightEncoding struct {
|
||||
bytes []byte
|
||||
//output io.Writer
|
||||
//logger common.Logger
|
||||
}
|
||||
@@ -67,13 +71,29 @@ func calcTightBytePerPixel(pf *common.PixelFormat) int {
|
||||
return bytesPerPixelTight
|
||||
}
|
||||
|
||||
func (z *TightEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
return w.Write(z.bytes)
|
||||
}
|
||||
|
||||
func StoreBytes(bytes *bytes.Buffer, data []byte) {
|
||||
_, err := bytes.Write(data)
|
||||
if err != nil {
|
||||
logger.Error("Error in encoding while saving bytes: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
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}
|
||||
|
||||
r.StartByteCollection()
|
||||
defer func() {
|
||||
t.bytes = r.EndByteCollection()
|
||||
}()
|
||||
//var subencoding uint8
|
||||
compctl, err := r.ReadUint8()
|
||||
//binary.Write(bytes, binary.BigEndian, compctl)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("error in handling tight encoding: %v\n", err)
|
||||
return nil, err
|
||||
@@ -88,7 +108,13 @@ func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangl
|
||||
case TightFill:
|
||||
fmt.Printf("reading fill size=%d\n", bytesPixel)
|
||||
//read color
|
||||
r.ReadBytes(int(bytesPixel))
|
||||
_, err := r.ReadBytes(int(bytesPixel))
|
||||
if err != nil {
|
||||
fmt.Printf("error in handling tight encoding: %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
//StoreBytes(bytes, bts)
|
||||
|
||||
//byt, _ := r.ReadBytes(3)
|
||||
//fmt.Printf(">>>>>>>>>TightFillBytes=%v", byt)
|
||||
return t, nil
|
||||
@@ -98,11 +124,16 @@ func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangl
|
||||
}
|
||||
|
||||
len, err := r.ReadCompactLen()
|
||||
//binary.Write(bytes, binary.BigEndian, len)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("reading jpeg size=%d\n", len)
|
||||
r.ReadBytes(len)
|
||||
fmt.Printf("reading jpeg, size=%d\n", len)
|
||||
_, err = r.ReadBytes(len)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//StoreBytes(bytes, bts)
|
||||
return t, nil
|
||||
default:
|
||||
|
||||
@@ -111,6 +142,7 @@ func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangl
|
||||
}
|
||||
|
||||
handleTightFilters(compctl, pixelFmt, rect, r)
|
||||
//t.bytes = bytes.Bytes()
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
@@ -126,6 +158,7 @@ func handleTightFilters(subencoding uint8, pixelFmt *common.PixelFormat, rect *c
|
||||
|
||||
if (subencoding & FILTER_ID_MASK) > 0 { // filter byte presence
|
||||
filterid, err = r.ReadUint8()
|
||||
//binary.Write(bytes, binary.BigEndian, filterid)
|
||||
if err != nil {
|
||||
fmt.Printf("error in handling tight encoding, reading filterid: %v\n", err)
|
||||
return
|
||||
@@ -146,10 +179,12 @@ func handleTightFilters(subencoding uint8, pixelFmt *common.PixelFormat, rect *c
|
||||
case TightFilterPalette: //PALETTE_FILTER
|
||||
|
||||
colorCount, err := r.ReadUint8()
|
||||
//binary.Write(bytes, binary.BigEndian, colorCount)
|
||||
paletteSize := colorCount + 1 // add one more
|
||||
fmt.Printf("----PALETTE_FILTER: paletteSize=%d bytesPixel=%d\n", paletteSize, bytesPixel)
|
||||
//complete palette
|
||||
r.ReadBytes(int(paletteSize) * bytesPixel)
|
||||
_, err = r.ReadBytes(int(paletteSize) * bytesPixel)
|
||||
//StoreBytes(bytes, bts)
|
||||
|
||||
var dataLength int
|
||||
if paletteSize == 2 {
|
||||
@@ -162,14 +197,26 @@ func handleTightFilters(subencoding uint8, pixelFmt *common.PixelFormat, rect *c
|
||||
fmt.Printf("error in handling tight encoding, Reading Palette: %v\n", err)
|
||||
return
|
||||
}
|
||||
//StoreBytes(bytes, bts)
|
||||
|
||||
case TightFilterGradient: //GRADIENT_FILTER
|
||||
fmt.Printf("----GRADIENT_FILTER: bytesPixel=%d\n", bytesPixel)
|
||||
//useGradient = true
|
||||
fmt.Printf("usegrad: %d\n", filterid)
|
||||
r.ReadTightData(lengthCurrentbpp)
|
||||
_, err := r.ReadTightData(lengthCurrentbpp)
|
||||
if err != nil {
|
||||
fmt.Printf("error in handling tight encoding, Reading GRADIENT_FILTER: %v\n", err)
|
||||
return
|
||||
}
|
||||
//StoreBytes(bytes, bts)
|
||||
case TightFilterCopy: //BASIC_FILTER
|
||||
fmt.Printf("----BASIC_FILTER: bytesPixel=%d\n", bytesPixel)
|
||||
r.ReadTightData(lengthCurrentbpp)
|
||||
_, err := r.ReadTightData(lengthCurrentbpp)
|
||||
if err != nil {
|
||||
fmt.Printf("error in handling tight encoding, Reading BASIC_FILTER: %v\n", err)
|
||||
return
|
||||
}
|
||||
//StoreBytes(bytes, bts)
|
||||
default:
|
||||
fmt.Printf("Bad tight filter id: %d\n", filterid)
|
||||
return
|
||||
|
@@ -2,17 +2,27 @@ package encodings
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
"vncproxy/logger"
|
||||
)
|
||||
|
||||
type TightPngEncoding struct {
|
||||
bytes []byte
|
||||
}
|
||||
|
||||
func (z *TightPngEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
return w.Write(z.bytes)
|
||||
}
|
||||
|
||||
func (*TightPngEncoding) Type() int32 { return int32(common.EncTightPng) }
|
||||
|
||||
func (t *TightPngEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
bytesPixel := calcTightBytePerPixel(pixelFmt)
|
||||
r.StartByteCollection()
|
||||
defer func() {
|
||||
t.bytes = r.EndByteCollection()
|
||||
}()
|
||||
|
||||
//var subencoding uint8
|
||||
compctl, err := r.ReadUint8()
|
||||
|
@@ -1,23 +1,39 @@
|
||||
package encodings
|
||||
|
||||
import "vncproxy/common"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
type ZLibEncoding struct {
|
||||
//Colors []Color
|
||||
bytes []byte
|
||||
}
|
||||
|
||||
func (z *ZLibEncoding) Type() int32 {
|
||||
return 6
|
||||
}
|
||||
func (z *ZLibEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
return w.Write(z.bytes)
|
||||
}
|
||||
func (z *ZLibEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := common.RfbReadHelper{Reader:r}
|
||||
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
|
||||
//bytesPerPixel := c.PixelFormat.BPP / 8
|
||||
len, _ := r.ReadUint32()
|
||||
_, err := r.ReadBytes(int(len))
|
||||
|
||||
bytes := &bytes.Buffer{}
|
||||
len, err := r.ReadUint32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
binary.Write(bytes, binary.BigEndian, len)
|
||||
bts, err := r.ReadBytes(int(len))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
StoreBytes(bytes, bts)
|
||||
z.bytes = bytes.Bytes()
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -1,23 +1,41 @@
|
||||
package encodings
|
||||
|
||||
import "vncproxy/common"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
type ZRLEEncoding struct {
|
||||
//Colors []Color
|
||||
bytes []byte
|
||||
}
|
||||
|
||||
func (z *ZRLEEncoding) Type() int32 {
|
||||
return 16
|
||||
}
|
||||
|
||||
func (z *ZRLEEncoding) WriteTo(w io.Writer) (n int, err error) {
|
||||
return w.Write(z.bytes)
|
||||
}
|
||||
|
||||
func (z *ZRLEEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := common.RfbReadHelper{Reader: r}
|
||||
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
|
||||
//bytesPerPixel := c.PixelFormat.BPP / 8
|
||||
len, _ := r.ReadUint32()
|
||||
_, err := r.ReadBytes(int(len))
|
||||
|
||||
bytes := &bytes.Buffer{}
|
||||
len, err := r.ReadUint32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
binary.Write(bytes, binary.BigEndian, len)
|
||||
bts, err := r.ReadBytes(int(len))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
StoreBytes(bytes, bts)
|
||||
z.bytes = bytes.Bytes()
|
||||
return z, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user