|
5 | 5 | "fmt" |
6 | 6 | "goaway/internal/blacklist" |
7 | 7 | "log" |
| 8 | + "net" |
8 | 9 | "os" |
9 | 10 | "strings" |
10 | 11 | "time" |
@@ -48,9 +49,15 @@ type cachedRecord struct { |
48 | 49 | } |
49 | 50 |
|
50 | 51 | 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 |
54 | 61 | } |
55 | 62 |
|
56 | 63 | func NewDNSServer(config ServerConfig) (DNSServer, error) { |
@@ -104,35 +111,42 @@ func (s *DNSServer) Init() (int, *dns.Server) { |
104 | 111 | } |
105 | 112 |
|
106 | 113 | 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 | + |
107 | 122 | msg := new(dns.Msg) |
108 | 123 | msg.SetReply(r) |
109 | 124 | msg.Authoritative = true |
110 | 125 |
|
111 | 126 | for _, question := range r.Question { |
112 | | - // Record the request timestamp, domain name, and blocked status |
113 | 127 | timestamp := time.Now() |
114 | 128 | domain := strings.TrimSuffix(question.Name, ".") |
115 | 129 |
|
116 | 130 | // Check if the domain is blacklisted |
117 | 131 | if s.IsBlacklisted(question.Name) { |
118 | 132 | s.handleBlacklisted(w, msg, question.Name) |
119 | 133 | 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}, |
123 | 138 | }) |
124 | | - // Save the request log to file after each log entry |
125 | 139 | go s.SaveRequestLog(s.Config.RequestLogFile) |
126 | 140 | return |
127 | 141 | } |
128 | 142 |
|
129 | 143 | s.handleQuery(w, msg, question) |
130 | 144 | 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}, |
134 | 149 | }) |
135 | | - // Save the request log to file after each log entry |
136 | 150 | go s.SaveRequestLog(s.Config.RequestLogFile) |
137 | 151 | } |
138 | 152 |
|
|
0 commit comments