Skip to content

Commit

Permalink
Hex Based Ids
Browse files Browse the repository at this point in the history
Smart ID Creation
Smart Id Hex 90% Intigrated
Smaller Client Connection Id
Default Server-Client Connection Id size
CharSet header for toString for specific decoding
  • Loading branch information
tomekmarchi committed Nov 1, 2023
1 parent 16eb39d commit cf212dc
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 68 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@universalweb/docredux": "^3.2.2",
"auto-changelog": "^2.4.0",
"axios": "^1.5.1",
"benchmark": "^2.1.4",
"brotli": "^1.3.3",
"compression": "^1.7.4",
"electron": "^23.1.3",
Expand Down Expand Up @@ -122,4 +123,4 @@
"devDependencies": {
"@electron-forge/cli": "^6.0.5"
}
}
}
2 changes: 1 addition & 1 deletion scripts/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const hostname = `[${serverIp}]`;
const port = 8888;
import fs from 'fs';
import { currentPath } from '@universalweb/acid';
import fetchIt from 'node-fetch';
const dirname = currentPath(import.meta);
const options = {
key: fs.readFileSync(`${currentPath(import.meta)}/key.pem`),
Expand All @@ -20,7 +21,6 @@ const server = https.createServer(options, (req, res) => {
server.listen(port, serverIp, () => {
console.log(`Server running at ${serverIp}:${port}`);
});
import fetchIt from 'node-fetch';
console.time('http');
const response = await fetchIt('https://[::1]:8888/');
const body = await response.text();
Expand Down
18 changes: 10 additions & 8 deletions udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { getAlgorithm, processPublicKey } from '../cryptoMiddleware/index.js';
import { getWANIPAddress } from '../../utilities/network/getWANIPAddress.js';
import { getLocalIpVersion } from '../../utilities/network/getLocalIP.js';
import { calculatePacketOverhead } from '../calculatePacketOverhead.js';
import { generateConnectionId, connectionIdToBuffer } from '#udsp/connectionId';
let ipInfo;
let globalIpVersion;
try {
Expand Down Expand Up @@ -282,26 +283,27 @@ export class Client extends UDSP {
return this;
}
assignId() {
this.id = randomConnectionId(this.destination.clientConnectionIdSize || 4);
this.connectionIdSize = this.id.length;
this.idString = this.id.toString('base64');
success(`Assigned ClientId ${this.idString}`);
Client.connections.set(this.idString, this);
const connectionIdString = generateConnectionId(this.connectionIdSize);
this.connectionIdSize = this.destination.clientConnectionIdSize || 4;
this.id = connectionIdToBuffer(connectionIdString);
this.connectionIdString = connectionIdString;
success(`Assigned ClientId ${connectionIdString}`);
Client.connections.set(connectionIdString, this);
}
async calculatePacketOverhead() {
return calculatePacketOverhead(this.cipherSuite, this.destination.connectionIdSize, this.destination);
}
reKey() {
const thisClient = this;
success(`client reKeyed -> ID: ${thisClient.idString}`);
success(`client reKeyed -> ID: ${thisClient.connectionIdString}`);
}
close(message, obj) {
if (obj) {
console.log(obj);
}
console.trace(this.idString, `client closed. code ${message?.state || message}`);
console.trace(this.connectionIdString, `client closed. code ${message?.state || message}`);
this.socket.close();
Client.connections.delete(this.idString);
Client.connections.delete(this.connectionIdString);
}
async send(message, headers, footer) {
console.log(`client.send to Server`, this.destination.ip, this.destination.port);
Expand Down
32 changes: 32 additions & 0 deletions udsp/connectionId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Benchmark from 'benchmark';
import { hasValue } from '@universalweb/acid';
import { randomConnectionId } from '#utilities/crypto';
const eight = 8;
export function generateConnectionId(size = eight, prepend) {
const characters = 'abcdef0123456789';
let result = '';
const charactersLength = characters.length;
const hexSize = size * 2;
for (let i = 0; i < hexSize; i++) {
const randomIndex = Math.floor(Math.random() * charactersLength);
result += characters.charAt(randomIndex);
}
if (hasValue(prepend)) {
result = `${prepend}${result.substring(prepend.length)}`;
}
return result;
}
export function connectionIdToBuffer(source) {
return Buffer.from(source, 'hex');
}
export function connectionIdToString(source) {
return source.toString('hex');
}
export function getConnectionIdReservedSpace(source, size) {
return source.subarray(0, size);
}
export function getConnectionIdReservedSpaceString(source, size) {
return source.subarray(0, size).toString('hex');
}
// const serverConnectionIdString = generateConnectionId(8, '01');
// console.log(connectionIdToBuffer(serverConnectionIdString).toString('hex'), connectionIdToBuffer(serverConnectionIdString).length);
14 changes: 12 additions & 2 deletions udsp/encoding/decodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
} from '@universalweb/acid';
import { toBase64 } from '#crypto';
import { createClient } from '../server/clients/index.js';
/**
* @TODO
* Add support to block connection Ids that are too large
* Add support to block connection Ids that are too small
*/
export async function decodePacketHeaders(config) {
const {
source,
Expand Down Expand Up @@ -71,7 +76,12 @@ export async function decodePacketHeaders(config) {
}
}
}
info(`clientId: ${toBase64(id)}`);
if (isClient) {
console.log(`Decode client side with id: ${destination.id.toString('hex')}`);
console.log(`Decode client side with Server-Client-id: ${source?.id?.toString('hex')}`);
} else {
console.log(`Decode Server side packet with id: ${id.toString('hex')}`);
}
if (!isShortHeaderMode) {
const headerRPC = headerDecoded[1];
if (headerRPC) {
Expand Down Expand Up @@ -148,7 +158,7 @@ export async function decodePacket(config) {
if (!decryptedMessage) {
return console.trace('Encryption failed');
}
info(`decrypted Message size ${decryptedMessage.length}bytes`);
info(`decrypted Message size ${decryptedMessage.length} BYTES`);
const message = decode(decryptedMessage);
if (!hasValue(message)) {
return console.trace('No Message in Packet');
Expand Down
16 changes: 9 additions & 7 deletions udsp/encoding/encodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import {
} from '@universalweb/acid';
import { toBase64 } from '#crypto';
/**
* @todo
* Set Smart Id Routing up
* Set up short headers with array of headers key then id alone in array applicable at startup only
* Allow complex headers being objects long header mode
* Way for packet to be as a whole buffer First check the first 8 bytes for connection id if not then parse whole packet.
* @todo
* - Set Smart Id Routing up.
* - If the packet is a request, then it should be sent to the server.
*/
// [id, RPC, (...[] || {})] (Arrays are flattened while objects are not)
// [id, RPC, ...Args]
export async function encodePacket(message = Buffer.from(0), source, destination, headers, footer) {
success(`PROCESSING TO ENCODE PACKET`);
const {
Expand Down Expand Up @@ -67,7 +65,11 @@ export async function encodePacket(message = Buffer.from(0), source, destination
if (message) {
console.log(message);
}
info(`clientId: ${toBase64(id)}`);
if (isClient) {
info(`Encode client side with id: ${id.toString('hex')}`);
} else {
info(`Decode Server side with Server-Client-id: ${id.toString('hex')}`);
}
info(`Transmit Key ${toBase64(source.sessionKeys.transmitKey)}`);
const headerEncoded = (shortHeaderMode) ? header : encode(header);
const messageEncoded = encode(message);
Expand Down
6 changes: 4 additions & 2 deletions udsp/request/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import { onHead } from './onHead.js';
import { onParameters } from './onParameters.js';
import { fire } from './events/fire.js';
/**
* @todo Adjust packet size to account for other packet data.
* @todo
* Add support for headers which indicate the headers content encoding?
* Add convertor for serialize header so to convert their normal string names to their numerical ids.
*/
export class Base {
constructor(source) {
Expand Down Expand Up @@ -379,7 +381,7 @@ export class Base {
if (source.data) {
this.outgoingData = source.data;
if (!isBuffer(source.data)) {
this.setHeader('serialize', true);
this.setHeader('serialize', 0);
this.outgoingData = encode(source.data);
}
this.outgoingDataSize = this.outgoingData.length;
Expand Down
35 changes: 15 additions & 20 deletions udsp/request/objects/dataMethods.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
eachObject, jsonParse, isTrue, noValue, assign, clear
eachObject, jsonParse, isTrue, noValue, assign, clear, hasValue
} from '@universalweb/acid';
import { decode } from '#utilities/serialize';
import { decode, encode } from '#utilities/serialize';
export const objectDataMethods = {
getters: {
data() {
Expand All @@ -11,12 +11,12 @@ export const objectDataMethods = {
if (noValue(this.dataBuffer)) {
return undefined;
}
const { head: { serialize } } = this;
const serialize = this.head?.serialize;
const dataConcatinated = Buffer.concat(this.dataBuffer);
if (serialize) {
if (isTrue(serialize) || serialize === 0) {
if (hasValue(serialize)) {
if (serialize === 0) {
this.compiledData = decode(dataConcatinated);
} else if (serialize === 1 || serialize === 'json') {
} else if (serialize === 1) {
this.compiledData = jsonParse(dataConcatinated);
}
dataConcatinated.fill(0);
Expand All @@ -43,7 +43,8 @@ export const objectDataMethods = {
if (noValue(this.dataBuffer)) {
return;
}
const target = this.data.toString();
const encodingHeader = this.head?.charset;
const target = this.data.toString(encodingHeader || 'utf8');
if (cache) {
this.toStringCached = target;
}
Expand All @@ -64,20 +65,15 @@ export const objectDataMethods = {
}
return jsonParse(this.data);
},
serialize(cache) {
if (cache) {
if (this.serializeCached) {
return this.serializeCached;
}
decode(cache) {
const encodingHeader = this.head?.charset;
if (encodingHeader) {
return this.toString(cache);
}
if (noValue(this.dataBuffer)) {
return;
}
const target = decode(this.data);
if (cache) {
this.serializeCached = target;
const serialize = this.head?.serialize;
if (serialize) {
return this.data;
}
return target;
},
toObject(cache) {
if (cache) {
Expand Down Expand Up @@ -115,4 +111,3 @@ export const objectDataMethods = {
});
}
};

8 changes: 4 additions & 4 deletions udsp/server/clients/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import {
success, failed, imported, msgSent, info, msgReceived
} from '#logs';
export async function destroy(client, reason, server) {
console.log(`client destroyed: ${client.idString}`);
console.log(`client destroyed: ${client.connectionIdString}`);
if (reason === 1) {
await client.send({
state: 3
});
info(`client ended from inactivity. Grace period ended.
ID: ${client.idString}
ID: ${client.connectionIdString}
Address: ${client.destination.ip}
Port: ${client.destination.port}
`);
} else if (reason === 0) {
info(`client ended due to natural causes.
ID: ${client.idString}
ID: ${client.connectionIdString}
Address: ${client.destination.ip}
Port: ${client.destination.port}
`);
}
await server.clientEvent('destroy', client);
server.clients.delete(client.idString);
server.clients.delete(client.connectionIdString);
// Clear all client data
client.ip = null;
client.port = null;
Expand Down
Loading

0 comments on commit cf212dc

Please sign in to comment.