2016-06-09 07:31:24 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-10-18 19:01:11 -04:00
|
|
|
"log"
|
2016-06-09 07:31:24 -04:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TCPProxy is a proxy for TCP connections. It implements the Proxy interface to
|
|
|
|
// handle TCP traffic forwarding between the frontend and backend addresses.
|
|
|
|
type TCPProxy struct {
|
|
|
|
listener *net.TCPListener
|
|
|
|
frontendAddr *net.TCPAddr
|
|
|
|
backendAddr *net.TCPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTCPProxy creates a new TCPProxy.
|
|
|
|
func NewTCPProxy(frontendAddr, backendAddr *net.TCPAddr) (*TCPProxy, error) {
|
2020-12-13 18:56:30 -05:00
|
|
|
// detect version of hostIP to bind only to correct version
|
|
|
|
ipVersion := ipv4
|
|
|
|
if frontendAddr.IP.To4() == nil {
|
|
|
|
ipVersion = ipv6
|
|
|
|
}
|
|
|
|
listener, err := net.ListenTCP("tcp"+string(ipVersion), frontendAddr)
|
2016-06-09 07:31:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If the port in frontendAddr was 0 then ListenTCP will have a picked
|
|
|
|
// a port to listen on, hence the call to Addr to get that actual port:
|
|
|
|
return &TCPProxy{
|
|
|
|
listener: listener,
|
|
|
|
frontendAddr: listener.Addr().(*net.TCPAddr),
|
|
|
|
backendAddr: backendAddr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proxy *TCPProxy) clientLoop(client *net.TCPConn, quit chan bool) {
|
|
|
|
backend, err := net.DialTCP("tcp", nil, proxy.backendAddr)
|
|
|
|
if err != nil {
|
2016-10-18 19:01:11 -04:00
|
|
|
log.Printf("Can't forward traffic to backend tcp/%v: %s\n", proxy.backendAddr, err)
|
2016-06-09 07:31:24 -04:00
|
|
|
client.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var broker = func(to, from *net.TCPConn) {
|
2017-01-11 23:46:23 -05:00
|
|
|
io.Copy(to, from)
|
2016-12-21 23:30:40 -05:00
|
|
|
from.CloseRead()
|
|
|
|
to.CloseWrite()
|
2016-06-09 07:31:24 -04:00
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(2)
|
|
|
|
go broker(client, backend)
|
|
|
|
go broker(backend, client)
|
|
|
|
|
|
|
|
finish := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(finish)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
case <-finish:
|
|
|
|
}
|
|
|
|
client.Close()
|
|
|
|
backend.Close()
|
|
|
|
<-finish
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts forwarding the traffic using TCP.
|
|
|
|
func (proxy *TCPProxy) Run() {
|
|
|
|
quit := make(chan bool)
|
|
|
|
defer close(quit)
|
|
|
|
for {
|
|
|
|
client, err := proxy.listener.Accept()
|
|
|
|
if err != nil {
|
2016-10-18 19:01:11 -04:00
|
|
|
log.Printf("Stopping proxy on tcp/%v for tcp/%v (%s)", proxy.frontendAddr, proxy.backendAddr, err)
|
2016-06-09 07:31:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
go proxy.clientLoop(client.(*net.TCPConn), quit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops forwarding the traffic.
|
|
|
|
func (proxy *TCPProxy) Close() { proxy.listener.Close() }
|
|
|
|
|
|
|
|
// FrontendAddr returns the TCP address on which the proxy is listening.
|
|
|
|
func (proxy *TCPProxy) FrontendAddr() net.Addr { return proxy.frontendAddr }
|
|
|
|
|
|
|
|
// BackendAddr returns the TCP proxied address.
|
|
|
|
func (proxy *TCPProxy) BackendAddr() net.Addr { return proxy.backendAddr }
|