Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Created April 1, 2019 18:29
Show Gist options
  • Save DarkWiiPlayer/9de8d52e5a71699b0cff8b53f9d44609 to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/9de8d52e5a71699b0cff8b53f9d44609 to your computer and use it in GitHub Desktop.
#!/usr/bin/env luajit
local c = require 'ansicolors'
local pegasus = require 'pegasus'
local http = require 'socket.http'
local ip = http.request('http://darkwiiplayer.com/api/ip')
math.randomseed(os.time())
for i=1,5 do math.random() end
local server = pegasus:new({
port='9878',
location='root/'
})
local secret do
local chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
secret = ('.'):rep(30):gsub('.', function(char)
local num = math.random(#chars)
return chars:sub(num, num)
end)
end
local function hexch(char)
return ('{%02x}'):format(char:byte())
end
local function escapenl(line)
return (line:gsub('[\010\013]', hexch))
end
local function parsemultipart(form, boundary)
local res = {}
local t = {}
local header = true
for line in form:gmatch("[^\n]*\n") do
if header then
if line:find('^%s*$') then
header = false
elseif not line:find(boundary) then
local key, value = line:match('^([^:]*):(.*)\n?$')
t[key] = value
end
else
if line:find(boundary) then
header = true
table.insert(res, t); t = {}
else
t.content = table.concat(t)
table.insert(t, line)
end
end
end
return res
end
print('Port: ', c('%{green}'..server.port))
print('Secret URL: ', c('%{green}'..secret))
print('External IP:', c('%{green}'..ip))
print('Full URL: ', c('%{red}http://'..ip..':'..server.port..'/'..secret))
server:start(function (request, response)
print(c(os.date("%%{blue}%Y-%m-%d %%{blue}%H:%M:%S")), c('%{yellow}'..request:method()), c(' %{red}'..request:path()))
if request:path():find(secret) then
response:statusCode(200, 'Ok')
print(c('\t%{magenta}Valid secret provided!'))
if request:method() == "GET" then
response:addHeader('content-type', 'text/html;charset=utf8')
response:write([[
<style>
body { font-family: sans-serif; }
</style>
<h1>File Upload</h1>
<h2>Access Granted ♥</h2>
<form action="]]..secret..[[" method="post" enctype="multipart/form-data">
<input type="file" name="file"></input>
<input type="submit"></input>
</form>
]])
else
response:addHeader('content-type', 'text/html;charset=utf8')
local body if tonumber(request:headers()['Content-Length']) > 0 then
body = request:receiveBody()
end
if body then
print(c'\t%{green}Uploading file...')
local t = parsemultipart(body, request:headers()['Content-Type']:match('boundary=[^%s]*'):sub(10,-1))[1]
local buffer = {}
t.filename = t['Content-Disposition'] : match('filename=".-"') : sub(11, -2)
local file = io.open(t.filename, 'w')
file:write(t.content)
file:close()
print(c'\t%{blue}File Uploaded:', t.filename)
response:write([[
<h1>File Uploaded</h1>
<p>]]..t.filename..[[</p>
<p><a href="/]]..secret..[[">Upload another file</a></p>
]])
else
print(c'\t%{red}Empty request body!')
response:write('Request body is empty!')
end
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment