-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* moved tcp dynamic * setting .gitignore and Makefile back to master * added features md for tcp-dynamic-proxy * udpated features doco * updated log message for cert source * added split check for route * added : check for tcp-dynamic * fixed dial timeout error
- Loading branch information
1 parent
08b493a
commit 3584cbb
Showing
10 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: "TCP Dynamic Proxy" | ||
--- | ||
|
||
The TCP dynamic proxy is similar to the TCP Proxy, but the listener is started from the Consul urlprefix tag. | ||
Also, the service is defined with IP and port, so that multiple services can be defined on the load balancer using | ||
the same TCP port. Connections are forwarded to services based on the combination of ip:port | ||
|
||
To use TCP Dynamic proxy support the service needs to advertise `urlprefix-127.0.0.1:1234 proto=tcp` in | ||
Consul. In addition, fabio needs to be configured with a placeholder for the proxy.addr.: | ||
|
||
``` | ||
fabio -proxy.addr '0.0.0.0:0;proto=tcp-dynamic;refresh=5s' | ||
``` | ||
|
||
The TCP listener is started for the given TCP ports. To use IP addressing to separate the services, matching IP | ||
addressed would need to be added to the loopback interface on the host. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package tcp | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
"time" | ||
|
||
"github.com/fabiolb/fabio/metrics" | ||
"github.com/fabiolb/fabio/route" | ||
) | ||
|
||
// Proxy implements a generic TCP proxying handler. | ||
type DynamicProxy struct { | ||
// DialTimeout sets the timeout for establishing the outbound | ||
// connection. | ||
DialTimeout time.Duration | ||
|
||
// Lookup returns a target host for the given request. | ||
// The proxy will panic if this value is nil. | ||
Lookup func(host string) *route.Target | ||
|
||
// Conn counts the number of connections. | ||
Conn metrics.Counter | ||
|
||
// ConnFail counts the failed upstream connection attempts. | ||
ConnFail metrics.Counter | ||
|
||
// Noroute counts the failed Lookup() calls. | ||
Noroute metrics.Counter | ||
} | ||
|
||
func (p *DynamicProxy) ServeTCP(in net.Conn) error { | ||
defer in.Close() | ||
|
||
if p.Conn != nil { | ||
p.Conn.Inc(1) | ||
} | ||
target := in.LocalAddr().String() | ||
t := p.Lookup(target) | ||
if t == nil { | ||
if p.Noroute != nil { | ||
p.Noroute.Inc(1) | ||
} | ||
return nil | ||
} | ||
addr := t.URL.Host | ||
log.Printf("[DEBUG] Connection: %s incoming %s to %s: ", in.RemoteAddr(), target, addr) | ||
|
||
if t.AccessDeniedTCP(in) { | ||
return nil | ||
} | ||
|
||
out, err := net.DialTimeout("tcp", addr, p.DialTimeout) | ||
if err != nil { | ||
log.Print("[WARN] tcp: cannot connect to upstream ", addr) | ||
if p.ConnFail != nil { | ||
p.ConnFail.Inc(1) | ||
} | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
errc := make(chan error, 2) | ||
cp := func(dst io.Writer, src io.Reader, c metrics.Counter) { | ||
errc <- copyBuffer(dst, src, c) | ||
} | ||
|
||
// rx measures the traffic to the upstream server (in <- out) | ||
// tx measures the traffic from the upstream server (out <- in) | ||
rx := metrics.DefaultRegistry.GetCounter(t.TimerName + ".rx") | ||
tx := metrics.DefaultRegistry.GetCounter(t.TimerName + ".tx") | ||
|
||
go cp(in, out, rx) | ||
go cp(out, in, tx) | ||
err = <-errc | ||
if err != nil && err != io.EOF { | ||
log.Print("[WARN]: tcp: ", err) | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters