mirror of
https://github.com/ArcticFoxes-net/ONC-Converter
synced 2024-12-22 00:11:33 -05:00
Make example file work
This commit is contained in:
parent
6660c1e2dd
commit
364a9e8892
@ -1,4 +1,4 @@
|
|||||||
<!doctype html>
|
<!DOCTYPE html>
|
||||||
|
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
@ -17,7 +17,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li>Name for connection: <input type="text" id="connname"></li>
|
<li>Name for connection: <input type="text" id="connname"></li>
|
||||||
<li>OpenVPN config file (*.ovpn): <input type="file" id="inputopenvpn"></li>
|
<li>OpenVPN config file (*.ovpn): <input type="file" id="inputopenvpn"></li>
|
||||||
<li>Certificates: <input mutliple type="file" id="inputcertificate"></li>
|
<li><label for="inputcertificates">Certificates (can be multiple files):</label> <input type="file" id="inputcertificates" multiple></li>
|
||||||
</ul>
|
</ul>
|
||||||
<button id="clickbutton" type="button">Convert</button>
|
<button id="clickbutton" type="button">Convert</button>
|
||||||
</div>
|
</div>
|
||||||
|
107
js/converter.js
107
js/converter.js
@ -8,25 +8,72 @@ const oncBasics = {
|
|||||||
'NetworkConfigurations': []
|
'NetworkConfigurations': []
|
||||||
}
|
}
|
||||||
|
|
||||||
export function convert(name, ovpn) {
|
export 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?')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check parameters
|
|
||||||
let params = {}
|
let params = {}
|
||||||
|
|
||||||
|
// Add certificates
|
||||||
|
let certs = []
|
||||||
|
let [cas, caGuids] = createCerts(keys, ovpn['ca'], 'Authority')
|
||||||
|
params['ServerCARefs'] = caGuids
|
||||||
|
certs = certs.concat(cas)
|
||||||
|
let [clientCerts, clientCertGuids] = createCerts(keys, ovpn['cert'], 'Client')
|
||||||
|
if (clientCerts[0]) {
|
||||||
|
params['ClientCertType'] = 'Ref'
|
||||||
|
params['ClientCertRef'] = clientCertGuids[0]
|
||||||
|
certs.push(clientCerts[0])
|
||||||
|
} else {
|
||||||
|
params['ClientCertType'] = 'None'
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add parameters
|
||||||
let remote = ovpn.remote.split(' ')
|
let remote = ovpn.remote.split(' ')
|
||||||
const host = remote[0]
|
const host = remote[0]
|
||||||
if (remote[1]) {
|
if (remote[1]) params['Port'] = remote[1]
|
||||||
params['Port'] = remote[1]
|
if (ovpn['auth-user-pass']) params['UserAuthenticationType'] = 'Password'
|
||||||
}
|
if (ovpn['comp-lzo'] && ovpn['comp-lzo'] !== 'no') {
|
||||||
if (ovpn.proto) {
|
params['CompLZO'] = 'true'
|
||||||
params['Proto'] = ovpn.proto
|
} else {
|
||||||
|
params['CompLZO'] = 'false'
|
||||||
}
|
}
|
||||||
|
if (ovpn['persist-key']) params['SaveCredentials'] = true
|
||||||
if (ovpn['tls-auth']) {
|
if (ovpn['tls-auth']) {
|
||||||
params['TLSAuthContents'] = convertKey(ovpn['tls-auth'])
|
let authKey = ovpn['tls-auth'].split(' ')
|
||||||
|
params['TLSAuthContents'] = convertKey(keys[authKey[0]])
|
||||||
|
if (authKey[1]) params['KeyDirection'] = authKey[1]
|
||||||
}
|
}
|
||||||
|
if (ovpn['verify-x509-name']) {
|
||||||
|
params['VerifyX509'] = {
|
||||||
|
'Name': ovpn['verify-x509-name']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// set parameters if they exist in the ovpn config
|
||||||
|
let conditionalSet = (ovpnName, oncName, type='str') => {
|
||||||
|
if (ovpn[ovpnName]) {
|
||||||
|
const raw = ovpn[ovpnName]
|
||||||
|
let value
|
||||||
|
switch (type) {
|
||||||
|
case 'int':
|
||||||
|
value = Number(raw)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
value = raw
|
||||||
|
}
|
||||||
|
params[oncName] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
conditionalSet('port', 'Port', 'int')
|
||||||
|
conditionalSet('proto', 'Proto')
|
||||||
|
conditionalSet('key-direction', 'KeyDirection')
|
||||||
|
conditionalSet('remote-cert-tls', 'RemoteCertTLS')
|
||||||
|
conditionalSet('cipher', 'Cipher')
|
||||||
|
conditionalSet('auth', 'Auth')
|
||||||
|
conditionalSet('auth-retry', 'AuthRetry')
|
||||||
|
conditionalSet('reneg-sec', 'RenegSec', 'int')
|
||||||
|
|
||||||
|
// Put together network configuration
|
||||||
let config = {
|
let config = {
|
||||||
'GUID': `{${uuidv4()}}`,
|
'GUID': `{${uuidv4()}}`,
|
||||||
'Name': name,
|
'Name': name,
|
||||||
@ -37,8 +84,11 @@ export function convert(name, ovpn) {
|
|||||||
'OpenVPN': params
|
'OpenVPN': params
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Put everything together
|
||||||
let onc = Object.assign({}, oncBasics) // create copy
|
let onc = Object.assign({}, oncBasics) // create copy
|
||||||
onc.NetworkConfigurations = [config]
|
onc.NetworkConfigurations = [config]
|
||||||
|
onc.Certificates = certs
|
||||||
return onc
|
return onc
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,5 +102,42 @@ function uuidv4() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function convertKey(key) {
|
function convertKey(key) {
|
||||||
return key.replace(/\n/g, '\n') + '\n'
|
let lines = key.split(/\n/g)
|
||||||
|
let out = ''
|
||||||
|
for (let line of lines) {
|
||||||
|
// filter out empty lines and lines with comments
|
||||||
|
if (!line || line.match(/^\s*[;#]/)) continue
|
||||||
|
out += line + '\n'
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractCas(str) {
|
||||||
|
let splits = str.replace(/\n/g, '').split('-----BEGIN CERTIFICATE-----')
|
||||||
|
console.log(splits)
|
||||||
|
let cas = []
|
||||||
|
for (const s of splits) {
|
||||||
|
if (s.includes('-----END CERTIFICATE-----')) {
|
||||||
|
cas.push(s.split('-----END CERTIFICATE-----')[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cas
|
||||||
|
}
|
||||||
|
|
||||||
|
function createCerts(keys, certName, certType) {
|
||||||
|
let certs = []
|
||||||
|
let certGuids = []
|
||||||
|
if (certName) {
|
||||||
|
let rawCerts = extractCas(keys[certName])
|
||||||
|
for (const cert of rawCerts) {
|
||||||
|
const guid = `{${uuidv4()}}`
|
||||||
|
certGuids.push(guid)
|
||||||
|
certs.push({
|
||||||
|
'GUID': guid,
|
||||||
|
'Type': certType,
|
||||||
|
'X509': cert
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [certs, certGuids]
|
||||||
}
|
}
|
||||||
|
44
js/main.js
44
js/main.js
@ -2,28 +2,40 @@ import {decode} from './parser.js'
|
|||||||
import {convert} from './converter.js'
|
import {convert} from './converter.js'
|
||||||
|
|
||||||
let clickButton = document.getElementById('clickbutton')
|
let clickButton = document.getElementById('clickbutton')
|
||||||
clickButton.addEventListener('click', main, false)
|
clickButton.addEventListener('click', handler, false)
|
||||||
|
|
||||||
function main() {
|
function handler() {
|
||||||
let selectedFile = document.getElementById('inputopenvpn').files[0]
|
let selectedFile = document.getElementById('inputopenvpn').files[0]
|
||||||
|
let certificates = document.getElementById('inputcertificates').files
|
||||||
let connName = document.getElementById('connname').value
|
let connName = document.getElementById('connname').value
|
||||||
|
let output = document.getElementById('output')
|
||||||
|
main(connName, selectedFile, certificates, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main(connName, selectedFile, 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 reader = new FileReader()
|
let content = await readFile(selectedFile)
|
||||||
// callback for when reader is done
|
let [ovpn, keys] = decode(content)
|
||||||
reader.onload = (e => {
|
console.log(ovpn)
|
||||||
let content = e.target.result
|
for (const certificateFile of certificateFiles) {
|
||||||
// remove windows-style newlines
|
keys[certificateFile.name] = await readFile(certificateFile)
|
||||||
content = content.replace(/\r/g, '')
|
}
|
||||||
let parsed = decode(content)
|
let onc = convert(connName, ovpn, keys)
|
||||||
console.log(parsed)
|
output.value = JSON.stringify(onc, null, 2)
|
||||||
let onc = convert(connName, parsed)
|
}
|
||||||
let output = document.getElementById('output')
|
|
||||||
output.value = JSON.stringify(onc, null, 2)
|
function readFile(file) {
|
||||||
});
|
return new Promise(resolve => {
|
||||||
// start reading
|
let reader = new FileReader()
|
||||||
reader.readAsText(selectedFile)
|
reader.onload = (e => {
|
||||||
|
// callback and remove windows-style newlines
|
||||||
|
resolve(e.target.result.replace(/\r/g, ''))
|
||||||
|
})
|
||||||
|
// start reading
|
||||||
|
reader.readAsText(file)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
15
js/parser.js
15
js/parser.js
@ -1,5 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* Parse *.ovpn file.
|
||||||
|
*/
|
||||||
export function decode (str) {
|
export function decode (str) {
|
||||||
let out = {}
|
let ovpn = {}
|
||||||
|
let keys = {}
|
||||||
const re = /^([^ ]+)( (.*))?$/i
|
const re = /^([^ ]+)( (.*))?$/i
|
||||||
const xmlOpen = /^<([^\/].*)>$/i
|
const xmlOpen = /^<([^\/].*)>$/i
|
||||||
const xmlClose = /^<\/(.*)>$/i
|
const xmlClose = /^<\/(.*)>$/i
|
||||||
@ -20,9 +24,10 @@ export function decode (str) {
|
|||||||
if (tag !== xmlTag) {
|
if (tag !== xmlTag) {
|
||||||
throw 'bad xml tag'
|
throw 'bad xml tag'
|
||||||
}
|
}
|
||||||
const key = unsafe(xmlTag)
|
const name = unsafe(xmlTag)
|
||||||
const value = unsafe(xmlContent)
|
const value = unsafe(xmlContent)
|
||||||
out[key] = value
|
keys[name] = value
|
||||||
|
ovpn[name] = name
|
||||||
xmlContent = ''
|
xmlContent = ''
|
||||||
inXml = false
|
inXml = false
|
||||||
continue
|
continue
|
||||||
@ -37,10 +42,10 @@ export function decode (str) {
|
|||||||
if (!match) continue
|
if (!match) continue
|
||||||
const key = unsafe(match[1])
|
const key = unsafe(match[1])
|
||||||
const value = match[2] ? unsafe((match[3] || '')) : true
|
const value = match[2] ? unsafe((match[3] || '')) : true
|
||||||
out[key] = value
|
ovpn[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
return out
|
return [ovpn, keys]
|
||||||
}
|
}
|
||||||
|
|
||||||
function isQuoted (val) {
|
function isQuoted (val) {
|
||||||
|
Loading…
Reference in New Issue
Block a user