update exit construction (#45)

This commit is contained in:
asmogo 2024-11-11 08:28:43 +01:00 committed by GitHub
parent a77add0859
commit 844bf987a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 14 deletions

View File

@ -62,7 +62,7 @@ func startExitNode(cmd *cobra.Command, _ []string) {
panic(err) panic(err)
} }
ctx := cmd.Context() ctx := cmd.Context()
exitNode := exit.NewExit(ctx, cfg) exitNode := exit.New(ctx, cfg)
exitNode.ListenAndServe(ctx) exitNode.ListenAndServe(ctx)
} }

View File

@ -53,12 +53,23 @@ type Exit struct {
publicKey string publicKey string
} }
// NewExit creates a new Exit node with the provided context and config. func newExit(pool *nostr.SimplePool, pubKey string, profile string) *Exit {
exit := &Exit{
nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](),
pool: pool,
mutexMap: NewMutexMap(),
publicKey: pubKey,
nprofile: profile,
}
return exit
}
// New creates a newExit Exit node with the provided context and config.
// This function will currently panic if there is an error while creating the Exit node. // This function will currently panic if there is an error while creating the Exit node.
func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit { func New(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
// generate new private key if it is not set // generate newExit private key if it is not set
if exitNodeConfig.NostrPrivateKey == "" { if exitNodeConfig.NostrPrivateKey == "" {
// generate new private key // generate newExit private key
exitNodeConfig.NostrPrivateKey = nostr.GeneratePrivateKey() exitNodeConfig.NostrPrivateKey = nostr.GeneratePrivateKey()
slog.Warn(generateKeyMessage, "key", exitNodeConfig.NostrPrivateKey) slog.Warn(generateKeyMessage, "key", exitNodeConfig.NostrPrivateKey)
} }
@ -74,16 +85,10 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
if err != nil { if err != nil {
panic(err) panic(err)
} }
// create a new pool // create a newExit pool
pool := nostr.NewSimplePool(ctx) pool := nostr.NewSimplePool(ctx)
exit := &Exit{ exit := newExit(pool, pubKey, profile)
nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](),
pool: pool,
mutexMap: NewMutexMap(),
publicKey: pubKey,
nprofile: profile,
}
// start reverse proxy if https port is set // start reverse proxy if https port is set
if exitNodeConfig.HttpsPort != 0 { if exitNodeConfig.HttpsPort != 0 {
exitNodeConfig.BackendHost = fmt.Sprintf(":%d", exitNodeConfig.HttpsPort) exitNodeConfig.BackendHost = fmt.Sprintf(":%d", exitNodeConfig.HttpsPort)
@ -105,7 +110,7 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
continue continue
} }
exit.relays = append(exit.relays, relay) exit.relays = append(exit.relays, relay)
slog.Info("added relay connection", "url", relayURL) //nolint:forbidigo slog.Info("added relay connection", "url", relayURL)
} }
domain, err := exit.getDomain() domain, err := exit.getDomain()
if err != nil { if err != nil {