sniffer_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package impl_test
  2. import (
  3. "testing"
  4. . "v2ray.com/core/app/dispatcher/impl"
  5. "v2ray.com/core/testing/assert"
  6. )
  7. func TestHTTPHeaders(t *testing.T) {
  8. assert := assert.On(t)
  9. cases := []struct {
  10. input string
  11. domain string
  12. err error
  13. }{
  14. {
  15. input: `GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
  16. Host: net.tutsplus.com
  17. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  18. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  19. Accept-Language: en-us,en;q=0.5
  20. Accept-Encoding: gzip,deflate
  21. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  22. Keep-Alive: 300
  23. Connection: keep-alive
  24. Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120
  25. Pragma: no-cache
  26. Cache-Control: no-cache`,
  27. domain: "net.tutsplus.com",
  28. err: nil,
  29. },
  30. {
  31. input: `POST /foo.php HTTP/1.1
  32. Host: localhost
  33. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  34. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  35. Accept-Language: en-us,en;q=0.5
  36. Accept-Encoding: gzip,deflate
  37. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  38. Keep-Alive: 300
  39. Connection: keep-alive
  40. Referer: http://localhost/test.php
  41. Content-Type: application/x-www-form-urlencoded
  42. Content-Length: 43
  43. first_name=John&last_name=Doe&action=Submit`,
  44. domain: "localhost",
  45. err: nil,
  46. },
  47. {
  48. input: `X /foo.php HTTP/1.1
  49. Host: localhost
  50. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  51. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  52. Accept-Language: en-us,en;q=0.5
  53. Accept-Encoding: gzip,deflate
  54. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  55. Keep-Alive: 300
  56. Connection: keep-alive
  57. Referer: http://localhost/test.php
  58. Content-Type: application/x-www-form-urlencoded
  59. Content-Length: 43
  60. first_name=John&last_name=Doe&action=Submit`,
  61. domain: "",
  62. err: ErrInvalidData,
  63. },
  64. {
  65. input: `GET /foo.php HTTP/1.1
  66. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  67. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  68. Accept-Language: en-us,en;q=0.5
  69. Accept-Encoding: gzip,deflate
  70. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  71. Keep-Alive: 300
  72. Connection: keep-alive
  73. Referer: http://localhost/test.php
  74. Content-Type: application/x-www-form-urlencoded
  75. Content-Length: 43
  76. Host: localhost
  77. first_name=John&last_name=Doe&action=Submit`,
  78. domain: "",
  79. err: ErrInvalidData,
  80. },
  81. {
  82. input: `GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1`,
  83. domain: "",
  84. err: ErrMoreData,
  85. },
  86. }
  87. for _, test := range cases {
  88. domain, err := SniffHTTP([]byte(test.input))
  89. assert.String(domain).Equals(test.domain)
  90. assert.Error(err).Equals(test.err)
  91. }
  92. }
  93. func TestTLSHeaders(t *testing.T) {
  94. assert := assert.On(t)
  95. cases := []struct {
  96. input []byte
  97. domain string
  98. err error
  99. }{
  100. {
  101. input: []byte{
  102. 0x16, 0x03, 0x01, 0x00, 0xc8, 0x01, 0x00, 0x00,
  103. 0xc4, 0x03, 0x03, 0x1a, 0xac, 0xb2, 0xa8, 0xfe,
  104. 0xb4, 0x96, 0x04, 0x5b, 0xca, 0xf7, 0xc1, 0xf4,
  105. 0x2e, 0x53, 0x24, 0x6e, 0x34, 0x0c, 0x58, 0x36,
  106. 0x71, 0x97, 0x59, 0xe9, 0x41, 0x66, 0xe2, 0x43,
  107. 0xa0, 0x13, 0xb6, 0x00, 0x00, 0x20, 0x1a, 0x1a,
  108. 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
  109. 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13,
  110. 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d,
  111. 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
  112. 0x00, 0x7b, 0xba, 0xba, 0x00, 0x00, 0xff, 0x01,
  113. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00,
  114. 0x14, 0x00, 0x00, 0x11, 0x63, 0x2e, 0x73, 0x2d,
  115. 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
  116. 0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x17, 0x00,
  117. 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00,
  118. 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04,
  119. 0x01, 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x08,
  120. 0x06, 0x06, 0x01, 0x02, 0x01, 0x00, 0x05, 0x00,
  121. 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12,
  122. 0x00, 0x00, 0x00, 0x10, 0x00, 0x0e, 0x00, 0x0c,
  123. 0x02, 0x68, 0x32, 0x08, 0x68, 0x74, 0x74, 0x70,
  124. 0x2f, 0x31, 0x2e, 0x31, 0x00, 0x0b, 0x00, 0x02,
  125. 0x01, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08,
  126. 0xaa, 0xaa, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18,
  127. 0xaa, 0xaa, 0x00, 0x01, 0x00,
  128. },
  129. domain: "c.s-microsoft.com",
  130. err: nil,
  131. },
  132. {
  133. input: []byte{
  134. 0x16, 0x03, 0x01, 0x00, 0xee, 0x01, 0x00, 0x00,
  135. 0xea, 0x03, 0x03, 0xe7, 0x91, 0x9e, 0x93, 0xca,
  136. 0x78, 0x1b, 0x3c, 0xe0, 0x65, 0x25, 0x58, 0xb5,
  137. 0x93, 0xe1, 0x0f, 0x85, 0xec, 0x9a, 0x66, 0x8e,
  138. 0x61, 0x82, 0x88, 0xc8, 0xfc, 0xae, 0x1e, 0xca,
  139. 0xd7, 0xa5, 0x63, 0x20, 0xbd, 0x1c, 0x00, 0x00,
  140. 0x8b, 0xee, 0x09, 0xe3, 0x47, 0x6a, 0x0e, 0x74,
  141. 0xb0, 0xbc, 0xa3, 0x02, 0xa7, 0x35, 0xe8, 0x85,
  142. 0x70, 0x7c, 0x7a, 0xf0, 0x00, 0xdf, 0x4a, 0xea,
  143. 0x87, 0x01, 0x14, 0x91, 0x00, 0x20, 0xea, 0xea,
  144. 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
  145. 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13,
  146. 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d,
  147. 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
  148. 0x00, 0x81, 0x9a, 0x9a, 0x00, 0x00, 0xff, 0x01,
  149. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00,
  150. 0x16, 0x00, 0x00, 0x13, 0x77, 0x77, 0x77, 0x30,
  151. 0x37, 0x2e, 0x63, 0x6c, 0x69, 0x63, 0x6b, 0x74,
  152. 0x61, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x00,
  153. 0x17, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
  154. 0x0d, 0x00, 0x14, 0x00, 0x12, 0x04, 0x03, 0x08,
  155. 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, 0x05, 0x05,
  156. 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x00,
  157. 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00,
  158. 0x00, 0x12, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0e,
  159. 0x00, 0x0c, 0x02, 0x68, 0x32, 0x08, 0x68, 0x74,
  160. 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x75, 0x50,
  161. 0x00, 0x00, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00,
  162. 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x9a, 0x9a,
  163. 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x8a, 0x8a,
  164. 0x00, 0x01, 0x00,
  165. },
  166. domain: "www07.clicktale.net",
  167. err: nil,
  168. },
  169. }
  170. for _, test := range cases {
  171. domain, err := SniffTLS(test.input)
  172. assert.String(domain).Equals(test.domain)
  173. assert.Error(err).Equals(test.err)
  174. }
  175. }