diff options
author | 2018-06-29 17:38:31 +0100 | |
---|---|---|
committer | 2018-06-29 17:38:31 +0100 | |
commit | f3afd700210ffee655672b90b8cb78698f1d9e42 (patch) | |
tree | a1bd98fa546cab2b08d2bcb75d5d42d46b8fa253 /request | |
parent | 2fd31cd3e033161982e19a19bd132d2c5c59ea42 (diff) | |
download | coredns-f3afd700210ffee655672b90b8cb78698f1d9e42.tar.gz coredns-f3afd700210ffee655672b90b8cb78698f1d9e42.tar.zst coredns-f3afd700210ffee655672b90b8cb78698f1d9e42.zip |
request: Add LocalAddr() and LocalPort() (#1907)
These are used in the rewrite plugin, makes sense to have a common place
for them.
Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'request')
-rw-r--r-- | request/request.go | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/request/request.go b/request/request.go index ac6e9866c..f5e035a6e 100644 --- a/request/request.go +++ b/request/request.go @@ -47,7 +47,7 @@ func (r *Request) IP() string { return ip } -// Port gets the (remote) Port of the client making the request. +// Port gets the (remote) port of the client making the request. func (r *Request) Port() string { _, port, err := net.SplitHostPort(r.W.RemoteAddr().String()) if err != nil { @@ -56,11 +56,21 @@ func (r *Request) Port() string { return port } -// RemoteAddr returns the net.Addr of the client that sent the current request. -func (r *Request) RemoteAddr() string { - return r.W.RemoteAddr().String() +// LocalPort gets the local port of the server handling the request. +func (r *Request) LocalPort() string { + _, port, err := net.SplitHostPort(r.W.LocalAddr().String()) + if err != nil { + return "0" + } + return port } +// RemoteAddr returns the net.Addr of the client that sent the current request. +func (r *Request) RemoteAddr() string { return r.W.RemoteAddr().String() } + +// LocalAddr returns the net.Addr of the server handling the current request. +func (r *Request) LocalAddr() string { return r.W.LocalAddr().String() } + // Proto gets the protocol used as the transport. This will be udp or tcp. func (r *Request) Proto() string { return Proto(r.W) } |