Refactor exit node setup and update command documentation

This commit is contained in:
dd dd 2024-07-23 22:18:41 +02:00
parent e5d2a0ec57
commit bd0700b852
3 changed files with 49 additions and 48 deletions

View File

@ -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: You can run the following commands to receive your nprofiles:
```bash ```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 ```bash
docker logs exit 2>&1 | awk -F'profile=' '{if ($2) print $2}' | awk '{print $1} docker logs exit 2>&1 | awk -F'profile=' '{if ($2) print $2}' | awk '{print $1}

View File

@ -3,18 +3,15 @@ package main
import ( import (
"github.com/asmogo/nws/config" "github.com/asmogo/nws/config"
"github.com/asmogo/nws/exit" "github.com/asmogo/nws/exit"
"github.com/nbd-wtf/go-nostr"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"log/slog"
) )
var httpsPort int32 var httpsPort int32
var httpTarget string var httpTarget string
const ( 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"
usagePort = "set the https reverse proxy port" usageTarget = "set https reverse proxy target (your local service)"
usageTarget = "set https reverse proxy target (your local service)"
) )
func main() { func main() {
@ -26,24 +23,21 @@ func main() {
panic(err) 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 // load the configuration
// from the environment // from the environment
cfg, err := config.LoadConfig[config.ExitConfig]() cfg, err := config.LoadConfig[config.ExitConfig]()
if err != nil { if err != nil {
panic(err) panic(err)
} }
cfg.HttpsPort = httpsPort updateConfigFlag(cfg)
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
ctx := cmd.Context() ctx := cmd.Context()
exitNode := exit.NewExit(ctx, cfg) exitNode := exit.NewExit(ctx, cfg)
exitNode.ListenAndServe(ctx) exitNode.ListenAndServe(ctx)

View File

@ -12,15 +12,14 @@ import (
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
"github.com/puzpuzpuz/xsync/v3" "github.com/puzpuzpuz/xsync/v3"
"golang.org/x/net/context" "golang.org/x/net/context"
"log"
"log/slog" "log/slog"
"net" "net"
"net/http"
_ "net/http/pprof" _ "net/http/pprof"
) )
const ( const (
startingReverseProxyMessage = "starting exit node with https reverse proxy" 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. // 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. // NewExit creates a new Exit node with the provided context and config.
func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit { func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
// todo -- this is for debugging purposes only and should be removed // generate new private key if it is not set
go func() { if exitNodeConfig.NostrPrivateKey == "" {
log.Println(http.ListenAndServe("localhost:6060", nil)) // generate new private key
}() exitNodeConfig.NostrPrivateKey = nostr.GeneratePrivateKey()
pool := nostr.NewSimplePool(ctx) slog.Warn(generateKeyMessage, "key", exitNodeConfig.NostrPrivateKey)
if exitNodeConfig.HttpsPort != 0 {
exitNodeConfig.BackendHost = fmt.Sprintf(":%d", exitNodeConfig.HttpsPort)
} }
// 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{ exit := &Exit{
nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](), nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](),
config: exitNodeConfig,
pool: pool, pool: pool,
mutexMap: NewMutexMap(), 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 { for _, relayUrl := range exitNodeConfig.NostrRelays {
relay, err := exit.pool.EnsureRelay(relayUrl) relay, err := exit.pool.EnsureRelay(relayUrl)
if err != nil { if err != nil {
@ -76,28 +101,10 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
exit.relays = append(exit.relays, relay) exit.relays = append(exit.relays, relay)
fmt.Printf("added relay connection to %s\n", relayUrl) 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) slog.Info("created exit node", "profile", profile)
// setup subscriptions
err = exit.setSubscriptions(ctx) 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 { if err != nil {
panic(err) panic(err)
} }