Skip to content

Commit 553668e

Browse files
author
Justin Azoff
committed
refactor the config into a struct
1 parent c2daaae commit 553668e

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

main.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ import (
1919

2020
const bufferSize int = 16384
2121

22+
type Config struct {
23+
Port int
24+
Addr string
25+
Targets []string
26+
Connections int
27+
}
28+
2229
var bufPool = sync.Pool{
2330
New: func() interface{} {
2431
buf := new(bytes.Buffer)
@@ -229,9 +236,9 @@ func proxy(ctx context.Context, l net.Listener, targets []string, connections in
229236
return err
230237
}
231238

232-
func listenAndProxy(addr string, port int, targets []string, connections int) error {
239+
func listenAndProxy(cfg Config) error {
233240
ctx, cancel := context.WithCancel(context.Background())
234-
bind := fmt.Sprintf("%s:%d", addr, port)
241+
bind := fmt.Sprintf("%s:%d", cfg.Addr, cfg.Port)
235242
log.Printf("Listening on %s", bind)
236243

237244
l, err := net.Listen("tcp", bind)
@@ -247,21 +254,19 @@ func listenAndProxy(addr string, port int, targets []string, connections int) er
247254
cancel()
248255
}()
249256

250-
return proxy(ctx, l, targets, connections)
257+
return proxy(ctx, l, cfg.Targets, cfg.Connections)
251258
}
252259

253260
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")
262267
flag.Parse()
263-
targets := strings.Split(target, ",")
264-
err := listenAndProxy(addr, port, targets, connections)
268+
cfg.Targets = strings.Split(targets, ",")
269+
err := listenAndProxy(cfg)
265270
if err != nil {
266271
log.Fatal(err)
267272
}

0 commit comments

Comments
 (0)