From 292b03fdb6b414c2299ec560557105ad7f24f393 Mon Sep 17 00:00:00 2001 From: phnz Date: Tue, 16 Dec 2014 07:38:16 -0500 Subject: [PATCH] First commit --- .gitignore | 2 + bin/.gitkeep | 0 index.js | 0 lib/request.js | 20 ++++++++++ package.json | 18 +++++++++ scripts/openvpn | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 bin/.gitkeep create mode 100644 index.js create mode 100644 lib/request.js create mode 100644 package.json create mode 100755 scripts/openvpn diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79e1811 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +bin/open* diff --git a/bin/.gitkeep b/bin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 diff --git a/lib/request.js b/lib/request.js new file mode 100644 index 0000000..34afddb --- /dev/null +++ b/lib/request.js @@ -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); diff --git a/package.json b/package.json new file mode 100644 index 0000000..effd5f6 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/scripts/openvpn b/scripts/openvpn new file mode 100755 index 0000000..7accbeb --- /dev/null +++ b/scripts/openvpn @@ -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); + } +});