1
0
mirror of https://github.com/ArcticFoxes-net/ONC-Converter synced 2024-11-09 05:41:33 -05:00

Improve documentation

This commit is contained in:
thomkeh 2018-07-09 12:03:02 +02:00 committed by GitHub
parent 1b76ad8fac
commit 583417f4da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,18 +42,18 @@
* The function is `async` because it uses `await` when reading files. * The function is `async` because it uses `await` when reading files.
* *
* @param {String} connName Name of the connection * @param {String} connName Name of the connection
* @param {File} selectedFile File object for the ovpn file * @param {File} ovpnFile File object for the ovpn file
* @param {Array} certificateFiles List of file objects for the certificates * @param {Array} certificateFiles List of file objects for the certificates
* @param {Object} output HTML element where the output should go * @param {Object} output HTML element where the output should go
*/ */
async function main (connName, selectedFile, certificateFiles, output) { async function main (connName, ovpnFile, certificateFiles, output) {
if (connName === '') { if (connName === '') {
alert('Please specify a name for the connection.') alert('Please specify a name for the connection.')
return return
} }
console.log(selectedFile.size + ' bytes') console.log(selectedFile.size + ' bytes')
let content = await readFile(selectedFile) let ovpnContent = await readFile(ovpnFile)
let [ovpn, keys] = decode(content) let [ovpn, keys] = parseOvpn(ovpnContent)
console.log(ovpn) console.log(ovpn)
for (const certificateFile of certificateFiles) { for (const certificateFile of certificateFiles) {
keys[certificateFile.name] = await readFile(certificateFile) keys[certificateFile.name] = await readFile(certificateFile)
@ -82,7 +82,7 @@
} }
/** /**
* Decode an OVPN file. Extract all the key-value pairs and keys * Parse an OVPN file. Extract all the key-value pairs and keys
* that are written inside XML tags. * that are written inside XML tags.
* *
* The key-value pairs are written into an object. The keys are also * The key-value pairs are written into an object. The keys are also
@ -93,47 +93,60 @@
* @return {Array} An array that contains the key-value pairs and * @return {Array} An array that contains the key-value pairs and
* the keys. * the keys.
*/ */
function decode (str) { function parseOvpn (str) {
let ovpn = {} let ovpn = {}
let keys = {} let keys = {}
const re = /^([^ ]+)( (.*))?$/i // define regexes for properties, opening xml tag and closing xml tag
const xmlOpen = /^<([^\/].*)>$/i const reProperty = /^([^ ]+)( (.*))?$/i
const xmlClose = /^<\/(.*)>$/i const reXmlOpen = /^<([^\/].*)>$/i
const reXmlClose = /^<\/(.*)>$/i
// temporary variables for handling xml tags
let xmlTag = '' let xmlTag = ''
let inXml = false let inXml = false
let xmlContent = '' let xmlContent = ''
let lines = str.split(/[\r\n]+/g) let lines = str.split(/[\r\n]+/g)
for (let line of lines) { for (let line of lines) {
// skip line if it is empty or begins with '#' or ';'
if (!line || line.match(/^\s*[;#]/)) continue if (!line || line.match(/^\s*[;#]/)) continue
if (inXml) { if (inXml) { // an XML tag was opened and hasn't been closed yet
const xmlMatch = line.match(xmlClose) const xmlMatch = line.match(reXmlClose)
if (!xmlMatch) { if (!xmlMatch) {
// no closing tag -> add content to `xmlContent`
xmlContent += line + '\n' xmlContent += line + '\n'
continue continue
} }
const tag = xmlMatch[1] const tag = xmlMatch[1]
if (tag !== xmlTag) { if (tag !== xmlTag) {
alert('Cannot parse ovpn file.')
throw 'bad xml tag' throw 'bad xml tag'
} }
const name = unsafe(xmlTag) // closing tag was found
const value = unsafe(xmlContent) // make sure the tag name and the contents are safe
const name = makeSafe(xmlTag)
const value = makeSafe(xmlContent)
// store everything and reset the xml variables
keys[name] = value keys[name] = value
ovpn[name] = name ovpn[name] = name
xmlContent = '' xmlContent = ''
inXml = false inXml = false
continue continue
} }
const xmlMatch = line.match(xmlOpen) const xmlMatch = line.match(reXmlOpen)
if (xmlMatch) { if (xmlMatch) {
// an xml tag was opened
inXml = true inXml = true
xmlTag = xmlMatch[1] xmlTag = xmlMatch[1]
continue continue
} }
const match = line.match(re) // check if the line contains a property
const match = line.match(reProperty)
if (!match) continue if (!match) continue
const key = unsafe(match[1]) // make sure everything is safe and then store it
const value = match[2] ? unsafe((match[3] || '')) : true const key = makeSafe(match[1])
const value = match[2] ? makeSafe((match[3] || '')) : true
ovpn[key] = value ovpn[key] = value
} }
@ -145,7 +158,7 @@
(val.charAt(0) === "'" && val.slice(-1) === "'")) (val.charAt(0) === "'" && val.slice(-1) === "'"))
} }
function unsafe (val, doUnesc) { function makeSafe (val, doUnesc) {
val = (val || '').trim() val = (val || '').trim()
if (isQuoted(val)) { if (isQuoted(val)) {
// remove the single quotes before calling JSON.parse // remove the single quotes before calling JSON.parse
@ -187,7 +200,15 @@
'Certificates': [], 'Certificates': [],
'NetworkConfigurations': [] 'NetworkConfigurations': []
} }
/**
* Convert the parsed OVPN file to ONC structure
*
* @param {string} name Name of the connection
* @param {Object} ovpn The parsed OVPN file
* @param {Object} keys Strings with keys, indexed by key name
* @return {Object} The converted ONC structure
*/
function convert (name, ovpn, keys) { function convert (name, ovpn, keys) {
if (!ovpn.client) { if (!ovpn.client) {
console.warn('Is this a server file?') console.warn('Is this a server file?')
@ -284,7 +305,10 @@
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
) )
} }
/**
* Replace newlines with explicit `\n` and filter out comments
*/
function convertKey (key) { function convertKey (key) {
let lines = key.split(/\n/g) let lines = key.split(/\n/g)
let out = '' let out = ''
@ -295,7 +319,10 @@
} }
return out return out
} }
/**
* Find all certificates in a string and extract them
*/
function extractCas (str) { function extractCas (str) {
let splits = str.replace(/\n/g, '').split('-----BEGIN CERTIFICATE-----') let splits = str.replace(/\n/g, '').split('-----BEGIN CERTIFICATE-----')
console.log(splits) console.log(splits)