Add proxy headers into request
All checks were successful
release-nightly / build-docker (push) Successful in 2m26s

This commit is contained in:
LittleSheep 2024-01-30 22:42:21 +08:00
parent 28fd5aca8b
commit 11cf35cf0d
3 changed files with 38 additions and 19 deletions

View File

@ -5,19 +5,19 @@ A blazing fast reverse proxy with a lot of shining features.
## Features ## Features
1. Reverse proxy 1. Reverse proxy
2. HTTP2 Support 2. WebSocket Support
3. WebSocket Support 3. Static File Hosting
4. Static File Hosting 4. Low Configuration
5. Low Configuration 5. Analytics and Metrics
6. Analytics and Metrics 6. Integrate with CI/CD
7. Integrate with CI/CD 7. Web management panel (Work in progres for v2, available in v1)
8. Web management panel (Work in progres for v2, available in v1) 8. One-liner CLI
9. One-liner CLI 9. Open-source and free
10. Open-source and free 10. **Blazing fast ⚡**
11. **Blazing fast ⚡**
> [!IMPORTANT] > [!IMPORTANT]
> Currently roadsign haven't supported for server-side events. We are working on it. > Currently roadsign haven't supported for server-side events. We are working on it.
> At the same time, we don't support HTTP/2.0, [here's](https://github.com/gofiber/fiber/issues/262) the reason
### How fast is it? ### How fast is it?

View File

@ -1,6 +1,7 @@
package hypertext package hypertext
import ( import (
"fmt"
"github.com/spf13/viper" "github.com/spf13/viper"
"math/rand" "math/rand"
"regexp" "regexp"
@ -94,22 +95,38 @@ func UseProxies(app *fiber.App) {
}) })
} }
func makeResponse(ctx *fiber.Ctx, region *navi.Region, location *navi.Location, dest *navi.Destination) error { func makeResponse(c *fiber.Ctx, region *navi.Region, location *navi.Location, dest *navi.Destination) error {
uri := ctx.Request().URI().String() uri := c.Request().URI().String()
// Modify request // Modify request
for _, transformer := range dest.Transformers { for _, transformer := range dest.Transformers {
if err := transformer.TransformRequest(ctx); err != nil { if err := transformer.TransformRequest(c); err != nil {
return err return err
} }
} }
// Add reserve proxy headers
ip := c.IP()
scheme := c.Protocol()
protocol := string(c.Request().Header.Protocol())
c.Request().Header.Set(fiber.HeaderXForwardedFor, ip)
c.Request().Header.Set(fiber.HeaderXForwardedHost, ip)
c.Request().Header.Set(fiber.HeaderXForwardedProto, scheme)
c.Request().Header.Set(
fiber.HeaderVia,
fmt.Sprintf("%s %s", protocol, viper.GetString("central")),
)
c.Request().Header.Set(
fiber.HeaderForwarded,
fmt.Sprintf("by=%s; for=%s; host=%s; proto=%s", c.IP(), c.IP(), c.Get(fiber.HeaderHost), scheme),
)
// Forward // Forward
err := navi.R.Forward(ctx, dest) err := navi.R.Forward(c, dest)
// Modify response // Modify response
for _, transformer := range dest.Transformers { for _, transformer := range dest.Transformers {
if err := transformer.TransformResponse(ctx); err != nil { if err := transformer.TransformResponse(c); err != nil {
return err return err
} }
} }
@ -126,8 +143,8 @@ func makeResponse(ctx *fiber.Ctx, region *navi.Region, location *navi.Location,
Location: location.ID, Location: location.ID,
Destination: dest.ID, Destination: dest.ID,
Uri: uri, Uri: uri,
IpAddress: ctx.IP(), IpAddress: c.IP(),
UserAgent: ctx.Get(fiber.HeaderUserAgent), UserAgent: c.Get(fiber.HeaderUserAgent),
Error: navi.RoadTraceError{ Error: navi.RoadTraceError{
IsNull: err == nil, IsNull: err == nil,
Message: message, Message: message,

View File

@ -1,3 +1,5 @@
id = "central"
[debug] [debug]
print_routes = false print_routes = false