forked from jpillora/chisel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel_in_proxy.go
150 lines (138 loc) · 3.11 KB
/
tunnel_in_proxy.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
149
150
package tunnel
import (
"context"
"io"
"net"
"sync"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/settings"
"github.com/jpillora/sizestr"
"golang.org/x/crypto/ssh"
)
//sshTunnel exposes a subset of Tunnel to subtypes
type sshTunnel interface {
getSSH(ctx context.Context) ssh.Conn
}
//Proxy is the inbound portion of a Tunnel
type Proxy struct {
*cio.Logger
sshTun sshTunnel
id int
count int
remote *settings.Remote
dialer net.Dialer
tcp *net.TCPListener
udp *udpListener
mu sync.Mutex
}
//NewProxy creates a Proxy
func NewProxy(logger *cio.Logger, sshTun sshTunnel, index int, remote *settings.Remote) (*Proxy, error) {
id := index + 1
p := &Proxy{
Logger: logger.Fork("proxy#%s", remote.String()),
sshTun: sshTun,
id: id,
remote: remote,
}
return p, p.listen()
}
func (p *Proxy) listen() error {
if p.remote.Stdio {
//TODO check if pipes active?
} else if p.remote.LocalProto == "tcp" {
addr, err := net.ResolveTCPAddr("tcp", p.remote.LocalHost+":"+p.remote.LocalPort)
if err != nil {
return p.Errorf("resolve: %s", err)
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return p.Errorf("tcp: %s", err)
}
p.Infof("Listening")
p.tcp = l
} else if p.remote.LocalProto == "udp" {
l, err := listenUDP(p.Logger, p.sshTun, p.remote)
if err != nil {
return err
}
p.Infof("Listening")
p.udp = l
} else {
return p.Errorf("unknown local proto")
}
return nil
}
//Run enables the proxy and blocks while its active,
//close the proxy by cancelling the context.
func (p *Proxy) Run(ctx context.Context) error {
if p.remote.Stdio {
return p.runStdio(ctx)
} else if p.remote.LocalProto == "tcp" {
return p.runTCP(ctx)
} else if p.remote.LocalProto == "udp" {
return p.udp.run(ctx)
}
panic("should not get here")
}
func (p *Proxy) runStdio(ctx context.Context) error {
defer p.Infof("Closed")
for {
p.pipeRemote(ctx, cio.Stdio)
select {
case <-ctx.Done():
return nil
default:
// the connection is not ready yet, keep waiting
}
}
}
func (p *Proxy) runTCP(ctx context.Context) error {
done := make(chan struct{})
//implements missing net.ListenContext
go func() {
select {
case <-ctx.Done():
p.tcp.Close()
case <-done:
}
}()
for {
src, err := p.tcp.Accept()
if err != nil {
select {
case <-ctx.Done():
//listener closed
err = nil
default:
p.Infof("Accept error: %s", err)
}
close(done)
return err
}
go p.pipeRemote(ctx, src)
}
}
func (p *Proxy) pipeRemote(ctx context.Context, src io.ReadWriteCloser) {
defer src.Close()
p.mu.Lock()
p.count++
cid := p.count
p.mu.Unlock()
l := p.Fork("conn#%d", cid)
l.Debugf("Open")
sshConn := p.sshTun.getSSH(ctx)
if sshConn == nil {
l.Debugf("No remote connection")
return
}
//ssh request for tcp connection for this proxy's remote
dst, reqs, err := sshConn.OpenChannel("chisel", []byte(p.remote.Remote()))
if err != nil {
l.Infof("Stream error: %s", err)
return
}
go ssh.DiscardRequests(reqs)
//then pipe
s, r := cio.Pipe(src, dst)
l.Debugf("Close (sent %s received %s)", sizestr.ToString(s), sizestr.ToString(r))
}