Last active
January 30, 2018 10:53
-
-
Save DavidJRobertson/4392246 to your computer and use it in GitHub Desktop.
Stealify: pinch Spotify music! Run using node.js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stealify | |
// | |
// A wee HTTP proxy to steal MP3 files from https://play.spotify.com | |
// By David Robertson | |
console.log("Stealify started! Instructions:"); | |
console.log("1. Set http://localhost:8080 as your HTTP proxy"); | |
console.log("2. Go to http://play.spotify.com in your browser and start playing music"); | |
console.log("3. Wonder at all the stolen music appearing in this directory! Woo!"); | |
console.log("Remember to reverse the HTTP proxy settings when you're done :P\n"); | |
var fs = require('fs'), | |
url = require('url'), | |
net = require('net'), | |
http = require('http'); | |
process.on('uncaughtException', logError); | |
function logRequest(req) { | |
// Comment out/remove return to show debug stuffs :P | |
return | |
console.log(req.method + ' ' + req.url); | |
for (var i in req.headers) | |
console.log(' * ' + i + ': ' + req.headers[i]); | |
} | |
function logError(e) { | |
console.warn('*** ' + e); | |
} | |
var server = http.createServer(function(request, response) { | |
logRequest(request); | |
req_url = url.parse(request.url); | |
var options = { | |
hostname: req_url.hostname, | |
port: req_url.port, | |
path: req_url.path, | |
method: request.method | |
}; | |
var rq = http.request(options, function(res){ | |
var type = res.headers["content-type"]; | |
if (type == "audio/mpeg") { | |
var filename = String(Math.round(Date.now() / 1000)) + ".mp3" | |
var outstream = fs.createWriteStream(filename); | |
} | |
res.addListener('data', function(chunk) { | |
response.write(chunk, 'binary'); | |
if (outstream != null) { | |
outstream.write(chunk); | |
} | |
}); | |
res.addListener('end', function() { | |
response.end(); | |
if (outstream != null) { | |
outstream.end(); | |
console.log("Stole MP3! File: " + filename); | |
} | |
}); | |
response.writeHead(res.statusCode, res.headers); | |
}); | |
request.addListener('data', function(chunk) { | |
rq.write(chunk, 'binary'); | |
}); | |
request.addListener('end', function() { | |
rq.end(); | |
}); | |
}) | |
// Handle UPGRADE requests (for HTTPS) | |
server.on('upgrade', function(req, socket, head) { | |
logRequest(req); | |
var parts = req.url.split(':', 2); // URL is in the form 'hostname:port' | |
// Open a TCP connection to the remote host | |
var conn = net.connect(parts[1], parts[0], function() { | |
socket.write("HTTP/1.1 200 OK\r\n\r\n"); | |
// Create a tunnel between the two hosts | |
socket.pipe(conn); | |
conn.pipe(socket); | |
}); | |
}); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment