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
+83
-3
@@ -1,8 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref } from 'vue';
|
||||
import { computed, reactive, ref, watch } from 'vue';
|
||||
import {
|
||||
answerOffer,
|
||||
createOffer,
|
||||
DEFAULT_STUN_SERVERS,
|
||||
DEFAULT_TRACKERS,
|
||||
receiveViaTracker,
|
||||
sendViaTracker,
|
||||
settings as coreSettings,
|
||||
@@ -10,9 +12,68 @@ import {
|
||||
type Events,
|
||||
} from './main';
|
||||
|
||||
const SETTINGS_KEY = 'hellcon:settings';
|
||||
|
||||
// Same object as the core's, so the core sees every change.
|
||||
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 offer = ref('');
|
||||
const answerInput = ref('');
|
||||
@@ -143,7 +204,7 @@ function onReceive() {
|
||||
|
||||
<section>
|
||||
<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>
|
||||
</section>
|
||||
</article>
|
||||
@@ -165,11 +226,30 @@ function onReceive() {
|
||||
<h3>Tracker</h3>
|
||||
<div role="group">
|
||||
<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>
|
||||
</section>
|
||||
</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>
|
||||
<summary>Log</summary>
|
||||
<pre id="log">{{ log.join('\n') }}</pre>
|
||||
|
||||
+13
-6
@@ -2,9 +2,9 @@ import { createApp } from 'vue';
|
||||
import App from './App.vue';
|
||||
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.cloudflare.com:3478',
|
||||
'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.
|
||||
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 MAX_BUFFERED = 1024 * 1024;
|
||||
@@ -40,7 +45,9 @@ export type Events = {
|
||||
};
|
||||
|
||||
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) {
|
||||
@@ -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);
|
||||
ws.onopen = () => {
|
||||
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.
|
||||
|
||||
@@ -368,6 +368,31 @@ output {
|
||||
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 */
|
||||
|
||||
summary {
|
||||
|
||||
Reference in new issue
Block a user