· 5 years ago · Sep 21, 2020, 06:56 AM
1--[[
2Securenet Modem API
3Code (C) DuskTheDutchie, 2020
4This API intends to bring semi-secure comm power
5over modems at the low-level Modem API included
6with CC.
7Features twin modem support, intended for
8a wireless and wired modem.
9--]]
10
11--MessageFormat {header, data}
12--Header is a filtering thing
13
14netKey = nil
15
16modem1 = nil
17modem1Wireless = nil
18modem2 = nil
19modem2Wireless = nil
20
21openChannels = {}
22
23
24--Load a string utility API.
25--If it does not exist, download it from pastebin.
26if fs.exists("strutil") then
27 os.loadAPI("strutil")
28else
29 shell.run("pastebin get ppJ0ggSq strutil")
30 os.loadAPI("strutil")
31end
32
33--Check to see if we have a UUID-Key for secure
34--transmissions.
35if fs.exists("network.key") then
36 f = fs.open("network.key", "r")
37 netKey = f.readAll()
38 f.close()
39else
40 error("No network key detected. Install a network key before continuing.")
41end
42
43--If we are at this point, then we have the
44--string utility and designated network key.
45
46
47--Call this when program starts.
48--Autodetect the modems based on start.
49function detectModems()
50 for k,v in pairs(rs.getSides()) do
51 if peripheral.getType(v) == "modem" and not modem1 then
52 modem1 = peripheral.wrap(v)
53 modem1Wireless = modem1.isWireless()
54 elseif peripheral.getType(v) == "modem" and not modem2 then
55 modem2 = peripheral.wrap(v)
56 modem2Wireless = modem2.isWireless()
57 end
58 end
59end
60
61--Open a channel for listening on both modems.
62function openChannel(channelNum)
63 if #openChannels < 128 then
64 table.insert(openChannels, channelNum)
65 if not modem1 then
66 error("No modems detected.")
67 else
68 modem1.open(channelNum)
69 end
70 if modem2 then
71 modem2.open(channelNum)
72 end
73 end
74end
75
76--Close a channel for listening on both modems.
77function closeChannel(spec)
78 for i=1, #openChannels do
79 if openChannels[i] == spec then
80 if modem1 then
81 modem1.close(spec)
82 else
83 error("No modems detected.")
84 end
85 if modem2 then
86 modem2.close(spec)
87 end
88 end
89 end
90end
91
92--Close all open channels on both modems.
93function closeAllOpen()
94 for i=1, #openChannels do
95 if modem1 then
96 modem1.close(openChannels[i])
97 else
98 error("No modems detected.")
99 end
100 if modem2 then
101 modem2.close(openChannels[i])
102 end
103 end
104 openChannels = {}
105end
106
107--Send a message on the specified channel.
108
109function sendMessage(chaNum, respNum, msg, secure, wireless)
110 if not respNum or not chaNum then
111 error("Missing parameters: Channel Number or Response Number")
112 end
113
114 if secure == true then
115 msg = strutil.encrypt(msg, netKey)
116 end
117
118 --Transmit on the wireless modem.
119 if wireless == true then
120 if modem1Wireless == true then
121 modem1.transmit(chaNum, respNum, msg)
122 elseif modem2Wireless == true then
123 modem2.transmit(chaNum, respNum, msg)
124 end
125 else
126 --Transmit on the wired modem.
127 if modem1Wireless == false then
128 modem1.transmit(chaNum, respNum, msg)
129 elseif modem2Wireless == false then
130 modem2.transmit(chaNum, respNum, msg)
131 end
132 end
133end
134
135function receiveMessage(secure, timeout)
136 if timeout then
137 os.startTimer(timeout)
138 end
139 while true do
140 event, mSide, msgChannel, respChannel, msg = os.pullEvent()
141 if event == "modem_message" then
142 if secure then
143 msg = strutil.decrypt(msg, netKey)
144 return msg, msgChannel, respChannel, mSide
145 else
146 return msg, msgChannel, respChannel, mSide
147 end
148 elseif event == "timer" then
149 print("Timed out.")
150 return nil, nil, nil, nil
151 end
152 sleep(0.1)
153 end
154end
155