JsonFormat.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package dnstap
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "net"
  20. "time"
  21. "github.com/miekg/dns"
  22. )
  23. type jsonTime time.Time
  24. func (jt *jsonTime) MarshalJSON() ([]byte, error) {
  25. stamp := time.Time(*jt).Format(time.RFC3339Nano)
  26. return []byte(fmt.Sprintf("\"%s\"", stamp)), nil
  27. }
  28. type jsonDnstap struct {
  29. Type string `json:"type"`
  30. Identity string `json:"identity,omitempty"`
  31. Version string `json:"version,omitempty"`
  32. Message jsonMessage `json:"message"`
  33. }
  34. type jsonMessage struct {
  35. Type string `json:"type"`
  36. QueryTime *jsonTime `json:"query_time,omitempty"`
  37. ResponseTime *jsonTime `json:"response_time,omitempty"`
  38. SocketFamily string `json:"socket_family,omitempty"`
  39. SocketProtocol string `json:"socket_protocol,omitempty"`
  40. QueryAddress *net.IP `json:"query_address,omitempty"`
  41. ResponseAddress *net.IP `json:"response_address,omitempty"`
  42. QueryPort uint32 `json:"query_port,omitempty"`
  43. ResponsePort uint32 `json:"response_port,omitempty"`
  44. QueryZone string `json:"query_zone,omitempty"`
  45. QueryMessage string `json:"query_message,omitempty"`
  46. ResponseMessage string `json:"response_message,omitempty"`
  47. }
  48. func convertJSONMessage(m *Message) jsonMessage {
  49. jMsg := jsonMessage{
  50. Type: fmt.Sprint(m.Type),
  51. SocketFamily: fmt.Sprint(m.SocketFamily),
  52. SocketProtocol: fmt.Sprint(m.SocketProtocol),
  53. }
  54. if m.QueryTimeSec != nil && m.QueryTimeNsec != nil {
  55. qt := jsonTime(time.Unix(int64(*m.QueryTimeSec), int64(*m.QueryTimeNsec)).UTC())
  56. jMsg.QueryTime = &qt
  57. }
  58. if m.ResponseTimeSec != nil && m.ResponseTimeNsec != nil {
  59. rt := jsonTime(time.Unix(int64(*m.ResponseTimeSec), int64(*m.ResponseTimeNsec)).UTC())
  60. jMsg.ResponseTime = &rt
  61. }
  62. if m.QueryAddress != nil {
  63. qa := net.IP(m.QueryAddress)
  64. jMsg.QueryAddress = &qa
  65. }
  66. if m.ResponseAddress != nil {
  67. ra := net.IP(m.ResponseAddress)
  68. jMsg.ResponseAddress = &ra
  69. }
  70. if m.QueryPort != nil {
  71. jMsg.QueryPort = *m.QueryPort
  72. }
  73. if m.ResponsePort != nil {
  74. jMsg.ResponsePort = *m.ResponsePort
  75. }
  76. if m.QueryZone != nil {
  77. name, _, err := dns.UnpackDomainName(m.QueryZone, 0)
  78. if err != nil {
  79. jMsg.QueryZone = fmt.Sprintf("parse failed: %v", err)
  80. } else {
  81. jMsg.QueryZone = string(name)
  82. }
  83. }
  84. if m.QueryMessage != nil {
  85. msg := new(dns.Msg)
  86. err := msg.Unpack(m.QueryMessage)
  87. if err != nil {
  88. jMsg.QueryMessage = fmt.Sprintf("parse failed: %v", err)
  89. } else {
  90. jMsg.QueryMessage = msg.String()
  91. }
  92. }
  93. if m.ResponseMessage != nil {
  94. msg := new(dns.Msg)
  95. err := msg.Unpack(m.ResponseMessage)
  96. if err != nil {
  97. jMsg.ResponseMessage = fmt.Sprintf("parse failed: %v", err)
  98. } else {
  99. jMsg.ResponseMessage = msg.String()
  100. }
  101. }
  102. return jMsg
  103. }
  104. // JSONFormat renders a Dnstap message in JSON format. Any encapsulated
  105. // DNS messages are rendered as strings in a format similar to 'dig' output.
  106. func JSONFormat(dt *Dnstap) (out []byte, ok bool) {
  107. var s bytes.Buffer
  108. j, err := json.Marshal(jsonDnstap{
  109. Type: fmt.Sprint(dt.Type),
  110. Identity: string(dt.Identity),
  111. Version: string(dt.Version),
  112. Message: convertJSONMessage(dt.Message),
  113. })
  114. if err != nil {
  115. return nil, false
  116. }
  117. s.WriteString(string(j) + "\n")
  118. return s.Bytes(), true
  119. }