-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (84 loc) · 2.51 KB
/
Copy pathmain.go
File metadata and controls
101 lines (84 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/tomventa/wirebalancer/internal/config"
"github.com/tomventa/wirebalancer/internal/proxy"
"github.com/tomventa/wirebalancer/internal/stats"
"github.com/tomventa/wirebalancer/internal/webserver"
"github.com/tomventa/wirebalancer/internal/wireguard"
)
func main() {
configPath := flag.String("config", "config.yml", "Path to configuration file")
flag.Parse()
// Load configuration
cfg, err := config.Load(*configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Initialize stats tracker
statsTracker := stats.NewTracker(len(cfg.WireGuard.Connections))
// Initialize WireGuard manager
wgManager := wireguard.NewManager(cfg.WireGuard, statsTracker)
if err := wgManager.Initialize(); err != nil {
log.Fatalf("Failed to initialize WireGuard: %v", err)
}
// Create context for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start health checks
go wgManager.StartHealthChecks(ctx)
// Initialize proxy manager
proxyManager := proxy.NewManager(cfg.Proxy, wgManager, statsTracker)
// Start SOCKS5 proxies
var wg sync.WaitGroup
for i := 0; i < len(cfg.WireGuard.Connections)+1; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
port := cfg.Proxy.BasePort + index
if err := proxyManager.StartProxy(ctx, index, port); err != nil {
log.Printf("Failed to start proxy on port %d: %v", port, err)
}
}(i)
}
// Start web server for stats
webServer := webserver.New(cfg.WebServer.Port, statsTracker, wgManager)
go func() {
if err := webServer.Start(); err != nil {
log.Printf("Web server error: %v", err)
}
}()
log.Printf("WireBalancer started successfully")
log.Printf("Random proxy: localhost:%d", cfg.Proxy.BasePort)
for i := 0; i < len(cfg.WireGuard.Connections); i++ {
log.Printf("Connection %d proxy: localhost:%d", i, cfg.Proxy.BasePort+i+1)
}
log.Printf("Stats dashboard: http://localhost:%d", cfg.WebServer.Port)
// Wait for interrupt signal
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.Println("Shutting down gracefully...")
cancel()
// Wait for all goroutines with timeout
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
log.Println("All proxies stopped")
case <-time.After(10 * time.Second):
log.Println("Shutdown timeout, forcing exit")
}
wgManager.Cleanup()
log.Println("Shutdown complete")
}