feat: add anonymize address middleware
Some checks failed
/ Build (push) Failing after 12s

This commit is contained in:
log101 2024-05-30 18:42:33 +03:00
parent 30b38e7247
commit 8e8d69d848
3 changed files with 56 additions and 3 deletions

10
main.go
View File

@ -8,10 +8,11 @@ import (
"net/http"
"os"
"github.com/go-sql-driver/mysql"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-sql-driver/mysql"
"log101-blog-services/middleware"
)
var db *sql.DB
@ -101,7 +102,10 @@ func main() {
}
corsConfig.AllowHeaders = []string{"hx-current-url", "hx-request"}
// Middlewares
r.Use(cors.New(corsConfig))
r.Use(middleware.AnonymizeIPMiddleware())
var hxPostUrl string
if ginMode == gin.DebugMode {
@ -145,7 +149,7 @@ func main() {
}
// Add the new emoji entry to the database
_, err := db.Exec("INSERT INTO emoji_clicks (post_id, emoji) VALUES (?, ?)", postId, emoji)
_, err := db.Exec("INSERT INTO emoji_clicks (user_anon_data, post_id, emoji) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE emoji = ?", c.Request.RemoteAddr, postId, emoji, emoji)
if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postId, "hxPostUrl": hxPostUrl})
return

48
middleware/middleware.go Normal file
View File

@ -0,0 +1,48 @@
package middleware
import (
"net/netip"
"github.com/gin-gonic/gin"
)
// AnonymizeIPMiddleware masks the last segment of IPv4 addresses
// and the last 80 bits of IPv6 addresses.
func AnonymizeIPMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
ip := c.ClientIP()
anonymizedIP := anonymizeIP(ip)
c.Request.RemoteAddr = anonymizedIP
c.Next()
}
}
// anonymizeIP masks the last segment of IPv4 addresses
// and the last 80 bits of IPv6 addresses.
func anonymizeIP(ip string) string {
parsedIP, err := netip.ParseAddr(ip)
if err != nil {
// If there's an error parsing the IP, return the original IP
return ip
}
if parsedIP.Is4() {
// Mask the last octet for IPv4
ipPrefix, err := parsedIP.Prefix(24)
if err != nil {
return ip
}
return ipPrefix.Masked().String()
} else if parsedIP.Is6() {
// Mask the last 80 bits for IPv6
ipPrefix, err := parsedIP.Prefix(48)
if err != nil {
return ip
}
return ipPrefix.Masked().String()
}
// If it's neither IPv4 nor IPv6, return the original IP
return ip
}

View File

@ -5,6 +5,7 @@ USE emojis;
CREATE TABLE IF NOT EXISTS emoji_clicks (
id INT PRIMARY KEY AUTO_INCREMENT,
user_anon_data TEXT NOT NULL,
post_id TEXT NOT NULL,
emoji TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP