-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
149 lines (126 loc) · 3.95 KB
/
main.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"flag"
"fmt"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"Groxy/logger"
"Groxy/proxy"
"Groxy/servers"
"Groxy/tls"
"Groxy/auth"
cryptotls "crypto/tls"
)
var (
targetURLStr string
transparent bool
customHeader string
enableHTTP bool
enableHTTPS bool
workers int
queueSize int
timeout int
enableObfuscation bool
enableRedirection bool
)
func main() {
flag.StringVar(&targetURLStr, "t", "", "Target URL for target-specific mode (e.g., http://10.10.10.80)")
flag.BoolVar(&transparent, "transparent", false, "Run in transparent mode")
flag.StringVar(&customHeader, "H", "", "Add a custom header (e.g., \"X-Request-ID: 12345\")")
flag.BoolVar(&enableHTTP, "http", false, "Enable the HTTP server")
flag.BoolVar(&enableHTTPS, "https", false, "Enable the HTTPS server")
flag.IntVar(&workers, "workers", 0, "Number of worker goroutines to use (0 disables worker pool)")
flag.IntVar(&queueSize, "queue-size", 100, "Size of the job queue for worker pool")
flag.IntVar(&timeout, "timeout", 30, "Timeout for requests in seconds")
flag.BoolVar(&enableObfuscation, "obfuscate", false, "Enable strong traffic obfuscation")
flag.BoolVar(&enableRedirection, "redirect", false, "Enable HTTP to HTTPS redirection")
flag.Parse()
logger.Init()
defer logger.LogFile.Close()
if !transparent && targetURLStr == "" {
fmt.Println("Error: You must specify either -t <target> or --transparent")
flag.Usage()
os.Exit(1)
}
if transparent && targetURLStr != "" {
fmt.Println("Error: You cannot specify both -t <target> and --transparent")
flag.Usage()
os.Exit(1)
}
if !enableHTTP && !enableHTTPS {
fmt.Println("Error: You must specify either -http or -https to enable the server")
flag.Usage()
os.Exit(1)
}
if enableRedirection && !enableHTTPS {
fmt.Println("Error: You must enable HTTPS (-https) when redirection (-redirect) is enabled")
flag.Usage()
os.Exit(1)
}
authModule := auth.InitAuthFromFlags()
tlsConfig := tls.NewConfig("certs/server-cert.pem", "certs/server-key.pem")
tlsManager := tls.NewManager(tlsConfig)
tlsManager.OnRotation = func(cert *cryptotls.Certificate) {
fmt.Println("Certificate rotated successfully")
}
tlsManager.OnError = func(err error) {
fmt.Printf("Certificate rotation error: %v\n", err)
}
var targetURL *url.URL
if !transparent {
var err error
targetURL, err = url.Parse(targetURLStr)
if err != nil || targetURL.Scheme == "" || targetURL.Host == "" {
fmt.Println("Failed to parse target URL: Invalid URL format")
os.Exit(1)
}
}
proxyHandler := proxy.NewProxy(targetURL, tlsConfig, customHeader, enableObfuscation)
proxyHandler.SetAuthModule(authModule)
proxyHandler.SetTimeout(time.Duration(timeout) * time.Second)
if workers > 0 {
fmt.Printf("Enabling worker pool with %d workers and queue size of %d\n", workers, queueSize)
proxyHandler.EnableWorkerPool(workers, queueSize)
}
server := servers.NewServer(
proxyHandler.Handler(),
tlsManager,
"certs/server-cert.pem",
"certs/server-key.pem",
"8080",
"8443",
)
server.SetRedirection(enableRedirection)
if enableHTTP {
if err := server.StartHTTP(); err != nil {
fmt.Printf("Failed to start HTTP server: %v\n", err)
os.Exit(1)
}
fmt.Println("HTTP server is running on port 8080")
}
if enableHTTPS {
if err := server.StartHTTPS(); err != nil {
fmt.Printf("Failed to start HTTPS server: %v\n", err)
os.Exit(1)
}
fmt.Println("HTTPS server is running on port 8443")
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigChan
fmt.Printf("Received signal %v, shutting down gracefully...\n", sig)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if workers > 0 {
proxyHandler.StopWorkerPool()
}
proxyHandler.Shutdown()
if err := server.Shutdown(ctx); err != nil {
fmt.Printf("Server shutdown error: %v\n", err)
}
fmt.Println("Server shutdown complete")
}