-
Notifications
You must be signed in to change notification settings - Fork 17
/
vm.js
273 lines (267 loc) · 8.23 KB
/
vm.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
var path = require('path'),
vm = require("vm"),
fs = require("fs"),
logger = require("console").Console,
util = require("util"),
memory = require("./memory"),
events = require("events");
CoffeeScript = require('coffee-script');
var colors = require('colors');
var convertCode = function (data, type) {
switch(type){
case 'text/coffeescript':
return CoffeeScript.compile(data.toString());
default:
return data;
}
}
function VMStream(filename,id,error) {
this.filename = filename;
this.id = id;
this.error = !!error;
}
util.inherits(VMStream,events.EventEmitter)
VMStream.prototype.write = function(message) {
process.stdout
.write( ("VM-SMS " + (this.error ? "ERROR" : "LOG") +' "' +this.filename + '" "'+ this.id +'" > '+ message).grey);
}
var sessions = {};
var localStorage = {};
var preScript = "("+(function(){
if(!Array.prototype.rnd){
Object.defineProperty(Array.prototype,'rnd',{
get:function (){
var randscript = -1,
max = this.length-1;
while (randscript < 0 || randscript > max || isNaN(randscript))
randscript = parseInt(Math.random()*(max+1));
return this[randscript];
}
});
};
if(!Array.prototype.sql){
/*
Usage :
var data = [{a:1,b:1,c:1},{a:1,b:2,c:1},{a:1,b:3,c:1}, {a:2,b:1,c:1}];
var res = data.sql('SELECT a, COUNT(*) AS b FROM ? GROUP BY a');
console.log(res);
*/
Object.defineProperty(Array.prototype,'sql',{
value : function (sql){
var alasql = require("alasql");
alasql.databases.alasql.tables.data = this ;
return alasql(sql);
}
});
};
}).toString()+")()";
var lang = false;
var script = {};
var modules = {};
var settings = {};
var allowNativeModules = {
tcp : function(){
return {
http : require("http").request,
https: require("https").request,
socket : require("net").connect,
dgram : require('dgram').createSocket,
tls : require('tls').connect
};
},
url : function(){
return require("url");
},
assert : function(){
return require("assert");
},
md5 : function(){
return require("MD5")
},
randomstring : function(){
return require("randomstring");
},
punycode : function(){
return require("punycode");
},
dns : function(){
return require('dns');
},
events : function(){
return require('events');
},
alasql : function(){
return require('alasql');
}
}
var adapter = {
"arango" : ["","arango"],
"node-firebird" : ["" ,"firebird"],
"mongodb" : ["createConnection","mongodb"],
"mongoose" : ["createConnection","mongoose"],
"mongoose/schema" : ["Schema","mongoose-schema"],
"mysql" : ["createConnection","mysql"],
"nano" : ["","nano"],
"neo4j" : ["","neo4j"],
"pg" : ["Client","postgres"],
"redis" : ["createClient" ,"redis"],
"rethinkdb" : ["","rethinkdb"],
"riak-js" : ["","riak"],
"sqlite3" : ["","sqlite"],
"tingodb" : ["", "tingodb"]
}
console.log("Expose module to VMs".grey, process.argv[4].grey,process.argv[2].grey);
for(var i in adapter){
try {
require.resolve(i.split('/')[0]);
console.log("resolve ".grey,adapter[i][1].grey,'(',i.split('/')[0].yellow,')',"...OK".grey);
allowNativeModules[adapter[i][1]] = (function(m,name){
if(name){
var mod = require(m);
var ret = mod[name];
if(ret instanceof Function)
return ret.bind(mod);
else
return ret;
}
return require(m);
}).bind(null,i.split('/')[0],adapter[i][0]);
} catch(e){
console.log("resolve ".grey,adapter[i][1].grey,'(',i.split('/')[0].yellow,')',"...FAILS".grey);
console.log("\t",e)
}
}
process.on("message",function(m){
if (m.type === 'settings'){
settings = m.data;
}else if (m.type === 'setDIR'){
__DIR = m.data;
}else if (m.type === 'sms'){
// console.log("recieve SMS",m.time);
m.msgdata_orig = m.msgdata;
m.msgdata = new Buffer(m.msgdata).toString().trim();
m.receiver = new Buffer(m.receiver).toString().toLowerCase();
m.sender = new Buffer(m.sender).toString().toLowerCase();
var priv = Symbol(m.sender);
m[priv] = true;
m.fileType = m.script.type;
m.file = m.script.file;
var sendError = function(err){
var tmp = m.receiver;
m.receiver = m.sender;
m.sender = tmp;
console.log(("SMS SEND Exec Error".red, process.argv[2],m.file, err.stack || err).grey);
m.msgdata = settings.defautErrorMSG || "EXEC ERROR";
process.send(m);
};
if(!script[m.file]){
try{data = fs.readFileSync(m.file);}catch(e){return sendError(e);}
script[m.file] = vm.createScript(preScript+";"+convertCode(data,m.fileType), m.file);
fs.watchFile(m.file, (function (file,sendError,curr, prev) {
console.log('VM script reload: '.grey, process.argv[4].grey,process.argv[2].grey , file);
try{data = fs.readFileSync(file);}catch(err){return console.log('VM Script Exception: '.grey, process.argv[4].grey,process.argv[2].grey , err);}
script[file] = vm.createScript(preScript+";"+convertCode(data,m.fileType), file);
}).bind(null,m.file,sendError));
}
/* definition de la session et du storage */
var _id = new Buffer(m.sender).toString();
m.filename = path.basename(m.file);
var MSG = function(conf){
conf = conf || {};
if(this instanceof arguments.callee){
for (var property in conf)
this[property] = conf[property];
if(conf[priv]){
var tmp = this.receiver;
this.receiver = this.sender;
this.sender = tmp;
}
delete this.type;
Object.defineProperties(this, {
sendSMS : {
value: function(msg){
if(msg)
this.msgdata = msg;
process.send(this);
},
writable: false,
enumerable: false,
configurable: false
},
send : {
value: function(msg){
if(msg)
this.msgdata = msg;
process.send(this);
},
writable: false,
enumerable: false,
configurable: false
},
type : {
value: "sms",
writable: false,
enumerable: true,
configurable: false
}
});
}else
return new arguments.callee(conf);
};
var require = function(name){
name = name.split(/[ -]/).join("-").replace(/[^\w\-]+/g,"").toLowerCase();
if(modules[name])
return modules[name];
if(allowNativeModules[name])
return modules[name] = allowNativeModules[name]();
var n = path.join(__DIR,"scripts","modules" ,name);
if(!fs.existsSync(n)) throw "Module "+name+" not found";
try{
var data = fs.readFileSync(n);
var sand = {
exports : {},
module : {
id : name,
exports : null
},
session : new memory.Client(_id),
localStorage : new memory.Client(m.keywords[0].toLowerCase()),
globalStorage : new memory.Client(m.receiver),
logger : new logger(new VMStream(name, "module-"+name),new VMStream(name, "module-"+name,true)),
Buffer : Buffer,
require : require,
get MSG() {return MSG }
};
vm.runInNewContext(preScript+";"+data, sand);
fs.watchFile(n, (function (file,curr, prev) {
console.log('Module VM script reload: '.grey, process.argv[4].grey,process.argv[2].grey , file);
try{data = fs.readFileSync(file);}catch(err){return console.log('Module VM Script Exception: '.grey, process.argv[4].grey,process.argv[2].grey , err);}
vm.runInNewContext(preScript+";"+data, sand);
modules[name]=sand.module.exports ? sand.module.exports : sand.exports;
}).bind(null,n));
return modules[name]=sand.module.exports ? sand.module.exports : sand.exports;
}catch(e){
throw e;
}
};
try{
script[m.file].runInContext(vm.createContext({
sms : m ,
logger : new logger(new VMStream(path.basename(m.file), m.id),new VMStream(path.basename(m.file), m.id,true)),
Buffer : Buffer,
now : Date.now(),
require : require,
get MSG() {return MSG },
session : new memory.Client(_id),
localStorage : new memory.Client(m.keywords[0].toLowerCase()),
globalStorage : new memory.Client(m.receiver)
}));
}catch(e){
return sendError(e);
}
delete sandbox;
}
});
process.on('uncaughtException', function(err) {
console.log('VM Caught exception: '.grey, process.argv[4].grey,process.argv[2].grey , err.stack);
});