223 lines
4.6 KiB
Go
223 lines
4.6 KiB
Go
|
|
package sshtest
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net"
|
||
|
|
"sync"
|
||
|
|
"sync/atomic"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Proxy is a TCP proxy in front of a target address that can inject latency,
|
||
|
|
// freeze traffic, or cut connections after a number of bytes.
|
||
|
|
type Proxy struct {
|
||
|
|
// Addr is host:port to dial instead of the target.
|
||
|
|
Addr string
|
||
|
|
|
||
|
|
target string
|
||
|
|
ln net.Listener
|
||
|
|
|
||
|
|
delay atomic.Int64 // one-way delay in nanoseconds, applied both ways
|
||
|
|
frozen atomic.Bool
|
||
|
|
sent atomic.Int64 // client->server bytes, all connections
|
||
|
|
cutAt atomic.Int64 // 0 = disabled; kill all when sent exceeds it
|
||
|
|
|
||
|
|
mu sync.Mutex
|
||
|
|
links map[*link]struct{}
|
||
|
|
closed bool
|
||
|
|
wg sync.WaitGroup
|
||
|
|
}
|
||
|
|
|
||
|
|
type link struct {
|
||
|
|
client, upstream net.Conn
|
||
|
|
done chan struct{}
|
||
|
|
once sync.Once
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *link) kill() {
|
||
|
|
l.once.Do(func() {
|
||
|
|
close(l.done)
|
||
|
|
l.client.Close()
|
||
|
|
l.upstream.Close()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewProxy starts a proxy to target and registers its shutdown with
|
||
|
|
// t.Cleanup.
|
||
|
|
func NewProxy(t testing.TB, target string) *Proxy {
|
||
|
|
t.Helper()
|
||
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("sshtest: proxy listen: %v", err)
|
||
|
|
}
|
||
|
|
p := &Proxy{Addr: ln.Addr().String(), target: target, ln: ln, links: map[*link]struct{}{}}
|
||
|
|
p.wg.Add(1)
|
||
|
|
go p.acceptLoop()
|
||
|
|
t.Cleanup(p.Close)
|
||
|
|
return p
|
||
|
|
}
|
||
|
|
|
||
|
|
// Port returns the TCP port of Addr.
|
||
|
|
func (p *Proxy) Port() string {
|
||
|
|
_, port, _ := net.SplitHostPort(p.Addr)
|
||
|
|
return port
|
||
|
|
}
|
||
|
|
|
||
|
|
// SetDelay sets the one-way latency applied to every chunk in both
|
||
|
|
// directions (RTT grows by about 2*d).
|
||
|
|
func (p *Proxy) SetDelay(d time.Duration) { p.delay.Store(int64(d)) }
|
||
|
|
|
||
|
|
// Freeze stops (on=true) or resumes forwarding in both directions. Data is
|
||
|
|
// buffered while frozen, connections stay open.
|
||
|
|
func (p *Proxy) Freeze(on bool) { p.frozen.Store(on) }
|
||
|
|
|
||
|
|
// CutAfter kills all current connections once n more bytes have been sent
|
||
|
|
// client->server (counted across connections). The trigger fires once;
|
||
|
|
// n <= 0 disarms it.
|
||
|
|
func (p *Proxy) CutAfter(n int64) {
|
||
|
|
if n <= 0 {
|
||
|
|
p.cutAt.Store(0)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
p.cutAt.Store(p.sent.Load() + n)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Sent reports the total client->server bytes forwarded so far.
|
||
|
|
func (p *Proxy) Sent() int64 { return p.sent.Load() }
|
||
|
|
|
||
|
|
// KillAll closes every current connection; the proxy keeps accepting new
|
||
|
|
// ones.
|
||
|
|
func (p *Proxy) KillAll() {
|
||
|
|
p.mu.Lock()
|
||
|
|
ls := make([]*link, 0, len(p.links))
|
||
|
|
for l := range p.links {
|
||
|
|
ls = append(ls, l)
|
||
|
|
}
|
||
|
|
p.mu.Unlock()
|
||
|
|
for _, l := range ls {
|
||
|
|
l.kill()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Close stops the proxy and waits for all its goroutines. Idempotent.
|
||
|
|
func (p *Proxy) Close() {
|
||
|
|
p.mu.Lock()
|
||
|
|
if p.closed {
|
||
|
|
p.mu.Unlock()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
p.closed = true
|
||
|
|
p.mu.Unlock()
|
||
|
|
p.ln.Close()
|
||
|
|
p.KillAll()
|
||
|
|
p.wg.Wait()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (p *Proxy) acceptLoop() {
|
||
|
|
defer p.wg.Done()
|
||
|
|
for {
|
||
|
|
c, err := p.ln.Accept()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
u, err := net.DialTimeout("tcp", p.target, 5*time.Second)
|
||
|
|
if err != nil {
|
||
|
|
c.Close()
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
l := &link{client: c, upstream: u, done: make(chan struct{})}
|
||
|
|
p.mu.Lock()
|
||
|
|
if p.closed {
|
||
|
|
p.mu.Unlock()
|
||
|
|
l.kill()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
p.links[l] = struct{}{}
|
||
|
|
p.mu.Unlock()
|
||
|
|
var lw sync.WaitGroup
|
||
|
|
lw.Add(2)
|
||
|
|
p.wg.Add(1)
|
||
|
|
go func() {
|
||
|
|
defer p.wg.Done()
|
||
|
|
lw.Wait()
|
||
|
|
l.kill()
|
||
|
|
p.mu.Lock()
|
||
|
|
delete(p.links, l)
|
||
|
|
p.mu.Unlock()
|
||
|
|
}()
|
||
|
|
go func() { defer lw.Done(); p.pipe(l, c, u, true) }()
|
||
|
|
go func() { defer lw.Done(); p.pipe(l, u, c, false) }()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type chunk struct {
|
||
|
|
b []byte
|
||
|
|
at time.Time
|
||
|
|
}
|
||
|
|
|
||
|
|
// pipe forwards src -> dst through a delay queue. Returning from either
|
||
|
|
// direction kills the whole link.
|
||
|
|
func (p *Proxy) pipe(l *link, src, dst net.Conn, upstream bool) {
|
||
|
|
defer l.kill()
|
||
|
|
q := make(chan chunk, 1024)
|
||
|
|
writerDone := make(chan struct{})
|
||
|
|
go func() {
|
||
|
|
defer close(writerDone)
|
||
|
|
defer l.kill()
|
||
|
|
for {
|
||
|
|
var c chunk
|
||
|
|
var ok bool
|
||
|
|
select {
|
||
|
|
case c, ok = <-q:
|
||
|
|
if !ok {
|
||
|
|
return // reader finished and queue drained
|
||
|
|
}
|
||
|
|
case <-l.done:
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if d := time.Until(c.at); d > 0 {
|
||
|
|
t := time.NewTimer(d)
|
||
|
|
select {
|
||
|
|
case <-t.C:
|
||
|
|
case <-l.done:
|
||
|
|
t.Stop()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for p.frozen.Load() {
|
||
|
|
select {
|
||
|
|
case <-time.After(5 * time.Millisecond):
|
||
|
|
case <-l.done:
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if _, err := dst.Write(c.b); err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
defer func() { close(q); <-writerDone }()
|
||
|
|
buf := make([]byte, 32*1024)
|
||
|
|
for {
|
||
|
|
n, err := src.Read(buf)
|
||
|
|
if n > 0 {
|
||
|
|
if upstream {
|
||
|
|
total := p.sent.Add(int64(n))
|
||
|
|
if lim := p.cutAt.Load(); lim > 0 && total > lim && p.cutAt.CompareAndSwap(lim, 0) {
|
||
|
|
p.KillAll()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
c := chunk{b: append([]byte(nil), buf[:n]...), at: time.Now().Add(time.Duration(p.delay.Load()))}
|
||
|
|
select {
|
||
|
|
case q <- c:
|
||
|
|
case <-l.done:
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|