From c797a87eb9b8bd6aac4cef9ffb3329bfdfe385a4 Mon Sep 17 00:00:00 2001 From: Harvey Kandola Date: Fri, 5 May 2017 10:44:52 +0100 Subject: [PATCH] record IP in events --- core/api/entity/objects.go | 1 + core/api/request/context.go | 11 ++++++++- core/api/request/event.go | 5 ++-- core/database/scripts/autobuild/db_00013.sql | 1 + core/utility/net.go | 26 ++++++++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 core/utility/net.go diff --git a/core/api/entity/objects.go b/core/api/entity/objects.go index eb721461..9ddb1330 100644 --- a/core/api/entity/objects.go +++ b/core/api/entity/objects.go @@ -515,6 +515,7 @@ type AppEvent struct { OrgID string `json:"orgId"` UserID string `json:"userId"` Type string `json:"eventType"` + IP string `json:"ip"` Created time.Time `json:"created"` } diff --git a/core/api/request/context.go b/core/api/request/context.go index 833f4c32..722fd342 100644 --- a/core/api/request/context.go +++ b/core/api/request/context.go @@ -21,6 +21,7 @@ import ( "github.com/jmoiron/sqlx" "github.com/documize/community/core/log" + "github.com/documize/community/core/utility" ) var rc = Context{} @@ -39,6 +40,7 @@ type Context struct { SSL bool AppURL string // e.g. https://{url}.documize.com Subdomain string + ClientIP string Expires time.Time Transaction *sqlx.Tx } @@ -60,7 +62,6 @@ func NewContext() Context { } func getContext(r *http.Request) Context { - if value := context.Get(r, rc); value != nil { return value.(Context) } @@ -74,6 +75,14 @@ func SetContext(r *http.Request, c Context) { c.Subdomain = GetSubdomainFromHost(r) c.SSL = r.TLS != nil + // get user IP from request + c.ClientIP = utility.GetRemoteIP(r.RemoteAddr) + + fip := r.Header.Get("X-Forwarded-For") + if len(fip) > 0 { + c.ClientIP = fip + } + context.Set(r, rc, c) } diff --git a/core/api/request/event.go b/core/api/request/event.go index 246a1067..eac98b4d 100644 --- a/core/api/request/event.go +++ b/core/api/request/event.go @@ -24,6 +24,7 @@ func (p *Persister) RecordEvent(t entity.EventType) { e.OrgID = p.Context.OrgID e.UserID = p.Context.UserID e.Created = time.Now().UTC() + e.IP = p.Context.ClientIP e.Type = string(t) if e.OrgID == "" || e.UserID == "" { @@ -37,14 +38,14 @@ func (p *Persister) RecordEvent(t entity.EventType) { return } - stmt, err := tx.Preparex("INSERT INTO userevent (orgid, userid, eventtype, created) VALUES (?, ?, ?, ?)") + stmt, err := tx.Preparex("INSERT INTO userevent (orgid, userid, eventtype, ip, created) VALUES (?, ?, ?, ?, ?)") if err != nil { tx.Rollback() log.Error("Unable to prepare insert RecordEvent", err) return } - _, err = stmt.Exec(e.OrgID, e.UserID, e.Type, e.Created) + _, err = stmt.Exec(e.OrgID, e.UserID, e.Type, e.IP, e.Created) if err != nil { log.Error("Unable to execute insert RecordEvent", err) tx.Rollback() diff --git a/core/database/scripts/autobuild/db_00013.sql b/core/database/scripts/autobuild/db_00013.sql index fe34017d..56553b7f 100644 --- a/core/database/scripts/autobuild/db_00013.sql +++ b/core/database/scripts/autobuild/db_00013.sql @@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS `userevent` ( `orgid` CHAR(16) NOT NULL COLLATE utf8_bin, `userid` CHAR(16) NOT NULL COLLATE utf8_bin, `eventtype` VARCHAR(100) NOT NULL DEFAULT '', + `ip` VARCHAR(39) NOT NULL COLLATE utf8_bin DEFAULT '', `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, CONSTRAINT pk_id PRIMARY KEY (id), INDEX `idx_userevent_orgid` (`orgid` ASC), diff --git a/core/utility/net.go b/core/utility/net.go new file mode 100644 index 00000000..120b0a48 --- /dev/null +++ b/core/utility/net.go @@ -0,0 +1,26 @@ +// Copyright 2016 Documize Inc. . All rights reserved. +// +// This software (Documize Community Edition) is licensed under +// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html +// +// You can operate outside the AGPL restrictions by purchasing +// Documize Enterprise Edition and obtaining a commercial license +// by contacting . +// +// https://documize.com + +package utility + +import ( + "strings" +) + +// GetRemoteIP returns just the IP and not the port number +func GetRemoteIP(ip string) string { + i := strings.LastIndex(ip, ":") + if i == -1 { + return ip + } + + return ip[:i] +}