增加防探测功能,增加流量加密功能

This commit is contained in:
wenyifan
2022-09-09 15:29:41 +08:00
parent c2ab6cbe5f
commit a064f72104
12 changed files with 185 additions and 79 deletions

View File

@ -47,6 +47,12 @@ func (m PackAppData) Read(p []byte) (n int, err error) {
copy(p[sum:], buf[HeaderLength+2+sum:HeaderLength+2+sum+r])
sum += r
}
if len(EncryptKey) > 0 {
encryptedData := p[0:sum]
decrypted := AesDecryptCBC(encryptedData, EncryptKey)
copy(p[0:], decrypted)
sum = len(decrypted)
}
return sum, err
} else {
fmt.Printf("Invalid header")
@ -55,15 +61,30 @@ func (m PackAppData) Read(p []byte) (n int, err error) {
}
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(p)))
binary.BigEndian.PutUint16(lenNum, uint16(len(sendData)))
packetBuf := bytes.NewBuffer(AppDataHeader)
packetBuf.Write(lenNum)
packetBuf.Write(p)
packetBuf.Write(sendData)
write, err := m.Conn.Write(packetBuf.Bytes())
write = write - HeaderLength - 2
if len(EncryptKey) > 0 {
if write != packetBuf.Len() {
write = 0
} else {
write = len(p)
}
} else {
write = write - HeaderLength - 2
}
return write, err
}