新增压缩功能-LZS算法

This commit is contained in:
lanrenwo
2023-01-17 12:09:04 +08:00
parent 70c82b8baa
commit 768e137ff9
12 changed files with 212 additions and 23 deletions

View File

@@ -66,7 +66,23 @@ func LinkCstp(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSessio
return
}
case 0x04:
// log.Println("recv DPD-RESP")
// log.Println("recv DPD-RESP")
case 0x08: // decompress
if cSess.CstpPickCmp == nil {
continue
}
dst := getByteFull()
n, err = cSess.CstpPickCmp.Uncompress(pl.Data[8:], (*dst)[8:])
if err != nil {
putByte(dst)
base.Debug("cstp decompress error", n)
continue
}
copy((*dst)[:8], pl.Data[:8])
binary.BigEndian.PutUint16((*dst)[4:6], uint16(n))
pl.Data = append(pl.Data[:0], (*dst)[:n+8]...)
putByte(dst)
fallthrough
case 0x00: // DATA
// 获取数据长度
dataLen = binary.BigEndian.Uint16(pl.Data[4:6]) // 4,5
@@ -112,16 +128,31 @@ func cstpWrite(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSessi
}
if pl.PType == 0x00 {
// 获取数据长度
l := len(pl.Data)
// 先扩容 +8
pl.Data = pl.Data[:l+8]
// 数据后移
copy(pl.Data[8:], pl.Data)
// 添加头信息
copy(pl.Data[:8], plHeader)
// 更新头长度
binary.BigEndian.PutUint16(pl.Data[4:6], uint16(l))
isCompress := false
if cSess.CstpPickCmp != nil && len(pl.Data) > base.Cfg.NoCompressLimit {
dst := getByteFull()
size, err := cSess.CstpPickCmp.Compress(pl.Data, (*dst)[8:])
if err == nil && size < len(pl.Data) {
copy((*dst)[:8], plHeader)
binary.BigEndian.PutUint16((*dst)[4:6], uint16(size))
(*dst)[6] = 0x08
pl.Data = append(pl.Data[:0], (*dst)[:size+8]...)
isCompress = true
}
putByte(dst)
}
if !isCompress {
// 获取数据长度
l := len(pl.Data)
// 先扩容 +8
pl.Data = pl.Data[:l+8]
// 数据后移
copy(pl.Data[8:], pl.Data)
// 添加头信息
copy(pl.Data[:8], plHeader)
// 更新头长度
binary.BigEndian.PutUint16(pl.Data[4:6], uint16(l))
}
} else {
pl.Data = append(pl.Data[:0], plHeader...)
// 设置头类型

View File

@@ -68,7 +68,23 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
return
}
case 0x04:
// base.Debug("recv DPD-RESP", cSess.IpAddr)
// base.Debug("recv DPD-RESP", cSess.IpAddr)
case 0x08: // decompress
if cSess.DtlsPickCmp == nil {
continue
}
dst := getByteFull()
n, err = cSess.DtlsPickCmp.Uncompress(pl.Data[1:], (*dst)[1:])
if err != nil {
base.Debug("dtls decompress error, size is ", n)
putByte(dst)
continue
}
n = n + 1
pl.Data = append(pl.Data[:0], (*dst)[:n]...)
putByte(dst)
fallthrough
case 0x00: // DATA
// 去除数据头
// copy(pl.Data, pl.Data[1:n])
@@ -108,14 +124,28 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
// header = []byte{payload.PType}
if pl.PType == 0x00 { // data
// 获取数据长度
l := len(pl.Data)
// 先扩容 +1
pl.Data = pl.Data[:l+1]
// 数据后移
copy(pl.Data[1:], pl.Data)
// 添加头信息
pl.Data[0] = pl.PType
isCompress := false
if cSess.DtlsPickCmp != nil && len(pl.Data) > base.Cfg.NoCompressLimit {
dst := getByteFull()
size, err := cSess.DtlsPickCmp.Compress(pl.Data, (*dst)[1:])
if err == nil && size < len(pl.Data) {
(*dst)[0] = 0x08
pl.Data = append(pl.Data[:0], (*dst)[:size+1]...)
isCompress = true
}
putByte(dst)
}
// 未压缩
if !isCompress {
// 获取数据长度
l := len(pl.Data)
// 先扩容 +1
pl.Data = pl.Data[:l+1]
// 数据后移
copy(pl.Data[1:], pl.Data)
// 添加头信息
pl.Data[0] = pl.PType
}
} else {
// 设置头类型
pl.Data = append(pl.Data[:0], pl.PType)

View File

@@ -89,6 +89,14 @@ func LinkTunnel(w http.ResponseWriter, r *http.Request) {
base.Debug(cSess.IpAddr, cSess.MacHw, sess.Username, mobile)
// 压缩
if cmpName, ok := cSess.SetPickCmp("cstp", r.Header.Get("X-Cstp-Accept-Encoding")); ok {
HttpSetHeader(w, "X-CSTP-Content-Encoding", cmpName)
}
if cmpName, ok := cSess.SetPickCmp("dtls", r.Header.Get("X-Dtls-Accept-Encoding")); ok {
HttpSetHeader(w, "X-DTLS-Content-Encoding", cmpName)
}
// 返回客户端数据
HttpSetHeader(w, "Server", fmt.Sprintf("%s %s", base.APP_NAME, base.APP_VER))
HttpSetHeader(w, "X-CSTP-Version", "1")