-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathrecord_detect.go
More file actions
185 lines (175 loc) · 4.4 KB
/
record_detect.go
File metadata and controls
185 lines (175 loc) · 4.4 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package reality
import (
"bytes"
"encoding/binary"
"io"
"math"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/pires/go-proxyproto"
utls "github.com/refraction-networking/utls"
)
var GlobalPostHandshakeRecordsLens sync.Map
var GlobalMaxCSSMsgCount sync.Map
func DetectPostHandshakeRecordsLens(config *Config) {
for sni := range config.ServerNames {
for alpn := range 3 { // 0, 1, 2
key := config.Dest + " " + sni + " " + strconv.Itoa(alpn)
if _, loaded := GlobalPostHandshakeRecordsLens.LoadOrStore(key, false); !loaded {
go func() {
defer func() {
val, _ := GlobalPostHandshakeRecordsLens.Load(key)
if _, ok := val.(bool); ok {
GlobalPostHandshakeRecordsLens.Store(key, []int{})
}
}()
target, err := net.Dial(config.Type, config.Dest)
if err != nil {
return
}
if config.Xver == 1 || config.Xver == 2 {
if _, err = proxyproto.HeaderProxyFromAddrs(config.Xver, target.LocalAddr(), target.RemoteAddr()).WriteTo(target); err != nil {
return
}
}
detectConn := &PostHandshakeRecordDetectConn{
Conn: target,
Key: key,
}
fingerprint := utls.HelloChrome_Auto
nextProtos := []string{"h2", "http/1.1"}
if alpn != 2 {
fingerprint = utls.HelloGolang
}
if alpn == 1 {
nextProtos = []string{"http/1.1"}
}
if alpn == 0 {
nextProtos = nil
}
uConn := utls.UClient(detectConn, &utls.Config{
ServerName: sni, // needs new loopvar behaviour
NextProtos: nextProtos,
}, fingerprint)
if err = uConn.Handshake(); err != nil {
return
}
io.Copy(io.Discard, uConn)
}()
go func() {
target, err := net.Dial(config.Type, config.Dest)
if err != nil {
return
}
if config.Xver == 1 || config.Xver == 2 {
if _, err = proxyproto.HeaderProxyFromAddrs(config.Xver, target.LocalAddr(), target.RemoteAddr()).WriteTo(target); err != nil {
return
}
}
fingerprint := utls.HelloChrome_Auto
nextProtos := []string{"h2", "http/1.1"}
if alpn != 2 {
fingerprint = utls.HelloGolang
}
if alpn == 1 {
nextProtos = []string{"http/1.1"}
}
if alpn == 0 {
nextProtos = nil
}
conn := &CCSDetectConn{
Conn: target,
Key: key,
}
uConn := utls.UClient(conn, &utls.Config{
ServerName: sni, // needs new loopvar behaviour
NextProtos: nextProtos,
}, fingerprint)
if err = uConn.Handshake(); err != nil {
return
}
}()
}
}
}
}
type PostHandshakeRecordDetectConn struct {
net.Conn
Key string
CcsSent bool
}
func (c *PostHandshakeRecordDetectConn) Write(b []byte) (n int, err error) {
if len(b) >= 3 && bytes.Equal(b[:3], []byte{20, 3, 3}) {
c.CcsSent = true
}
return c.Conn.Write(b)
}
func (c *PostHandshakeRecordDetectConn) Read(b []byte) (n int, err error) {
if !c.CcsSent {
return c.Conn.Read(b)
}
c.Conn.SetReadDeadline(time.Now().Add(5 * time.Second))
data, _ := io.ReadAll(c.Conn)
var postHandshakeRecordsLens []int
for {
if len(data) >= 5 && bytes.Equal(data[:3], []byte{23, 3, 3}) {
length := int(binary.BigEndian.Uint16(data[3:5])) + 5
postHandshakeRecordsLens = append(postHandshakeRecordsLens, length)
data = data[length:]
} else {
break
}
}
GlobalPostHandshakeRecordsLens.Store(c.Key, postHandshakeRecordsLens)
return 0, io.EOF
}
var CCSMsg = []byte{0x14, 0x3, 0x3, 0x0, 0x1, 0x1}
type CCSDetectConn struct {
net.Conn
Key string
}
func (c *CCSDetectConn) Write(b []byte) (n int, err error) {
if len(b) >= 3 && bytes.Equal(b[:3], []byte{20, 3, 3}) {
var hasAlert atomic.Bool
go func() {
defer hasAlert.Store(true)
buf := make([]byte, 512)
for {
_, err = c.Conn.Read(buf)
if err != nil {
return
}
if buf[0] == 0x15 {
return
}
}
}()
sendProbePayload := func(count int) bool {
msg := bytes.Repeat(CCSMsg, count)
c.Conn.Write(msg)
time.Sleep(1 * time.Second)
if hasAlert.Load() {
return true
}
return false
}
if sendProbePayload(2) {
GlobalMaxCSSMsgCount.Store(c.Key, 1)
return c.Conn.Write(b)
}
if sendProbePayload(15) {
GlobalMaxCSSMsgCount.Store(c.Key, 16)
return c.Conn.Write(b)
}
if sendProbePayload(16) {
GlobalMaxCSSMsgCount.Store(c.Key, 32)
return c.Conn.Write(b)
}
GlobalMaxCSSMsgCount.Store(c.Key, math.MaxInt)
return c.Conn.Write(b)
}
return c.Conn.Write(b)
}