Skip to content

Commit

Permalink
Print random port to log if port is equal to "0"
Browse files Browse the repository at this point in the history
  • Loading branch information
msoap committed Jul 19, 2018
1 parent d2f10d2 commit 023649e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions shell2http.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" https://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"
Expand All @@ -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
Expand Down
72 changes: 51 additions & 21 deletions shell2http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"mime/multipart"
"net"
"net/http"
"os"
"os/exec"
Expand All @@ -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
)

// ------------------------------------------------------------------

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 023649e

Please sign in to comment.