From 681ac863d3f2ca1098e8703e5dd6f4199f50704f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kuffel?= Date: Sun, 20 Nov 2022 17:19:42 +0100 Subject: [PATCH 1/3] feat: add `host` option --- args.go | 4 ++++ args_test.go | 2 ++ lib/speedbump.go | 4 +++- lib/speedbump_test.go | 20 ++++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/args.go b/args.go index 7a1e52f..f174e3f 100644 --- a/args.go +++ b/args.go @@ -9,6 +9,9 @@ func parseArgs(args []string) (*lib.SpeedbumpCfg, error) { var app = kingpin.New("speedbump", "TCP proxy for simulating variable network latency.") var ( + host = app.Flag("host", "IP or hostname to listen on. Speedbump will bind to all network interfaces if unspecified."). + Default(""). + String() port = app.Flag("port", "Port number to listen on.").Default("8000").Int() bufferSize = app.Flag("buffer", "Size of the buffer used for TCP reads."). Default("64KB"). @@ -59,6 +62,7 @@ func parseArgs(args []string) (*lib.SpeedbumpCfg, error) { } var cfg = lib.SpeedbumpCfg{ + Host: *host, Port: *port, DestAddr: *destAddr, BufferSize: int(*bufferSize), diff --git a/args_test.go b/args_test.go index df23fd4..9a1faea 100644 --- a/args_test.go +++ b/args_test.go @@ -25,6 +25,7 @@ func TestParseArgsError(t *testing.T) { func TestParseArgsAll(t *testing.T) { cfg, err := parseArgs( []string{ + "--host=somehost", "--port=1234", "--buffer=200B", "--latency=100ms", @@ -39,6 +40,7 @@ func TestParseArgsAll(t *testing.T) { ) assert.Nil(t, err) assert.Equal(t, cfg.DestAddr, "host:777") + assert.Equal(t, cfg.Host, "somehost") assert.Equal(t, cfg.Port, 1234) assert.Equal(t, 200, cfg.BufferSize) assert.Equal(t, time.Millisecond*100, cfg.Latency.Base) diff --git a/lib/speedbump.go b/lib/speedbump.go index d0f7c0b..ccafa8d 100644 --- a/lib/speedbump.go +++ b/lib/speedbump.go @@ -30,6 +30,8 @@ type Speedbump struct { // SpeedbumpCfg contains Spedbump instance configuration type SpeedbumpCfg struct { + // IP or a hostname to listen on (binds to all network interfaces if unspecified) + Host string // Port specifies the local port number to listen on Port int // DestAddr specifies the proxy desination address in host:port format @@ -46,7 +48,7 @@ type SpeedbumpCfg struct { // NewSpeedbump creates a Speedbump instance based on a provided config func NewSpeedbump(cfg *SpeedbumpCfg) (*Speedbump, error) { - localTCPAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", cfg.Port)) + localTCPAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)) if err != nil { return nil, fmt.Errorf("Error resolving local address: %s", err) } diff --git a/lib/speedbump_test.go b/lib/speedbump_test.go index 10726c9..430c513 100644 --- a/lib/speedbump_test.go +++ b/lib/speedbump_test.go @@ -37,6 +37,7 @@ func startEchoSrv(port int) error { func TestNewSpeedbump(t *testing.T) { cfg := SpeedbumpCfg{ + "localhost", 8000, "localhost:1234", 0xffff, @@ -49,8 +50,24 @@ func TestNewSpeedbump(t *testing.T) { assert.Equal(t, 0xffff, s.bufferSize) } +func TestNewSpeedbumpInvalidHost(t *testing.T) { + cfg := SpeedbumpCfg{ + "nope", + 8080, + "localhost:1234", + 0xffff, + 100, + defaultLatencyCfg, + "WARN", + } + s, err := NewSpeedbump(&cfg) + assert.Nil(t, s) + assert.ErrorContains(t, err, "lookup nope") +} + func TestNewSpeedbumpErrorResolvingLocal(t *testing.T) { cfg := SpeedbumpCfg{ + "localhost", -1, "localhost:1234", 0xffff, @@ -65,6 +82,7 @@ func TestNewSpeedbumpErrorResolvingLocal(t *testing.T) { func TestNewSpeedbumpErrorResolvingDest(t *testing.T) { cfg := SpeedbumpCfg{ + "localhost", 8000, "nope:1234", 0xffff, @@ -93,6 +111,7 @@ func TestNewSpeedbumpDefaultQueueSize(t *testing.T) { func TestStartListenError(t *testing.T) { cfg := SpeedbumpCfg{ + "localhost", 1, // a privileged port "localhost:1234", 0xffff, @@ -123,6 +142,7 @@ func TestSpeedbumpWithEchoServer(t *testing.T) { go startEchoSrv(port) cfg := SpeedbumpCfg{ + "localhost", 8000, testSrvAddr, 0xffff, From dcbcc5fc8f228c7459999a2fe7f4addeea428b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kuffel?= Date: Sun, 20 Nov 2022 17:20:05 +0100 Subject: [PATCH 2/3] docs: add `host` flag to `--help` output --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 43092df..23b6111 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ TCP proxy for simulating variable network latency. Flags: --help Show context-sensitive help (also try --help-long and --help-man). + --host="" IP or hostname to listen on. Speedbump will bind to + all available network interfaces if unspecified. --port=8000 Port number to listen on. --buffer=64KB Size of the buffer used for TCP reads. --queue-size=1024 Size of the delay queue storing read buffers. From aad4d2fcd559f9e1ef5f43ad2b3871466673e279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kuffel?= Date: Sun, 20 Nov 2022 17:21:17 +0100 Subject: [PATCH 3/3] chore: bump version to 1.1.0 --- args.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/args.go b/args.go index f174e3f..5d656f7 100644 --- a/args.go +++ b/args.go @@ -54,7 +54,7 @@ func parseArgs(args []string) (*lib.SpeedbumpCfg, error) { String() ) - app.Version("1.0.0") + app.Version("1.1.0") _, err := app.Parse(args) if err != nil {