|
7 | 7 | "net" |
8 | 8 | "strconv" |
9 | 9 | "sync" |
| 10 | + "sync/atomic" |
10 | 11 | "time" |
11 | 12 |
|
12 | 13 | "github.com/pires/go-proxyproto" |
@@ -145,19 +146,44 @@ type CCSDetectConn struct { |
145 | 146 |
|
146 | 147 | func (c *CCSDetectConn) Write(b []byte) (n int, err error) { |
147 | 148 | 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 | + } |
158 | 161 | } |
| 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) |
159 | 184 | } |
160 | | - GlobalMaxCSSMsgCount.Store(c.Key, i-2) |
| 185 | + GlobalMaxCSSMsgCount.Store(c.Key, 1145141919810) |
| 186 | + return c.Conn.Write(b) |
161 | 187 | } |
162 | 188 | return c.Conn.Write(b) |
163 | 189 | } |
0 commit comments