Skip to content

Commit d670c83

Browse files
committed
handleTCPStream: relocate from server/ to share/
server/handleTCPStream() is sufficiently general to handle TCP stream functionality for both the client and server. Therefore, move it to share/ so it can be re-used by the client when chisel learns to support reverse port forwarding, in which case TCP stream handling will be done by the client rather than the server.
1 parent 26610d7 commit d670c83

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

server/handler.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package chserver
22

33
import (
44
"io"
5-
"net"
65
"net/http"
76
"strings"
87
"sync/atomic"
98
"time"
109

11-
"github.com/jpillora/sizestr"
1210
"golang.org/x/crypto/ssh"
1311

1412
"github.com/jpillora/chisel/share"
@@ -150,7 +148,7 @@ func (s *Server) handleSSHChannels(clientLog *chshare.Logger, chans <-chan ssh.N
150148
if socks {
151149
go s.handleSocksStream(clientLog.Fork("socks#%05d", connID), stream)
152150
} else {
153-
go HandleTCPStream(clientLog.Fork(" tcp#%05d", connID), &s.connStats, stream, remote)
151+
go chshare.HandleTCPStream(clientLog.Fork(" tcp#%05d", connID), &s.connStats, stream, remote)
154152
}
155153
}
156154
}
@@ -168,17 +166,3 @@ func (s *Server) handleSocksStream(l *chshare.Logger, src io.ReadWriteCloser) {
168166
l.Debugf("%s Closed", s.connStats.Status())
169167
}
170168
}
171-
172-
func HandleTCPStream(l *chshare.Logger, connStats *shshare.ConnStats, src io.ReadWriteCloser, remote string) {
173-
dst, err := net.Dial("tcp", remote)
174-
if err != nil {
175-
l.Debugf("Remote failed (%s)", err)
176-
src.Close()
177-
return
178-
}
179-
connStats.Open()
180-
l.Debugf("%s Open", connStats.Status())
181-
sent, received := chshare.Pipe(src, dst)
182-
connStats.Close()
183-
l.Debugf("%s Close (sent %s received %s)", connStats.Status(), sizestr.ToString(sent), sizestr.ToString(received))
184-
}

share/ssh.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"encoding/pem"
1010
"fmt"
1111
"io"
12+
"net"
1213
"strings"
1314

15+
"github.com/jpillora/sizestr"
1416
"golang.org/x/crypto/ssh"
1517
)
1618

@@ -55,3 +57,17 @@ func RejectStreams(chans <-chan ssh.NewChannel) {
5557
ch.Reject(ssh.Prohibited, "Tunnels disallowed")
5658
}
5759
}
60+
61+
func HandleTCPStream(l *Logger, connStats *ConnStats, src io.ReadWriteCloser, remote string) {
62+
dst, err := net.Dial("tcp", remote)
63+
if err != nil {
64+
l.Debugf("Remote failed (%s)", err)
65+
src.Close()
66+
return
67+
}
68+
connStats.Open()
69+
l.Debugf("%s Open", connStats.Status())
70+
sent, received := Pipe(src, dst)
71+
connStats.Close()
72+
l.Debugf("%s Close (sent %s received %s)", connStats.Status(), sizestr.ToString(sent), sizestr.ToString(received))
73+
}

0 commit comments

Comments
 (0)