| 1 |
package daemon |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log/slog" |
| 7 |
"net/http" |
| 8 |
"net/http/pprof" |
| 9 |
|
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 11 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/crypto" |
| 12 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/httphandler" |
| 13 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/httpserver" |
| 14 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2" |
| 15 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers" |
| 16 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/openvpn" |
| 17 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/tokenstorage" |
| 18 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/utils" |
| 19 |
) |
| 20 |
|
| 21 |
// New wires the daemon services for one loaded configuration. |
| 22 |
func New(ctx context.Context, logger *slog.Logger, conf *config.Config, tokenStorage tokenstorage.Storage) (*Runtime, error) { |
| 23 |
openvpnClient, httpHandler, err := setupOpenVPNClient(ctx, logger, conf, tokenStorage) |
| 24 |
if err != nil { |
| 25 |
return nil, fmt.Errorf("error setting up openvpn client: %w", err) |
| 26 |
} |
| 27 |
|
| 28 |
server := httpserver.NewHTTPServer(httpserver.ServerNameDefault, logger, conf.HTTP, httpHandler) |
| 29 |
services := []service{ |
| 30 |
{ |
| 31 |
errPrefix: "error http listener", |
| 32 |
run: server.Listen, |
| 33 |
}, |
| 34 |
{ |
| 35 |
errPrefix: "openvpn", |
| 36 |
run: openvpnClient.Connect, |
| 37 |
}, |
| 38 |
} |
| 39 |
|
| 40 |
if conf.Debug.Pprof { |
| 41 |
debugServer := newDebugServer(logger, conf) |
| 42 |
services = append(services, service{ |
| 43 |
errPrefix: "error debug http listener", |
| 44 |
run: debugServer.Listen, |
| 45 |
}) |
| 46 |
} |
| 47 |
|
| 48 |
return newRuntime(logger, server, services), nil |
| 49 |
} |
| 50 |
|
| 51 |
func newDebugServer(logger *slog.Logger, conf *config.Config) *httpserver.Server { |
| 52 |
mux := http.NewServeMux() |
| 53 |
mux.Handle("GET /", http.RedirectHandler("/debug/pprof/", http.StatusTemporaryRedirect)) |
| 54 |
mux.HandleFunc("GET /debug/pprof/", pprof.Index) |
| 55 |
mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline) |
| 56 |
mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile) |
| 57 |
mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol) |
| 58 |
mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace) |
| 59 |
|
| 60 |
return httpserver.NewHTTPServer(httpserver.ServerNameDebug, logger, config.HTTP{Listen: conf.Debug.Listen}, mux) |
| 61 |
} |
| 62 |
|
| 63 |
func setupOpenVPNClient( |
| 64 |
ctx context.Context, logger *slog.Logger, conf *config.Config, tokenStorage tokenstorage.Storage, |
| 65 |
) (*openvpn.Client, *http.ServeMux, error) { |
| 66 |
httpClient := utils.NewOAuth2HTTPClient(nil) |
| 67 |
|
| 68 |
provider, err := providers.New(ctx, conf, httpClient) |
| 69 |
if err != nil { |
| 70 |
return nil, nil, fmt.Errorf("error creating oauth2 provider: %w", err) |
| 71 |
} |
| 72 |
|
| 73 |
openvpnClient := openvpn.New(logger, conf) |
| 74 |
|
| 75 |
stateCrypto, err := crypto.NewWithMaxAge(conf.HTTP.Secret.String(), conf.OpenVPN.AuthPendingTimeout) |
| 76 |
if err != nil { |
| 77 |
return nil, nil, fmt.Errorf("create state cipher: %w", err) |
| 78 |
} |
| 79 |
|
| 80 |
oAuth2Client, err := oauth2.New(ctx, logger, conf, httpClient, tokenStorage, stateCrypto, provider, openvpnClient) |
| 81 |
if err != nil { |
| 82 |
return nil, nil, fmt.Errorf("error creating oauth2 client: %w", err) |
| 83 |
} |
| 84 |
|
| 85 |
openvpnClient.SetOAuth2Client(oAuth2Client) |
| 86 |
|
| 87 |
return openvpnClient, httphandler.New(conf, oAuth2Client), nil |
| 88 |
} |