mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
1
common/protocol/tls/cert/.gitignore
vendored
Normal file
1
common/protocol/tls/cert/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.pem
|
178
common/protocol/tls/cert/cert.go
Normal file
178
common/protocol/tls/cert/cert.go
Normal file
|
@ -0,0 +1,178 @@
|
|||
package cert
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/asn1"
|
||||
"encoding/pem"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
)
|
||||
|
||||
//go:generate go run github.com/xtls/xray-core/v1/common/errors/errorgen
|
||||
|
||||
type Certificate struct {
|
||||
// Cerificate in ASN.1 DER format
|
||||
Certificate []byte
|
||||
// Private key in ASN.1 DER format
|
||||
PrivateKey []byte
|
||||
}
|
||||
|
||||
func ParseCertificate(certPEM []byte, keyPEM []byte) (*Certificate, error) {
|
||||
certBlock, _ := pem.Decode(certPEM)
|
||||
if certBlock == nil {
|
||||
return nil, newError("failed to decode certificate")
|
||||
}
|
||||
keyBlock, _ := pem.Decode(keyPEM)
|
||||
if keyBlock == nil {
|
||||
return nil, newError("failed to decode key")
|
||||
}
|
||||
return &Certificate{
|
||||
Certificate: certBlock.Bytes,
|
||||
PrivateKey: keyBlock.Bytes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Certificate) ToPEM() ([]byte, []byte) {
|
||||
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: c.Certificate}),
|
||||
pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: c.PrivateKey})
|
||||
}
|
||||
|
||||
type Option func(*x509.Certificate)
|
||||
|
||||
func Authority(isCA bool) Option {
|
||||
return func(cert *x509.Certificate) {
|
||||
cert.IsCA = isCA
|
||||
}
|
||||
}
|
||||
|
||||
func NotBefore(t time.Time) Option {
|
||||
return func(c *x509.Certificate) {
|
||||
c.NotBefore = t
|
||||
}
|
||||
}
|
||||
|
||||
func NotAfter(t time.Time) Option {
|
||||
return func(c *x509.Certificate) {
|
||||
c.NotAfter = t
|
||||
}
|
||||
}
|
||||
|
||||
func DNSNames(names ...string) Option {
|
||||
return func(c *x509.Certificate) {
|
||||
c.DNSNames = names
|
||||
}
|
||||
}
|
||||
|
||||
func CommonName(name string) Option {
|
||||
return func(c *x509.Certificate) {
|
||||
c.Subject.CommonName = name
|
||||
}
|
||||
}
|
||||
|
||||
func KeyUsage(usage x509.KeyUsage) Option {
|
||||
return func(c *x509.Certificate) {
|
||||
c.KeyUsage = usage
|
||||
}
|
||||
}
|
||||
|
||||
func Organization(org string) Option {
|
||||
return func(c *x509.Certificate) {
|
||||
c.Subject.Organization = []string{org}
|
||||
}
|
||||
}
|
||||
|
||||
func MustGenerate(parent *Certificate, opts ...Option) *Certificate {
|
||||
cert, err := Generate(parent, opts...)
|
||||
common.Must(err)
|
||||
return cert
|
||||
}
|
||||
|
||||
func publicKey(priv interface{}) interface{} {
|
||||
switch k := priv.(type) {
|
||||
case *rsa.PrivateKey:
|
||||
return &k.PublicKey
|
||||
case *ecdsa.PrivateKey:
|
||||
return &k.PublicKey
|
||||
case ed25519.PrivateKey:
|
||||
return k.Public().(ed25519.PublicKey)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func Generate(parent *Certificate, opts ...Option) (*Certificate, error) {
|
||||
var (
|
||||
pKey interface{}
|
||||
parentKey interface{}
|
||||
err error
|
||||
)
|
||||
// higher signing performance than RSA2048
|
||||
selfKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return nil, newError("failed to generate self private key").Base(err)
|
||||
}
|
||||
parentKey = selfKey
|
||||
if parent != nil {
|
||||
if _, e := asn1.Unmarshal(parent.PrivateKey, &ecPrivateKey{}); e == nil {
|
||||
pKey, err = x509.ParseECPrivateKey(parent.PrivateKey)
|
||||
} else if _, e := asn1.Unmarshal(parent.PrivateKey, &pkcs8{}); e == nil {
|
||||
pKey, err = x509.ParsePKCS8PrivateKey(parent.PrivateKey)
|
||||
} else if _, e := asn1.Unmarshal(parent.PrivateKey, &pkcs1PrivateKey{}); e == nil {
|
||||
pKey, err = x509.ParsePKCS1PrivateKey(parent.PrivateKey)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse parent private key").Base(err)
|
||||
}
|
||||
parentKey = pKey
|
||||
}
|
||||
|
||||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
||||
if err != nil {
|
||||
return nil, newError("failed to generate serial number").Base(err)
|
||||
}
|
||||
|
||||
template := &x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
NotBefore: time.Now().Add(time.Hour * -1),
|
||||
NotAfter: time.Now().Add(time.Hour),
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(template)
|
||||
}
|
||||
|
||||
parentCert := template
|
||||
if parent != nil {
|
||||
pCert, err := x509.ParseCertificate(parent.Certificate)
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse parent certificate").Base(err)
|
||||
}
|
||||
parentCert = pCert
|
||||
}
|
||||
|
||||
derBytes, err := x509.CreateCertificate(rand.Reader, template, parentCert, publicKey(selfKey), parentKey)
|
||||
if err != nil {
|
||||
return nil, newError("failed to create certificate").Base(err)
|
||||
}
|
||||
|
||||
privateKey, err := x509.MarshalPKCS8PrivateKey(selfKey)
|
||||
if err != nil {
|
||||
return nil, newError("Unable to marshal private key").Base(err)
|
||||
}
|
||||
|
||||
return &Certificate{
|
||||
Certificate: derBytes,
|
||||
PrivateKey: privateKey,
|
||||
}, nil
|
||||
}
|
92
common/protocol/tls/cert/cert_test.go
Normal file
92
common/protocol/tls/cert/cert_test.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package cert
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/common/task"
|
||||
)
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
err := generate(nil, true, true, "ca")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func generate(domainNames []string, isCA bool, jsonOutput bool, fileOutput string) error {
|
||||
commonName := "Xray Root CA"
|
||||
organization := "Xray Inc"
|
||||
|
||||
expire := time.Hour * 3
|
||||
|
||||
var opts []Option
|
||||
if isCA {
|
||||
opts = append(opts, Authority(isCA))
|
||||
opts = append(opts, KeyUsage(x509.KeyUsageCertSign|x509.KeyUsageKeyEncipherment|x509.KeyUsageDigitalSignature))
|
||||
}
|
||||
|
||||
opts = append(opts, NotAfter(time.Now().Add(expire)))
|
||||
opts = append(opts, CommonName(commonName))
|
||||
if len(domainNames) > 0 {
|
||||
opts = append(opts, DNSNames(domainNames...))
|
||||
}
|
||||
opts = append(opts, Organization(organization))
|
||||
|
||||
cert, err := Generate(nil, opts...)
|
||||
if err != nil {
|
||||
return newError("failed to generate TLS certificate").Base(err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
printJSON(cert)
|
||||
}
|
||||
|
||||
if len(fileOutput) > 0 {
|
||||
if err := printFile(cert, fileOutput); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type jsonCert struct {
|
||||
Certificate []string `json:"certificate"`
|
||||
Key []string `json:"key"`
|
||||
}
|
||||
|
||||
func printJSON(certificate *Certificate) {
|
||||
certPEM, keyPEM := certificate.ToPEM()
|
||||
jCert := &jsonCert{
|
||||
Certificate: strings.Split(strings.TrimSpace(string(certPEM)), "\n"),
|
||||
Key: strings.Split(strings.TrimSpace(string(keyPEM)), "\n"),
|
||||
}
|
||||
content, err := json.MarshalIndent(jCert, "", " ")
|
||||
common.Must(err)
|
||||
os.Stdout.Write(content)
|
||||
os.Stdout.WriteString("\n")
|
||||
}
|
||||
func printFile(certificate *Certificate, name string) error {
|
||||
certPEM, keyPEM := certificate.ToPEM()
|
||||
return task.Run(context.Background(), func() error {
|
||||
return writeFile(certPEM, name+"_cert.pem")
|
||||
}, func() error {
|
||||
return writeFile(keyPEM, name+"_key.pem")
|
||||
})
|
||||
}
|
||||
func writeFile(content []byte, name string) error {
|
||||
f, err := os.Create(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return common.Error2(f.Write(content))
|
||||
}
|
9
common/protocol/tls/cert/errors.generated.go
Normal file
9
common/protocol/tls/cert/errors.generated.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package cert
|
||||
|
||||
import "github.com/xtls/xray-core/v1/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
44
common/protocol/tls/cert/privateKey.go
Normal file
44
common/protocol/tls/cert/privateKey.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package cert
|
||||
|
||||
import (
|
||||
"crypto/x509/pkix"
|
||||
"encoding/asn1"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
type ecPrivateKey struct {
|
||||
Version int
|
||||
PrivateKey []byte
|
||||
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
|
||||
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
|
||||
}
|
||||
|
||||
type pkcs8 struct {
|
||||
Version int
|
||||
Algo pkix.AlgorithmIdentifier
|
||||
PrivateKey []byte
|
||||
// optional attributes omitted.
|
||||
}
|
||||
|
||||
type pkcs1AdditionalRSAPrime struct {
|
||||
Prime *big.Int
|
||||
|
||||
// We ignore these values because rsa will calculate them.
|
||||
Exp *big.Int
|
||||
Coeff *big.Int
|
||||
}
|
||||
|
||||
type pkcs1PrivateKey struct {
|
||||
Version int
|
||||
N *big.Int
|
||||
E int
|
||||
D *big.Int
|
||||
P *big.Int
|
||||
Q *big.Int
|
||||
// We ignore these values, if present, because rsa will calculate them.
|
||||
Dp *big.Int `asn1:"optional"`
|
||||
Dq *big.Int `asn1:"optional"`
|
||||
Qinv *big.Int `asn1:"optional"`
|
||||
|
||||
AdditionalPrimes []pkcs1AdditionalRSAPrime `asn1:"optional,omitempty"`
|
||||
}
|
146
common/protocol/tls/sniff.go
Normal file
146
common/protocol/tls/sniff.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package tls
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
)
|
||||
|
||||
type SniffHeader struct {
|
||||
domain string
|
||||
}
|
||||
|
||||
func (h *SniffHeader) Protocol() string {
|
||||
return "tls"
|
||||
}
|
||||
|
||||
func (h *SniffHeader) Domain() string {
|
||||
return h.domain
|
||||
}
|
||||
|
||||
var errNotTLS = errors.New("not TLS header")
|
||||
var errNotClientHello = errors.New("not client hello")
|
||||
|
||||
func IsValidTLSVersion(major, minor byte) bool {
|
||||
return major == 3
|
||||
}
|
||||
|
||||
// ReadClientHello returns server name (if any) from TLS client hello message.
|
||||
// https://github.com/golang/go/blob/master/src/crypto/tls/handshake_messages.go#L300
|
||||
func ReadClientHello(data []byte, h *SniffHeader) error {
|
||||
if len(data) < 42 {
|
||||
return common.ErrNoClue
|
||||
}
|
||||
sessionIDLen := int(data[38])
|
||||
if sessionIDLen > 32 || len(data) < 39+sessionIDLen {
|
||||
return common.ErrNoClue
|
||||
}
|
||||
data = data[39+sessionIDLen:]
|
||||
if len(data) < 2 {
|
||||
return common.ErrNoClue
|
||||
}
|
||||
// cipherSuiteLen is the number of bytes of cipher suite numbers. Since
|
||||
// they are uint16s, the number must be even.
|
||||
cipherSuiteLen := int(data[0])<<8 | int(data[1])
|
||||
if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
|
||||
return errNotClientHello
|
||||
}
|
||||
data = data[2+cipherSuiteLen:]
|
||||
if len(data) < 1 {
|
||||
return common.ErrNoClue
|
||||
}
|
||||
compressionMethodsLen := int(data[0])
|
||||
if len(data) < 1+compressionMethodsLen {
|
||||
return common.ErrNoClue
|
||||
}
|
||||
data = data[1+compressionMethodsLen:]
|
||||
|
||||
if len(data) == 0 {
|
||||
return errNotClientHello
|
||||
}
|
||||
if len(data) < 2 {
|
||||
return errNotClientHello
|
||||
}
|
||||
|
||||
extensionsLength := int(data[0])<<8 | int(data[1])
|
||||
data = data[2:]
|
||||
if extensionsLength != len(data) {
|
||||
return errNotClientHello
|
||||
}
|
||||
|
||||
for len(data) != 0 {
|
||||
if len(data) < 4 {
|
||||
return errNotClientHello
|
||||
}
|
||||
extension := uint16(data[0])<<8 | uint16(data[1])
|
||||
length := int(data[2])<<8 | int(data[3])
|
||||
data = data[4:]
|
||||
if len(data) < length {
|
||||
return errNotClientHello
|
||||
}
|
||||
|
||||
if extension == 0x00 { /* extensionServerName */
|
||||
d := data[:length]
|
||||
if len(d) < 2 {
|
||||
return errNotClientHello
|
||||
}
|
||||
namesLen := int(d[0])<<8 | int(d[1])
|
||||
d = d[2:]
|
||||
if len(d) != namesLen {
|
||||
return errNotClientHello
|
||||
}
|
||||
for len(d) > 0 {
|
||||
if len(d) < 3 {
|
||||
return errNotClientHello
|
||||
}
|
||||
nameType := d[0]
|
||||
nameLen := int(d[1])<<8 | int(d[2])
|
||||
d = d[3:]
|
||||
if len(d) < nameLen {
|
||||
return errNotClientHello
|
||||
}
|
||||
if nameType == 0 {
|
||||
serverName := string(d[:nameLen])
|
||||
// An SNI value may not include a
|
||||
// trailing dot. See
|
||||
// https://tools.ietf.org/html/rfc6066#section-3.
|
||||
if strings.HasSuffix(serverName, ".") {
|
||||
return errNotClientHello
|
||||
}
|
||||
h.domain = serverName
|
||||
return nil
|
||||
}
|
||||
d = d[nameLen:]
|
||||
}
|
||||
}
|
||||
data = data[length:]
|
||||
}
|
||||
|
||||
return errNotTLS
|
||||
}
|
||||
|
||||
func SniffTLS(b []byte) (*SniffHeader, error) {
|
||||
if len(b) < 5 {
|
||||
return nil, common.ErrNoClue
|
||||
}
|
||||
|
||||
if b[0] != 0x16 /* TLS Handshake */ {
|
||||
return nil, errNotTLS
|
||||
}
|
||||
if !IsValidTLSVersion(b[1], b[2]) {
|
||||
return nil, errNotTLS
|
||||
}
|
||||
headerLen := int(binary.BigEndian.Uint16(b[3:5]))
|
||||
if 5+headerLen > len(b) {
|
||||
return nil, common.ErrNoClue
|
||||
}
|
||||
|
||||
h := &SniffHeader{}
|
||||
err := ReadClientHello(b[5:5+headerLen], h)
|
||||
if err == nil {
|
||||
return h, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
161
common/protocol/tls/sniff_test.go
Normal file
161
common/protocol/tls/sniff_test.go
Normal file
|
@ -0,0 +1,161 @@
|
|||
package tls_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/xtls/xray-core/v1/common/protocol/tls"
|
||||
)
|
||||
|
||||
func TestTLSHeaders(t *testing.T) {
|
||||
cases := []struct {
|
||||
input []byte
|
||||
domain string
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
input: []byte{
|
||||
0x16, 0x03, 0x01, 0x00, 0xc8, 0x01, 0x00, 0x00,
|
||||
0xc4, 0x03, 0x03, 0x1a, 0xac, 0xb2, 0xa8, 0xfe,
|
||||
0xb4, 0x96, 0x04, 0x5b, 0xca, 0xf7, 0xc1, 0xf4,
|
||||
0x2e, 0x53, 0x24, 0x6e, 0x34, 0x0c, 0x58, 0x36,
|
||||
0x71, 0x97, 0x59, 0xe9, 0x41, 0x66, 0xe2, 0x43,
|
||||
0xa0, 0x13, 0xb6, 0x00, 0x00, 0x20, 0x1a, 0x1a,
|
||||
0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
|
||||
0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13,
|
||||
0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d,
|
||||
0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
|
||||
0x00, 0x7b, 0xba, 0xba, 0x00, 0x00, 0xff, 0x01,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00,
|
||||
0x14, 0x00, 0x00, 0x11, 0x63, 0x2e, 0x73, 0x2d,
|
||||
0x6d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
|
||||
0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x17, 0x00,
|
||||
0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00,
|
||||
0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04,
|
||||
0x01, 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x08,
|
||||
0x06, 0x06, 0x01, 0x02, 0x01, 0x00, 0x05, 0x00,
|
||||
0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12,
|
||||
0x00, 0x00, 0x00, 0x10, 0x00, 0x0e, 0x00, 0x0c,
|
||||
0x02, 0x68, 0x32, 0x08, 0x68, 0x74, 0x74, 0x70,
|
||||
0x2f, 0x31, 0x2e, 0x31, 0x00, 0x0b, 0x00, 0x02,
|
||||
0x01, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08,
|
||||
0xaa, 0xaa, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18,
|
||||
0xaa, 0xaa, 0x00, 0x01, 0x00,
|
||||
},
|
||||
domain: "c.s-microsoft.com",
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
input: []byte{
|
||||
0x16, 0x03, 0x01, 0x00, 0xee, 0x01, 0x00, 0x00,
|
||||
0xea, 0x03, 0x03, 0xe7, 0x91, 0x9e, 0x93, 0xca,
|
||||
0x78, 0x1b, 0x3c, 0xe0, 0x65, 0x25, 0x58, 0xb5,
|
||||
0x93, 0xe1, 0x0f, 0x85, 0xec, 0x9a, 0x66, 0x8e,
|
||||
0x61, 0x82, 0x88, 0xc8, 0xfc, 0xae, 0x1e, 0xca,
|
||||
0xd7, 0xa5, 0x63, 0x20, 0xbd, 0x1c, 0x00, 0x00,
|
||||
0x8b, 0xee, 0x09, 0xe3, 0x47, 0x6a, 0x0e, 0x74,
|
||||
0xb0, 0xbc, 0xa3, 0x02, 0xa7, 0x35, 0xe8, 0x85,
|
||||
0x70, 0x7c, 0x7a, 0xf0, 0x00, 0xdf, 0x4a, 0xea,
|
||||
0x87, 0x01, 0x14, 0x91, 0x00, 0x20, 0xea, 0xea,
|
||||
0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
|
||||
0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13,
|
||||
0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d,
|
||||
0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
|
||||
0x00, 0x81, 0x9a, 0x9a, 0x00, 0x00, 0xff, 0x01,
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00,
|
||||
0x16, 0x00, 0x00, 0x13, 0x77, 0x77, 0x77, 0x30,
|
||||
0x37, 0x2e, 0x63, 0x6c, 0x69, 0x63, 0x6b, 0x74,
|
||||
0x61, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x00,
|
||||
0x17, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
|
||||
0x0d, 0x00, 0x14, 0x00, 0x12, 0x04, 0x03, 0x08,
|
||||
0x04, 0x04, 0x01, 0x05, 0x03, 0x08, 0x05, 0x05,
|
||||
0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x00,
|
||||
0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x12, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0e,
|
||||
0x00, 0x0c, 0x02, 0x68, 0x32, 0x08, 0x68, 0x74,
|
||||
0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x75, 0x50,
|
||||
0x00, 0x00, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00,
|
||||
0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x9a, 0x9a,
|
||||
0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x8a, 0x8a,
|
||||
0x00, 0x01, 0x00,
|
||||
},
|
||||
domain: "www07.clicktale.net",
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
input: []byte{
|
||||
0x16, 0x03, 0x01, 0x00, 0xe6, 0x01, 0x00, 0x00, 0xe2, 0x03, 0x03, 0x81, 0x47, 0xc1,
|
||||
0x66, 0xd5, 0x1b, 0xfa, 0x4b, 0xb5, 0xe0, 0x2a, 0xe1, 0xa7, 0x87, 0x13, 0x1d, 0x11, 0xaa, 0xc6,
|
||||
0xce, 0xfc, 0x7f, 0xab, 0x94, 0xc8, 0x62, 0xad, 0xc8, 0xab, 0x0c, 0xdd, 0xcb, 0x20, 0x6f, 0x9d,
|
||||
0x07, 0xf1, 0x95, 0x3e, 0x99, 0xd8, 0xf3, 0x6d, 0x97, 0xee, 0x19, 0x0b, 0x06, 0x1b, 0xf4, 0x84,
|
||||
0x0b, 0xb6, 0x8f, 0xcc, 0xde, 0xe2, 0xd0, 0x2d, 0x6b, 0x0c, 0x1f, 0x52, 0x53, 0x13, 0x00, 0x08,
|
||||
0x13, 0x02, 0x13, 0x03, 0x13, 0x01, 0x00, 0xff, 0x01, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00, 0x0c,
|
||||
0x00, 0x0a, 0x00, 0x00, 0x07, 0x64, 0x6f, 0x67, 0x66, 0x69, 0x73, 0x68, 0x00, 0x0b, 0x00, 0x04,
|
||||
0x03, 0x00, 0x01, 0x02, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x1e,
|
||||
0x00, 0x19, 0x00, 0x18, 0x00, 0x23, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00,
|
||||
0x00, 0x0d, 0x00, 0x1e, 0x00, 0x1c, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x08, 0x07, 0x08, 0x08,
|
||||
0x08, 0x09, 0x08, 0x0a, 0x08, 0x0b, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06, 0x04, 0x01, 0x05, 0x01,
|
||||
0x06, 0x01, 0x00, 0x2b, 0x00, 0x07, 0x06, 0x7f, 0x1c, 0x7f, 0x1b, 0x7f, 0x1a, 0x00, 0x2d, 0x00,
|
||||
0x02, 0x01, 0x01, 0x00, 0x33, 0x00, 0x26, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x2f, 0x35, 0x0c,
|
||||
0xb6, 0x90, 0x0a, 0xb7, 0xd5, 0xc4, 0x1b, 0x2f, 0x60, 0xaa, 0x56, 0x7b, 0x3f, 0x71, 0xc8, 0x01,
|
||||
0x7e, 0x86, 0xd3, 0xb7, 0x0c, 0x29, 0x1a, 0x9e, 0x5b, 0x38, 0x3f, 0x01, 0x72,
|
||||
},
|
||||
domain: "dogfish",
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
input: []byte{
|
||||
0x16, 0x03, 0x01, 0x01, 0x03, 0x01, 0x00, 0x00,
|
||||
0xff, 0x03, 0x03, 0x3d, 0x89, 0x52, 0x9e, 0xee,
|
||||
0xbe, 0x17, 0x63, 0x75, 0xef, 0x29, 0xbd, 0x14,
|
||||
0x6a, 0x49, 0xe0, 0x2c, 0x37, 0x57, 0x71, 0x62,
|
||||
0x82, 0x44, 0x94, 0x8f, 0x6e, 0x94, 0x08, 0x45,
|
||||
0x7f, 0xdb, 0xc1, 0x00, 0x00, 0x3e, 0xc0, 0x2c,
|
||||
0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc, 0xa8,
|
||||
0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
|
||||
0xc0, 0x24, 0xc0, 0x28, 0x00, 0x6b, 0xc0, 0x23,
|
||||
0xc0, 0x27, 0x00, 0x67, 0xc0, 0x0a, 0xc0, 0x14,
|
||||
0x00, 0x39, 0xc0, 0x09, 0xc0, 0x13, 0x00, 0x33,
|
||||
0x00, 0x9d, 0x00, 0x9c, 0x13, 0x02, 0x13, 0x03,
|
||||
0x13, 0x01, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
|
||||
0x00, 0x2f, 0x00, 0xff, 0x01, 0x00, 0x00, 0x98,
|
||||
0x00, 0x00, 0x00, 0x10, 0x00, 0x0e, 0x00, 0x00,
|
||||
0x0b, 0x31, 0x30, 0x2e, 0x34, 0x32, 0x2e, 0x30,
|
||||
0x2e, 0x32, 0x34, 0x33, 0x00, 0x0b, 0x00, 0x04,
|
||||
0x03, 0x00, 0x01, 0x02, 0x00, 0x0a, 0x00, 0x0a,
|
||||
0x00, 0x08, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x19,
|
||||
0x00, 0x18, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d,
|
||||
0x00, 0x20, 0x00, 0x1e, 0x04, 0x03, 0x05, 0x03,
|
||||
0x06, 0x03, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06,
|
||||
0x04, 0x01, 0x05, 0x01, 0x06, 0x01, 0x02, 0x03,
|
||||
0x02, 0x01, 0x02, 0x02, 0x04, 0x02, 0x05, 0x02,
|
||||
0x06, 0x02, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17,
|
||||
0x00, 0x00, 0x00, 0x2b, 0x00, 0x09, 0x08, 0x7f,
|
||||
0x14, 0x03, 0x03, 0x03, 0x02, 0x03, 0x01, 0x00,
|
||||
0x2d, 0x00, 0x03, 0x02, 0x01, 0x00, 0x00, 0x28,
|
||||
0x00, 0x26, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20,
|
||||
0x13, 0x7c, 0x6e, 0x97, 0xc4, 0xfd, 0x09, 0x2e,
|
||||
0x70, 0x2f, 0x73, 0x5a, 0x9b, 0x57, 0x4d, 0x5f,
|
||||
0x2b, 0x73, 0x2c, 0xa5, 0x4a, 0x98, 0x40, 0x3d,
|
||||
0x75, 0x6e, 0xb4, 0x76, 0xf9, 0x48, 0x8f, 0x36,
|
||||
},
|
||||
domain: "10.42.0.243",
|
||||
err: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range cases {
|
||||
header, err := SniffTLS(test.input)
|
||||
if test.err {
|
||||
if err == nil {
|
||||
t.Errorf("Exepct error but nil in test %v", test)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("Expect no error but actually %s in test %v", err.Error(), test)
|
||||
}
|
||||
if header.Domain() != test.domain {
|
||||
t.Error("expect domain ", test.domain, " but got ", header.Domain())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue