summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2017-05-19 03:31:41 +0200
committerChristian Pointner <equinox@spreadspace.org>2017-05-19 03:31:41 +0200
commitf1fce55d2c34520d76b6bc86a815377f1b8de8b6 (patch)
tree59cb37344434c698f3379bbe4da29207727de14e
parentadded inital geo-ip lookup (diff)
added encoding for geo-ip info
-rw-r--r--doc/protocol.md12
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5srv.go2
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5srvForwardPiwik.go7
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go25
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5typesApi.go19
5 files changed, 54 insertions, 11 deletions
diff --git a/doc/protocol.md b/doc/protocol.md
index e960506..9c4718d 100644
--- a/doc/protocol.md
+++ b/doc/protocol.md
@@ -53,3 +53,15 @@ The start-time will be processesd and stored with millisecond precision.
....
}
}
+
+In addition to the user-agent string a client entry may have the following geo-info
+fields (all of which might be omitted):
+
+ "country" ......... the name of the country
+ "country-code2" ... the 2-letter country code
+ "region" .......... the name of the region
+ "region-code" ..... the 2-letter code for the region as defined by the
+ MaxMind GeoIP2 database
+ "city" ............ the name of the city
+ "latitude" ........ latitude in ° as float value
+ "longitude" ....... longitude in ° as float value
diff --git a/src/hub/src/spreadspace.org/sfive/s5srv.go b/src/hub/src/spreadspace.org/sfive/s5srv.go
index e9c1baa..d3ce519 100644
--- a/src/hub/src/spreadspace.org/sfive/s5srv.go
+++ b/src/hub/src/spreadspace.org/sfive/s5srv.go
@@ -70,7 +70,7 @@ func (srv Server) transform(update *UpdateFull) *UpdateFull {
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)
+ s5l.Printf("transform: Geo-IP lookup; %s -> %+v", client.IP, rec)
}
}
diff --git a/src/hub/src/spreadspace.org/sfive/s5srvForwardPiwik.go b/src/hub/src/spreadspace.org/sfive/s5srvForwardPiwik.go
index 047ebcf..7dc034e 100644
--- a/src/hub/src/spreadspace.org/sfive/s5srvForwardPiwik.go
+++ b/src/hub/src/spreadspace.org/sfive/s5srvForwardPiwik.go
@@ -94,6 +94,7 @@ tryResync:
ip = client.IP
}
+ // see: https://developer.piwik.org/api-reference/tracking-api
q := make(url.Values)
q.Add("rec", "1")
q.Add("idsite", strconv.FormatUint(uint64(siteID), 10))
@@ -101,6 +102,12 @@ tryResync:
q.Add("cip", ip)
q.Add("cdt", strconv.FormatInt(update.StartTime.Unix(), 10))
q.Add("ua", client.UserAgent)
+ // TODO: add geoip info as soon this is implemented:
+ //q.Add("country", ...) 2 Letter Country-Code
+ //q.Add("region", ...) 2 Letter Region-Code
+ //q.Add("city", ...)
+ //q.Add("lat", ...)
+ //q.Add("lon", ...)
req.Requests = append(req.Requests, "?"+q.Encode())
}
}
diff --git a/src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go b/src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go
index 23e3888..a966617 100644
--- a/src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go
+++ b/src/hub/src/spreadspace.org/sfive/s5srvGeoIP.go
@@ -33,14 +33,12 @@
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
+ Lookup(addr string) (*GeoInfo, error)
}
//
@@ -56,13 +54,28 @@ 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) {
+func (mm *MaxMindGeoIP2) Lookup(addr string) (*GeoInfo, error) {
rec, err := mm.db.Lookup(addr)
if err != nil {
- return "", err
+ return nil, err
+ }
+
+ info := &GeoInfo{}
+ if rec.Country != nil {
+ info.CountryName = rec.Country.Name.String()
+ info.CountryCode2 = rec.Country.Code
+ }
+ if len(rec.Subdivisions) > 0 && rec.Subdivisions[0] != nil {
+ info.RegionName = rec.Subdivisions[0].Name.String()
+ info.RegionCode = rec.Subdivisions[0].Code
+ }
+ if rec.City != nil {
+ info.CityName = rec.City.Name.String()
}
+ info.Latitude = rec.Latitude
+ info.Longitude = rec.Longitude
- return fmt.Sprintf("%+v", rec), nil
+ return info, nil
}
func NewMaxMindGeoIP2(dbFile string) (GeoIPLookup, error) {
diff --git a/src/hub/src/spreadspace.org/sfive/s5typesApi.go b/src/hub/src/spreadspace.org/sfive/s5typesApi.go
index 90a413a..c1c425e 100644
--- a/src/hub/src/spreadspace.org/sfive/s5typesApi.go
+++ b/src/hub/src/spreadspace.org/sfive/s5typesApi.go
@@ -49,10 +49,21 @@ type Source struct {
Tags []string `json:"tags,omitempty"`
}
+type GeoInfo struct {
+ CountryName string `json:"country,omitempty"`
+ CountryCode2 string `json:"country-code2,omitempty"`
+ RegionName string `json:"region,omitempty"`
+ RegionCode string `json:"region-code,omitempty"`
+ CityName string `json:"city,omitempty"`
+ Latitude float64 `json:"latitude,omitempty"`
+ Longitude float64 `json:"longitude,omitempty"`
+}
+
type Client struct {
IP string `json:"ip"`
UserAgent string `json:"user-agent,omitempty"`
BytesSent uint `json:"bytes-sent"`
+ // GeoInfo
}
type UpdateData struct {
@@ -84,16 +95,16 @@ type UpdateFull struct {
func (uf *UpdateFull) checkSource() error {
if uf.Source.Hostname == "" {
- return errors.New("empty hostname ist not allowed")
+ return errors.New("empty hostname is not allowed")
}
if uf.Source.Stream.ContentID == "" {
- return errors.New("empty content-id ist not allowed")
+ return errors.New("empty content-id is not allowed")
}
if uf.Source.Stream.Format == "" {
- return errors.New("empty format ist not allowed")
+ return errors.New("empty format is not allowed")
}
if uf.Source.Stream.Quality == "" {
- return errors.New("empty quality ist not allowed")
+ return errors.New("empty quality is not allowed")
}
return nil
}