From 6660c1e2ddb2d85fa6522ce89ea23702606885b2 Mon Sep 17 00:00:00 2001 From: thomkeh Date: Fri, 17 Nov 2017 15:53:06 +0100 Subject: [PATCH] Code for basic structure of ONC file --- index.html | 49 +++++++++++++++++++++--------------------- js/converter.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++-- js/main.js | 29 +++++++++++++++++++++++++ js/parser.js | 4 ++-- js/scripts.js | 23 -------------------- 5 files changed, 111 insertions(+), 51 deletions(-) create mode 100644 js/main.js delete mode 100644 js/scripts.js diff --git a/index.html b/index.html index 9ffbe59..d79cb3f 100644 --- a/index.html +++ b/index.html @@ -1,28 +1,29 @@ - - - - OpenVPN to ONC - - - - - - - -
-

ovpn2onc

- - -
-
-

Output

- -
- + + + + OpenVPN to ONC + + + + + + + +
+

ovpn2onc

+ + +
+
+

Output

+ +
+ diff --git a/js/converter.js b/js/converter.js index 54c4123..5b8d75a 100644 --- a/js/converter.js +++ b/js/converter.js @@ -1,3 +1,56 @@ -export function convert(parsed) { - return {"test": "great content"} +/** + * Convert the parsed OpenVPN config to ONC. + */ + +const oncBasics = { + 'Type': 'UnencryptedConfiguration', + 'Certificates': [], + 'NetworkConfigurations': [] +} + +export function convert(name, ovpn) { + if (!ovpn.client) { + console.warn('Is this a server file?') + } + + // Check parameters + let params = {} + let remote = ovpn.remote.split(' ') + const host = remote[0] + if (remote[1]) { + params['Port'] = remote[1] + } + if (ovpn.proto) { + params['Proto'] = ovpn.proto + } + if (ovpn['tls-auth']) { + params['TLSAuthContents'] = convertKey(ovpn['tls-auth']) + } + + let config = { + 'GUID': `{${uuidv4()}}`, + 'Name': name, + 'Type': 'VPN', + 'VPN': { + 'Type': 'OpenVPN', + 'Host': host, + 'OpenVPN': params + } + } + let onc = Object.assign({}, oncBasics) // create copy + onc.NetworkConfigurations = [config] + return onc +} + +/** + * Create UUID (from Stackoverflow). + */ +function uuidv4() { + return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c=> + (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) + ) +} + +function convertKey(key) { + return key.replace(/\n/g, '\n') + '\n' } diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..55a5133 --- /dev/null +++ b/js/main.js @@ -0,0 +1,29 @@ +import {decode} from './parser.js' +import {convert} from './converter.js' + +let clickButton = document.getElementById('clickbutton') +clickButton.addEventListener('click', main, false) + +function main() { + let selectedFile = document.getElementById('inputopenvpn').files[0] + let connName = document.getElementById('connname').value + if (connName === '') { + alert('Please specify a name for the connection.') + return + } + console.log(selectedFile.size + ' bytes') + let reader = new FileReader() + // callback for when reader is done + reader.onload = (e => { + let content = e.target.result + // remove windows-style newlines + content = content.replace(/\r/g, '') + let parsed = decode(content) + console.log(parsed) + let onc = convert(connName, parsed) + let output = document.getElementById('output') + output.value = JSON.stringify(onc, null, 2) + }); + // start reading + reader.readAsText(selectedFile) +} diff --git a/js/parser.js b/js/parser.js index 4fc73c9..387328f 100644 --- a/js/parser.js +++ b/js/parser.js @@ -44,8 +44,8 @@ export function decode (str) { } function isQuoted (val) { - return (val.charAt(0) === '"' && val.slice(-1) === '"') || - (val.charAt(0) === "'" && val.slice(-1) === "'") + return ((val.charAt(0) === '"' && val.slice(-1) === '"') || + (val.charAt(0) === "'" && val.slice(-1) === "'")) } function unsafe (val, doUnesc) { diff --git a/js/scripts.js b/js/scripts.js deleted file mode 100644 index 9788cae..0000000 --- a/js/scripts.js +++ /dev/null @@ -1,23 +0,0 @@ -import {decode} from '/js/parser.js'; -import {convert} from '/js/converter.js'; - -let clickButton = document.getElementById('clickbutton'); -clickButton.addEventListener("click", main, false); - -function main() { - let selectedFile = document.getElementById('inputopenvpn').files[0]; - console.log(selectedFile.size + " bytes"); - let reader = new FileReader(); - reader.onload = (e => { - let content = e.target.result; - // remove windows-style newlines - content = content.replace(/\r/g, ""); - let parsed = decode(content); - console.log(parsed); - let onc = convert(parsed); - let output = document.getElementById('output'); - output.value = JSON.stringify(onc); - }); - // start reading - reader.readAsText(selectedFile); -}