about summary refs log tree commit diff
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2019-02-12 15:41:27 +0100
committermaride <maride@darknebu.la>2019-02-12 15:41:27 +0100
commit6e3eff089d765192054713d9653d2ab5f5bd7f46 (patch)
tree67f35b46484b89ee38c61c0b04b66dca707eb5a0
parent34e0f86d7eea9ed9cb891d20d0de63ce7e79dc0a (diff)
Add simple metrics HEAD master
-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)
+}