Skip to content

Commit

Permalink
add godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneDutto committed Jan 13, 2025
1 parent c355654 commit 1ac215f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions internal/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,36 @@ func directTCPIPHandler(resolver Resolver) func(srv *ssh.Server, conn *gossh.Ser
return
}

dChan, reqs, err := client.OpenChannel("direct-tcpip", gossh.Marshal(d))
dstChan, reqs, err := client.OpenChannel("direct-tcpip", gossh.Marshal(d))
if err != nil {
rejectConnectionAndLogError(ctx, newChan, "Failed to open destination channel", err)
return
}

// gossh.Request are requests sent outside of the normal stream of data (ex. pty-req for an interactive session).
// Since we only need the raw data to redirect, we can discard them.
go gossh.DiscardRequests(reqs)

ch, reqs, err := newChan.Accept()
srcDest, reqs, err := newChan.Accept()
if err != nil {
dChan.Close()
dstChan.Close()
return
}

// gossh.Request are requests sent outside of the normal stream of data (ex. pty-req for an interactive session).
// Since we only need the raw data to redirect, we can discard them.
go gossh.DiscardRequests(reqs)

go func() {
defer ch.Close()
defer dChan.Close()
_, err := io.Copy(ch, dChan)
defer srcDest.Close()
defer dstChan.Close()
_, err := io.Copy(srcDest, dstChan)
if err != nil {
rejectConnectionAndLogError(ctx, newChan, "Failed to copy data from src to dts", err)
}
}()
go func() {
defer ch.Close()
defer dChan.Close()
_, err := io.Copy(dChan, ch)
defer srcDest.Close()
defer dstChan.Close()
_, err := io.Copy(dstChan, srcDest)
if err != nil {
rejectConnectionAndLogError(ctx, newChan, "Failed to copy data from dst to src", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *sshSuite) TestSSHJump(c *qt.C) {
SrcPort: 0,
}
s.testInDestinationServerF = func(fm ssh.ForwardMessage) {
c.Assert(fm.DestAddr, qt.Equals, "model1")
c.Check(fm.DestAddr, qt.Equals, "model1")
}
ch, _, err := client.OpenChannel("direct-tcpip", gossh.Marshal(&msg))
c.Check(err, qt.IsNil)
Expand Down Expand Up @@ -159,7 +159,7 @@ func (s *sshSuite) TestSSHFinalDestinationDialFail(c *qt.C) {
SrcPort: 0,
}
s.testInDestinationServerF = func(fm ssh.ForwardMessage) {
c.Assert(fm.DestAddr, qt.Equals, "model1")
c.Check(fm.DestAddr, qt.Equals, "model1")
}
_, _, err = client.OpenChannel("direct-tcpip", gossh.Marshal(&msg))
c.Assert(err, qt.ErrorMatches, ".*connect failed.*")
Expand Down

0 comments on commit 1ac215f

Please sign in to comment.