Skip to content

Commit

Permalink
Merge pull request #28 from kffl/feat/specify-network-interface
Browse files Browse the repository at this point in the history
feat: add `--host` cli flag and config param
  • Loading branch information
kffl authored Nov 20, 2022
2 parents a4e38c6 + aad4d2f commit f444e17
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down Expand Up @@ -51,14 +54,15 @@ 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 {
return nil, err
}

var cfg = lib.SpeedbumpCfg{
Host: *host,
Port: *port,
DestAddr: *destAddr,
BufferSize: int(*bufferSize),
Expand Down
2 changes: 2 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/speedbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions lib/speedbump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func startEchoSrv(port int) error {

func TestNewSpeedbump(t *testing.T) {
cfg := SpeedbumpCfg{
"localhost",
8000,
"localhost:1234",
0xffff,
Expand All @@ -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,
Expand All @@ -65,6 +82,7 @@ func TestNewSpeedbumpErrorResolvingLocal(t *testing.T) {

func TestNewSpeedbumpErrorResolvingDest(t *testing.T) {
cfg := SpeedbumpCfg{
"localhost",
8000,
"nope:1234",
0xffff,
Expand Down Expand Up @@ -93,6 +111,7 @@ func TestNewSpeedbumpDefaultQueueSize(t *testing.T) {

func TestStartListenError(t *testing.T) {
cfg := SpeedbumpCfg{
"localhost",
1, // a privileged port
"localhost:1234",
0xffff,
Expand Down Expand Up @@ -123,6 +142,7 @@ func TestSpeedbumpWithEchoServer(t *testing.T) {
go startEchoSrv(port)

cfg := SpeedbumpCfg{
"localhost",
8000,
testSrvAddr,
0xffff,
Expand Down

0 comments on commit f444e17

Please sign in to comment.