From 62f3f0a3590839fc412175fb6cb0b5d9493d951b Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 19 May 2017 02:01:39 +0200 Subject: added inital geo-ip lookup --- src/hub/Makefile | 1 + src/hub/src/spreadspace.org/sfive-hub/s5hub.go | 3 +- src/hub/src/spreadspace.org/sfive/s5srv.go | 20 ++++++- src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go | 73 +++++++++++++++++++++++++ src/hub/test-srv-geoip | 15 +++++ 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go create mode 100755 src/hub/test-srv-geoip (limited to 'src/hub') diff --git a/src/hub/Makefile b/src/hub/Makefile index b7640f7..2d6da98 100644 --- a/src/hub/Makefile +++ b/src/hub/Makefile @@ -41,6 +41,7 @@ EXECUTEABLE := sfive-hub LIBS := "github.com/boltdb/bolt" \ "github.com/pborman/uuid" \ "github.com/Yawning/cryptopan" \ + "github.com/rainycape/geoip" \ "github.com/equinox0815/graphite-golang" diff --git a/src/hub/src/spreadspace.org/sfive-hub/s5hub.go b/src/hub/src/spreadspace.org/sfive-hub/s5hub.go index 9c58481..6411e1e 100644 --- a/src/hub/src/spreadspace.org/sfive-hub/s5hub.go +++ b/src/hub/src/spreadspace.org/sfive-hub/s5hub.go @@ -48,6 +48,7 @@ func main() { readOnly := flag.Bool("read-only", false, "open database in read-only mode") anonymize := flag.Bool("anonymize", false, "anonymize clients IP addresses using crypto-pan") anonKeyFile := flag.String("anonymization-key-file", "", "path to the file containing the key to be used for crypto-pan anonymization (default: use randomly generated key)") + geoipDB := flag.String("geoip-db", "", "path to the Geo-IP database") pipe := flag.String("pipe", "/var/run/sfive/pipe", "path to the unix pipe for the pipeserver") startPipe := flag.Bool("start-pipe-server", true, "start a connection oriented pipe server; see option pipe") pipegram := flag.String("pipegram", "/var/run/sfive/pipegram", "path to the unix datagram pipe for the pipeserver") @@ -72,7 +73,7 @@ func main() { return } - srv, err := sfive.NewServer(*db, *readOnly, *anonymize, *anonKeyFile) + srv, err := sfive.NewServer(*db, *readOnly, *anonymize, *anonKeyFile, *geoipDB) if err != nil { s5hl.Fatalf(err.Error()) } diff --git a/src/hub/src/spreadspace.org/sfive/s5srv.go b/src/hub/src/spreadspace.org/sfive/s5srv.go index 6b5f030..e9c1baa 100644 --- a/src/hub/src/spreadspace.org/sfive/s5srv.go +++ b/src/hub/src/spreadspace.org/sfive/s5srv.go @@ -52,6 +52,7 @@ type Server struct { store *Store numWorker int anonymization AnonymizationAlgo + geoip GeoIPLookup quit chan bool done *sync.WaitGroup appendChan chan appendToken @@ -64,7 +65,14 @@ func (srv Server) transform(update *UpdateFull) *UpdateFull { for _, client := range update.Data.Clients { bytesSentTotal += client.BytesSent - // TODO: add GeoIP lookup here + if srv.geoip != nil { + if rec, err := srv.geoip.Lookup(client.IP); err != nil { + s5l.Printf("transform: Geo-IP lookup failed: %v", err) + } else { + // TODO: actually store the record + s5l.Printf("transform: Geo-IP lookup; %s -> %s", client.IP, rec) + } + } if srv.anonymization != nil { if aIP, err := srv.anonymization.Anonymize(client.IP); err != nil { @@ -140,7 +148,7 @@ func (srv Server) Close() { s5l.Printf("server: finished") } -func NewServer(dbPath string, readOnly, anonymize bool, anonKeyfile string) (srv *Server, err error) { +func NewServer(dbPath string, readOnly, anonymize bool, anonKeyfile, geoipDB string) (srv *Server, err error) { // TODO: read configuration and create instance with correct settings srv = &Server{} if srv.store, err = NewStore(dbPath, readOnly); err != nil { @@ -155,6 +163,14 @@ func NewServer(dbPath string, readOnly, anonymize bool, anonKeyfile string) (srv s5l.Printf("using IP address anonymization: %s", srv.anonymization) } + if geoipDB != "" { + if srv.geoip, err = NewMaxMindGeoIP2(geoipDB); err != nil { + err = errors.New("failed to initialize Geo-IP Lookup: " + err.Error()) + return + } + s5l.Printf("using Geo-IP Lookup: %s", srv.geoip) + } + srv.numWorker = runtime.NumCPU() srv.quit = make(chan bool) srv.done = &sync.WaitGroup{} diff --git a/src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go b/src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go new file mode 100644 index 0000000..23e3888 --- /dev/null +++ b/src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go @@ -0,0 +1,73 @@ +// +// sfive +// +// sfive - spreadspace streaming statistics suite is a generic +// statistic collection tool for streaming server infrastuctures. +// The system collects and stores meta data like number of views +// and throughput from a number of streaming servers and stores +// it in a global data store. +// The data acquisition is designed to be generic and extensible in +// order to support different streaming software. +// sfive also contains tools and applications to filter and visualize +// live and recorded data. +// +// +// Copyright (C) 2014-2017 Christian Pointner +// Markus Grüneis +// +// This file is part of sfive. +// +// sfive is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 3 +// as published by the Free Software Foundation. +// +// sfive is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with sfive. If not, see . +// + +package sfive + +import ( + "fmt" + + maxmind "github.com/rainycape/geoip" +) + +type GeoIPLookup interface { + String() string + Lookup(addr string) (string, error) // TODO: return LAT,LON,City,Contry as seperate fields +} + +// +// MaxMind GeoIP2 +// + +type MaxMindGeoIP2 struct { + file string + db *maxmind.GeoIP +} + +func (mm *MaxMindGeoIP2) String() string { + return "MaxMind GeoIP2 database (" + mm.file + "), last updated: " + mm.db.Updated().String() +} + +func (mm *MaxMindGeoIP2) Lookup(addr string) (string, error) { + rec, err := mm.db.Lookup(addr) + if err != nil { + return "", err + } + + return fmt.Sprintf("%+v", rec), nil +} + +func NewMaxMindGeoIP2(dbFile string) (GeoIPLookup, error) { + var err error + mm := &MaxMindGeoIP2{file: dbFile} + mm.db, err = maxmind.Open(dbFile) + return mm, err +} diff --git a/src/hub/test-srv-geoip b/src/hub/test-srv-geoip new file mode 100755 index 0000000..ef9c2fc --- /dev/null +++ b/src/hub/test-srv-geoip @@ -0,0 +1,15 @@ +#!/bin/sh + +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + +TEST_D="./test" +TEST_DB="$TEST_D/$1.bolt" +EXTRA_OPTS="" + + +mkdir -p "$TEST_D" +rm -f "$TEST_D/pipe" "$TEST_D/pipegram" +exec ./bin/sfive-hub -db "$TEST_DB" -start-pipe-server -pipe "$TEST_D/pipe" -start-pipegram-server -pipegram "$TEST_D/pipegram" -start-web-server -web ":8000" -geoip-db $2 -- cgit v1.2.3