2021-09-08 18:04:56 +00:00
|
|
|
package proxyv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"goauthentik.io/api"
|
|
|
|
"goauthentik.io/internal/crypto"
|
|
|
|
"goauthentik.io/internal/outpost/ak"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/application"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/metrics"
|
|
|
|
"goauthentik.io/internal/utils/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProxyServer struct {
|
|
|
|
Listen string
|
|
|
|
PortOffset int
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-09 08:23:46 +00:00
|
|
|
func NewProxyServer(ac *ak.APIController, portOffset int) *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)
|
|
|
|
rw.Header().Set("Server", "authentik_proxy2")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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{
|
|
|
|
Listen: "0.0.0.0:%d",
|
2021-09-09 08:23:46 +00:00
|
|
|
PortOffset: portOffset,
|
2021-09-08 18:04:56 +00:00
|
|
|
|
|
|
|
cryptoStore: ak.NewCryptoStore(ac.Client.CryptoApi),
|
|
|
|
apps: make(map[string]*application.Application),
|
|
|
|
log: l,
|
|
|
|
mux: rootMux,
|
|
|
|
akAPI: ac,
|
|
|
|
defaultCert: defaultCert,
|
|
|
|
}
|
|
|
|
globalMux.PathPrefix("/akprox/static").HandlerFunc(s.HandleStatic)
|
2021-12-20 21:12:02 +00:00
|
|
|
globalMux.Path("/akprox/ping").HandlerFunc(s.HandlePing)
|
2021-09-08 18:04:56 +00:00
|
|
|
rootMux.PathPrefix("/").HandlerFunc(s.Handle)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProxyServer) HandleHost(host string, rw http.ResponseWriter, r *http.Request) bool {
|
|
|
|
if app, ok := ps.apps[host]; ok {
|
|
|
|
if app.Mode() == api.PROXYMODE_PROXY {
|
|
|
|
ps.log.WithField("host", host).Trace("routing to proxy outpost")
|
|
|
|
app.ServeHTTP(rw, r)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 08:14:51 +00:00
|
|
|
func (ps *ProxyServer) Type() string {
|
|
|
|
return "proxy"
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
func (ps *ProxyServer) TimerFlowCacheExpiry() {}
|
|
|
|
|
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 {
|
2021-12-22 09:16:01 +00:00
|
|
|
ps.log.WithField("server-name", serverName).Debug("app does not exist")
|
|
|
|
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) {
|
|
|
|
appCert := ps.GetCertificate(info.ServerName)
|
|
|
|
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() {
|
|
|
|
listenAddress := fmt.Sprintf(ps.Listen, 9000+ps.PortOffset)
|
|
|
|
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() {
|
|
|
|
listenAddress := fmt.Sprintf(ps.Listen, 9443+ps.PortOffset)
|
|
|
|
config := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
MaxVersion: tls.VersionTLS12,
|
|
|
|
GetCertificate: ps.getCertificates,
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", listenAddress)
|
|
|
|
if err != nil {
|
2021-12-22 09:33:21 +00:00
|
|
|
ps.log.WithError(err).Warning("Failed to listen for HTTPS")
|
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.log.Debug("Starting HTTP Server...")
|
|
|
|
ps.ServeHTTP()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ps.log.Debug("Starting HTTPs Server...")
|
|
|
|
ps.ServeHTTPS()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ps.log.Debug("Starting Metrics Server...")
|
|
|
|
metrics.RunServer()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|