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

Add files

This commit is contained in:
thomkeh 2017-11-15 18:31:02 +01:00
parent b47a444576
commit 9183ba841f
5 changed files with 143 additions and 0 deletions

28
index.html Normal file
View File

@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>OpenVPN to ONC</title>
<meta name="description" content="Convert OpenVPN config files to ONC files">
<link rel="stylesheet" href="style.css">
<script type="module" src="js/scripts.js"></script>
</head>
<body>
<div>
<h1>ovpn2onc</h1>
<ul>
<li>OpenVPN config file (*.ovpn): <input type="file" id="inputopenvpn"></li>
<li>Certificates: <input mutliple type="file" id="inputcertificate"></li>
</ul>
<button id="clickbutton" type="button">Convert</button>
</div>
<div>
<p>Output</p>
<textarea readonly id="output" wrap="off" rows="10" cols="50"></textarea>
</div>
</body>
</html>

3
js/converter.js Normal file
View File

@ -0,0 +1,3 @@
export function convert(parsed) {
return {"test": "great content"}
}

86
js/parser.js Normal file
View File

@ -0,0 +1,86 @@
export function decode (str) {
let out = {}
const re = /^([^ ]+)( (.*))?$/i
const xmlOpen = /^<([^\/].*)>$/i
const xmlClose = /^<\/(.*)>$/i
let xmlTag = ''
let inXml = false
let xmlContent = ''
let lines = str.split(/[\r\n]+/g)
for (let line of lines) {
if (!line || line.match(/^\s*[;#]/)) continue
if (inXml) {
const xmlMatch = line.match(xmlClose)
if (!xmlMatch) {
xmlContent += line + '\n'
continue
}
const tag = xmlMatch[1]
if (tag !== xmlTag) {
throw 'bad xml tag'
}
const key = unsafe(xmlTag)
const value = unsafe(xmlContent)
out[key] = value
xmlContent = ''
inXml = false
continue
}
const xmlMatch = line.match(xmlOpen)
if (xmlMatch) {
inXml = true
xmlTag = xmlMatch[1]
continue
}
const match = line.match(re)
if (!match) continue
const key = unsafe(match[1])
const value = match[2] ? unsafe((match[3] || '')) : true
out[key] = value
}
return out
}
function isQuoted (val) {
return (val.charAt(0) === '"' && val.slice(-1) === '"') ||
(val.charAt(0) === "'" && val.slice(-1) === "'")
}
function unsafe (val, doUnesc) {
val = (val || '').trim()
if (isQuoted(val)) {
// remove the single quotes before calling JSON.parse
if (val.charAt(0) === "'") {
val = val.substr(1, val.length - 2)
}
try { val = JSON.parse(val) } catch (_) {}
} else {
// walk the val to find the first not-escaped ; character
var esc = false
var unesc = ''
for (var i = 0, l = val.length; i < l; i++) {
var c = val.charAt(i)
if (esc) {
if ('\\;#'.indexOf(c) !== -1) {
unesc += c
} else {
unesc += '\\' + c
}
esc = false
} else if (';#'.indexOf(c) !== -1) {
break
} else if (c === '\\') {
esc = true
} else {
unesc += c
}
}
if (esc) {
unesc += '\\'
}
return unesc
}
return val
}

23
js/scripts.js Normal file
View File

@ -0,0 +1,23 @@
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);
}

3
style.css Normal file
View File

@ -0,0 +1,3 @@
#output {
background-color: lightgray;
}