Skip to content

Commit c7dcea2

Browse files
authored
Better detector
1 parent 32d4a69 commit c7dcea2

1 file changed

Lines changed: 37 additions & 11 deletions

File tree

fingerprint_detect.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"strconv"
99
"sync"
10+
"sync/atomic"
1011
"time"
1112

1213
"github.com/pires/go-proxyproto"
@@ -145,19 +146,44 @@ type CCSDetectConn struct {
145146

146147
func (c *CCSDetectConn) Write(b []byte) (n int, err error) {
147148
if len(b) >= 3 && bytes.Equal(b[:3], []byte{20, 3, 3}) {
148-
var i int
149-
// 32(idx 31) → max allowed (what's we need)
150-
// 33(idx 32) → trigger remote TLS Alert
151-
// 34(idx 33) → trigger remote TCP RST
152-
// 35(idx 34) → write err, pass to system
153-
for i = range 35 {
154-
if _, err = c.Conn.Write(CCSMsg); err != nil {
155-
break
156-
} else {
157-
time.Sleep(c.rtt * 2)
149+
var hasAlert atomic.Bool
150+
go func() {
151+
defer hasAlert.Store(true)
152+
buf := make([]byte, 512)
153+
for {
154+
_, err = c.Conn.Read(buf)
155+
if err != nil {
156+
return
157+
}
158+
if buf[0] == 0x15 {
159+
return
160+
}
158161
}
162+
}()
163+
sendProbePayload := func(count int) bool {
164+
msg := bytes.Repeat(CCSMsg, count)
165+
rtt := max(100*time.Millisecond, c.rtt)
166+
c.Conn.Write(msg)
167+
time.Sleep(rtt)
168+
if hasAlert.Load() {
169+
return true
170+
}
171+
return false
172+
}
173+
if sendProbePayload(2) {
174+
GlobalMaxCSSMsgCount.Store(c.Key, 1)
175+
return c.Conn.Write(b)
176+
}
177+
if sendProbePayload(15) {
178+
GlobalMaxCSSMsgCount.Store(c.Key, 16)
179+
return c.Conn.Write(b)
180+
}
181+
if sendProbePayload(16) {
182+
GlobalMaxCSSMsgCount.Store(c.Key, 32)
183+
return c.Conn.Write(b)
159184
}
160-
GlobalMaxCSSMsgCount.Store(c.Key, i-2)
185+
GlobalMaxCSSMsgCount.Store(c.Key, 1145141919810)
186+
return c.Conn.Write(b)
161187
}
162188
return c.Conn.Write(b)
163189
}

0 commit comments

Comments
 (0)