From fa6be8ef74cb1d5a1a3181d614247bcb70f39727 Mon Sep 17 00:00:00 2001 From: dd dd Date: Tue, 27 Aug 2024 23:50:05 +0200 Subject: [PATCH] using single binary --- README.md | 6 ++--- cmd/entry/.env | 3 --- cmd/entry/Dockerfile | 17 ------------ cmd/entry/proxy.go | 25 ------------------ cmd/exit/.env | 5 ---- cmd/nws/.env | 5 ++++ cmd/{exit => nws}/Dockerfile | 6 ++--- cmd/{exit/exit.go => nws/nws.go} | 44 +++++++++++++++++++++++++++----- config/config.go | 4 +++ docker-compose.yaml | 10 +++++--- 10 files changed, 58 insertions(+), 67 deletions(-) delete mode 100644 cmd/entry/.env delete mode 100644 cmd/entry/Dockerfile delete mode 100644 cmd/entry/proxy.go delete mode 100644 cmd/exit/.env create mode 100644 cmd/nws/.env rename cmd/{exit => nws}/Dockerfile (70%) rename cmd/{exit/exit.go => nws/nws.go} (51%) diff --git a/README.md b/README.md index 0d7b1ff..2bd5d7c 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ To make your services reachable via Nostr, set up the exit node. Configuration can be completed using environment variables. Alternatively, you can create a `.env` file in the current working directory with the following content: ``` -NOSTR_RELAYS='ws://localhost:6666;wss://relay.domain.com' +NOSTR_RELAYS='ws://localhost:6666;ws://localhost:7777;wss://relay.domain.com' NOSTR_PRIVATE_KEY="EXITPRIVATEHEX" BACKEND_HOST='localhost:3338' PUBLIC=false @@ -96,7 +96,7 @@ PUBLIC=false To start the exit node, use this command: ```bash -go run cmd/exit/exit.go +go run cmd/nws/nws.go exit ``` If your backend services support TLS, your service can now start using TLS encryption through a publicly available entry node. @@ -108,7 +108,7 @@ If your backend services support TLS, your service can now start using TLS encry To run an entry node for accessing NWS services behind exit nodes, use the following command: ```bash -go run cmd/entry/main.go +go run cmd/nws/nws.go entry ``` If you don't want to use the `PUBLIC_ADDRESS` feature, no further configuration is needed. diff --git a/cmd/entry/.env b/cmd/entry/.env deleted file mode 100644 index 6b14307..0000000 --- a/cmd/entry/.env +++ /dev/null @@ -1,3 +0,0 @@ -NOSTR_RELAYS = 'wss://relay.8333.space' -#NOSTR_RELAYS = 'ws://localhost:6666' -NOSTR_PRIVATE_KEY = "" \ No newline at end of file diff --git a/cmd/entry/Dockerfile b/cmd/entry/Dockerfile deleted file mode 100644 index 260f35b..0000000 --- a/cmd/entry/Dockerfile +++ /dev/null @@ -1,17 +0,0 @@ -FROM golang:1.21-alpine as builder - -ADD . /build/ - -WORKDIR /build -RUN apk add --no-cache git bash openssh-client && \ - go build -o entry cmd/entry/*.go - - -#building finished. Now extracting single bin in second stage. -FROM alpine - -COPY --from=builder /build/entry /app/ - -WORKDIR /app - -CMD ["./entry"] diff --git a/cmd/entry/proxy.go b/cmd/entry/proxy.go deleted file mode 100644 index 29bd6ec..0000000 --- a/cmd/entry/proxy.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "github.com/asmogo/nws/config" - "github.com/asmogo/nws/proxy" - "golang.org/x/net/context" -) - -func main() { - // load the configuration - // from the environment - cfg, err := config.LoadConfig[config.EntryConfig]() - if err != nil { - panic(err) - } - - // create a new gw server - // and start it - socksProxy := proxy.New(context.Background(), cfg) - - err = socksProxy.Start() - if err != nil { - panic(err) - } -} diff --git a/cmd/exit/.env b/cmd/exit/.env deleted file mode 100644 index 86dfd2a..0000000 --- a/cmd/exit/.env +++ /dev/null @@ -1,5 +0,0 @@ -#NOSTR_RELAYS = 'wss://relay.8333.space' -NOSTR_RELAYS = 'ws://localhost:6666' -NOSTR_PRIVATE_KEY = "" -BACKEND_HOST = 'localhost:3338' -PUBLIC = false diff --git a/cmd/nws/.env b/cmd/nws/.env new file mode 100644 index 0000000..2898f03 --- /dev/null +++ b/cmd/nws/.env @@ -0,0 +1,5 @@ +NOSTR_RELAYS = 'wss://localhost:7777'#NOSTR_RELAYS = 'ws://localhost:6666' +NOSTR_PRIVATE_KEY = "" +BACKEND_HOST = 'localhost:3338' +PUBLIC = true +PUBLIC_ADDRESS = 'localhost:4443' \ No newline at end of file diff --git a/cmd/exit/Dockerfile b/cmd/nws/Dockerfile similarity index 70% rename from cmd/exit/Dockerfile rename to cmd/nws/Dockerfile index 7fbb467..e7613b5 100644 --- a/cmd/exit/Dockerfile +++ b/cmd/nws/Dockerfile @@ -4,14 +4,14 @@ ADD . /build/ WORKDIR /build RUN apk add --no-cache git bash openssh-client && \ - go build -o exit cmd/exit/*.go + go build -o nws cmd/nws/*.go #building finished. Now extracting single bin in second stage. FROM alpine -COPY --from=builder /build/exit /app/ +COPY --from=builder /build/nws /app/ WORKDIR /app -CMD ["./exit"] \ No newline at end of file +CMD ["./nws"] \ No newline at end of file diff --git a/cmd/exit/exit.go b/cmd/nws/nws.go similarity index 51% rename from cmd/exit/exit.go rename to cmd/nws/nws.go index f4f9582..c90af73 100644 --- a/cmd/exit/exit.go +++ b/cmd/nws/nws.go @@ -1,8 +1,11 @@ package main import ( + "log/slog" + "github.com/asmogo/nws/config" "github.com/asmogo/nws/exit" + "github.com/asmogo/nws/proxy" "github.com/spf13/cobra" ) @@ -12,12 +15,15 @@ const ( ) func main() { - + rootCmd := &cobra.Command{Use: "nws"} + exitCmd := &cobra.Command{Use: "exit", Run: startExitNode} var httpsPort int32 var httpTarget string - rootCmd := &cobra.Command{Use: "exit", Run: startExitNode} - rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, usagePort) - rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", usageTarget) + exitCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, usagePort) + exitCmd.Flags().StringVarP(&httpTarget, "target", "t", "", usageTarget) + entryCmd := &cobra.Command{Use: "entry", Run: startEntryNode} + rootCmd.AddCommand(exitCmd) + rootCmd.AddCommand(entryCmd) err := rootCmd.Execute() if err != nil { panic(err) @@ -26,7 +32,6 @@ func main() { // updateConfigFlag updates the configuration with the provided flags. func updateConfigFlag(cmd *cobra.Command, cfg *config.ExitConfig) error { - httpsPort, err := cmd.Flags().GetInt32("port") if err != nil { return err @@ -41,14 +46,39 @@ func updateConfigFlag(cmd *cobra.Command, cfg *config.ExitConfig) error { } func startExitNode(cmd *cobra.Command, args []string) { + slog.Info("Starting exit node") // load the configuration - // from the environment cfg, err := config.LoadConfig[config.ExitConfig]() if err != nil { panic(err) } - updateConfigFlag(cmd, cfg) + if len(cfg.NostrRelays) == 0 { + slog.Info("No relays provided, using default relays") + cfg.NostrRelays = config.DefaultRelays + } + err = updateConfigFlag(cmd, cfg) + if err != nil { + panic(err) + } ctx := cmd.Context() exitNode := exit.NewExit(ctx, cfg) exitNode.ListenAndServe(ctx) } + +func startEntryNode(cmd *cobra.Command, args []string) { + slog.Info("Starting entry node") + cfg, err := config.LoadConfig[config.EntryConfig]() + if err != nil { + panic(err) + } + + // create a new gw server + // and start it + socksProxy := proxy.New(cmd.Context(), cfg) + + err = socksProxy.Start() + if err != nil { + panic(err) + } + +} diff --git a/config/config.go b/config/config.go index 7c247dd..9119881 100644 --- a/config/config.go +++ b/config/config.go @@ -24,6 +24,10 @@ type ExitConfig struct { Public bool `env:"PUBLIC"` } +var DefaultRelays = []string{ + "wss://relay.8333.space", +} + // load the and marshal Configuration from .env file from the UserHomeDir // if this file was not found, fallback to the os environment variables func LoadConfig[T any]() (*T, error) { diff --git a/docker-compose.yaml b/docker-compose.yaml index 7547a8b..917c614 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -26,8 +26,9 @@ services: exit: build: context: . - dockerfile: cmd/exit/Dockerfile + dockerfile: cmd/nws/Dockerfile container_name: exit + command: [ "./nws","exit" ] networks: nostr: environment: @@ -40,9 +41,9 @@ services: exit-https: build: context: . - dockerfile: cmd/exit/Dockerfile + dockerfile: cmd/nws/Dockerfile container_name: exit-https - command: ["./exit", "--port", "4443", "--target", "http://mint:3338"] + command: ["./nws","exit","--port", "4443", "--target", "http://mint:3338"] networks: nostr: environment: @@ -55,7 +56,8 @@ services: entry: build: context: . - dockerfile: cmd/entry/Dockerfile + dockerfile: cmd/nws/Dockerfile + command: [ "./nws","entry"] container_name: entry ports: - 8882:8882