Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 5x | import { newid, toJSON } from "./common";
import { getDB, IGroup } from './db';
import { IConnection, onRemoteMessage, ping, RPC } from "./remote-calls";
import { IUser, signObject, init as initUser } from "./user";
import { checkPendingInvitations, IInviteAccept, IInviteAcceptType } from "./invitations"
import { shuffle, uniq } from "lodash";
import { Event } from "./events";
export interface ISDIExchange {
connectionId: string
fromDevice: string
toDevice: string
iceCandidates: RTCIceCandidate[]
sdi: RTCSessionDescriptionInit,
user?: IUser
}
export interface IDeviceRegistration {
deviceId: string
user: IUser,
groups: string[],
}
export interface IDeviceConnection extends IConnection {
id: string
pc: RTCPeerConnection
dc: RTCDataChannel
lastAck: number
onAnswer: ((sdi: ISDIExchange) => void)
handlers: { [key: string]: ((err: any, result: any) => void) }
remoteUser?: IUser
waitForDataChannel: (label: string) => Promise<RTCDataChannel>
}
export let deviceId: string = null;
export let me: IUser = null;
let socket;
// export const connections: IDeviceConnection[] = [];
export const deviceConnections: { [deviceId: string]: IDeviceConnection } = {};
export const connections = () => Object.values(deviceConnections);
let initialized = false;
export async function init(_deviceId: string, _me: IUser, serverUrl?: string) {
Iif (initialized) throw new Error('initialized should only be called once');
const userId = await initUser();
Iif (userId != _me.id) {
throw new Error('Connection must be initialized with the same user that is currently signed in');
}
initialized = true;
console.log('initializing peerIO')
deviceId = _deviceId;
me = _me;
signObject(me);
if (serverUrl) {
socket = require('socket.io-client')(serverUrl, { secure: true, rejectUnauthorized: false });
} else {
socket = require('socket.io-client')();
}
socket.on('connect', async () => {
console.log('connected to server', socket.id);
registerDevice();
});
// reconnect is called in addition to connect so redundant for now
socket.on('reconnect', async () => {
console.log('reconnected to server');
events.signalingReconnected.emit();
});
socket.on('disconnect', async () => {
console.log('disconnected from server');
})
socket.on('offer', (offer: ISDIExchange) => handelOffer(offer));
socket.on('answer', (answer: ISDIExchange) => handelAnswer(answer));
socket.on('iceCandidate', async (iceCandidate: ISDIExchange) => {
console.log('received ice candidate', { deviceId: iceCandidate.fromDevice });
const conn = connections().find(c => c.id == iceCandidate.connectionId)
Iif (!conn) {
console.warn('no connection found for iceCandidate, storing in anticipation of upcoming connection', iceCandidate);
if (!earlyIceCandidates[iceCandidate.connectionId]) {
earlyIceCandidates[iceCandidate.connectionId] = [...iceCandidate.iceCandidates];
} else {
earlyIceCandidates[iceCandidate.connectionId].push(...iceCandidate.iceCandidates);
}
return;
}
try {
for (const ic of iceCandidate.iceCandidates) {
await conn.pc.addIceCandidate(ic)
}
} catch (err) {
console.log('error adding ice candidate', iceCandidate.iceCandidates, err);
}
});
Object.keys(onMessageHandlers).forEach(messageType => {
socket.on(messageType, args => {
onMessageHandlers[messageType]?.forEach(async handler => {
try {
await handler(args)
} catch (err) {
console.log('error while handling message', err);
}
})
})
})
}
export async function registerDevice() {
const db = await getDB();
const allGroups = (await db.find('Group', 'type')) as IGroup[];
let allGroupIds = allGroups.map(g => g.id);
allGroupIds.push(me.id);
const pendingInvites = (await db.find(IInviteAcceptType, 'type')) as IInviteAccept[];
pendingInvites.forEach(invite => {
allGroupIds.push(invite.invitation.group);
})
allGroupIds = uniq(allGroupIds);
const registration: IDeviceRegistration = { deviceId, user: me, groups: allGroupIds };
// TODO try to do it through peers first
await new Promise((resolve, reject) => {
socket.emit('register-device', registration, (err, res) => {
if (err) reject(err);
else resolve(res);
})
})
const otherDevices = await getAvailableDevices();
console.log('availableDevices', otherDevices.map(d => ({ deviceId: d.deviceId, userId: d.user.id })), otherDevices.length);
otherDevices.forEach(device => events.deviceDiscovered.emit(device.deviceId));
}
export async function getAvailableDevices(): Promise<IDeviceRegistration[]> {
// TODO try to do it through peers first
return new Promise((resolve, reject) => {
socket.emit('get-available-devices', {}, (err, res) => {
if (err) reject(err)
else resolve(res)
})
})
}
// TODO this could be a memory leak over a long enough period of time
// it could also be maliciously exploited to be a memory leak
const earlyIceCandidates: { [connectionId: string]: RTCIceCandidate[] } = {};
let iceServers: RTCIceServer[] = null;
async function getIceServers() {
Iif (iceServers) return iceServers;
let _iceServers: RTCIceServer[] = [
{
urls: [
"stun:stun.l.google.com:19302",
"stun:stun1.l.google.com:19302",
"stun:stun2.l.google.com:19302",
"stun:stun3.l.google.com:19302",
"stun:stun4.l.google.com:19302",
],
}
]
try {
// TODO try to do it through peers first
iceServers = await new Promise((resolve, reject) =>
socket.emit('getIceServers', {}, (err, res) => err ? reject(err) : resolve(res)));
_iceServers = iceServers;
} catch (err) {
console.warn('failed to get iceServers, using fallback', err)
}
return _iceServers;
}
async function sendOffer(offer: ISDIExchange) {
// TODO try to do it through peers first
await socket.emit('offer', offer);
}
async function sendAnswer(answer: ISDIExchange) {
// TODO try to do it through peers first
await socket.emit('answer', answer);
}
async function sendIceCandidate(iceCandidate: ISDIExchange) {
// TODO try to do it through peers first
socket.emit('iceCandidate', iceCandidate)
}
export const events = {
deviceDiscovered: new Event<string>('DeviceDiscovered'),
deviceConnected: new Event<IDeviceConnection>('DeviceConnected'),
deviceDisconnected: new Event<IDeviceConnection>('DeviceDisconnected'),
signalingReconnected: new Event<void>('SignalingReconnected'),
}
export interface IRemoteChunk {
type: 'chunk',
id: string,
iChunk: number,
totalChunks: number
chunk: string,
}
export const chunkSize = 16384; // this is the safe maximum size but many devices can handle much larger sizes
const strChunkSize = Math.floor(chunkSize / 7);
async function dcSend(connection: IDeviceConnection, data) {
const id = data?.id || newid();
const strData = JSON.stringify(toJSON(data));
if (strData.length < strChunkSize) {
dcSendAndCloseOnError(connection, strData);
} else {
const totalChunks = Math.ceil(strData.length / strChunkSize);
for (var i = 0; i < strData.length; i += strChunkSize) {
const chunk = strData.substr(i, strChunkSize);
const chunkPayload: IRemoteChunk = {
type: 'chunk',
id,
iChunk: i / strChunkSize,
totalChunks,
chunk
}
dcSendAndCloseOnError(connection, JSON.stringify(chunkPayload));
// TODO we should find a better way to apply back pressure (and only if needed)
// TODO see remote-files for a better way to do this: if (dcSend.bufferedAmount > chunkSize * 64)...
Iif ((chunkPayload.iChunk % 2) === 0) {
await new Promise(resolve => setTimeout(resolve, 1));
}
}
}
}
function dcSendAndCloseOnError(connection: IDeviceConnection, strData: string) {
try {
connection.dc.send(strData);
} catch (err) {
connection.close();
throw err;
}
}
export async function checkConnection(connection: IDeviceConnection) {
try {
await RPC(connection, ping)();
} catch (err) {
console.log('INFO: connection heartbeat ping failed so closing connection: ' + connection.id);
closeConnection(connection);
return false;
}
return true;
}
// regularly check if connections are active and close them if not
const connectionWatchDog = setInterval(() => {
const connection = shuffle(connections())[0];
Iif (connection) {
checkConnection(connection);
}
}, 60_000);
// @ts-ignore
connectionWatchDog?.unref?.();
function closeConnection(connection: IDeviceConnection) {
connection.dc?.close();
connection.pc?.close();
connection.closed = true;
delete deviceConnections[connection.remoteDeviceId];
events.deviceDisconnected.emit(connection);
// garbageCollectConnections();
for (const connection of Object.values(deviceConnections)) {
Iif (
['closed', 'closing'].includes(connection.dc?.readyState)
|| ['closed', 'closing'].includes(connection.pc?.connectionState)
) {
connection.dc?.close();
connection.pc?.close();
connection.closed = true;
delete deviceConnections[connection.remoteDeviceId];
events.deviceDisconnected.emit(connection);
}
}
}
export async function connectToDevice(toDeviceId): Promise<IConnection> {
try {
const existingConnection = deviceConnections[toDeviceId];
Iif (existingConnection && (await checkConnection(existingConnection))) {
return existingConnection;
}
const connectionId = newid();
const rtcConfig: RTCConfiguration = {
// peerIdentity: connectionId,
iceServers: await getIceServers()
}
// prepare connection
let pc = new RTCPeerConnection(rtcConfig);
let dc = pc.createDataChannel(`${connectionId}-data`);
const sdi = await pc.createOffer();
await pc.setLocalDescription(sdi);
// await sleep(2000);
// gather ice candidates
const iceCandidates: RTCIceCandidate[] = [];
// send any additional ice candidates through the signaling channel
pc.onicecandidate = e => {
Iif (!e.candidate) return;
iceCandidates.push(e.candidate)
sendIceCandidate({
connectionId,
fromDevice: deviceId,
toDevice: toDeviceId,
iceCandidates,
sdi: null
})
}
// record offer and setup answer promise
let onAnswer: ((sdi: ISDIExchange) => void);
const answerPromise = new Promise<ISDIExchange>(resolve => onAnswer = resolve);
const pendingDCConns: { [label: string]: ((dc: RTCDataChannel) => any) } = {};
const availableDCConns: { [label: string]: RTCDataChannel } = {};
let connection: IDeviceConnection = {
id: connectionId,
remoteDeviceId: toDeviceId,
send: data => dcSend(connection, data),
close: () => closeConnection(connection),
pc,
dc,
lastAck: Date.now(),
onAnswer,
handlers: {},
me,
waitForDataChannel: label => new Promise<RTCDataChannel>((resolve) => {
if (availableDCConns[label]) {
resolve(availableDCConns[label])
} else {
pendingDCConns[label] = resolve;
}
}),
}
// listen for data connections
pc.ondatachannel = e => {
let dc: RTCDataChannel = e.channel;
Iif (dc.label !== `${connection.id}-data`) {
dc.onopen = e => {
console.log('pc data channel open', dc.label);
Iif (pendingDCConns[dc.label]) {
pendingDCConns[dc.label](dc);
delete pendingDCConns[dc.label];
}
availableDCConns[dc.label] = dc;
};
dc.onclose = e => {
delete availableDCConns[dc.label];
}
}
}
deviceConnections[toDeviceId] = connection;
// TODO maybe add a promise that gets resolved once it's open
let resolveConnectionOpen;
const connectionOpenPromise = new Promise(resolve => resolveConnectionOpen = resolve);
dc.onmessage = e => onRemoteMessage(connection, e.data);
dc.onopen = e => {
console.log('dc connection open to', { deviceId: connection.remoteDeviceId, userId: connection.remoteUser?.id })
resolveConnectionOpen();
}
dc.onclose = e => {
console.log("dc.onclose: ", { deviceId: connection.remoteDeviceId, userId: connection.remoteUser?.id })
connection.close();
}
// send offer
sendOffer({
connectionId,
fromDevice: deviceId,
toDevice: toDeviceId,
sdi,
iceCandidates,
})
// wait for answer
const answer = await answerPromise;
connection.remoteUser = answer.user;
Iif (answer.user) connection.remoteUser = answer.user;
// set answer
Iif (!answer.sdi) throw new Error('sdi falsy on received answer')
await pc.setRemoteDescription(answer.sdi)
await connectionOpenPromise;
// connection is now established and data connection ready to use
// console.log(`connection to peer established!`, { deviceId: connection.remoteDeviceId, userId: connection.remoteUser?.id });
events.deviceConnected.emit(connection);
checkPendingInvitations(connection);
return connection;
}
catch (err) {
console.error('error connecting to device', toDeviceId, err);
throw err;
}
}
async function handelOffer(offer: ISDIExchange) {
try {
const rtcConfig: RTCConfiguration = {
// peerIdentity: offer.connectionId,
iceServers: await getIceServers()
}
// build answer connection
const pc2 = new RTCPeerConnection(rtcConfig);
const pendingDCConns: { [label: string]: ((dc: RTCDataChannel) => any) } = {};
const availableDCConns: { [label: string]: RTCDataChannel } = {};
// add connection to list
const connection: IDeviceConnection = {
id: offer.connectionId,
remoteDeviceId: offer.fromDevice,
send: null,
close: () => closeConnection(connection),
pc: pc2,
dc: null,
lastAck: Date.now(),
onAnswer: null,
handlers: {},
remoteUser: offer.user,
me: me,
waitForDataChannel: label => new Promise<RTCDataChannel>((resolve) => {
if (availableDCConns[label]) {
resolve(availableDCConns[label])
} else {
pendingDCConns[label] = resolve;
}
}),
}
deviceConnections[connection.remoteDeviceId] = connection;
// TODO again, maybe add a promise to say when the connection is actually open
// a lot of this feels like a duplicate of `connectToDevice` and can probably be merged into a shared function
// gather ice candidates
const iceCandidates: RTCIceCandidate[] = [];
// send any additional ice candidates through the signalling channel
pc2.onicecandidate = e => {
Iif (!e.candidate) return;
iceCandidates.push(e.candidate);
sendIceCandidate({
connectionId: offer.connectionId,
fromDevice: deviceId,
toDevice: offer.fromDevice,
iceCandidates: iceCandidates,
sdi: null
})
}
await pc2.setRemoteDescription(offer.sdi);
// build answer
const sdi = await pc2.createAnswer();
Iif (!sdi) return alert('generated falsy sdi answer: ' + JSON.stringify(sdi));
await pc2.setLocalDescription(sdi);
// add any known ice candidates
Iif (earlyIceCandidates[connection.id]) {
console.log('found early ice candidates');
for (const ic of earlyIceCandidates[connection.id]) {
await connection.pc.addIceCandidate(ic);
}
delete earlyIceCandidates[connection.id];
}
// send answer
sendAnswer({
connectionId: offer.connectionId,
fromDevice: deviceId,
toDevice: offer.fromDevice,
iceCandidates,
sdi
})
// listen for data connections
pc2.ondatachannel = e => {
let dc: RTCDataChannel = e.channel;
if (dc.label == `${connection.id}-data`) {
connection.dc = dc;
connection.send = data => dcSend(connection, data);
dc.onmessage = e => onRemoteMessage(connection, e.data);
dc.onopen = e => {
console.log('dc2 connection open to', { deviceId: connection.remoteDeviceId, userId: connection.remoteUser?.id })
events.deviceConnected.emit(connection);
checkPendingInvitations(connection);
}
dc.onclose = e => {
console.log("dc2.onclose", { deviceId: connection.remoteDeviceId, userId: connection.remoteUser?.id })
connection.close();
};
} else {
dc.onopen = e => {
console.log('pc2 data channel open', dc.label);
Iif (pendingDCConns[dc.label]) {
pendingDCConns[dc.label](dc);
delete pendingDCConns[dc.label];
}
availableDCConns[dc.label] = dc;
};
dc.onclose = e => {
delete availableDCConns[dc.label];
console.log('pc2 data channel closed', dc.label);
}
}
}
}
catch (err) {
console.error('error handling offer', err);
throw err;
}
}
async function handelAnswer(answer: ISDIExchange) {
const connection = connections().find(c => c.id == answer.connectionId)
if (connection) connection.onAnswer(answer);
else console.log('could not find connection for answer', answer);
}
export function emit(messageType: string, args: any) {
// TODO try to do through peer connections first
return new Promise((resolve, reject) => {
socket.emit(messageType, args, (err, res) => {
if (err) reject(err);
else resolve(res);
})
})
}
const onMessageHandlers: { [messageType: string]: ((...args) => any)[] } = {};
export function onMessage(messageType: string, handler: (...args) => any) {
// TODO somehow link this to RPC for devices
onMessageHandlers[messageType] = onMessageHandlers[messageType] ?? [];
onMessageHandlers[messageType].push(handler);
Iif (socket) {
socket.on(messageType, args => {
onMessageHandlers[messageType]?.forEach(async handler => {
try {
await handler(args)
} catch (err) {
console.log('error while handling message', err);
}
})
})
}
}
// @ts-ignore
Iif (typeof window !== 'undefined') window.deviceConnections = deviceConnections; |