Skip to content

Instantly share code, notes, and snippets.

@ImTheSquid
Created June 10, 2021 20:38
Show Gist options
  • Save ImTheSquid/626a66a3e9cbf49971b0b9d626242668 to your computer and use it in GitHub Desktop.
Save ImTheSquid/626a66a3e9cbf49971b0b9d626242668 to your computer and use it in GitHub Desktop.
ComputerCraft program to download files from a password-protected server
const express = require('express')
const app = express()
const fs = require('fs')
const path = require('path')
const port = 80
const secret = 'testing'
const safeDir = 'E:/Programming/Lua'
app.use('/luasync', (req, res) => {
if (req.headers.authorization == null) {
console.log('No secret')
res.status(400)
res.send('No secret given')
return
}
if (req.headers.authorization != secret) {
console.log('Invalid secret')
res.status(401)
res.send('Invalid secret')
return
}
if (!('file' in req.query)) {
console.log('No file')
res.status(400)
res.send('No file requested')
return
}
try {
const filePath = path.join(safeDir, req.query['file'])
console.log(filePath)
if (!fs.statSync(filePath).isFile()) {
console.log('Invalid path')
res.status(400)
res.send('Invalid path')
return
}
const file = fs.readFileSync(filePath)
res.download(filePath)
return
} catch (e) {
console.log(e)
res.status(404)
return
}
})
app.listen(port)
local target, key, target_file, local_file = ...
if target == nil or key == nil or target_file == nil then
print('Usage: sync <url> <key> <remote file> <local file>')
return
end
print('Attempting connection to ' .. target)
local res, err = http.checkURL(target)
if not res then
error(err)
end
print('Valid connection found. Sending sync request...')
res = http.get(target .. '/luasync?file=' .. target_file, {['Authorization'] = key})
if type(res) == 'string' then
error(res)
elseif res == nil then
error('Nil response')
end
print('Received response, writing file...')
-- Write file
local dir = shell.dir()
local file = fs.open(dir .. '/' .. local_file, 'w')
file.write(res.readAll())
file.close()
print('File written')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment