-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
369 additions
and
627 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
export const WEB_SERVER = import.meta.env.VITE_WEB_SERVER || 'localhost:8080'; | ||
export const WEB_SERVER = import.meta.env.VITE_WEB_SERVER || ''; | ||
export const SERVER_SECURE = import.meta.env.VITE_SERVER_SECURE; | ||
export const ICE_SERVERS: RTCIceServer[] = | ||
import.meta.env.VITE_ICE_SERVERS && JSON.parse(import.meta.env.VITE_ICE_SERVERS) || | ||
[ | ||
{urls: 'stun:stun.l.google.com:19302'}, | ||
{urls: 'stun:stun1.l.google.com:19302'}, | ||
]; | ||
import.meta.env.VITE_ICE_SERVERS && JSON.parse(import.meta.env.VITE_ICE_SERVERS) || []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import {settings} from './localstorage.ts'; | ||
import _ from 'lodash'; | ||
import cloneDeep from 'lodash/cloneDeep.js'; | ||
import {PeerOptions} from 'peerjs'; | ||
|
||
export default class ServerSettings { | ||
private static get _secureChar() { | ||
return settings.secure ? 's' : ''; | ||
} | ||
export function getPeerOptions() { | ||
const options: PeerOptions = {}; | ||
|
||
static get wsServer() { | ||
return `ws${this._secureChar}://${settings.webServer}`; | ||
if (settings.iceServers.length) { | ||
options.config = { | ||
iceServers: cloneDeep(settings.iceServers) | ||
}; | ||
} | ||
|
||
static get httpServer() { | ||
return `http${this._secureChar}://${settings.webServer}`; | ||
if (settings.webServer) { | ||
const url = new URL(`https://${settings.webServer}`); | ||
options.host = url.hostname; | ||
options.port = Number(url.port); | ||
options.path = url.pathname; | ||
options.secure = settings.secure; | ||
} | ||
|
||
static get iceServers() { | ||
return _.cloneDeep(settings.iceServers); | ||
} | ||
return options; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,46 @@ | ||
import SimplePeer from 'simple-peer'; | ||
import WebSocketRequest from './web-socket-request.ts'; | ||
import {v4 as uuid} from 'uuid'; | ||
import PeerRequest, {CallbackChunk} from './peer-request.ts'; | ||
import ServerSettings from '../app-store/server-settings.ts'; | ||
import PeerDataConnection, {CallbackChunk} from './peer-data-connection.ts'; | ||
import Peer from 'peerjs'; | ||
|
||
type PeerManagerRequestCallback = (body: any, sendChunk: CallbackChunk<any>, peer: PeerRequest) => any; | ||
type PeerManagerRequestCallback = (body: any, sendChunk: CallbackChunk<any>, peer: PeerDataConnection) => any; | ||
export type NewPeerResponse = { | ||
peerId: string, | ||
connectInfo: string | ||
error?: string | ||
} | ||
|
||
export default class PeerManager { | ||
private static _serverWS = new WebSocketRequest(); | ||
private static _serverWSActiveDirectories = 0; | ||
private _destroyed = false; | ||
public shareId = uuid(); | ||
|
||
private _peers: { | ||
[id: string]: SimplePeer.Instance | ||
} = {}; | ||
|
||
private _requestInfo: { | ||
[resource: string]: PeerManagerRequestCallback | ||
} = {}; | ||
private _peer?: Peer; | ||
public peerId?: string; | ||
|
||
public constructor() { | ||
this._createNewPeer = this._createNewPeer.bind(this); | ||
this._signalPeer = this._signalPeer.bind(this); | ||
} | ||
|
||
public async initServerWS() { | ||
const serverWS = await this._makeSureServerWSConnected(); | ||
public async init() { | ||
const {peer, id} = await PeerDataConnection.newPeerConnection(); | ||
|
||
await serverWS.request('new-share', this.shareId); | ||
this._peer = peer; | ||
this.peerId = id; | ||
|
||
PeerManager._serverWSActiveDirectories++; | ||
serverWS.listen(`new-peer/${this.shareId}`, this._createNewPeer); | ||
serverWS.listen(`signal-peer/${this.shareId}`, this._signalPeer); | ||
} | ||
|
||
public listen(resource: string, callback: PeerManagerRequestCallback) { | ||
this._requestInfo[resource] = callback; | ||
} | ||
|
||
private _createNewPeer(): Promise<NewPeerResponse> { | ||
const peerId = uuid(); | ||
const peer = new SimplePeer({ | ||
initiator: true, | ||
config: { | ||
iceServers: ServerSettings.iceServers | ||
} | ||
}) | ||
|
||
this._peers[peerId] = peer; | ||
this._initPeerMethods(peer, peerId); | ||
|
||
return new Promise(res => { | ||
peer.once('signal', data => { | ||
res({ | ||
peerId, | ||
connectInfo: JSON.stringify(data) | ||
}); | ||
}); | ||
this._peer.on('connection', conn => { | ||
console.log('new connection'); | ||
const dataConnection = new PeerDataConnection(conn); | ||
this._initDataConnectionMethods(dataConnection); | ||
}); | ||
} | ||
|
||
private _signalPeer({peerId, connectInfo}: {peerId: string, connectInfo: string}) { | ||
const peer = this._peers[peerId]; | ||
|
||
if (!peer) { | ||
console.error(`No peer with id ${peerId}`); | ||
return {error: `No peer with id ${peerId}`}; | ||
} | ||
|
||
peer.signal(JSON.parse(connectInfo)); | ||
return {ok: true}; | ||
} | ||
|
||
destroy() { | ||
if (this._destroyed || !PeerManager._serverWS) | ||
return; | ||
|
||
|
||
const serverWS = PeerManager._serverWS; | ||
|
||
serverWS.request('closed-share', this.shareId); | ||
serverWS.unregisterListen(`new-peer/${this.shareId}`); | ||
serverWS.unregisterListen(`signal-peer/${this.shareId}`); | ||
|
||
for(const peer of Object.values(this._peers)){ | ||
peer.destroy(); | ||
} | ||
|
||
PeerManager._serverWSActiveDirectories--; | ||
this._destroyed = true; | ||
|
||
if (PeerManager._serverWSActiveDirectories === 0) { | ||
serverWS.close(); | ||
private _initDataConnectionMethods(peer: PeerDataConnection) { | ||
for (const [resource, callback] of Object.entries(this._requestInfo)) { | ||
peer.listen(resource, (body, sendChunk) => | ||
callback(body, sendChunk, peer) | ||
); | ||
} | ||
} | ||
|
||
private async _makeSureServerWSConnected() { | ||
PeerManager._serverWS ??= new WebSocketRequest(); | ||
if (!PeerManager._serverWS.connected) { | ||
await PeerManager._serverWS.connect(ServerSettings.wsServer); | ||
} | ||
|
||
return PeerManager._serverWS; | ||
public listen(resource: string, callback: PeerManagerRequestCallback) { | ||
this._requestInfo[resource] = callback; | ||
} | ||
|
||
private _initPeerMethods(peer: InstanceType<typeof SimplePeer>, peerId: string){ | ||
peer.once('close', () => { | ||
delete this._peers[peerId]; | ||
}); | ||
|
||
const peerRequest = new PeerRequest(peer); | ||
for(const [resource, callback] of Object.entries(this._requestInfo)){ | ||
peerRequest.listen(resource, (body, sendChunk) => | ||
callback(body, sendChunk, peerRequest) | ||
); | ||
} | ||
public destroy() { | ||
this._peer?.destroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.