From 023649e3d2c010ca3dd3635bcdacb9245ffceb8a Mon Sep 17 00:00:00 2001 From: Sergey Mudrik Date: Thu, 19 Jul 2018 10:51:42 +0300 Subject: [PATCH] Print random port to log if port is equal to "0" --- README.md | 2 +- doc.go | 2 +- shell2http.1 | 4 +-- shell2http.go | 72 ++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index d9b9500..ac5881b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Usage shell2http [options] /path "shell command" /path2 "shell command2" ... options: -host="host" : host IP for http server (default bind to all interfaces) - -port=NNNN : port for http server (default 8080) + -port=NNNN : port for http server, 0 - to receive a random port (default 8080) -form : parse query into environment vars, handle uploaded files -cgi : run scripts in CGI-mode: - set environment variables with HTTP-request information diff --git a/doc.go b/doc.go index c8ce3e0..e1e9405 100644 --- a/doc.go +++ b/doc.go @@ -17,7 +17,7 @@ Usage: shell2http [options] /path "shell command" /path2 "shell command2" ... options: -host="host" : host for http server, default - all interfaces - -port=NNNN : port for http server, default - 8080 + -port=NNNN : port for http server, 0 - to receive a random port, default - 8080 -form : parse query into environment vars, handle uploaded files -cgi : run scripts in CGI-mode: - set environment variables with HTTP-request information diff --git a/shell2http.1 b/shell2http.1 index 9517a22..4f83c67 100644 --- a/shell2http.1 +++ b/shell2http.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "SHELL2HTTP" "" "May 2018" "" "" +.TH "SHELL2HTTP" "" "July 2018" "" "" HTTP\-server for executing shell commands\. Designed for develop, prototype or remote control\. Settings through two command line arguments, path and shell command\. By default bind to :8080\. . .SH "Usage" @@ -11,7 +11,7 @@ HTTP\-server for executing shell commands\. Designed for develop, prototype or r shell2http [options] /path "shell command" /path2 "shell command2" \.\.\. options: \-host="host" : host IP for http server (default bind to all interfaces) - \-port=NNNN : port for http server (default 8080) + \-port=NNNN : port for http server, 0 \- to receive a random port (default 8080) \-form : parse query into environment vars, handle uploaded files \-cgi : run scripts in CGI\-mode: \- set environment variables with HTTP\-request information diff --git a/shell2http.go b/shell2http.go index ca40c84..2a71676 100644 --- a/shell2http.go +++ b/shell2http.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "log" "mime/multipart" + "net" "net/http" "os" "os/exec" @@ -25,23 +26,28 @@ import ( raphanuscommon "github.com/msoap/raphanus/common" ) -// VERSION - version -const VERSION = "1.13" +const ( + // VERSION - version + VERSION = "1.13" + + // PORT - default port for http-server + PORT = 8080 -// PORT - default port for http-server -const PORT = 8080 + // shBasicAuthVar - name of env var for basic auth credentials + shBasicAuthVar = "SH_BASIC_AUTH" -// shBasicAuthVar - name of env var for basic auth credentials -const shBasicAuthVar = "SH_BASIC_AUTH" + // defaultShellPOSIX - shell executable by default in POSIX systems + defaultShellPOSIX = "sh" -// defaultShellPOSIX - shell executable by default in POSIX systems -const defaultShellPOSIX = "sh" + // defaultShellWindows - shell executable by default in Windows + defaultShellWindows = "cmd" -// defaultShellWindows - shell executable by default in Windows -const defaultShellWindows = "cmd" + // defaultShellPlan9 - shell executable by default in Plan9 + defaultShellPlan9 = "rc" -// defaultShellPlan9 - shell executable by default in Plan9 -const defaultShellPlan9 = "rc" + maxHTTPCode = 1000 + maxMemoryForUploadFile = 65536 +) // ------------------------------------------------------------------ @@ -107,10 +113,31 @@ type Config struct { includeStderr bool // also returns output written to stderr (default is stdout only) } -const ( - maxHTTPCode = 1000 - maxMemoryForUploadFile = 65536 -) +// readableURL - get readable URL for logging +func (cnf Config) readableURL(addr net.Addr) string { + prefix := "http" + if len(cnf.cert) > 0 && len(cnf.key) > 0 { + prefix = "https" + } + + urlParts := strings.Split(addr.String(), ":") + if len(urlParts) == 0 { + log.Printf("listen address is invalid, port not found", addr.String()) + return fmt.Sprintf("%s//%s/", prefix, addr.String()) + } + + port := strconv.Itoa(cnf.port) + if port == "0" { + port = urlParts[len(urlParts)-1] + } + + host := cnf.host + if host == "" { + host = "localhost" + } + + return fmt.Sprintf("%s://%s:%s/", prefix, host, port) +} // ------------------------------------------------------------------ // getConfig - parse arguments @@ -754,13 +781,16 @@ func main() { log.Printf("register: %s (%s)\n", handler.path, handler.cmd) } - address := fmt.Sprintf("%s:%d", appConfig.host, appConfig.port) + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", appConfig.host, appConfig.port)) + if err != nil { + log.Fatal(err) + } + + log.Printf("listen %s\n", appConfig.readableURL(listener.Addr())) if len(appConfig.cert) > 0 && len(appConfig.key) > 0 { - log.Printf("listen https://%s/\n", address) - log.Fatal(http.ListenAndServeTLS(address, appConfig.cert, appConfig.key, nil)) + log.Fatal(http.ServeTLS(listener, nil, appConfig.cert, appConfig.key)) } else { - log.Printf("listen http://%s/\n", address) - log.Fatal(http.ListenAndServe(address, nil)) + log.Fatal(http.Serve(listener, nil)) } }