Tcp多路复用

This commit is contained in:
刘河
2019-01-06 03:16:46 +08:00
parent ade3bb0c71
commit 9bec5366a6
14 changed files with 271 additions and 187 deletions

View File

@@ -27,6 +27,7 @@ const (
COMPRESS_NONE_DECODE
COMPRESS_SNAPY_ENCODE
COMPRESS_SNAPY_DECODE
IO_EOF = "EOF"
)
//error
@@ -130,19 +131,35 @@ func replaceHost(resp []byte) []byte {
}
//copy
func relay(in, out *Conn, compressType int, crypt bool) {
func relay(in, out *Conn, compressType int, crypt, mux bool) {
switch compressType {
case COMPRESS_SNAPY_ENCODE:
copyBuffer(NewSnappyConn(in.conn, crypt), out)
if mux {
NewSnappyConn(in.conn, crypt).Write([]byte(IO_EOF))
out.Close()
}
case COMPRESS_SNAPY_DECODE:
copyBuffer(in, NewSnappyConn(out.conn, crypt))
if mux {
in.Close()
}
case COMPRESS_NONE_ENCODE:
copyBuffer(NewCryptConn(in.conn, crypt), out)
if mux {
NewCryptConn(in.conn, crypt).Write([]byte(IO_EOF))
out.Close()
}
case COMPRESS_NONE_DECODE:
copyBuffer(in, NewCryptConn(out.conn, crypt))
if mux {
in.Close()
}
}
if !mux {
in.Close()
out.Close()
}
out.Close()
in.Close()
}
//判断压缩方式
@@ -183,7 +200,7 @@ func verify(verifyKeyMd5 string) bool {
}
//get key by host from x
func getKeyByHost(host string) (h *HostList, t *TaskList, err error) {
func getKeyByHost(host string) (h *HostList, t *ServerConfig, err error) {
for _, v := range CsvDb.Hosts {
if strings.Contains(host, v.Host) {
h = v
@@ -258,6 +275,10 @@ func GetStrByBool(b bool) string {
}
return "0"
}
func GetIntNoerrByStr(str string) int {
i, _ := strconv.Atoi(str)
return i
}
// io.copy的优化版读取buffer长度原为32*1024与snappy不同导致读取出的内容存在差异不利于解密特此修改
func copyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {