mirror of
https://github.com/asmogo/nws.git
synced 2025-01-18 18:11:33 +00:00
moved stuff from cmd to exit
This commit is contained in:
parent
6f4db0092d
commit
e5d2a0ec57
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
27
exit/exit.go
27
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user