Skip to content

Commit

Permalink
This commit
Browse files Browse the repository at this point in the history
  • Loading branch information
virtulis committed Dec 19, 2023
1 parent 7589b87 commit fb85454
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dist
/conf

/.idea
31 changes: 31 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mkdir, readdir, readFile, writeFile } from 'fs/promises';

async function main() {

const args = process.argv.slice(2);

if (args.length != 4) {
console.log('Usage: node build.js base_host ip4_host ip6_host root_dir');
return;
}

const [BASE_HOST, IP4_HOST, IP6_HOST, DIST_DIR] = args;
const vars = { BASE_HOST, IP4_HOST, IP6_HOST, DIST_DIR };
console.log(vars);

await mkdir('dist', { recursive: true });
await mkdir('conf', { recursive: true });

const files = await readdir('template');
for (const fn of files) {
let src = await readFile(`template/${fn}`, 'utf-8');
for (const [key, val] of Object.entries(vars)) {
src = src.replaceAll(key, val);
}
const out = fn.match(/\.conf$/) ? 'conf' : 'dist';
await writeFile(`${out}/${fn}`, src);
}

}

await main();
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@virtulis/this-computer",
"private": true,
"type": "module"
}
73 changes: 73 additions & 0 deletions template/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<html lang="en">

<head>
<title>BASE_HOST</title>
<meta charset="utf8" />
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=yes" />
<style>
body {
font-family: sans-serif;
padding: 16px;
}
footer {
margin: 16px 0;
}
</style>
</head>

<body>

<noscript>
This page requires JavaScript to work. Or use the <a href="https://BASE_HOST/text">text version</a>.
</noscript>

<main>
<h2>This Computer:</h2>
<dl>
<dt>IPv4:</dt>
<dd id="public4">...</dd>
<dt>IPv6:</dt>
<dd id="public6">...</dd>
</dl>
<button id="copy">Copy</button>
<button id="copy4" disabled>IPv4</button>
<button id="copy6" disabled>IPv6</button>
</main>

<footer>
100% <a href="https://kludge.guru">kludges</a>.
<a href="https://github.com/virtulis/this-computer">Github</a>.
<a href="https://ko-fi.com/kludge_guru">Ko-fi</a>.
</footer>

<script type="application/javascript">

const timeout = new Promise(resolve => setTimeout(() => resolve(null), 30_000));

const ips = {};
const promises = {
4: Promise.race([timeout, fetch('https://IP4_HOST/').then(res => res.text()).catch(() => null)]),
6: Promise.race([timeout, fetch('https://IP6_HOST/').then(res => res.text()).catch(() => null)]),
};
Object.entries(promises).forEach(([v, p]) => p.then(str => {
ips[v] = str?.trim();
document.getElementById(`public${v}`).textContent = str ?? '—';
document.getElementById(`copy${v}`).disabled = !str;
}));

document.getElementById('copy').addEventListener('click', async e => {
const lines = Object.values(ips).filter(s => !!s);
if (!lines.length) return;
await navigator.clipboard.writeText(lines.join('\n'));
e.target.textContent = 'Copied';
});
[4, 6].forEach(v => document.getElementById(`copy${v}`).addEventListener('click', async e => {
await navigator.clipboard.writeText(ips[v]);
e.target.textContent = 'Copied';
}));

</script>

</body>

</html>
7 changes: 7 additions & 0 deletions template/this-computer-certbot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
server {
listen 80;
server_name BASE_HOST IP4_HOST IP6_HOST;
location /.well-known {
root /srv/tmp;
}
}
43 changes: 43 additions & 0 deletions template/this-computer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
map $http_accept $respond_with {
default /text;
~text/html /index;
}

server {

listen 443 ssl http2;
server_name BASE_HOST;
#ssl_certificate /etc/letsencrypt/live/BASE_HOST/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/BASE_HOST/privkey.pem;

root DIST_DIR;
location = / {
rewrite . $respond_with;
}
location /text {
add_header Cache-Control "no-cache";
return 200 "$remote_addr\n\nSee also:\nhttps://IP4_HOST\nhttps://IP6_HOST\n";
}
location / {
add_header Cache-Control "no-cache";
index index.html;
try_files $uri $uri.html =404;
}

}

server {

listen 443 ssl http2;
server_name IP4_HOST IP6_HOST;
#ssl_certificate /etc/letsencrypt/live/BASE_HOST/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/BASE_HOST/privkey.pem;

location / {
add_header Content-Type text/plain;
add_header Cache-Control "no-cache";
add_header Access-Control-Allow-Origin "https://BASE_HOST";
return 200 "$remote_addr\n";
}

}

0 comments on commit fb85454

Please sign in to comment.