func NewWithGeoIP(filepath string) (*API, error) { db, err := geoip2.Open(filepath) if err != nil { return nil, err } return &API{db: db}, nil }
func maxmindInit() (err error) { maxmind, err = geo.Open(cfg.General.MaxMind) if err != nil { return err } logf("initialized maxmind db") return nil }
func NewGeoip(filename string) *geoip2.Reader { geoip, err := geoip2.Open(filename) if err != nil { panic(err) } return geoip }
func initMaxmind() { var err error cfg.Maxmind.Reader, err = geo.Open(cfg.Maxmind.DB) if err != nil { panic(err) } cfg.Maxmind.available = true return }
// Init() initializes a context from a configuration file into an // existing context struct func Init(path string, debug bool) (ctx Context, err error) { defer func() { if e := recover(); e != nil { err = fmt.Errorf("Init() -> %v", e) } }() ctx.Channels.Log = make(chan mig.Log, 37) err = gcfg.ReadFileInto(&ctx, path) if err != nil { panic(err) } ctx.Server.BaseURL = ctx.Server.Host + ctx.Server.BaseRoute ctx.Authentication.duration, err = time.ParseDuration(ctx.Authentication.TokenDuration) if err != nil { panic(err) } // Set the mode we will use to determine a client's public IP address if ctx.Server.ClientPublicIP == "" { ctx.Server.ClientPublicIP = "peer" } ctx.Server.ClientPublicIPOffset, err = parseClientPublicIP(ctx.Server.ClientPublicIP) if err != nil { fmt.Println(err) panic(err) } if debug { ctx.Logging.Level = "debug" ctx.Logging.Mode = "stdout" } ctx.Logging, err = mig.InitLogger(ctx.Logging, "mig-api") if err != nil { panic(err) } if ctx.Manifest.RequiredSignatures < 1 { panic("manifest:requiredsignatures must be at least 1 in config file") } ctx, err = initDB(ctx) if err != nil { panic(err) } if ctx.MaxMind.Path != "" { ctx.MaxMind.r, err = geo.Open(ctx.MaxMind.Path) if err != nil { panic(err) } } return }
func NewWithGeoIP(filepath string) (*API, error) { db, err := geoip2.Open(filepath) if err != nil { return nil, err } api := New() api.lookupCountry = func(ip net.IP) (string, error) { return lookupCountry(db, ip) } return api, nil }
func (l *IPLocatorPlugin) retriveLocationFromFsDB(ip string, ctx *apiplexy.APIContext) error { db, err := g.Open(l.pathToMmdbFile) if err != nil { return fmt.Errorf("GeoLite database not found") } defer db.Close() netIP := net.ParseIP(ip) city, err := db.City(netIP) if err != nil { return fmt.Errorf("No record for IP:" + ip) } ctx.Log["Location"] = city.Location return nil }
func main() { var maxdb = flag.String("m", "GeoIP2-City.mmdb", "Location of Maxmind database") var src = flag.String("i", "logfile.txt", "Source log file") var geoDistThreshold = flag.Float64("d", 5000.0, "Distance an IP must be from the geocenter to alert") var googleKey = flag.String("k", "", "Key to Google geocoding API") var mapsPath = flag.String("maps", "", "Create maps under the path given as argument. Maps are labelled <path>/<traveler id>.html") flag.Parse() maxmind, err := geo.Open(*maxdb) if err != nil { panic(err) } travelers, err := parse_travelers_logs(*src, maxmind) if err != nil { panic(err) } for email := range travelers { fmt.Println("Evaluating traveler", email) tvl := travelers[email] tvl.AlertDistance = *geoDistThreshold tvl.Geocenter, err = find_geocenter(tvl, *googleKey) if err != nil { panic(err) } for _, loc := range tvl.Locations { geodist := km_between_two_points(loc.Latitude, loc.Longitude, tvl.Geocenter.Latitude, tvl.Geocenter.Longitude) if geodist > tvl.AlertDistance { alert := fmt.Sprintf("in %s, %s connected from %s %.0f times; srcip='%s' (%.6f,%.6f) was %.0fkm away from usual connection center ", loc.Date.Format("2006/01"), email, loc.Locality, loc.Weight, loc.IP, loc.Latitude, loc.Longitude, geodist) if tvl.Geocenter.Locality != "" { alert += fmt.Sprintf("in %s ", tvl.Geocenter.Locality) } alert += fmt.Sprintf("(%.6f,%.6f)\n", tvl.Geocenter.Latitude, tvl.Geocenter.Longitude) fmt.Println(alert) tvl.Alerts = append(tvl.Alerts, alert) } } if *mapsPath != "" { make_traveler_map(tvl, *mapsPath) } } }
func consumer(src <-chan uint32, dst <-chan uint32, producer kafka.AsyncProducer) { topic := "network" db, err := geoip.Open("GeoLite2-City.mmdb") if err != nil { fmt.Println(err) } defer db.Close() for { src_ip := <-src src_str := intToIP(src_ip) dst_ip := <-dst dst_str := intToIP(dst_ip) src_ip_parsed := net.ParseIP(src_str) dst_ip_parsed := net.ParseIP(dst_str) src_record, err := db.City(src_ip_parsed) if err != nil { fmt.Println(err) } dst_record, err := db.City(dst_ip_parsed) if err != nil { fmt.Println(err) } src_coords := fmt.Sprintf("[%v,%v]", src_record.Location.Latitude, src_record.Location.Longitude) dst_coords := fmt.Sprintf("[%v,%v]", dst_record.Location.Latitude, dst_record.Location.Longitude) t := time.Now() current_time := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d-00:00", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) if (src_coords == "0,0") || dst_coords == "0,0" { continue } str := src_str + "," + dst_str + "," + "\"" + src_coords + "\"" + "," + "\"" + dst_coords + "\"" + "," + current_time fmt.Println(str) message := kafka.ProducerMessage{Topic: topic, Value: kafka.StringEncoder(str)} producer.Input() <- &message } }
// Init() initializes a context from a configuration file into an // existing context struct func Init(path string, debug bool) (ctx Context, err error) { defer func() { if e := recover(); e != nil { err = fmt.Errorf("Init() -> %v", e) } }() ctx.Channels.Log = make(chan mig.Log, 37) err = gcfg.ReadFileInto(&ctx, path) if err != nil { panic(err) } ctx.Server.BaseURL = ctx.Server.Host + ctx.Server.BaseRoute ctx.Authentication.duration, err = time.ParseDuration(ctx.Authentication.TokenDuration) if err != nil { panic(err) } if debug { ctx.Logging.Level = "debug" ctx.Logging.Mode = "stdout" } ctx.Logging, err = mig.InitLogger(ctx.Logging, "mig-api") if err != nil { panic(err) } ctx, err = initDB(ctx) if err != nil { panic(err) } if ctx.MaxMind.Path != "" { ctx.MaxMind.r, err = geo.Open(ctx.MaxMind.Path) if err != nil { panic(err) } } return }