· 2 years ago · Aug 22, 2023, 10:15 PM
1--Code will be deployed in ComputerCraft
2
3--Load the cryptoNet API
4os.loadAPI("cryptoNET")
5
6--region Constants
7--The config file path
8local CONFIG_FILE = "admin_controller.cfg"
9--endregion
10
11--region Variables
12local serverSocket
13local config
14local connectionEstablished = false
15local activated = false
16--endregion
17
18--region Methods
19local onConnectionOpened
20local onConnectionClosed
21local onEncryptedMessageReceived
22local onPlainMessageReceived
23local onLogin
24local onLoginFailed
25local onLogout
26local onKeyUp
27local onTerminate
28
29--region Message Methods
30
31--Report the status of the system
32function reportStatus(status)
33 local message = {
34 type = "status",
35 message = status
36 }
37
38 cryptoNET.send(serverSocket, message)
39end
40
41--endregion
42
43--region Network Events
44function onNetworkStartup()
45 --Get the side of the wireless modem and make sure it is not the wired modem
46 local modemSide
47 local modems = { peripheral.find("modem", function(name, modem)
48 return modem.isWireless() -- Check this modem is wireless.
49 end) }
50
51 for _, modem in pairs(modems) do
52 --Get the side that the modem is on
53 modemSide = peripheral.getName(modem)
54 end
55
56 --Read the configuration file
57 config = readConfigurationFile(CONFIG_FILE)
58
59 --Prepare the network connections
60 serverSocket = cryptoNET.connect(config.server, false, false, modemSide)
61
62 --Send a login message to the host controller
63 cryptoNET.login(serverSocket, config.systemUsername, config.systemPassword)
64end
65
66function onNetworkEventRaised(event)
67 --Different events have different parameters:
68 -- connection_opened
69 -- -> socket, server
70 -- connection_closed
71 -- -> socket, server
72 -- encrypted_message
73 -- -> message, socket, server
74 -- plain_message
75 -- -> message, socket, server
76 -- login
77 -- -> username, socket, server
78 -- login_failed
79 -- -> username, socket, server
80 -- logout
81 -- -> username, socket, server
82 -- key_up
83 -- -> key
84 local eventType = event[1]
85
86 if (eventType == "connection_opened") then
87 local socket = event[2]
88 local server = event[3]
89
90 onConnectionOpened(socket, server)
91 elseif (eventType == "connection_closed") then
92 local socket = event[2]
93 local server = event[3]
94
95 onConnectionClosed(socket, server)
96 elseif (eventType == "encrypted_message") then
97 local message = event[2]
98 local socket = event[3]
99 local server = event[4]
100
101 onEncryptedMessageReceived(message, socket, server)
102 elseif (eventType == "plain_message") then
103 local message = event[2]
104 local socket = event[3]
105 local server = event[4]
106
107 onPlainMessageReceived(message, socket, server)
108 elseif (eventType == "login") then
109 local username = event[2]
110 local socket = event[3]
111 local server = event[4]
112
113 onLogin(username, socket, server)
114 elseif (eventType == "login_failed") then
115 local username = event[2]
116 local socket = event[3]
117 local server = event[4]
118
119 onLoginFailed(username, socket, server)
120 elseif (eventType == "logout") then
121 local username = event[2]
122 local socket = event[3]
123 local server = event[4]
124
125 onLogout(username, socket, server)
126 --Add a check for a key press event, and if the key is "q", then shut down the network
127 elseif (eventType == "key_up") then
128 local key = event[2]
129
130 onKeyUp(key)
131 elseif (eventType == "terminate") then
132 onTerminate()
133 end
134end
135
136--Connection Opened Event Handler
137function onConnectionOpened(socket, server)
138 print("Connection opened" .. socket.target)
139
140 cryptoNET.send(socket, "Hello")
141end
142
143--Connection Closed Event Handler
144function onConnectionClosed(socket, server)
145 print("Connection closed" .. socket.target)
146end
147
148--Encrypted Message Received Event Handler
149function onEncryptedMessageReceived(message, socket, server)
150 --print("Encrypted message received" .. message .. socket.target)
151
152 if (connectionEstablished) then
153 --The server is successfully connected, so we can process instructions
154
155 --If the message is a request for intention, then send an intention message along with the intention
156 if (message == "get_intention") then
157 local message = {
158 type = "intention",
159 message = config.intention
160 }
161
162 cryptoNET.send(socket, message)
163
164 --If the message is "disconnect", then disconnect from the server
165 elseif (message == "disconnect") then
166 print ("Disconnecting from server")
167
168 reportStatus("disconnected")
169
170 cryptoNET.logout(socket)
171
172 cryptoNET.close(socket)
173
174 connectionEstablished = false
175 activated = false
176
177 --If the message is "activate", then activate the system
178 elseif (message == "activate") then
179 print("Activating system")
180 activated = true
181
182 --Send a message to the host controller to report system status
183 reportStatus("ready")
184 end
185 end
186
187end
188
189--Plain Message Received Event Handler
190function onPlainMessageReceived(message, socket, server)
191 --print("Plain message received" .. message .. socket.target)
192end
193
194--Login Event Handler
195function onLogin(username, socket, server)
196 print("Successfully logged in to Host as " .. config.systemUsername)
197 connectionEstablished = true
198end
199
200--Login Failed Event Handler
201function onLoginFailed(username, socket, server)
202 print("Failed to log in to Host as " .. config.systemUsername)
203 connectionEstablished = false
204end
205
206--Logout Event Handler
207function onLogout(username, socket, server)
208 print("Logged out of Host.")
209 connectionEstablished = false
210end
211
212--Key Up Event Handler
213function onKeyUp(key)
214 if (key == keys.q) then
215 reportStatus("shutting down")
216 print("Q key pressed, shutting down server")
217 reportStatus("disconnected")
218 cryptoNET.closeAll()
219
220 os.shutdown()
221 end
222end
223
224--Terminate Event Handler
225function onTerminate()
226 print("Terminate event raised, shutting down server")
227 cryptoNET.closeAll()
228
229 os.shutdown()
230end
231
232--endregion
233
234--region Configuration Reading
235function readConfigurationFile(file)
236 --Read the configuration file
237 local configFile = fs.open(file, "r")
238
239 --The configuration file is a JSON file, so we can use the JSON API to decode it
240
241 --The config is in the following format:
242 --{
243 -- server,
244 -- systemName,
245 -- systemUsername,
246 -- systemPassword,
247 -- intention
248 --}
249 local rawText = configFile.readAll()
250 local config = textutils.unserializeJSON(rawText)
251
252 print("Raw config: " .. rawText)
253
254 configFile.close()
255
256 --Check that the config is valid
257 if (config == nil) then
258 error("Invalid configuration file")
259 end
260
261 --Check that the server is valid
262 if (config.server == nil) then
263 error("Invalid server")
264 end
265
266 --Check that the system name is valid
267 if (config.systemName == nil) then
268 error("Invalid system name")
269 end
270
271 --Check that the system username is valid
272 if (config.systemUsername == nil) then
273 error("Invalid system username")
274 end
275
276 --Check that the system password is valid
277 if (config.systemPassword == nil) then
278 error("Invalid system password")
279 end
280
281 --Check that the intention is valid
282 if (config.intention == nil) then
283 error("Invalid intention")
284 end
285
286 return config
287end
288--endregion
289
290
291--region Main Code
292cryptoNET.startEventLoop(onNetworkStartup, onNetworkEventRaised)
293--endregion