-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinAgent.js
307 lines (274 loc) · 9.86 KB
/
winAgent.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var fs = require("fs");
var path = require("path");
var ipc = require('node-ipc');
var Service = require('node-windows').Service;
ipc.config.id = 'server';
ipc.config.retry = 2000;
ipc.config.silent = true;
ipc.config.maxRetries = 2;
module.exports = function(opts){
var modules = {};
// Config for Windows service
var agentPrefix = opts.prefix;
var agentDescPrefix = opts.description;
var agentScript = opts.script;
var agentFolder = opts.folder || path.join(process.cwd(), 'agents');
var execPath = opts.execPath || null; //Default to node
try{
fs.mkdirSync(agentFolder);
}catch(e){}
if(opts.ipcConfig){
for(var setting in opts.ipcConfig){
ipc.config[setting] = opts.ipcConfig[setting];
}
}
/**********************************************************************
* Windows Agents
**********************************************************************/
modules.configureAgent = function(win_config, next){
// var agentName = "Agent";
var daemonDir = path.join(agentFolder, win_config.agentId);
try{
fs.mkdirSync(daemonDir);
}catch(e){}
// Create a new service object
var svc = new Service({
name: agentPrefix + win_config.username,
description: agentDescPrefix + win_config.username,
// script: path.join(__dirname, 'serviceAgent.js'),
script: agentScript,
execPath: execPath,
env: [{
name: "agentId",
value: win_config.agentId
}]
});
// Set a directory per agent
svc._directory = daemonDir;
svc.on('error',function(err){
return next(err);
});
return next(null, svc);
};
// Test if an agent is online,
// Return (err, agentId)
modules.install = function(win_config, password, next){
if(typeof password === 'function'){
next = password;
password = false;
}
modules.configureAgent(win_config, function(err, service){
if(err){
return next(err);
}
var once = true;
var daemonXmlFile = path.join(service._directory, "daemon", service.name + '.xml');
if(password){
service.logOnAs.domain = win_config.domain;
service.logOnAs.account = win_config.username;
service.logOnAs.password = password;
}
// Listen for the "install" event, which indicates the
// process is available as a service.
service.on('install',function(){
//Start the service if password was provided
if(password){
service.start();
}else{
return next(null);
}
});
service.on('start',function(){
if(once){
once = false;
// Delete password from file
fs.readFile(daemonXmlFile, 'utf8', function(err, xmlContent){
if(err){
return next(err);
}else{
// Supress password
xmlContent = xmlContent.replace(
/<password>.+?<\/password>/g,
"<password>XXXXXX</password>");
// Rewrite
fs.writeFile(daemonXmlFile, xmlContent, 'utf8', function(err){
if(err){
return next(err);
}else{
// Success
return next(null);
}
});
}
});
}
});
service.on('error',function(err){
return next(err);
});
//Install and start
service.install();
});
};
modules.exists = function(win_config, next){
modules.configureAgent(win_config, function(err, service){
if(err){
return next(err);
}
// Verify existence
if(service.exists){
return next(null);
}else{
return next(new Error('Agent is not installed.'));
}
});
};
modules.start = function(win_config, next){
modules.configureAgent(win_config, function(err, service){
if(err){
return next(err);
}
var once = true;
// Listen for the "uninstall" event so we know when it's done.
service.on('start',function(){
// Uninstall the service.
if(once){
once = false;
next();
}
});
// Start service
service.start();
});
};
modules.stop = function(win_config, next){
modules.configureAgent(win_config, function(err, service){
if(err){
return next(err);
}
var once = true;
// Listen for the "stop" event
service.on('stop',function(){
// Uninstall the service.
if(once){
once = false;
return next();
}
});
// Stop service
service.stop();
});
};
modules.restart = function(win_config, next){
modules.configureAgent(win_config, function(err, service){
if(err){
return next(err);
}
var onceStop = true;
var onceStart = true;
// Listen for the "stop" event
service.on('stop',function(){
// Uninstall the service.
if(onceStop){
onceStop = false;
setTimeout(function(){
// Start service
service.start();
},2000);
}
});
// Listen for the "uninstall" event so we know when it's done.
service.on('start',function(){
// Uninstall the service.
if(onceStart){
onceStart = false;
next();
}
});
// Stop service
service.stop();
});
};
modules.uninstall = function(win_config, next){
modules.configureAgent(win_config, function(err, service){
if(err){
return next(err);
}
var once = true;
// Listen for the "stop" event
service.on('stop',function(){
// Uninstall the service.
if(once){
once = false;
service.uninstall();
}
});
// Listen for the "uninstall" event
service.on('uninstall',function(){
// Success
return next(null);
});
// Stop and uninstall
service.stop();
});
};
/**********************************************************************
* IPC Messages
**********************************************************************/
// Submit job to an agent by jobfile
modules.submit = function(win_config, jobWorkingDir, jobFile, next){
// Connect to AgentId
ipc.connectTo(win_config.agentId,function(){
//On Connect, send action
ipc.of[win_config.agentId].on('connect',function(){
ipc.of[win_config.agentId].emit('action',
{
win_config : win_config,
jobWorkingDir : jobWorkingDir,
jobfile : jobFile
}
);
});
ipc.of[win_config.agentId].on('answer',function(data){
ipc.disconnect(win_config.agentId);
return next(null, data);
});
ipc.of[win_config.agentId].on('error', function(err){
if(err.code === 'ENOENT'){
return next(new Error('Agent is unreachable.'));
}else{
return next(err);
}
});
});
};
// Test if an agent is online,
// Return (err, {agentId, username})
modules.ping = function(win_config, next){
// Count ping-ping, destroy event is emitted even if it works
var pingPongTest = 0;
// Connect to AgentId
ipc.connectTo(win_config.agentId,function(){
//Send ping, listen for pong
ipc.of[win_config.agentId].on('connect',function(){
ipc.of[win_config.agentId].emit('ping');
});
ipc.of[win_config.agentId].on('pong',function(data){
ipc.disconnect(win_config.agentId);
return next(null, data);
});
ipc.of[win_config.agentId].on('error', function(err){
if(err.code === 'ENOENT'){
pingPongTest++;
if(pingPongTest === ipc.config.maxRetries){
return next(new Error('Agent is unreachable.'));
}
}else{
return next(err);
}
});
});
};
// END
return modules;
};