@@ -19,6 +19,13 @@ import (
19
19
20
20
const bufferSize int = 16384
21
21
22
+ type Config struct {
23
+ Port int
24
+ Addr string
25
+ Targets []string
26
+ Connections int
27
+ }
28
+
22
29
var bufPool = sync.Pool {
23
30
New : func () interface {} {
24
31
buf := new (bytes.Buffer )
@@ -229,9 +236,9 @@ func proxy(ctx context.Context, l net.Listener, targets []string, connections in
229
236
return err
230
237
}
231
238
232
- func listenAndProxy (addr string , port int , targets [] string , connections int ) error {
239
+ func listenAndProxy (cfg Config ) error {
233
240
ctx , cancel := context .WithCancel (context .Background ())
234
- bind := fmt .Sprintf ("%s:%d" , addr , port )
241
+ bind := fmt .Sprintf ("%s:%d" , cfg . Addr , cfg . Port )
235
242
log .Printf ("Listening on %s" , bind )
236
243
237
244
l , err := net .Listen ("tcp" , bind )
@@ -247,21 +254,19 @@ func listenAndProxy(addr string, port int, targets []string, connections int) er
247
254
cancel ()
248
255
}()
249
256
250
- return proxy (ctx , l , targets , connections )
257
+ return proxy (ctx , l , cfg . Targets , cfg . Connections )
251
258
}
252
259
253
260
func main () {
254
- var port int
255
- var addr string
256
- var target string
257
- var connections int
258
- flag .StringVar (& addr , "addr" , "0.0.0.0" , "Address to listen on" )
259
- flag .IntVar (& port , "port" , 9000 , "Port to listen on" )
260
- flag .StringVar (& target , "target" , "127.0.0.1:9999" , "Address to proxy to. separate multiple with comma" )
261
- flag .IntVar (& connections , "connections" , 4 , "Number of outbound connections to make to each target" )
261
+ var cfg Config
262
+ var targets string
263
+ flag .StringVar (& cfg .Addr , "addr" , "0.0.0.0" , "Address to listen on" )
264
+ flag .IntVar (& cfg .Port , "port" , 9000 , "Port to listen on" )
265
+ flag .StringVar (& targets , "target" , "127.0.0.1:9999" , "Address to proxy to. separate multiple with comma" )
266
+ flag .IntVar (& cfg .Connections , "connections" , 4 , "Number of outbound connections to make to each target" )
262
267
flag .Parse ()
263
- targets : = strings .Split (target , "," )
264
- err := listenAndProxy (addr , port , targets , connections )
268
+ cfg . Targets = strings .Split (targets , "," )
269
+ err := listenAndProxy (cfg )
265
270
if err != nil {
266
271
log .Fatal (err )
267
272
}
0 commit comments