forked from menubboi/HerokuShell-Rclone
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.js
More file actions
251 lines (205 loc) · 6.66 KB
/
Copy pathutils.js
File metadata and controls
251 lines (205 loc) · 6.66 KB
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
/**
* Miscellaneous utilities.
**/
var fs = require("fs");
var util = require("util");
var mime = require("mime");
var crypto = require("crypto");
var url = require("url");
/** TIMER **/
function Timer(delay) {
this.delay = delay;
}
util.inherits(Timer, require("events").EventEmitter);
/* Starts the timer, does nothing if started already. */
Timer.prototype.set = function set() {
if (this.timeout) return;
this.timeout = setTimeout(function () {
this.timeout = null;
this.emit("fire");
}.bind(this), this.delay);
this.emit("active");
};
/* Cancels the timer if set. */
Timer.prototype.cancel = function cancel() {
if (!this.timeout) return;
clearTimeout(this.timeout);
delete this.timeout;
};
/* Starts the timer, cancelling first if set. */
Timer.prototype.reset = function reset() {
this.cancel();
this.set();
};
/** EDITED MESSAGE **/
function EditedMessage(reply, text, mode) {
this.reply = reply;
this.mode = mode;
this.lastText = text;
this.markup = reply.parameters["reply_markup"];
this.disablePreview = reply.parameters["disable_web_page_preview"];
this.text = text;
this.callbacks = [];
this.pendingText = null;
this.pendingCallbacks = [];
this.idPromise = new Promise(function (resolve, reject) {
reply.text(this.text, this.mode).then(function (err, msg) {
if (err) reject(err);
else resolve(msg.id);
this._whenEdited(err, msg);
}.bind(this));
}.bind(this));
}
util.inherits(EditedMessage, require("events").EventEmitter);
EditedMessage.prototype.refresh = function refresh(callback) {
if (callback) this.pendingCallbacks.push(callback);
this.pendingText = this.lastText;
if (this.callbacks === undefined) this._flushEdit();
};
EditedMessage.prototype.edit = function edit(text, callback) {
this.lastText = text;
var idle = this.callbacks === undefined;
if (callback) this.pendingCallbacks.push(callback);
if (text === this.text) {
this.callbacks = (this.callbacks || []).concat(this.pendingCallbacks);
this.pendingText = null;
this.pendingCallbacks = [];
if (idle) this._whenEdited();
} else {
this.pendingText = text;
if (idle) this._flushEdit();
}
};
EditedMessage.prototype._flushEdit = function _flushEdit() {
this.text = this.pendingText;
this.callbacks = this.pendingCallbacks;
this.pendingText = null;
this.pendingCallbacks = [];
this.reply.parameters["reply_markup"] = this.markup;
this.reply.parameters["disable_web_page_preview"] = this.disablePreview;
this.reply.editText(this.id, this.text, this.mode).then(this._whenEdited.bind(this));
};
EditedMessage.prototype._whenEdited = function _whenEdited(err, msg) {
if (err) this.emit(this.id === undefined ? "error" : "editError", err);
if (this.id === undefined) this.id = msg.id;
var callbacks = this.callbacks;
delete this.callbacks;
callbacks.forEach(function (callback) { callback(); });
if (this.pendingText !== null) this._flushEdit();
};
/** SANITIZED ENV **/
function getSanitizedEnv() {
// Adapted from pty.js source
var env = {};
Object.keys(process.env).forEach(function (key) {
env[key] = process.env[key];
});
// Make sure we didn't start our
// server from inside tmux.
delete env.TMUX;
delete env.TMUX_PANE;
// Make sure we didn't start
// our server from inside screen.
// http://web.mit.edu/gnu/doc/html/screen_20.html
delete env.STY;
delete env.WINDOW;
// Delete some variables that
// might confuse our terminal.
delete env.WINDOWID;
delete env.TERMCAP;
delete env.COLUMNS;
delete env.LINES;
// Set $TERM to screen. This disables multiplexers
// that have login hooks, such as byobu.
env.TERM = "screen";
return env;
}
/** RESOLVE SIGNAL **/
var SIGNALS = "HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS".split(" ");
function formatSignal(signal) {
signal--;
if (signal in SIGNALS) return "SIG" + SIGNALS[signal];
return "unknown signal " + signal;
}
/** SHELLS **/
function getShells() {
var lines = fs.readFileSync("/etc/shells", "utf-8").split("\n")
var shells = lines.map(function (line) { return line.split("#")[0]; })
.filter(function (line) { return line.trim().length; });
// Add process.env.SHELL at #1 position
var shell = process.env.SHELL;
if (shell) {
var idx = shells.indexOf(shell);
if (idx !== -1) shells.splice(idx, 1);
shells.unshift(shell);
}
return shells;
}
var shells = getShells();
/** RESOLVE SHELLS **/
function resolveShell(shell) {
return shell; //TODO: if found in list, otherwise resolve with which & verify access
}
/** TOKEN GENERATION **/
function generateToken() {
return crypto.randomBytes(12).toString("hex");
}
/** RESOLVE BOOLEAN **/
var BOOLEANS = {
"yes": true, "no": false,
"y": true, "n": false,
"on": true, "off": false,
"enable": true, "disable": false,
"enabled": true, "disabled": false,
"active": true, "inactive": false,
"true": true, "false": false,
};
function resolveBoolean(arg) {
arg = arg.trim().toLowerCase();
if (!Object.hasOwnProperty.call(BOOLEANS, arg)) return null;
return BOOLEANS[arg];
}
/** GENERATE FILENAME WHEN NOT AVAILABLE **/
function constructFilename(msg) {
return "upload." + mime.extension(msg.file.mime);
}
/** AGENT **/
function createAgent() {
var proxy = process.env["https_proxy"] || process.env["all_proxy"];
if (!proxy) return;
try {
proxy = url.parse(proxy);
} catch (e) {
console.error("Error parsing proxy URL:", e, "Ignoring proxy.");
return;
}
if ([ "socks:", "socks4:", "socks4a:", "socks5:", "socks5h:" ].indexOf(proxy.protocol) !== -1) {
try {
var SocksProxyAgent = require('socks-proxy-agent');
} catch (e) {
console.error("Error loading SOCKS proxy support, verify socks-proxy-agent is correctly installed. Ignoring proxy.");
return;
}
return new SocksProxyAgent(proxy);
}
if ([ "http:", "https:" ].indexOf(proxy.protocol) !== -1) {
try {
var HttpsProxyAgent = require('https-proxy-agent');
} catch (e) {
console.error("Error loading HTTPS proxy support, verify https-proxy-agent is correctly installed. Ignoring proxy.");
return;
}
return new HttpsProxyAgent(proxy);
}
console.error("Unknown proxy protocol:", util.inspect(proxy.protocol), "Ignoring proxy.");
}
exports.Timer = Timer;
exports.EditedMessage = EditedMessage;
exports.getSanitizedEnv = getSanitizedEnv;
exports.formatSignal = formatSignal;
exports.shells = shells;
exports.resolveShell = resolveShell;
exports.generateToken = generateToken;
exports.resolveBoolean = resolveBoolean;
exports.constructFilename = constructFilename;
exports.createAgent = createAgent;