2023-04-18 19:36:43 +02:00
|
|
|
package proxy
|
2022-11-23 17:58:45 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/openziti/sdk-golang/ziti"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/endpoints"
|
2023-04-18 19:38:32 +02:00
|
|
|
"github.com/openziti/zrok/endpoints/publicProxy/notFoundUi"
|
2023-07-13 20:26:35 +02:00
|
|
|
"github.com/openziti/zrok/environment"
|
2023-11-21 20:27:17 +01:00
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/util"
|
2022-11-23 17:58:45 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-05-25 20:59:39 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2022-11-23 17:58:45 +01:00
|
|
|
)
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
type FrontendConfig struct {
|
2023-04-17 19:55:35 +02:00
|
|
|
IdentityName string
|
|
|
|
ShrToken string
|
|
|
|
Address string
|
2024-01-17 22:37:46 +01:00
|
|
|
Tls *endpoints.TlsConfig
|
2023-04-17 19:55:35 +02:00
|
|
|
RequestsChan chan *endpoints.Request
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
func DefaultFrontendConfig(identityName string) *FrontendConfig {
|
|
|
|
return &FrontendConfig{
|
2023-04-17 19:55:35 +02:00
|
|
|
IdentityName: identityName,
|
|
|
|
Address: "0.0.0.0:8080",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
type Frontend struct {
|
|
|
|
cfg *FrontendConfig
|
2022-11-30 18:46:19 +01:00
|
|
|
zCtx ziti.Context
|
2023-01-04 20:42:58 +01:00
|
|
|
shrToken string
|
2022-11-30 18:46:19 +01:00
|
|
|
handler http.Handler
|
2022-11-23 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
func NewFrontend(cfg *FrontendConfig) (*Frontend, error) {
|
2023-07-13 20:26:35 +02:00
|
|
|
env, err := environment.LoadRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading environment root")
|
|
|
|
}
|
2023-07-14 16:14:32 +02:00
|
|
|
zCfgPath, err := env.ZitiIdentityNamed(cfg.IdentityName)
|
2022-11-23 17:58:45 +01:00
|
|
|
if err != nil {
|
2023-07-10 22:41:16 +02:00
|
|
|
return nil, errors.Wrapf(err, "error getting ziti identity '%v' from environment", cfg.IdentityName)
|
2022-11-23 17:58:45 +01:00
|
|
|
}
|
2023-05-25 17:50:38 +02:00
|
|
|
zCfg, err := ziti.NewConfigFromFile(zCfgPath)
|
2022-11-23 17:58:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading config")
|
|
|
|
}
|
2023-07-17 22:45:20 +02:00
|
|
|
zCfg.ConfigTypes = []string{sdk.ZrokProxyConfig}
|
2023-05-25 17:50:38 +02:00
|
|
|
zCtx, err := ziti.NewContext(zCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading ziti context")
|
|
|
|
}
|
2023-01-04 20:42:58 +01:00
|
|
|
zDialCtx := zitiDialContext{ctx: zCtx, shrToken: cfg.ShrToken}
|
2022-11-23 17:58:45 +01:00
|
|
|
zTransport := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
zTransport.DialContext = zDialCtx.Dial
|
|
|
|
|
|
|
|
proxy, err := newServiceProxy(cfg, zCtx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proxy.Transport = zTransport
|
|
|
|
|
2024-07-22 18:34:42 +02:00
|
|
|
handler := authHandler(cfg.ShrToken, util.NewRequestsWrapper(proxy), "zrok", cfg, zCtx)
|
2023-04-18 19:36:43 +02:00
|
|
|
return &Frontend{
|
2022-11-23 17:58:45 +01:00
|
|
|
cfg: cfg,
|
|
|
|
zCtx: zCtx,
|
|
|
|
handler: handler,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
func (h *Frontend) Run() error {
|
2024-01-17 22:37:46 +01:00
|
|
|
if h.cfg.Tls != nil {
|
|
|
|
return http.ListenAndServeTLS(h.cfg.Address, h.cfg.Tls.CertPath, h.cfg.Tls.KeyPath, h.handler)
|
|
|
|
}
|
2022-11-23 18:39:42 +01:00
|
|
|
return http.ListenAndServe(h.cfg.Address, h.handler)
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:58:45 +01:00
|
|
|
type zitiDialContext struct {
|
2022-11-30 18:46:19 +01:00
|
|
|
ctx ziti.Context
|
2023-01-04 20:42:58 +01:00
|
|
|
shrToken string
|
2022-11-23 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (zdc *zitiDialContext) Dial(_ context.Context, _ string, addr string) (net.Conn, error) {
|
2023-01-04 20:42:58 +01:00
|
|
|
conn, err := zdc.ctx.Dial(zdc.shrToken)
|
2022-11-23 17:58:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
func newServiceProxy(cfg *FrontendConfig, ctx ziti.Context) (*httputil.ReverseProxy, error) {
|
2022-11-23 17:58:45 +01:00
|
|
|
proxy := serviceTargetProxy(cfg, ctx)
|
|
|
|
director := proxy.Director
|
|
|
|
proxy.Director = func(req *http.Request) {
|
2023-01-10 22:38:53 +01:00
|
|
|
if cfg.RequestsChan != nil {
|
2023-01-10 23:40:20 +01:00
|
|
|
cfg.RequestsChan <- &endpoints.Request{
|
2023-01-10 22:38:53 +01:00
|
|
|
Stamp: time.Now(),
|
|
|
|
RemoteAddr: fmt.Sprintf("%v", req.Header["X-Real-Ip"]),
|
|
|
|
Method: req.Method,
|
|
|
|
Path: req.URL.String(),
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 17:58:45 +01:00
|
|
|
director(req)
|
|
|
|
req.Header.Set("X-Proxy", "zrok")
|
|
|
|
}
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
logrus.Errorf("error proxying: %v", err)
|
2022-12-14 20:41:54 +01:00
|
|
|
notFoundUi.WriteNotFound(w)
|
2022-11-23 17:58:45 +01:00
|
|
|
}
|
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
func serviceTargetProxy(cfg *FrontendConfig, ctx ziti.Context) *httputil.ReverseProxy {
|
2022-11-23 17:58:45 +01:00
|
|
|
director := func(req *http.Request) {
|
2023-01-04 20:42:58 +01:00
|
|
|
targetShrToken := cfg.ShrToken
|
|
|
|
if svc, found := endpoints.GetRefreshedService(targetShrToken, ctx); found {
|
2023-07-17 22:45:20 +02:00
|
|
|
if cfg, found := svc.Config[sdk.ZrokProxyConfig]; found {
|
2022-11-23 17:58:45 +01:00
|
|
|
logrus.Debugf("auth model: %v", cfg)
|
|
|
|
} else {
|
|
|
|
logrus.Warn("no config!")
|
|
|
|
}
|
2023-01-04 20:42:58 +01:00
|
|
|
if target, err := url.Parse(fmt.Sprintf("http://%v", targetShrToken)); err == nil {
|
2023-01-10 22:38:53 +01:00
|
|
|
logrus.Debugf("[%v] -> %v", targetShrToken, req.URL)
|
2022-11-23 17:58:45 +01:00
|
|
|
|
|
|
|
targetQuery := target.RawQuery
|
|
|
|
req.URL.Scheme = target.Scheme
|
|
|
|
req.URL.Host = target.Host
|
|
|
|
req.URL.Path, req.URL.RawPath = endpoints.JoinURLPath(target, req.URL)
|
|
|
|
if targetQuery == "" || req.URL.RawQuery == "" {
|
|
|
|
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
|
|
|
} else {
|
|
|
|
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
|
|
|
|
}
|
|
|
|
if _, ok := req.Header["User-Agent"]; !ok {
|
|
|
|
// explicitly disable User-Agent so it's not set to default value
|
|
|
|
req.Header.Set("User-Agent", "")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("error proxying: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:36:43 +02:00
|
|
|
func authHandler(shrToken string, handler http.Handler, realm string, cfg *FrontendConfig, ctx ziti.Context) http.HandlerFunc {
|
2022-11-23 17:58:45 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2023-01-04 20:42:58 +01:00
|
|
|
if svc, found := endpoints.GetRefreshedService(shrToken, ctx); found {
|
2023-07-17 22:45:20 +02:00
|
|
|
if cfg, found := svc.Config[sdk.ZrokProxyConfig]; found {
|
2022-11-23 17:58:45 +01:00
|
|
|
if scheme, found := cfg["auth_scheme"]; found {
|
|
|
|
switch scheme {
|
2023-07-17 22:45:20 +02:00
|
|
|
case string(sdk.None):
|
2023-01-04 20:42:58 +01:00
|
|
|
logrus.Debugf("auth scheme none '%v'", shrToken)
|
2022-11-23 17:58:45 +01:00
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
|
2023-07-17 22:45:20 +02:00
|
|
|
case string(sdk.Basic):
|
2023-01-04 20:42:58 +01:00
|
|
|
logrus.Debugf("auth scheme basic '%v", shrToken)
|
2022-11-23 17:58:45 +01:00
|
|
|
inUser, inPass, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
writeUnauthorizedResponse(w, realm)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authed := false
|
|
|
|
if v, found := cfg["basic_auth"]; found {
|
|
|
|
if basicAuth, ok := v.(map[string]interface{}); ok {
|
|
|
|
if v, found := basicAuth["users"]; found {
|
|
|
|
if arr, ok := v.([]interface{}); ok {
|
|
|
|
for _, v := range arr {
|
|
|
|
if um, ok := v.(map[string]interface{}); ok {
|
|
|
|
username := ""
|
|
|
|
if v, found := um["username"]; found {
|
|
|
|
if un, ok := v.(string); ok {
|
|
|
|
username = un
|
|
|
|
}
|
|
|
|
}
|
|
|
|
password := ""
|
|
|
|
if v, found := um["password"]; found {
|
|
|
|
if pw, ok := v.(string); ok {
|
|
|
|
password = pw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if username == inUser && password == inPass {
|
|
|
|
authed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !authed {
|
|
|
|
writeUnauthorizedResponse(w, realm)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
default:
|
|
|
|
logrus.Infof("invalid auth scheme '%v'", scheme)
|
|
|
|
writeUnauthorizedResponse(w, realm)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-04 20:42:58 +01:00
|
|
|
logrus.Warnf("%v -> no auth scheme for '%v'", r.RemoteAddr, shrToken)
|
2022-12-14 20:41:54 +01:00
|
|
|
notFoundUi.WriteNotFound(w)
|
2022-11-23 17:58:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-04 20:42:58 +01:00
|
|
|
logrus.Warnf("%v -> no proxy config for '%v'", r.RemoteAddr, shrToken)
|
2022-12-14 20:41:54 +01:00
|
|
|
notFoundUi.WriteNotFound(w)
|
2022-11-23 17:58:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-04 20:42:58 +01:00
|
|
|
logrus.Warnf("%v -> service '%v' not found", r.RemoteAddr, shrToken)
|
2022-12-14 20:41:54 +01:00
|
|
|
notFoundUi.WriteNotFound(w)
|
2022-11-23 17:58:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeUnauthorizedResponse(w http.ResponseWriter, realm string) {
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
|
w.WriteHeader(401)
|
|
|
|
w.Write([]byte("No Authorization\n"))
|
|
|
|
}
|