Skip to content

Commit

Permalink
fix: upstream context implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 8, 2024
1 parent e11ae27 commit 7e1d867
Showing 1 changed file with 6 additions and 37 deletions.
43 changes: 6 additions & 37 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"net"
"sync"
"time"

gossh "golang.org/x/crypto/ssh"
)
Expand Down Expand Up @@ -93,8 +92,8 @@ type Context interface {
}

type sshContext struct {
ctx context.Context
mtx *sync.RWMutex
context.Context
*sync.Mutex

values map[interface{}]interface{}
valuesMu sync.Mutex
Expand All @@ -107,9 +106,9 @@ var _ sync.Locker = &sshContext{}
func newContext(srv *Server) (*sshContext, context.CancelFunc) {
innerCtx, cancel := context.WithCancel(context.Background())
ctx := &sshContext{
ctx: innerCtx,
mtx: &sync.RWMutex{},
values: make(map[interface{}]interface{}),
Context: innerCtx,
Mutex: &sync.Mutex{},
values: make(map[interface{}]interface{}),
}
ctx.SetValue(ContextKeyServer, srv)
perms := &Permissions{&gossh.Permissions{}}
Expand Down Expand Up @@ -137,7 +136,7 @@ func (ctx *sshContext) Value(key interface{}) interface{} {
if v, ok := ctx.values[key]; ok {
return v
}
return ctx.ctx.Value(key)
return ctx.Context.Value(key)
}

func (ctx *sshContext) SetValue(key, value interface{}) {
Expand All @@ -146,36 +145,6 @@ func (ctx *sshContext) SetValue(key, value interface{}) {
ctx.values[key] = value
}

func (ctx *sshContext) Done() <-chan struct{} {
ctx.mtx.RLock()
defer ctx.mtx.RUnlock()
return ctx.ctx.Done()
}

// Deadline implements context.Context.
func (ctx *sshContext) Deadline() (deadline time.Time, ok bool) {
ctx.mtx.RLock()
defer ctx.mtx.RUnlock()
return ctx.ctx.Deadline()
}

// Err implements context.Context.
func (ctx *sshContext) Err() error {
ctx.mtx.RLock()
defer ctx.mtx.RUnlock()
return ctx.ctx.Err()
}

// Lock implements sync.Locker.
func (ctx *sshContext) Lock() {
ctx.mtx.Lock()
}

// Unlock implements sync.Locker.
func (ctx *sshContext) Unlock() {
ctx.mtx.Unlock()
}

func (ctx *sshContext) User() string {
return ctx.Value(ContextKeyUser).(string)
}
Expand Down

0 comments on commit 7e1d867

Please sign in to comment.