Skip to content

Commit 47df570

Browse files
moulprogrium
authored andcommitted
small api updates (#69)
These updates make it easier to implement and pass custom Session and Context implementations No compatibilty breaking, all tests pass
1 parent ce31f3c commit 47df570

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

agent.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ const (
2222
// client requested agent forwarding
2323
var contextKeyAgentRequest = &contextKey{"auth-agent-req"}
2424

25-
func setAgentRequested(sess *session) {
26-
sess.ctx.SetValue(contextKeyAgentRequest, true)
25+
// SetAgentRequested sets up the session context so that AgentRequested
26+
// returns true.
27+
func SetAgentRequested(ctx Context) {
28+
ctx.SetValue(contextKeyAgentRequest, true)
2729
}
2830

2931
// AgentRequested returns true if the client requested agent forwarding.

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func newContext(srv *Server) (*sshContext, context.CancelFunc) {
103103

104104
// this is separate from newContext because we will get ConnMetadata
105105
// at different points so it needs to be applied separately
106-
func (ctx *sshContext) applyConnMetadata(conn gossh.ConnMetadata) {
106+
func applyConnMetadata(ctx Context, conn gossh.ConnMetadata) {
107107
if ctx.Value(ContextKeySessionID) != nil {
108108
return
109109
}

server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Server struct {
4242
}
4343

4444
// internal for now
45-
type channelHandler func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx *sshContext)
45+
type channelHandler func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
4646

4747
func (srv *Server) ensureHostSigner() error {
4848
if len(srv.HostSigners) == 0 {
@@ -55,7 +55,7 @@ func (srv *Server) ensureHostSigner() error {
5555
return nil
5656
}
5757

58-
func (srv *Server) config(ctx *sshContext) *gossh.ServerConfig {
58+
func (srv *Server) config(ctx Context) *gossh.ServerConfig {
5959
srv.channelHandlers = map[string]channelHandler{
6060
"session": sessionHandler,
6161
"direct-tcpip": directTcpipHandler,
@@ -72,7 +72,7 @@ func (srv *Server) config(ctx *sshContext) *gossh.ServerConfig {
7272
}
7373
if srv.PasswordHandler != nil {
7474
config.PasswordCallback = func(conn gossh.ConnMetadata, password []byte) (*gossh.Permissions, error) {
75-
ctx.applyConnMetadata(conn)
75+
applyConnMetadata(ctx, conn)
7676
if ok := srv.PasswordHandler(ctx, string(password)); !ok {
7777
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
7878
}
@@ -81,7 +81,7 @@ func (srv *Server) config(ctx *sshContext) *gossh.ServerConfig {
8181
}
8282
if srv.PublicKeyHandler != nil {
8383
config.PublicKeyCallback = func(conn gossh.ConnMetadata, key gossh.PublicKey) (*gossh.Permissions, error) {
84-
ctx.applyConnMetadata(conn)
84+
applyConnMetadata(ctx, conn)
8585
if ok := srv.PublicKeyHandler(ctx, key); !ok {
8686
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
8787
}
@@ -223,7 +223,7 @@ func (srv *Server) handleConn(newConn net.Conn) {
223223
defer srv.trackConn(sshConn, false)
224224

225225
ctx.SetValue(ContextKeyConn, sshConn)
226-
ctx.applyConnMetadata(sshConn)
226+
applyConnMetadata(ctx, sshConn)
227227
go gossh.DiscardRequests(reqs)
228228
for ch := range chans {
229229
handler, found := srv.channelHandlers[ch.ChannelType()]

session.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type Session interface {
7777
// when there is no signal channel specified
7878
const maxSigBufSize = 128
7979

80-
func sessionHandler(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx *sshContext) {
80+
func sessionHandler(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context) {
8181
ch, reqs, err := newChan.Accept()
8282
if err != nil {
8383
// TODO: trigger event callback
@@ -105,7 +105,7 @@ type session struct {
105105
env []string
106106
ptyCb PtyCallback
107107
cmd []string
108-
ctx *sshContext
108+
ctx Context
109109
sigCh chan<- Signal
110110
sigBuf []Signal
111111
}
@@ -142,7 +142,7 @@ func (sess *session) Permissions() Permissions {
142142
}
143143

144144
func (sess *session) Context() context.Context {
145-
return sess.ctx.Context
145+
return sess.ctx
146146
}
147147

148148
func (sess *session) Exit(code int) error {
@@ -278,7 +278,7 @@ func (sess *session) handleRequests(reqs <-chan *gossh.Request) {
278278
req.Reply(ok, nil)
279279
case agentRequestType:
280280
// TODO: option/callback to allow agent forwarding
281-
setAgentRequested(sess)
281+
SetAgentRequested(sess.ctx)
282282
req.Reply(true, nil)
283283
default:
284284
// TODO: debug log

tcpip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type forwardData struct {
1717
OriginatorPort uint32
1818
}
1919

20-
func directTcpipHandler(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx *sshContext) {
20+
func directTcpipHandler(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context) {
2121
d := forwardData{}
2222
if err := gossh.Unmarshal(newChan.ExtraData(), &d); err != nil {
2323
newChan.Reject(gossh.ConnectionFailed, "error parsing forward data: "+err.Error())

0 commit comments

Comments
 (0)