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>
|
||||
|
||||
Reference in new issue
Block a user