Ejemplo n.º 1
0
func lookupCountry(db *geoip2.Reader, ip net.IP) (string, error) {
	if db == nil {
		return "", nil
	}
	record, err := db.Country(ip)
	if err != nil {
		return "", err
	}
	if country, exists := record.Country.Names["en"]; exists {
		return country, nil
	}
	if country, exists := record.RegisteredCountry.Names["en"]; exists {
		return country, nil
	}
	return "", fmt.Errorf("could not determine country for IP: %s", ip)
}
Ejemplo n.º 2
0
func parse_travelers_logs(src string, maxmind *geo.Reader) (travelers map[string]Traveler, err error) {
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("parse_travelers_logs() -> %v", e)
		}
	}()
	travelers = make(map[string]Traveler)

	date := regexp.MustCompile(`^(201[1-5]/(0|1)[0-9])$`)
	email := regexp.MustCompile(`^  (\S.+@.+)$`)
	hits := regexp.MustCompile(`\s+([0-9]{1,10})\s([0-9].+)$`)
	fd, err := os.Open(src)
	if err != nil {
		panic(err)
	}
	defer fd.Close()
	scanner := bufio.NewScanner(fd)
	var (
		cdate       time.Time
		cemail, cip string
		chits       float64
	)
	for scanner.Scan() {
		if err := scanner.Err(); err != nil {
			panic(err)
		}
		if date.MatchString(scanner.Text()) {
			fields := date.FindAllStringSubmatch(scanner.Text(), -1)
			cdate, err = time.Parse("2006/01", fields[0][1])
			if err != nil {
				panic(err)
			}
		} else if email.MatchString(scanner.Text()) {
			fields := email.FindAllStringSubmatch(scanner.Text(), -1)
			cemail = fields[0][1]
		} else if hits.MatchString(scanner.Text()) {
			fields := hits.FindAllStringSubmatch(scanner.Text(), -1)
			chits, err = strconv.ParseFloat(fields[0][1], 64)
			if err != nil {
				panic(err)
			}
			cip = fields[0][2]
			record, err := maxmind.City(net.ParseIP(cip))
			if err != nil {
				panic(err)
			}

			var cloc = Location{
				IP:        cip,
				Date:      cdate,
				Weight:    chits,
				Latitude:  record.Location.Latitude,
				Longitude: record.Location.Longitude,
				Locality:  record.City.Names["en"] + ", " + record.Country.Names["en"],
			}
			var tvl Traveler
			if _, ok := travelers[cemail]; !ok {
				tvl.Locations = append(tvl.Locations, cloc)
				tvl.ID = cemail
			} else {
				tvl = travelers[cemail]
				tvl.Locations = append(tvl.Locations, cloc)
			}
			travelers[cemail] = tvl
		}
	}
	return
}