Add server lists to edit
This commit is contained in:
1 parent
08b9f659c2
commit
85ccdbfc03
4 files changed
+151
-11
No files matched your search
+30
-2
@@ -4,7 +4,7 @@ import { open as openFile, readFile, writeFile } from 'node:fs/promises';
|
|||||||
import { chromium, expect, firefox, test, type Browser, type BrowserType, type Page } from '@playwright/test';
|
import { chromium, expect, firefox, test, type Browser, type BrowserType, type Page } from '@playwright/test';
|
||||||
import { createServer, type ViteDevServer } from 'vite';
|
import { createServer, type ViteDevServer } from 'vite';
|
||||||
|
|
||||||
// Runs against the real public trackers and STUN servers from src/main.ts.
|
// Runs against the real public trackers and STUN servers, the defaults in src/main.ts.
|
||||||
const BROWSERS: Record<string, BrowserType> = { chromium, firefox };
|
const BROWSERS: Record<string, BrowserType> = { chromium, firefox };
|
||||||
const SIZE = 5_000_000;
|
const SIZE = 5_000_000;
|
||||||
// Opt-in with LARGE=1: beyond what fits comfortably in RAM and beyond the 2000 MiB the old OPFS
|
// Opt-in with LARGE=1: beyond what fits comfortably in RAM and beyond the 2000 MiB the old OPFS
|
||||||
@@ -38,7 +38,9 @@ const MODES: Record<string, (sender: Page, receiver: Page) => Promise<void>> = {
|
|||||||
// gatherIce Chromium would wait 40 s per side, with only the hard timeout still 10 s.
|
// gatherIce Chromium would wait 40 s per side, with only the hard timeout still 10 s.
|
||||||
async 'dead-stun'(sender, receiver) {
|
async 'dead-stun'(sender, receiver) {
|
||||||
for (const page of [sender, receiver]) {
|
for (const page of [sender, receiver]) {
|
||||||
await page.evaluate((path) => import(path).then((core) => core.STUN_SERVERS.push('stun:192.0.2.1:3478')), '/src/main.ts');
|
await page.click('#servers summary');
|
||||||
|
const servers = await page.inputValue('#stun-servers');
|
||||||
|
await page.fill('#stun-servers', `${servers}\nstun:192.0.2.1:3478`);
|
||||||
}
|
}
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
await MODES.manual!(sender, receiver);
|
await MODES.manual!(sender, receiver);
|
||||||
@@ -260,6 +262,32 @@ for (const mode of Object.keys(MODES)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const name of Object.keys(BROWSERS)) {
|
||||||
|
test(`servers: ${name} skips invalid lines and keeps the lists over a reload`, async () => {
|
||||||
|
const page = await open(name, []);
|
||||||
|
const core = () =>
|
||||||
|
page.evaluate((path) => import(path).then(({ settings }) => [settings.stunServers, settings.trackers]), '/src/main.ts');
|
||||||
|
await page.click('#servers summary');
|
||||||
|
// Both would make the constructors throw.
|
||||||
|
await page.fill('#stun-servers', 'stun:stun.example:99999');
|
||||||
|
await page.fill('#trackers', 'tracker.example\nwss://tracker.example/#\nwss://tracker.example');
|
||||||
|
await expect(page.locator('#servers')).toContainText('accepts: stun:stun.example:99999');
|
||||||
|
await expect(page.locator('#servers')).toContainText('without #: tracker.example, wss://tracker.example/#');
|
||||||
|
expect(await core()).toEqual([[], ['wss://tracker.example']]);
|
||||||
|
|
||||||
|
await page.reload();
|
||||||
|
await page.click('#servers summary');
|
||||||
|
await expect(page.locator('#trackers')).toHaveValue('wss://tracker.example');
|
||||||
|
expect(await core()).toEqual([[], ['wss://tracker.example']]);
|
||||||
|
|
||||||
|
await page.fill('#trackers', '');
|
||||||
|
await expect(page.locator('#receive')).toBeDisabled();
|
||||||
|
await page.click('#reset-servers');
|
||||||
|
await expect(page.locator('#receive')).toBeEnabled();
|
||||||
|
expect((await core())[1]).toContain('wss://tracker.webtorrent.dev');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for (const name of Object.keys(BROWSERS)) {
|
for (const name of Object.keys(BROWSERS)) {
|
||||||
test(`update: ${name} receives after sw.js changed`, async () => {
|
test(`update: ${name} receives after sw.js changed`, async () => {
|
||||||
const [sender, receiver] = await Promise.all([open(name, []), open(name, [])]);
|
const [sender, receiver] = await Promise.all([open(name, []), open(name, [])]);
|
||||||
|
|||||||
+83
-3
@@ -1,8 +1,10 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { reactive, ref } from 'vue';
|
import { computed, reactive, ref, watch } from 'vue';
|
||||||
import {
|
import {
|
||||||
answerOffer,
|
answerOffer,
|
||||||
createOffer,
|
createOffer,
|
||||||
|
DEFAULT_STUN_SERVERS,
|
||||||
|
DEFAULT_TRACKERS,
|
||||||
receiveViaTracker,
|
receiveViaTracker,
|
||||||
sendViaTracker,
|
sendViaTracker,
|
||||||
settings as coreSettings,
|
settings as coreSettings,
|
||||||
@@ -10,9 +12,68 @@ import {
|
|||||||
type Events,
|
type Events,
|
||||||
} from './main';
|
} from './main';
|
||||||
|
|
||||||
|
const SETTINGS_KEY = 'hellcon:settings';
|
||||||
|
|
||||||
// Same object as the core's, so the core sees every change.
|
// Same object as the core's, so the core sees every change.
|
||||||
const settings = reactive(coreSettings);
|
const settings = reactive(coreSettings);
|
||||||
|
|
||||||
|
// Kept in this browser only. Storage can be blocked or hold something old, then the defaults stay.
|
||||||
|
try {
|
||||||
|
const saved = JSON.parse(localStorage.getItem(SETTINGS_KEY) ?? '{}');
|
||||||
|
if (typeof saved.stun === 'boolean') settings.stun = saved.stun;
|
||||||
|
if (Array.isArray(saved.stunServers)) settings.stunServers = saved.stunServers;
|
||||||
|
if (Array.isArray(saved.trackers)) settings.trackers = saved.trackers;
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
watch(
|
||||||
|
settings,
|
||||||
|
() => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
|
||||||
|
} catch {}
|
||||||
|
},
|
||||||
|
{ deep: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
// RTCPeerConnection and WebSocket throw on a URL they don't accept, so only valid lines reach the core.
|
||||||
|
// For STUN the browser decides, e.g. Firefox rejects stuns: and both a port beyond 65535. A connection
|
||||||
|
// contacts nobody before an offer, but typing makes one per keystroke, so each line is checked once.
|
||||||
|
const stunChecks = new Map<string, boolean>();
|
||||||
|
function isStun(url: string) {
|
||||||
|
if (!stunChecks.has(url)) {
|
||||||
|
try {
|
||||||
|
new RTCPeerConnection({ iceServers: [{ urls: url }] }).close();
|
||||||
|
stunChecks.set(url, /^stuns?:/i.test(url));
|
||||||
|
} catch {
|
||||||
|
stunChecks.set(url, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stunChecks.get(url)!;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every # starts a fragment, which WebSocket rejects even when empty.
|
||||||
|
const isTracker = (url: string) =>
|
||||||
|
URL.canParse(url) && ['ws:', 'wss:'].includes(new URL(url).protocol) && !url.includes('#');
|
||||||
|
|
||||||
|
// A textarea with one URL per line for the list at settings[key], invalid lines are listed and skipped.
|
||||||
|
function serverList(key: 'stunServers' | 'trackers', valid: (url: string) => boolean) {
|
||||||
|
const text = ref(settings[key].join('\n'));
|
||||||
|
const lines = computed(() => text.value.split('\n').map((line) => line.trim()).filter(Boolean));
|
||||||
|
const invalid = computed(() => lines.value.filter((line) => !valid(line)));
|
||||||
|
// Immediate, a saved list may hold lines this browser rejects.
|
||||||
|
watch(lines, (lines) => (settings[key] = lines.filter(valid)), { immediate: true });
|
||||||
|
// Unwraps both refs, so the template can bind them as plain properties.
|
||||||
|
return reactive({ text, invalid });
|
||||||
|
}
|
||||||
|
|
||||||
|
const stunServers = serverList('stunServers', isStun);
|
||||||
|
const trackers = serverList('trackers', isTracker);
|
||||||
|
|
||||||
|
function onResetServers() {
|
||||||
|
stunServers.text = DEFAULT_STUN_SERVERS.join('\n');
|
||||||
|
trackers.text = DEFAULT_TRACKERS.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
const file = ref<File>();
|
const file = ref<File>();
|
||||||
const offer = ref('');
|
const offer = ref('');
|
||||||
const answerInput = ref('');
|
const answerInput = ref('');
|
||||||
@@ -143,7 +204,7 @@ function onReceive() {
|
|||||||
|
|
||||||
<section>
|
<section>
|
||||||
<h3>Tracker</h3>
|
<h3>Tracker</h3>
|
||||||
<button id="share" :disabled="!file" @click="onShare">Share via tracker</button>
|
<button id="share" :disabled="!file || !settings.trackers.length" @click="onShare">Share via tracker</button>
|
||||||
<p v-if="sendCode">Code: <output id="send-code">{{ sendCode }}</output></p>
|
<p v-if="sendCode">Code: <output id="send-code">{{ sendCode }}</output></p>
|
||||||
</section>
|
</section>
|
||||||
</article>
|
</article>
|
||||||
@@ -165,11 +226,30 @@ function onReceive() {
|
|||||||
<h3>Tracker</h3>
|
<h3>Tracker</h3>
|
||||||
<div role="group">
|
<div role="group">
|
||||||
<input id="receive-code" v-model="receiveCode" placeholder="Code" />
|
<input id="receive-code" v-model="receiveCode" placeholder="Code" />
|
||||||
<button id="receive" @click="onReceive">Receive</button>
|
<button id="receive" :disabled="!settings.trackers.length" @click="onReceive">Receive</button>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
|
<details id="servers">
|
||||||
|
<summary>Servers</summary>
|
||||||
|
<div>
|
||||||
|
<section>
|
||||||
|
<h3>STUN</h3>
|
||||||
|
<label for="stun-servers">Tell each side its public address, one URL per line:</label>
|
||||||
|
<textarea id="stun-servers" v-model="stunServers.text" :aria-invalid="!!stunServers.invalid.length"></textarea>
|
||||||
|
<p v-if="stunServers.invalid.length">Skipped, not a STUN URL this browser accepts: {{ stunServers.invalid.join(', ') }}</p>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<h3>Trackers</h3>
|
||||||
|
<label for="trackers">WebTorrent trackers for the code exchange, one URL per line:</label>
|
||||||
|
<textarea id="trackers" v-model="trackers.text" :aria-invalid="!!trackers.invalid.length"></textarea>
|
||||||
|
<p v-if="trackers.invalid.length">Skipped, not a ws: or wss: URL without #: {{ trackers.invalid.join(', ') }}</p>
|
||||||
|
</section>
|
||||||
|
<button id="reset-servers" @click="onResetServers">Reset to defaults</button>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Log</summary>
|
<summary>Log</summary>
|
||||||
<pre id="log">{{ log.join('\n') }}</pre>
|
<pre id="log">{{ log.join('\n') }}</pre>
|
||||||
|
|||||||
+13
-6
@@ -2,9 +2,9 @@ import { createApp } from 'vue';
|
|||||||
import App from './App.vue';
|
import App from './App.vue';
|
||||||
import './style.css';
|
import './style.css';
|
||||||
|
|
||||||
export const TRACKERS = ['wss://tracker.webtorrent.dev', 'wss://open.ftorrent.com'];
|
export const DEFAULT_TRACKERS = ['wss://tracker.webtorrent.dev', 'wss://open.ftorrent.com'];
|
||||||
|
|
||||||
export const STUN_SERVERS = [
|
export const DEFAULT_STUN_SERVERS = [
|
||||||
'stun:stun.pagequit.net:3478',
|
'stun:stun.pagequit.net:3478',
|
||||||
'stun:stun.cloudflare.com:3478',
|
'stun:stun.cloudflare.com:3478',
|
||||||
'stun:stun.l.google.com:19302',
|
'stun:stun.l.google.com:19302',
|
||||||
@@ -12,7 +12,12 @@ export const STUN_SERVERS = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
// Without STUN only local addresses are offered: works on the same network or VPN, contacts nobody.
|
// Without STUN only local addresses are offered: works on the same network or VPN, contacts nobody.
|
||||||
export const settings = { stun: true };
|
// Read whenever a connection or tracker socket is created, so changes apply to the next one.
|
||||||
|
export const settings = {
|
||||||
|
stun: true,
|
||||||
|
stunServers: [...DEFAULT_STUN_SERVERS],
|
||||||
|
trackers: [...DEFAULT_TRACKERS],
|
||||||
|
};
|
||||||
|
|
||||||
const CHUNK_SIZE = 16 * 1024;
|
const CHUNK_SIZE = 16 * 1024;
|
||||||
const MAX_BUFFERED = 1024 * 1024;
|
const MAX_BUFFERED = 1024 * 1024;
|
||||||
@@ -40,7 +45,9 @@ export type Events = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function createPeerConnection() {
|
function createPeerConnection() {
|
||||||
return new RTCPeerConnection({ iceServers: settings.stun ? [{ urls: STUN_SERVERS }] : [] });
|
// An empty urls list is a syntax error.
|
||||||
|
const stun = settings.stun && settings.stunServers.length;
|
||||||
|
return new RTCPeerConnection({ iceServers: stun ? [{ urls: settings.stunServers }] : [] });
|
||||||
}
|
}
|
||||||
|
|
||||||
function randomBinary(length: number) {
|
function randomBinary(length: number) {
|
||||||
@@ -420,7 +427,7 @@ export async function sendViaTracker(file: File, events: Events) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const url of TRACKERS) {
|
for (const url of settings.trackers) {
|
||||||
const ws = new WebSocket(url);
|
const ws = new WebSocket(url);
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
announce(ws, hash, peerId, { left: 0, event: 'started' });
|
announce(ws, hash, peerId, { left: 0, event: 'started' });
|
||||||
@@ -489,7 +496,7 @@ export async function receiveViaTracker(code: string, events: Events) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const url of TRACKERS) offer(url);
|
for (const url of settings.trackers) offer(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mounted last: App.vue imports the core from this module.
|
// Mounted last: App.vue imports the core from this module.
|
||||||
|
|||||||
@@ -368,6 +368,31 @@ output {
|
|||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Servers */
|
||||||
|
|
||||||
|
#servers > div {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(min(100%, 22rem), 1fr));
|
||||||
|
justify-items: start;
|
||||||
|
gap: var(--space-4);
|
||||||
|
padding: var(--space-4) var(--space-6) var(--space-6);
|
||||||
|
border-top: 1px solid var(--color-border);
|
||||||
|
}
|
||||||
|
|
||||||
|
#servers section {
|
||||||
|
justify-self: stretch;
|
||||||
|
padding-top: 0;
|
||||||
|
border-top: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#servers p {
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|
||||||
|
[aria-invalid='true'] {
|
||||||
|
border-color: var(--color-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
/* Log */
|
/* Log */
|
||||||
|
|
||||||
summary {
|
summary {
|
||||||
|
|||||||
Reference in new issue
Block a user