summary refs log tree commit diff
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2019-02-15 17:13:22 +0100
committermaride <maride@darknebu.la>2019-02-15 17:13:22 +0100
commitf1bd309910e6540bbd6b40adee8def114f62ea85 (patch)
treee3cf9c1a6ccbfcfcd3b83a277af7359e55c48095
Init
-rw-r--r--main.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..92e2d9c
--- /dev/null
+++ b/main.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+    "fmt"
+    "net/http"
+    "log"
+)
+
+var(
+    metrics_num_passwords int
+)
+
+func main() {
+    log.Println("Starting HTTP listener")
+
+    http.HandleFunc("/", httpHandler)
+    http.HandleFunc("/metrics", metricsHandler)
+    listenErr := http.ListenAndServe(":80", nil) // set listen port
+    if listenErr != nil {
+        log.Fatalln(listenErr.Error())
+    }
+}
+
+// Handling incoming HTTP connections
+func httpHandler(w http.ResponseWriter, r *http.Request) {
+    // Raise stats
+    metrics_num_passwords++
+
+    // Log user/pass combo
+    user, pass, ok := r.BasicAuth()
+
+    if ok {
+    	// This also includes empty user/pass combos (if they are correctly encoded)
+    	// To avoid them, use `len(user) > 0 && len(pass) > 0`
+    	log.Printf("%s: %s %s:%s@%s%s", r.RemoteAddr, r.Method, user, pass, r.Host, r.URL.Path)
+    } else {
+    	log.Printf("%s: %s %s%s", r.RemoteAddr, r.Method, r.Host, r.URL.Path)
+    }
+
+    // Decline that try
+    w.Header().Set("WWW-Authenticate", `Basic realm="Protected Area"`)
+    w.WriteHeader(http.StatusUnauthorized)
+}
+
+// Handle HTTP /metrics requests
+func metricsHandler(w http.ResponseWriter, req *http.Request) {
+    fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords)
+}