-
Notifications
You must be signed in to change notification settings - Fork 136
/
ssh_session.go
89 lines (71 loc) · 2.41 KB
/
ssh_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// reverseSSH - a lightweight ssh server with a reverse connection feature
// Copyright (C) 2021 Ferdinor <[email protected]>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"io"
"log"
"os/exec"
"github.com/gliderlabs/ssh"
)
func createSSHSessionHandler(shell string) ssh.Handler {
return func(s ssh.Session) {
log.Printf("New login from %s@%s", s.User(), s.RemoteAddr().String())
_, _, isPty := s.Pty()
switch {
case isPty:
log.Println("PTY requested")
createPty(s, shell)
case len(s.Command()) > 0:
log.Printf("Command execution requested: '%s'", s.RawCommand())
cmd := exec.CommandContext(s.Context(), s.Command()[0], s.Command()[1:]...)
// We use StdinPipe to avoid blocking on missing input
if stdin, err := cmd.StdinPipe(); err != nil {
log.Println("Could not initialize stdinPipe", err)
s.Exit(255)
return
} else {
go func() {
if _, err := io.Copy(stdin, s); err != nil {
log.Printf("Error while copying input from %s to stdin: %s", s.RemoteAddr().String(), err)
}
s.Close()
}()
}
cmd.Stdout = s
cmd.Stderr = s
done := make(chan error, 1)
go func() { done <- cmd.Run() }()
select {
case err := <-done:
if err != nil {
log.Println("Command execution failed:", err)
io.WriteString(s, "Command execution failed: "+err.Error()+"\n")
s.Exit(255)
return
}
log.Println("Command execution successful")
s.Exit(cmd.ProcessState.ExitCode())
return
case <-s.Context().Done():
log.Printf("Session terminated: %s", s.Context().Err())
return
}
default:
log.Println("No PTY requested, no command supplied")
// Keep this open until the session exits, could e.g. be port forwarding
<-s.Context().Done()
log.Printf("Session terminated: %s", s.Context().Err())
return
}
}
}