-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchicha-ip-proxy.go
More file actions
214 lines (182 loc) · 7.17 KB
/
Copy pathchicha-ip-proxy.go
File metadata and controls
214 lines (182 loc) · 7.17 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Package main wires CLI parsing with TCP and UDP proxy workers.
// The application favors channels and goroutines to follow Go proverbs about communication over shared memory.
package main
import (
"errors"
"flag"
"fmt"
"log"
"runtime"
"strings"
"time"
"github.com/matveynator/chicha-ip-proxy/pkg/branding"
"github.com/matveynator/chicha-ip-proxy/pkg/config"
"github.com/matveynator/chicha-ip-proxy/pkg/limits"
"github.com/matveynator/chicha-ip-proxy/pkg/logging"
"github.com/matveynator/chicha-ip-proxy/pkg/proxy"
"github.com/matveynator/chicha-ip-proxy/pkg/setup"
"github.com/matveynator/chicha-ip-proxy/pkg/version"
)
func main() {
localFlag := flag.String("local", "", "Local port to listen on")
remoteFlag := flag.String("remote", "", "Remote target IP or IP:PORT")
protoFlag := flag.String("proto", "tcp", "Protocol to proxy: tcp or udp")
allowFlags := repeatedFlag{}
flag.Var(&allowFlags, "allow", "Client IP or CIDR allowed to use the proxy. Repeat for multiple sources.")
logFile := flag.String("log", "chicha-ip-proxy.log", "Path to the log file")
rotationFrequency := flag.Duration("rotation", 24*time.Hour, "Log rotation frequency (e.g. 24h, 1h, etc.)")
versionFlag := flag.Bool("version", false, "Print the version of the proxy and exit")
// Legacy route flags stay registered for compatibility but are intentionally absent from help output.
routesFlag := flag.String("routes", "", "legacy TCP routes in LOCALPORT:REMOTEIP:REMOTEPORT format")
udpRoutesFlag := flag.String("udp-routes", "", "legacy UDP routes in LOCALPORT:REMOTEIP:REMOTEPORT format")
flag.Usage = showFlagHelp
flag.Parse()
// Resolve the build version once so every subsystem prints a consistent identifier.
appVersion := version.Resolve()
if *versionFlag {
fmt.Printf("chicha-ip-proxy version %s\n", appVersion)
return
}
if err := validateRotationFrequency(*rotationFrequency); err != nil {
log.Fatalf("Error: %v", err)
}
tcpRoutes, udpRoutes, err := parseRoutesFromFlags(*routesFlag, *udpRoutesFlag, config.SimpleRouteFlags{
Local: *localFlag,
Remote: *remoteFlag,
Proto: *protoFlag,
})
if err != nil {
log.Fatalf("Error parsing route flags: %v", err)
}
allowList, err := config.ParseAllowList(allowFlags.Values)
if err != nil {
log.Fatalf("Error parsing allowed client sources: %v", err)
}
actualLogFile := *logFile
var autostartResult *setup.SystemdResult
// Fall back to interactive setup when no routes are provided.
if len(tcpRoutes) == 0 && len(udpRoutes) == 0 {
interactiveResult, err := setup.RunInteractiveSetup("chicha-ip-proxy")
if err != nil {
if errors.Is(err, setup.ErrSetupCancelled) {
fmt.Println("Setup cancelled.")
return
}
log.Fatalf("Interactive setup failed: %v", err)
}
tcpRoutes = interactiveResult.TCPRoutes
udpRoutes = interactiveResult.UDPRoutes
allowList = interactiveResult.AllowList
actualLogFile = interactiveResult.LogFile
autostartResult, err = setup.OfferAutostartSetup("chicha-ip-proxy", interactiveResult, *rotationFrequency)
if err != nil {
log.Printf("Autostart setup encountered an issue: %v", err)
}
}
if len(tcpRoutes) == 0 && len(udpRoutes) == 0 {
log.Fatal("Error: provide -local and -remote, use legacy -routes/-udp-routes, or run without route flags for interactive setup.")
}
printStartupSummary(tcpRoutes, udpRoutes, allowList, actualLogFile)
logger, file, err := logging.SetupLogger(actualLogFile)
if err != nil {
log.Fatalf("Error setting up logger: %v", err)
}
if err := limits.SetupLimits(logger); err != nil {
logger.Printf("System limit tuning encountered an issue: %v", err)
}
log.Printf("Starting chicha-ip-proxy version %s", appVersion)
numCPUs := runtime.NumCPU()
runtime.GOMAXPROCS(numCPUs)
logger.Printf("Using %d CPU cores", numCPUs)
log.Printf("Using %d CPU cores", numCPUs)
go logging.RotateLogs(actualLogFile, file, logger, *rotationFrequency, logging.DefaultMaxSizeBytes)
for _, route := range tcpRoutes {
listenAddr := ":" + route.LocalPort
targetAddr := route.RemoteAddress()
logger.Printf("Starting TCP proxy for route: local=%s remote=%s", listenAddr, targetAddr)
go proxy.StartTCPProxy(listenAddr, targetAddr, allowList, logger)
}
for _, route := range udpRoutes {
listenAddr := ":" + route.LocalPort
targetAddr := route.RemoteAddress()
logger.Printf("Starting UDP proxy for route: local=%s remote=%s", listenAddr, targetAddr)
go proxy.StartUDPProxy(listenAddr, targetAddr, allowList, logger)
}
if autostartResult != nil && autostartResult.FollowLogs {
stop := make(chan struct{})
go setup.StreamLogs(actualLogFile, stop)
}
select {}
}
func printStartupSummary(tcpRoutes, udpRoutes []config.Route, allowList config.AllowList, logFile string) {
fmt.Print(branding.Banner)
for _, route := range tcpRoutes {
fmt.Printf("tcp :%s -> %s\n", route.LocalPort, route.RemoteAddress())
}
for _, route := range udpRoutes {
fmt.Printf("udp :%s -> %s\n", route.LocalPort, route.RemoteAddress())
}
fmt.Printf("allow %s\n", allowListSummary(allowList))
fmt.Printf("log %s\n\n", logFile)
}
func parseRoutesFromFlags(legacyTCPRoutes, legacyUDPRoutes string, simpleFlags config.SimpleRouteFlags) ([]config.Route, []config.Route, error) {
if legacyTCPRoutes != "" || legacyUDPRoutes != "" {
tcpRoutes, err := config.ParseRoutes(legacyTCPRoutes)
if err != nil {
return nil, nil, fmt.Errorf("legacy TCP routes: %v", err)
}
udpRoutes, err := config.ParseRoutes(legacyUDPRoutes)
if err != nil {
return nil, nil, fmt.Errorf("legacy UDP routes: %v", err)
}
return tcpRoutes, udpRoutes, nil
}
tcpRoutes, udpRoutes, _, err := config.ParseSimpleRoute(simpleFlags)
return tcpRoutes, udpRoutes, err
}
func validateRotationFrequency(rotation time.Duration) error {
if rotation <= 0 {
return fmt.Errorf("-rotation must be positive")
}
return nil
}
// repeatedFlag stores every occurrence of flags such as -allow.
type repeatedFlag struct {
Values []string
}
func (flagValue *repeatedFlag) String() string {
return strings.Join(flagValue.Values, ",")
}
func (flagValue *repeatedFlag) Set(value string) error {
flagValue.Values = append(flagValue.Values, value)
return nil
}
// allowListSummary keeps CLI output explicit about whether the proxy is open or restricted.
func allowListSummary(allowList config.AllowList) string {
values := allowList.FlagValues()
if len(values) == 0 {
return "all client IPs"
}
return strings.Join(values, ", ")
}
// showFlagHelp displays CLI usage and runnable examples for scripted runs.
func showFlagHelp() {
fmt.Print(branding.Banner)
fmt.Println("Usage:")
fmt.Println(" chicha-ip-proxy -local=PORT -remote=IP|IP:PORT|[IPv6]:PORT [options]")
fmt.Println(" chicha-ip-proxy # setup wizard")
fmt.Println()
fmt.Println("Flags:")
fmt.Println(" -local PORT")
fmt.Println(" -remote IP|IP:PORT|[IPv6]:PORT")
fmt.Println(" -proto tcp|udp")
fmt.Println(" -allow IP|CIDR")
fmt.Println(" -log PATH")
fmt.Println(" -rotation 24h")
fmt.Println(" -version")
fmt.Println()
fmt.Println("Examples:")
fmt.Println(" chicha-ip-proxy -local=8080 -remote=203.0.113.10 -allow=198.51.100.7")
fmt.Println(" chicha-ip-proxy -local=5353 -remote=203.0.113.20:53 -proto=udp")
fmt.Println(" chicha-ip-proxy -local=8443 -remote=[2001:db8::10]:443")
}