badnet

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

badnet

badnet is a TCP proxy for tests. Release notes are on GitHub Releases. It sits between your application and the service it talks to so you can simulate latency, bandwidth limits, and connection failures without changing application code.

  1. Start a proxy with badnet.ForTest
  2. Point the application at proxy.BindAddr()
  3. Run the tests you already have

Zero-config is a transparent TCP forwarder. Impairments are optional and applied from the connecting client's point of view.

proxy := badnet.ForTest(t, badnet.Config{
    Listen: "127.0.0.1:0",
    Target: "127.0.0.1:8080", // host:port, http://..., https://..., or [IPv6]
    Read: badnet.Direction{ // data the client receives
        Latency:      50 * time.Millisecond,
        MaxKBps:      32,
        FailureRatio: 10, // percent of connections
    },
    Write: badnet.Direction{ // data the client sends
        FailureRatio: 5,
    },
})

// App dials the proxy instead of the real target.
addr := proxy.BindAddr()

Config

Field Meaning
Listen Local address to accept on. 127.0.0.1:0 picks a free port.
Target Upstream host:port. http:// defaults to 80, https:// / wss:// to 443. IPv6 works with or without brackets.
Read Impairments on bytes the client reads (server → client).
Write Impairments on bytes the client writes (client → server).
DialTimeout Timeout when the proxy dials Target. Default 5s.

Each Direction:

Field Meaning
Latency Delay applied once, on the first I/O in that direction.
MaxKBps Throughput cap in kilobytes/second. 0 is unlimited.
FailureRatio 0–100. Chance this connection fails in that direction (partial I/O, then an error). Not per Read/Write call.

ForTest shuts the proxy down in t.Cleanup. Target dial failures are counted in stats; they do not fail the caller’s test.

Stats

proxy.Port()          // listening port, or -1
proxy.BindAddr()      // host:port to dial
proxy.FailureRatio()  // failed connections / accepted connections

Notes

  • This is a TCP proxy. HTTP clients must set Host themselves if the upstream is name-based.
  • Both directions are copied to completion. A client CloseWrite is forwarded so protocols that half-close still get a response.
  • See examples/ for HTTP, latency, and concurrent TCP.

Related

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Listen      string // local address, e.g. "127.0.0.1:0"
	Target      string // upstream host:port or URL
	Read        Direction
	Write       Direction
	DialTimeout time.Duration // 0 uses a 5s default
}

Config describes a test proxy. Read and Write are from the connecting client's point of view: Read impairs data the client receives, Write impairs data the client sends.

type Direction

type Direction struct {
	MaxKBps      int
	Latency      time.Duration
	FailureRatio int
}

Direction impairs one direction of traffic, from the connecting client's point of view. FailureRatio is the percent (0-100) of connections that fail in this direction. Latency is applied once, on the first I/O. MaxKBps of 0 means unlimited.

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

func ForTest

func ForTest(t *testing.T, conf Config) *Proxy

ForTest starts a proxy that shuts down when the test ends. Listen errors fail the test; later target dial errors do not.

func (*Proxy) BindAddr

func (p *Proxy) BindAddr() string

BindAddr is the host:port the application should dial.

func (*Proxy) FailureRatio added in v0.3.0

func (p *Proxy) FailureRatio() float64

FailureRatio is the fraction of accepted connections that had a dial failure or an injected/copy failure.

func (*Proxy) Port added in v0.1.1

func (p *Proxy) Port() int

Port is the listening port, or -1 if BindAddr cannot be parsed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL