55 "encoding/hex"
66 "net"
77 "sync"
8+ "time"
89
910 gossh "golang.org/x/crypto/ssh"
1011)
@@ -92,10 +93,14 @@ type Context interface {
9293}
9394
9495type sshContext struct {
95- context.Context
96- * sync.RWMutex
96+ ctx context.Context
97+ mtx * sync.RWMutex
9798}
9899
100+ var _ context.Context = & sshContext {}
101+
102+ var _ sync.Locker = & sshContext {}
103+
99104func newContext (srv * Server ) (* sshContext , context.CancelFunc ) {
100105 innerCtx , cancel := context .WithCancel (context .Background ())
101106 ctx := & sshContext {innerCtx , & sync.RWMutex {}}
@@ -120,21 +125,45 @@ func applyConnMetadata(ctx Context, conn gossh.ConnMetadata) {
120125}
121126
122127func (ctx * sshContext ) SetValue (key , value interface {}) {
123- ctx .RWMutex .Lock ()
124- defer ctx .RWMutex .Unlock ()
125- ctx .Context = context .WithValue (ctx .Context , key , value )
128+ ctx .mtx .Lock ()
129+ defer ctx .mtx .Unlock ()
130+ ctx .ctx = context .WithValue (ctx .ctx , key , value )
126131}
127132
128133func (ctx * sshContext ) Value (key interface {}) interface {} {
129- ctx .RWMutex .RLock ()
130- defer ctx .RWMutex .RUnlock ()
131- return ctx .Context .Value (key )
134+ ctx .mtx .RLock ()
135+ defer ctx .mtx .RUnlock ()
136+ return ctx .ctx .Value (key )
132137}
133138
134139func (ctx * sshContext ) Done () <- chan struct {} {
135- ctx .RWMutex .RLock ()
136- defer ctx .RWMutex .RUnlock ()
137- return ctx .Context .Done ()
140+ ctx .mtx .RLock ()
141+ defer ctx .mtx .RUnlock ()
142+ return ctx .ctx .Done ()
143+ }
144+
145+ // Deadline implements context.Context.
146+ func (ctx * sshContext ) Deadline () (deadline time.Time , ok bool ) {
147+ ctx .mtx .RLock ()
148+ defer ctx .mtx .RUnlock ()
149+ return ctx .ctx .Deadline ()
150+ }
151+
152+ // Err implements context.Context.
153+ func (ctx * sshContext ) Err () error {
154+ ctx .mtx .RLock ()
155+ defer ctx .mtx .RUnlock ()
156+ return ctx .ctx .Err ()
157+ }
158+
159+ // Lock implements sync.Locker.
160+ func (ctx * sshContext ) Lock () {
161+ ctx .mtx .Lock ()
162+ }
163+
164+ // Unlock implements sync.Locker.
165+ func (ctx * sshContext ) Unlock () {
166+ ctx .mtx .Unlock ()
138167}
139168
140169func (ctx * sshContext ) User () string {
0 commit comments