mirror of
https://github.com/fosrl/newt.git
synced 2026-02-27 23:36:41 +00:00
Merge branch 'dev' into clients-pops
This commit is contained in:
5
main.go
5
main.go
@@ -93,6 +93,7 @@ var (
|
|||||||
publicKey wgtypes.Key
|
publicKey wgtypes.Key
|
||||||
pingStopChan chan struct{}
|
pingStopChan chan struct{}
|
||||||
stopFunc func()
|
stopFunc func()
|
||||||
|
healthFile string
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -113,6 +114,7 @@ func main() {
|
|||||||
pingIntervalStr := os.Getenv("PING_INTERVAL")
|
pingIntervalStr := os.Getenv("PING_INTERVAL")
|
||||||
pingTimeoutStr := os.Getenv("PING_TIMEOUT")
|
pingTimeoutStr := os.Getenv("PING_TIMEOUT")
|
||||||
dockerEnforceNetworkValidation = os.Getenv("DOCKER_ENFORCE_NETWORK_VALIDATION")
|
dockerEnforceNetworkValidation = os.Getenv("DOCKER_ENFORCE_NETWORK_VALIDATION")
|
||||||
|
healthFile = os.Getenv("HEALTH_FILE")
|
||||||
|
|
||||||
if endpoint == "" {
|
if endpoint == "" {
|
||||||
flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server")
|
flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server")
|
||||||
@@ -175,6 +177,9 @@ func main() {
|
|||||||
if dockerEnforceNetworkValidation == "" {
|
if dockerEnforceNetworkValidation == "" {
|
||||||
flag.StringVar(&dockerEnforceNetworkValidation, "docker-enforce-network-validation", "false", "Enforce validation of container on newt network (true or false)")
|
flag.StringVar(&dockerEnforceNetworkValidation, "docker-enforce-network-validation", "false", "Enforce validation of container on newt network (true or false)")
|
||||||
}
|
}
|
||||||
|
if healthFile == "" {
|
||||||
|
flag.StringVar(&healthFile, "health-file", "", "Path to health file (if unset, health file won’t be written)")
|
||||||
|
}
|
||||||
|
|
||||||
// do a --version check
|
// do a --version check
|
||||||
version := flag.Bool("version", false, "Print the version")
|
version := flag.Bool("version", false, "Print the version")
|
||||||
|
|||||||
33
util.go
33
util.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -92,6 +93,14 @@ func ping(tnet *netstack.Net, dst string, timeout time.Duration) (time.Duration,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopChan chan struct{}, err error) {
|
func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopChan chan struct{}, err error) {
|
||||||
|
|
||||||
|
if healthFile != "" {
|
||||||
|
err = os.Remove(healthFile)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to remove health file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
initialMaxAttempts = 5
|
initialMaxAttempts = 5
|
||||||
initialRetryDelay = 2 * time.Second
|
initialRetryDelay = 2 * time.Second
|
||||||
@@ -108,6 +117,12 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
|
|||||||
// Successful ping
|
// Successful ping
|
||||||
logger.Info("Ping latency: %v", latency)
|
logger.Info("Ping latency: %v", latency)
|
||||||
logger.Info("Tunnel connection to server established successfully!")
|
logger.Info("Tunnel connection to server established successfully!")
|
||||||
|
if healthFile != "" {
|
||||||
|
err := os.WriteFile(healthFile, []byte("ok"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to write health file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return stopChan, nil
|
return stopChan, nil
|
||||||
} else {
|
} else {
|
||||||
logger.Warn("Ping attempt %d failed: %v", attempt, err)
|
logger.Warn("Ping attempt %d failed: %v", attempt, err)
|
||||||
@@ -143,6 +158,12 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
|
|||||||
logger.Info("Ping succeeded after %d attempts", attempt)
|
logger.Info("Ping succeeded after %d attempts", attempt)
|
||||||
logger.Info("Ping latency: %v", latency)
|
logger.Info("Ping latency: %v", latency)
|
||||||
logger.Info("Tunnel connection to server established successfully!")
|
logger.Info("Tunnel connection to server established successfully!")
|
||||||
|
if healthFile != "" {
|
||||||
|
err := os.WriteFile(healthFile, []byte("ok"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to write health file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,6 +206,12 @@ func startPingCheck(tnet *netstack.Net, serverIP string, client *websocket.Clien
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to send registration message: %v", err)
|
logger.Error("Failed to send registration message: %v", err)
|
||||||
}
|
}
|
||||||
|
if healthFile != "" {
|
||||||
|
err = os.Remove(healthFile)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to remove health file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
currentInterval = time.Duration(float64(currentInterval) * 1.5)
|
currentInterval = time.Duration(float64(currentInterval) * 1.5)
|
||||||
if currentInterval > maxInterval {
|
if currentInterval > maxInterval {
|
||||||
@@ -197,6 +224,12 @@ func startPingCheck(tnet *netstack.Net, serverIP string, client *websocket.Clien
|
|||||||
if connectionLost {
|
if connectionLost {
|
||||||
connectionLost = false
|
connectionLost = false
|
||||||
logger.Info("Connection to server restored!")
|
logger.Info("Connection to server restored!")
|
||||||
|
if healthFile != "" {
|
||||||
|
err := os.WriteFile(healthFile, []byte("ok"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to write health file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if currentInterval > initialInterval {
|
if currentInterval > initialInterval {
|
||||||
currentInterval = time.Duration(float64(currentInterval) * 0.8)
|
currentInterval = time.Duration(float64(currentInterval) * 0.8)
|
||||||
|
|||||||
Reference in New Issue
Block a user