2023-03-03 20:37:09 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-05 23:21:43 +02:00
|
|
|
"net"
|
2023-03-03 20:37:09 +01:00
|
|
|
"net/http"
|
2023-03-29 17:29:16 +02:00
|
|
|
"strings"
|
2023-03-13 10:44:58 +01:00
|
|
|
"sync"
|
2023-03-28 20:43:45 +02:00
|
|
|
"time"
|
2023-03-03 20:37:09 +01:00
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app/spec"
|
|
|
|
appSpec "forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app/spec"
|
2023-03-29 17:29:16 +02:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/proxy/wildcard"
|
2023-03-03 20:37:09 +01:00
|
|
|
edgeHTTP "forge.cadoles.com/arcad/edge/pkg/http"
|
2023-03-21 15:21:19 +01:00
|
|
|
authHTTP "forge.cadoles.com/arcad/edge/pkg/module/auth/http"
|
2023-03-13 10:44:58 +01:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2023-03-03 20:37:09 +01:00
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bundle"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"github.com/go-chi/chi/v5"
|
2023-03-21 15:21:19 +01:00
|
|
|
"github.com/lestrrat-go/jwx/v2/jwa"
|
2023-03-21 13:28:41 +01:00
|
|
|
"github.com/lestrrat-go/jwx/v2/jwk"
|
2023-03-03 20:37:09 +01:00
|
|
|
"github.com/pkg/errors"
|
2023-03-21 15:21:19 +01:00
|
|
|
|
2023-03-24 23:17:55 +01:00
|
|
|
_ "forge.cadoles.com/Cadoles/emissary/internal/imports/passwd"
|
2023-03-03 20:37:09 +01:00
|
|
|
)
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
const defaultCookieDuration time.Duration = 24 * time.Hour
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
type Server struct {
|
2023-03-28 20:43:45 +02:00
|
|
|
bundle bundle.Bundle
|
|
|
|
handlerOptions []edgeHTTP.HandlerOptionFunc
|
|
|
|
server *http.Server
|
|
|
|
serverMutex sync.RWMutex
|
2023-04-05 23:21:43 +02:00
|
|
|
config *appSpec.Config
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 10:44:58 +01:00
|
|
|
func (s *Server) Start(ctx context.Context, addr string) (err error) {
|
2023-03-31 17:28:52 +02:00
|
|
|
if s.Running() {
|
2023-03-03 20:37:09 +01:00
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:28:52 +02:00
|
|
|
s.serverMutex.Lock()
|
|
|
|
defer s.serverMutex.Unlock()
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
router := chi.NewRouter()
|
|
|
|
|
|
|
|
router.Use(middleware.Logger)
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
handler := edgeHTTP.NewHandler(s.handlerOptions...)
|
2023-03-03 20:37:09 +01:00
|
|
|
if err := handler.Load(s.bundle); err != nil {
|
|
|
|
return errors.Wrap(err, "could not load app bundle")
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
if s.config != nil {
|
|
|
|
if s.config.UnexpectedHostRedirect != nil {
|
|
|
|
router.Use(unexpectedHostRedirect(
|
|
|
|
s.config.UnexpectedHostRedirect.HostTarget,
|
|
|
|
s.config.UnexpectedHostRedirect.AcceptedHostPatterns...,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.config.Auth != nil {
|
|
|
|
if err := s.configureAuth(router, s.config.Auth); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 15:21:19 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
router.Handle("/*", handler)
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2023-03-13 10:44:58 +01:00
|
|
|
defer func() {
|
|
|
|
if recovered := recover(); recovered != nil {
|
|
|
|
if err, ok := recovered.(error); ok {
|
|
|
|
logger.Error(ctx, err.Error(), logger.E(errors.WithStack(err)))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(recovered)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
defer func() {
|
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.server = server
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 10:44:58 +01:00
|
|
|
func (s *Server) Running() bool {
|
|
|
|
s.serverMutex.RLock()
|
|
|
|
defer s.serverMutex.RUnlock()
|
|
|
|
|
|
|
|
return s.server != nil
|
|
|
|
}
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
func (s *Server) Stop() error {
|
2023-03-31 17:28:52 +02:00
|
|
|
if !s.Running() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.serverMutex.Lock()
|
|
|
|
defer s.serverMutex.Unlock()
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
if s.server == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:28:52 +02:00
|
|
|
if err := s.server.Close(); err != nil {
|
2023-03-03 20:37:09 +01:00
|
|
|
s.server = nil
|
|
|
|
|
2023-03-29 17:29:16 +02:00
|
|
|
return errors.WithStack(err)
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-31 17:28:52 +02:00
|
|
|
s.server = nil
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
func (s *Server) configureAuth(router chi.Router, auth *spec.Auth) error {
|
|
|
|
switch {
|
|
|
|
case auth.Local != nil:
|
2023-04-05 23:21:43 +02:00
|
|
|
var rawKey any = auth.Local.Key
|
2023-03-28 20:43:45 +02:00
|
|
|
if strKey, ok := rawKey.(string); ok {
|
|
|
|
rawKey = []byte(strKey)
|
|
|
|
}
|
2023-03-22 18:15:22 +01:00
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
key, err := jwk.FromRaw(rawKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2023-03-13 10:44:58 +01:00
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
cookieDuration := defaultCookieDuration
|
2023-04-05 23:21:43 +02:00
|
|
|
if auth.Local.CookieDuration != "" {
|
|
|
|
cookieDuration, err = time.ParseDuration(auth.Local.CookieDuration)
|
2023-03-28 20:43:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
router.Handle("/auth/*", authHTTP.NewLocalHandler(
|
|
|
|
jwa.HS256, key,
|
|
|
|
authHTTP.WithRoutePrefix("/auth"),
|
2023-04-05 23:21:43 +02:00
|
|
|
authHTTP.WithAccounts(auth.Local.Accounts...),
|
|
|
|
authHTTP.WithCookieOptions(getCookieDomain, cookieDuration),
|
2023-03-28 20:43:45 +02:00
|
|
|
))
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
return nil
|
2023-03-21 13:28:41 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
func NewServer(bundle bundle.Bundle, config *spec.Config, handlerOptions ...edgeHTTP.HandlerOptionFunc) *Server {
|
2023-03-03 20:37:09 +01:00
|
|
|
return &Server{
|
2023-03-28 20:43:45 +02:00
|
|
|
bundle: bundle,
|
2023-04-05 23:21:43 +02:00
|
|
|
config: config,
|
2023-03-28 20:43:45 +02:00
|
|
|
handlerOptions: handlerOptions,
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-29 17:29:16 +02:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
func getCookieDomain(r *http.Request) (string, error) {
|
|
|
|
host, _, err := net.SplitHostPort(r.Host)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If host is an IP address
|
|
|
|
if wildcard.Match(host, "*.*.*.*") {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If host is an domain, return top level domain
|
|
|
|
domainParts := strings.Split(host, ".")
|
|
|
|
if len(domainParts) >= 2 {
|
|
|
|
topLevelDomain := strings.Join(domainParts[len(domainParts)-2:], ".")
|
|
|
|
return topLevelDomain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default, return host
|
|
|
|
return host, nil
|
|
|
|
}
|
2023-03-29 17:29:16 +02:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
func unexpectedHostRedirect(hostTarget string, acceptedHostPatterns ...string) func(http.Handler) http.Handler {
|
2023-03-29 17:29:16 +02:00
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
2023-04-05 23:21:43 +02:00
|
|
|
host, port, err := net.SplitHostPort(r.Host)
|
|
|
|
if err != nil {
|
2023-04-06 10:21:52 +02:00
|
|
|
host = r.Host
|
2023-04-05 23:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
matched := wildcard.MatchAny(host, acceptedHostPatterns...)
|
2023-03-29 17:29:16 +02:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
if !matched {
|
2023-03-29 17:29:16 +02:00
|
|
|
url := r.URL
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
url.Host = hostTarget
|
|
|
|
if port != "" {
|
|
|
|
url.Host += ":" + port
|
2023-03-29 17:29:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|