-
Notifications
You must be signed in to change notification settings - Fork 3
/
veeam_conn.go
99 lines (87 loc) · 2.54 KB
/
veeam_conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package veeam_ds_client
import (
"bytes"
"compress/flate"
"encoding/binary"
"errors"
"io"
"log"
"net"
"time"
)
type CompressedConn struct {
tcpConn net.Conn
buffer *bytes.Buffer
}
const (
IsCompressed uint32 = 2147483648
)
func WrapConnection(conn net.Conn) net.Conn {
return &CompressedConn{
tcpConn: conn,
buffer: bytes.NewBuffer([]byte{}),
}
}
func (conn *CompressedConn) Read(b []byte) (n int, err error) {
if conn.buffer.Len() != 0 {
return conn.buffer.Read(b)
}
conn.buffer.Reset()
var frameCompressedSize uint32
var frameUncompressedSize uint32
// First packet is total frame size
err = binary.Read(conn.tcpConn, binary.LittleEndian, &frameUncompressedSize)
err = binary.Read(conn.tcpConn, binary.LittleEndian, &frameCompressedSize)
isCompressed := frameUncompressedSize&IsCompressed == IsCompressed
if !isCompressed {
log.Printf("frame size C/U : %d/%d",
frameCompressedSize, frameUncompressedSize)
written, err := io.CopyN(conn.buffer, conn.tcpConn, int64(frameCompressedSize))
if err != nil {
return n, err
}
if uint32(written) != uint32(frameUncompressedSize) {
return 0, errors.New("could not retrieve full packet")
}
} else {
frameUncompressedSize ^= IsCompressed
log.Printf("frame size C/U : %d/%d",
frameCompressedSize, frameUncompressedSize)
compressedReader := flate.NewReader(conn.tcpConn)
written, err := io.CopyN(conn.buffer, compressedReader, int64(frameUncompressedSize))
if err != nil {
return n, err
}
if uint32(written) != uint32(frameUncompressedSize) {
return 0, errors.New("could not retrieve full packet")
}
}
log.Println(conn.buffer.String())
return conn.buffer.Read(b)
}
func (conn *CompressedConn) Write(b []byte) (n int, err error) {
payloadBuffer := bytes.NewBuffer(nil)
err = binary.Write(payloadBuffer, binary.LittleEndian, uint32(0))
err = binary.Write(payloadBuffer, binary.LittleEndian, uint32(len(b)))
_, err = payloadBuffer.Write(b)
_, err = payloadBuffer.WriteTo(conn.tcpConn)
return len(b), err
}
func (conn *CompressedConn) Close() error {
return conn.tcpConn.Close()
}
func (conn *CompressedConn) LocalAddr() net.Addr {
return conn.tcpConn.LocalAddr()
}
func (conn *CompressedConn) RemoteAddr() net.Addr {
return conn.tcpConn.RemoteAddr()
}
func (conn *CompressedConn) SetDeadline(t time.Time) error {
return conn.tcpConn.SetDeadline(t)
}
func (conn *CompressedConn) SetReadDeadline(t time.Time) error {
return conn.tcpConn.SetReadDeadline(t)
}
func (conn *CompressedConn) SetWriteDeadline(t time.Time) error {
return conn.tcpConn.SetWriteDeadline(t)
}