-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (142 loc) · 4.86 KB
/
index.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
// WebPad Server
// import stuff and init websocket
const WebSocket = require("ws");
const flatten = require("lodash.flatten")
const file = require("./file.js")
const requireF = require("import-fresh")
const config = require("./settings.json")
const package = require("./package.json")
// setting up commands
let commands = {commands: [], commandList: []}
try {
const userCommands = requireF("./commands.js")
commands.commands.push(userCommands.commands)
commands.commandList.push(userCommands.commands.map((r) => r.name))
} catch(e) {
console.log("[SERVER] An error occured while trying to load the user command file.: " + e)
}
const manifest = file.getAddonManifest()
for (i of manifest) {
if (i.enabled === false) {
continue
}
try {
var addon = requireF(i.filePath)
commands.commands.push(addon.commands)
commands.commandList.push(addon.commands.map((r) => r.name))
} catch(e) {
console.log("[SERVER] An error occured while trying to load '" + i.name + "'.: " + e)
}
}
commands.commands = flatten(commands.commands)
commands.commandList = flatten(commands.commandList)
const wss = new WebSocket.Server({host: "0.0.0.0", port: config.port});
//set up server to receive and parse message
console.log("WebPad Server " + package.version + " is running on port " + config.port)
wss.on("connection", function connection(ws) {
console.log("A client connected.")
ws.authorized = false
ws.send(JSON.stringify({messageType: "handshake", requiresAuth: (config.password != null)}))
setTimeout(() => {
if (ws.authorized === false) {
ws.close(4001, "Handshake/authentication timed out.")
}
}, 10000)
ws.on("message", function incoming(message) {
parseMessage(message, ws);
});
});
function parseMessage(message, ws) {
// for invalid JSON
try {
message = JSON.parse(message)
} catch(e) {
ws.send(JSON.stringify({messageType: "error", message: "INVALID_JSON"}))
return
}
// message types
switch(message.messageType) {
case "error":
console.log("Client reported an error to the server: " + message.message)
break
case "auth":
if (ws.authorized === true) {
ws.send(JSON.stringify({messageType: "error", message: "ALREADY_PASSED_AUTHENTICATION"}))
return
}
if (message.password == null) {
ws.send(JSON.stringify({messageType: "auth", state: false}))
return
}
if (message.password === config.password) {
ws.send(JSON.stringify({messageType: "auth", state: true}))
ws.authorized = true
} else {
ws.send(JSON.stringify({messageType: "auth", state: false}))
}
break
case "handshake":
if (message.apiLevel !== package.apiLevel) {
ws.close(4000, "WebPad Version is incompatible (API level " + message.apiLevel + " and " + package.apiLevel + ").")
}
if (config.password == null) {
ws.authorized = true
}
break
case "command":
if (!ws.authorized) {
ws.send(JSON.stringify({messageType: "error", message: "AUTHENTICATION_NOT_PASSED"}))
return
}
if (!message.command) {
ws.send(JSON.stringify({messageType: "error", message: "NO_COMMAND_SPECIFIED"}))
} else {
executeCommand(ws, message.command, message.arguments)
}
break
case "fetch":
if (!ws.authorized) {
ws.send(JSON.stringify({messageType: "error", message: "AUTHENTICATION_NOT_PASSED"}))
return
}
ws.send(JSON.stringify({messageType: "success", commands: commands.commands.map((r) => { return {name: r.name, friendlyName: r.friendlyName, description: r.description, arguments: r.arguments}})}))
break
default:
ws.send(JSON.stringify({messageType: "error", message: "MESSAGE_TYPE_INVALID"}))
break
}
}
function executeCommand(ws, command, arguments) {
// if command exists
let res;
if (!commands.commandList.includes(command)) {
ws.send(JSON.stringify({messageType: "error", message: "COMMAND_DOES_NOT_EXIST"}))
return
}
executedCommand = commands.commands.find((r) => { return r.name === command})
// if there are args
if (executedCommand.arguments) {
// check if required args are there
if (!arguments) {
arguments = {}
}
for (i of executedCommand.arguments.map((r) => {return r.name})) {
if (!Object.keys(arguments).includes(i) && executedCommand.arguments.find((r) => { return r.name === i}).required) {
ws.send(JSON.stringify({messageType: "error", message: "REQUIRED_ARGUMENT_MISSING", context: i}))
return
}
}
// execute command
res = executedCommand.function(arguments)
} else {
res = executedCommand.function()
}
// return the result if it exists, otherwise, determine success status using true and false flag from res
if (res === true) {
ws.send(JSON.stringify({messageType: "success", context: executedCommand.name}))
} else if (res === false) {
ws.send(JSON.stringify({messageType: "error", message: "COMMAND_ERROR", context: executedCommand.name}))
} else {
ws.send(JSON.stringify({messageType: "undetermined", context: executedCommand.name, result: res}))
}
}