about summary refs log tree commit diff
path: root/main.go
blob: 1517c79ce6866bc600788e4cc246467a1a5a3d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main

import (
    "fmt"
    "github.com/gliderlabs/ssh"
    "net/http"
    "log"
)

var(
    metrics_num_passwords int
)

func main() {
    log.Println("Starting SSH listener")
    go func() {
        listenErr := ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(handlePass))
        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 handlePass(ctx ssh.Context, pass string) bool {
    metrics_num_passwords++
    log.Printf("%s@%s: '%s'", ctx.User(), ctx.RemoteAddr().String(), pass)
    return false
}

// Handle HTTP /metrics requests
func metricsHandler(w http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords)
}