-
Notifications
You must be signed in to change notification settings - Fork 7
/
repl.js
104 lines (101 loc) · 3.14 KB
/
repl.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
/**
*
* A network based REPL (Read–eval–print loop).
*
* Usage:
* netcat [IP-ADDRESS] 1234
*
* After successful connection a prompt appears: '>'
* Then type [USER]:[PASS] (the defaults are esp32:esp32)
* After successful authentication you will see "====> authorized."
*
* After that you can type every JS expression, which then gets
* evaluated in esp32-javascript and the result is printed.
*
* This is only considered as demo. If you want to use it in production
* please change the ssl flag to true, otherwise credentials
* are visible for "persons in the middle".
*/
var configManager = require("esp32-javascript/config");
var PROMPT = "> ";
var textDecoder = new TextDecoder();
function writeOutput(socket, result) {
socket.write(result);
socket.flush();
}
require("socket-events").sockListen(
1234,
function (socket) {
var authorized = false;
var _ = undefined;
socket.onData = function (buffer) {
var data = textDecoder.decode(buffer);
var result = null;
if (!authorized) {
var accessConfig = configManager.config.access;
if (
data ===
accessConfig.username + ":" + accessConfig.password + "\n"
) {
authorized = true;
result = "====> authorized.\n";
} else {
result = "ERR=> type [username]:[pass] to authorize.\n";
}
} else {
try {
var logOutput = [];
(function () {
var _console = console;
try {
console = {
log: function (msg) {
_console.log(msg);
//writeOutput(socket, '|' + msg + '\n');
logOutput.push("LOG|" + msg + "\n");
},
debug: function (msg) {
_console.debug(msg);
//writeOutput(socket, "|" + msg + "\n");
logOutput.push("DEBUG|" + msg + "\n");
},
info: function (msg) {
_console.info(msg);
//writeOutput(socket, "|" + msg + "\n");
logOutput.push("INFO|" + msg + "\n");
},
warn: function (msg) {
_console.warn(msg);
//writeOutput(socket, "|" + msg + "\n");
logOutput.push("WARN|" + msg + "\n");
},
error: function (msg) {
_console.error(msg);
//writeOutput(socket, "|" + msg + "\n");
logOutput.push("ERROR|" + msg + "\n");
},
};
_ = eval.call(globalThis, data);
} finally {
console = _console;
}
})();
result = logOutput.join("") + "====> " + _ + "\n";
} catch (error) {
result = logOutput.join("") + "ERR=> " + error + "\n";
}
}
writeOutput(socket, result);
writeOutput(socket, PROMPT);
};
// write initial prompt
writeOutput(socket, PROMPT);
},
function (sockfd) {
console.error("ON ERROR: Socket " + sockfd);
},
function () {
console.debug("SOCKET WAS CLOSED!");
},
false
);