Skip to content

Commit

Permalink
Merge pull request #35 from asmogo/single_binary
Browse files Browse the repository at this point in the history
using single binary
  • Loading branch information
asmogo authored Aug 27, 2024
2 parents 41d7a45 + fa6be8e commit 2f9dbca
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 67 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions cmd/entry/.env

This file was deleted.

17 changes: 0 additions & 17 deletions cmd/entry/Dockerfile

This file was deleted.

25 changes: 0 additions & 25 deletions cmd/entry/proxy.go

This file was deleted.

5 changes: 0 additions & 5 deletions cmd/exit/.env

This file was deleted.

5 changes: 5 additions & 0 deletions cmd/nws/.env
Original file line number Diff line number Diff line change
@@ -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'
6 changes: 3 additions & 3 deletions cmd/exit/Dockerfile → cmd/nws/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
CMD ["./nws"]
44 changes: 37 additions & 7 deletions cmd/exit/exit.go → cmd/nws/nws.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
}

}
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 2f9dbca

Please sign in to comment.