@@ -37,6 +37,7 @@ type forwardedUnixHandler struct {
37
37
}
38
38
39
39
func (h * forwardedUnixHandler ) HandleSSHRequest (ctx ssh.Context , _ * ssh.Server , req * gossh.Request ) (bool , []byte ) {
40
+ h .log .Debug (ctx , "handling SSH unix forward" )
40
41
h .Lock ()
41
42
if h .forwards == nil {
42
43
h .forwards = make (map [string ]net.Listener )
@@ -47,22 +48,25 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
47
48
h .log .Warn (ctx , "SSH unix forward request from client with no gossh connection" )
48
49
return false , nil
49
50
}
51
+ log := h .log .With (slog .F ("remote_addr" , conn .RemoteAddr ()))
50
52
51
53
switch req .Type {
52
54
case "streamlocal-forward@openssh.com" :
53
55
var reqPayload streamLocalForwardPayload
54
56
err := gossh .Unmarshal (req .Payload , & reqPayload )
55
57
if err != nil {
56
- h .log .Warn (ctx , "parse streamlocal-forward@openssh.com request payload from client" , slog .Error (err ))
58
+ h .log .Warn (ctx , "parse streamlocal-forward@openssh.com request (SSH unix forward) payload from client" , slog .Error (err ))
57
59
return false , nil
58
60
}
59
61
60
62
addr := reqPayload .SocketPath
63
+ log = log .With (slog .F ("socket_path" , addr ))
64
+ log .Debug (ctx , "request begin SSH unix forward" )
61
65
h .Lock ()
62
66
_ , ok := h .forwards [addr ]
63
67
h .Unlock ()
64
68
if ok {
65
- h . log .Warn (ctx , "SSH unix forward request for socket path that is already being forwarded (maybe to another client?)" ,
69
+ log .Warn (ctx , "SSH unix forward request for socket path that is already being forwarded (maybe to another client?)" ,
66
70
slog .F ("socket_path" , addr ),
67
71
)
68
72
return false , nil
@@ -72,22 +76,22 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
72
76
parentDir := filepath .Dir (addr )
73
77
err = os .MkdirAll (parentDir , 0o700 )
74
78
if err != nil {
75
- h . log .Warn (ctx , "create parent dir for SSH unix forward request" ,
79
+ log .Warn (ctx , "create parent dir for SSH unix forward request" ,
76
80
slog .F ("parent_dir" , parentDir ),
77
- slog .F ("socket_path" , addr ),
78
81
slog .Error (err ),
79
82
)
80
83
return false , nil
81
84
}
82
85
83
86
ln , err := net .Listen ("unix" , addr )
84
87
if err != nil {
85
- h . log .Warn (ctx , "listen on Unix socket for SSH unix forward request" ,
88
+ log .Warn (ctx , "listen on Unix socket for SSH unix forward request" ,
86
89
slog .F ("socket_path" , addr ),
87
90
slog .Error (err ),
88
91
)
89
92
return false , nil
90
93
}
94
+ log .Debug (ctx , "SSH unix forward listening on socket" )
91
95
92
96
// The listener needs to successfully start before it can be added to
93
97
// the map, so we don't have to worry about checking for an existing
@@ -97,6 +101,7 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
97
101
h .Lock ()
98
102
h .forwards [addr ] = ln
99
103
h .Unlock ()
104
+ log .Debug (ctx , "SSH unix forward added to cache" )
100
105
101
106
ctx , cancel := context .WithCancel (ctx )
102
107
go func () {
@@ -110,22 +115,23 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
110
115
c , err := ln .Accept ()
111
116
if err != nil {
112
117
if ! xerrors .Is (err , net .ErrClosed ) {
113
- h .log .Warn (ctx , "accept on local Unix socket for SSH unix forward request" ,
114
- slog .F ("socket_path" , addr ),
118
+ log .Warn (ctx , "accept on local Unix socket for SSH unix forward request" ,
115
119
slog .Error (err ),
116
120
)
117
121
}
118
122
// closed below
123
+ log .Debug (ctx , "SSH unix forward listener closed" )
119
124
break
120
125
}
126
+ log .Debug (ctx , "accepted SSH unix forward connection" )
121
127
payload := gossh .Marshal (& forwardedStreamLocalPayload {
122
128
SocketPath : addr ,
123
129
})
124
130
125
131
go func () {
126
132
ch , reqs , err := conn .OpenChannel ("forwarded-streamlocal@openssh.com" , payload )
127
133
if err != nil {
128
- h .log .Warn (ctx , "open SSH channel to forward Unix connection to client" ,
134
+ h .log .Warn (ctx , "open SSH unix forward channel to client" ,
129
135
slog .F ("socket_path" , addr ),
130
136
slog .Error (err ),
131
137
)
@@ -143,6 +149,7 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
143
149
delete (h .forwards , addr )
144
150
}
145
151
h .Unlock ()
152
+ log .Debug (ctx , "SSH unix forward listener removed from cache" , slog .F ("path" , addr ))
146
153
_ = ln .Close ()
147
154
}()
148
155
@@ -152,9 +159,10 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
152
159
var reqPayload streamLocalForwardPayload
153
160
err := gossh .Unmarshal (req .Payload , & reqPayload )
154
161
if err != nil {
155
- h .log .Warn (ctx , "parse cancel-streamlocal-forward@openssh.com request payload from client" , slog .Error (err ))
162
+ h .log .Warn (ctx , "parse cancel-streamlocal-forward@openssh.com (SSH unix forward) request payload from client" , slog .Error (err ))
156
163
return false , nil
157
164
}
165
+ log .Debug (ctx , "request to cancel SSH unix forward" , slog .F ("path" , reqPayload .SocketPath ))
158
166
h .Lock ()
159
167
ln , ok := h .forwards [reqPayload .SocketPath ]
160
168
h .Unlock ()
0 commit comments