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

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-11-17 09:53:06 -05:00
import {decode} from './parser.js'
import {convert} from './converter.js'
let clickButton = document.getElementById('clickbutton')
2017-11-17 16:58:24 -05:00
clickButton.addEventListener('click', handler, false)
2017-11-17 09:53:06 -05:00
2017-11-17 16:58:24 -05:00
function handler() {
2017-11-17 09:53:06 -05:00
let selectedFile = document.getElementById('inputopenvpn').files[0]
2017-11-17 16:58:24 -05:00
let certificates = document.getElementById('inputcertificates').files
2017-11-17 09:53:06 -05:00
let connName = document.getElementById('connname').value
2017-11-17 16:58:24 -05:00
let output = document.getElementById('output')
main(connName, selectedFile, certificates, output)
}
async function main(connName, selectedFile, certificateFiles, output) {
2017-11-17 09:53:06 -05:00
if (connName === '') {
alert('Please specify a name for the connection.')
return
}
console.log(selectedFile.size + ' bytes')
2017-11-17 16:58:24 -05:00
let content = await readFile(selectedFile)
let [ovpn, keys] = decode(content)
console.log(ovpn)
for (const certificateFile of certificateFiles) {
keys[certificateFile.name] = await readFile(certificateFile)
}
let onc = convert(connName, ovpn, keys)
output.value = JSON.stringify(onc, null, 2)
}
function readFile(file) {
return new Promise(resolve => {
let reader = new FileReader()
reader.onload = (e => {
// callback and remove windows-style newlines
resolve(e.target.result.replace(/\r/g, ''))
})
// start reading
reader.readAsText(file)
})
2017-11-17 09:53:06 -05:00
}