Skip to content

Instantly share code, notes, and snippets.

@leesalminen
Last active January 12, 2023 21:55
Show Gist options
  • Save leesalminen/75b9df03946d010c74403c6953f78123 to your computer and use it in GitHub Desktop.
Save leesalminen/75b9df03946d010c74403c6953f78123 to your computer and use it in GitHub Desktop.
torrent http proxy
FROM node:18
WORKDIR /workspace
COPY package.json /workspace
COPY index.js /workspace
RUN npm install
EXPOSE 8080
CMD [ "node" , "index.js" ]
import express from 'express'
import bb from 'express-busboy';
import WebTorrent from 'webtorrent-hybrid'
import parseTorrent from 'parse-torrent'
const app = express()
bb.extend(app, {
upload: true,
path: '/torrent-data/uploads',
})
const client = new WebTorrent()
app.get('/proxy.jpg', (req, res) => {
const magnetUri = req.query.magnetUri
const parsedTorrent = parseTorrent(magnetUri)
console.log('received request for ', parsedTorrent.infoHash)
const exists = client.torrents.find(torrent => torrent.infoHash === parsedTorrent.infoHash)
if(exists && exists.files && exists.files.length) {
res.sendFile(exists.path + '/' + exists.files[0].name)
return
}
client.add(magnetUri, {path: '/torrent-data/webtorrent/'}, (torrent) => {
console.log('downloading...')
torrent.on('done', () => {
res.sendFile(torrent.path + '/' + torrent.files[0].name)
})
})
})
app.post('/upload', (req, res) => {
const files = req.files
Object.keys(files).forEach((fileName) => {
const file = req.files[fileName]
console.log('start seeding ', file.file)
client.seed(file.file, (torrent) => {
console.log('we are seeding a new file!!', torrent.infoHash)
res.send('ok')
})
})
})
app.listen(8080)
console.log('Express started on port 8080')
{
"name": "webtorrent-server",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"express-busboy": "^9.0.0",
"parse-torrent": "^10.0.2",
"webtorrent": "^1.9.6",
"webtorrent-hybrid": "github:konsumer/webtorrent-hybrid"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment