Skip to content

Commit

Permalink
fix: remote ip being "wrong" for wrapped grpc server
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Oct 11, 2024
1 parent d430368 commit 7bc730c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/components/jobs/timeclock/TimeclockStatsChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const xTicks = (i: number) => {
};
const template = (d: DataRecord) =>
`${formatDate(d.date)}<br />
`<span class="font-semibold">${formatDate(d.date)}</span><br />
${t('components.jobs.timeclock.Stats.sum')}: ${n(d.sum, 'decimal')} h<br />
${t('components.jobs.timeclock.Stats.avg')}: ${n(d.avg, 'decimal')} h<br />
${t('components.jobs.timeclock.Stats.max')}: ${n(d.max, 'decimal')} h`;
Expand Down
14 changes: 10 additions & 4 deletions pkg/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"context"
"database/sql"
"fmt"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -107,14 +108,16 @@ type EngineParams struct {
GRPCSrv *grpc.Server
}

func NewEngine(p EngineParams) *gin.Engine {
func NewEngine(p EngineParams) (*gin.Engine, error) {
// Gin HTTP Server
gin.SetMode(p.Config.Mode)
e := gin.New()

// Enable forwarded by client ip headers when one or more trusted proxies specified
e.ForwardedByClientIP = len(p.Config.HTTP.TrustedProxies) > 0
e.SetTrustedProxies(p.Config.HTTP.TrustedProxies)
if err := e.SetTrustedProxies(p.Config.HTTP.TrustedProxies); err != nil {
return nil, fmt.Errorf("failed to set trusted proxies list. %w", err)
}

// Add Zap logger and panic recovery to Gin
e.Use(ginzap.GinzapWithConfig(p.Logger, &ginzap.Config{
Expand Down Expand Up @@ -194,7 +197,10 @@ func NewEngine(p EngineParams) *gin.Engine {
grpcws.WithAllowNonRootResource(true),
grpcws.WithWebsocketPingInterval(40*time.Second),
)
ginWrappedGrpc := gin.WrapH(wrapperGrpc)
ginWrappedGrpc := func(c *gin.Context) {
c.Request.RemoteAddr = c.ClientIP()
wrapperGrpc.ServeHTTP(c.Writer, c.Request)
}
e.Any("/api/grpc", ginWrappedGrpc)
e.Any("/api/grpc/*path", ginWrappedGrpc)

Expand Down Expand Up @@ -234,5 +240,5 @@ func NewEngine(p EngineParams) *gin.Engine {
c.Abort()
})

return e
return e, nil
}

0 comments on commit 7bc730c

Please sign in to comment.