Adjust logging

This commit is contained in:
Owen
2025-07-08 08:48:27 -07:00
parent 07bd283604
commit e4bdbbec7c
3 changed files with 23 additions and 14 deletions

24
main.go
View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"net"
"net/http" "net/http"
"net/netip" "net/netip"
"os" "os"
@@ -86,8 +87,8 @@ var (
dockerSocket string dockerSocket string
dockerEnforceNetworkValidation string dockerEnforceNetworkValidation string
dockerEnforceNetworkValidationBool bool dockerEnforceNetworkValidationBool bool
pingInterval = 1 * time.Second pingInterval = 2 * time.Second
pingTimeout = 2 * time.Second pingTimeout = 3 * time.Second
publicKey wgtypes.Key publicKey wgtypes.Key
pingStopChan chan struct{} pingStopChan chan struct{}
stopFunc func() stopFunc func()
@@ -330,7 +331,7 @@ func main() {
clientsHandleNewtConnection(wgData.PublicKey) clientsHandleNewtConnection(wgData.PublicKey)
logger.Info("Received: %+v", msg) logger.Debug("Received: %+v", msg)
tun, tnet, err = netstack.CreateNetTUN( tun, tnet, err = netstack.CreateNetTUN(
[]netip.Addr{netip.MustParseAddr(wgData.TunnelIP)}, []netip.Addr{netip.MustParseAddr(wgData.TunnelIP)},
[]netip.Addr{netip.MustParseAddr(dns)}, []netip.Addr{netip.MustParseAddr(dns)},
@@ -345,6 +346,14 @@ func main() {
"wireguard: ", "wireguard: ",
)) ))
host, _, err := net.SplitHostPort(wgData.Endpoint)
if err != nil {
logger.Error("Failed to split endpoint: %v", err)
return
}
logger.Info("Connecting to endpoint: %s", host)
endpoint, err := resolveDomain(wgData.Endpoint) endpoint, err := resolveDomain(wgData.Endpoint)
if err != nil { if err != nil {
logger.Error("Failed to resolve endpoint: %v", err) logger.Error("Failed to resolve endpoint: %v", err)
@@ -369,7 +378,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
logger.Error("Failed to bring up WireGuard device: %v", err) logger.Error("Failed to bring up WireGuard device: %v", err)
} }
logger.Info("WireGuard device created. Lets ping the server now...") logger.Debug("WireGuard device created. Lets ping the server now...")
// Even if pingWithRetry returns an error, it will continue trying in the background // Even if pingWithRetry returns an error, it will continue trying in the background
if pingWithRetryStopChan != nil { if pingWithRetryStopChan != nil {
@@ -382,7 +391,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
// Always mark as connected and start the proxy manager regardless of initial ping result // Always mark as connected and start the proxy manager regardless of initial ping result
// as the pings will continue in the background // as the pings will continue in the background
if !connected { if !connected {
logger.Info("Starting ping check") logger.Debug("Starting ping check")
pingStopChan = startPingCheck(tnet, wgData.ServerIP, client) pingStopChan = startPingCheck(tnet, wgData.ServerIP, client)
} }
@@ -417,7 +426,6 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
// Mark as disconnected // Mark as disconnected
connected = false connected = false
// start asking for the exit nodes again
if stopFunc != nil { if stopFunc != nil {
stopFunc() // stop the ws from sending more requests stopFunc() // stop the ws from sending more requests
stopFunc = nil // reset stopFunc to nil to avoid double stopping stopFunc = nil // reset stopFunc to nil to avoid double stopping
@@ -438,7 +446,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
// Mark as disconnected // Mark as disconnected
connected = false connected = false
logger.Info("Tunnel destroyed, ready for reconnection") logger.Info("Tunnel destroyed")
}) })
client.RegisterHandler("newt/ping/exitNodes", func(msg websocket.WSMessage) { client.RegisterHandler("newt/ping/exitNodes", func(msg websocket.WSMessage) {
@@ -547,7 +555,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
}) })
client.RegisterHandler("newt/tcp/add", func(msg websocket.WSMessage) { client.RegisterHandler("newt/tcp/add", func(msg websocket.WSMessage) {
logger.Info("Received: %+v", msg) logger.Debug("Received: %+v", msg)
// if there is no wgData or pm, we can't add targets // if there is no wgData or pm, we can't add targets
if wgData.TunnelIP == "" || pm == nil { if wgData.TunnelIP == "" || pm == nil {

View File

@@ -213,7 +213,8 @@ func (pm *ProxyManager) startTarget(proto, listenIP string, port int, targetAddr
return fmt.Errorf("unsupported protocol: %s", proto) return fmt.Errorf("unsupported protocol: %s", proto)
} }
logger.Info("Started %s proxy from %s:%d to %s", proto, listenIP, port, targetAddr) logger.Info("Started %s proxy to %s", proto, targetAddr)
logger.Debug("Started %s proxy from %s:%d to %s", proto, listenIP, port, targetAddr)
return nil return nil
} }

10
util.go
View File

@@ -112,10 +112,10 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
retryDelay := initialRetryDelay retryDelay := initialRetryDelay
// First try with the initial parameters // First try with the initial parameters
logger.Info("Ping attempt %d", attempt) logger.Debug("Ping attempt %d", attempt)
if latency, err := ping(tnet, dst, timeout); err == nil { if latency, err := ping(tnet, dst, timeout); err == nil {
// Successful ping // Successful ping
logger.Info("Ping latency: %v", latency) logger.Debug("Ping latency: %v", latency)
logger.Info("Tunnel connection to server established successfully!") logger.Info("Tunnel connection to server established successfully!")
if healthFile != "" { if healthFile != "" {
err := os.WriteFile(healthFile, []byte("ok"), 0644) err := os.WriteFile(healthFile, []byte("ok"), 0644)
@@ -137,7 +137,7 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
case <-stopChan: case <-stopChan:
return return
default: default:
logger.Info("Ping attempt %d", attempt) logger.Debug("Ping attempt %d", attempt)
if latency, err := ping(tnet, dst, timeout); err != nil { if latency, err := ping(tnet, dst, timeout); err != nil {
logger.Warn("Ping attempt %d failed: %v", attempt, err) logger.Warn("Ping attempt %d failed: %v", attempt, err)
@@ -155,8 +155,8 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
attempt++ attempt++
} else { } else {
// Successful ping // Successful ping
logger.Info("Ping succeeded after %d attempts", attempt) logger.Debug("Ping succeeded after %d attempts", attempt)
logger.Info("Ping latency: %v", latency) logger.Debug("Ping latency: %v", latency)
logger.Info("Tunnel connection to server established successfully!") logger.Info("Tunnel connection to server established successfully!")
if healthFile != "" { if healthFile != "" {
err := os.WriteFile(healthFile, []byte("ok"), 0644) err := os.WriteFile(healthFile, []byte("ok"), 0644)