177 lines
4.5 KiB
Go
177 lines
4.5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"container/list"
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"math/rand"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/gorilla/mux"
|
||
|
"github.com/gorilla/websocket"
|
||
|
"github.com/oschwald/geoip2-golang"
|
||
|
)
|
||
|
|
||
|
var upgrader = websocket.Upgrader{CheckOrigin: fakeOrigin} // use default options
|
||
|
|
||
|
func fakeOrigin(r *http.Request) bool {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func IpV4Address() string {
|
||
|
blocks := []string{}
|
||
|
for i := 0; i < 4; i++ {
|
||
|
number := rand.Intn(255)
|
||
|
blocks = append(blocks, strconv.Itoa(number))
|
||
|
}
|
||
|
|
||
|
return strings.Join(blocks, ".")
|
||
|
}
|
||
|
|
||
|
// type Alert struct {
|
||
|
// Latitude string `json:"latitude"`
|
||
|
// Longitude string `json:"longitude"`
|
||
|
// Countrycode string `json:"countrycode"`
|
||
|
// Country string `json:"country"`
|
||
|
// City string `json:"city"`
|
||
|
// Org string `json:"org"`
|
||
|
// Latitude2 string `json:"latitude2"`
|
||
|
// Longitude2 string `json:"longitude2"`
|
||
|
// Countrycode2 string `json:"countrycode2"`
|
||
|
// Country2 string `json:"country2"`
|
||
|
// City2 string `json:"city2"`
|
||
|
// Type string `json:"type"`
|
||
|
// Md5 string `json:"md5"`
|
||
|
// Dport string `json:"dport"`
|
||
|
// Zerg string `json:"zerg"`
|
||
|
// }
|
||
|
|
||
|
// var PORTS = []int64{21, 22, 80, 443}
|
||
|
|
||
|
// func (a Attack) New() Attack {
|
||
|
// attacker_ip := net.ParseIP(IpV4Address())
|
||
|
// c, _ := MMDB_CITY.City(attacker_ip)
|
||
|
// o, _ := MMDB_ASN.ASN(attacker_ip)
|
||
|
|
||
|
// a.Latitude = strconv.FormatFloat(c.Location.Latitude, 'f', 2, 64)
|
||
|
// a.Longitude = strconv.FormatFloat(c.Location.Longitude, 'f', 2, 64)
|
||
|
// a.Countrycode = c.Country.IsoCode
|
||
|
// a.Country = c.Country.IsoCode
|
||
|
// a.City = c.City.Names["en"]
|
||
|
// a.Org = o.AutonomousSystemOrganization
|
||
|
|
||
|
// destination := net.ParseIP("88.198.117.66")
|
||
|
// c, _ = MMDB_CITY.City(destination)
|
||
|
|
||
|
// a.Latitude2 = strconv.FormatFloat(c.Location.Latitude, 'f', 2, 64)
|
||
|
// a.Longitude2 = strconv.FormatFloat(c.Location.Longitude, 'f', 2, 64)
|
||
|
// a.Countrycode2 = c.Country.IsoCode
|
||
|
// a.Country2 = c.Country.IsoCode
|
||
|
// a.City2 = c.City.Names["en"]
|
||
|
// a.Type = "ipviking.honey"
|
||
|
// a.Md5 = attacker_ip.String()
|
||
|
// a.Dport = strconv.FormatInt(PORTS[rand.Int()%len(PORTS)], 10)
|
||
|
// a.Zerg = "rush"
|
||
|
// return a
|
||
|
// }
|
||
|
|
||
|
type Source struct {
|
||
|
AS_Name string `json:"as_name"`
|
||
|
AS_Number string `json:"as_number"`
|
||
|
CN string `json:"cn"`
|
||
|
IP string `json:"ip"`
|
||
|
Latitude float64 `json:"latitude"`
|
||
|
Longitude float64 `json:"longitude"`
|
||
|
Scope string `json:"scope"`
|
||
|
Value string `json:"value"`
|
||
|
}
|
||
|
|
||
|
type Destination struct {
|
||
|
Latitude float64 `json:"latitude"`
|
||
|
Longitude float64 `json:"longitude"`
|
||
|
CountryCode string `json:"countrycode"`
|
||
|
Country string `json:"country"`
|
||
|
City string `json:"city"`
|
||
|
}
|
||
|
|
||
|
type Alert struct {
|
||
|
Capacity int `json:"capacity"`
|
||
|
Source Source `json:"source"`
|
||
|
Destination Destination `json:"destination"`
|
||
|
}
|
||
|
|
||
|
func (a Alert) setDestination(ip string) Alert {
|
||
|
destination := net.ParseIP("88.198.117.66")
|
||
|
c, _ := MMDB_CITY.City(destination)
|
||
|
|
||
|
a.Destination.Latitude = c.Location.Latitude
|
||
|
a.Destination.Longitude = c.Location.Longitude
|
||
|
a.Destination.CountryCode = c.Country.IsoCode
|
||
|
a.Destination.Country = c.Country.IsoCode
|
||
|
a.Destination.City = c.City.Names["en"]
|
||
|
return a
|
||
|
}
|
||
|
|
||
|
var sockets = list.New()
|
||
|
|
||
|
// TODO: if you have a,b,c people and b leaves, a won't get the next alert but all after
|
||
|
func (a Alert) broadcast() {
|
||
|
log.Println(sockets.Len())
|
||
|
for s := sockets.Front(); s != nil; s = s.Next() {
|
||
|
b, _ := json.Marshal(a)
|
||
|
err := s.Value.(*websocket.Conn).WriteMessage(1, b)
|
||
|
if err != nil {
|
||
|
log.Println("Write error:", err)
|
||
|
sockets.Remove(s)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func alertHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
var alerts []Alert
|
||
|
err := json.NewDecoder(r.Body).Decode(&alerts)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
|
||
|
for _, a := range alerts {
|
||
|
a.setDestination("88.198.117.66").broadcast()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func socketHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||
|
if err != nil {
|
||
|
log.Print("Error during connection upgradation:", err)
|
||
|
return
|
||
|
}
|
||
|
log.Println("Connection opened", conn.RemoteAddr())
|
||
|
sockets.PushFront(conn)
|
||
|
}
|
||
|
|
||
|
var MMDB_CITY *geoip2.Reader
|
||
|
var MMDB_ASN *geoip2.Reader
|
||
|
|
||
|
func main() {
|
||
|
db, err := geoip2.Open("GeoLite2-City.mmdb")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
MMDB_CITY = db
|
||
|
|
||
|
db, err = geoip2.Open("GeoLite2-ASN.mmdb")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
MMDB_ASN = db
|
||
|
r := mux.NewRouter()
|
||
|
|
||
|
r.HandleFunc("/", socketHandler).Methods("GET")
|
||
|
r.HandleFunc("/", alertHandler).Methods("POST")
|
||
|
|
||
|
log.Fatal(http.ListenAndServe("127.0.0.1:9999", r))
|
||
|
}
|