forked from cadergator10/Opencomputers-servertine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serpAPI.lua
199 lines (177 loc) · 4.92 KB
/
serpAPI.lua
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
local version = "4.0.0"
--testR = true
local serp = {}
local modemPort = 1000
local syncPort = 199
local diagPort = 180
local component = require("component")
local event = require("event")
local ser = require("serialization")
local term = require("term")
local process = require("process")
local modem = component.modem
local link
local varSettings = {}
local query
local extraConfig = {}
--------TableToFile
local function saveTable( tbl,filename )
local tableFile = assert(io.open(filename, "w"))
tableFile:write(ser.serialize(tbl))
tableFile:close()
end
local function loadTable( sfile )
local tableFile = io.open(sfile)
if tableFile ~= nil then
return ser.unserialize(tableFile:read("*all"))
else
return nil
end
end
--------Base Functions
local function convert( chars, dist, inv )
return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
end
local function crypt(str,k,inv)
local enc= "";
for i=1,#str do
if(#str-k[5] >= i or not inv)then
for inc=0,3 do
if(i%4 == inc)then
enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
break;
end
end
end
end
if(not inv)then
for i=1,k[5] do
enc = enc .. string.char(math.random(32,126));
end
end
return enc;
end
local function splitString(str, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
str:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
local function send(label,port,linker,...) --Pingme
if linker and link ~= nil then
link.send(modem.address,...)
return
end
if label then
modem.send(label,port,...)
else
modem.broadcast(port,...)
end
end
--------Called Functions
local function update(_, localAddress, remoteAddress, port, distance, msg, data)
if msg == "deviceCheck" then
send(nil,modemPort,true,"true")
end
end
function serp.setup(doorTable,queryTable)
local e
local fill = io.open("extraConfig.txt", "r")
if fill ~= nil then
io.close(fill)
else
local config = {}
config.cryptKey = {}
term.clear()
print("First Time Config Setup: Would you like to use default cryptKey? 1 for yes, 2 for no")
local text = term.read()
if tonumber(text) == 2 then
print("there are 5 parameters, each requiring a number. Recommend doing 1 digit numbers")
for i=1,5,1 do
print("enter param " .. i)
text = term.read()
config.cryptKey[i] = tonumber(text)
end
else
config.cryptKey = {1,2,3,4,5}
end
config.type = "single"
config.num = 2
config.version = version
modem.open(syncPort)
modem.broadcast(syncPort,"syncport")
local e,_,_,_,_,msg = event.pull(1,"modem_message")
modem.close(syncPort)
if e then
config.port = tonumber(msg)
else
print("What port is the server running off of?")
local text = term.read()
config.port = tonumber(text:sub(1,-2))
term.clear()
end
saveTable(config,"extraConfig.txt")
end
extraConfig = loadTable("extraConfig.txt")
modemPort = extraConfig.port
if component.isAvailable("tunnel") then
link = component.tunnel
modem.close(modemPort)
else
modem.open(modemPort)
end
send(nil,modemPort,true,"getquery",ser.serialize(queryTable))
e,_,_,_,_,query = event.pull(3,"modem_message")
if e == nil then
print("Failed query. Is the server on?")
os.exit()
end
query = ser.unserialize(crypt(query,extraConfig.cryptKey,true))
if query.num ~= 3 then
print("Server is not 3.0.0 and up")
os.exit()
end
term.clear()
fill = doorTable
fill["type"] = doorTable.type or "custom"
send(nil,modemPort,true,"setdevice",crypt(ser.serialize(fill),extraConfig.cryptKey))
local got, _, _, _, _, fill = event.pull(2, "modem_message")
if got then
varSettings = ser.unserialize(crypt(fill,extraConfig.cryptKey,true))
else
print("Failed to receive confirmation from server")
os.exit()
end
got = nil
event.listen("modem_message", update)
process.info().data.signal = function(...)
print("caught hard interrupt")
event.ignore("modem_message", update)
os.exit()
end
return query
end
function serp.crypt(str,reverse)
return true,crypt(str,extraConfig.cryptKey,reverse)
end
function serp.save(table,location)
saveTable(table,location)
end
function serp.load(location)
return loadTable(location)
end
function serp.send(wait,...)
send(nil,modemPort,true,...)
if wait then
local e, _, _, _, _, msg,msg2 = event.pull(3, "modem_message")
if e then
return true,msg,msg2
else
return false,"timed out"
end
else
return true,"no return requested"
end
return false, "unknown error"
end
return serp