jlelse
/
kis3
Archived
1
Fork 0

Clean referrer and just keep it's hostname

This commit is contained in:
Jan-Lukas Else 2019-04-05 17:08:05 +02:00
parent 223191d95c
commit f3f0a8637c
2 changed files with 11 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/gobuffalo/packr/v2" "github.com/gobuffalo/packr/v2"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/rubenv/sql-migrate" "github.com/rubenv/sql-migrate"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -38,11 +39,17 @@ func migrateDatabase(database *sql.DB) (e error) {
// Tracking // Tracking
func (db *Database) trackView(url string, ref string) { func (db *Database) trackView(urlString string, ref string) {
if len(url) == 0 { if len(urlString) == 0 {
// Don't track empty urls
return return
} }
_, e := db.sqlDB.Exec("insert into views(url, ref) values(:url, :ref)", sql.Named("url", url), sql.Named("ref", ref)) if ref != "" {
// Clean referrer and just keep the hostname for more privacy
parsedRef, _ := url.Parse(ref)
ref = parsedRef.Hostname()
}
_, e := db.sqlDB.Exec("insert into views(url, ref) values(:url, :ref)", sql.Named("url", urlString), sql.Named("ref", ref))
if e != nil { if e != nil {
fmt.Println("Inserting into DB failed:", e) fmt.Println("Inserting into DB failed:", e)
} }

View File

@ -69,10 +69,7 @@ func (kis3 kis3) startListening() {
func (kis3 kis3) trackView(w http.ResponseWriter, r *http.Request) { func (kis3 kis3) trackView(w http.ResponseWriter, r *http.Request) {
url := r.Header.Get("Referer") // URL of requesting source url := r.Header.Get("Referer") // URL of requesting source
ref := r.URL.Query().Get("ref") ref := r.URL.Query().Get("ref")
if r.Header.Get("DNT") == "1" && appConfig.dnt { if !(r.Header.Get("DNT") == "1" && appConfig.dnt) {
fmt.Println("Not tracking because of DNT")
} else {
fmt.Printf("Tracking %s with referrer %s\n", url, ref)
go kis3.db.trackView(url, ref) // run with goroutine for awesome speed! go kis3.db.trackView(url, ref) // run with goroutine for awesome speed!
_, _ = fmt.Fprint(w, "true") _, _ = fmt.Fprint(w, "true")
} }