-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
231 lines (205 loc) · 5.88 KB
/
main.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { InstanceBase, Regex, runEntrypoint, InstanceStatus } = require('@companion-module/base')
const UpdateActions = require('./actions')
const UpdateFeedbacks = require('./feedbacks')
const UpdateVariableDefinitions = require('./variables')
const ssh = require('ssh2')
const fs = require('fs')
const algorithms = require('./algorithms')
const Constants = {
CMD_ERROR_VAR_NAME: 'returnedError',
CMD_ERROR_FEEDBACK_NAME: 'commandErrorState',
}
class SSHInstance extends InstanceBase {
constructor(internal) {
super(internal)
}
async init(config) {
this.config = config
this.updateActions() // export actions
this.updateFeedbacks() // export feedbacks
this.updateVariableDefinitions() // export variable definitions
this.initSSH() // start up SSH connection
}
// allows other files that have a reference to the instance class to grab the constants
getConstants() {
return Constants
}
initSSH() {
if (this.sshClient !== undefined) {
// clean up the SSH connection
this.sshClient.destroy()
delete this.sshClient
this.updateStatus(InstanceStatus.Disconnected)
}
if (this.config.host) {
// create the ssh connection
this.sshClient = new ssh.Client()
var loadedPrivateKey = ''
// we will enable key-based authentication and load the key from the filepath if there is no password present
if (this.config.password == null || this.config.password == '') {
try {
loadedPrivateKey = fs.readFileSync(this.config.privatekeypath, 'utf8')
this.log('debug', 'private-key file loaded successfully!')
} catch (err) {
this.log('error', 'private-key file load error: ' + err)
}
}
// setup the needed parameters for the SSH client connection
const clientConfig = {
host: this.config.host,
port: this.config.port,
username: this.config.username,
password: this.config.password,
privateKey: loadedPrivateKey,
passphrase: this.config.passphrase,
keepaliveInterval: this.config.keepaliveInterval,
algorithms: algorithms.createAlgorithmsObjectForSSH2(this.config),
}
try {
// initiate the SSH client connection
this.sshClient.connect(clientConfig)
} catch (err) {
this.log('error', 'initiating connection failed, error: ' + err)
this.updateStatus(InstanceStatus.ConnectionFailure)
return
}
this.updateStatus(InstanceStatus.Connecting)
// for password-based authentication, if the server requests for a password change,
// we really can't do this interactively, so we will need to put the module into an error state and try again.
this.sshClient.on('change password', (message) => {
this.log(
'error',
'Server requests that you change your password, please do this manually and try the module again in Companion: ' +
message
)
this.updateStatus(InstanceStatus.ConnectionFailure)
})
this.sshClient.on('error', (err) => {
this.log('error', 'Server connection error: ' + err)
this.updateStatus(InstanceStatus.ConnectionFailure)
})
this.sshClient.on('end', () => {
this.log('error', 'Server ended connection')
this.updateStatus(InstanceStatus.Disconnected)
})
this.sshClient.on('timeout', () => {
this.log('error', 'Server connection timed out')
this.updateStatus(InstanceStatus.ConnectionFailure)
})
this.sshClient.on('connect', () => {
this.log('debug', 'Server connection successful!')
this.updateStatus(InstanceStatus.Ok)
})
this.sshClient.on('greeting', (greeting) => {
this.log('debug', 'Server greeting: ' + greeting)
})
this.sshClient.on('handshake', (negotiated) => {
this.log('debug', 'Server handshake: ' + JSON.stringify(negotiated))
})
this.sshClient.on('keyboard-interactive', (name, instructions, instructionsLang, prompts, finish) => {
this.log('debug', 'Interactive triggerd: ' + JSON.stringify(instructions));
this.log('debug', 'Interactive triggerd: ' + JSON.stringify(instructionsLang));
this.log('debug', 'Interactive triggerd: ' + JSON.stringify(prompts));
if (prompts.length === 1
&& prompts[0].prompt === "Password: ") {
finish([this.config.password]);
}
})
}
}
// When module gets deleted
async destroy() {
this.log('debug', 'destroy')
}
async configUpdated(config) {
this.config = config
this.initSSH() // restart SSH connection when the config is updated
}
// Return config fields for web config
getConfigFields() {
return [
{
type: 'textinput',
id: 'host',
label: 'Target Hostname/IP',
width: 8,
regex: Regex.HOSTNAME,
},
{
type: 'textinput',
id: 'port',
label: 'Target Port',
width: 4,
regex: Regex.PORT,
default: 22,
},
{
type: 'textinput',
id: 'username',
label: 'Username',
width: 6,
},
{
type: 'textinput',
id: 'password',
label: 'Password',
width: 6,
},
{
type: 'textinput',
id: 'privatekeypath',
label: 'Full path to Private Key file on local system (key-based authentication)',
width: 6,
},
{
type: 'textinput',
id: 'passphrase',
label: 'Passphrase (key-based authentication)',
width: 6,
},
{
type: 'textinput',
id: 'keepaliveInterval',
label: 'Keepalive Interval in ms',
width: 6,
regex: Regex.NUMBER,
default: 0,
},
{
type: 'dropdown',
id: 'preferedCipher',
label: 'Prefered Cipher Methods',
width: 6,
default: algorithms.Ciphers.AUTO,
choices: [
{
id: algorithms.Ciphers.AUTO,
label: 'Auto'
},
{
id: algorithms.Ciphers.AES256,
label: 'AES256',
},
{
id: algorithms.Ciphers.AES192,
label: 'AES192',
},
{
id: algorithms.Ciphers.AES128,
label: 'AES128',
},
],
},
]
}
updateActions() {
UpdateActions(this)
}
updateFeedbacks() {
UpdateFeedbacks(this)
}
updateVariableDefinitions() {
UpdateVariableDefinitions(this)
}
}
runEntrypoint(SSHInstance, [])