Skip to content

Commit

Permalink
Add trusted proxies property and find a solution to unix sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenceJJones committed Jan 20, 2024
1 parent d32d131 commit 15dd6a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Expand Up @@ -39,6 +39,8 @@ type Configuration struct {
AllowMethods []string
AllowHeaders []string
}

TrustedProxies []string
}
Database struct {
Dialect string `default:"sqlite3"`
Expand Down
13 changes: 13 additions & 0 deletions router/router.go
Expand Up @@ -2,6 +2,7 @@ package router

import (
"fmt"
"net"
"net/http"
"path/filepath"
"regexp"
Expand All @@ -27,6 +28,18 @@ import (
func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Configuration) (*gin.Engine, func()) {
g := gin.New()

if conf.Server.TrustedProxies != nil {
fmt.Printf("Trusted proxies: %s\n", strings.Join(conf.Server.TrustedProxies, ", "))
g.SetTrustedProxies(conf.Server.TrustedProxies)
g.ForwardedByClientIP = true
}

g.Use(func(ctx *gin.Context) {
if localAddr, ok := ctx.Request.Context().Value(http.LocalAddrContextKey).(net.Addr); ok && localAddr.Network() == "unix" {
ctx.Request.RemoteAddr = "127.0.0.1:65535" // set remote address to localhost for unix socket requests
}
})

g.Use(gin.LoggerWithFormatter(logFormatter), gin.Recovery(), gerror.Handler(), location.Default())
g.NoRoute(gerror.NotFound())

Expand Down

0 comments on commit 15dd6a3

Please sign in to comment.