From 28e430929d1cc664155b081405474ffcfb90d0e1 Mon Sep 17 00:00:00 2001 From: Randy Stauner Date: Thu, 3 Oct 2019 19:10:54 -0700 Subject: [PATCH] Pass local and remote addresses to exec: command --- CHANGELOG.md | 5 +++++ README.mkdn | 5 ++++- service/service.go | 2 +- test/dest-addr | 9 ++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3722c10..6d7f571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# v0.13 (2019-10-04) + +- Pass local and remote addresses to "exec:" commands. +- Don't error for destination addresses of less than 5 characters. + # v0.12 (2019-08-18) - Allow destination address to come from a command. diff --git a/README.mkdn b/README.mkdn index c9e5cc1..8cb49c3 100644 --- a/README.mkdn +++ b/README.mkdn @@ -155,7 +155,10 @@ to avoid having to use additional port numbers: - "exec:/full/path/to/command" (destination) Useful to get the destination address dynamically from a command. -It should print "host:port" on stdout and return zero. +The command will receive the following arguments: +- local address (host:port) +- remote address (host:port) +The command should print the destination "host:port" on stdout and return zero. If the command exits non-zero the status and stderr of the command will be printed to stdout and the connection will be dropped. diff --git a/service/service.go b/service/service.go index 0ca7066..e6606fe 100644 --- a/service/service.go +++ b/service/service.go @@ -63,7 +63,7 @@ func (s *Service) handleConnection(src *net.TCPConn, dst string, done chan bool) if len(dst) > 5 && dst[0:6] == "exec:/" { var stdout, stderr bytes.Buffer path := dst[5:len(dst)] - cmd := exec.Command(path) + cmd := exec.Command(path, src.LocalAddr().String(), src.RemoteAddr().String()) cmd.Stdout = &stdout cmd.Stderr = &stderr err := cmd.Run() diff --git a/test/dest-addr b/test/dest-addr index 29e1393..db8166f 100755 --- a/test/dest-addr +++ b/test/dest-addr @@ -1,3 +1,10 @@ #!/bin/bash -echo "localhost:${PROXY_PORT:-62000}" +# use same ip as request +host="${1%:*}" +if [[ -n "$host" ]]; then + echo "$host:${PROXY_PORT:-62000}" +else + echo "args: ($1) ($2)" >&2 + exit 1 +fi