@@ -2,18 +2,36 @@ package sshutils
22
33import (
44 "encoding/hex"
5- "errors"
65 "fmt"
76 "net"
7+ "strconv"
88
99 "golang.org/x/crypto/ssh"
1010)
1111
12- var (
13- ErrEstablishSSH = errors .New ("failed to establish SSH connection" )
14- ErrSendRequest = errors .New ("failed to send request" )
15- ErrChannelOpen = errors .New ("failed to open channel" )
16- )
12+ type EstablishError struct {
13+ err error
14+ }
15+
16+ func (e EstablishError ) Error () string {
17+ return fmt .Sprintf ("failed to establish SSH connection: %v" , e .err )
18+ }
19+
20+ type SendRequestError struct {
21+ err error
22+ }
23+
24+ func (e SendRequestError ) Error () string {
25+ return fmt .Sprintf ("failed to send request: %v" , e .err )
26+ }
27+
28+ type ChannelOpenError struct {
29+ err error
30+ }
31+
32+ func (e ChannelOpenError ) Error () string {
33+ return fmt .Sprintf ("failed to open channel: %v" , e .err )
34+ }
1735
1836type Listener struct {
1937 net.Listener
@@ -28,7 +46,7 @@ func (listener *Listener) Accept() (*Conn, error) {
2846 sshConn , sshNewChannels , sshRequests , err := ssh .NewServerConn (conn , & listener .config )
2947 if err != nil {
3048 conn .Close ()
31- return nil , fmt . Errorf ( "%w: %v" , ErrEstablishSSH , err )
49+ return nil , EstablishError { err }
3250 }
3351 return handleConn (sshConn , sshNewChannels , sshRequests ), nil
3452}
@@ -51,7 +69,7 @@ type Conn struct {
5169func (conn * Conn ) RawChannel (name string , payload []byte ) (* Channel , error ) {
5270 sshChannel , sshRequests , err := conn .Conn .OpenChannel (name , payload )
5371 if err != nil {
54- return nil , fmt . Errorf ( "%w: %v" , ErrChannelOpen , err )
72+ return nil , ChannelOpenError { err }
5573 }
5674 return handleChannel (sshChannel , sshRequests , conn , name ), nil
5775}
@@ -67,7 +85,7 @@ func (conn *Conn) Channel(name string, payload Payload) (*Channel, error) {
6785func (conn * Conn ) RawRequest (name string , wantReply bool , payload []byte ) (bool , []byte , error ) {
6886 accepted , reply , err := conn .SendRequest (name , wantReply , payload )
6987 if err != nil {
70- return false , nil , fmt . Errorf ( "%w: %v" , ErrSendRequest , err )
88+ return false , nil , SendRequestError { err }
7189 }
7290 return accepted , reply , nil
7391}
@@ -92,7 +110,7 @@ func Dial(address string, config *ssh.ClientConfig) (*Conn, error) {
92110 sshConn , sshNewChannels , sshRequests , err := ssh .NewClientConn (conn , address , config )
93111 if err != nil {
94112 conn .Close ()
95- return nil , fmt . Errorf ( "%w: %v" , ErrEstablishSSH , err )
113+ return nil , EstablishError { err }
96114 }
97115 return handleConn (sshConn , sshNewChannels , sshRequests ), nil
98116}
@@ -137,7 +155,7 @@ type NewChannel struct {
137155func (newChannel * NewChannel ) AcceptChannel () (* Channel , error ) {
138156 sshChannel , sshRequests , err := newChannel .Accept ()
139157 if err != nil {
140- return nil , fmt . Errorf ( "%w: %v" , ErrChannelOpen , err )
158+ return nil , ChannelOpenError { err }
141159 }
142160 return handleChannel (sshChannel , sshRequests , newChannel .conn , newChannel .ChannelType ()), nil
143161}
@@ -177,7 +195,7 @@ func (channel *Channel) ConnMetadata() ssh.ConnMetadata {
177195func (channel * Channel ) RawRequest (name string , wantReply bool , payload []byte ) (bool , error ) {
178196 accepted , err := channel .SendRequest (name , wantReply , payload )
179197 if err != nil {
180- return false , fmt . Errorf ( "%w: %v" , ErrSendRequest , err )
198+ return false , SendRequestError { err }
181199 }
182200 return accepted , nil
183201}
@@ -196,7 +214,7 @@ func (channel *Channel) String() string {
196214
197215func handleChannel (sshChannel ssh.Channel , sshRequests <- chan * ssh.Request , conn * Conn , name string ) * Channel {
198216 requests := make (chan * ChannelRequest )
199- channel := & Channel {sshChannel , requests , fmt . Sprint ( conn .nextChannelID ), name , conn }
217+ channel := & Channel {sshChannel , requests , strconv . FormatInt ( int64 ( conn .nextChannelID ), 10 ), name , conn }
200218 go func () {
201219 for request := range sshRequests {
202220 requests <- & ChannelRequest {request , channel }
0 commit comments