diff --git a/src/app.coffee b/src/app.coffee index 29d05a1e..19147e88 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -3,7 +3,6 @@ fs = Promise.promisifyAll(require('fs')) os = require 'os' knex = require './db' utils = require './utils' -{spawn} = require 'child_process' bootstrap = require './bootstrap' config = require './config' @@ -32,19 +31,10 @@ knex('config').select('value').where(key: 'uuid').then ([ uuid ]) -> api = require './api' application = require './application' supervisor = require './supervisor-update' + vpn = require './lib/vpn' console.log('Starting OpenVPN..') - openvpn = spawn('openvpn', [ 'client.conf' ], cwd: '/data') - - # Prefix and log all OpenVPN output - openvpn.stdout.on 'data', (data) -> - prefix = 'OPENVPN: ' - console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}")) - - # Prefix and log all OpenVPN output - openvpn.stderr.on 'data', (data) -> - prefix = 'OPENVPN: ' - console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}")) + setImmediate(vpn.connect) console.log('Starting API server..') api.listen(80) diff --git a/src/lib/vpn.coffee b/src/lib/vpn.coffee index d027e2a2..e111fa87 100644 --- a/src/lib/vpn.coffee +++ b/src/lib/vpn.coffee @@ -1,8 +1,10 @@ Promise = require 'bluebird' +_ = require 'lodash' csrgen = Promise.promisify require 'csr-gen' fs = Promise.promisifyAll require 'fs' request = Promise.promisifyAll require 'request' url = require 'url' +{spawn} = require 'child_process' exports.generate = (apiEndpoint, userConfig) -> # Generate SSL certificate @@ -49,4 +51,17 @@ exports.generate = (apiEndpoint, userConfig) -> fs.writeFileAsync('/data/ca.crt', body.ca) fs.writeFileAsync('/data/client.crt', body.cert) vpnConf - ]) \ No newline at end of file + ]) + +exports.connect = -> + openvpn = spawn('openvpn', [ 'client.conf' ], cwd: '/data') + + # Prefix and log all OpenVPN output + openvpn.stdout.on 'data', (data) -> + prefix = 'OPENVPN: ' + console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}")) + + # Prefix and log all OpenVPN output + openvpn.stderr.on 'data', (data) -> + prefix = 'OPENVPN: ' + console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}"))