2021-01-16 20:41:39 +00:00
|
|
|
package proxy
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2021-07-29 09:30:30 +00:00
|
|
|
"fmt"
|
2020-09-02 22:04:12 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-02-11 22:48:54 +00:00
|
|
|
"strings"
|
2020-09-02 22:04:12 +00:00
|
|
|
"time"
|
|
|
|
|
2021-07-29 09:30:30 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
2020-09-02 22:04:12 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-06-16 15:29:01 +00:00
|
|
|
"goauthentik.io/internal/crypto"
|
2021-06-16 10:02:02 +00:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
2021-07-29 09:30:30 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2020-09-02 22:04:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server represents an HTTP server
|
|
|
|
type Server struct {
|
|
|
|
Handlers map[string]*providerBundle
|
2021-07-29 09:30:30 +00:00
|
|
|
Listen string
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2021-01-16 20:41:39 +00:00
|
|
|
stop chan struct{} // channel for waiting shutdown
|
|
|
|
logger *log.Entry
|
|
|
|
ak *ak.APIController
|
2021-07-21 21:53:43 +00:00
|
|
|
cs *ak.CryptoStore
|
2020-09-02 22:04:12 +00:00
|
|
|
defaultCert tls.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer initialise a new HTTP Server
|
2021-01-16 20:41:39 +00:00
|
|
|
func NewServer(ac *ak.APIController) *Server {
|
2021-06-16 15:29:01 +00:00
|
|
|
defaultCert, err := crypto.GenerateSelfSignedCert()
|
2020-09-02 22:04:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warning(err)
|
|
|
|
}
|
|
|
|
return &Server{
|
|
|
|
Handlers: make(map[string]*providerBundle),
|
2021-07-29 09:30:30 +00:00
|
|
|
Listen: "0.0.0.0:%d",
|
2021-02-11 22:48:54 +00:00
|
|
|
logger: log.WithField("logger", "authentik.outpost.proxy-http-server"),
|
2020-09-02 22:04:12 +00:00
|
|
|
defaultCert: defaultCert,
|
2021-01-16 20:41:39 +00:00
|
|
|
ak: ac,
|
2021-07-21 21:53:43 +00:00
|
|
|
cs: ak.NewCryptoStore(ac.Client.CryptoApi),
|
2020-09-02 22:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 09:30:30 +00:00
|
|
|
// ServeHTTP constructs a net.Listener and starts handling HTTP requests
|
|
|
|
func (s *Server) ServeHTTP() {
|
|
|
|
listenAddress := fmt.Sprintf(s.Listen, 4180)
|
|
|
|
listener, err := net.Listen("tcp", listenAddress)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Fatalf("FATAL: listen (%s) failed - %s", listenAddress, err)
|
|
|
|
}
|
|
|
|
proxyListener := &proxyproto.Listener{Listener: listener}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
|
|
|
|
s.logger.Printf("listening on %s", listener.Addr())
|
|
|
|
s.serve(proxyListener)
|
|
|
|
s.logger.Printf("closing %s", listener.Addr())
|
|
|
|
}
|
|
|
|
|
2021-08-21 14:17:30 +00:00
|
|
|
func (s *Server) TimerFlowCacheExpiry() {}
|
|
|
|
|
2021-07-29 09:30:30 +00:00
|
|
|
func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
|
2020-12-05 21:08:42 +00:00
|
|
|
if r.URL.Path == "/akprox/ping" {
|
2020-09-19 09:43:22 +00:00
|
|
|
w.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
2021-07-29 09:30:30 +00:00
|
|
|
host := web.GetHost(r)
|
2021-04-23 12:07:44 +00:00
|
|
|
handler, ok := s.Handlers[host]
|
2020-09-02 22:04:12 +00:00
|
|
|
if !ok {
|
|
|
|
// If we only have one handler, host name switching doesn't matter
|
|
|
|
if len(s.Handlers) == 1 {
|
|
|
|
for k := range s.Handlers {
|
|
|
|
s.Handlers[k].ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 22:48:54 +00:00
|
|
|
// Get a list of all host keys we know
|
|
|
|
hostKeys := make([]string, 0, len(s.Handlers))
|
|
|
|
for k := range s.Handlers {
|
|
|
|
hostKeys = append(hostKeys, k)
|
|
|
|
}
|
2021-05-11 18:02:36 +00:00
|
|
|
s.logger.WithField("host", host).WithField("known-hosts", strings.Join(hostKeys, ",")).Debug("Host header does not match any we know of")
|
2021-02-11 22:48:54 +00:00
|
|
|
w.WriteHeader(404)
|
2020-09-02 22:04:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) serve(listener net.Listener) {
|
2021-07-29 09:30:30 +00:00
|
|
|
srv := &http.Server{Handler: http.HandlerFunc(s.Handler)}
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
// See https://golang.org/pkg/net/http/#Server.Shutdown
|
|
|
|
idleConnsClosed := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
<-s.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:
|
|
|
|
s.logger.Printf("HTTP server Shutdown: %v", err)
|
|
|
|
}
|
|
|
|
close(idleConnsClosed)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := srv.Serve(listener)
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
s.logger.Errorf("ERROR: http.Serve() - %s", err)
|
|
|
|
}
|
|
|
|
<-idleConnsClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
|
|
|
|
// connections. It's used by ListenAndServe and ListenAndServeTLS so
|
|
|
|
// dead TCP connections (e.g. closing laptop mid-download) eventually
|
|
|
|
// go away.
|
|
|
|
type tcpKeepAliveListener struct {
|
|
|
|
*net.TCPListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
|
|
|
|
tc, err := ln.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = tc.SetKeepAlive(true)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error setting Keep-Alive: %v", err)
|
|
|
|
}
|
|
|
|
err = tc.SetKeepAlivePeriod(3 * time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error setting Keep-Alive period: %v", err)
|
|
|
|
}
|
|
|
|
return tc, nil
|
|
|
|
}
|