Checkpoint
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"git.maze.io/maze/styx/internal/netutil"
|
||||
"git.maze.io/maze/styx/logger"
|
||||
proxy "git.maze.io/maze/styx/proxy"
|
||||
)
|
||||
@@ -26,17 +28,66 @@ func NewRequestHandler(p *Policy) proxy.RequestHandler {
|
||||
})
|
||||
}
|
||||
|
||||
func NewDialHandler(p *Policy) proxy.DialHandler {
|
||||
log := logger.StandardLog.Value("policy", p.name)
|
||||
return proxy.DialHandlerFunc(func(ctx proxy.Context, req *http.Request) (net.Conn, error) {
|
||||
input := NewInputFromRequest(ctx, req)
|
||||
input.logValues(log).Trace("Running dial handler")
|
||||
result, err := p.Query(input)
|
||||
if err != nil {
|
||||
log.Err(err).Error("Error evaulating policy")
|
||||
return nil, nil
|
||||
}
|
||||
r, err := result.Response(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Error("Error generating response")
|
||||
return nil, nil
|
||||
}
|
||||
if r == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
c := netutil.NewLoopback()
|
||||
|
||||
go func(c net.Conn) {
|
||||
s := &http.Server{
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
r.Write(w)
|
||||
}),
|
||||
}
|
||||
_ = s.Serve(&netutil.AcceptOnce{Conn: c})
|
||||
}(c.Server)
|
||||
|
||||
return c.Client, nil
|
||||
})
|
||||
}
|
||||
|
||||
func NewForwardHandler(p *Policy) proxy.ForwardHandler {
|
||||
log := logger.StandardLog.Value("policy", p.name)
|
||||
return proxy.ForwardHandlerFunc(func(ctx proxy.Context, req *http.Request) (*http.Response, error) {
|
||||
input := NewInputFromRequest(ctx, req)
|
||||
input.logValues(log).Trace("Running forward handler")
|
||||
result, err := p.Query(input)
|
||||
if err != nil {
|
||||
log.Err(err).Error("Error evaulating policy")
|
||||
return nil, nil
|
||||
}
|
||||
return result.Response(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
func NewResponseHandler(p *Policy) proxy.ResponseHandler {
|
||||
log := logger.StandardLog.Value("policy", p.name)
|
||||
return proxy.ResponseHandlerFunc(func(ctx proxy.Context) *http.Response {
|
||||
input := NewInputFromResponse(ctx, ctx.Response())
|
||||
result, err := p.Query(input)
|
||||
if err != nil {
|
||||
logger.StandardLog.Err(err).Error("Error evaulating policy")
|
||||
log.Err(err).Error("Error evaulating policy")
|
||||
return nil
|
||||
}
|
||||
r, err := result.Response(ctx)
|
||||
if err != nil {
|
||||
logger.StandardLog.Err(err).Error("Error generating response")
|
||||
log.Err(err).Error("Error generating response")
|
||||
return nil
|
||||
}
|
||||
return r
|
||||
|
Reference in New Issue
Block a user