github.com/searKing/golang/go@v1.2.117/net/mux/matcher_tls.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mux
     6  
     7  import (
     8  	"encoding/binary"
     9  
    10  	"github.com/searKing/golang/go/crypto/tls"
    11  )
    12  
    13  // TLS matches HTTPS requests.
    14  //
    15  // By default, any TLS handshake packet is matched. An optional whitelist
    16  // of versions can be passed in to restrict the matcher, for example:
    17  //
    18  //	TLS(tls.VersionTLS11, tls.VersionTLS12)
    19  //
    20  // reverse of crypto/tls/conn.go func (c *Conn) readRecordOrCCS(expectChangeCipherSpec bool) error {
    21  // HandlerShake of TLS
    22  // type byte	// recordTypeHandshake
    23  // versions [2]byte
    24  func TLS(versions ...int) MatcherFunc {
    25  	const recordTypeHandshake = 22
    26  	if len(versions) == 0 {
    27  		versions = tls.Versions
    28  	}
    29  	var prefixes [][]byte
    30  	for _, v := range versions {
    31  		var ver = make([]byte, 2)
    32  		binary.BigEndian.PutUint16(ver, uint16(v))
    33  		// recordType+VersionTLS+len(PayLoad)
    34  		var prefix []byte
    35  		prefix = append(prefix, recordTypeHandshake)
    36  		prefix = append(prefix, ver...)
    37  		prefixes = append(prefixes, prefix)
    38  	}
    39  	return AnyPrefixByteMatcher(prefixes...)
    40  }