sniff_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package tls_test
  2. import (
  3. "testing"
  4. "v2ray.com/core/common/compare"
  5. . "v2ray.com/core/common/protocol/tls"
  6. )
  7. func TestTLSHeaders(t *testing.T) {
  8. cases := []struct {
  9. input []byte
  10. domain string
  11. err bool
  12. }{
  13. {
  14. input: []byte{
  15. 0x16, 0x03, 0x01, 0x00, 0xc8, 0x01, 0x00, 0x00,
  16. 0xc4, 0x03, 0x03, 0x1a, 0xac, 0xb2, 0xa8, 0xfe,
  17. 0xb4, 0x96, 0x04, 0x5b, 0xca, 0xf7, 0xc1, 0xf4,
  18. 0x2e, 0x53, 0x24, 0x6e, 0x34, 0x0c, 0x58, 0x36,
  19. 0x71, 0x97, 0x59, 0xe9, 0x41, 0x66, 0xe2, 0x43,
  20. 0xa0, 0x13, 0xb6, 0x00, 0x00, 0x20, 0x1a, 0x1a,
  21. 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
  22. 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13,
  23. 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d,
  24. 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
  25. 0x00, 0x7b, 0xba, 0xba, 0x00, 0x00, 0xff, 0x01,
  26. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00,
  27. 0x14, 0x00, 0x00, 0x11, 0x63, 0x2e, 0x73, 0x2d,
  28. 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
  29. 0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x17, 0x00,
  30. 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00,
  31. 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04,
  32. 0x01, 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x08,
  33. 0x06, 0x06, 0x01, 0x02, 0x01, 0x00, 0x05, 0x00,
  34. 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12,
  35. 0x00, 0x00, 0x00, 0x10, 0x00, 0x0e, 0x00, 0x0c,
  36. 0x02, 0x68, 0x32, 0x08, 0x68, 0x74, 0x74, 0x70,
  37. 0x2f, 0x31, 0x2e, 0x31, 0x00, 0x0b, 0x00, 0x02,
  38. 0x01, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08,
  39. 0xaa, 0xaa, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18,
  40. 0xaa, 0xaa, 0x00, 0x01, 0x00,
  41. },
  42. domain: "c.s-microsoft.com",
  43. err: false,
  44. },
  45. {
  46. input: []byte{
  47. 0x16, 0x03, 0x01, 0x00, 0xee, 0x01, 0x00, 0x00,
  48. 0xea, 0x03, 0x03, 0xe7, 0x91, 0x9e, 0x93, 0xca,
  49. 0x78, 0x1b, 0x3c, 0xe0, 0x65, 0x25, 0x58, 0xb5,
  50. 0x93, 0xe1, 0x0f, 0x85, 0xec, 0x9a, 0x66, 0x8e,
  51. 0x61, 0x82, 0x88, 0xc8, 0xfc, 0xae, 0x1e, 0xca,
  52. 0xd7, 0xa5, 0x63, 0x20, 0xbd, 0x1c, 0x00, 0x00,
  53. 0x8b, 0xee, 0x09, 0xe3, 0x47, 0x6a, 0x0e, 0x74,
  54. 0xb0, 0xbc, 0xa3, 0x02, 0xa7, 0x35, 0xe8, 0x85,
  55. 0x70, 0x7c, 0x7a, 0xf0, 0x00, 0xdf, 0x4a, 0xea,
  56. 0x87, 0x01, 0x14, 0x91, 0x00, 0x20, 0xea, 0xea,
  57. 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
  58. 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13,
  59. 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d,
  60. 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
  61. 0x00, 0x81, 0x9a, 0x9a, 0x00, 0x00, 0xff, 0x01,
  62. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00,
  63. 0x16, 0x00, 0x00, 0x13, 0x77, 0x77, 0x77, 0x30,
  64. 0x37, 0x2e, 0x63, 0x6c, 0x69, 0x63, 0x6b, 0x74,
  65. 0x61, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x00,
  66. 0x17, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
  67. 0x0d, 0x00, 0x14, 0x00, 0x12, 0x04, 0x03, 0x08,
  68. 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, 0x05, 0x05,
  69. 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x00,
  70. 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x12, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0e,
  72. 0x00, 0x0c, 0x02, 0x68, 0x32, 0x08, 0x68, 0x74,
  73. 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x75, 0x50,
  74. 0x00, 0x00, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00,
  75. 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x9a, 0x9a,
  76. 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x8a, 0x8a,
  77. 0x00, 0x01, 0x00,
  78. },
  79. domain: "www07.clicktale.net",
  80. err: false,
  81. },
  82. }
  83. for _, test := range cases {
  84. header, err := SniffTLS(test.input)
  85. if test.err {
  86. if err == nil {
  87. t.Errorf("Exepct error but nil in test %v", test)
  88. }
  89. } else {
  90. if err != nil {
  91. t.Errorf("Expect no error but actually %s in test %v", err.Error(), test)
  92. }
  93. if err := compare.StringEqualWithDetail(header.Domain(), test.domain); err != nil {
  94. t.Error(err)
  95. }
  96. }
  97. }
  98. }