Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
phnz committed Dec 16, 2014
0 parents commit 292b03f
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bin/open*
Empty file added bin/.gitkeep
Empty file.
Empty file added index.js
Empty file.
20 changes: 20 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function() {
var config, configureRequest, loadNpm, npm, request;

request = require('request');


configureRequest = function(requestOptions, callback) {
requestOptions.headers = {};
};

module.exports = {
get: function(requestOptions, callback) {
return request.get(requestOptions, callback);
},
createReadStream: function(requestOptions, callback) {
return callback(request.get(requestOptions));
}
};

}).call(this);
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "node-builder",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"mv": "^2.0.3",
"request": "^2.51.0",
"runas": "^1.0.1",
"tar": "^1.0.3",
"temp": "^0.8.1"
}
}
97 changes: 97 additions & 0 deletions scripts/openvpn
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env node

var fs = require('fs');
var mv = require('mv');
var zlib = require('zlib');
var path = require('path');

var request = null;
request = require('../lib/request');

var tar = require('tar');
var temp = require('temp');

temp.track();

// OpenVPN Download Link
var config = {
win: {
x86: '',
x64: '',
}
}

var downloadFileToLocation = function (url, filename, callback) {
var stream = fs.createWriteStream(filename);
stream.on('end', callback);
stream.on('error', callback);
request.createReadStream({
url: url
}, function (requestStream) {
requestStream.pipe(stream);
});
};

var downloadTarballAndExtract = function (url, location, callback) {
var tempPath = temp.mkdirSync('vpnht-openvpn-');
var stream = tar.Extract({
path: tempPath
});
stream.on('end', callback.bind(this, tempPath));
stream.on('error', callback);
request.createReadStream({
url: url
}, function (requestStream) {
requestStream.pipe(zlib.createGunzip()).pipe(stream);
});
};

var copyToLocation = function (callback, version, targetFilename, fromDirectory) {
var arch = process.arch === 'ia32' ? 'x86' : process.arch;
var subDir = "node-" + version + "-" + process.platform + "-" + arch;
var fromPath = path.join(fromDirectory, subDir, 'bin', 'node');
return mv(fromPath, targetFilename, function (err) {
if (err) {
callback(err);
return;
}
fs.chmod(targetFilename, "755", callback);
});
};

var downloadOpenVPN = function (version, done) {
var arch, downloadURL, filename;
arch = process.arch === 'ia32' ? 'x86' : process.arch;

if (process.platform === 'win32') {
downloadURL = "http://nodejs.org/dist/" + version + "/" + arch + "node.exe";
filename = path.join('bin', "openvpn.exe");
} else {
downloadURL = "http://nodejs.org/dist/" + version + "/node-" + version + "-" + process.platform + "-" + arch + ".tar.gz";
filename = path.join('bin', "openvpn");
}

var downloadFile = function () {
// it's a EXE
if (process.platform === 'win32') {
downloadFileToLocation(downloadURL, filename, done);
} else if (process.platform === 'linux') {
var next = copyToLocation.bind(this, done, version, filename);
downloadTarballAndExtract(downloadURL, filename, next);
} else {
var next = copyToLocation.bind(this, done, version, filename);
downloadTarballAndExtract(downloadURL, filename, next);
}
};

downloadFile();
};

downloadOpenVPN('v0.10.33', function (error) {
if (error != null) {
console.error('Failed to download node', error);
return process.exit(1);
} else {
return process.exit(0);
}
});

0 comments on commit 292b03f

Please sign in to comment.