RoadSign/pkg/hypertext/proxies.go

138 lines
2.7 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"
)
2024-01-31 07:13:57 +00:00
func ProxiesHandler(ctx *fiber.Ctx) error {
host := ctx.Hostname()
path := ctx.Path()
queries := ctx.Queries()
headers := ctx.GetReqHeaders()
// Filtering sites
for _, region := range navi.R.Regions {
// Matching rules
for _, location := range region.Locations {
if !lo.Contains(location.Host, host) {
continue
}
2023-11-16 15:06:59 +00:00
2024-01-31 07:13:57 +00:00
if !func() bool {
flag := false
for _, pattern := range location.Path {
if ok, _ := regexp.MatchString(pattern, path); ok {
flag = true
break
2023-11-17 17:42:04 +00:00
}
}
2024-01-31 07:13:57 +00:00
return flag
}() {
continue
}
2023-11-16 15:06:59 +00:00
2024-01-31 07:13:57 +00:00
// Filter query strings
flag := true
for rk, rv := range location.Queries {
for ik, iv := range queries {
if rk != ik && rv != iv {
flag = false
2023-11-16 15:06:59 +00:00
break
}
}
if !flag {
2024-01-31 07:13:57 +00:00
break
2023-11-16 15:06:59 +00:00
}
2024-01-31 07:13:57 +00:00
}
if !flag {
continue
}
2023-11-16 15:06:59 +00:00
2024-01-31 07:13:57 +00:00
// Filter headers
for rk, rv := range location.Headers {
for ik, iv := range headers {
if rk == ik {
for _, ov := range iv {
if !lo.Contains(rv, ov) {
flag = false
break
2023-11-16 15:06:59 +00:00
}
}
}
if !flag {
break
}
}
if !flag {
2024-01-31 07:13:57 +00:00
break
2023-11-16 15:06:59 +00:00
}
2024-01-31 07:13:57 +00:00
}
if !flag {
continue
}
2023-11-16 15:06:59 +00:00
2024-01-31 07:13:57 +00:00
idx := rand.Intn(len(location.Destinations))
dest := location.Destinations[idx]
2024-01-24 16:09:39 +00:00
2024-01-31 07:13:57 +00:00
// Passing all the rules means the site is what we are looking for.
// Let us respond to our client!
return makeResponse(ctx, region, &location, &dest)
2023-11-16 15:06:59 +00:00
}
2024-01-31 07:13:57 +00:00
}
2023-11-16 15:06:59 +00:00
2024-01-31 07:13:57 +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
2023-11-16 15:06:59 +00:00
}
func makeResponse(c *fiber.Ctx, region *navi.Region, location *navi.Location, dest *navi.Destination) error {
uri := c.Request().URI().String()
2024-01-25 06:50:08 +00:00
2023-11-17 16:23:40 +00:00
// Modify request
2024-01-24 16:09:39 +00:00
for _, transformer := range dest.Transformers {
if err := transformer.TransformRequest(c); err != nil {
2023-12-16 03:42:14 +00:00
return err
}
2023-11-17 16:23:40 +00:00
}
2023-11-17 17:42:04 +00:00
// Forward
err := navi.R.Forward(c, dest)
2023-11-17 16:23:40 +00:00
// Modify response
2024-01-24 16:09:39 +00:00
for _, transformer := range dest.Transformers {
if err := transformer.TransformResponse(c); err != nil {
2023-12-16 03:42:14 +00:00
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()
}
2024-01-31 05:17:52 +00:00
go navi.R.Metrics.AddTrace(navi.RoadTrace{
2024-01-25 06:46:43 +00:00
Region: region.ID,
Location: location.ID,
Destination: dest.ID,
2024-01-25 06:50:08 +00:00
Uri: uri,
IpAddress: c.IP(),
UserAgent: c.Get(fiber.HeaderUserAgent),
Error: navi.RoadTraceError{
IsNull: err == nil,
2024-01-25 06:46:43 +00:00
Message: message,
},
})
}
2023-11-17 16:23:40 +00:00
return err
2023-11-16 15:06:59 +00:00
}