File: JsonFormat.go

package info (click to toggle)
golang-github-dnstap-golang-dnstap 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 240 kB
  • sloc: makefile: 17
file content (140 lines) | stat: -rw-r--r-- 3,820 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dnstap

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net"
	"time"

	"github.com/miekg/dns"
)

type jsonTime time.Time

func (jt *jsonTime) MarshalJSON() ([]byte, error) {
	stamp := time.Time(*jt).Format(time.RFC3339Nano)
	return []byte(fmt.Sprintf("\"%s\"", stamp)), nil
}

type jsonDnstap struct {
	Type     string      `json:"type"`
	Identity string      `json:"identity,omitempty"`
	Version  string      `json:"version,omitempty"`
	Message  jsonMessage `json:"message"`
}

type jsonMessage struct {
	Type            string    `json:"type"`
	QueryTime       *jsonTime `json:"query_time,omitempty"`
	ResponseTime    *jsonTime `json:"response_time,omitempty"`
	SocketFamily    string    `json:"socket_family,omitempty"`
	SocketProtocol  string    `json:"socket_protocol,omitempty"`
	QueryAddress    *net.IP   `json:"query_address,omitempty"`
	ResponseAddress *net.IP   `json:"response_address,omitempty"`
	QueryPort       uint32    `json:"query_port,omitempty"`
	ResponsePort    uint32    `json:"response_port,omitempty"`
	QueryZone       string    `json:"query_zone,omitempty"`
	QueryMessage    string    `json:"query_message,omitempty"`
	ResponseMessage string    `json:"response_message,omitempty"`
}

func convertJSONMessage(m *Message) jsonMessage {
	jMsg := jsonMessage{
		Type:           fmt.Sprint(m.Type),
		SocketFamily:   fmt.Sprint(m.SocketFamily),
		SocketProtocol: fmt.Sprint(m.SocketProtocol),
	}

	if m.QueryTimeSec != nil && m.QueryTimeNsec != nil {
		qt := jsonTime(time.Unix(int64(*m.QueryTimeSec), int64(*m.QueryTimeNsec)).UTC())
		jMsg.QueryTime = &qt
	}

	if m.ResponseTimeSec != nil && m.ResponseTimeNsec != nil {
		rt := jsonTime(time.Unix(int64(*m.ResponseTimeSec), int64(*m.ResponseTimeNsec)).UTC())
		jMsg.ResponseTime = &rt
	}

	if m.QueryAddress != nil {
		qa := net.IP(m.QueryAddress)
		jMsg.QueryAddress = &qa
	}

	if m.ResponseAddress != nil {
		ra := net.IP(m.ResponseAddress)
		jMsg.ResponseAddress = &ra
	}

	if m.QueryPort != nil {
		jMsg.QueryPort = *m.QueryPort
	}

	if m.ResponsePort != nil {
		jMsg.ResponsePort = *m.ResponsePort
	}

	if m.QueryZone != nil {
		name, _, err := dns.UnpackDomainName(m.QueryZone, 0)
		if err != nil {
			jMsg.QueryZone = fmt.Sprintf("parse failed: %v", err)
		} else {
			jMsg.QueryZone = string(name)
		}
	}

	if m.QueryMessage != nil {
		msg := new(dns.Msg)
		err := msg.Unpack(m.QueryMessage)
		if err != nil {
			jMsg.QueryMessage = fmt.Sprintf("parse failed: %v", err)
		} else {
			jMsg.QueryMessage = msg.String()
		}
	}

	if m.ResponseMessage != nil {
		msg := new(dns.Msg)
		err := msg.Unpack(m.ResponseMessage)
		if err != nil {
			jMsg.ResponseMessage = fmt.Sprintf("parse failed: %v", err)
		} else {
			jMsg.ResponseMessage = msg.String()
		}
	}
	return jMsg
}

// JSONFormat renders a Dnstap message in JSON format. Any encapsulated
// DNS messages are rendered as strings in a format similar to 'dig' output.
func JSONFormat(dt *Dnstap) (out []byte, ok bool) {
	var s bytes.Buffer

	j, err := json.Marshal(jsonDnstap{
		Type:     fmt.Sprint(dt.Type),
		Identity: string(dt.Identity),
		Version:  string(dt.Version),
		Message:  convertJSONMessage(dt.Message),
	})
	if err != nil {
		return nil, false
	}

	s.WriteString(string(j) + "\n")

	return s.Bytes(), true
}