-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (175 loc) · 6.58 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
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
// Copyright 2019 Path Network, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net"
"os"
"syscall"
"time"
"go.uber.org/zap"
)
type options struct {
Protocol string
ListenAddr arrayFlags
UpstreamAddr string
Fallback bool
EnableTransparentProxy bool
Mark int
Verbose int
allowedSubnetsPath string
AllowedSubnets []*net.IPNet
Listeners int
ProtocolMatchers []ProtocolMatcher
tlsUpstreamAddr string
httpUpstreamAddr string
sshUpstreamAddr string
eternalTerminalUpstreamAddr string
Logger *zap.Logger
udpCloseAfter int
UDPCloseAfter time.Duration
}
type arrayFlags []string
func (i *arrayFlags) String() string {
if i == nil {
return "[]"
}
return fmt.Sprintf("%+q", *i)
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var Opts options
func init() {
flag.StringVar(&Opts.Protocol, "p", "tcp", "Protocol that will be proxied: tcp, udp, both")
flag.BoolVar(&Opts.Fallback, "fallback", true, "Whether to fallback when decode on decoding PROXY protocol failure")
flag.BoolVar(&Opts.EnableTransparentProxy, "t", true, "Whether to enable transparent proxy")
flag.Var(&(Opts.ListenAddr), "l", "Address the proxy listens on")
flag.StringVar(&Opts.UpstreamAddr, "u", "127.0.0.1:443", "UpstreamAddr address to which traffic will be forwarded to")
flag.StringVar(&Opts.httpUpstreamAddr, "http", "", "UpstreamAddr address to which http traffic will be forwarded to")
flag.StringVar(&Opts.tlsUpstreamAddr, "tls", "", "UpstreamAddr address to which tls traffic will be forwarded to")
flag.StringVar(&Opts.sshUpstreamAddr, "ssh", "", "UpstreamAddr address to which ssh traffic will be forwarded to")
flag.StringVar(&Opts.eternalTerminalUpstreamAddr, "eternal-terminal", "", "UpstreamAddr address to which eternal terminal traffic will be forwarded to")
flag.IntVar(&Opts.Mark, "mark", 0, "The mark that will be set on outbound packets")
flag.IntVar(&Opts.Verbose, "v", 0, `0 - no logging of individual connections
1 - log errors occurring in individual connections
2 - log all state changes of individual connections`)
flag.StringVar(&Opts.allowedSubnetsPath, "allowed-subnets", "",
"Path to a file that contains allowed subnets of the proxy servers")
flag.IntVar(&Opts.Listeners, "listeners", 1,
"Number of listener sockets that will be opened for the listen address (Linux 3.9+)")
flag.IntVar(&Opts.udpCloseAfter, "close-after", 60, "Number of seconds after which UDP socket will be cleaned up")
}
func listen(listenerNum int, errors chan<- error) {
logger := Opts.Logger.With(zap.Int("listenerNum", listenerNum),
zap.String("protocol", Opts.Protocol), zap.Stringer("listenAdr", &Opts.ListenAddr))
listenConfig := net.ListenConfig{}
if Opts.Listeners > 1 {
listenConfig.Control = func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
soReusePort := 15
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReusePort, 1); err != nil {
logger.Warn("failed to set SO_REUSEPORT - only one listener setup will succeed")
}
})
}
}
for _, addr := range Opts.ListenAddr {
if Opts.Protocol == "tcp" {
go TCPListen(&listenConfig, addr, logger, errors)
} else if Opts.Protocol == "udp" {
go UDPListen(&listenConfig, addr, logger, errors)
} else {
go TCPListen(&listenConfig, addr, logger, errors)
go UDPListen(&listenConfig, addr, logger, errors)
}
}
}
func loadAllowedSubnets() error {
file, err := os.Open(Opts.allowedSubnetsPath)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
_, ipNet, err := net.ParseCIDR(scanner.Text())
if err != nil {
return err
}
Opts.AllowedSubnets = append(Opts.AllowedSubnets, ipNet)
Opts.Logger.Info("allowed subnet", zap.String("subnet", ipNet.String()))
}
return nil
}
func initLogger() error {
logConfig := zap.NewProductionConfig()
if Opts.Verbose > 0 {
logConfig.Level.SetLevel(zap.DebugLevel)
}
l, err := logConfig.Build()
if err == nil {
Opts.Logger = l
}
return err
}
func main() {
flag.Parse()
if err := initLogger(); err != nil {
log.Fatalf("Failed to initialize logging: %s", err.Error())
}
defer Opts.Logger.Sync()
if Opts.tlsUpstreamAddr != "" {
Opts.Logger.Debug("added tls forwarder", zap.String("upstreamAddr", Opts.tlsUpstreamAddr))
Opts.ProtocolMatchers = append(Opts.ProtocolMatchers, NewTLSMatcher(Opts.tlsUpstreamAddr))
}
if Opts.httpUpstreamAddr != "" {
Opts.Logger.Debug("added http forwarder", zap.String("upstreamAddr", Opts.httpUpstreamAddr))
Opts.ProtocolMatchers = append(Opts.ProtocolMatchers, NewHTTPMatcher(Opts.httpUpstreamAddr))
}
if Opts.sshUpstreamAddr != "" {
Opts.Logger.Debug("added ssh forwarder", zap.String("upstreamAddr", Opts.sshUpstreamAddr))
Opts.ProtocolMatchers = append(Opts.ProtocolMatchers, NewSSHMatcher(Opts.sshUpstreamAddr))
}
if Opts.eternalTerminalUpstreamAddr != "" {
Opts.Logger.Debug("added eternalTerminal forwarder", zap.String("upstreamAddr", Opts.eternalTerminalUpstreamAddr))
Opts.ProtocolMatchers = append(Opts.ProtocolMatchers, NewEternalTerminalMatcher(Opts.eternalTerminalUpstreamAddr))
}
if Opts.allowedSubnetsPath != "" {
if err := loadAllowedSubnets(); err != nil {
Opts.Logger.Fatal("failed to load allowed subnets file",
zap.String("path", Opts.allowedSubnetsPath), zap.Error(err))
}
}
if Opts.Protocol != "tcp" && Opts.Protocol != "udp" && Opts.Protocol != "both" {
Opts.Logger.Fatal("--protocol has to be one of udp, tcp, both", zap.String("protocol", Opts.Protocol))
}
if len(Opts.ListenAddr) == 0 {
Opts.Logger.Fatal("-l not specifed, no listening port")
}
if Opts.Mark < 0 {
Opts.Logger.Fatal("--mark has to be >= 0", zap.Int("mark", Opts.Mark))
}
if Opts.Verbose < 0 {
Opts.Logger.Fatal("-v has to be >= 0", zap.Int("verbose", Opts.Verbose))
}
if Opts.Listeners < 1 {
Opts.Logger.Fatal("--listeners has to be >= 1")
}
if Opts.udpCloseAfter < 0 {
Opts.Logger.Fatal("--close-after has to be >= 0", zap.Int("close-after", Opts.udpCloseAfter))
}
Opts.UDPCloseAfter = time.Duration(Opts.udpCloseAfter) * time.Second
listenErrors := make(chan error, Opts.Listeners)
for i := 0; i < Opts.Listeners; i++ {
go listen(i, listenErrors)
}
for i := 0; i < Opts.Listeners; i++ {
<-listenErrors
}
}