From da5b5cade37443ba4491f69e1d84748379cc7f07 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 19 May 2017 01:02:34 +0200 Subject: added improved sanity checks --- src/hub/src/spreadspace.org/sfive/s5srv.go | 56 ++++++++++++++++++------------ 1 file changed, 34 insertions(+), 22 deletions(-) (limited to 'src/hub') diff --git a/src/hub/src/spreadspace.org/sfive/s5srv.go b/src/hub/src/spreadspace.org/sfive/s5srv.go index ee1cba0..6b5f030 100644 --- a/src/hub/src/spreadspace.org/sfive/s5srv.go +++ b/src/hub/src/spreadspace.org/sfive/s5srv.go @@ -58,46 +58,58 @@ type Server struct { appendManyChan chan appendManyToken } -func (srv Server) Anonymize(update *UpdateFull) *UpdateFull { - anonymized := []Client{} +func (srv Server) transform(update *UpdateFull) *UpdateFull { + bytesSentTotal := uint(0) + clients := []Client{} for _, client := range update.Data.Clients { - aIP, err := srv.anonymization.Anonymize(client.IP) - if err != nil { - s5l.Printf("anonymization: failed: %v", err) - } else { - client.IP = aIP + bytesSentTotal += client.BytesSent + + // TODO: add GeoIP lookup here + + if srv.anonymization != nil { + if aIP, err := srv.anonymization.Anonymize(client.IP); err != nil { + s5l.Printf("transform: anonymization failed: %v", err) + } else { + client.IP = aIP + } + } + + clients = append(clients, client) + } + update.Data.Clients = clients + + if uint(len(update.Data.Clients)) > update.Data.ClientCount { + if update.Data.ClientCount > 0 { + s5l.Printf("transform: fixing client-count: %d -> %d", update.Data.ClientCount, len(update.Data.Clients)) } - anonymized = append(anonymized, client) + update.Data.ClientCount = uint(len(update.Data.Clients)) } - update.Data.Clients = anonymized + if bytesSentTotal > update.Data.BytesSent { + if update.Data.BytesSent > 0 { + s5l.Printf("transform: fixing bytes-sent: %d -> %d", update.Data.BytesSent, bytesSentTotal) + } + update.Data.BytesSent = bytesSentTotal + } + return update } -func (srv Server) AnonymizeMany(updates []*UpdateFull) []*UpdateFull { - anonymized := []*UpdateFull{} +func (srv Server) transformMany(updates []*UpdateFull) { for _, update := range updates { - anonymized = append(anonymized, srv.Anonymize(update)) + srv.transform(update) } - return anonymized } -// TODO: do a sanity check for input data -// TODO: add GeoIP lookup befor anonymization - func (srv Server) appendWorker(idx int) { for { select { case <-srv.quit: return case token := <-srv.appendChan: - if srv.anonymization != nil { - token.update = srv.Anonymize(token.update) - } + srv.transform(token.update) token.response <- srv.store.Append(token.update) case token := <-srv.appendManyChan: - if srv.anonymization != nil { - token.updates = srv.AnonymizeMany(token.updates) - } + srv.transformMany(token.updates) token.response <- srv.store.AppendMany(token.updates) } } -- cgit v1.2.3