Droplr Randomizer


Are you using any file sharing services? Are your files safe? Some time ago I was using droplr (a free version), and I noticed that the generated url's are short and easy to guess. I wrote a node.js script which was guessing some droplr url's, and decided to wrap it into a UI and publish. This article describes how droplr randomizer was created.


The application is built with Node.js and Express. It consists of few parts - http requests handler, random droplr url generator and the droplr requester, which makes sure the generated url exists.

The http handler is very simple, and consists only of two routes handling functions:

// main page request
app.get('/', function(req, res) {
	res.render('index', { title: 'Droplr Randomizer' });
});
 
// XHR request, sent when user clicks on "generate" button
app.get('/generate', function(req, res) {
	// calling start() function, which generates the link and checks it's status
	start(function(url, html) {
		if (++total_iter%5 == 0) {
			fs.writeFile('searches.count', total_iter); // save generation stats to file
		}
 
		res.json({url: url, html: html, total_iter: total_iter});
	})
});

The second, and the most interesting part of the application, is the start() function. As I said it generates a random droplr url, and checks if it exists. Public droplr url usually looks like http://d.pr/a/d8wm, so we are going to generate a random 4-character string, and use it with the shortened droplr domain.

function start(cbk) {
	// generate random 4-char string
	var url = Math.random().toString(36).substring(2,6);
 
	// calling the droplr checker function
	makeRequest(url, function(alive, url, content) {
		if (!alive) {
			setTimeout(function() {
				start(cbk); // if page doesn't exist - generate another url
			}, 
			100);
		}
		else {
			// if page exists - parse it with jsdom and output it's contents
			jsdom.env(
				content,
				["http://code.jquery.com/jquery.js"],
				function (errors, window) {
					var html = window.$("#viewport > section").html();
					window.close();
 
					if (html == undefined)
						start(cbk);
					else
						cbk(url, html);
				});
		}
	});
}

For parsing the page html I use jsdom.

The last part of the application is makeRequest() method. It does a http request to the generated page, and checks either it's alive or not.

function makeRequest(path, callback) {
	var opts = {
		host: "d.pr",
		path: "/a/" + path
	};
 
	var content = '';
 
	var req = http.request(opts, function(res) {
		res.on('data', function(chunk) {
			content += chunk;
		});
 
		// Check the http status code of the request
		res.on('end', function() {
			if (res.statusCode == 200) {
				callback(true, "http://d.pr/a/" + path, content);
				return;
			} else {
				callback(false, null, null);
			}
		});
	});
 
	req.end();
}

That's it. You can get the code on github. The demo is available here.

About

My name is Eugene. I am a passioned web developer, in love with Node.js, with over 6yrs of experience.

My main domain of expertise is full cycle application development. I love building robust scalable Node.js applications.

I create all kind of applications - websites, API servers, realtime applications.

Skills / Technologies
Node.js, MongoDB, Mongoose, Socket.io, Mocha, Grunt, Git, LAMP, C/C++, Nginx
Contact