first commit
This commit is contained in:
commit
b2502bd29e
19 files changed
+556
No files matched your search
@@ -0,0 +1,94 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/securecookie"
|
||||
"github.com/influxdb/influxdb/client"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var hashKey = []byte("0ea20ffa64fb127136be5bb9fb0814289267f6d162c81f06af237a09e375d182bada41b9ba14eb4d34f73c9c8b792fc0")
|
||||
var s = securecookie.New(hashKey, nil)
|
||||
|
||||
type Data struct {
|
||||
IncluxDB *client.Client
|
||||
}
|
||||
|
||||
func (d *Data) CreateUserId(w http.ResponseWriter) bson.ObjectId {
|
||||
id := bson.NewObjectId()
|
||||
|
||||
if encoded, err := s.Encode("onepixel", map[string]string{"id": id.Hex()}); err == nil {
|
||||
cookie := &http.Cookie{
|
||||
Name: "onepixel",
|
||||
Value: encoded,
|
||||
Path: "/",
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
} else {
|
||||
log.Println("cannot encrypt uuid")
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func (d *Data) GetUserIdInCookie(r *http.Request) bson.ObjectId {
|
||||
if cookie, err := r.Cookie("onepixel"); err == nil {
|
||||
value := make(map[string]string)
|
||||
if err = s.Decode("onepixel", cookie.Value, &value); err == nil {
|
||||
if bson.IsObjectIdHex(value["id"]) {
|
||||
return bson.ObjectIdHex(value["id"])
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d *Data) GetAndSetId(w http.ResponseWriter, request *http.Request) bson.ObjectId {
|
||||
if id := d.GetUserIdInCookie(request); id.Valid() {
|
||||
return id
|
||||
} else {
|
||||
return d.CreateUserId(w)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) ServeHTTP(w http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
|
||||
ip, _, _ := net.SplitHostPort(request.RemoteAddr)
|
||||
|
||||
userAgent := request.UserAgent()
|
||||
useId := d.GetAndSetId(w, request)
|
||||
url := vars["url"]
|
||||
serieName := vars["serie_name"]
|
||||
|
||||
d.AddDataPoint(serieName, url, userAgent, ip, useId)
|
||||
}
|
||||
|
||||
func (d *Data) CreateSeries(serieName, url, userAgent, ip string, userId bson.ObjectId) []*client.Series {
|
||||
series := &client.Series{
|
||||
Name: serieName,
|
||||
Columns: []string{"url", "user_id", "user_agent", "ip"},
|
||||
Points: [][]interface{}{
|
||||
{url, userId, userAgent, ip},
|
||||
},
|
||||
}
|
||||
return []*client.Series{series}
|
||||
}
|
||||
|
||||
func (d *Data) AddDataPoint(serieName, url, userAgent, ip string, userId bson.ObjectId) {
|
||||
fmt.Println("serieName :", serieName)
|
||||
fmt.Println("url :", url)
|
||||
fmt.Println("id :", userId.Hex())
|
||||
fmt.Println("userAgent :", userAgent)
|
||||
fmt.Println("")
|
||||
|
||||
series := d.CreateSeries(serieName, url, userAgent, ip, userId)
|
||||
if err := d.IncluxDB.WriteSeries(series); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type MockResponseWriter struct {
|
||||
header http.Header
|
||||
}
|
||||
|
||||
func NewMockResponseWriter() http.ResponseWriter {
|
||||
var header http.Header = make(map[string][]string)
|
||||
return &MockResponseWriter{header}
|
||||
}
|
||||
|
||||
func (m *MockResponseWriter) Header() http.Header {
|
||||
return m.header
|
||||
}
|
||||
|
||||
func (m *MockResponseWriter) Write(b []byte) (int, error) {
|
||||
return 500, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (m *MockResponseWriter) WriteHeader(c int) {}
|
||||
|
||||
func TestSecureCookie(t *testing.T) {
|
||||
w := NewMockResponseWriter()
|
||||
d := new(Data)
|
||||
|
||||
uuid := d.CreateUserId(w)
|
||||
|
||||
if !uuid.Valid() {
|
||||
t.Error("invalid uuid")
|
||||
}
|
||||
|
||||
if w.Header().Get("Set-Cookie") == "" {
|
||||
t.Error("nothing in set in the cookie")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package data
|
||||
Reference in new issue
Block a user