@@ -219,59 +219,35 @@ func openCONNECTTunnel(ctx context.Context, pepAddress string, identity ActorIde
219219}
220220
221221func deriveConnectAuthority (ctx context.Context , actorConn net.Conn , originalDst net.Addr ) (string , []byte ) {
222- if tcpAddr , ok := originalDst .(* net.TCPAddr ); ok && tcpAddr .Port == 443 {
223- authority , initialBytes := deriveTLSConnectAuthority (ctx , actorConn , tcpAddr )
224- return authority , initialBytes
225- }
226- if tcpAddr , ok := originalDst .(* net.TCPAddr ); ok && tcpAddr .Port == 80 {
227- authority , initialBytes := deriveHTTPConnectAuthority (ctx , actorConn , tcpAddr )
228- return authority , initialBytes
222+ if tcpAddr , ok := originalDst .(* net.TCPAddr ); ok {
223+ return classifyConnectAuthority (ctx , actorConn , tcpAddr )
229224 }
230225 return originalDst .String (), nil
231226}
232227
233- func deriveTLSConnectAuthority (ctx context.Context , actorConn net.Conn , originalDst * net.TCPAddr ) (string , []byte ) {
234- const maxClientHelloBytes = 16 * 1024
235- _ = actorConn .SetReadDeadline (time .Now ().Add (2 * time .Second ))
236- defer actorConn .SetReadDeadline (time.Time {})
237-
238- var initialBytes []byte
239- buf := make ([]byte , 2048 )
240- for len (initialBytes ) < maxClientHelloBytes {
241- n , err := actorConn .Read (buf )
242- if n > 0 {
243- initialBytes = append (initialBytes , buf [:n ]... )
244- if sni , ok , needMore := tlsClientHelloSNI (initialBytes ); ok {
245- return net .JoinHostPort (sni , strconv .Itoa (originalDst .Port )), initialBytes
246- } else if ! needMore {
247- break
248- }
249- }
250- if err != nil {
251- if ctx .Err () != nil {
252- return originalDst .String (), initialBytes
253- }
254- break
255- }
256- }
257- return originalDst .String (), initialBytes
258- }
259-
260- func deriveHTTPConnectAuthority (ctx context.Context , actorConn net.Conn , originalDst * net.TCPAddr ) (string , []byte ) {
261- const maxHeaderBytes = 16 * 1024
228+ func classifyConnectAuthority (ctx context.Context , actorConn net.Conn , originalDst * net.TCPAddr ) (string , []byte ) {
229+ const maxSniffBytes = 16 * 1024
262230 _ = actorConn .SetReadDeadline (time .Now ().Add (2 * time .Second ))
263231 defer actorConn .SetReadDeadline (time.Time {})
264232
265233 var initialBytes []byte
266234 buf := make ([]byte , 2048 )
267- for len (initialBytes ) < maxHeaderBytes {
235+ for len (initialBytes ) < maxSniffBytes {
268236 n , err := actorConn .Read (buf )
269237 if n > 0 {
270238 initialBytes = append (initialBytes , buf [:n ]... )
271- if host , ok , needMore := httpHostHeader (initialBytes ); ok {
272- return authorityWithDefaultPort (host , originalDst .Port ), initialBytes
273- } else if ! needMore {
274- break
239+ if initialBytes [0 ] == 0x16 {
240+ if sni , ok , needMore := tlsClientHelloSNI (initialBytes ); ok {
241+ return net .JoinHostPort (sni , strconv .Itoa (originalDst .Port )), initialBytes
242+ } else if ! needMore {
243+ break
244+ }
245+ } else {
246+ if host , ok , needMore := httpHostHeader (initialBytes ); ok {
247+ return authorityWithDefaultPort (host , originalDst .Port ), initialBytes
248+ } else if ! needMore {
249+ break
250+ }
275251 }
276252 }
277253 if err != nil {
0 commit comments