Skip to content

Commit

Permalink
merges phoebus-84-master, adds local keystore directory import into WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-burns committed Aug 7, 2024
2 parents 3319611 + 95ac124 commit c88d7fa
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 111 deletions.
47 changes: 47 additions & 0 deletions golang/wasm/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ async function findDOHEndpoint() {

return
}

const DNS_RCODE = [
"NoErr", // 0
"FormErr", // 1
Expand Down Expand Up @@ -715,3 +716,49 @@ async function query(dohQName, dohQType) {

return resultJson
}

async function handleFileSelection(event) {
const files = Array.from(event.target.files);
const fileList = document.getElementById('fileList');
fileList.innerHTML = ''; // Clear any existing list items

const filePairs = {};

files.forEach(file => {
const fileName = file.name;
const fileExt = fileName.split('.').pop().toLowerCase();
const baseName = fileName.slice(0, fileName.lastIndexOf('.'));

if (fileExt === 'key' || fileExt === 'private') {
if (!filePairs[baseName]) {
filePairs[baseName] = {};
}
if (fileExt === 'key') {
filePairs[baseName].keyFile = file;
} else if (fileExt === 'private') {
filePairs[baseName].privateFile = file;
}
}
});

for (const [baseName, pair] of Object.entries(filePairs)) {
if (pair.keyFile && pair.privateFile) {
if (localStorage.getItem(baseName)) {
const li = document.createElement('li');
li.textContent = `${baseName}: Already registered`;
fileList.appendChild(li);
} else {
const keyContent = await pair.keyFile.text();
const privateContent = await pair.privateFile.text();
const jsonString = JSON.stringify({ key: keyContent, private: privateContent });

localStorage.setItem(baseName, jsonString);

const li = document.createElement('li');
li.textContent = `${baseName}: Saved to localStorage`;
fileList.appendChild(li);
}
}
}
}

228 changes: 117 additions & 111 deletions golang/wasm/wasm_exec.html
Original file line number Diff line number Diff line change
@@ -1,112 +1,118 @@
<html>
<head>
<title>Go<>JS Demo</title>
<meta charset="utf-8"/>
<script type="application/javascript" src="wasm_exec.js"></script>
<script type="application/javascript" src="pure.js"></script>
</head>
<body>
<h2>DNS</h2>
<h3>DOH Resolver</h3>
<p>
<input type="text" id="query-name" placeholder="mythingymajig.zenr.io">
<input type="text" id="query-type" placeholder="A">
<button onclick="renderQuery()">Resolve</button>
<pre id="query-result"></pre>
</p>

<h2>DNS Keys</h2>
<h3>List Keystore</h3>
<button onclick="listKeys()">Update</button>
<button onclick="listKeysWithStatus()">Update with DNS Status</button>
<div id="existing-keys"></div>


<h3>Request</h3>
<p>
<input type="text" id="new-name" placeholder="mythingymajig.zenr.io">
<button onclick="requestKey()">Request new Key</button>
</p>
<h3>Search Keystore for Key to sign update for a given Subdomain</h3>
<p>
<input type="text" id="keys-for-domain" placeholder="mythingymajig.zenr.io">
<button onclick="getKeysForDomain()">Find key in keystore</button>
<div id="domain-key"></div>
</p>
<h3>Find DOH Endpoint for a given Subdomain</h3>
<p>
<input type="text" id="doh-for-domain" placeholder="mythingymajig.zenr.io">
<button onclick="findDOHEndpoint()">Find DOH endpoint</button>
<div id="domain-doh-endpoint"></div>
</p>
<h3>Import Key pair files from filesystem into Browser Keystore</h3>
<p>
<input type="file" id="input-key" accept=".key"/>
<input type="file" id="input-private" accept=".private"/>
<div id="imported-key"></div>
</p>
<script>
/// Import Key from file pair
///
const streamToText = async (blob) => {
const readableStream = await blob.getReader();
const chunk = await readableStream.read();

return new TextDecoder('utf-8').decode(chunk.value);
};

const bufferToText = (buffer) => {
const bufferByteLength = buffer.byteLength;
const bufferUint8Array = new Uint8Array(buffer, 0, bufferByteLength);

return new TextDecoder().decode(bufferUint8Array);
};

document.getElementById('input-key').addEventListener('change', function(e) {
let file = document.getElementById('input-key').files[0];

(async () => {
const fileContent = await file.text();

console.log('.text()', fileContent);

const fileContentStream = await file.stream();

console.log('.stream()', await streamToText(fileContentStream));

const buffer = await file.arrayBuffer();

console.log('.buffer()', bufferToText(buffer));

const fileSliceBlob = file.slice(0, file.length);
const fileSliceBlobStream = await fileSliceBlob.stream();

console.log('.slice() and .stream()', await streamToText(fileSliceBlobStream));
})();
});

document.getElementById('input-private').addEventListener('change', function(e) {
let file = document.getElementById('input-private').files[0];

(async () => {
const fileContent = await file.text();

console.log('.text()', fileContent);

const fileContentStream = await file.stream();

console.log('.stream()', await streamToText(fileContentStream));

const buffer = await file.arrayBuffer();

console.log('.buffer()', bufferToText(buffer));

const fileSliceBlob = file.slice(0, file.length);
const fileSliceBlobStream = await fileSliceBlob.stream();

console.log('.slice() and .stream()', await streamToText(fileSliceBlobStream));
})();
});
</script>
</body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Go<>JS Demo</title>
<script type="application/javascript" src="wasm_exec.js"></script>
<script type="application/javascript" src="pure.js"></script>
</head>
<body>
<h2>DNS</h2>
<h3>DOH Resolver</h3>
<p>
<input type="text" id="query-name" placeholder="mythingymajig.zenr.io">
<input type="text" id="query-type" placeholder="A">
<button onclick="renderQuery()">Resolve</button>
<pre id="query-result"></pre>
</p>

<h2>DNS Keys</h2>
<h3>List Keystore</h3>
<button onclick="listKeys()">Update</button>
<button onclick="listKeysWithStatus()">Update with DNS Status</button>
<div id="existing-keys"></div>

<h3>Request</h3>
<p>
<input type="text" id="new-name" placeholder="mythingymajig.zenr.io">
<button onclick="requestKey()">Request new Key</button>
</p>

<h3>Search Keystore for Key to sign update for a given Subdomain</h3>
<p>
<input type="text" id="keys-for-domain" placeholder="mythingymajig.zenr.io">
<button onclick="getKeysForDomain()">Find key in keystore</button>
<div id="domain-key"></div>
</p>

<h3>Find DOH Endpoint for a given Subdomain</h3>
<p>
<input type="text" id="doh-for-domain" placeholder="mythingymajig.zenr.io">
<button onclick="findDOHEndpoint()">Find DOH endpoint</button>
<div id="domain-doh-endpoint"></div>
</p>

<h3>Import Key pair files from filesystem into Browser Keystore</h3>
<p>
<input type="file" id="directoryPicker" webkitdirectory multiple>
<ul id="fileList"></ul>
<div id="imported-key"></div>
</p>

<script>
document.getElementById('directoryPicker').addEventListener('change', function(event) {
const files = event.target.files;
const fileList = document.getElementById('fileList');
fileList.innerHTML = ''; // Clear any existing list items

// Object to store pairs of .key and .private files
const filePairs = {};

// Filter and organize files by their name (without extension)
for (let i = 0; i < files.length; i++) {
const file = files[i];
const fileName = file.name;
const fileExt = fileName.split('.').pop().toLowerCase();
const baseName = fileName.slice(0, fileName.lastIndexOf('.'));

if (fileExt === 'key' || fileExt === 'private') {
if (!filePairs[baseName]) {
filePairs[baseName] = {};
}
if (fileExt === 'key') {
filePairs[baseName].keyFile = file;
} else if (fileExt === 'private') {
filePairs[baseName].privateFile = file;
}
}
}

// Process each pair and save to localStorage
for (const baseName in filePairs) {
const pair = filePairs[baseName];
if (pair.keyFile && pair.privateFile) {
// Check if the pair is already in localStorage
if (localStorage.getItem(baseName)) {
const li = document.createElement('li');
li.textContent = `${baseName}: Already registered`;
fileList.appendChild(li);
} else {
// Read the files asynchronously
const keyReader = new FileReader();
const privateReader = new FileReader();

keyReader.onload = function(e) {
const keyContent = e.target.result;
privateReader.onload = function(e) {
const privateContent = e.target.result;
const jsonString = JSON.stringify({ key: keyContent, private: privateContent });

// Save to localStorage
localStorage.setItem(baseName, jsonString);

// Update the UI
const li = document.createElement('li');
li.textContent = `${baseName}: Saved to localStorage`;
fileList.appendChild(li);
};
privateReader.readAsText(pair.privateFile);
};
keyReader.readAsText(pair.keyFile);
}
}
}
});
</script>
</body>
</html>
53 changes: 53 additions & 0 deletions golang/wasm/wasm_exec_es6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Go<>JS Demo</title>
<script type="application/javascript" src="wasm_exec.js"></script>
<script type="application/javascript" src="pure.js"></script>
</head>
<body>
<h2>DNS</h2>
<h3>DOH Resolver</h3>
<p>
<input type="text" id="query-name" placeholder="mythingymajig.zenr.io">
<input type="text" id="query-type" placeholder="A">
<button onclick="renderQuery()">Resolve</button>
<pre id="query-result"></pre>
</p>

<h2>DNS Keys</h2>
<h3>List Keystore</h3>
<button onclick="listKeys()">Update</button>
<button onclick="listKeysWithStatus()">Update with DNS Status</button>
<div id="existing-keys"></div>

<h3>Request</h3>
<p>
<input type="text" id="new-name" placeholder="mythingymajig.zenr.io">
<button onclick="requestKey()">Request new Key</button>
</p>

<h3>Search Keystore for Key to sign update for a given Subdomain</h3>
<p>
<input type="text" id="keys-for-domain" placeholder="mythingymajig.zenr.io">
<button onclick="getKeysForDomain()">Find key in keystore</button>
<div id="domain-key"></div>
</p>

<h3>Find DOH Endpoint for a given Subdomain</h3>
<p>
<input type="text" id="doh-for-domain" placeholder="mythingymajig.zenr.io">
<button onclick="findDOHEndpoint()">Find DOH endpoint</button>
<div id="domain-doh-endpoint"></div>
</p>

<h3>Import Key pair files from filesystem into Browser Keystore</h3>
<p>
<input type="file" id="directoryPicker" webkitdirectory multiple onchange="handleFileSelection(event)">
<ul id="fileList"></ul>
<div id="imported-key"></div>
</p>
</body>
</html>

0 comments on commit c88d7fa

Please sign in to comment.