Skip to content

Commit

Permalink
Merge pull request #2 from cego/add-dumpwriter
Browse files Browse the repository at this point in the history
Add dumpwriter
  • Loading branch information
Anders Brander authored Feb 14, 2020
2 parents 8384603 + d2c38fb commit ca46e39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The following tokens from OpenSSH are supported:
| --port | 22 | TCP port of the local SSH server |
| --use-syslog | true | Log to syslog |
| --guess-remote-ip | true | Try to guess remote IP. Requires root |
| --dump <path> | | Dump HTTP traffic to path |

## Implement the server-side

Expand Down
34 changes: 30 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/syslog"
"net"
"net/http"
"net/http/httputil"
"os"
"regexp"
"strconv"
Expand All @@ -31,19 +32,23 @@ var (
hostname string
port uint16

useSyslog = true
url string
guessRemoteIP bool
dumpPath string

rootCmd = &cobra.Command{
Use: appName,
Short: "A helper for OpenSSH's AuthorizedKeysCommand",
Run: root,
Use: appName,
PreRun: prerun,
Short: "A helper for OpenSSH's AuthorizedKeysCommand",
Run: root,
}

dumpWriter io.Writer
)

func init() {
helpFlag := false
useSyslog := true

// Trick to use '-h' for something else than help. This works by
// replacing the default help flag with one with no shorthand set.
Expand All @@ -64,7 +69,10 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&url, "url", "", "", "URL to use")
rootCmd.PersistentFlags().BoolVarP(&guessRemoteIP, "guess-remote-ip", "", true, "Try to guess remote IP. Requires root")
rootCmd.PersistentFlags().BoolVarP(&useSyslog, "use-syslog", "", useSyslog, "Log to syslog")
rootCmd.PersistentFlags().StringVarP(&dumpPath, "dump", "", "", "Dump HTTP request/response to path")
}

func prerun(_ *cobra.Command, _ []string) {
if useSyslog {
writer, err := syslog.New(syslog.LOG_ERR|syslog.LOG_AUTH, appName)
if err != nil {
Expand All @@ -73,6 +81,14 @@ func init() {

log.SetOutput(writer)
}

if dumpPath != "" {
w, err := os.OpenFile(dumpPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatalf("Error opening %s: %s", dumpPath, err.Error())
}
dumpWriter = w
}
}

// httpDo will try a http request multiple times if the server responds
Expand All @@ -84,12 +100,22 @@ func httpDo(req *http.Request) (*http.Response, error) {
panic("httpDo() only supports requests without body")
}

if dumpWriter != nil {
d, _ := httputil.DumpRequestOut(req, false)
_, _ = dumpWriter.Write(d)
}

for retryCount := 0; retryCount < 5; retryCount++ {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

if dumpWriter != nil {
d, _ := httputil.DumpResponse(resp, false)
_, _ = dumpWriter.Write(d)
}

if resp.StatusCode < 500 {
return resp, err
}
Expand Down

0 comments on commit ca46e39

Please sign in to comment.