Skip to content

Commit 313afdb

Browse files
Merge pull request #3 from fosrl/dev
Allow use of env vars, docs, and ping interval
2 parents 2c612d4 + 235a3b9 commit 313afdb

File tree

4 files changed

+75
-24
lines changed

4 files changed

+75
-24
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ Example:
4646
--endpoint https://example.com
4747
```
4848

49+
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):
50+
51+
```yaml
52+
services:
53+
newt:
54+
image: fosrl/newt
55+
container_name: newt
56+
restart: unless-stopped
57+
environment:
58+
- PANGOLIN_ENDPOINT=https://example.com
59+
- NEWT_ID=2ix2t8xk22ubpfy
60+
- NEWT_SECRET=nnisrfsdfc7prqsp9ewo1dvtvci50j5uiqotez00dgap0ii2
61+
```
62+
63+
You can also pass the CLI args to the container:
64+
4965
```yaml
5066
services:
5167
newt:

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
newt:
3+
image: fosrl/newt:latest
4+
container_name: newt
5+
restart: unless-stopped
6+
environment:
7+
- PANGOLIN_ENDPOINT=https://example.com
8+
- NEWT_ID=2ix2t8xk22ubpfy
9+
- NEWT_SECRET=nnisrfsdfc7prqsp9ewo1dvtvci50j5uiqotez00dgap0ii2

entrypoint.sh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
#!/bin/sh
22

3-
# Sample from https://github.com/traefik/traefik-library-image/blob/5070edb25b03cca6802d75d5037576c840f73fdd/v3.1/alpine/entrypoint.sh
4-
53
set -e
64

75
# first arg is `-f` or `--some-option`
86
if [ "${1#-}" != "$1" ]; then
97
set -- newt "$@"
108
fi
119

12-
# if our command is a valid newt subcommand, let's invoke it through newt instead
13-
# (this allows for "docker run newt version", etc)
14-
if newt "$1" --help >/dev/null 2>&1
15-
then
16-
set -- newt "$@"
17-
else
18-
echo "= '$1' is not a newt command: assuming shell execution." 1>&2
19-
fi
20-
2110
exec "$@"

main.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ func ping(tnet *netstack.Net, dst string) error {
112112
return nil
113113
}
114114

115+
func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{}) {
116+
ticker := time.NewTicker(10 * time.Second)
117+
defer ticker.Stop()
118+
119+
go func() {
120+
for {
121+
select {
122+
case <-ticker.C:
123+
err := ping(tnet, serverIP)
124+
if err != nil {
125+
logger.Warn("Periodic ping failed: %v", err)
126+
}
127+
case <-stopChan:
128+
logger.Info("Stopping ping check")
129+
return
130+
}
131+
}
132+
}()
133+
}
134+
115135
func pingWithRetry(tnet *netstack.Net, dst string) error {
116136
const (
117137
maxAttempts = 5
@@ -222,13 +242,6 @@ func resolveDomain(domain string) (string, error) {
222242
return ipAddr, nil
223243
}
224244

225-
func getEnvWithDefault(key, defaultValue string) string {
226-
if value := os.Getenv(key); value != "" {
227-
return value
228-
}
229-
return defaultValue
230-
}
231-
232245
func main() {
233246
var (
234247
endpoint string
@@ -240,12 +253,28 @@ func main() {
240253
logLevel string
241254
)
242255

243-
// Define CLI flags with default values from environment variables
244-
flag.StringVar(&endpoint, "endpoint", os.Getenv("PANGOLIN_ENDPOINT"), "Endpoint of your pangolin server")
245-
flag.StringVar(&id, "id", os.Getenv("NEWT_ID"), "Newt ID")
246-
flag.StringVar(&secret, "secret", os.Getenv("NEWT_SECRET"), "Newt secret")
247-
flag.StringVar(&dns, "dns", getEnvWithDefault("DEFAULT_DNS", "8.8.8.8"), "DNS server to use")
248-
flag.StringVar(&logLevel, "log-level", getEnvWithDefault("LOG_LEVEL", "INFO"), "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
256+
// if PANGOLIN_ENDPOINT, NEWT_ID, and NEWT_SECRET are set as environment variables, they will be used as default values
257+
endpoint = os.Getenv("PANGOLIN_ENDPOINT")
258+
id = os.Getenv("NEWT_ID")
259+
secret = os.Getenv("NEWT_SECRET")
260+
dns = os.Getenv("DNS")
261+
logLevel = os.Getenv("LOG_LEVEL")
262+
263+
if endpoint == "" {
264+
flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server")
265+
}
266+
if id == "" {
267+
flag.StringVar(&id, "id", "", "Newt ID")
268+
}
269+
if secret == "" {
270+
flag.StringVar(&secret, "secret", "", "Newt secret")
271+
}
272+
if dns == "" {
273+
flag.StringVar(&dns, "dns", "8.8.8.8", "DNS server to use")
274+
}
275+
if logLevel == "" {
276+
flag.StringVar(&logLevel, "log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
277+
}
249278
flag.Parse()
250279

251280
logger.Init()
@@ -291,6 +320,9 @@ func main() {
291320
client.Close()
292321
})
293322

323+
pingStopChan := make(chan struct{})
324+
defer close(pingStopChan)
325+
294326
// Register handlers for different message types
295327
client.RegisterHandler("newt/wg/connect", func(msg websocket.WSMessage) {
296328
logger.Info("Received registration message")
@@ -365,6 +397,11 @@ persistent_keepalive_interval=5`, fixKey(fmt.Sprintf("%s", privateKey)), fixKey(
365397
logger.Error("Failed to ping %s: %v", wgData.ServerIP, err)
366398
}
367399

400+
if !connected {
401+
logger.Info("Starting ping check")
402+
startPingCheck(tnet, wgData.ServerIP, pingStopChan)
403+
}
404+
368405
// Create proxy manager
369406
pm = proxy.NewProxyManager(tnet)
370407

0 commit comments

Comments
 (0)