moved stuff from cmd to exit

This commit is contained in:
dd dd 2024-07-23 21:49:15 +02:00
parent 6f4db0092d
commit e5d2a0ec57
3 changed files with 31 additions and 24 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"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/nbd-wtf/go-nostr"
@ -13,14 +12,15 @@ var httpsPort int32
var httpTarget string var httpTarget string
const ( 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." generateKeyMessage = "Generated new private key. Please set your environment using the new key, otherwise your key will be lost."
startingReverseProxyMessage = "starting exit node with https reverse proxy" usagePort = "set the https reverse proxy port"
usageTarget = "set https reverse proxy target (your local service)"
) )
func main() { func main() {
rootCmd := &cobra.Command{Use: "exit", Run: startExitNode} rootCmd := &cobra.Command{Use: "exit", Run: startExitNode}
rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, "port for the https reverse proxy") rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, usagePort)
rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", "target for the https reverse proxy (your local service)") rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", usageTarget)
err := rootCmd.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
panic(err) panic(err)
@ -34,9 +34,9 @@ func startExitNode(cmd *cobra.Command, args []string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if httpsPort != 0 { cfg.HttpsPort = httpsPort
cfg.BackendHost = fmt.Sprintf(":%d", httpsPort) cfg.HttpsTarget = httpTarget
}
if cfg.NostrPrivateKey == "" { if cfg.NostrPrivateKey == "" {
// generate new private key // generate new private key
cfg.NostrPrivateKey = nostr.GeneratePrivateKey() cfg.NostrPrivateKey = nostr.GeneratePrivateKey()
@ -46,15 +46,5 @@ func startExitNode(cmd *cobra.Command, args []string) {
// and start it // and start it
ctx := cmd.Context() ctx := cmd.Context()
exitNode := exit.NewExit(ctx, cfg) 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) exitNode.ListenAndServe(ctx)
} }

View File

@ -18,6 +18,8 @@ type ExitConfig struct {
NostrPrivateKey string `env:"NOSTR_PRIVATE_KEY"` NostrPrivateKey string `env:"NOSTR_PRIVATE_KEY"`
BackendHost string `env:"BACKEND_HOST"` BackendHost string `env:"BACKEND_HOST"`
BackendScheme string `env:"BACKEND_SCHEME"` BackendScheme string `env:"BACKEND_SCHEME"`
HttpsPort int32
HttpsTarget string
} }
// load the and marshal Configuration from .env file from the UserHomeDir // load the and marshal Configuration from .env file from the UserHomeDir

View File

@ -19,6 +19,10 @@ import (
_ "net/http/pprof" _ "net/http/pprof"
) )
const (
startingReverseProxyMessage = "starting exit node with https reverse proxy"
)
// Exit represents a structure that holds information related to an exit node. // Exit represents a structure that holds information related to an exit node.
type Exit struct { type Exit struct {
@ -47,21 +51,23 @@ 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, config *config.ExitConfig) *Exit { func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
// todo -- this is for debugging purposes only and should be removed // todo -- this is for debugging purposes only and should be removed
go func() { go func() {
log.Println(http.ListenAndServe("localhost:6060", nil)) log.Println(http.ListenAndServe("localhost:6060", nil))
}() }()
pool := nostr.NewSimplePool(ctx) pool := nostr.NewSimplePool(ctx)
if exitNodeConfig.HttpsPort != 0 {
exitNodeConfig.BackendHost = fmt.Sprintf(":%d", exitNodeConfig.HttpsPort)
}
exit := &Exit{ exit := &Exit{
nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](), nostrConnectionMap: xsync.NewMapOf[string, *netstr.NostrConnection](),
config: config, config: exitNodeConfig,
pool: pool, pool: pool,
mutexMap: NewMutexMap(), mutexMap: NewMutexMap(),
} }
for _, relayUrl := range config.NostrRelays { for _, relayUrl := range exitNodeConfig.NostrRelays {
relay, err := exit.pool.EnsureRelay(relayUrl) relay, err := exit.pool.EnsureRelay(relayUrl)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -70,12 +76,12 @@ func NewExit(ctx context.Context, config *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(config.NostrPrivateKey) pubKey, err := nostr.GetPublicKey(exitNodeConfig.NostrPrivateKey)
if err != nil { if err != nil {
panic(err) panic(err)
} }
profile, err := nip19.EncodeProfile(pubKey, profile, err := nip19.EncodeProfile(pubKey,
config.NostrRelays) exitNodeConfig.NostrRelays)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -83,6 +89,15 @@ func NewExit(ctx context.Context, config *config.ExitConfig) *Exit {
exit.publicKey = pubKey exit.publicKey = pubKey
slog.Info("created exit node", "profile", profile) slog.Info("created exit node", "profile", profile)
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)
} }