rule.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package mux
  2. type MatchFunc func(data []byte) (match bool)
  3. var (
  4. HttpsNeedBytesNum uint32 = 1
  5. HttpNeedBytesNum uint32 = 3
  6. YamuxNeedBytesNum uint32 = 2
  7. )
  8. var HttpsMatchFunc MatchFunc = func(data []byte) bool {
  9. if len(data) < int(HttpsNeedBytesNum) {
  10. return false
  11. }
  12. if data[0] == 0x16 {
  13. return true
  14. } else {
  15. return false
  16. }
  17. }
  18. // From https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
  19. var httpHeadBytes = map[string]struct{}{
  20. "GET": struct{}{},
  21. "HEA": struct{}{},
  22. "POS": struct{}{},
  23. "PUT": struct{}{},
  24. "DEL": struct{}{},
  25. "CON": struct{}{},
  26. "OPT": struct{}{},
  27. "TRA": struct{}{},
  28. "PAT": struct{}{},
  29. }
  30. var HttpMatchFunc MatchFunc = func(data []byte) bool {
  31. if len(data) < int(HttpNeedBytesNum) {
  32. return false
  33. }
  34. _, ok := httpHeadBytes[string(data[:3])]
  35. return ok
  36. }
  37. // From https://github.com/hashicorp/yamux/blob/master/spec.md
  38. var YamuxMatchFunc MatchFunc = func(data []byte) bool {
  39. if len(data) < int(YamuxNeedBytesNum) {
  40. return false
  41. }
  42. if data[0] == 0 && data[1] >= 0x0 && data[1] <= 0x3 {
  43. return true
  44. }
  45. return false
  46. }