2022-12-14 20:40:45 +01:00
|
|
|
package proxyBackend
|
2022-07-20 19:18:40 +02:00
|
|
|
|
2022-07-20 19:41:23 +02:00
|
|
|
import (
|
2022-10-10 22:25:35 +02:00
|
|
|
"context"
|
2023-01-09 23:24:35 +01:00
|
|
|
"fmt"
|
2022-07-20 19:41:23 +02:00
|
|
|
"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"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/endpoints"
|
|
|
|
"github.com/openziti/zrok/util"
|
2022-07-20 19:41:23 +02:00
|
|
|
"github.com/pkg/errors"
|
2022-08-10 21:16:36 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-10-10 22:25:35 +02:00
|
|
|
"net"
|
2022-07-20 19:41:23 +02:00
|
|
|
"net/http"
|
2022-08-10 21:16:36 +02:00
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2022-07-20 19:41:23 +02:00
|
|
|
"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
|
2022-12-13 17:17:18 +01:00
|
|
|
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) {
|
2022-07-20 19:41:23 +02:00
|
|
|
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")
|
2022-07-20 19:41:23 +02:00
|
|
|
}
|
2023-01-04 20:42:58 +01:00
|
|
|
listener, err := ziti.NewContextWithConfig(zcfg).ListenWithOptions(cfg.ShrToken, &options)
|
2022-07-20 19:41:23 +02:00
|
|
|
if err != nil {
|
2022-08-09 19:57:45 +02:00
|
|
|
return nil, errors.Wrap(err, "error listening")
|
2022-07-20 19:41:23 +02:00
|
|
|
}
|
|
|
|
|
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-07-20 19:41:23 +02:00
|
|
|
}
|
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,
|
2022-12-13 17:17:18 +01:00
|
|
|
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-20 19:41:23 +02:00
|
|
|
}
|
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 {
|
2022-12-13 17:17:18 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-10 22:25:35 +02:00
|
|
|
tpt := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
tpt.DialContext = metricsDial
|
|
|
|
|
2022-08-10 21:16:36 +02:00
|
|
|
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
2022-10-10 22:25:35 +02:00
|
|
|
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
|
|
|
|
}
|
2022-10-10 22:25:35 +02:00
|
|
|
|
|
|
|
func metricsDial(_ context.Context, network string, addr string) (net.Conn, error) {
|
|
|
|
conn, err := net.Dial(network, addr)
|
|
|
|
if err != nil {
|
|
|
|
return conn, err
|
|
|
|
}
|
2022-10-18 19:43:05 +02:00
|
|
|
return newMetricsConn("backend", conn), nil
|
2022-10-10 22:25:35 +02:00
|
|
|
}
|