Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lsgrep committed Dec 18, 2024
1 parent ae68bb4 commit a51d59e
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions cmd/swgp-go/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"context"
"errors"
"fmt"
"github.com/database64128/swgp-go/tslog"
"log/slog"
"net"
"os"
"strings"
Expand All @@ -15,7 +17,6 @@ import (
"unsafe"

"github.com/database64128/swgp-go/service"
"go.uber.org/zap"
"golang.org/x/net/route"
)

Expand All @@ -28,7 +29,7 @@ var (
type GatewayMonitor struct {
mu sync.RWMutex
ip net.IP
logger *zap.Logger
logger *tslog.Logger
cfg *service.Config
ctx context.Context
cancel context.CancelFunc
Expand All @@ -38,7 +39,7 @@ type GatewayMonitor struct {
}

// NewGatewayMonitor creates a new gateway monitor instance
func NewGatewayMonitor(cfg *service.Config, logger *zap.Logger, interval time.Duration) (*GatewayMonitor, error) {
func NewGatewayMonitor(cfg *service.Config, logger *tslog.Logger, interval time.Duration) (*GatewayMonitor, error) {
ctx, cancel := context.WithCancel(context.Background())

// Open routing socket
Expand Down Expand Up @@ -120,13 +121,13 @@ func (g *GatewayMonitor) discoverGateway() (net.IP, error) {
}
}
if len(ips) > 0 {
g.logger.Info("Found gateway", zap.String("ip", ips[0].String()))
g.logger.Info("Found gateway", slog.String("ip", ips[0].String()))
return ips[0], nil
}
return nil, fmt.Errorf("no default gateway found")
}

func isValidGateway(ip net.IP, logger *zap.Logger) bool {
func isValidGateway(ip net.IP, logger *tslog.Logger) bool {
if ip == nil || ip.Equal(net.IPv4zero) {
logger.Debug("Invalid gateway: nil or zero IP")
return false
Expand All @@ -143,15 +144,15 @@ func isValidGateway(ip net.IP, logger *zap.Logger) bool {
for _, network := range privateNetworks {
_, ipnet, err := net.ParseCIDR(network)
if err != nil {
logger.Error("Failed to parse CIDR", zap.String("network", network), zap.Error(err))
logger.Error("Failed to parse CIDR", slog.String("network", network), tslog.Err(err))
continue
}
if ipnet.Contains(ip) {
return true
}
}

logger.Debug("Invalid gateway: not in private or link-local range", zap.String("ip", ip.String()))
logger.Debug("Invalid gateway: not in private or link-local range", slog.String("ip", ip.String()))
return false
}

Expand Down Expand Up @@ -319,7 +320,7 @@ func (g *GatewayMonitor) deleteRouteSyscall(dest net.IP) error {
}

g.logger.Info("Successfully deleted route using syscall",
zap.String("destination", dest.String()))
slog.String("destination", dest.String()))
return nil
}

Expand Down Expand Up @@ -359,7 +360,7 @@ func (g *GatewayMonitor) verifyRoutesSyscall(gatewayIP net.IP) (map[string]bool,
fd, err := syscall.Socket(syscall.AF_ROUTE, syscall.SOCK_RAW, syscall.AF_UNSPEC)
if err != nil {
g.logger.Error("Failed to open route socket",
zap.Error(err))
tslog.Err(err))
return nil, err
}
defer syscall.Close(fd)
Expand All @@ -368,15 +369,15 @@ func (g *GatewayMonitor) verifyRoutesSyscall(gatewayIP net.IP) (map[string]bool,
tab, err := syscall.RouteRIB(syscall.NET_RT_DUMP2, 0)
if err != nil {
g.logger.Error("Failed to get routing table",
zap.Error(err))
tslog.Err(err))
return nil, err
}

// Parse the routing messages
msgs, err := syscall.ParseRoutingMessage(tab)
if err != nil {
g.logger.Error("Failed to parse routing messages",
zap.Error(err))
tslog.Err(err))
return nil, err
}

Expand Down Expand Up @@ -422,7 +423,7 @@ func (g *GatewayMonitor) updateRoutes(gatewayIP net.IP) error {
return ErrInvalidGateway
}

g.logger.Info("Updating gateway routes", zap.String("gateway", gatewayIP.String()))
g.logger.Info("Updating gateway routes", slog.String("gateway", gatewayIP.String()))

for _, client := range g.cfg.Clients {
clientAddr := client.ProxyEndpointAddress.IP()
Expand All @@ -432,8 +433,8 @@ func (g *GatewayMonitor) updateRoutes(gatewayIP net.IP) error {
err := g.deleteRouteSyscall(clientIP)
if err != nil {
g.logger.Debug("Route deletion failed (may not exist)",
zap.String("client", clientAddr.String()),
zap.Error(err))
slog.String("client", clientAddr.String()),
tslog.Err(err))
}

// Add the new route
Expand All @@ -452,7 +453,7 @@ func (g *GatewayMonitor) cleanup() {
// Get current routes first
routes, err := g.verifyRoutesSyscall(g.ip)
if err != nil {
g.logger.Warn("Failed to get current routes during cleanup", zap.Error(err))
g.logger.Warn("Failed to get current routes during cleanup", tslog.Err(err))
}

maxRetries := 3
Expand All @@ -474,13 +475,13 @@ func (g *GatewayMonitor) cleanup() {
// Only log as error if it's not "no such route"
if !strings.Contains(err.Error(), "no such process") {
g.logger.Error("Failed to delete route",
zap.String("client", clientAddr.String()),
zap.Error(err))
slog.String("client", clientAddr.String()),
tslog.Err(err))
allDeleted = false
}
} else {
g.logger.Info("Successfully deleted route",
zap.String("client", clientAddr.String()))
slog.String("client", clientAddr.String()))
}
}

Expand All @@ -494,8 +495,8 @@ func (g *GatewayMonitor) cleanup() {
routes, err = g.verifyRoutesSyscall(g.ip)
if err != nil {
g.logger.Warn("Failed to verify routes during cleanup retry",
zap.Int("retry", retry+1),
zap.Error(err))
slog.Int("retry", retry+1),
tslog.Err(err))
}
}

Expand Down Expand Up @@ -544,9 +545,9 @@ func (g *GatewayMonitor) watch() {
if err != nil {
consecutiveErrors++
if consecutiveErrors > 3 {
g.logger.Error("Failed to get gateway address", zap.Error(err))
g.logger.Error("Failed to get gateway address", tslog.Err(err))
} else {
g.logger.Debug("Temporary error getting gateway", zap.Error(err))
g.logger.Debug("Temporary error getting gateway", tslog.Err(err))
}

// If we have a last valid gateway, keep using it
Expand All @@ -563,8 +564,8 @@ func (g *GatewayMonitor) watch() {
gatewayChanged := !ip.Equal(g.ip)
if gatewayChanged {
g.logger.Info("Gateway IP changed",
zap.String("old", g.ip.String()),
zap.String("new", ip.String()))
slog.String("old", g.ip.String()),
slog.String("new", ip.String()))

// Delete old routes before updating the gateway IP
g.logger.Info("Cleaning up old routes")
Expand All @@ -576,7 +577,7 @@ func (g *GatewayMonitor) watch() {

// Add new routes
if err := g.updateRoutes(ip); err != nil {
g.logger.Error("Failed to update routes", zap.Error(err))
g.logger.Error("Failed to update routes", tslog.Err(err))
}
}
g.mu.Unlock()
Expand All @@ -587,15 +588,15 @@ func (g *GatewayMonitor) watch() {
var monitor *GatewayMonitor

// Initialize sets up the gateway monitor
func initHook(cfg *service.Config, logger *zap.Logger) {
func initHook(cfg *service.Config, logger *tslog.Logger) {
var err error
monitor, err = NewGatewayMonitor(cfg, logger, 10*time.Second)
if err != nil {
logger.Fatal("Failed to create gateway monitor", zap.Error(err))
panic(err)
}

if err := monitor.Start(); err != nil {
logger.Fatal("Failed to start gateway monitor", zap.Error(err))
panic(err)
}
}

Expand Down

0 comments on commit a51d59e

Please sign in to comment.