initial commit

This commit is contained in:
2023-06-04 17:46:19 +02:00
commit 4b90f7c8d5
51 changed files with 9952 additions and 0 deletions

13
frontend/.eslintignore Normal file
View File

@@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock

30
frontend/.eslintrc.cjs Normal file
View File

@@ -0,0 +1,30 @@
module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:svelte/recommended',
'prettier'
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
extraFileExtensions: ['.svelte']
},
env: {
browser: true,
es2017: true,
node: true
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser'
}
}
]
};

10
frontend/.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*

2
frontend/.npmrc Normal file
View File

@@ -0,0 +1,2 @@
engine-strict=true
resolution-mode=highest

13
frontend/.prettierignore Normal file
View File

@@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock

9
frontend/.prettierrc Normal file
View File

@@ -0,0 +1,9 @@
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte"],
"pluginSearchDirs": ["."],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}

38
frontend/README.md Normal file
View File

@@ -0,0 +1,38 @@
# create-svelte
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
## Creating a project
If you're seeing this, you've probably already done this step. Congrats!
```bash
# create a new project in the current directory
npm create svelte@latest
# create a new project in my-app
npm create svelte@latest my-app
```
## Developing
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
```bash
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
```
## Building
To create a production version of your app:
```bash
npm run build
```
You can preview the production build with `npm run preview`.
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.

4958
frontend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

35
frontend/package.json Normal file
View File

@@ -0,0 +1,35 @@
{
"name": "frontend",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev --strictPort --port 5173",
"build": "vite build",
"preview": "vite preview --strictPort --port 5173",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/adapter-static": "^2.0.2",
"@sveltejs/kit": "^1.5.0",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte": "^2.26.0",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.8.1",
"svelte": "^3.54.0",
"svelte-check": "^3.0.1",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.3.0"
},
"type": "module",
"dependencies": {
"webrtc-adapter": "^8.2.2"
}
}

13
frontend/src/app.d.ts vendored Normal file
View File

@@ -0,0 +1,13 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
}
}
export { };

12
frontend/src/app.html Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>

View File

@@ -0,0 +1,2 @@
export const prerender = true
export const ssr = false;

View File

@@ -0,0 +1,204 @@
<script lang="ts">
import adapter from 'webrtc-adapter';
import * as media from './media';
let media_devices = media.devices;
let available_devices = media.availableDevices;
let video: HTMLMediaElement;
let audio: HTMLMediaElement;
let public_ip: string | null = '';
let remote_offer = '';
let local_offer = '';
$: getUserMediaOptions = {
audio: $available_devices.audioinput,
video: $available_devices.videoinput
};
media.initialize().then(() => console.log('options:', getUserMediaOptions));
async function getUserMedia(): Promise<[RTCPeerConnection, Promise<RTCIceCandidate[]>]> {
const supportedConstraints = navigator.mediaDevices.getSupportedConstraints();
console.log(supportedConstraints);
// This obviously shouldn't be here and only is for testing
async function sign_username(secret: string, user: string): Promise<string> {
// encoder to convert string to Uint8Array
let enc = new TextEncoder('utf-8');
let key = await window.crypto.subtle.importKey(
'raw', // raw format of the key - should be Uint8Array
enc.encode(secret),
{
// algorithm details
name: 'HMAC',
hash: { name: 'SHA-1' }
},
false, // export = false
['sign', 'verify'] // what this key can do
);
let signature = await window.crypto.subtle.sign('HMAC', key, enc.encode(user));
function _arrayBufferToBase64(buffer: ArrayBuffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (let i = 0; i < len; i++) binary += String.fromCharCode(bytes[i]);
return window.btoa(binary);
}
return _arrayBufferToBase64(signature);
}
let username = `${+new Date() + 60 * 1000}:test`;
let credential = await sign_username('secret', username);
console.log(credential);
const iceServers = [
// Test some TURN server
{
urls: ['turn:turn.h.glsys.de:3478'],
username,
credential
}
// Test some STUN server
// { urls: ['stun:turn.h.glsys.de:3478'] },
];
let pc = new RTCPeerConnection({ iceServers });
let candidates: RTCIceCandidate[] = [];
pc.addEventListener('icecandidate', (e) => {
if (!e.candidate) return;
console.log(e.candidate.candidate);
candidates.push(e.candidate);
// If a srflx candidate was found, notify that the STUN server works!
if (e.candidate.type == 'srflx') {
console.log('The STUN server is reachable!');
console.log(` Your Public IP Address is: ${e.candidate.address}`);
public_ip = e.candidate.address;
}
// If a relay candidate was found, notify that the TURN server works!
if (e.candidate.type == 'relay') {
console.log('The TURN server is reachable !');
}
});
let resolve: (candidates: RTCIceCandidate[]) => void, reject: (value: unknown) => void;
let promise: Promise<RTCIceCandidate[]> = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
pc.addEventListener('icegatheringstatechange', (e) => {
console.log(e);
if (e.target.iceGatheringState == 'complete') resolve(candidates);
});
pc.addEventListener('icecandidateerror', (e) => {
console.error(e);
reject(e);
});
pc.ontrack = (e) => {
audio.srcObject = e.streams[0];
};
console.log('options:', getUserMediaOptions);
let stream = await navigator.mediaDevices.getUserMedia(getUserMediaOptions);
pc.addTrack(stream.getAudioTracks()[0]);
return [pc, promise];
let stream2 = await navigator.mediaDevices.getDisplayMedia({ audio: true });
await media.update();
console.log(stream);
video.srcObject = stream;
await video.play();
console.log(stream.getAudioTracks());
console.log(stream.getVideoTracks());
}
let pc: RTCPeerConnection;
let candidates: Promise<RTCIceCandidate[]>;
async function accept() {
[pc, candidates] = await getUserMedia();
pc.setRemoteDescription(JSON.parse(remote_offer));
let answer = await pc.createAnswer({
offerToReceiveAudio: true,
offerToReceiveVideo: false
});
console.log(answer);
await pc.setLocalDescription(answer);
for (let candidate of await candidates) pc.addIceCandidate(candidate);
local_offer = JSON.stringify(answer);
}
async function offer() {
[pc, candidates] = await getUserMedia();
let offer = await pc.createOffer({
offerToReceiveAudio: true,
offerToReceiveVideo: false
});
console.log(offer);
await pc.setLocalDescription(offer);
local_offer = JSON.stringify(offer);
}
async function establish() {
pc.setRemoteDescription(JSON.parse(remote_offer));
for (let candidate of await candidates) pc.addIceCandidate(candidate);
}
</script>
<h1>Welcome</h1>
<p>Your public ip is: {public_ip || 'unknown'}</p>
<video bind:this={video} />
<audio bind:this={audio} />
<p>See also: <a href="/a">a</a></p>
<h1>remote offer:</h1>
<textarea bind:value={remote_offer} />
<button on:click={accept}>accept</button>
<button on:click={offer}>offer</button>
<button on:click={establish}>establish</button>
<h1>local offer:</h1>
<textarea value={local_offer} />
<ul>
{#each $media_devices as device}
<li>{device.kind} | <code>{device.deviceId}</code> | {device.label}</li>
{/each}
</ul>
<div>{JSON.stringify($available_devices)}</div>

View File

@@ -0,0 +1 @@
<h1>A</h1>

View File

@@ -0,0 +1,37 @@
import { writable, type Writable, derived, type Readable } from 'svelte/store';
export const devices: Writable<MediaDeviceInfo[]> = writable([]);
export const availableDevices: Readable<{
audioinput: boolean,
audiooutput: boolean,
videoinput: boolean,
}> = derived(devices, $devices => {
let available: { [key in MediaDeviceKind]: boolean } = {
audioinput: false,
audiooutput: false,
videoinput: false,
};
for (const device of $devices) {
if (available.hasOwnProperty(device.kind)) available[device.kind] = true;
}
return available;
})
export async function initialize() {
await update();
addEventListener("devicechange", event => {
console.log(event);
update();
});
}
export async function update() {
devices.set(await navigator.mediaDevices.enumerateDevices());
}

BIN
frontend/static/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

28
frontend/svelte.config.js Normal file
View File

@@ -0,0 +1,28 @@
// import adapter from '@sveltejs/adapter-auto';
import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: vitePreprocess(),
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
// adapter: adapter()
adapter: adapter({
// default options are shown. On some platforms
// these options are set automatically — see below
pages: 'build',
assets: 'build',
fallback: null,
precompress: false,
strict: true
}),
}
};
export default config;

18
frontend/tsconfig.json Normal file
View File

@@ -0,0 +1,18 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"strictNullChecks": true
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}

6
frontend/vite.config.ts Normal file
View File

@@ -0,0 +1,6 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()]
});