sniffer_test.go 6.3 KB

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