diff options
-rw-r--r-- | main.go | 203 |
1 files changed, 116 insertions, 87 deletions
diff --git a/main.go b/main.go index abe0710..dbf8f93 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,20 @@ package main import ( - "fmt" - "net/http" - "log" - //"net" - "io/ioutil" - "encoding/json" - "strings" - "github.com/gliderlabs/ssh" + "encoding/json" + "fmt" + "github.com/gliderlabs/ssh" + //"github.com/gorilla/mux" + "io/ioutil" + "log" + "net/http" + "strings" ) var ( - metrics_num_passwords int - metrics_country_num map[string]int + metrics_num_passwords int + metrics_city_num map[string]int + cities map[string]location ) type geoipresult struct { @@ -33,93 +34,121 @@ type geoipresult struct { As string `json:"as"` } +type location struct { + key string `json:"key"` + latitude float64 `json:"latitude"` + longitude float64 `json:"longitude"` + name string `json:"name"` +} + func main() { - metrics_country_num = make(map[string]int) - - // start the ssh server - log.Println("Starting SSH listener") - go func() { - listenErr := ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(handlePass)) - if listenErr != nil { - log.Fatalln(listenErr.Error()) - } - }() - - // start the http server logging the metrics - log.Println("Starting HTTP metrics listener") - http.HandleFunc("/", indexHandler) - http.HandleFunc("/metrics", metricsHandler) - listenErr := http.ListenAndServe(":8080", nil) - if listenErr != nil { - log.Fatalln(listenErr.Error()) - } + metrics_city_num = make(map[string]int) + cities = make(map[string]location) + + // start the ssh server + log.Println("Starting SSH listener") + go func() { + listenErr := ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(handlePass)) + if listenErr != nil { + log.Fatalln(listenErr.Error()) + } + }() + + // start the http server logging the metrics + log.Println("Starting HTTP metrics listener") + http.HandleFunc("/", indexHandler) + http.HandleFunc("/metrics", metricsHandler) + http.HandleFunc("/locations", locationHandlerEndpoint) + listenErr := http.ListenAndServe(":8084", nil) + if listenErr != nil { + log.Fatalln(listenErr.Error()) + } +} + +func locationHandlerEndpoint(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Access-Control-Allow-Origin", "https://grafana.nbg1.emile.space") + fmt.Fprintf(w, "%s", "[") + var i int = 0 + for _, v := range cities { + fmt.Fprintf(w, "{") + fmt.Fprintf(w, "\"key\": \"%s\",", v.key) + fmt.Fprintf(w, "\"latitude\": %f,", v.latitude) + fmt.Fprintf(w, "\"longitude\": %f,", v.longitude) + fmt.Fprintf(w, "\"name\": \"%s\"", v.name) + if i == len(cities) - 1 { + fmt.Fprintf(w, "}") + } else { + fmt.Fprintf(w, "},") + } + i++ + } + fmt.Fprintf(w, "%s", "]") } -// Handling incoming SSH connections +// Handling incoming SSH conn95.216.207.95/32ections func handlePass(ctx ssh.Context, pass string) bool { - metrics_num_passwords++ - log.Printf("%s@%s: '%s'", ctx.User(), ctx.RemoteAddr().String(), pass) - - log.Printf("Parsing ip address...") - //ip := net.ParseIP(ctx.RemoteAddr().String()) - stringip := strings.Split(ctx.RemoteAddr().String(), ":")[0] - log.Printf("Remote ip: %s", stringip) - log.Printf("Done") - - // Define the request string - requestString := fmt.Sprintf("%s%s", "http://ip-api.com/json/", stringip) - - fmt.Println(requestString) - - // Send the GET request - resp, err := http.Get(requestString) - if err != nil { - log.Fatal(err) - } - - if resp.StatusCode != 200 { - return false - } - - // Read the response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - // Unmarshal the response to json - var result geoipresult - err = json.Unmarshal(body, &result) - if err != nil { - fmt.Println("JSON ERROR, abort mission!") - log.Fatal(err) - } - - log.Printf("country: %s", result.CountryCode) - - if metrics_country_num[result.CountryCode] == 0 { - log.Println("Map is empty, initializing...") - metrics_country_num[result.CountryCode] = 1 - log.Println("Done") - } else { - log.Println("Map is initialized, incrementing...") - metrics_country_num[result.CountryCode] += 1 - log.Println("Done") - } - log.Printf("Done") - - return false + metrics_num_passwords++ + log.Printf("%s@%s: '%s'", ctx.User(), ctx.RemoteAddr().String(), pass) + + stringip := strings.Split(ctx.RemoteAddr().String(), ":")[0] + + // Define the request string + requestString := fmt.Sprintf("%s%s", "http://ip-api.com/json/", stringip) + + // Send the GET request + resp, err := http.Get(requestString) + if err != nil { + log.Fatal(err) + } + + if resp.StatusCode != 200 { + return false + } + + // Read the response + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + + // Unmarshal the response to json + var result geoipresult + err = json.Unmarshal(body, &result) + if err != nil { + fmt.Println("JSON ERROR, abort mission!") + log.Fatal(err) + } + + if metrics_city_num[result.City] == 0 { + metrics_city_num[result.City] = 1 + } else { + metrics_city_num[result.City] += 1 + } + + if (cities[result.City] == location{}) { + newCity := location{ + key: strings.ToLower(result.City), + latitude: result.Lat, + longitude: result.Lon, + name: result.City, + } + + cities[result.City] = newCity + } + + return false } // Handle HTTP /metrics requests func metricsHandler(w http.ResponseWriter, req *http.Request) { - fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords) + fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords) - for k, v := range metrics_country_num { - fmt.Fprintf(w, "a_metric{country=\"%s\"} %d\n", k, v) - } + for k, v := range metrics_city_num { + fmt.Fprintf(w, "a_metric{city=\"%s\"} %d\n", strings.ToLower(k), v) + } } func indexHandler(w http.ResponseWriter, req *http.Request) { - _, _ = fmt.Fprintf(w, "<a href='/metrics'>metrics</a>") + _, _ = fmt.Fprintf(w, "<a href='/metrics'>metrics</a>") } |