Skip to content

Allow to set proxy routes in bulk #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions tcpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ func (p *Proxy) AddRoute(ipPort string, dest Target) {
p.addRoute(ipPort, fixedTarget{dest})
}

func (p *Proxy) setRoutes(ipPort string, targets []Target) {
var routes []route
for _, target := range targets {
routes = append(routes, fixedTarget{target})
}
cfg := p.configFor(ipPort)
cfg.routes = routes
}

// SetRoutes replaces routes for the ipPort.
//
// It's possible that the old routes are still used once after this
// function is called.
func (p *Proxy) SetRoutes(ipPort string, targets []Target) {
p.setRoutes(ipPort, targets)
}

type fixedTarget struct {
t Target
}
Expand Down Expand Up @@ -198,7 +215,7 @@ func (p *Proxy) Start() error {
return err
}
p.lns = append(p.lns, ln)
go p.serveListener(errc, ln, config.routes)
go p.serveListener(errc, ln, config)
}
go p.awaitFirstError(errc)
return nil
Expand All @@ -209,22 +226,22 @@ func (p *Proxy) awaitFirstError(errc <-chan error) {
close(p.donec)
}

func (p *Proxy) serveListener(ret chan<- error, ln net.Listener, routes []route) {
func (p *Proxy) serveListener(ret chan<- error, ln net.Listener, cfg *config) {
for {
c, err := ln.Accept()
if err != nil {
ret <- err
return
}
go p.serveConn(c, routes)
go p.serveConn(c, cfg)
}
}

// serveConn runs in its own goroutine and matches c against routes.
// It returns whether it matched purely for testing.
func (p *Proxy) serveConn(c net.Conn, routes []route) bool {
func (p *Proxy) serveConn(c net.Conn, cfg *config) bool {
br := bufio.NewReader(c)
for _, route := range routes {
for _, route := range cfg.routes {
if target, hostName := route.match(br); target != nil {
if n := br.Buffered(); n > 0 {
peeked, _ := br.Peek(br.Buffered())
Expand Down
44 changes: 44 additions & 0 deletions tcpproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,50 @@ func TestProxyPROXYOut(t *testing.T) {
}
}

func TestSetRoutes(t *testing.T) {

var p Proxy
ipPort := ":8080"
p.AddRoute(ipPort, To("127.0.0.2:8080"))
cfg := p.configFor(ipPort)

expectedAddrsList := [][]string{
{"127.0.0.1:80"},
{"127.0.0.1:80", "127.0.0.1:443"},
{},
{"127.0.0.1:80"},
}

for _, expectedAddrs := range expectedAddrsList {
p.setRoutes(ipPort, stringsToTargets(expectedAddrs))
if !equalRoutes(cfg.routes, expectedAddrs) {
t.Fatalf("got %v; want %v", cfg.routes, expectedAddrs)
}
}
}

func stringsToTargets(s []string) []Target {
targets := make([]Target, len(s))
for i, v := range s {
targets[i] = To(v)
}

return targets
}
func equalRoutes(routes []route, expectedAddrs []string) bool {
if len(routes) != len(expectedAddrs) {
return false
}

for i, _ := range routes {
addr := routes[i].(fixedTarget).t.(*DialProxy).Addr
if addr != expectedAddrs[i] {
return false
}
}
return true
}

type tlsServer struct {
Listener net.Listener
Domain string
Expand Down