46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package shadow
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
var (
|
|
AppDataHeader = []byte("GGGGGGGG")
|
|
HeaderLength = len(AppDataHeader)
|
|
)
|
|
|
|
type PackAppData struct {
|
|
Conn net.Conn
|
|
}
|
|
|
|
func (m PackAppData) Read(p []byte) (n int, err error) {
|
|
buf := make([]byte, 32*1024)
|
|
read, err := m.Conn.Read(buf[0 : HeaderLength+2])
|
|
if bytes.Equal(buf[0:HeaderLength], AppDataHeader) {
|
|
u := int(binary.BigEndian.Uint16(buf[HeaderLength : HeaderLength+2]))
|
|
r, err := m.Conn.Read(buf[HeaderLength+2 : u+HeaderLength+2])
|
|
copy(p, buf[HeaderLength+2:r+HeaderLength+2])
|
|
return r, err
|
|
} else {
|
|
fmt.Println("Header is not present")
|
|
return read, err
|
|
}
|
|
}
|
|
|
|
func (m PackAppData) Write(p []byte) (n int, err error) {
|
|
t := make([]byte, len(p)+HeaderLength+2)
|
|
copy(t[0:], AppDataHeader)
|
|
binary.BigEndian.PutUint16(t[HeaderLength:], uint16(len(p)))
|
|
copy(t[HeaderLength+2:], p)
|
|
write, err := m.Conn.Write(t)
|
|
write = write - HeaderLength - 2
|
|
return write, err
|
|
}
|
|
|
|
func (m PackAppData) Close() error {
|
|
return m.Conn.Close()
|
|
}
|