sniff.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package http
  2. import (
  3. "bytes"
  4. "errors"
  5. "strings"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/net"
  8. )
  9. type version byte
  10. const (
  11. HTTP1 version = iota
  12. HTTP2
  13. )
  14. type SniffHeader struct {
  15. version version
  16. host string
  17. }
  18. func (h *SniffHeader) Protocol() string {
  19. switch h.version {
  20. case HTTP1:
  21. return "http1"
  22. case HTTP2:
  23. return "http2"
  24. default:
  25. return "unknown"
  26. }
  27. }
  28. func (h *SniffHeader) Domain() string {
  29. return h.host
  30. }
  31. var (
  32. methods = [...]string{"get", "post", "head", "put", "delete", "options", "connect"}
  33. errNotHTTPMethod = errors.New("not an HTTP method")
  34. )
  35. func beginWithHTTPMethod(b []byte) error {
  36. for _, m := range &methods {
  37. if len(b) >= len(m) && strings.EqualFold(string(b[:len(m)]), m) {
  38. return nil
  39. }
  40. if len(b) < len(m) {
  41. return common.ErrNoClue
  42. }
  43. }
  44. return errNotHTTPMethod
  45. }
  46. func SniffHTTP(b []byte) (*SniffHeader, error) {
  47. if err := beginWithHTTPMethod(b); err != nil {
  48. return nil, err
  49. }
  50. sh := &SniffHeader{
  51. version: HTTP1,
  52. }
  53. headers := bytes.Split(b, []byte{'\n'})
  54. for i := 1; i < len(headers); i++ {
  55. header := headers[i]
  56. if len(header) == 0 {
  57. break
  58. }
  59. parts := bytes.SplitN(header, []byte{':'}, 2)
  60. if len(parts) != 2 {
  61. continue
  62. }
  63. key := strings.ToLower(string(parts[0]))
  64. if key == "host" {
  65. rawHost := strings.ToLower(string(bytes.TrimSpace(parts[1])))
  66. dest, err := ParseHost(rawHost, net.Port(80))
  67. if err != nil {
  68. return nil, err
  69. }
  70. sh.host = dest.Address.String()
  71. }
  72. }
  73. if len(sh.host) > 0 {
  74. return sh, nil
  75. }
  76. return nil, common.ErrNoClue
  77. }