-
Notifications
You must be signed in to change notification settings - Fork 4
/
sshell.go
213 lines (189 loc) · 4.75 KB
/
sshell.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package sshell
import (
"crypto/subtle"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"net"
"strconv"
"strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/awgh/sshell/commands"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func init() {
commands.RegisterCommand("test", cmdTest, nil)
commands.RegisterCommand("exit", cmdExit, nil)
}
var errExitApp = errors.New("exiting")
// SSHell settings struct
type SSHell struct {
User, Password string
Port int
Running bool
Prompt string
}
// NewSSHell - create a SSHell with default settings
func NewSSHell() *SSHell {
s := new(SSHell)
return s
}
// Listen starts the server
func (s *SSHell) Listen() {
portString := strconv.Itoa(s.Port)
config := &ssh.ServerConfig{
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
if subtle.ConstantTimeCompare([]byte(c.User()), []byte(s.User)) == 1 &&
subtle.ConstantTimeCompare(pass, []byte(s.Password)) == 1 {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %q", c.User())
},
}
_, privateBytes, err := GetKeyPair("id_rsa")
if err != nil {
log.Fatal("Failed to load private key (./id_rsa)")
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
log.Fatal("Failed to parse private key")
}
config.AddHostKey(private)
listener, err := net.Listen("tcp", "0.0.0.0:"+portString)
if err != nil {
log.Fatalf("Failed to listen on %s (%s)", portString, err)
}
log.Printf("Listening on %s...\n", portString)
s.Running = true
for {
if !s.Running {
break
}
tcpConn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept incoming connection (%s)", err)
continue
}
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, config)
if err != nil {
log.Printf("Failed to handshake (%s)", err)
continue
}
log.Printf("New SSH connection from %s (%s)\n", sshConn.RemoteAddr(), sshConn.ClientVersion())
go ssh.DiscardRequests(reqs)
go s.handleChannels(chans)
}
}
func (s *SSHell) handleChannels(chans <-chan ssh.NewChannel) {
for newChannel := range chans {
go s.handleChannel(newChannel)
}
}
func (s *SSHell) handleChannel(newChannel ssh.NewChannel) {
if t := newChannel.ChannelType(); t != "session" {
newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
return
}
connection, requests, err := newChannel.Accept()
if err != nil {
log.Printf("Could not accept channel (%s)", err)
return
}
c := make(chan *ssh.Request, 2)
for req := range requests {
switch req.Type {
case "shell":
if len(req.Payload) == 0 {
req.Reply(true, nil)
go func() {
defer connection.Close()
s.serveTerminal(connection, c)
}()
}
case "subsystem":
if string(req.Payload[4:]) == "sftp" {
req.Reply(true, nil)
go func() {
defer connection.Close()
defer connection.CloseWrite()
s.serveSFTP(connection)
}()
}
case "pty-req":
c <- req // we have not created the pty yet, pass along
case "window-change":
c <- req // we have not created the pty yet, pass along
}
}
}
func (s *SSHell) serveTerminal(connection ssh.Channel, oldrequests <-chan *ssh.Request) {
term := terminal.NewTerminal(connection, s.Prompt)
term.AutoCompleteCallback = commands.AutoCompleteCallback
go func() { // OOB requests
for req := range oldrequests {
switch req.Type {
case "pty-req":
termLen := req.Payload[3]
w, h := parseDims(req.Payload[termLen+4:])
term.SetSize(int(w), int(h))
req.Reply(true, nil)
case "window-change":
w, h := parseDims(req.Payload)
term.SetSize(int(w), int(h))
}
}
}()
for {
line, err := term.ReadLine()
if err != nil {
log.Printf("channel read error (%s)", err)
break
}
f := strings.Fields(line)
if len(f) == 0 {
continue
}
cmd, args := f[0], f[1:]
if _, c, ok := commands.LookupCommand(cmd); ok {
err = c.Run(term, args)
if err == errExitApp {
term.Write([]byte("Exiting." + "\n"))
return
}
} else {
term.Write([]byte("Unknown command: " + line + "\n"))
}
}
}
func (s *SSHell) serveSFTP(channel ssh.Channel) {
serverOptions := []sftp.ServerOption{}
server, err := sftp.NewServer(
channel,
serverOptions...,
)
if err != nil {
log.Fatal(err)
}
if err := server.Serve(); err == io.EOF {
server.Close()
log.Print("sftp client exited session.")
} else if err != nil {
log.Fatal("sftp server completed with error:", err)
}
}
func cmdTest(term io.Writer, args []string) error {
msg := fmt.Sprintf("Test: %+v", args)
term.Write([]byte(msg + "\n"))
return nil
}
func cmdExit(term io.Writer, args []string) error {
return errExitApp
}
func parseDims(b []byte) (uint32, uint32) {
w := binary.BigEndian.Uint32(b)
h := binary.BigEndian.Uint32(b[4:])
return w, h
}