From bd0700b8522f027f02a75a926cb8c9c05e4893eb Mon Sep 17 00:00:00 2001 From: dd dd Date: Tue, 23 Jul 2024 22:18:41 +0200 Subject: [PATCH] Refactor exit node setup and update command documentation --- README.md | 2 +- cmd/exit/exit.go | 26 +++++++----------- exit/exit.go | 69 ++++++++++++++++++++++++++---------------------- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 7be5945..61bbcf5 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ This will start an example environment, including the entry node, exit node, and You can run the following commands to receive your nprofiles: ```bash -docker logs exit-https 2>&1 | awk -F'profile=' '{if ($2) print $2}' | awk '{print $1} +docker logs exit-https 2>&1 | awk -F'profile=' '{if ($2) print $2}' | awk '{print $1}' ``` ```bash docker logs exit 2>&1 | awk -F'profile=' '{if ($2) print $2}' | awk '{print $1} diff --git a/cmd/exit/exit.go b/cmd/exit/exit.go index 7f2fcea..b8f6c37 100644 --- a/cmd/exit/exit.go +++ b/cmd/exit/exit.go @@ -3,18 +3,15 @@ package main import ( "github.com/asmogo/nws/config" "github.com/asmogo/nws/exit" - "github.com/nbd-wtf/go-nostr" "github.com/spf13/cobra" - "log/slog" ) var httpsPort int32 var httpTarget string const ( - 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)" + usagePort = "set the https reverse proxy port" + usageTarget = "set https reverse proxy target (your local service)" ) func main() { @@ -26,24 +23,21 @@ func main() { panic(err) } } -func startExitNode(cmd *cobra.Command, args []string) { +// updateConfigFlag updates the configuration with the provided flags. +func updateConfigFlag(cfg *config.ExitConfig) { + cfg.HttpsPort = httpsPort + cfg.HttpsTarget = httpTarget +} + +func startExitNode(cmd *cobra.Command, args []string) { // load the configuration // from the environment cfg, err := config.LoadConfig[config.ExitConfig]() if err != nil { panic(err) } - cfg.HttpsPort = httpsPort - cfg.HttpsTarget = httpTarget - - if cfg.NostrPrivateKey == "" { - // generate new private key - cfg.NostrPrivateKey = nostr.GeneratePrivateKey() - slog.Warn(generateKeyMessage, "key", cfg.NostrPrivateKey) - } - // create a new gw server - // and start it + updateConfigFlag(cfg) ctx := cmd.Context() exitNode := exit.NewExit(ctx, cfg) exitNode.ListenAndServe(ctx) diff --git a/exit/exit.go b/exit/exit.go index bca8a22..b0c607e 100644 --- a/exit/exit.go +++ b/exit/exit.go @@ -12,15 +12,14 @@ import ( "github.com/nbd-wtf/go-nostr/nip19" "github.com/puzpuzpuz/xsync/v3" "golang.org/x/net/context" - "log" "log/slog" "net" - "net/http" _ "net/http/pprof" ) const ( 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." ) // Exit represents a structure that holds information related to an exit node. @@ -52,21 +51,47 @@ type Exit struct { // NewExit creates a new Exit node with the provided context and config. 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) + // generate new private key if it is not set + if exitNodeConfig.NostrPrivateKey == "" { + // generate new private key + exitNodeConfig.NostrPrivateKey = nostr.GeneratePrivateKey() + slog.Warn(generateKeyMessage, "key", exitNodeConfig.NostrPrivateKey) } + // get public key from private key + pubKey, err := nostr.GetPublicKey(exitNodeConfig.NostrPrivateKey) + if err != nil { + panic(err) + } + // encode profile + profile, err := nip19.EncodeProfile(pubKey, + exitNodeConfig.NostrRelays) + if err != nil { + panic(err) + } + // create a new pool + pool := nostr.NewSimplePool(ctx) + exit := &Exit{ nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](), - config: exitNodeConfig, pool: pool, mutexMap: NewMutexMap(), + publicKey: pubKey, + nprofile: profile, } - + // start reverse proxy if https port is set + if exitNodeConfig.HttpsPort != 0 { + exitNodeConfig.BackendHost = fmt.Sprintf(":%d", exitNodeConfig.HttpsPort) + go func(cfg *config.ExitConfig) { + slog.Info(startingReverseProxyMessage, "port", cfg.HttpsPort) + err := exit.StartReverseProxy(cfg.HttpsTarget, cfg.HttpsPort) + if err != nil { + panic(err) + } + }(exitNodeConfig) + } + // set config + exit.config = exitNodeConfig + // add relays to the pool for _, relayUrl := range exitNodeConfig.NostrRelays { relay, err := exit.pool.EnsureRelay(relayUrl) if err != nil { @@ -76,28 +101,10 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit { exit.relays = append(exit.relays, relay) fmt.Printf("added relay connection to %s\n", relayUrl) } - pubKey, err := nostr.GetPublicKey(exitNodeConfig.NostrPrivateKey) - if err != nil { - panic(err) - } - profile, err := nip19.EncodeProfile(pubKey, - exitNodeConfig.NostrRelays) - if err != nil { - panic(err) - } - exit.nprofile = profile - exit.publicKey = pubKey + slog.Info("created exit node", "profile", profile) + // setup subscriptions 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) }