-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprotocolMuxListener.go
More file actions
68 lines (57 loc) · 1.57 KB
/
Copy pathprotocolMuxListener.go
File metadata and controls
68 lines (57 loc) · 1.57 KB
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 vproxy
import (
"bufio"
"crypto/tls"
"net"
"time"
)
// protocolMuxListener 实现了 net.Listener 接口,用于在同一个连接上嗅探 HTTP/HTTPS 协议
type protocolMuxListener struct {
net.Listener
tlsConfig *tls.Config
}
// Accept 覆盖了标准的 Accept 方法,实现协议分发
func (l *protocolMuxListener) Accept() (net.Conn, error) {
for {
// 1. 接受底层连接
conn, err := l.Listener.Accept()
if err != nil {
return nil, err
}
if l.tlsConfig == nil {
// 如果没有 TLS 配置,直接返回普通连接
return conn, nil
}
// 设置读取截止时间,防止阻塞过久
if err := conn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
conn.Close()
continue
}
// 2. 包装连接以便我们可以“偷看”前几个字节
reader := bufio.NewReader(conn)
// 3. 偷看前 5 个字节
peeked, err := reader.Peek(5)
if err != nil {
// 如果连接失效或读取超时,直接关闭连接继续等待下一个,避免使主 Listener 崩溃
conn.Close()
continue
}
if err := conn.SetReadDeadline(time.Time{}); err != nil {
conn.Close()
continue
}
bc := &bufferConn{
Conn: conn,
br: reader,
}
// 4. 判断协议类型
// TLS 握手消息的起始字节 (0x16)
if len(peeked) >= 1 && peeked[0] == 0x16 {
// 创建一个包装器,将偷看的数据重新放回流中
tlsConn := tls.Server(bc, l.tlsConfig)
return tlsConn, nil
}
// 创建一个包装器,将偷看的数据重新放回流中,供标准 HTTP 处理器读取
return bc, nil
}
}