@@ -2,19 +2,36 @@ package sshutils
22
33import (
44 "encoding/hex"
5- "errors"
65 "fmt"
76 "net"
87 "strconv"
98
109 "golang.org/x/crypto/ssh"
1110)
1211
13- var (
14- ErrEstablishSSH = errors .New ("failed to establish SSH connection" )
15- ErrSendRequest = errors .New ("failed to send request" )
16- ErrChannelOpen = errors .New ("failed to open channel" )
17- )
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+ }
1835
1936type Listener struct {
2037 net.Listener
@@ -29,7 +46,7 @@ func (listener *Listener) Accept() (*Conn, error) {
2946 sshConn , sshNewChannels , sshRequests , err := ssh .NewServerConn (conn , & listener .config )
3047 if err != nil {
3148 conn .Close ()
32- return nil , fmt . Errorf ( "%w: %v" , ErrEstablishSSH , err )
49+ return nil , EstablishError { err }
3350 }
3451 return handleConn (sshConn , sshNewChannels , sshRequests ), nil
3552}
@@ -52,7 +69,7 @@ type Conn struct {
5269func (conn * Conn ) RawChannel (name string , payload []byte ) (* Channel , error ) {
5370 sshChannel , sshRequests , err := conn .Conn .OpenChannel (name , payload )
5471 if err != nil {
55- return nil , fmt . Errorf ( "%w: %v" , ErrChannelOpen , err )
72+ return nil , ChannelOpenError { err }
5673 }
5774 return handleChannel (sshChannel , sshRequests , conn , name ), nil
5875}
@@ -68,7 +85,7 @@ func (conn *Conn) Channel(name string, payload Payload) (*Channel, error) {
6885func (conn * Conn ) RawRequest (name string , wantReply bool , payload []byte ) (bool , []byte , error ) {
6986 accepted , reply , err := conn .SendRequest (name , wantReply , payload )
7087 if err != nil {
71- return false , nil , fmt . Errorf ( "%w: %v" , ErrSendRequest , err )
88+ return false , nil , SendRequestError { err }
7289 }
7390 return accepted , reply , nil
7491}
@@ -93,7 +110,7 @@ func Dial(address string, config *ssh.ClientConfig) (*Conn, error) {
93110 sshConn , sshNewChannels , sshRequests , err := ssh .NewClientConn (conn , address , config )
94111 if err != nil {
95112 conn .Close ()
96- return nil , fmt . Errorf ( "%w: %v" , ErrEstablishSSH , err )
113+ return nil , EstablishError { err }
97114 }
98115 return handleConn (sshConn , sshNewChannels , sshRequests ), nil
99116}
@@ -138,7 +155,7 @@ type NewChannel struct {
138155func (newChannel * NewChannel ) AcceptChannel () (* Channel , error ) {
139156 sshChannel , sshRequests , err := newChannel .Accept ()
140157 if err != nil {
141- return nil , fmt . Errorf ( "%w: %v" , ErrChannelOpen , err )
158+ return nil , ChannelOpenError { err }
142159 }
143160 return handleChannel (sshChannel , sshRequests , newChannel .conn , newChannel .ChannelType ()), nil
144161}
@@ -178,7 +195,7 @@ func (channel *Channel) ConnMetadata() ssh.ConnMetadata {
178195func (channel * Channel ) RawRequest (name string , wantReply bool , payload []byte ) (bool , error ) {
179196 accepted , err := channel .SendRequest (name , wantReply , payload )
180197 if err != nil {
181- return false , fmt . Errorf ( "%w: %v" , ErrSendRequest , err )
198+ return false , SendRequestError { err }
182199 }
183200 return accepted , nil
184201}
0 commit comments