using single binary

This commit is contained in:
dd dd 2024-08-27 23:50:05 +02:00
parent 41d7a454fb
commit fa6be8ef74
10 changed files with 58 additions and 67 deletions

View File

@ -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.

View File

@ -1,3 +0,0 @@
NOSTR_RELAYS = 'wss://relay.8333.space'
#NOSTR_RELAYS = 'ws://localhost:6666'
NOSTR_PRIVATE_KEY = ""

View File

@ -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"]

View File

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

View File

@ -1,5 +0,0 @@
#NOSTR_RELAYS = 'wss://relay.8333.space'
NOSTR_RELAYS = 'ws://localhost:6666'
NOSTR_PRIVATE_KEY = ""
BACKEND_HOST = 'localhost:3338'
PUBLIC = false

5
cmd/nws/.env Normal file
View File

@ -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'

View File

@ -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"]

View File

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

View File

@ -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) {

View File

@ -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