-
Notifications
You must be signed in to change notification settings - Fork 6
/
session.go
74 lines (67 loc) · 1.59 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package dssh
import (
"fmt"
"io"
"code.google.com/p/go.crypto/ssh"
)
type SessionHandler interface {
HandleSSHSubsystem(arg string, stdin io.Reader, stdout, stderr io.Writer, reqs <-chan *ssh.Request) error
HandleSSHExec(arg string, stdin io.Reader, stdout, stderr io.Writer, reqs <-chan *ssh.Request) error
HandleSSHShell(arg string, stdin io.Reader, stdout, stderr io.Writer, reqs <-chan *ssh.Request) error
}
func SimpleHandler(sh SessionHandler) RawHandler {
return &simpleHandler{sh}
}
type simpleHandler struct {
SessionHandler
}
func (h *simpleHandler) AcceptSSHRaw(chType, chArg string) bool {
return chType == "session"
}
func (h *simpleHandler) HandleSSHRaw(chType, chArg string, ch ssh.Channel, reqs <-chan *ssh.Request) {
if chType != "session" {
// unsupported channel type
return
}
var (
f func(string, io.Reader, io.Writer, io.Writer, <-chan *ssh.Request) error
req *ssh.Request
)
reqLoop:
for req = range reqs {
switch req.Type {
// All 3 main session requests (shell, subsystem, exec)
case "shell":
{
f = h.HandleSSHShell
break reqLoop
}
case "subsystem":
{
f = h.HandleSSHSubsystem
break reqLoop
}
case "exec":
{
f = h.HandleSSHExec
break reqLoop
}
default:
fmt.Fprintf(ch.Stderr(), "unsupported channel request: %s\n", req.Type)
req.Reply(false, nil)
}
}
if f != nil {
var arg string
if len(req.Payload) >= 4 {
arg = string(req.Payload[4:])
}
err := f(arg, ch, ch, ch.Stderr(), reqs)
if err != nil {
fmt.Fprintf(ch, "%v\n", err)
req.Reply(false, nil)
} else {
req.Reply(true, nil)
}
}
}