Skip to content

Commit

Permalink
conversion to new communication system in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal Web committed Jul 7, 2023
1 parent 15f2de1 commit d29c502
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 638 deletions.
7 changes: 6 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@
"new-cap": "error",
"new-parens": "error",
"newline-before-return": "off",
"newline-per-chained-call": "error",
"newline-per-chained-call": [
"error",
{
"ignoreChainWithDepth": 3
}
],
"no-array-constructor": "error",
"no-bitwise": "error",
"no-continue": "off",
Expand Down
791 changes: 194 additions & 597 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packetDesign.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
[
// HEAD
{
cert,
connectionID
},
// BODY
{
method
data
},
// FOOTER
Expand Down
19 changes: 15 additions & 4 deletions scripts/certificates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const dirname = currentPath(import.meta);
const domainProfile = await createProfile({
template: {
ephemeral: {
// udsp is the version number of udsp
// if left blank or true it would automatically select the most recent version of the UW/UDSP protocol
// version numbers can be used for the udsp property
udsp: 1,
// certificate version
version: 1,
// Crypto can be explicit or use shorthand like this algo: 'default' The default is listed below
// Encryption algorithm must be an AEAD algorithm (Authenticated Encryption with Associated Data) such as xchacha20poly1305
Expand Down Expand Up @@ -32,14 +37,16 @@ const domainProfile = await createProfile({
// encryptServerKey: 'sealedbox',
encryptKeypair: true
},
// Max connection id size in bytes
// Max connection id size in bytes usually randomly generated and checked but used to calculate max packet payload size
connectionIdSize: 8,
// Max payload (head or data) size in bytes
// maxPayloadSize: 1200,
// Max Payload size
// maxDataSize: 1200,
// Max size of the header payload
// maxHeadSize: 1200,
// heartbeat is an interval check for when a client must send something to the server to remain connected
heartbeat: 30000,
ip: '::1',
port: 8888,
domain: 'universal.web',
Expand All @@ -52,11 +59,15 @@ const domainProfile = await createProfile({
},
locality: {
state: 'FL',
country: 'US'
country: 'US',
zip: '00000',
town: 'UW Township',
county: 'UW County',
},
crypto: {
type: 'viat',
puzzles: 'enabled',
// enable viat puzzles as a form of congestion control
puzzles: true,
curve: '25519',
// When publicKey is set to true it will use the public key in the certificate as the main Viat wallet for the domain. If a string is provided then it would be the main wallet for the domain.
publicKey: true
Expand All @@ -65,7 +76,7 @@ const domainProfile = await createProfile({
realtime: true,
// blocked methods mean methods the server doesn't permit
// blockedMethods: ['open'],
allowedMethods: ['get', 'connect', 'open', 'close'],
allowedMethods: ['get', 'connect', 'open', 'file', 'stream', 'close'],
compression: true,
headerCompression: true,
autoLogin: true,
Expand Down
16 changes: 13 additions & 3 deletions scripts/simulateClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,31 @@ const uwClient = await client({
// Load Profile Certificate from file
certificate: `${currentPath(import.meta)}/../profiles/default-Ephemeral.cert`,
});
// await client('universalweb.io', {keychain: 'Universal Web Profile'});
// await request('universalweb.io/index.html');
console.timeEnd('Connected');
// console.log('INTRO =>', uwClient);
console.time('FileRequest');
// short hand get request
const fileRequest = await uwClient.request('index.html');
// medium hand
// const fileRequest = await uwClient.request({
// path: 'index.html'
// });
// const fileRequest = await uwClient.file('index.html');
// Get Method
fileRequest.on({
data(...args) {
console.log('onData for simulate client', ...args);
console.log('custom onData event', ...args);
},
head(...args) {
console.log('custom onHead event', ...args);
}
});
const response = await fileRequest.send();
// const response = await uwClient.request('index.html').on({
// data(...args) {
// console.log('onData for simulate client', ...args);
// }
// }).send();
console.log('head', response.head);
console.log('data', response.data);
console.timeEnd('FileRequest');
Expand Down
38 changes: 33 additions & 5 deletions udsp/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@ import { construct, UniqID } from '@universalweb/acid';
import { actions } from './server/actions/index.js';
import { cryptography } from '#udsp/cryptography';
import dgram from 'dgram';
import { randomConnectionId } from '#crypto';
class UDSP {
constructor() {
return this.initialize();
}
async calculatePacketOverhead() {
const packetOverhead = 2;
this.encryptPacketOverhead = this.cryptography.encryptOverhead;
this.packetOverhead = packetOverhead + this.encryptPacketOverhead + this.connectionIdSize;
this.packetMaxPayload = this.maxPacketSize - this.packetOverhead;
this.packetMaxPayloadSafeEstimate = this.packetMaxPayload - 10;
const {
connectionIdSize,
maxPayloadSize,
maxDataSize,
maxHeadSize
} = this;
if (maxPayloadSize) {
if (!maxDataSize) {
this.maxDataSize = maxPayloadSize;
}
if (!maxHeadSize) {
this.maxHeadSize = maxPayloadSize;
}
} else {
const packetInitialOverhead = 2;
this.encryptPacketOverhead = this.cryptography.encryptOverhead;
this.packetOverhead = packetInitialOverhead + this.encryptPacketOverhead + this.connectionIdSize;
this.maxPayloadSize = this.maxPacketSize - this.packetOverhead;
this.maxPayloadSizeSafeEstimate = this.maxPayloadSize - 10;
if (!maxDataSize) {
this.maxDataSize = this.packetMaxPayload;
}
if (!maxHeadSize) {
this.maxHeadSize = this.packetMaxPayload;
}
}
console.log(`Max Packet Size: ${this.maxPacketSize} bytes`);
console.log(`Max Payload Size: ${this.maxPayloadSize} bytes`);
console.log(`Packet Overhead: ${this.packetOverhead} bytes`);
console.log(`connectionIdSize Overhead: ${this.connectionIdSize} bytes`);
}
generateConnectionID() {
const target = randomConnectionId(this.connectionIdSize || 8);
return target;
}
async setupSocket() {
const ipVersion = this.ipVersion;
const socket = dgram.createSocket(ipVersion);
Expand Down
2 changes: 1 addition & 1 deletion udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ export class Client extends UDSP {
success(`clientId:`, this.idString);
await this.setDestination();
await this.setCertificate();
await this.calculatePacketOverhead();
await this.configCryptography();
await this.calculatePacketOverhead();
await this.setupSocket();
await this.attachEvents();
Client.connections.set(this.idString, this);
Expand Down
8 changes: 1 addition & 7 deletions udsp/cryptography.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ class Cryptography {
convertEd25519ToX25519,
connectionIdKeypair
} = cryptographyConfig;
const {
generate, connectionIdSize
} = config;
const { generate } = config;
if (alias === 'default') {
aead = 'xchacha20poly1305';
signature = 'ed25519';
Expand Down Expand Up @@ -205,10 +203,6 @@ class Cryptography {
hashMin(...args) {
return this.hashMinMethod(...args);
}
generateConnectionID() {
const target = randomConnectionId(this.config.connectionIdSize || 8);
return target;
}
signKeypair(...args) {
return this.signKeypairMethod(...args);
}
Expand Down
13 changes: 8 additions & 5 deletions udsp/request/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,33 @@ export class Base {
}
async headPacketization() {
const {
packetMaxPayloadSafeEstimate,
maxHeadSize,
id: sid,
isAsk,
outgoingHeadPackets
} = this;
const message = (this.isAsk) ? this.request : this.response;
let currentBytePosition = 0;
let packetId = 0;
const headSize = this.outgoingHeadSize;
while (currentBytePosition < this.outgoingHeadSize) {
const packet = assign({}, this.packetTemplate);
packet.sid = sid;
packet.pid = packetId;
packet.head = this.outgoingHead.subarray(currentBytePosition, currentBytePosition + packetMaxPayloadSafeEstimate);
const endIndex = currentBytePosition + maxHeadSize;
const safeEndIndex = endIndex > headSize ? headSize : endIndex;
packet.head = this.outgoingHead.subarray(currentBytePosition, safeEndIndex);
packet.headSize = packet.head.length;
packetId++;
currentBytePosition += packetMaxPayloadSafeEstimate;
outgoingHeadPackets.push(packet);
currentBytePosition += maxHeadSize;
outgoingHeadPackets[packetId] = packet;
}
}
async dataPacketization() {
const {
packetMaxPayloadSafeEstimate,
packetTemplate,
maxPacketSize,
maxDataSize,
sid,
isAsk,
isReply
Expand Down
18 changes: 9 additions & 9 deletions udsp/request/dataPacketization.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { buildMessage } from './buildMessage.js';
import { request } from '#udsp/request';
export async function dataPacketization(source) {
const {
maxPacketSize,
maxDataSize,
id: sid,
isAsk,
outgoingPackets
outgoingDataPackets
} = source;
const message = (isAsk) ? source.request : source.response;
const data = message.data;
const dataSize = data?.length;
let currentBytePosition = 0;
let packetId = outgoingPackets.length;
if (dataSize > maxPacketSize) {
let packetId = outgoingDataPackets.length;
if (dataSize > maxDataSize) {
console.log('data size', data.length);
while (currentBytePosition < dataSize) {
const endIndex = currentBytePosition + maxPacketSize;
const endIndex = currentBytePosition + maxDataSize;
const safeEndIndex = endIndex > dataSize ? dataSize : endIndex;
const chunk = data.subarray(currentBytePosition, safeEndIndex);
console.log('chunksize', chunk.length, currentBytePosition, endIndex);
Expand All @@ -26,12 +26,12 @@ export async function dataPacketization(source) {
sid
};
packet.data = chunk;
outgoingPackets[packetId] = outgoingPackets;
outgoingDataPackets[packetId] = outgoingDataPackets;
if (endIndex >= dataSize) {
packet.end = true;
break;
}
currentBytePosition = currentBytePosition + maxPacketSize;
currentBytePosition = currentBytePosition + maxDataSize;
packetId++;
}
} else {
Expand All @@ -41,7 +41,7 @@ export async function dataPacketization(source) {
data
};
console.log(source);
outgoingPackets[0] = packet;
outgoingDataPackets[0] = packet;
}
console.log('bufferToPackets', outgoingPackets);
console.log('bufferToPackets', outgoingDataPackets);
}
31 changes: 26 additions & 5 deletions udsp/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { onPacket } from './onPacket.js';
import { sendPacket } from '#udsp/sendPacket';
import { actions } from './actions/index.js';
import { getCertificate, parseCertificate } from '#certificate';
import { randomConnectionId, signKeypairToEncryptKeypair } from '#crypto';
import { randomBuffer } from '#crypto';
import { cryptography } from '#udsp/cryptography';
import { UDSP } from '#udsp/base.js';
const { seal } = Object;
Expand Down Expand Up @@ -62,9 +62,15 @@ export class Server extends UDSP {
}
async setCertificate() {
const {
certificate,
connectionIdSize
} = this.configuration;
configuration,
configuration: {
certificate,
connectionIdSize,
maxPayloadSize,
maxDataSize,
maxHeadSize
}
} = this;
if (certificate) {
this.certificate = await parseCertificate(certificate);
console.log(this.certificate);
Expand All @@ -81,6 +87,21 @@ export class Server extends UDSP {
this.connectionIdSize = this.certificate.connectionIdSize;
}
}
if (!maxPayloadSize) {
if (this.certificate.maxPayloadSize) {
this.maxPayloadSize = this.certificate.maxPayloadSize;
}
}
if (!maxDataSize) {
if (this.certificate.maxDataSize) {
this.maxDataSize = this.certificate.maxDataSize;
}
}
if (!maxHeadSize) {
if (this.certificate.maxHeadSize) {
this.maxHeadSize = this.certificate.maxHeadSize;
}
}
if (this.certificate.cryptography) {
const cryptoConfig = assign({
keypair: this.keypair,
Expand Down Expand Up @@ -114,7 +135,7 @@ export class Server extends UDSP {
this.configuration = seal(assign({}, configuration));
info(this.configuration);
if (!this.id) {
this.id = randomConnectionId(4);
this.id = randomBuffer(4);
} else if (isFunction(this.id)) {
this.id = await this.id();
}
Expand Down

0 comments on commit d29c502

Please sign in to comment.