about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--main.go25
1 files changed, 23 insertions, 2 deletions
diff --git a/main.go b/main.go
index b2b2ae4..bf1a021 100644
--- a/main.go
+++ b/main.go
@@ -3,19 +3,34 @@ package main
 import (
     "fmt"
     "github.com/gliderlabs/ssh"
+    "net/http"
     "log"
     "strings"
 )
 
+var(
+    metrics_num_passwords int
+)
+
 func main() {
-    ssh.Handle(handleConnection)
-    listenErr := ssh.ListenAndServe(":2222", nil)
+    log.Println("Starting SSH listener")
+    go func() {
+        ssh.Handle(handleConnection)
+        listenErr := ssh.ListenAndServe(":2222", nil)
+        if listenErr != nil {
+            log.Fatalln(listenErr.Error())
+        }
+    }()
 
+    log.Println("Starting HTTP metrics listener")
+    http.HandleFunc("/metrics", metricsHandler)
+    listenErr := http.ListenAndServe(":8080", nil)
     if listenErr != nil {
         log.Fatalln(listenErr.Error())
     }
 }
 
+// Handling incoming SSH connections
 func handleConnection(s ssh.Session) {
     // Set up buffer
     buf := make([]byte, 1)
@@ -39,6 +54,7 @@ func handleConnection(s ssh.Session) {
 
     if readErr == nil {
         // Print out pass
+        metrics_num_passwords++
         log.Printf("%s@%s: '%s'", s.User(), s.RemoteAddr().String(), strBuf)
     } else {
         // Read error - just log that.
@@ -48,3 +64,8 @@ func handleConnection(s ssh.Session) {
     // And close it.
     s.Exit(1)
 }
+
+// Handle HTTP /metrics requests
+func metricsHandler(w http.ResponseWriter, req *http.Request) {
+    fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords)
+}