dockerfiles/anylink/server/handler/link_cstp.go

176 lines
3.9 KiB
Go
Raw Normal View History

2021-06-08 20:45:26 +08:00
package handler
import (
2021-08-26 23:09:52 +08:00
"bufio"
2021-06-08 20:45:26 +08:00
"encoding/binary"
"net"
"time"
"github.com/bjdgyc/anylink/base"
2023-04-26 22:17:10 +08:00
"github.com/bjdgyc/anylink/dbdata"
2021-08-26 23:09:52 +08:00
"github.com/bjdgyc/anylink/pkg/utils"
2021-06-08 20:45:26 +08:00
"github.com/bjdgyc/anylink/sessdata"
)
2021-08-26 23:09:52 +08:00
func LinkCstp(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSession) {
2022-11-10 15:53:48 +08:00
base.Debug("LinkCstp connect ip:", cSess.IpAddr, "user:", cSess.Username, "rip:", conn.RemoteAddr())
2021-06-08 20:45:26 +08:00
defer func() {
2023-04-26 22:17:10 +08:00
base.Debug("LinkCstp return", cSess.Username, cSess.IpAddr)
2021-06-08 20:45:26 +08:00
_ = conn.Close()
cSess.Close()
}()
var (
err error
n int
dataLen uint16
dead = time.Duration(cSess.CstpDpd+5) * time.Second
)
2021-08-26 23:09:52 +08:00
go cstpWrite(conn, bufRW, cSess)
2021-06-08 20:45:26 +08:00
for {
// 设置超时限制
2021-08-26 23:09:52 +08:00
err = conn.SetReadDeadline(utils.NowSec().Add(dead))
2021-06-08 20:45:26 +08:00
if err != nil {
2023-04-26 22:17:10 +08:00
base.Error("SetDeadline: ", cSess.Username, err)
2021-06-08 20:45:26 +08:00
return
}
// hdata := make([]byte, BufferSize)
2021-08-02 20:41:35 +08:00
pl := getPayload()
2021-08-26 23:09:52 +08:00
n, err = bufRW.Read(pl.Data)
2021-06-08 20:45:26 +08:00
if err != nil {
2023-04-26 22:17:10 +08:00
base.Error("read hdata: ", cSess.Username, err)
2021-06-08 20:45:26 +08:00
return
}
// 限流设置
err = cSess.RateLimit(n, true)
if err != nil {
base.Error(err)
}
2021-08-02 20:41:35 +08:00
switch pl.Data[6] {
2021-06-08 20:45:26 +08:00
case 0x07: // KEEPALIVE
// do nothing
// base.Debug("recv keepalive", cSess.IpAddr)
case 0x05: // DISCONNECT
2023-04-26 22:17:10 +08:00
cSess.UserLogoutCode = dbdata.UserLogoutClient
base.Debug("DISCONNECT", cSess.Username, cSess.IpAddr)
2021-06-08 20:45:26 +08:00
return
case 0x03: // DPD-REQ
// base.Debug("recv DPD-REQ", cSess.IpAddr)
2021-08-02 20:41:35 +08:00
pl.PType = 0x04
if payloadOutCstp(cSess, pl) {
2021-06-08 20:45:26 +08:00
return
}
case 0x04:
2023-04-26 22:17:10 +08:00
// log.Println("recv DPD-RESP")
case 0x08: // decompress
if cSess.CstpPickCmp == nil {
continue
}
dst := getByteFull()
nn, err := cSess.CstpPickCmp.Uncompress(pl.Data[8:], *dst)
if err != nil {
putByte(dst)
base.Error("cstp decompress error", err, nn)
continue
}
binary.BigEndian.PutUint16(pl.Data[4:6], uint16(nn))
pl.Data = append(pl.Data[:8], (*dst)[:nn]...)
putByte(dst)
fallthrough
2021-06-08 20:45:26 +08:00
case 0x00: // DATA
2021-08-02 20:41:35 +08:00
// 获取数据长度
dataLen = binary.BigEndian.Uint16(pl.Data[4:6]) // 4,5
2022-11-10 15:53:48 +08:00
// 修复 cstp 数据长度溢出报错
if 8+dataLen > BufferSize {
2023-04-26 22:17:10 +08:00
base.Error("recv error dataLen", cSess.Username, dataLen)
2022-11-10 15:53:48 +08:00
continue
}
2021-08-02 20:41:35 +08:00
// 去除数据头
copy(pl.Data, pl.Data[8:8+dataLen])
// 更新切片长度
pl.Data = pl.Data[:dataLen]
// pl.Data = append(pl.Data[:0], pl.Data[8:8+dataLen]...)
if payloadIn(cSess, pl) {
2021-06-08 20:45:26 +08:00
return
}
}
}
}
2021-08-26 23:09:52 +08:00
func cstpWrite(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSession) {
2021-06-08 20:45:26 +08:00
defer func() {
2023-04-26 22:17:10 +08:00
base.Debug("cstpWrite return", cSess.Username, cSess.IpAddr)
2021-06-08 20:45:26 +08:00
_ = conn.Close()
cSess.Close()
}()
var (
err error
n int
2021-08-02 20:41:35 +08:00
pl *sessdata.Payload
2021-06-08 20:45:26 +08:00
)
for {
select {
2021-08-02 20:41:35 +08:00
case pl = <-cSess.PayloadOutCstp:
2021-06-08 20:45:26 +08:00
case <-cSess.CloseChan:
return
}
2021-08-02 20:41:35 +08:00
if pl.LType != sessdata.LTypeIPData {
2021-06-08 20:45:26 +08:00
continue
}
2021-08-02 20:41:35 +08:00
if pl.PType == 0x00 {
2023-04-26 22:17:10 +08:00
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))
}
2021-08-02 20:41:35 +08:00
} else {
pl.Data = append(pl.Data[:0], plHeader...)
// 设置头类型
pl.Data[6] = pl.PType
2021-06-08 20:45:26 +08:00
}
2021-08-02 20:41:35 +08:00
n, err = conn.Write(pl.Data)
2021-06-08 20:45:26 +08:00
if err != nil {
2023-04-26 22:17:10 +08:00
base.Error("write err", cSess.Username, err)
2021-06-08 20:45:26 +08:00
return
}
2021-08-02 20:41:35 +08:00
putPayload(pl)
2021-06-08 20:45:26 +08:00
// 限流设置
err = cSess.RateLimit(n, false)
if err != nil {
base.Error(err)
}
}
}