-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.js
177 lines (171 loc) · 5.55 KB
/
server.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
// Watch and sync a local folder with a remote one.
// All the local operations will be repeated on the remote.
//
// This this the remote server.
var cs, http, kit, localPath;
kit = require('nokit');
cs = kit.require('brush');
http = require('http');
localPath = function(path) {
if (process.platform === 'win32') {
return path.replace(/\//g, '\\');
} else {
return path.replace(/\\/g, '\/');
}
};
module.exports = function(conf) {
var decodeInfo, service;
kit._.defaults(conf, require('./config.default'));
decodeInfo = function(str) {
return JSON.parse(conf.password ? kit.decrypt(new Buffer(str, 'hex'), conf.password, conf.algorithm) : decodeURIComponent(str));
};
service = http.createServer(function(req, res) {
var absPath, absRoot, data, err, getStream, httpError, mode, p, path, pipeToFile, ref, reqStream, type;
httpError = function(code, err) {
kit.err((err != null ? err.stack : void 0) || err);
res.statusCode = code;
return res.end(http.STATUS_CODES[code]);
};
try {
ref = decodeInfo(req.url.slice(1)), type = ref.type, path = ref.path, mode = ref.mode;
} catch (_error) {
err = _error;
return httpError(400, err);
}
path = localPath(path);
if (conf.rootAllowed) {
absPath = kit.path.normalize(kit.path.resolve(path));
absRoot = kit.path.normalize(kit.path.resolve(conf.rootAllowed));
if (absPath.indexOf(absRoot) !== 0) {
return httpError(403, err);
}
}
kit.log(cs.grey("[server] ") + cs.cyan(type) + ': ' + path);
p = kit.Promise.resolve();
reqStream = null;
getStream = function() {
var crypto, decipher;
if (conf.password) {
crypto = kit.require('crypto', __dirname);
decipher = crypto.createDecipher(conf.algorithm, conf.password);
return req.pipe(decipher);
} else {
return req;
}
};
pipeToFile = function() {
reqStream = getStream();
return kit.mkdirs(kit.path.dirname(path)).then(function() {
var f;
f = kit.createWriteStream(path, {
mode: mode
});
f.on('error', function(err) {
return p = kit.Promise.reject(err);
});
return new kit.Promise(function(resolve) {
return reqStream.pipe(f).on('finish', function() {
return resolve();
});
});
});
};
switch (type) {
case 'create':
if (path.slice(-1) === kit.path.sep) {
p = kit.mkdirs(path);
} else {
p = pipeToFile();
}
break;
case 'modify':
p = pipeToFile();
}
if (!reqStream) {
data = new Buffer(0);
req.on('data', function(chunk) {
return data = Buffer.concat([data, chunk]);
});
}
req.on('error', function(err) {
return p = kit.Promise.reject(err);
});
return req.on('end', function() {
var child_process, oldPath, tmpFile;
oldPath = null;
switch (type) {
case 'create':
case 'modify':
null;
break;
case 'move':
if (conf.password && data.length > 0) {
data = kit.decrypt(data, conf.password, conf.algorithm);
}
oldPath = localPath(data.toString());
p = kit.move(oldPath, path.replace(/\/+$/, ''));
break;
case 'delete':
p = kit.remove(path);
break;
case 'execute':
child_process = kit.require('child_process', __dirname);
if (conf.password && data.length > 0) {
data = kit.decrypt(data, conf.password, conf.algorithm);
}
tmpFile = __dirname + '/tmp/' + Date.now() + Math.random() + (path || '.js');
kit.outputFile(tmpFile, data).then(function() {
var cipher, crypto, proc;
proc = child_process.fork(tmpFile, {
silent: true
});
res.on('error', function() {
return proc.kill('SIGINT');
});
proc.on('close', function() {
var ref1;
kit.remove(tmpFile);
return (ref1 = conf.onChange) != null ? ref1.call(0, type, path) : void 0;
});
if (conf.password) {
crypto = kit.require('crypto', __dirname);
cipher = crypto.createCipher(conf.algorithm, conf.password);
proc.on('close', function() {
return res.end(cipher.final());
});
proc.stdout.on('data', function(c) {
return res.write(cipher.update(c));
});
return proc.stderr.on('data', function(c) {
return res.write(cipher.update(c));
});
} else {
proc.on('close', function() {
return res.end();
});
proc.stdout.on('data', function(c) {
return res.write(c);
});
return proc.stderr.on('data', function(c) {
return res.write(c);
});
}
});
return;
default:
return httpError(404, new Error('Unknown Change Type'));
}
return p.then(function() {
var ref1;
return kit.Promise.resolve((ref1 = conf.onChange) != null ? ref1.call(0, type, path, oldPath, mode) : void 0);
}).then(function() {
return res.end();
})["catch"](function(err) {
return httpError(500, err);
});
});
});
return service.listen(conf.port, function() {
return kit.log(cs.cyan("Listen: ") + conf.port);
});
};