-
Notifications
You must be signed in to change notification settings - Fork 19
/
conn.go
68 lines (61 loc) · 1.64 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package pa
import (
"io"
"net"
)
// ProtocolDetectConn 连接类型检测连接
// 该类型连接对象将会对连接到来的客户端Hello消息进行分析解析出连接协议,
// 并缓存收到的消息,将自己作为原始连接对象。
type ProtocolDetectConn struct {
net.Conn
major, minor uint8 // 协议版本
recordHeader []byte // 客户端Hello消息的记录层协议头部
}
// protocolVersion 连接所使用的协议版本
func (c *ProtocolDetectConn) protocolVersion() (major uint8, minor uint8) {
return c.major, c.minor
}
// Raw 返回原始连接对象
func (c *ProtocolDetectConn) Raw() net.Conn {
return c.Conn
}
// ReadFirstHeader 读取第1个记录层消息的头部
func (c *ProtocolDetectConn) ReadFirstHeader() error {
// struct {
// ContentType type; // 1 Byte
// ProtocolVersion version; // 2 Byte
// uint16 length; // 2 Byte
// opaque fragment[TLSPlaintext.length]; // length Byte
//}
c.recordHeader = make([]byte, 5)
_, err := io.ReadFull(c.Conn, c.recordHeader)
c.major, c.minor = c.recordHeader[1], c.recordHeader[2]
return err
}
func (c *ProtocolDetectConn) Read(b []byte) (n int, err error) {
if len(c.recordHeader) == 0 {
return c.Conn.Read(b)
}
if len(b) >= len(c.recordHeader) {
n = copy(b, c.recordHeader)
c.recordHeader = nil
if len(b) > n {
var n1 = 0
n1, err = c.Conn.Read(b[n:])
n += n1
if err != nil {
return n, err
}
}
return n, nil
} else {
p := c.recordHeader[:len(b)]
n = len(b)
copy(b, p)
c.recordHeader = c.recordHeader[len(b):]
if len(c.recordHeader) == 0 {
c.recordHeader = nil
}
return n, nil
}
}