diff options
author | 2018-02-28 18:15:12 -0800 | |
---|---|---|
committer | 2018-02-28 18:15:12 -0800 | |
commit | 5faa9e7bc178775e023f5bff1394e61bddf94eeb (patch) | |
tree | f511f3284c4409209918d2115eb6156478556144 /plugin | |
parent | 74825a46f21d2ab9d132e467e9c1d0c6cf9a52ee (diff) | |
download | coredns-5faa9e7bc178775e023f5bff1394e61bddf94eeb.tar.gz coredns-5faa9e7bc178775e023f5bff1394e61bddf94eeb.tar.zst coredns-5faa9e7bc178775e023f5bff1394e61bddf94eeb.zip |
plugin/log: log remote port addr as well (#1573)
Log the remote's port, for IPv6 addr this means we should enclose the v6
address in brackets; make this the default for 'remote'.
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/log/README.md | 4 | ||||
-rw-r--r-- | plugin/log/log.go | 2 | ||||
-rw-r--r-- | plugin/pkg/replacer/replacer.go | 10 |
3 files changed, 12 insertions, 4 deletions
diff --git a/plugin/log/README.md b/plugin/log/README.md index 26a79f7f5..c0c208534 100644 --- a/plugin/log/README.md +++ b/plugin/log/README.md @@ -59,7 +59,7 @@ The following place holders are supported: * `{class}`: qclass of the request * `{proto}`: protocol used (tcp or udp) * `{when}`: time of the query -* `{remote}`: client's IP address +* `{remote}`: client's IP address, for IPv6 addresses these are enclosed in brackets: `[::1]` * `{size}`: request size in bytes * `{port}`: client's port * `{duration}`: response duration @@ -75,7 +75,7 @@ The following place holders are supported: The default Common Log Format is: ~~~ txt -`{remote} - [{when}] {>id} "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}` +`{remote}:{port} - [{when}] {>id} "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}` ~~~ ## Examples diff --git a/plugin/log/log.go b/plugin/log/log.go index 247b7355d..26e825c8f 100644 --- a/plugin/log/log.go +++ b/plugin/log/log.go @@ -78,7 +78,7 @@ type Rule struct { const ( // CommonLogFormat is the common log format. - CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] {>id} "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}` + CommonLogFormat = `{remote}:{port} ` + CommonLogEmptyValue + ` [{when}] {>id} "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}` // CommonLogEmptyValue is the common empty log value. CommonLogEmptyValue = "-" // CombinedLogFormat is the combined log format. diff --git a/plugin/pkg/replacer/replacer.go b/plugin/pkg/replacer/replacer.go index c10022f79..1775f7c24 100644 --- a/plugin/pkg/replacer/replacer.go +++ b/plugin/pkg/replacer/replacer.go @@ -43,7 +43,7 @@ func New(r *dns.Msg, rr *dnstest.Recorder, emptyValue string) Replacer { return time.Now().Format(timeFormat) }(), "{size}": strconv.Itoa(req.Len()), - "{remote}": req.IP(), + "{remote}": addrToRFC3986(req.IP()), "{port}": req.Port(), }, emptyValue: emptyValue, @@ -155,6 +155,14 @@ func flagsToString(h dns.MsgHdr) string { return strings.Join(flags[:i], ",") } +// addrToRFC3986 will add brackets to the address if it is an IPv6 address. +func addrToRFC3986(addr string) string { + if strings.Contains(addr, ":") { + return "[" + addr + "]" + } + return addr +} + const ( timeFormat = "02/Jan/2006:15:04:05 -0700" headerReplacer = "{>" |