RoadSign/pkg/hypertext/proxies.go

107 lines
2.1 KiB
Go
Raw Normal View History

2023-11-16 15:06:59 +00:00
package hypertext
import (
2023-11-17 17:42:04 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
2023-11-16 15:06:59 +00:00
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"regexp"
)
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
2023-11-17 17:42:04 +00:00
for _, site := range sign.C.Sites {
2023-11-16 15:06:59 +00:00
// Matching rules
for _, rule := range site.Rules {
if !lo.Contains(rule.Host, host) {
continue
}
2023-11-17 17:42:04 +00:00
if !func() bool {
flag := false
for _, pattern := range rule.Path {
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
2023-11-16 15:06:59 +00:00
for rk, rv := range rule.Queries {
for ik, iv := range queries {
if rk != ik && rv != iv {
flag = false
break
}
}
if !flag {
break
}
}
if !flag {
continue
}
// Filter headers
for rk, rv := range rule.Headers {
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
}
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!
return makeResponse(ctx, site)
}
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
})
}
2023-11-17 17:42:04 +00:00
func makeResponse(ctx *fiber.Ctx, site sign.SiteConfig) error {
2023-11-17 16:23:40 +00:00
// Modify request
for _, transformer := range site.Transformers {
transformer.TransformRequest(ctx)
}
2023-11-17 17:42:04 +00:00
// Forward
err := sign.C.Forward(ctx, site)
2023-11-17 16:23:40 +00:00
// Modify response
for _, transformer := range site.Transformers {
transformer.TransformResponse(ctx)
}
return err
2023-11-16 15:06:59 +00:00
}