Browse Source

Hopefully fixed library

master
BuildTools 1 year ago
parent
commit
4f3fd51c6b
  1. 114
      get-all.lua
  2. 33
      grab-run.lua
  3. 49
      lib/async.lua
  4. 15
      lib/dhcp.lua
  5. 28
      lib/dns.lua
  6. 17
      lib/net.lua

114
get-all.lua

@ -1,11 +1,22 @@
local shell = require("shell")
local fs = require("filesystem")
local internet = require("internet")
-- local ll = require("liblua")
local args = {...}
-- Split string based on delimiter
function split(s, sep)
-- Error checking
if args[1] == nil then
print("URL must exist")
return
end
-- Finish this off
local function strip(s, p)
return string.gsub(s, string.format("(^(%s+))|((%s+)$)", p, p))
end
local function split(s, sep)
if sep == nil then
sep = "%s"
end
@ -17,57 +28,68 @@ function split(s, sep)
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]
local function fixURL(url)
if not string.match(url, "/$") and not string.match(url, "\\.") then
url = url.."/"
end
return sliced
return url
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)
local function makeRequest(url)
local handle = internet.request(url)
local result = ""
for chunk in handle do result = result..chunk end
-- Grab the metatable for the handle. This contains the
-- internal HTTPRequest object.
local mt = getmetatable(handle)
-- 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()
-- 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))
if code ~=200 then
return nil, "Bad code received"
end
return url, result
end
local function getFiles(url, result)
for i in string.gmatch(result, "href=\".-\"") do
local dir = split(i, "\"")[2]
if string.match(dir, ".lua$") then
shell.execute("wget -f "..url..dir)
goto continue
end
if string.match(dir, "/$")
and not string.match(dir, ".git")
and not string.match(dir, ".vscode")
then
dir = dir
-- Remove any weird files
if fs.exists(dir) and not fs.isDirectory(dir) then
fs.remove(dir)
end
-- 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}
}
-- Make missing dir
if not fs.exists(dir) then
fs.makeDirectory(dir)
end
-- Recurse
shell.setWorkingDirectory(dir)
print(shell.getWorkingDirectory())
getFiles(makeRequest(url..dir))
end
::continue::
end
end
-- pcall(fs.remove, fileName)
-- shell.execute("wget -f "..args[1].." "..fileName, sandbox_env)
-- Grab all files on the server
local path = shell.getWorkingDirectory()
getFiles(makeRequest(fixURL(args[1])))
fs.setDirectory(path)
-- -- Run downloaded file
-- local c = fileName
-- for _, i in ipairs(newArgs) do c = c.." "..i end
-- shell.execute(c)function split(s, sep)
-- wget -f http://mc.bashed.rocks:13699/get-all.lua

33
grab-run.lua

@ -1,33 +1,10 @@
local shell = require("shell")
local internet = require("internet")
local fs = require("filesystem")
local ll = require("liblua")
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")
@ -38,12 +15,12 @@ args[1] = "http://mc.bashed.rocks:13699/"..args[1]
-- Get local filename
local fileName = ""
if args[2] == nil then
local t = split(args[1], "/")
local t = ll.split(args[1], "/")
fileName = t[#t]
else
fileName = args[2]
end
local newArgs = slice(args, 3, #args-2)
local newArgs = ll.slice(args, 3, #args-2)
-- Sandbox so we don't write to the screen
sandbox_env = {
@ -62,4 +39,6 @@ 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)
shell.execute(c)
-- wget -f http://mc.bashed.rocks:13699/grab-run.lua

49
lib/async.lua

@ -7,15 +7,21 @@ local table = require("table")
local async = {}
-- TODO FINISH THIS
-- An async wrapper for event.pullFiltered
function async.pullFiltered(...)
-- Check if we're in a coroutine or not
_, status = coroutine.running()
local start = computer.uptime()
local args = {...}
local blocking = nil
-- Check if we're in a coroutine or not
if type(args[#args]) == "boolean" and args[#args] then
blocking = true
else
blocking = false
end
--------------------
-- Break on interrupts
function f(name, ...)
-- if args[1] happens to be "interrupted" already the second won't trigger
@ -23,11 +29,11 @@ function async.pullFiltered(...)
return true
end
end
--------------------
if type(args[1]) == "string" then
-- Check if not in a coroutine
if not status then return event.pullFiltered(f, ...) end
print("Testing")
if blocking then return event.pullFiltered(f, ...) end
repeat
coroutine.yield()
@ -38,8 +44,10 @@ function async.pullFiltered(...)
if type(args[1]) == "number" then
timeout = args[1]
args = ll.slice({...}, 2)
end
-- For speed purposes check up here even though it's more repeated code
elseif type(args[1]) == "function" then
if type(args[1]) == "function" then
--------------------
-- Wrap other function so this will handle interrupts
function g(name, ...)
local a = ll.slice(args, 2)
@ -50,10 +58,11 @@ function async.pullFiltered(...)
-- Otherwise run other function
return args[1](name, ...)
end
--------------------
if timeout ~= nil then
-- Check if not in a coroutine
if not status then
if blocking then
return event.pullFiltered(timeout, g, table.unpack(args))
end
@ -63,7 +72,7 @@ function async.pullFiltered(...)
until #tmp > 0 or computer.uptime() - start >= timeout
else
-- Check if not in a coroutine
if not status then
if blocking then
return event.pullFiltered(g, ...)
end
@ -73,9 +82,10 @@ function async.pullFiltered(...)
until #tmp > 0
end
else
print(timeout, blocking)
if timeout ~= nil then
-- Check if not in a coroutine
if not status then
if blocking then
return event.pullFiltered(timeout, f, table.unpack(args))
end
@ -85,7 +95,7 @@ function async.pullFiltered(...)
until #tmp > 0 or computer.uptime() - start >= timeout
else
-- Check if not in a coroutine
if not status then
if blocking then
return event.pullFiltered(f, table.unpack(args))
end
@ -96,6 +106,9 @@ function async.pullFiltered(...)
end
end
end
if tmp == nil then
return nil
end
return table.unpack(tmp)
end
@ -103,10 +116,9 @@ end
async.pull = async.pullFiltered
-- Sleep in a coroutine friendly way
function async.sleep(s)
local _, status = coroutine.running()
function async.sleep(s, blocking)
-- If not a coroutine just regular sleep
if status then
if blocking then
os.sleep(s)
return
end
@ -136,7 +148,7 @@ local loop = {
function async.bundle(f, ...)
return {
coro = coroutine.create(f),
args = {...},
args = {...} or {},
results = {}
}
end
@ -156,19 +168,20 @@ function async.roundRobin(loop, ignoreError)
local i = 1
repeat
local b = loop.live[i]
print(b)
local r = {coroutine.resume(b.coro, table.unpack(b.args))}
local status, results = r[1], ll.slice(r, 2)
local blocking, results = r[1], ll.slice(r, 2)
-- Fail and exit if a function is bad
if not status and not ignoreError then
if blocking and not ignoreError then
io.stderr:write(results[1])
return
-- Don't exit but print the bad message
elseif not status then
elseif blocking then
print(results[1])
end
-- Stop running dead coroutines and add the results
if coroutine.status(b.coro) == "dead" then
if coroutine.blocking(b.coro) == "dead" then
b.results = results
table.insert(loop.dead, #loop.dead+1, table.remove(loop.live, i))
else

15
lib/dhcp.lua

@ -67,7 +67,7 @@ function dhcp.getServerConfig(fileName)
end
end
function dhcp.startServer(servPort, clientPort, cacheConfig)
function dhcp.startServer(servPort, clientPort, nonblocking, cacheConfig)
-- Make sure ports aren't invalid
if type(servPort) ~= "number" then
servPort = dhcp.SERV_PORT
@ -102,7 +102,7 @@ function dhcp.startServer(servPort, clientPort, cacheConfig)
local config = dhcp.getServerConfig()
while true do
local pkt, err = net.recv{servPort, filter = dhcp.serverMsg}
local pkt, err = net.recv{servPort, filter = dhcp.serverMsg, nonblocking = nonblocking}
if pkt == nil and err == "interrupted" then
print("Interrupted")
return
@ -117,7 +117,7 @@ function dhcp.startServer(servPort, clientPort, cacheConfig)
end
end
function dhcp.query(servPort, clientPort)
function dhcp.query(servPort, clientPort, nonblocking)
-- Make sure ports aren't invalid
if type(servPort) ~= "number" then
servPort = dhcp.SERV_PORT
@ -143,8 +143,13 @@ function dhcp.query(servPort, clientPort)
end
net.broadcast(servPort, "dhcp", clientPort, "get")
pkt = net.recv{clientPort, timeout = 3, filter = dhcp.clientMsg}
return pkt
pkt, err = net.recv{
clientPort,
timeout = 3,
filter = dhcp.clientMsg,
nonblocking = nonblocking
}
return pkt, err
end
-- dhcp.startServer()

28
lib/dns.lua

@ -1,10 +1,9 @@
local component = require("component")
local event = require("event")
local async = require("async")
local fs = require("filesystem")
local io = require("io")
local serial = require("serialization")
local split = require("split")
local slice = require("slice")
local ll = require("liblua")
local modem = component.proxy(component.list("modem")())
local dns = {
@ -58,7 +57,7 @@ function dns.getRecords(fileName)
while true do
local line = f:read()
if line ~= nil then
local t = split(line)
local t = ll.split(line)
-- Records file format: <address> <host>>
records[t[2]] = t[1]
else
@ -103,9 +102,6 @@ function dns.startServer(port, cacheRecords)
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
@ -132,13 +128,13 @@ function dns.startServer(port, cacheRecords)
end
while true do
local pkt = {event.pullFiltered(dns.serverMsg)}
local pkt = {async.pullFiltered(dns.serverMsg, not nonblocking)}
if pkt[1] == "interrupted" then
print("Interrupted")
return
end
local raddr, port = pkt[3], pkt[4]
local data = slice(pkt, 6)
local data = ll.slice(pkt, 6)
print("Received request for "..data[4].." from "..raddr)
modem.send(raddr, port, "dns", "server", "record", getRecord(data, records))
@ -146,7 +142,7 @@ function dns.startServer(port, cacheRecords)
end
-- local helper function for doing resolving
function query(host, useCache, queryType)
function query(host, useCache, queryType, nonblocking)
if useCache == nil then
useCache = true
end
@ -221,14 +217,14 @@ function query(host, useCache, queryType)
-- Make sure to not listen to random people and only the server requested to
local pkt = nil
repeat
pkt = {event.pullFiltered(timeout, dns.clientMsg)}
pkt = {async.pullFiltered(timeout, dns.clientMsg, not nonblocking)}
if #pkt == 0 then
return nil, "Server timeout"
end
until (pkt[3] == servAddr)
-- Return the record
local data = slice(pkt, 6)
local data = ll.slice(pkt, 6)
if #data == 4 then
record = data[4]
@ -246,9 +242,13 @@ function query(host, useCache, queryType)
end
-- Look up a record
function dns.query(host, useCache) return query(host, useCache, "resolve") end
function dns.query(host, useCache, nonblocking)
return query(host, useCache, "resolve", nonblocking)
end
-- Reverse look up a record
function dns.revQuery(host, useCache) return query(host, useCache, "reverse") end
function dns.revQuery(host, useCache, nonblocking)
return query(host, useCache, "reverse", nonblocking)
end
-- dns.startServer()

17
lib/net.lua

@ -6,6 +6,7 @@ local dns = require("dns")
local serial = require("serialization")
local table = require("table")
local string = require("string")
local ll = require("liblua")
local modem = component.proxy(component.list("modem")())
local args = {...}
@ -24,7 +25,7 @@ local net = {
-- Used to determine if a string is a proper uuid
function isUUID(s)
local pattern = {8,4,4,4,12}
local t = split(s, "-")
local t = ll.split(s, "-")
for i, v in ipairs(t) do
if #v ~= pattern[i] or tonumber(v, 16) == nil then
return false
@ -113,13 +114,15 @@ function net.recv(t)
timeout = nil,
filter = nil,
open = true,
nonblocking = false,
}})
local port, addr, timeout, filter, open =
local port, addr, timeout, filter, open, nonblocking =
t[1] or t.port,
t[2] or t.addr,
t[3] or t.timeout,
t[4] or t.filter,
t[5] or t.open
t[5] or t.open,
t[6] or t.nonblocking
if addr ~= nil then
addr = checkAddr(addr)
@ -158,9 +161,9 @@ function net.recv(t)
----------------
if timeout ~= nil then
tmp = {async.pullFiltered(timeout, f)}
tmp = {async.pullFiltered(timeout, f, not nonblocking)}
else
tmp = {async.pullFiltered(f)}
tmp = {async.pullFiltered(f, not nonblocking)}
end
end
@ -178,8 +181,8 @@ function net.recv(t)
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
nonblocking, res = pcall(serial.unserialize, tmp[i])
if nonblocking and res == nil then data[i-5] = tmp[i] else data[i-5] = res end
else
data[i-5] = tmp[i]
end

Loading…
Cancel
Save