| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "codeberg.org/miekg/dns" |
| 11 | "github.com/jedisct1/dlog" |
| 12 | ) |
| 13 | |
| 14 | type PluginQueryLog struct { |
| 15 | logger io.Writer |
| 16 | format string |
| 17 | ignoredQtypes []string |
| 18 | ipCryptConfig *IPCryptConfig |
| 19 | } |
| 20 | |
| 21 | func (plugin *PluginQueryLog) Name() string { |
| 22 | return "query_log" |
| 23 | } |
| 24 | |
| 25 | func (plugin *PluginQueryLog) Description() string { |
| 26 | return "Log DNS queries." |
| 27 | } |
| 28 | |
| 29 | func (plugin *PluginQueryLog) Init(proxy *Proxy) error { |
| 30 | plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.queryLogFile) |
| 31 | plugin.format = proxy.queryLogFormat |
| 32 | plugin.ignoredQtypes = proxy.queryLogIgnoredQtypes |
| 33 | plugin.ipCryptConfig = proxy.ipCryptConfig |
| 34 | |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | func (plugin *PluginQueryLog) Drop() error { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | func (plugin *PluginQueryLog) Reload() error { |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) error { |
| 47 | clientIPStr, ok := ExtractClientIPStrEncrypted(pluginsState, plugin.ipCryptConfig) |
| 48 | if !ok { |
| 49 | // Ignore internal flow. |
| 50 | return nil |
| 51 | } |
| 52 | question := msg.Question[0] |
| 53 | qType, ok := dns.TypeToString[dns.RRToType(question)] |
| 54 | if !ok { |
| 55 | qType = fmt.Sprintf("%d", dns.RRToType(question)) |
| 56 | } |
| 57 | if len(plugin.ignoredQtypes) > 0 { |
| 58 | for _, ignoredQtype := range plugin.ignoredQtypes { |
| 59 | if strings.EqualFold(ignoredQtype, qType) { |
| 60 | return nil |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | qName := pluginsState.qName |
| 65 | |
| 66 | if pluginsState.cacheHit { |
| 67 | pluginsState.serverName = "-" |
| 68 | } else { |
| 69 | switch pluginsState.returnCode { |
| 70 | case PluginsReturnCodeSynth, PluginsReturnCodeCloak, PluginsReturnCodeParseError: |
| 71 | pluginsState.serverName = "-" |
| 72 | } |
| 73 | } |
| 74 | returnCode, ok := PluginsReturnCodeToString[pluginsState.returnCode] |
| 75 | if !ok { |
| 76 | returnCode = fmt.Sprintf("%d", pluginsState.returnCode) |
| 77 | } |
| 78 | |
| 79 | var requestDuration time.Duration |
| 80 | if !pluginsState.requestStart.IsZero() && !pluginsState.requestEnd.IsZero() { |
| 81 | requestDuration = pluginsState.requestEnd.Sub(pluginsState.requestStart) |
| 82 | } else { |
| 83 | // For incomplete queries, use timeout duration |
| 84 | requestDuration = pluginsState.timeout |
| 85 | } |
| 86 | |
| 87 | // Cap at timeout to handle system sleep/suspend |
| 88 | // Max: UDP + TCP, Dial + (write + read) |
| 89 | triedUDPTCPTimeout := 4 * pluginsState.timeout |
| 90 | if requestDuration > triedUDPTCPTimeout { |
| 91 | requestDuration = triedUDPTCPTimeout |
| 92 | } |
| 93 | relayName := pluginsState.relayName |
| 94 | if relayName == "" { |
| 95 | relayName = "-" |
| 96 | } |
| 97 | |
| 98 | var line string |
| 99 | if plugin.format == "tsv" { |
| 100 | now := time.Now() |
| 101 | year, month, day := now.Date() |
| 102 | hour, minute, second := now.Clock() |
| 103 | tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second) |
| 104 | line = fmt.Sprintf( |
| 105 | "%s\t%s\t%s\t%s\t%s\t%dms\t%s\t%s\n", |
| 106 | tsStr, |
| 107 | clientIPStr, |
| 108 | StringQuote(qName), |
| 109 | qType, |
| 110 | returnCode, |
| 111 | requestDuration/time.Millisecond, |
| 112 | StringQuote(pluginsState.serverName), |
| 113 | StringQuote(relayName), |
| 114 | ) |
| 115 | } else if plugin.format == "ltsv" { |
| 116 | cached := 0 |
| 117 | if pluginsState.cacheHit { |
| 118 | cached = 1 |
| 119 | } |
| 120 | line = fmt.Sprintf("time:%d\thost:%s\tmessage:%s\ttype:%s\treturn:%s\tcached:%d\tduration:%d\tserver:%s\trelay:%s\n", |
| 121 | time.Now().Unix(), clientIPStr, StringQuote(qName), qType, returnCode, cached, requestDuration/time.Millisecond, StringQuote(pluginsState.serverName), StringQuote(relayName)) |
| 122 | } else { |
| 123 | dlog.Fatalf("Unexpected log format: [%s]", plugin.format) |
| 124 | } |
| 125 | if plugin.logger == nil { |
| 126 | return errors.New("Log file not initialized") |
| 127 | } |
| 128 | _, _ = plugin.logger.Write([]byte(line)) |
| 129 | |
| 130 | return nil |
| 131 | } |