This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
wsrc.js
104 lines (92 loc) · 2.46 KB
/
wsrc.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
var fs = require('fs'),
stylize = require('./colors').stylize,
wsutil = require('./wsutil'),
_ = require('./underscore')._;
var functionPrefix = '___WSFUNC___';
function getRC() {
try {
return JSON.parse(fs.readFileSync(process.env.HOME + '/.webshellrc'));
} catch (e) {
return { history: [], contexts: {}, cookies: {} };
}
}
function writeRC(rc, cookies) {
rc.cookies = cookies.compactJar(true);
return fs.writeFileSync(
process.env.HOME + '/.webshellrc',
JSON.stringify(rc)
);
}
function saveContext(name, $_) {
var obj = {};
_.each($_, function(v, k) {
if (!_.isFunction(v)) {
obj[k] = v;
}
});
var toolbox = {};
_.each(obj.toolbox, function(v, k) {
if (typeof v === 'function') {
toolbox[k] = functionPrefix + v;
} else {
toolbox[k] = v;
}
});
obj.toolbox = toolbox;
delete obj['cookies'];
obj.__cookieJar = $_.cookies.__get_raw__();
var rc = getRC();
if (!rc.contexts) {
rc.contexts = {};
}
rc.contexts[name] = obj;
writeRC(rc, $_.cookies);
console.log("Saved context: " + name);
return getRC();
}
function loadContext(name, $_, web_repl, ignoreError) {
var rc = getRC();
if (rc.contexts[name]) {
_.each(rc.contexts[name], function (v, k) {
$_[k] = v;
});
// transpose functions from toolbox
if ($_.toolbox) {
var toolbox = {};
_.each($_.toolbox, function (v, k) {
if (typeof v == 'string' && v.substring(0, functionPrefix.length) == functionPrefix) {
try {
eval("toolbox[k] = " + v.slice(functionPrefix.length));
} catch (e) {}
} else {
toolbox[k] = v;
}
});
$_.toolbox = toolbox;
}
$_.cookies.__set_raw__($_.__cookieJar);
delete $_['__cookieJar'];
console.log("Loaded context: " + name);
} else if (!ignoreError) {
console.log(stylize("Could not load context: " + name, 'red'));
}
if (web_repl && $_.previousUrl) {
u = wsutil.parseURL($_.previousUrl);
web_repl.prompt = wsutil.formatUrl(u, false) + ' > ';
}
}
function delContext(name, $_) {
var rc = getRC();
if (rc.contexts[name]) {
delete rc.contexts[name];
writeRC(rc, $_.cookies);
console.log("Deleted context: " + name);
} else {
console.log(stylize("Context " + name + " does not exist.", 'red'));
}
}
exports.get = getRC;
exports.write = writeRC;
exports.saveContext = saveContext;
exports.loadContext = loadContext;
exports.delContext = delContext;