2021-09-08 18:04:56 +00:00
|
|
|
package proxyv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2021-12-16 10:00:19 +00:00
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2021-09-08 18:04:56 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/pires/go-proxyproto"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2022-08-03 19:33:27 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/crypto"
|
|
|
|
"goauthentik.io/internal/outpost/ak"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/application"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/metrics"
|
2022-06-10 20:19:48 +00:00
|
|
|
sentryutils "goauthentik.io/internal/utils/sentry"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProxyServer struct {
|
|
|
|
defaultCert tls.Certificate
|
|
|
|
stop chan struct{} // channel for waiting shutdown
|
|
|
|
|
|
|
|
cryptoStore *ak.CryptoStore
|
|
|
|
apps map[string]*application.Application
|
|
|
|
log *log.Entry
|
|
|
|
mux *mux.Router
|
|
|
|
akAPI *ak.APIController
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:33:27 +00:00
|
|
|
func NewProxyServer(ac *ak.APIController) *ProxyServer {
|
2021-09-08 18:04:56 +00:00
|
|
|
l := log.WithField("logger", "authentik.outpost.proxyv2")
|
|
|
|
defaultCert, err := crypto.GenerateSelfSignedCert()
|
|
|
|
if err != nil {
|
|
|
|
l.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rootMux := mux.NewRouter()
|
|
|
|
rootMux.Use(func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
h.ServeHTTP(rw, r)
|
2022-01-24 21:05:11 +00:00
|
|
|
rw.Header().Set("X-Powered-By", "authentik_proxy2")
|
2021-09-08 18:04:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
globalMux := rootMux.NewRoute().Subrouter()
|
|
|
|
globalMux.Use(web.NewLoggingHandler(l.WithField("logger", "authentik.outpost.proxyv2.http"), nil))
|
2021-12-16 10:00:19 +00:00
|
|
|
globalMux.Use(sentryhttp.New(sentryhttp.Options{}).Handle)
|
2021-09-08 18:04:56 +00:00
|
|
|
s := &ProxyServer{
|
|
|
|
cryptoStore: ak.NewCryptoStore(ac.Client.CryptoApi),
|
|
|
|
apps: make(map[string]*application.Application),
|
|
|
|
log: l,
|
|
|
|
mux: rootMux,
|
|
|
|
akAPI: ac,
|
|
|
|
defaultCert: defaultCert,
|
|
|
|
}
|
2022-02-08 19:25:38 +00:00
|
|
|
globalMux.PathPrefix("/outpost.goauthentik.io/static").HandlerFunc(s.HandleStatic)
|
2022-06-10 20:19:48 +00:00
|
|
|
globalMux.Path("/outpost.goauthentik.io/ping").HandlerFunc(sentryutils.SentryNoSample(s.HandlePing))
|
2021-09-08 18:04:56 +00:00
|
|
|
rootMux.PathPrefix("/").HandlerFunc(s.Handle)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-01-18 22:19:43 +00:00
|
|
|
func (ps *ProxyServer) HandleHost(rw http.ResponseWriter, r *http.Request) bool {
|
2022-08-17 11:36:56 +00:00
|
|
|
a, _ := ps.lookupApp(r)
|
2022-08-18 18:43:01 +00:00
|
|
|
if a == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a.HasQuerySignature(r) || a.Mode() == api.PROXYMODE_PROXY {
|
|
|
|
a.ServeHTTP(rw, r)
|
|
|
|
return true
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 08:14:51 +00:00
|
|
|
func (ps *ProxyServer) Type() string {
|
|
|
|
return "proxy"
|
|
|
|
}
|
|
|
|
|
2023-01-23 19:36:30 +00:00
|
|
|
func (ps *ProxyServer) TimerFlowCacheExpiry(context.Context) {}
|
2021-09-08 18:04:56 +00:00
|
|
|
|
2021-12-22 09:16:01 +00:00
|
|
|
func (ps *ProxyServer) GetCertificate(serverName string) *tls.Certificate {
|
|
|
|
app, ok := ps.apps[serverName]
|
2021-09-08 18:04:56 +00:00
|
|
|
if !ok {
|
2022-01-25 14:45:21 +00:00
|
|
|
ps.log.WithField("server-name", serverName).Debug("failed to get certificate for ServerName")
|
2021-12-22 09:16:01 +00:00
|
|
|
return nil
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
if app.Cert == nil {
|
2021-12-22 09:16:01 +00:00
|
|
|
ps.log.WithField("server-name", serverName).Debug("app does not have a certificate")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return app.Cert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProxyServer) getCertificates(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2022-02-09 11:33:25 +00:00
|
|
|
sn := info.ServerName
|
|
|
|
if sn == "" {
|
|
|
|
return &ps.defaultCert, nil
|
|
|
|
}
|
|
|
|
appCert := ps.GetCertificate(sn)
|
2021-12-22 09:16:01 +00:00
|
|
|
if appCert == nil {
|
2021-09-08 18:04:56 +00:00
|
|
|
return &ps.defaultCert, nil
|
|
|
|
}
|
2021-12-22 09:16:01 +00:00
|
|
|
return appCert, nil
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP constructs a net.Listener and starts handling HTTP requests
|
|
|
|
func (ps *ProxyServer) ServeHTTP() {
|
2022-08-03 19:33:27 +00:00
|
|
|
listenAddress := config.Get().Listen.HTTP
|
2021-09-08 18:04:56 +00:00
|
|
|
listener, err := net.Listen("tcp", listenAddress)
|
|
|
|
if err != nil {
|
2021-12-11 21:09:18 +00:00
|
|
|
ps.log.WithField("listen", listenAddress).WithError(err).Fatalf("listen failed")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
proxyListener := &proxyproto.Listener{Listener: listener}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
|
2021-12-22 09:33:21 +00:00
|
|
|
ps.log.WithField("listen", listenAddress).Info("Starting HTTP server")
|
2021-09-08 18:04:56 +00:00
|
|
|
ps.serve(proxyListener)
|
2021-12-22 09:33:21 +00:00
|
|
|
ps.log.WithField("listen", listenAddress).Info("Stopping HTTP server")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTPS constructs a net.Listener and starts handling HTTPS requests
|
|
|
|
func (ps *ProxyServer) ServeHTTPS() {
|
2022-08-03 19:33:27 +00:00
|
|
|
listenAddress := config.Get().Listen.HTTPS
|
2021-09-08 18:04:56 +00:00
|
|
|
config := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
MaxVersion: tls.VersionTLS12,
|
|
|
|
GetCertificate: ps.getCertificates,
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", listenAddress)
|
|
|
|
if err != nil {
|
2022-01-03 11:36:11 +00:00
|
|
|
ps.log.WithError(err).Warning("Failed to listen (TLS)")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2021-12-22 09:33:21 +00:00
|
|
|
proxyListener := &proxyproto.Listener{Listener: web.TCPKeepAliveListener{TCPListener: ln.(*net.TCPListener)}}
|
2021-09-08 18:04:56 +00:00
|
|
|
defer proxyListener.Close()
|
|
|
|
|
|
|
|
tlsListener := tls.NewListener(proxyListener, config)
|
2021-12-22 09:33:21 +00:00
|
|
|
ps.log.WithField("listen", listenAddress).Info("Starting HTTPS server")
|
2021-09-08 18:04:56 +00:00
|
|
|
ps.serve(tlsListener)
|
2021-12-22 09:33:21 +00:00
|
|
|
ps.log.WithField("listen", listenAddress).Info("Stopping HTTPS server")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProxyServer) Start() error {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(3)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ps.ServeHTTP()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ps.ServeHTTPS()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
metrics.RunServer()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-23 19:36:30 +00:00
|
|
|
func (ps *ProxyServer) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
func (ps *ProxyServer) serve(listener net.Listener) {
|
|
|
|
srv := &http.Server{Handler: ps.mux}
|
|
|
|
|
|
|
|
// See https://golang.org/pkg/net/http/#Server.Shutdown
|
|
|
|
idleConnsClosed := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
<-ps.stop // wait notification for stopping server
|
|
|
|
|
|
|
|
// We received an interrupt signal, shut down.
|
|
|
|
if err := srv.Shutdown(context.Background()); err != nil {
|
|
|
|
// Error from closing listeners, or context timeout:
|
2021-12-22 09:33:21 +00:00
|
|
|
ps.log.WithError(err).Info("HTTP server Shutdown")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
close(idleConnsClosed)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := srv.Serve(listener)
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
ps.log.Errorf("ERROR: http.Serve() - %s", err)
|
|
|
|
}
|
|
|
|
<-idleConnsClosed
|
|
|
|
}
|