From e5d2a0ec57f73191a6de256aef08cf4ad6114646 Mon Sep 17 00:00:00 2001 From: dd dd Date: Tue, 23 Jul 2024 21:49:15 +0200 Subject: [PATCH] moved stuff from cmd to exit --- cmd/exit/exit.go | 26 ++++++++------------------ config/config.go | 2 ++ exit/exit.go | 27 +++++++++++++++++++++------ 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/cmd/exit/exit.go b/cmd/exit/exit.go index fb71efa..7f2fcea 100644 --- a/cmd/exit/exit.go +++ b/cmd/exit/exit.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "github.com/asmogo/nws/config" "github.com/asmogo/nws/exit" "github.com/nbd-wtf/go-nostr" @@ -13,14 +12,15 @@ var httpsPort int32 var httpTarget string const ( - generateKeyMessage = "Generated new private key. Please update your configuration file with the new key, otherwise your key will be lost, once this application restarts." - startingReverseProxyMessage = "starting exit node with https reverse proxy" + generateKeyMessage = "Generated new private key. Please set your environment using the new key, otherwise your key will be lost." + usagePort = "set the https reverse proxy port" + usageTarget = "set https reverse proxy target (your local service)" ) func main() { rootCmd := &cobra.Command{Use: "exit", Run: startExitNode} - rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, "port for the https reverse proxy") - rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", "target for the https reverse proxy (your local service)") + rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, usagePort) + rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", usageTarget) err := rootCmd.Execute() if err != nil { panic(err) @@ -34,9 +34,9 @@ func startExitNode(cmd *cobra.Command, args []string) { if err != nil { panic(err) } - if httpsPort != 0 { - cfg.BackendHost = fmt.Sprintf(":%d", httpsPort) - } + cfg.HttpsPort = httpsPort + cfg.HttpsTarget = httpTarget + if cfg.NostrPrivateKey == "" { // generate new private key cfg.NostrPrivateKey = nostr.GeneratePrivateKey() @@ -46,15 +46,5 @@ func startExitNode(cmd *cobra.Command, args []string) { // and start it ctx := cmd.Context() exitNode := exit.NewExit(ctx, cfg) - if httpsPort != 0 { - slog.Info(startingReverseProxyMessage, "port", httpsPort) - go func() { - err = exitNode.StartReverseProxy(httpTarget, httpsPort) - if err != nil { - panic(err) - } - }() - - } exitNode.ListenAndServe(ctx) } diff --git a/config/config.go b/config/config.go index fba69f5..161c76e 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,8 @@ type ExitConfig struct { NostrPrivateKey string `env:"NOSTR_PRIVATE_KEY"` BackendHost string `env:"BACKEND_HOST"` BackendScheme string `env:"BACKEND_SCHEME"` + HttpsPort int32 + HttpsTarget string } // load the and marshal Configuration from .env file from the UserHomeDir diff --git a/exit/exit.go b/exit/exit.go index 54ab42e..bca8a22 100644 --- a/exit/exit.go +++ b/exit/exit.go @@ -19,6 +19,10 @@ import ( _ "net/http/pprof" ) +const ( + startingReverseProxyMessage = "starting exit node with https reverse proxy" +) + // Exit represents a structure that holds information related to an exit node. type Exit struct { @@ -47,21 +51,23 @@ type Exit struct { } // NewExit creates a new Exit node with the provided context and config. -func NewExit(ctx context.Context, config *config.ExitConfig) *Exit { +func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit { // todo -- this is for debugging purposes only and should be removed go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }() pool := nostr.NewSimplePool(ctx) - + if exitNodeConfig.HttpsPort != 0 { + exitNodeConfig.BackendHost = fmt.Sprintf(":%d", exitNodeConfig.HttpsPort) + } exit := &Exit{ nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](), - config: config, + config: exitNodeConfig, pool: pool, mutexMap: NewMutexMap(), } - for _, relayUrl := range config.NostrRelays { + for _, relayUrl := range exitNodeConfig.NostrRelays { relay, err := exit.pool.EnsureRelay(relayUrl) if err != nil { fmt.Println(err) @@ -70,12 +76,12 @@ func NewExit(ctx context.Context, config *config.ExitConfig) *Exit { exit.relays = append(exit.relays, relay) fmt.Printf("added relay connection to %s\n", relayUrl) } - pubKey, err := nostr.GetPublicKey(config.NostrPrivateKey) + pubKey, err := nostr.GetPublicKey(exitNodeConfig.NostrPrivateKey) if err != nil { panic(err) } profile, err := nip19.EncodeProfile(pubKey, - config.NostrRelays) + exitNodeConfig.NostrRelays) if err != nil { panic(err) } @@ -83,6 +89,15 @@ func NewExit(ctx context.Context, config *config.ExitConfig) *Exit { exit.publicKey = pubKey slog.Info("created exit node", "profile", profile) err = exit.setSubscriptions(ctx) + if exit.config.HttpsPort != 0 { + slog.Info(startingReverseProxyMessage, "port", exit.config.HttpsPort) + go func() { + err = exit.StartReverseProxy(exitNodeConfig.HttpsTarget, exit.config.HttpsPort) + if err != nil { + panic(err) + } + }() + } if err != nil { panic(err) }