Skip to content

Commit

Permalink
Add the daily changing salt for visitor id calculations (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
th0th authored Jan 31, 2023
1 parent 3c7b9c5 commit 34644e9
Show file tree
Hide file tree
Showing 81 changed files with 327 additions and 168 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/cmd/scheduler/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package main

import (
"log"

"github.com/getsentry/sentry-go"
"github.com/poeticmetric/poeticmetric/backend/pkg/depot"
"github.com/poeticmetric/poeticmetric/backend/pkg/env"
sentry2 "github.com/poeticmetric/poeticmetric/backend/pkg/sentry"
"github.com/poeticmetric/poeticmetric/backend/pkg/signal"
"github.com/poeticmetric/poeticmetric/backend/pkg/worker"
"github.com/robfig/cron/v3"
"log"
)

func handleCallError(err error) {
Expand Down
18 changes: 9 additions & 9 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ module github.com/poeticmetric/poeticmetric/backend
go 1.19

require (
github.com/ClickHouse/clickhouse-go/v2 v2.3.0
github.com/ClickHouse/clickhouse-go/v2 v2.5.1
github.com/RussellLuo/validating/v3 v3.0.0-beta.1
github.com/brianvoe/gofakeit/v6 v6.20.1
github.com/dchest/uniuri v1.2.0
github.com/getsentry/sentry-go v0.17.0
github.com/go-redis/redis/v9 v9.0.0-rc.2
github.com/gofiber/contrib/fibersentry v0.0.0-20230116072133-939d79692a7f
github.com/gofiber/contrib/fibersentry v0.0.0-20230130075250-85a941de3c25
github.com/gofiber/fiber/v2 v2.41.0
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/uuid v1.3.0
github.com/jackc/pgx/v5 v5.2.0
github.com/lib/pq v1.10.7
github.com/mileusna/useragent v1.2.1
github.com/rabbitmq/amqp091-go v1.5.0
github.com/rabbitmq/amqp091-go v1.6.0
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.8.1
github.com/stripe/stripe-go/v74 v74.5.0
github.com/stripe/stripe-go/v74 v74.6.0
github.com/tdewolff/minify/v2 v2.12.4
golang.org/x/crypto v0.5.0
golang.org/x/sync v0.1.0
Expand All @@ -29,7 +29,7 @@ require (
)

require (
github.com/ClickHouse/ch-go v0.48.0 // indirect
github.com/ClickHouse/ch-go v0.51.0 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -43,11 +43,11 @@ require (
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/klauspost/compress v1.15.13 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/paulmach/orb v0.7.1 // indirect
github.com/paulmach/orb v0.8.0 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -58,8 +58,8 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.43.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/exp v0.0.0-20220314205449-43aec2f8a4e7 // indirect
golang.org/x/sys v0.4.0 // indirect
Expand Down
45 changes: 23 additions & 22 deletions backend/go.sum

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion backend/pkg/depot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package depot

import (
"context"
"net/http"

"github.com/go-redis/redis/v9"
"github.com/poeticmetric/poeticmetric/backend/pkg/depot/rabbitmq"
"gorm.io/gorm"
"net/http"
)

var Ctx = context.Background()
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/depot/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package depot

import (
"database/sql"

"github.com/poeticmetric/poeticmetric/backend/pkg/env"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down
5 changes: 3 additions & 2 deletions backend/pkg/depot/rabbitmq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package rabbitmq

import (
"fmt"
"github.com/poeticmetric/poeticmetric/backend/pkg/env"
amqp "github.com/rabbitmq/amqp091-go"
"log"
"time"

"github.com/poeticmetric/poeticmetric/backend/pkg/env"
amqp "github.com/rabbitmq/amqp091-go"
)

type QueueName string
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/depot/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package depot

import (
"fmt"

"github.com/go-redis/redis/v9"
"github.com/poeticmetric/poeticmetric/backend/pkg/env"
)
Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package email

import (
"fmt"
"github.com/poeticmetric/poeticmetric/backend/pkg/env"
"net/smtp"

"github.com/poeticmetric/poeticmetric/backend/pkg/env"
)

type SendInput struct {
Expand Down
5 changes: 3 additions & 2 deletions backend/pkg/env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package env

import (
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"strconv"
"strings"
"time"

"gorm.io/gorm"
"gorm.io/gorm/logger"
)

const (
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package frontend

import (
"fmt"

"github.com/poeticmetric/poeticmetric/backend/pkg/env"
)

Expand Down
33 changes: 28 additions & 5 deletions backend/pkg/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
url2 "net/url"
"time"

"github.com/mileusna/useragent"
"github.com/poeticmetric/poeticmetric/backend/pkg/country"
"github.com/poeticmetric/poeticmetric/backend/pkg/depot"
locale2 "github.com/poeticmetric/poeticmetric/backend/pkg/locale"
"github.com/poeticmetric/poeticmetric/backend/pkg/pointer"
"log"
url2 "net/url"
"time"
salt2 "github.com/poeticmetric/poeticmetric/backend/pkg/service/salt"
)

type Event struct {
Expand Down Expand Up @@ -140,8 +143,28 @@ func (e *Event) FillFromUserAgent(userAgent string) {
}
}

func (e *Event) FillVisitorId(ipAddress string, userAgent string) {
h := sha256.Sum256([]byte(fmt.Sprintf("%s%s", ipAddress, userAgent)))
func (e *Event) FillVisitorId(dp *depot.Depot, ipAddress string, userAgent string) {
salt := salt2.Get(dp)
hashOrder := salt2.GetHashOrder(dp)

var hash string

switch hashOrder {
case salt2.HashOrder1:
hash = fmt.Sprintf("%s%s%s", ipAddress, userAgent, salt)
case salt2.HashOrder2:
hash = fmt.Sprintf("%s%s%s", ipAddress, salt, userAgent)
case salt2.HashOrder3:
hash = fmt.Sprintf("%s%s%s", userAgent, ipAddress, salt)
case salt2.HashOrder4:
hash = fmt.Sprintf("%s%s%s", userAgent, salt, ipAddress)
case salt2.HashOrder5:
hash = fmt.Sprintf("%s%s%s", salt, ipAddress, userAgent)
case salt2.HashOrder6:
hash = fmt.Sprintf("%s%s%s", salt, userAgent, ipAddress)
}

h := sha256.Sum256([]byte(hash))

e.VisitorId = hex.EncodeToString(h[:])
}
4 changes: 2 additions & 2 deletions backend/pkg/model/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package model
import "time"

const (
OrganizationNameMaxLength = 70
OrganizationNameMinLength = 2
OrganizationNameMaxLength = 70
OrganizationNameMinLength = 2
OrganizationSubscriptionPeriodMonth OrganizationSubscriptionPeriod = "MONTH"
OrganizationSubscriptionPeriodYear OrganizationSubscriptionPeriod = "YEAR"
)
Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/restapi/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package restapi
import (
"errors"
"fmt"
"log"

v "github.com/RussellLuo/validating/v3"
"github.com/gofiber/contrib/fibersentry"
"github.com/gofiber/fiber/v2"
"github.com/poeticmetric/poeticmetric/backend/pkg/env"
"github.com/poeticmetric/poeticmetric/backend/pkg/restapi/helpers"
"log"
)

func errorHandler(c *fiber.Ctx, err error) error {
Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/restapi/event/create_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package event

import (
"encoding/json"
"time"

"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
dm "github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/depot"
"github.com/poeticmetric/poeticmetric/backend/pkg/service/event"
"github.com/poeticmetric/poeticmetric/backend/pkg/worker"
"time"
)

func createEvent(c *fiber.Ctx) error {
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/restapi/helpers/id_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"errors"

"github.com/gofiber/fiber/v2"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package authentication

import (
"errors"
"log"
"strconv"
"strings"
"time"

"github.com/getsentry/sentry-go"
"github.com/gofiber/contrib/fibersentry"
"github.com/gofiber/fiber/v2"
Expand All @@ -10,10 +15,6 @@ import (
"github.com/poeticmetric/poeticmetric/backend/pkg/pointer"
dm "github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/depot"
"gorm.io/gorm"
"log"
"strconv"
"strings"
"time"
)

func NewUserAccessTokenAuth() fiber.Handler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package authentication
import (
"encoding/base64"
"errors"
"strconv"
"strings"

"github.com/getsentry/sentry-go"
"github.com/gofiber/contrib/fibersentry"
"github.com/gofiber/fiber/v2"
Expand All @@ -11,8 +14,6 @@ import (
dm "github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/depot"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"strconv"
"strings"
)

func NewUserBasicAuth() fiber.Handler {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package permission

import (
"reflect"

"github.com/gofiber/fiber/v2"
"github.com/poeticmetric/poeticmetric/backend/pkg/restapi/helpers"
"github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/authentication"
"reflect"
)

func OrganizationPlanFeatureFlag(flag string) fiber.Handler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package organization

import (
"errors"

"github.com/gofiber/fiber/v2"
"github.com/poeticmetric/poeticmetric/backend/pkg/restapi/helpers"
am "github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/authentication"
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/restapi/organization/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ func destroy(c *fiber.Ctx) error {
}

return c.SendStatus(fiber.StatusNoContent)
}
}
3 changes: 1 addition & 2 deletions backend/pkg/restapi/root/index.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package root

import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/poeticmetric/poeticmetric/backend/pkg/frontend"
)

func index(c *fiber.Ctx) error {
return c.Redirect(fmt.Sprintf("%s%s", c.Protocol(), frontend.GenerateUrl("/")))
return c.Redirect(frontend.GenerateUrl("/"))
}
1 change: 1 addition & 0 deletions backend/pkg/restapi/site/read_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package site

import (
"errors"

"github.com/gofiber/fiber/v2"
dm "github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/depot"
"github.com/poeticmetric/poeticmetric/backend/pkg/service/site"
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/restapi/sitereport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"

"github.com/poeticmetric/poeticmetric/backend/pkg/model"
dm "github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/depot"
"github.com/poeticmetric/poeticmetric/backend/pkg/restapi/middleware/permission"
Expand Down
5 changes: 3 additions & 2 deletions backend/pkg/restapi/stripe/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package stripe
import (
"encoding/json"
"errors"
"log"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/poeticmetric/poeticmetric/backend/pkg/email"
"github.com/poeticmetric/poeticmetric/backend/pkg/env"
Expand All @@ -15,8 +18,6 @@ import (
"github.com/stripe/stripe-go/v74/customer"
webhook2 "github.com/stripe/stripe-go/v74/webhook"
"gorm.io/gorm"
"log"
"strings"
)

func webhook(c *fiber.Ctx) error {
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/restapi/user/sign_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func signUp(c *fiber.Ctx) error {
}

return c.Status(fiber.StatusCreated).JSON(map[string]any{
"user": userSelf,
"user": userSelf,
"userAccessToken": userAccessToken,
})
}
Loading

0 comments on commit 34644e9

Please sign in to comment.