Skip to content

Instantly share code, notes, and snippets.

@SoniEx2
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SoniEx2/f392c77ae81032bc295e to your computer and use it in GitHub Desktop.
Save SoniEx2/f392c77ae81032bc295e to your computer and use it in GitHub Desktop.
--[[ pastebin didn't like me so I'm moving it here ]]
--[[ code used to make Shocky run this remote file
.r sonilua <lua>local function runpage(pageurl) local page,f,l; page = {net.get(pageurl)}; f=loadstring(page[1]); l = {}; for k,v in pairs(_G) do l[k] = v; end; for k,v in pairs(getfenv()) do l[k] = v; end; setfenv(f,l); return f(); end; return runpage("https://gist.githubusercontent.com/SoniEx2/f392c77ae81032bc295e/raw/sonilua.lua");
--]]
--[[
This is public domain btw
--]]
if (not args) or (args == "") then
return;
end;
local function stuff(status, err, ...) --[[ This function does stuff ]]
if not status then --[[ if status == fail (false) ]]
error(err); --[[ error ]]
else --[[ else ]]
return err, ...; --[[ return stuff]]
end;
end;
local env;
local deep_copy;
do
local next,type,rawset,rawget,pcall = next,type,rawset,rawget,pcall
local gmt = debug and debug.getmetatable or getmetatable
local function trycopy(obj)
local mt = gmt(obj)
if type(mt) == "table" and rawget(mt, "__copy") then
return pcall(rawget(mt, "__copy"), obj)
else
return false
end
end
local function check(obj, todo, copies)
if copies[obj] ~= nil then
return copies[obj]
end
local status, copy = trycopy(obj)
if status then
copies[obj] = copy
return copy
end
if type(obj) == "table" then
local t = {}
todo[obj], copies[obj] = t, t
return t
end
return obj
end
function deep_copy(inp, copies)
local todo = todo or {}
local copies = type(copies) == "table" and copies or {}
local out = check(inp, todo, copies)
while next(todo) do
local i, o = next(todo)
todo[i] = nil
for k, v in next, i do
rawset(o, k, check(v, todo, copies))
end
end
return out
end
local copies = {[package] = (package)} --[[don't copy package]]
env = deep_copy(_G, copies);
local temp = deep_copy(getfenv(), copies);
for x,y in pairs(temp) do
rawset(env, x, y);
end;
end
do
--[[ setup env ]]
local function checktype(v, t, n) --[[ this function should not be put in env! ]]
assert(type(v) == t,"bad argument" .. (n and ("#" .. n .. " ") or "") .. ": " .. t.. " expected, got " .. type(v));
end
env.info = function() --[[ info function, returns some info about this program ]]
return "A small Lua sandbox for Shocky. Reports compile-time errors. Code: https://gist.github.com/SoniEx2/f392c77ae81032bc295e";
end;
env.pyformat = function(v)
if v then
getmetatable('').__mod = function(a,b) return string.format(a,unpack(b)); end;
else
getmetatable('').__mod = nil;
end;
end;
if not env.net then
env.net={};
end;
local net_load = function(url) --[[ netload function, loads remote files ]]
checktype(url, "string", 1);
local page = {net.get(url)}; --[[ net.get = http.get ]]
return loadstring(page[1], url);
end;
env.net.run = function(url, ...)
local f, err = net_load(url);
if not f then
return nil, err;
end;
local l = deep_copy(env, {[package] = (package)});
setfenv(f,l);
if pcall then
return pcall(f, ...);
else
return true, f(...);
end;
end;
env.net.preload = function(name, url)
local f, e = net_load(url);
if not f then error(("could not preload %s: %s"):format(name, e)); end;
package.preload[name] = f;
end;
local function escape(s)
s = s:gsub(".",function(c) return "\\x"..string.format("%X",string.byte(c)); end);
return s;
end;
local function tophparray(t, i)
local out = {}
if t then
checktype(t, "table", i);
for x,y in pairs(t) do
out[#out + 1] = "\"" .. escape(tostring(x)) .. "\"=>\"" .. escape(tostring(y)) .. "\",";
end;
end;
return "[" .. table.concat(out, "") .. "]"
end
env.net.POST = function(url, headers, data, options)
checktype(url, "string", 1);
local str_headers = tophparray(headers, 2);
local str_data = tophparray(data, 3);
local str_options = tophparray(options, 4);
local reqstring = [[
$req = Requests::post("%s",%s,%s,%s);
$newstring = '';
foreach(str_split($req->body) as $byte) {
$newstring .= '\\'.ord($byte);
};
/* sneakly use sprunge.us in production code for results */
$sprunge = Requests::post("http://sprunge.us/", array(), array("sprunge" => $req->body));
echo $sprunge->body;
]]
local phpcmd = reqstring:format(escape(url), str_headers, str_data, str_options);
local d = cmd.php(phpcmd);
local body, status, addr = net.get(d);
if env.debug then
return body, status, url, d;
else
return body, status, url;
end;
end;
env.net.GET = function(url, headers, options)
checktype(url, "string", 1);
local str_headers = tophparray(headers, 2);
local str_options = tophparray(options, 3);
local reqstring = [[
$req = Requests::get("%s",%s,%s);
$newstring = '';
foreach(str_split($req->body) as $byte) {
$newstring .= '\\'.ord($byte);
};
/* sneakly use sprunge.us in production code for results */
$sprunge = Requests::post("http://sprunge.us/", array(), array("sprunge" => $req->body));
echo $sprunge->body;
]]
local phpcmd = reqstring:format(escape(url), str_headers, str_options);
local d = cmd.php(phpcmd);
local body, status, addr = net.get(d);
if env.debug then
return body, status, url, d;
else
return body, status, url;
end;
end;
env.getfenv = function()
return env;
end;
env.setfenv = function(f,e)
setfenv(f,e)
end;
end;
local f,e = loadstring(args, sender); --[[ load stuff ]]
if not f then --[[ check for compile-time errors ]]
error(e); --[[ if any, error them ]]
else --[[ else ]]
setfenv(f, env) --[[ set the function environment ]]
return f(); -- [[ no pcall ]]
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment