-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.ts
160 lines (136 loc) · 3.89 KB
/
shell.ts
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
import { Socket } from "socket.io";
import ssh2 from "ssh2";
import { Logger } from "./logger";
interface ShellConfig {
host: string;
port: number;
username: string;
password?: string;
privateKey?: string;
passphrase?: string;
}
interface TermConfig {
rows: number;
cols: number;
height: number;
width: number;
}
enum ErrorType {
SSH_CONNECTION_ERROR = "SSH_CONNECTION_ERROR",
SSH_AUTHENTICATION_ERROR = "SSH_AUTHENTICATION_ERROR",
SHELL_ERROR = "SHELL_ERROR",
SOCKET_ERROR = "SOCKET_ERROR",
}
type ShellRequest = ShellConfig & TermConfig;
const shell = (socket: Socket, log: Logger) => {
const session = new ssh2.Client();
let shellConfig: ShellConfig;
let termConfig: TermConfig;
socket.on("shell", (req: ShellRequest) => {
log.info("shell requested for %s:%d", req.host, req.port);
if (req.username && (req.password || req.privateKey)) {
shellConfig = req;
termConfig = req;
session.connect({
host: req.host,
port: req.port,
username: req.username,
password: req.password,
privateKey: req.privateKey,
passphrase: req.passphrase,
tryKeyboard: true,
});
} else {
log.error(
"Authentication failure due to missing credentials for %s:%d",
req.host,
req.port
);
handleError(ErrorType.SSH_AUTHENTICATION_ERROR);
}
});
session.on("banner", (data) => {
log.debug("banner sent by %s:%d", shellConfig.host, shellConfig.port);
// need to convert to cr/lf for proper formatting
data = data.replace(/\r?\n/g, "\r\n");
socket.emit("data", Buffer.from(data, "utf-8"));
});
session.on("ready", () => {
log.info("shell is ready for %s:%d", shellConfig.host, shellConfig.port);
session.shell(
{
term: "xterm-color",
rows: termConfig.rows,
cols: termConfig.cols,
height: termConfig.height,
width: termConfig.width,
},
(err, stream) => {
if (err) {
log.error(err);
handleError(ErrorType.SHELL_ERROR);
return;
}
socket.on("data", (data: string) => {
stream.write(data);
});
socket.on(
"resize",
(data: {
rows: number;
cols: number;
height: number;
width: number;
}) => {
stream.setWindow(data.rows, data.cols, data.height, data.width);
}
);
socket.on("disconnecting", (reason) => {
log.debug("client disconnecting with reason '%s'", reason);
});
socket.on("disconnect", (reason) => {
log.debug("client disconnected with reason '%s'", reason);
session.end();
});
socket.on("error", (err) => {
log.error("connection error");
log.error(err);
handleError(ErrorType.SOCKET_ERROR);
});
stream.on("data", (data: string) => {
socket.emit("data", Buffer.from(data, "utf-8"));
});
stream.on("close", (code?: number, signal?: string) => {
log.debug(
"connection closed. exit code: %s, signal: %s",
code,
signal
);
session.end();
});
stream.stderr.on("data", (err) => {
log.error("shell error");
log.error(err);
});
}
);
});
session.on("end", () => {
log.debug("connection end by host");
});
session.on("close", (hadError) => {
log.info("connection closed %s", hadError ? "with error" : "without error");
});
session.on("error", (err) => {
log.error("connection error");
log.error(err);
handleError(ErrorType.SSH_CONNECTION_ERROR);
});
const handleError = (type: ErrorType) => {
log.info("Disconnecting socket client.");
socket.emit("shell_error", type);
socket.disconnect(true);
session.end();
};
};
export default shell;