-
Notifications
You must be signed in to change notification settings - Fork 308
/
tcpproxy.go
148 lines (127 loc) · 3.02 KB
/
tcpproxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (C) 2017 Michał Matczuk
// Use of this source code is governed by an AGPL-style
// license that can be found in the LICENSE file.
package tunnel
import (
"fmt"
"io"
"net"
"github.com/mmatczuk/go-http-tunnel/log"
"github.com/mmatczuk/go-http-tunnel/proto"
)
// TCPProxy forwards TCP streams.
type TCPProxy struct {
// localAddr specifies default TCP address of the local server.
localAddr string
// localAddrMap specifies mapping from ControlMessage.ForwardedHost to
// local server address, keys may contain host and port, only host or
// only port. The order of precedence is the following
// * host and port
// * port
// * host
localAddrMap map[string]string
// logger is the proxy logger.
logger log.Logger
}
// NewTCPProxy creates new direct TCPProxy, everything will be proxied to
// localAddr.
func NewTCPProxy(localAddr string, logger log.Logger) *TCPProxy {
if logger == nil {
logger = log.NewNopLogger()
}
return &TCPProxy{
localAddr: localAddr,
logger: logger,
}
}
// NewMultiTCPProxy creates a new dispatching TCPProxy, connections may go to
// different backends based on localAddrMap.
func NewMultiTCPProxy(localAddrMap map[string]string, logger log.Logger) *TCPProxy {
if logger == nil {
logger = log.NewNopLogger()
}
return &TCPProxy{
localAddrMap: localAddrMap,
logger: logger,
}
}
// Proxy is a ProxyFunc.
func (p *TCPProxy) Proxy(w io.Writer, r io.ReadCloser, msg *proto.ControlMessage) {
switch msg.ForwardedProto {
case proto.TCP, proto.TCP4, proto.TCP6, proto.UNIX, proto.SNI:
// ok
default:
p.logger.Log(
"level", 0,
"msg", "unsupported protocol",
"ctrlMsg", msg,
)
return
}
target := p.localAddrFor(msg.ForwardedHost)
if target == "" {
p.logger.Log(
"level", 1,
"msg", "no target",
"ctrlMsg", msg,
)
return
}
local, err := net.DialTimeout("tcp", target, DefaultTimeout)
if err != nil {
p.logger.Log(
"level", 0,
"msg", "dial failed",
"target", target,
"ctrlMsg", msg,
"err", err,
)
return
}
defer local.Close()
if err := keepAlive(local); err != nil {
p.logger.Log(
"level", 1,
"msg", "TCP keepalive for tunneled connection failed",
"target", target,
"ctrlMsg", msg,
"err", err,
)
}
done := make(chan struct{})
go func() {
transfer(flushWriter{w}, local, log.NewContext(p.logger).With(
"dst", msg.ForwardedHost,
"src", target,
))
close(done)
}()
transfer(local, r, log.NewContext(p.logger).With(
"dst", target,
"src", msg.ForwardedHost,
))
<-done
}
func (p *TCPProxy) localAddrFor(hostPort string) string {
if len(p.localAddrMap) == 0 {
return p.localAddr
}
// try hostPort
if addr := p.localAddrMap[hostPort]; addr != "" {
return addr
}
// try port
host, port, _ := net.SplitHostPort(hostPort)
if addr := p.localAddrMap[port]; addr != "" {
return addr
}
// try 0.0.0.0:port
if addr := p.localAddrMap[fmt.Sprintf("0.0.0.0:%s", port)]; addr != "" {
return addr
}
// try host
if addr := p.localAddrMap[host]; addr != "" {
return addr
}
return p.localAddr
}