zrok/endpoints/proxyBackend/http.go

111 lines
2.5 KiB
Go
Raw Normal View History

2022-12-14 20:40:45 +01:00
package proxyBackend
2022-07-20 19:18:40 +02:00
import (
"context"
"fmt"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
2022-08-09 19:57:45 +02:00
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/util"
"github.com/pkg/errors"
2022-08-10 21:16:36 +02:00
"github.com/sirupsen/logrus"
"net"
"net/http"
2022-08-10 21:16:36 +02:00
"net/http/httputil"
"net/url"
"time"
)
2022-08-10 21:16:36 +02:00
type Config struct {
IdentityPath string
EndpointAddress string
2023-01-04 20:42:58 +01:00
ShrToken string
2023-01-10 23:40:20 +01:00
RequestsChan chan *endpoints.Request
2022-08-10 21:16:36 +02:00
}
2022-12-13 18:51:04 +01:00
type backend struct {
2022-08-18 22:57:38 +02:00
cfg *Config
requests func() int32
2022-08-09 19:57:45 +02:00
listener edge.Listener
handler http.Handler
}
2022-12-13 18:51:04 +01:00
func NewBackend(cfg *Config) (*backend, error) {
options := ziti.ListenOptions{
ConnectTimeout: 5 * time.Minute,
MaxConnections: 64,
}
zcfg, err := config.NewFromFile(cfg.IdentityPath)
if err != nil {
2022-08-09 19:57:45 +02:00
return nil, errors.Wrap(err, "error loading config")
}
2023-01-04 20:42:58 +01:00
listener, err := ziti.NewContextWithConfig(zcfg).ListenWithOptions(cfg.ShrToken, &options)
if err != nil {
2022-08-09 19:57:45 +02:00
return nil, errors.Wrap(err, "error listening")
}
2023-01-10 20:52:28 +01:00
proxy, err := newReverseProxy(cfg.EndpointAddress, cfg.RequestsChan)
2022-07-21 21:46:14 +02:00
if err != nil {
2022-08-09 19:57:45 +02:00
return nil, err
}
2022-08-09 19:57:45 +02:00
handler := util.NewProxyHandler(proxy)
2022-12-13 18:51:04 +01:00
return &backend{
2022-08-18 22:57:38 +02:00
cfg: cfg,
requests: handler.Requests,
2022-08-09 19:57:45 +02:00
listener: listener,
handler: handler,
}, nil
}
2022-12-13 18:51:04 +01:00
func (self *backend) Run() error {
2022-08-18 22:57:38 +02:00
if err := http.Serve(self.listener, self.handler); err != nil {
2022-07-21 21:46:14 +02:00
return err
}
2022-07-21 21:46:14 +02:00
return nil
}
2022-08-10 21:16:36 +02:00
2022-12-13 18:51:04 +01:00
func (self *backend) Requests() func() int32 {
return self.requests
}
2023-01-10 23:40:20 +01:00
func newReverseProxy(target string, requests chan *endpoints.Request) (*httputil.ReverseProxy, error) {
2022-08-10 21:16:36 +02:00
targetURL, err := url.Parse(target)
if err != nil {
return nil, err
}
tpt := http.DefaultTransport.(*http.Transport).Clone()
tpt.DialContext = metricsDial
2022-08-10 21:16:36 +02:00
proxy := httputil.NewSingleHostReverseProxy(targetURL)
proxy.Transport = tpt
2022-08-10 21:16:36 +02:00
director := proxy.Director
proxy.Director = func(req *http.Request) {
2023-01-10 20:52:28 +01:00
if requests != nil {
2023-01-10 23:40:20 +01:00
requests <- &endpoints.Request{
2023-01-10 20:52:28 +01:00
Stamp: time.Now(),
RemoteAddr: fmt.Sprintf("%v", req.Header["X-Real-Ip"]),
Method: req.Method,
Path: req.URL.String(),
}
}
2022-08-10 21:16:36 +02:00
director(req)
req.Header.Set("X-Proxy", "zrok")
}
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
logrus.Errorf("error proxying: %v", err)
}
return proxy, nil
}
func metricsDial(_ context.Context, network string, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return conn, err
}
return newMetricsConn("backend", conn), nil
}