Browse Source

Add all code

master
stew3254 1 year ago
parent
commit
77ceb9e054
  1. 69
      coro.lua
  2. 11
      dhclient.lua
  3. 3
      dhcp-server.lua
  4. 3
      dns-server.lua
  5. 68
      ftp-server.lua
  6. 143
      ftp.lua
  7. 73
      get-all.lua
  8. 65
      grab-run.lua
  9. 5
      lib/async.lua
  10. 155
      lib/dhcp.lua
  11. 257
      lib/dns.lua
  12. 109
      lib/liblua.lua
  13. 214
      lib/net.lua
  14. 0
      lib/slice.lua
  15. 16
      lib/split.lua

69
coro.lua

@ -0,0 +1,69 @@
local coroutine = require("coroutine")
local net = require("net")
local table = require("table")
function eventLoop(...)
-- Put in here in for later usage
local done = {}
-- Get running coroutines
local running = {...}
local canRun = true
local i = 1
while canRun do
while coroutine.status(running[i]) == "dead" do
table.insert(done, #done+1, table.remove(running, i))
if #running == 0 then
goto done
end
end
if #running == 0 then
canRun = false
else
_, pkt = coroutine.resume(running[i])
if pkt ~= nil then print("Received: "..pkt.msg) end
i = (i % #running) + 1
end
end
::done::
end
-- TODO finish this
function betterLoop(...)
-- Get running coroutines
local done = {}
local results = {}
local running = {...}
local canRun = true
local i = 1
while true do
local coro = running[i][1]
status, t = coroutine.resume(coro, slice(running[i], 2, #(running[i])))
if t ~= nil then
print(#t)
end
i = (i % #running) + 1
while coroutine.status(coro) == "dead" do
table.insert(results, #results+1, table.remove(running, i)) -- can do this better
if #running == 0 then
goto done
end
end
end
::done::
return results
end
function simple()
print("Foo")
coroutine.yield()
print("Done")
return "a"
end
local coro = coroutine.create(simple)
betterLoop({coro})

11
dhclient.lua

@ -0,0 +1,11 @@
local io = require("io")
local serial = require("serialization")
local dhcp = require("dhcp")
local pkt = dhcp.query()
print("Received response from "..pkt.raddr)
print("Writing to /etc/resolv.cfg")
local f = io.open("/etc/resolv.cfg", "w")
f:write(serial.serialize(pkt.data[2],5))
f:close()

3
dhcp-server.lua

@ -0,0 +1,3 @@
local dhcp = require("dhcp")
dhcp.startServer()

3
dns-server.lua

@ -0,0 +1,3 @@
local dns = require("dns")
dns.startServer()

68
ftp-server.lua

@ -0,0 +1,68 @@
local component = require("component")
local event = require("event")
local fs = require("filesystem")
local io = require("io")
local shell = require("shell")
local m = component.proxy(component.list("modem")())
local os = require("os")
arg = {...}
function listen(m)
while true do
local _, _, from, port, _, msg = event.pull("modem_message")
print(e, from, port, msg, type(msg))
local pos = msg:find("%s")
local sub = msg:sub(pos+1)
--Get command and file name
local command = msg:sub(0, pos-1)
pos = sub:find("%s")
local file = ""
if pos ~= nil then
file = sub:sub(0, pos-1)
else
file = sub
end
os.sleep(.1)
if command == "ls" then
m.send(from, port, fs.list(file))
elseif command == "cd" then
m.send(from, port, shell.setPath(file))
elseif command == "pull" then
f = fs.open(file, "r")
if f == nil then
m.send(from, port, "ERROR")
else
m.send(from, port, "OK")
os.sleep(.1)
m.send(from, port, f.readAll())
f:close()
end
elseif command == "put" then
f = io.open(file, "w")
if pos == nil then
m.send(from, port, "ERROR")
else
f:write(sub:sub(pos+1))
f:close()
m.send(from, port, "OK")
end
else
m.send(from, port, "ERROR")
end
end
end
local port = 21
if arg[1] ~= nil then
port = tonumber(arg[1])
end
if m == nil then
print("No modems found")
return
end
if not m.isOpen(21) then
m.open(21)
end
listen(m)

143
ftp.lua

@ -0,0 +1,143 @@
local component = require("component")
local event = require("event")
local fs = require("filesystem")
local io = require("io")
local shell = require("shell")
local term = require("term")
local m = component.proxy(component.list("modem")())
args = {...}
function doCommand(server, port, cwd, scwd)
term.write(">> ")
input = io.read()
if input == "exit" or input == "bye" or input == "quit" then
print("Bye")
return
end
t = split(input)
if t[1] == "clear" then
shell.execute("clear")
elseif t[1] == "lls" then
local ls = {}
if t[2] == nil then
ls = fs.list(cwd)
else
ls = fs.list(cwd.."/"..t[2])
end
for _,v in ipairs(ls) do
print(v)
end
elseif t[1] == "ls" then
if t[2] == nil then
m.send(server, port, t[1].." "..scwd)
else
m.send(server, port, t[1].." "..scwd.."/"..t[2])
end
_, _, from, port, _, msg = event.pull("modem_message")
for _, v in ipairs(msg) do
print(v)
end
elseif t[1] == "lcd" then
if t[2] == nil then
cwd = "/"
else
if t[2]:gmatch(".") == "/" then
cwd = t[2]
--Didn't handle .. operator
else
if cwd == "/" then
cwd = cwd..t[2]
else
cwd = cwd.."/"..t[2]
end
end
end
elseif t[1] == "cd" then
if t[2] == nil then
scwd = "/"
else
if t[2]:gmatch(".") == "/" then
scwd = t[2]
--Didn't handle .. operator
else
if scwd == "/" then
scwd = scwd..t[2]
else
scwd = scwd.."/"..t[2]
end
end
end
elseif t[1] == "put" then
if t[2] == nil then
print("Please supply a file to put")
else
ls = fs.list(cwd)
--Bad search algorithm
isFile = false
for _,v in ipairs(ls) do
if v == t[2] then
isFile = true
end
end
if isFile then
if cwd == "/" then
file = cwd..t[2]
else
file = cwd.."/"..t[2]
end
f = fs.open(file, "r")
m.send(server, port, t[1].." "..file.." "..f.readAll())
_, _, from, port, _, msg = event.pull("modem_message")
print(msg)
else
print("File not found")
end
end
elseif t[1] == "pull" then
if t[2] == nil then
print("Please supply a file to pull")
else
if scwd == "/" then
file = scwd..t[2]
else
file = scwd.."/"..t[2]
end
m.send(server, port, t[1].." "..file)
_, _, from, port, _, msg = event.pull("modem_message")
if msg == "ERROR" then
print("File not found")
else
print(msg)
_, _, from, port, _, msg = event.pull("modem_message")
f = io.open(file, "w")
f:write(msg)
f:close()
end
end
end
doCommand(m, id, from, cwd, scwd)
end
if m == nil then
print("No modems found")
return
end
local port = 21
if args[1] ~= nil then
port = tonumber(args[1])
end
local server = nil
if args[2] == nil then
f = io.open("addr", "r")
server = f:read()
f:close()
else
server = args[2]
end
print("FTP CLIENT")
doCommand(server, port, shell.getPath(), "/")

73
get-all.lua

@ -0,0 +1,73 @@
local shell = require("shell")
local fs = require("filesystem")
local internet = require("internet")
local args = {...}
-- Split string based on delimiter
function split(s, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(s, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
-- Slice table
function slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl[i]
end
return sliced
end
-- Error checking
if args[1] == nil then
print("URL must exist")
return
end
local handle = internet.request(args[1])
local result = ""
for chunk in handle do result = result..chunk end
-- Print the body of the HTTP response
-- print(result)
-- Grab the metatable for the handle. This contains the
-- internal HTTPRequest object.
local mt = getmetatable(handle)
-- The response method grabs the information for
-- the HTTP response code, the response message, and the
-- response headers.
local code, message, headers = mt.__index.response()
print("code = "..tostring(code))
print("message = "..tostring(message))
-- Sandbox so we don't write to the screen
sandbox_env = {
assert = assert,
require = require,
pairs = pairs,
pcall = pcall,
tostring = tostring,
string = string,
io = {write = function(...) end, read = io.read, open = io.open, stderr = io.stderr}
}
-- pcall(fs.remove, fileName)
-- shell.execute("wget -f "..args[1].." "..fileName, sandbox_env)
-- -- Run downloaded file
-- local c = fileName
-- for _, i in ipairs(newArgs) do c = c.." "..i end
-- shell.execute(c)

65
grab-run.lua

@ -0,0 +1,65 @@
local shell = require("shell")
local internet = require("internet")
local fs = require("filesystem")
local args = {...}
-- Split string based on delimiter
function split(s, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(s, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
-- Slice table
function slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl[i]
end
return sliced
end
-- Error checking
if args[1] == nil then
print("URL must exist")
return
end
args[1] = "http://mc.bashed.rocks:13699/"..args[1]
-- Get local filename
local fileName = ""
if args[2] == nil then
local t = split(args[1], "/")
fileName = t[#t]
else
fileName = args[2]
end
local newArgs = slice(args, 3, #args-2)
-- Sandbox so we don't write to the screen
sandbox_env = {
assert = assert,
require = require,
pairs = pairs,
pcall = pcall,
tostring = tostring,
string = string,
io = {write = function(...) end, read = io.read, open = io.open, stderr = io.stderr}
}
pcall(fs.remove, fileName)
shell.execute("wget -f "..args[1].." "..fileName, sandbox_env)
-- Run downloaded file
local c = fileName
for _, i in ipairs(newArgs) do c = c.." "..i end
shell.execute(c)

5
lib/async.lua

@ -0,0 +1,5 @@
local coroutine = require("coroutine")
local async = {}
return async

155
lib/dhcp.lua

@ -0,0 +1,155 @@
local net = require("net")
local fs = require("filesystem")
local io = require("io")
local serial = require("serialization")
local dhcp = {
SERV_PORT = 67,
CLIENT_PORT = 68,
CONFIG_FILE = "/etc/dhcp/config"
}
-- Local function for creating directory
function createConfigDir(fileName)
local configsExist = true
local dir = fs.path(fileName)
if fs.exists(dir) and not fs.isDirectory(dir) then
fs.remove(dir)
end
if not fs.exists(dir) then
fs.makeDirectory(dir)
return true
end
return false
end
-- Gets an existing server config or creates one if it doesn't exist
function dhcp.getServerConfig(fileName)
if fileName == nil then fileName = dhcp.CONFIG_FILE end
local configsExist = true
if not createConfigDir(fileName) then
if fs.isDirectory(fileName) then
fs.remove(fileName)
end
if not fs.exists(fileName) then
configsExist = false
end
else
configsExist = false
end
if not configsExist then
print("Creating config since one doesn't exist")
local f = io.open(fileName, "w")
config = {dns = "", pxe = ""}
f:write(serial.serialize(config, 5))
f:close()
return config
else
f = io.open(fileName, "r")
local data = ""
while true do
local line = f:read()
if line ~= nil then
data = data..line
else
break
end
end
f:close()
return serial.unserialize(data)
end
end
function dhcp.startServer(servPort, clientPort, cacheConfig)
-- Make sure ports aren't invalid
if type(servPort) ~= "number" then
servPort = dhcp.SERV_PORT
end
if type(clientPort) ~= "number" then
clientPort = dhcp.CLIENT_PORT
end
-- Filter function for getting dhcp server messages
function dhcp.serverMsg(name, ...)
local t = {...}
if t[3] == servPort and
t[5] == "dhcp" and
t[6] == clientPort and
t[7] == "get" then
return true
end
return false
end
print("Starting up DHCP server")
if cacheConfig == nil then
cacheConfig = true
else
cacheConfig = false
end
-- Read from config file each time, or just read once and use that for life of execution
local config = dhcp.getServerConfig()
while true do
local pkt, err = net.recv{servPort, filter = dhcp.serverMsg}
if pkt == nil and err == "interrupted" then
print("Interrupted")
return
end
print("Received request from "..pkt.raddr)
-- os.sleep(1)
if cacheConfig then
net.send(pkt.raddr, clientPort, "dhcp", serial.serialize(config))
else
net.send(pkt.raddr, clientPort, "dhcp", serial.serialize(dhcp.getServerConfig()))
end
end
end
function dhcp.query(servPort, clientPort)
-- Make sure ports aren't invalid
if type(servPort) ~= "number" then
servPort = dhcp.SERV_PORT
end
if type(clientPort) ~= "number" then
clientPort = dhcp.CLIENT_PORT
end
-- Filter function for getting dhcp client messages
function dhcp.clientMsg(name, ...)
local t = {...}
if t[3] ~= clientPort or t[5] ~= "dhcp" then
return false
elseif type(t[6]) ~= "string" then
return false
elseif serial.unserialize(t[6]) == nil then
return false
else
return true
end
end
net.broadcast(servPort, "dhcp", clientPort, "get")
pkt = net.recv{clientPort, timeout = 3, filter = dhcp.clientMsg}
return pkt
end
-- dhcp.startServer()
-- wget -f http://localhost:8080/dhcp.lua /usr/lib/dhcp.lua
-- wget -f http://localhost:8080/net.lua /usr/lib/net.lua
return dhcp

257
lib/dns.lua

@ -0,0 +1,257 @@
local component = require("component")
local event = require("event")
local fs = require("filesystem")
local io = require("io")
local serial = require("serialization")
local split = require("split")
local slice = require("slice")
local modem = component.proxy(component.list("modem")())
local dns = {
PORT = 53,
RECORDS = "/etc/dns/records"
}
-- Local function for creating directory
function createRecordsDir(fileName)
local configsExist = true
local dir = fs.path(fileName)
if fs.exists(dir) and not fs.isDirectory(dir) then
fs.remove(dir)
end
if not fs.exists(dir) then
fs.makeDirectory(dir)
return true
end
return false
end
-- Gets an existing server config or creates one if it doesn't exist
function dns.getRecords(fileName)
if fileName == nil then fileName = dns.RECORDS end
local recordsExist = true
if not createRecordsDir(fileName) then
if fs.isDirectory(fileName) then
fs.remove(fileName)
end
if not fs.exists(fileName) then
recordsExist = false
end
else
recordsExist = false
end
records = {}
if not recordsExist then
print("Creating records file since it doesn't exist")
local f = io.open(fileName, "w")
f:close()
return records
else
f = io.open(fileName, "r")
while true do
local line = f:read()
if line ~= nil then
local t = split(line)
-- Records file format: <address> <host>>
records[t[2]] = t[1]
else
break
end
end
f:close()
end
return records
end
-- Helper to get a single record
function getRecord(data, records)
if records == nil then
records = dns.getRecords()
end
-- Assume most people will resolve not reverse, so resolve is fast, reverse is slow
local record = nil
if data[3] == "resolve" then
record = records[data[4]]
else
for k, v in pairs(records) do
if data[4] == v then
record = k
end
end
end
return record
end
function dns.startServer(port, cacheRecords)
-- Make sure ports aren't invalid
if type(port) ~= "number" then
port = dns.PORT
end
-- Remember to open the port
modem.open(port)
-- Filter function for getting dns server messages
function dns.serverMsg(name, ...)
local t = {...}
-- Check for interrupt
if name == "interrupted" then return true end
-- Check proper start to client dns query
if name ~= "modem_message" or t[3] ~= port or t[5] ~= "dns" or t[6] ~= "client" then
return false
end
-- Check resolve or reverse
if t[7] ~= "resolve" and t[7] ~= "reverse" then
return false
end
-- Make sure it's at least of type string if not going to check valid uuid
if type(t[8]) ~= "string" then
return false
end
return true
end
print("Starting up DNS server")
local records = nil
if cacheRecords == nil or cacheRecords then
records = dns.getRecords()
end
while true do
local pkt = {event.pullFiltered(dns.serverMsg)}
if pkt[1] == "interrupted" then
print("Interrupted")
return
end
local raddr, port = pkt[3], pkt[4]
local data = slice(pkt, 6)
print("Received request for "..data[4].." from "..raddr)
modem.send(raddr, port, "dns", "server", "record", getRecord(data, records))
end
end
-- local helper function for doing resolving
function query(host, useCache, queryType)
if useCache == nil then
useCache = true
end
if queryType == nil then
queryType = "resolve"
end
record = nil
if useCache then
data = {"dns", "client", queryType, host}
record = getRecord(data)
if record ~= nil then
return record
end
end
-- Make sure ports aren't invalid
local port = dns.PORT
-- Don't forget to open the port
modem.open(port)
-- Filter function for getting dns client messages
function dns.clientMsg(name, ...)
local t = {...}
-- Check proper start to server dns response
if name ~= "modem_message"
or t[3] ~= port
or t[5] ~= "dns"
or t[6] ~= "server"
or t[7] ~= "record" then
return false
end
-- Make sure it's at least of type string if not going to check valid uuid
if type(t[8]) ~= "string" and t[8] ~= nil then
return false
end
return true
end
-- Throw error if file can't be opened
local f, err = io.open("/etc/resolv.cfg", "r")
if f == nil then
return nil, err
end
data = ""
while true do
local line = f:read()
if line ~= nil then
data = data..line
else
break
end
end
f:close()
local servAddr = serial.unserialize(data).dns
if servAddr == "" then
return nil, "No DNS server specified\nPlease edit /etc/resolv.cfg or set one on your dhcp server and run dhclient again"
end
modem.send(servAddr, port, "dns", "client", queryType, host)
-- Add timeout so we don't wait forever
local timeout = 3
-- Make sure to not listen to random people and only the server requested to
local pkt = nil
repeat
pkt = {event.pullFiltered(timeout, dns.clientMsg)}
if #pkt == 0 then
return nil, "Server timeout"
end
until (pkt[3] == servAddr)
-- Return the record
local data = slice(pkt, 6)
if #data == 4 then
record = data[4]
end
f = io.open(dns.RECORDS, "a")
if queryType == "resolve" then
f:write(record.."\t"..host.."\n")
else
f:write(host.."\t"..record)
end
f:close()
return record
end
-- Look up a record
function dns.query(host, useCache) return query(host, useCache, "resolve") end
-- Reverse look up a record
function dns.revQuery(host, useCache) return query(host, useCache, "reverse") end
-- dns.startServer()
-- wget -f http://localhost:8080/lib/dns.lua /usr/lib/dns.lua
return dns

109
lib/liblua.lua

@ -0,0 +1,109 @@
#!/bin/lua
local string = require("string")
local table = require("table")
local ll = {}
-- Split a string
function ll.split(s, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(s, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
split = ll.split
-- Slice table
function ll.slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl[i]
end
return sliced
end
slice = ll.slice
-- Map function
function ll.map(f, ...)
local args = {...}
local ret = {}
for i = 1, #args[1] do
local t = {}
for j = 1, #args do
table.insert(t, #t+1, args[j][i])
end
table.insert(ret, #ret+1, f(table.unpack(t)))
end
return ret
end
map = ll.map
-- Used to apply filters to a regular list
function ll.filter(f, l)
local ret = {}
for _,v in ipairs(l) do
table.insert(ret, #ret+1, f(v) and v or nil)
end
return ret
end
filter = ll.filter
-- Returns true is something is in the sequence
function liblua.any(seq)
for i, v in ipairs(seq) do
if v then
return true
end
end
return false
end
any = liblua.any
-- TODO finish this
-- Reduce a regular list
-- function ll.reduce(f, l, init)
-- local start = 1
-- if init == nil then
-- init = l[1]
-- start = 2
-- end
-- local agg = nil
-- for i = start, #l-1 do
-- if l[i]
-- end
-- for _,v in ipairs(t) do
-- table.insert(ret, #ret+1, f(v) and v or nil)
-- end
-- return ret
-- end
-- Simple table printing. Does not do recursive tables
function ll.tprint(...)
local t = {...}
for i = 1, #t do
for k,v in pairs(t[i]) do
print(k,v)
end
end
end
ll.tprint(ll.filter(
function(a)
if a > 5 then
return a
else
return nil
end
end, {1,4,8,3,12,10}))
return ll

214
lib/net.lua

@ -0,0 +1,214 @@
local computer = require("computer")
local component = require("component")
local coroutine = require("coroutine")
local event = require("event")
local dns = require("dns")
local serial = require("serialization")
local table = require("table")
local string = require("string")
local modem = component.proxy(component.list("modem")())
local args = {...}
-- Take some existing modem functions and data
local net = {
addr = modem.address,
type = modem.type,
open = modem.open,
isOpen = modem.isOpen,
close = modem.close,
isWireless = modem.isWireless,
maxPacketSize = modem.maxPacketSize,
}
-- Used to determine if a string is a proper uuid
function isUUID(s)
local pattern = {8,4,4,4,12}
local t = split(s, "-")
for i, v in ipairs(t) do
if #v ~= pattern[i] or tonumber(v, 16) == nil then
return false
end
end
return true
end
-- Resolve addresses if they aren't UUIDs
function checkAddr(addr)
if not isUUID(addr) then
return dns.query(addr)
end
return addr
end
-- Close all ports
function net.closeAll()
for i = 1, 65535 do
net.close(i)
end
end
-- Strength getter and setter
function net.strength(n)
if n ~= nil then
modem.setStrength(n)
else
return modem.getStrength()
end
end
-- Wake mesage getter and setter
function net.wakeMsg(msg, fuzzy)
if msg ~= nil then
modem.setWakeMessage(msg, fuzzy)
else
return modem.getWakeMessage()
end
end
-- Send network packets
-- Auto serializes data for you
function net.send(addr, port, ...)
addr = checkAddr(addr)
local arg = table.pack(...)
for k, v in ipairs(arg) do
-- Only serialize tables
if type(v) == "table" then
arg[k] = serial.serialize(v)
end
end
modem.send(addr, port, table.unpack(arg))
end
-- Broadcast network packets
-- Auto serializes data for you
function net.broadcast(port, ...)
local arg = table.pack(...)
for k, v in ipairs(arg) do
-- Only serialize tables
if type(v) == "table" then
arg[k] = serial.serialize(v)
end
end
modem.broadcast(port, table.unpack(arg))
end
-- Custom function to receive network packets
-- Can specify a port and or address for simple filtering
-- Can also supply a filter function in addition to simple filtering
-- If port supplied, default action is to open the port for you
-- If nonblocking, uses coroutine.yield() do be prepared to handle that
function net.recv(t)
local pkt = {}
local tmp = {}
-- This allows deafault and keyword args
if t ~= nil then
setmetatable(t,{__index={
port = nil,
addr = nil,
fuzzy = false,
timeout = nil,
nonblocking = false,
filter = nil,
open = true,
}})
local port, addr, timeout, nonblocking, filter, open =
t[1] or t.port,
t[2] or t.addr,
t[3] or t.timeout,
t[4] or t.nonblocking,
t[5] or t.filter,
t[6] or t.open
if addr ~= nil then
addr = checkAddr(addr)
end
-- Automatically open port to listen on
-- NOTE: Only works on a single port, if the filter function listens on
-- multiple you'll have to do that manually
if open and net ~= nil then net.open(port) end
----------------
-- Basic filter function used to call user defined filter functions
function f(name, ...)
-- Get rid of all non modem messages
if name == "interrupted" then return true end
if name ~= "modem_message" then return false end
local arg = {...}
local resAddress, resPort = arg[2], arg[3]
-- Check port is good
if port ~= nil and resPort ~= port then
return false
end
-- Check addr is good
if resAddr ~= nil and resAddr == addr then
return false
end
-- Do further filtering
if filter ~= nil then
return filter(name, ...)
else
return true
end
end
----------------
-- Supply timeout
if timeout ~= nil then
if nonblocking then
local start = computer.uptime()
repeat
coroutine.yield()
tmp = {event.pullFiltered(.05, f)}
until (#tmp > 0 or computer.uptime() - start >= timeout)
else
tmp = {event.pullFiltered(timeout, f)}
end
else
if nonblocking then
repeat
coroutine.yield()
tmp = {event.pullFiltered(.05, f)}
until (#tmp > 0)
else
tmp = {event.pullFiltered(f)}
end
end
end
-- Check for non-packets
if #tmp == 0 then
return nil, "timeout"
elseif tmp[1] == "interrupted" then
return nil, "interrupted"
end
-- Create basis of packet
pkt.laddr, pkt.raddr, pkt.port, pkt.distance, pkt.msg = tmp[2], tmp[3], tmp[4], tmp[5], tmp[6]
local data = {}
for i = 6, #tmp do
-- Unserialize any serialized data
if type(tmp[i]) == "string" then
status, res = pcall(serial.unserialize, tmp[i])
if status and res == nil then data[i-5] = tmp[i] else data[i-5] = res end
else
data[i-5] = tmp[i]
end
end
pkt.data = data
return pkt
end
-- wget -f http://localhost:8080/lib/net.lua /usr/lib/net.lua
-- wget -f http://mc.bashed.rocks:13699/lib/net.lua /usr/lib/net.lua
return net

0
lib/slice.lua

16
lib/split.lua

@ -0,0 +1,16 @@
local string = require("string")
local table = require("table")
function split(s, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(s, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
return split
Loading…
Cancel
Save