Skip to content

Commit 154e7d8

Browse files
committed
feat: include client information
1 parent a863b38 commit 154e7d8

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

internal/server/server.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"goaway/internal/blacklist"
77
"log"
8+
"net"
89
"os"
910
"strings"
1011
"time"
@@ -48,9 +49,15 @@ type cachedRecord struct {
4849
}
4950

5051
type RequestLogEntry struct {
51-
Timestamp time.Time `json:"timestamp"`
52-
Domain string `json:"domain"`
53-
Blocked bool `json:"blocked"`
52+
Timestamp time.Time `json:"timestamp"`
53+
Domain string `json:"domain"`
54+
Blocked bool `json:"blocked"`
55+
ClientInfo *Client `json:"client"`
56+
}
57+
58+
type Client struct {
59+
IP string
60+
Name string
5461
}
5562

5663
func NewDNSServer(config ServerConfig) (DNSServer, error) {
@@ -104,35 +111,42 @@ func (s *DNSServer) Init() (int, *dns.Server) {
104111
}
105112

106113
func (s *DNSServer) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
114+
clientIP := strings.Split(w.RemoteAddr().String(), ":")[0]
115+
var clientName = "None"
116+
117+
lookupNames, _ := net.LookupAddr(clientIP)
118+
if len(lookupNames) > 0 {
119+
clientName = lookupNames[0]
120+
}
121+
107122
msg := new(dns.Msg)
108123
msg.SetReply(r)
109124
msg.Authoritative = true
110125

111126
for _, question := range r.Question {
112-
// Record the request timestamp, domain name, and blocked status
113127
timestamp := time.Now()
114128
domain := strings.TrimSuffix(question.Name, ".")
115129

116130
// Check if the domain is blacklisted
117131
if s.IsBlacklisted(question.Name) {
118132
s.handleBlacklisted(w, msg, question.Name)
119133
s.RequestLog = append(s.RequestLog, RequestLogEntry{
120-
Timestamp: timestamp,
121-
Domain: domain,
122-
Blocked: true,
134+
Timestamp: timestamp,
135+
Domain: domain,
136+
Blocked: true,
137+
ClientInfo: &Client{IP: clientIP, Name: clientName},
123138
})
124-
// Save the request log to file after each log entry
125139
go s.SaveRequestLog(s.Config.RequestLogFile)
126140
return
127141
}
128142

129143
s.handleQuery(w, msg, question)
130144
s.RequestLog = append(s.RequestLog, RequestLogEntry{
131-
Timestamp: timestamp,
132-
Domain: domain,
133-
Blocked: false,
145+
Timestamp: timestamp,
146+
Domain: domain,
147+
Blocked: false,
148+
ClientInfo: &Client{IP: clientIP, Name: clientName},
134149
})
135-
// Save the request log to file after each log entry
136150
go s.SaveRequestLog(s.Config.RequestLogFile)
137151
}
138152

website/logs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<th>Timestamp</th>
6060
<th>Domain</th>
6161
<th>Blocked</th>
62+
<th>Client</th>
6263
</tr>
6364
</thead>
6465
<tbody></tbody>

website/static/js/logs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function populateLogTable(logs) {
2424
<td>${detail.timestamp}</td>
2525
<td>${detail.domain}</td>
2626
<td ${blockedClass}>${detail.blocked}</td>
27+
<td>${detail.client.Name} | ${detail.client.IP}</td>
2728
</tr>`,
2829
);
2930
});

0 commit comments

Comments
 (0)