2024-06-25 14:03:49 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-09-24 15:46:42 +02:00
|
|
|
"context"
|
2024-06-25 14:03:49 +02:00
|
|
|
"fmt"
|
2024-09-25 15:50:13 +02:00
|
|
|
"net/http"
|
2024-06-25 14:03:49 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
2024-09-24 15:46:42 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
2024-06-25 14:03:49 +02:00
|
|
|
"github.com/expr-lang/expr"
|
2024-09-24 15:46:42 +02:00
|
|
|
"github.com/pkg/errors"
|
2024-06-25 14:03:49 +02:00
|
|
|
)
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
func addResponseHeaderFunc() expr.Option {
|
2024-06-25 14:03:49 +02:00
|
|
|
return expr.Function(
|
|
|
|
"add_header",
|
|
|
|
func(params ...any) (any, error) {
|
2024-09-24 15:46:42 +02:00
|
|
|
ctx, err := rule.Assert[context.Context](params[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := rule.Assert[string](params[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawValue := params[2]
|
2024-06-25 14:03:49 +02:00
|
|
|
|
|
|
|
var value string
|
|
|
|
switch v := rawValue.(type) {
|
|
|
|
case []string:
|
|
|
|
value = strings.Join(v, ",")
|
|
|
|
case time.Time:
|
|
|
|
value = strconv.FormatInt(v.UTC().Unix(), 10)
|
|
|
|
case time.Duration:
|
|
|
|
value = strconv.FormatInt(int64(v.Seconds()), 10)
|
|
|
|
default:
|
|
|
|
value = fmt.Sprintf("%v", rawValue)
|
|
|
|
}
|
|
|
|
|
2024-09-25 15:50:13 +02:00
|
|
|
r, ok := CtxResponse(ctx)
|
2024-09-24 15:46:42 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not find http response in context")
|
|
|
|
}
|
|
|
|
|
2024-06-25 14:03:49 +02:00
|
|
|
r.Header.Add(name, value)
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
},
|
2024-09-24 15:46:42 +02:00
|
|
|
new(func(context.Context, string, string) bool),
|
2024-06-25 14:03:49 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
func setResponseHeaderFunc() expr.Option {
|
2024-06-25 14:03:49 +02:00
|
|
|
return expr.Function(
|
|
|
|
"set_header",
|
|
|
|
func(params ...any) (any, error) {
|
2024-09-24 15:46:42 +02:00
|
|
|
ctx, err := rule.Assert[context.Context](params[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := rule.Assert[string](params[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawValue := params[2]
|
2024-06-25 14:03:49 +02:00
|
|
|
|
|
|
|
var value string
|
|
|
|
switch v := rawValue.(type) {
|
|
|
|
case []string:
|
|
|
|
value = strings.Join(v, ",")
|
|
|
|
case time.Time:
|
|
|
|
value = strconv.FormatInt(v.UTC().Unix(), 10)
|
|
|
|
case time.Duration:
|
|
|
|
value = strconv.FormatInt(int64(v.Seconds()), 10)
|
|
|
|
default:
|
|
|
|
value = fmt.Sprintf("%v", rawValue)
|
|
|
|
}
|
|
|
|
|
2024-09-25 15:50:13 +02:00
|
|
|
r, ok := CtxResponse(ctx)
|
2024-09-24 15:46:42 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not find http response in context")
|
|
|
|
}
|
|
|
|
|
2024-06-25 14:03:49 +02:00
|
|
|
r.Header.Set(name, value)
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
},
|
2024-09-24 15:46:42 +02:00
|
|
|
new(func(context.Context, string, string) bool),
|
2024-06-25 14:03:49 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
func delResponseHeadersFunc() expr.Option {
|
2024-06-25 14:03:49 +02:00
|
|
|
return expr.Function(
|
|
|
|
"del_headers",
|
|
|
|
func(params ...any) (any, error) {
|
2024-09-24 15:46:42 +02:00
|
|
|
ctx, err := rule.Assert[context.Context](params[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern, err := rule.Assert[string](params[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2024-09-25 15:50:13 +02:00
|
|
|
r, ok := CtxResponse(ctx)
|
2024-09-24 15:46:42 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not find http response in context")
|
|
|
|
}
|
|
|
|
|
2024-06-25 14:03:49 +02:00
|
|
|
deleted := false
|
|
|
|
|
|
|
|
for key := range r.Header {
|
|
|
|
if !wildcard.Match(key, pattern) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Del(key)
|
|
|
|
deleted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return deleted, nil
|
|
|
|
},
|
2024-09-24 15:46:42 +02:00
|
|
|
new(func(context.Context, string) bool),
|
2024-06-25 14:03:49 +02:00
|
|
|
)
|
|
|
|
}
|
2024-09-25 15:50:13 +02:00
|
|
|
|
|
|
|
func addResponseCookieFunc() expr.Option {
|
|
|
|
return expr.Function(
|
|
|
|
"add_cookie",
|
|
|
|
func(params ...any) (any, error) {
|
|
|
|
ctx, err := rule.Assert[context.Context](params[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
values, err := rule.Assert[map[string]any](params[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie, err := cookieFrom(values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, ok := CtxResponse(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not find http request in context")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Add("Set-Cookie", cookie.String())
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
new(func(context.Context, map[string]any) bool),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getResponseCookieFunc() expr.Option {
|
|
|
|
return expr.Function(
|
|
|
|
"get_cookie",
|
|
|
|
func(params ...any) (any, error) {
|
|
|
|
ctx, err := rule.Assert[context.Context](params[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := rule.Assert[string](params[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok := CtxResponse(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not find http response in context")
|
|
|
|
}
|
|
|
|
|
|
|
|
var cookie *http.Cookie
|
|
|
|
for _, c := range res.Cookies() {
|
|
|
|
if c.Name != name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie = c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if cookie == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return CookieVar{
|
|
|
|
Name: cookie.Name,
|
|
|
|
Value: cookie.Value,
|
|
|
|
Path: cookie.Path,
|
|
|
|
Domain: cookie.Domain,
|
|
|
|
Expires: cookie.Expires,
|
|
|
|
MaxAge: cookie.MaxAge,
|
|
|
|
Secure: cookie.Secure,
|
|
|
|
HttpOnly: cookie.HttpOnly,
|
|
|
|
SameSite: cookie.SameSite,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
new(func(context.Context, string) CookieVar),
|
|
|
|
)
|
|
|
|
}
|