RoadSign/pkg/hypertext/proxies.go

140 lines
2.8 KiB
Go
Raw Normal View History

2023-11-16 15:06:59 +00:00
package hypertext
import (
2024-01-25 06:46:43 +00:00
"github.com/spf13/viper"
2024-01-24 16:09:39 +00:00
"math/rand"
2023-12-10 03:30:31 +00:00
"regexp"
2024-01-24 16:09:39 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/navi"
2023-11-16 15:06:59 +00:00
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
)
func UseProxies(app *fiber.App) {
2023-11-17 16:23:40 +00:00
app.All("/*", func(ctx *fiber.Ctx) error {
2023-11-16 15:06:59 +00:00
host := ctx.Hostname()
path := ctx.Path()
queries := ctx.Queries()
headers := ctx.GetReqHeaders()
// Filtering sites
2024-01-24 16:09:39 +00:00
for _, region := range navi.R.Regions {
2023-11-16 15:06:59 +00:00
// Matching rules
2024-01-24 16:09:39 +00:00
for _, location := range region.Locations {
if !lo.Contains(location.Host, host) {
2023-11-16 15:06:59 +00:00
continue
}
2023-11-17 17:42:04 +00:00
if !func() bool {
flag := false
2024-01-24 16:09:39 +00:00
for _, pattern := range location.Path {
2023-11-17 17:42:04 +00:00
if ok, _ := regexp.MatchString(pattern, path); ok {
flag = true
break
}
}
return flag
}() {
continue
}
2023-11-16 15:06:59 +00:00
// Filter query strings
2023-11-17 17:42:04 +00:00
flag := true
2024-01-24 16:09:39 +00:00
for rk, rv := range location.Queries {
2023-11-16 15:06:59 +00:00
for ik, iv := range queries {
if rk != ik && rv != iv {
flag = false
break
}
}
if !flag {
break
}
}
if !flag {
continue
}
// Filter headers
2024-01-24 16:09:39 +00:00
for rk, rv := range location.Headers {
2023-11-16 15:06:59 +00:00
for ik, iv := range headers {
if rk == ik {
for _, ov := range iv {
if !lo.Contains(rv, ov) {
flag = false
break
}
}
}
if !flag {
break
}
}
if !flag {
break
}
}
if !flag {
continue
}
2024-01-24 16:09:39 +00:00
idx := rand.Intn(len(location.Destinations))
dest := location.Destinations[idx]
2023-11-17 17:42:04 +00:00
// Passing all the rules means the site is what we are looking for.
// Let us respond to our client!
2024-01-25 06:46:43 +00:00
return makeResponse(ctx, region, &location, &dest)
2023-11-17 17:42:04 +00:00
}
2023-11-16 15:06:59 +00:00
}
// There is no site available for this request.
// Just ignore it and give our client a not found status.
// Do not care about the user experience, we can do it in custom error handler.
return fiber.ErrNotFound
})
}
2024-01-25 06:46:43 +00:00
func makeResponse(ctx *fiber.Ctx, region *navi.Region, location *navi.Location, dest *navi.Destination) error {
2024-01-25 06:50:08 +00:00
uri := ctx.Request().URI().String()
2023-11-17 16:23:40 +00:00
// Modify request
2024-01-24 16:09:39 +00:00
for _, transformer := range dest.Transformers {
2023-12-16 03:42:14 +00:00
if err := transformer.TransformRequest(ctx); err != nil {
return err
}
2023-11-17 16:23:40 +00:00
}
2023-11-17 17:42:04 +00:00
// Forward
2024-01-24 16:09:39 +00:00
err := navi.R.Forward(ctx, dest)
2023-11-17 16:23:40 +00:00
// Modify response
2024-01-24 16:09:39 +00:00
for _, transformer := range dest.Transformers {
2023-12-16 03:42:14 +00:00
if err := transformer.TransformResponse(ctx); err != nil {
return err
}
2023-11-17 16:23:40 +00:00
}
2024-01-25 06:46:43 +00:00
// Collect trace
if viper.GetBool("telemetry.capture_traces") {
var message string
if err != nil {
message = err.Error()
}
go navi.R.AddTrace(navi.RoadTrace{
Region: region.ID,
Location: location.ID,
Destination: dest.ID,
2024-01-25 06:50:08 +00:00
Uri: uri,
2024-01-25 06:46:43 +00:00
IpAddress: ctx.IP(),
UserAgent: ctx.Get(fiber.HeaderUserAgent),
Error: navi.RoadTraceError{
IsNull: err == nil,
Message: message,
},
})
}
2023-11-17 16:23:40 +00:00
return err
2023-11-16 15:06:59 +00:00
}