-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
281 lines (213 loc) · 6.13 KB
/
main.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/TylerBrock/colorjson"
"github.com/spf13/viper"
)
var nextdnsProfile string
var nextdnsApiKey string
var count = 0
// NextDns create a struct to hold the response body
type NextDns struct {
Data []struct {
Timestamp time.Time `json:"timestamp"`
Domain string `json:"domain"`
Root string `json:"root"`
Type string `json:"type"`
Dnssec bool `json:"dnssec"`
Encrypted bool `json:"encrypted"`
Protocol string `json:"protocol"`
ClientIP string `json:"clientIp"`
Client string `json:"client"`
Device struct {
ID string `json:"id"`
Name string `json:"name"`
Model string `json:"model"`
LocalIP string `json:"localIp"`
} `json:"device"`
Status string `json:"status"`
Reasons []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"reasons"`
} `json:"data"`
Meta struct {
Pagination struct {
Cursor string `json:"cursor"`
} `json:"pagination"`
} `json:"meta"`
}
// Check error, exit if error
func check(e error) {
if e != nil {
fmt.Println(e)
panic(e)
}
}
// Check input arguments
func checkInput(inputArg string) string {
// Allowed input formats: -1h, 2022-09-01 and now
match, _ := regexp.MatchString("-[0-9]{1,3}[a-z]|now|[0-9]{4}-[0-9]{2}-[0-9]{2}", inputArg)
if !match {
fmt.Println("Invalid input: " + inputArg)
fmt.Println("Example: ./main stream, ./main download -1h now, ./main download -3d now")
os.Exit(1)
}
return inputArg
}
func streamRequest(client http.Client, f *os.File, keyword string) {
// Create URL string
url := "https://api.nextdns.io/profiles/" + nextdnsProfile + "/logs/stream?raw=1"
// Add optional search keyword to URL
if keyword != "" {
url += "&search=" + keyword
}
// Create HTTP GET request
req, err := http.NewRequest("GET", url, nil)
check(err)
// Add API key to request header
req.Header = http.Header{
"X-Api-Key": []string{nextdnsApiKey},
}
// Perform request and check for errors
res, err := client.Do(req)
check(err)
// Read response body
reader := bufio.NewReader(res.Body)
// Start indefinite loop through response records
for {
// Read line separated by newline
line, err := reader.ReadBytes('\n')
check(err)
// Return only JSON responses
match, _ := regexp.Compile("timestamp")
if match.Match(line) {
jsonstr := string(line[6 : len(line)-1])
_, err := f.WriteString(jsonstr + ",\n")
check(err)
// Create color formatter with indent
f := colorjson.NewFormatter()
f.Indent = 4
f.RawStrings = true
// Format JSON
var obj map[string]interface{}
json.Unmarshal([]byte(string(line[6:])), &obj)
// Marshall the colorized JSON
s, err := f.Marshal(obj)
check(err)
// Print the colorized JSON
fmt.Println(string(s))
}
}
}
// Get request to NextDNS API
func downloadRequest(client http.Client, cursor string, f *os.File, start string, end string) (string, int) {
// Create URL string
url := "https://api.nextdns.io/profiles/" + nextdnsProfile + "/logs?from=" + start + "&to=" + end + "&limit=1000&raw=1"
// Add optional cursor to URL
if cursor != "" && cursor != "empty" {
url = url + "&cursor=" + cursor
}
// Create HTTP GET request
req, err := http.NewRequest("GET", url, nil)
check(err)
// Add API key to request header
req.Header = http.Header{
"X-Api-Key": []string{nextdnsApiKey},
}
// Perform request and check for errors
res, err := client.Do(req)
check(err)
// Decode response body into struct
var p NextDns
err1 := json.NewDecoder(res.Body).Decode(&p)
check(err1)
var maxTs = 0
// Loop through response records
for _, record := range p.Data {
// Get max_ts timestamp
if int(record.Timestamp.Unix()) > maxTs {
maxTs = int(record.Timestamp.Unix())
}
// Marshal JSON
line, err := json.Marshal(record)
check(err)
// Write to file
_, err2 := f.WriteString(string(line) + ",\n")
check(err2)
// Increment counter
count++
}
// Return cursor and max_ts
returnToken := p.Meta.Pagination.Cursor
return returnToken, maxTs
}
// Main function
func main() {
// Read config file from .env
viper.SetConfigFile(".env")
viper.ReadInConfig()
// Set API key and profile
nextdnsApiKey = viper.GetString("nextdns_api_key")
nextdnsProfile = viper.GetString("nextdns_profile")
var maxTs int
var startDt string
var endDt string
// Get argument length from input
argLen := len(os.Args[1:])
// Create HTTP client
client := http.Client{}
// If stream argument without keyword given
if argLen == 1 && os.Args[1] == "stream" {
// Create file
f, err := os.Create("stream-all.log")
check(err)
defer f.Close()
fmt.Println("streaming logs to stream-all.log ...")
streamRequest(client, f, "")
} else if argLen == 2 && os.Args[1] == "stream" {
keyword := os.Args[2]
// Create file
f, err := os.Create("stream-" + keyword + ".log")
check(err)
defer f.Close()
// If stream and keyword argument given
fmt.Println("streaming logs with keyword: " + keyword + " to stream-" + keyword + ".log ...")
streamRequest(client, f, keyword)
} else if argLen == 3 && os.Args[1] == "download" {
// If download, check input and get start and end date
startDt = checkInput(os.Args[2])
endDt = checkInput(os.Args[3])
fmt.Println("download logs - start: ", startDt, " end: ", endDt+"\n")
// Create file
f, err := os.Create("download-output.log")
check(err)
defer f.Close()
// Iterate over NextDNS API cursors
cursor := "empty"
// If cursor is not empty, get next cursor
for cursor != "" {
// Get request
cursor, maxTs = downloadRequest(client, cursor, f, startDt, endDt)
// Convert max_ts to date
date := time.Unix(int64(maxTs), 0)
// Print progress
fmt.Printf("%v %v \n", count, date)
}
// Print total number of records
fmt.Println("\nDone with " + strconv.Itoa(count) + " records")
} else {
// If no arguments given, return error and quit
fmt.Println("Error: invalid input: " + strings.Join(os.Args[1:], " "))
fmt.Println("Example: ./main stream, ./main download -1h now, ./main download -3d now")
os.Exit(1)
}
}