Skip to content

Commit

Permalink
Do not panic when connections are misbehaving, just log the errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Jun 17, 2019
1 parent 588a0a9 commit 9dfc266
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions data_source_ssh_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,29 @@ func dataSourceSSHTunnelRead(d *schema.ResourceData, meta interface{}) error {
for {
localConn, err := localListener.Accept()
if err != nil {
panic(err)
log.Printf("error accepting connection: %s", err)
continue
}

sshConn, err := sshClientConn.Dial("tcp", remoteAddress)
if err != nil {
panic(err)
log.Printf("error opening connection to %s: %s", remoteAddress, err)
continue
}

// Send traffic from the SSH server -> local program
go func() {
_, err = io.Copy(sshConn, localConn)
if err != nil {
panic(err)
log.Printf("error copying data remote -> local: %s", err)
}
}()

// Send traffic from the local program -> SSH server
go func() {
_, err = io.Copy(localConn, sshConn)
if err != nil {
panic(err)
log.Printf("error copying data local -> remote: %s", err)
}
}()
}
Expand Down
10 changes: 6 additions & 4 deletions terraform-open-ssh-tunnels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,29 @@ func main() {
for {
localConn, err := localListener.Accept()
if err != nil {
panic(err)
fmt.Printf("error accepting connection: %s", err)
continue
}

sshConn, err := sshClientConn.Dial("tcp", remoteAddress)
if err != nil {
panic(err)
fmt.Printf("error opening connection to %s: %s", remoteAddress, err)
continue
}

// Send traffic from the SSH server -> local program
go func() {
_, err = io.Copy(sshConn, localConn)
if err != nil {
panic(err)
fmt.Printf("error copying data remote -> local: %s", err)
}
}()

// Send traffic from the local program -> SSH server
go func() {
_, err = io.Copy(localConn, sshConn)
if err != nil {
panic(err)
fmt.Printf("error copying data local -> remote: %s", err)
}
}()
}
Expand Down

0 comments on commit 9dfc266

Please sign in to comment.