forked from fictive-kin/webshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsutil.js
145 lines (128 loc) · 3.86 KB
/
wsutil.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
// vim: sw=2 ts=2 et
var url = require('url'),
util = require('util'),
stylize = require('./colors').stylize,
querystring = require('querystring'),
fs = require('fs'),
wsrc = require('./wsrc'),
_ = require('./underscore')._;
exports.parseURL = function(urlStr, protocolHelp, previousUrl) {
var u = url.parse(urlStr);
if (!u.protocol && !u.hostname) {
var prevU = previousUrl ? exports.parseURL(previousUrl) : url.parse('http://example.com:80/');
u.protocol = prevU.protocol;
u.hostname = prevU.hostname;
u.slashes = prevU.slashes;
u.port = prevU.port;
if (u.pathname.substr(0,1) != '/') {
u.pathname = '/' + u.pathname;
}
}
if (protocolHelp && !u.protocol) {
u = url.parse('http://'+urlStr);
}
u.port = u.port || (u.protocol === 'https:' ? 443 : 80);
u.pathname = u.pathname || '/';
return u;
};
exports.formatUrl = function (u, includePath, showPassword) {
var auth = '';
var port = '';
if (('http:' === u.protocol && 80 !== u.port) || ('https:' === u.protocol && 443 !== u.port)) {
port = ':' + u.port;
}
if (u.auth) {
if (showPassword) {
auth = u.auth + '@';
} else {
auth = u.auth.split(':')[0] + ':***@';
}
}
var url = u.protocol + (u.slashes ? '//' : '') + auth + u.hostname + port;
if (includePath) {
url += u.pathname;
}
return url;
};
exports.responsePrinter = function($_, response) {
var bufferOk = true;
if (_.isFunction($_.toolbox.responsePrinter)) {
bufferOk = $_.toolbox.responsePrinter($_, response);
} else {
if ($_.json) {
bufferOk = web_repl.rli.outputWrite(util.inspect($_.json, false, undefined, true));
web_repl.rli.outputWrite("\n");
}
}
return bufferOk;
};
exports.formatStatus = function(status, url, seq, auth) {
var url = exports.formatUrl(url, true);
var msg = "HTTP " + status + " " + stylize(url, 'white') + stylize(' #' + seq, 'grey');
if (auth) {
msg += stylize(' (' + auth + ')', 'grey');
}
if (exports.httpSuccess(status)) {
return stylize(msg, 'green');
} else if (exports.httpRedirection(status)) {
return stylize(msg, 'yellow');
} else if (exports.httpClientError(status) || exports.httpServerError(status)) {
return stylize(msg, 'red');
} else {
return stylize(msg, 'white');
}
};
exports.httpSuccess = function(status) {
return status >= 200 && status < 300;
};
exports.httpRedirection = function(status) {
return status >= 300 && status < 400;
};
exports.httpClientError = function(status) {
return status >= 400 && status < 500;
};
exports.httpServerError = function(status) {
return status >= 500 && status < 600;
};
exports.base64Encode = function(str) {
return (new Buffer(str, 'ascii')).toString('base64');
};
exports.printHeader = function(value, name) {
function normalizeName(name) {
return _.map(name.split('-'), function(s) { return s[0].toUpperCase() + s.slice(1, s.length); }).join('-');
}
console.log(normalizeName(name) + ": " + value);
};
exports.postToRequestData = function ($_, post) {
var data = querystring.parse(post);
if (data) {
$_.requestData = data;
return data;
}
return false;
};
exports.fileToRequestData = function ($_, filename, encoding) {
if (undefined == encoding) {
encoding = 'utf8';
}
try {
$_.requestData = fs.readFileSync(filename, encoding);
console.log(stylize("Set requestData to '" + filename + "' (" + $_.requestData.length + " bytes, " + encoding + ")", "yellow"));
} catch (e) {
console.log(stylize("Could not read " + filename, "red"));
}
};
exports.evalFile = function (filename) {
eval("var s = " + fs.readFileSync(filename));
return s;
};
exports.onExit = function (web_repl, $_) {
var rc;
if (web_repl.rli._hardClosed) {
rc = wsrc.get();
} else {
rc = wsrc.saveContext('_previous', $_);
}
rc.history = web_repl.rli.history;
wsrc.write(rc, $_.cookies);
};