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
1. Reverse proxy
2. HTTP2 Support
3. WebSocket Support
4. Static File Hosting
5. Low Configuration
6. Analytics and Metrics
7. Integrate with CI/CD
8. Web management panel (Work in progres for v2, available in v1)
9. One-liner CLI
10. Open-source and free
11. **Blazing fast ⚡**
2. WebSocket Support
3. Static File Hosting
4. Low Configuration
5. Analytics and Metrics
6. Integrate with CI/CD
7. Web management panel (Work in progres for v2, available in v1)
8. One-liner CLI
9. Open-source and free
10. **Blazing fast ⚡**
> [!IMPORTANT]
> 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?

View File

@ -1,6 +1,7 @@
package hypertext
import (
"fmt"
"github.com/spf13/viper"
"math/rand"
"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 {
uri := ctx.Request().URI().String()
func makeResponse(c *fiber.Ctx, region *navi.Region, location *navi.Location, dest *navi.Destination) error {
uri := c.Request().URI().String()
// Modify request
for _, transformer := range dest.Transformers {
if err := transformer.TransformRequest(ctx); err != nil {
if err := transformer.TransformRequest(c); err != nil {
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
err := navi.R.Forward(ctx, dest)
err := navi.R.Forward(c, dest)
// Modify response
for _, transformer := range dest.Transformers {
if err := transformer.TransformResponse(ctx); err != nil {
if err := transformer.TransformResponse(c); err != nil {
return err
}
}
@ -126,10 +143,10 @@ func makeResponse(ctx *fiber.Ctx, region *navi.Region, location *navi.Location,
Location: location.ID,
Destination: dest.ID,
Uri: uri,
IpAddress: ctx.IP(),
UserAgent: ctx.Get(fiber.HeaderUserAgent),
Error: navi.RoadTraceError{
IsNull: err == nil,
IpAddress: c.IP(),
UserAgent: c.Get(fiber.HeaderUserAgent),
Error: navi.RoadTraceError{
IsNull: err == nil,
Message: message,
},
})

View File

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