95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package shadow
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
)
|
|
|
|
var (
|
|
AppDataHeader = []byte{0x17, 0x3, 0x3}
|
|
HeaderLength = len(AppDataHeader)
|
|
)
|
|
|
|
type PackAppData struct {
|
|
Conn net.Conn
|
|
}
|
|
|
|
func (m PackAppData) Read(p []byte) (n int, err error) {
|
|
|
|
buf := make([]byte, 48*1024+HeaderLength+2)
|
|
|
|
headRead, err := io.ReadAtLeast(m.Conn, buf[0:HeaderLength+2], HeaderLength+2)
|
|
if err != nil {
|
|
if err != io.EOF {
|
|
fmt.Printf("Read header error: %v\n", err)
|
|
}
|
|
return 0, err
|
|
}
|
|
if headRead < HeaderLength+2 {
|
|
return 0, errors.New("Read header failed")
|
|
}
|
|
if bytes.Equal(buf[0:HeaderLength], AppDataHeader) {
|
|
payLoadLength := int(binary.BigEndian.Uint16(buf[HeaderLength : HeaderLength+2]))
|
|
sum := 0
|
|
for sum < payLoadLength {
|
|
r, e := m.Conn.Read(buf[HeaderLength+2+sum : HeaderLength+2+payLoadLength])
|
|
if e != nil {
|
|
if e == io.EOF {
|
|
break
|
|
} else {
|
|
return 0, e
|
|
}
|
|
}
|
|
sum += r
|
|
}
|
|
if len(EncryptKey) > 0 {
|
|
encryptedData := buf[HeaderLength+2 : HeaderLength+2+sum]
|
|
decrypted := AesDecryptCBC(encryptedData, EncryptKey)
|
|
copy(p[0:], decrypted)
|
|
sum = len(decrypted)
|
|
} else {
|
|
copy(p[0:], buf[HeaderLength+2:HeaderLength+2+sum])
|
|
}
|
|
return sum, err
|
|
} else {
|
|
fmt.Printf("Invalid header")
|
|
return 0, errors.New("invalid header")
|
|
}
|
|
}
|
|
|
|
func (m PackAppData) Write(p []byte) (n int, err error) {
|
|
var sendData []byte
|
|
if len(EncryptKey) > 0 {
|
|
sendData = AesEncryptCBC(p, EncryptKey)
|
|
} else {
|
|
sendData = p
|
|
}
|
|
|
|
lenNum := make([]byte, 2)
|
|
binary.BigEndian.PutUint16(lenNum, uint16(len(sendData)))
|
|
|
|
packetBuf := bytes.NewBuffer(AppDataHeader)
|
|
packetBuf.Write(lenNum)
|
|
packetBuf.Write(sendData)
|
|
|
|
write, err := m.Conn.Write(packetBuf.Bytes())
|
|
if len(EncryptKey) > 0 {
|
|
if write != packetBuf.Len() {
|
|
write = 0
|
|
} else {
|
|
write = len(p)
|
|
}
|
|
} else {
|
|
write = write - HeaderLength - 2
|
|
}
|
|
return write, err
|
|
}
|
|
|
|
func (m PackAppData) Close() error {
|
|
return m.Conn.Close()
|
|
}
|