From 2897b92f72f5da860e00bcebd1d0ab6a45cb8340 Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Tue, 7 Jan 2025 20:51:33 -0500 Subject: [PATCH 1/5] Allow use of env vars --- docker-compose.yml | 10 ++++++++++ entrypoint.sh | 11 ----------- main.go | 35 ++++++++++++++++++++++------------- 3 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..13b9b5b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +services: + newt: + image: newttest + container_name: newt + restart: unless-stopped + environment: + - PANGOLIN_ENDPOINT=https://proxy.schwartznetwork.net + - NEWT_ID=2ix2t8xk22ubpfy + - NEWT_SECRET=nnisrfsdfc7prqsp9ewo1dvtvci50j5uiqotez00dgap0ii2 + - LOG_LEVEL=DEBUG \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 497d640..79ae7a0 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,7 +1,5 @@ #!/bin/sh -# Sample from https://github.com/traefik/traefik-library-image/blob/5070edb25b03cca6802d75d5037576c840f73fdd/v3.1/alpine/entrypoint.sh - set -e # first arg is `-f` or `--some-option` @@ -9,13 +7,4 @@ if [ "${1#-}" != "$1" ]; then set -- newt "$@" fi -# if our command is a valid newt subcommand, let's invoke it through newt instead -# (this allows for "docker run newt version", etc) -if newt "$1" --help >/dev/null 2>&1 -then - set -- newt "$@" -else - echo "= '$1' is not a newt command: assuming shell execution." 1>&2 -fi - exec "$@" \ No newline at end of file diff --git a/main.go b/main.go index 5e05bd4..dd04ea9 100644 --- a/main.go +++ b/main.go @@ -222,13 +222,6 @@ func resolveDomain(domain string) (string, error) { return ipAddr, nil } -func getEnvWithDefault(key, defaultValue string) string { - if value := os.Getenv(key); value != "" { - return value - } - return defaultValue -} - func main() { var ( endpoint string @@ -240,12 +233,28 @@ func main() { logLevel string ) - // Define CLI flags with default values from environment variables - flag.StringVar(&endpoint, "endpoint", os.Getenv("PANGOLIN_ENDPOINT"), "Endpoint of your pangolin server") - flag.StringVar(&id, "id", os.Getenv("NEWT_ID"), "Newt ID") - flag.StringVar(&secret, "secret", os.Getenv("NEWT_SECRET"), "Newt secret") - flag.StringVar(&dns, "dns", getEnvWithDefault("DEFAULT_DNS", "8.8.8.8"), "DNS server to use") - flag.StringVar(&logLevel, "log-level", getEnvWithDefault("LOG_LEVEL", "INFO"), "Log level (DEBUG, INFO, WARN, ERROR, FATAL)") + // if PANGOLIN_ENDPOINT, NEWT_ID, and NEWT_SECRET are set as environment variables, they will be used as default values + endpoint = os.Getenv("PANGOLIN_ENDPOINT") + id = os.Getenv("NEWT_ID") + secret = os.Getenv("NEWT_SECRET") + dns = os.Getenv("DNS") + logLevel = os.Getenv("LOG_LEVEL") + + if endpoint == "" { + flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server") + } + if id == "" { + flag.StringVar(&id, "id", "", "Newt ID") + } + if secret == "" { + flag.StringVar(&secret, "secret", "", "Newt secret") + } + if dns == "" { + flag.StringVar(&dns, "dns", "8.8.8.8", "DNS server to use") + } + if logLevel == "" { + flag.StringVar(&logLevel, "log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)") + } flag.Parse() logger.Init() From 739f708ff7d66e1bcfd5edddbcc9b8409b607aac Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Tue, 7 Jan 2025 20:52:38 -0500 Subject: [PATCH 2/5] Update sample docker-compose --- docker-compose.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 13b9b5b..d63747d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,6 @@ services: container_name: newt restart: unless-stopped environment: - - PANGOLIN_ENDPOINT=https://proxy.schwartznetwork.net + - PANGOLIN_ENDPOINT=https://example.com - NEWT_ID=2ix2t8xk22ubpfy - - NEWT_SECRET=nnisrfsdfc7prqsp9ewo1dvtvci50j5uiqotez00dgap0ii2 - - LOG_LEVEL=DEBUG \ No newline at end of file + - NEWT_SECRET=nnisrfsdfc7prqsp9ewo1dvtvci50j5uiqotez00dgap0ii2 \ No newline at end of file From 75518b2e04228b9ae920a85639256a8724ed6461 Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Tue, 7 Jan 2025 21:12:07 -0500 Subject: [PATCH 3/5] Ping interval --- main.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/main.go b/main.go index dd04ea9..7fe9026 100644 --- a/main.go +++ b/main.go @@ -112,6 +112,26 @@ func ping(tnet *netstack.Net, dst string) error { return nil } +func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{}) { + ticker := time.NewTicker(10 * time.Second) + defer ticker.Stop() + + go func() { + for { + select { + case <-ticker.C: + err := ping(tnet, serverIP) + if err != nil { + logger.Warn("Periodic ping failed: %v", err) + } + case <-stopChan: + logger.Info("Stopping ping check") + return + } + } + }() +} + func pingWithRetry(tnet *netstack.Net, dst string) error { const ( maxAttempts = 5 @@ -300,6 +320,9 @@ func main() { client.Close() }) + pingStopChan := make(chan struct{}) + defer close(pingStopChan) + // Register handlers for different message types client.RegisterHandler("newt/wg/connect", func(msg websocket.WSMessage) { logger.Info("Received registration message") @@ -374,6 +397,11 @@ persistent_keepalive_interval=5`, fixKey(fmt.Sprintf("%s", privateKey)), fixKey( logger.Error("Failed to ping %s: %v", wgData.ServerIP, err) } + if !connected { + logger.Info("Starting ping check") + startPingCheck(tnet, wgData.ServerIP, pingStopChan) + } + // Create proxy manager pm = proxy.NewProxyManager(tnet) From c298ff52f39eceff715836f5a9b1954a3c8150aa Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Tue, 7 Jan 2025 21:16:21 -0500 Subject: [PATCH 4/5] Update readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 3dc6f7e..1bfc666 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,22 @@ Example: --endpoint https://example.com ``` +You can also run it with Docker compose. For example, a service in your `docker-compose.yml` might look like this using environment vars (recommended): + +```yaml +services: + newt: + image: fosrl/newt + container_name: newt + restart: unless-stopped + environment: + - PANGOLIN_ENDPOINT=https://example.com + - NEWT_ID=2ix2t8xk22ubpfy + - NEWT_SECRET=nnisrfsdfc7prqsp9ewo1dvtvci50j5uiqotez00dgap0ii2 +``` + +You can also pass the CLI args to the container: + ```yaml services: newt: From 235a3b9426b10a628508dac0e4812c42bbc690ec Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Tue, 7 Jan 2025 21:45:30 -0500 Subject: [PATCH 5/5] Fix docker compose --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index d63747d..b67c69a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: newt: - image: newttest + image: fosrl/newt:latest container_name: newt restart: unless-stopped environment: