mar.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package mar
  2. import (
  3. "io/ioutil"
  4. "path"
  5. "strings"
  6. )
  7. //go:generate go-bindata -ignore (.go|^\.) -o mar.gen.go -pkg mar ./...
  8. var FormatVersions = []string{"20150701", "20150702"}
  9. // Format returns the contents of the named embedded MAR file.
  10. // If the verison is not specified then latest version is returned.
  11. // Returns nil if the format does not exist.
  12. func Format(name, version string) []byte {
  13. // Return specific version, if specified.
  14. if version != "" {
  15. buf, _ := Asset(path.Join("formats", version, name+".mar"))
  16. return buf
  17. }
  18. // Otherwise iterate over versions from newest to oldest.
  19. for i := len(FormatVersions) - 1; i >= 0; i-- {
  20. if buf, _ := Asset(path.Join("formats", FormatVersions[i], name+".mar")); buf != nil {
  21. return buf
  22. }
  23. }
  24. return nil
  25. }
  26. // ReadFormat returns a built-in format, if it exists, or reads from a file.
  27. func ReadFormat(name string) ([]byte, error) {
  28. // Search built-in first.
  29. formatName, formatVersion := SplitFormat(name)
  30. if data := Format(formatName, formatVersion); data != nil {
  31. return data, nil
  32. }
  33. // Otherwise read from file.
  34. return ioutil.ReadFile(name)
  35. }
  36. // Formats returns a list of available built-in formats.
  37. // Excludes formats that are only to be spawned by other formats.
  38. func Formats() []string {
  39. return []string{
  40. "active_probing/ftp_pureftpd_10:20150701",
  41. "active_probing/http_apache_247:20150701",
  42. "active_probing/ssh_openssh_661:20150701",
  43. "dns_request:20150701",
  44. "dummy:20150701",
  45. "ftp_simple_blocking:20150701",
  46. "http_active_probing2:20150701",
  47. "http_active_probing:20150701",
  48. "http_probabilistic_blocking:20150701",
  49. "http_simple_blocking:20150701",
  50. "http_simple_blocking:20150702",
  51. "http_simple_blocking_with_msg_lens:20150701",
  52. "http_simple_nonblocking:20150701",
  53. "http_squid_blocking:20150701",
  54. "https_simple_blocking:20150701",
  55. "nmap/kpdyer.com:20150701",
  56. "smb_simple_nonblocking:20150701",
  57. "ssh_simple_nonblocking:20150701",
  58. "ta/amzn_sess:20150701",
  59. "udp_test_format:20150701",
  60. "web_sess443:20150701",
  61. "web_sess:20150701",
  62. }
  63. }
  64. // SplitFormat splits a fully qualified format name into it's name and version parts.
  65. func SplitFormat(s string) (name, version string) {
  66. a := strings.SplitN(s, ":", 2)
  67. if len(a) == 1 {
  68. return a[0], ""
  69. }
  70. return a[0], a[1]
  71. }
  72. // StripFormatVersion removes any version specified on a format.
  73. func StripFormatVersion(format string) string {
  74. if i := strings.Index(format, ":"); i != -1 {
  75. return format[:i]
  76. }
  77. return format
  78. }