Skip to content

Commit

Permalink
onPacket Server side patch
Browse files Browse the repository at this point in the history
Packet Security
  • Loading branch information
Universal Web committed Aug 5, 2023
1 parent 8c20d4c commit c208d4f
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 26 deletions.
1 change: 0 additions & 1 deletion udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ export class Client extends UDSP {
autoConnect = false;
connect = connect;
static connections = new Map();
packetIdGenerator = construct(UniqID);
certChunks = [];
}
export async function client(configuration) {
Expand Down
4 changes: 2 additions & 2 deletions udsp/client/processMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function processMessage(packet, client) {
message,
footer
} = packet;
const { queue } = client;
const { requestQueue } = client;
const {
connectionClose,
state,
Expand All @@ -26,7 +26,7 @@ export async function processMessage(packet, client) {
return failed(`End event received from server disconnected closing client`);
}
// console.log(queue.keys());
const askObject = queue.get(sid);
const askObject = requestQueue.get(sid);
if (askObject) {
askObject.onPacket(packet);
} else {
Expand Down
4 changes: 1 addition & 3 deletions udsp/decodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ export async function decodePacketHeaders(config) {
let encryptConnectionId;
if (isServerEnd) {
encryptConnectionId = cryptography.config.encryptServerConnectionId;
info(`encrypt server ConnectionId ${encryptConnectionId}`);
} else {
encryptConnectionId = cryptography.config.encryptClientConnectionId;
info(`encrypt client ConnectionId ${encryptConnectionId}`);
}
const client = config.client;
info(`Packet Encoded Size ${packetEncoded.length}`);
Expand All @@ -56,7 +54,7 @@ export async function decodePacketHeaders(config) {
return failed(`No connection id in header -> Invalid Packet`);
}
if (connectionIdKeypair && encryptConnectionId) {
success('Server Connection ID Decrypted');
success('Connection ID Decrypted');
// console.log(destination);
if (isServerEnd) {
header.id = cryptography.decryptServerConnectionId(header.id, connectionIdKeypair);
Expand Down
12 changes: 3 additions & 9 deletions udsp/request/ask.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
promise, assign, omit,
eachArray, stringify,
get,
isBuffer, isPlainObject,
isArray, isMap, construct,
each, hasLength,
hasValue
promise, assign, omit, eachArray, stringify, get, isBuffer, isPlainObject, isArray, isMap, construct, each, hasLength, hasValue, UniqID
} from '@universalweb/acid';
import { decode, encode } from 'msgpackr';
import {
Expand All @@ -18,15 +12,15 @@ export class Ask extends Base {
super(options, source);
const {
requestQueue,
packetIdGenerator,
streamIdGenerator,
} = source;
const {
data,
method = 'get'
} = requestObject;
const head = requestObject.head || options.head;
console.log('Ask', requestObject);
const streamId = packetIdGenerator.get();
const streamId = streamIdGenerator.get();
this.request.sid = streamId;
this.request.method = method;
this.method = method;
Expand Down
3 changes: 2 additions & 1 deletion udsp/request/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export async function onPacket(packet) {
const source = this;
this.lastPacketTime = Date.now();
const { message } = packet;
console.log(packet);
const {
// main data payload
data,
Expand Down Expand Up @@ -33,7 +34,7 @@ export async function onPacket(packet) {
dataReady,
last
} = message;
console.log(`Stream Id ${streamId}`);
console.log(`onPacket Stream Id ${streamId}`);
this.totalIncomingPackets++;
if (this.ok) {
return;
Expand Down
9 changes: 3 additions & 6 deletions udsp/request/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ export class Reply extends Base {
const { sid } = message;
// console.log(source);
// // console.log(message);
const {
queue,
packetIdGenerator
} = source;
const { requestQueue, } = source;
this.sid = sid;
this.id = sid;
this.response.sid = sid;
queue.set(sid, this);
this.onPacket(message);
requestQueue.set(sid, this);
this.onPacket(request);
}
isReply = true;
async complete() {
Expand Down
5 changes: 3 additions & 2 deletions udsp/server/clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class Client {
}
// server intro to client with connection id and new keypair
async sendIntro(introMessage) {
info(`Client Intro Sent -> - ID:${this.idString}`);
info(`Client Intro Sent -> - ID:${this.idString}`, introMessage);
if (isFalsy(this.newKeypairGenerated)) {
this.generateSessionKeypair();
this.newKeypairGenerated = true;
Expand Down Expand Up @@ -122,6 +122,7 @@ export class Client {
}
proccessProtocolPacket(message) {
info(`Client Intro -> - ID:${this.idString}`);
console.log(message);
const {
intro,
certRequest,
Expand All @@ -140,11 +141,11 @@ export class Client {
isServerClient = true;
isServerEnd = true;
pending = false;
packetIdGenerator = construct(UniqID);
state = 0;
encryptConnectionId = false;
randomId = randomBuffer(8);
privateData = {};
requestQueue = construct(Map);
}
export async function createClient(config) {
const client = await construct(Client, [config]);
Expand Down
4 changes: 2 additions & 2 deletions udsp/server/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
success, failed, imported, msgSent, info, msgReceived
} from '#logs';
import { toBase64 } from '#crypto';
import { isEmpty } from '@universalweb/acid';
import { isEmpty, hasValue } from '@universalweb/acid';
import { decodePacket, decodePacketHeaders } from '#udsp/decodePacket';
import { createClient } from './clients/index.js';
import { reply } from '#udsp/request/reply';
Expand Down Expand Up @@ -55,7 +55,7 @@ export async function onPacket(packet, connection) {
header,
message
} = config.packetDecoded;
if (message.sid) {
if (hasValue(message?.sid)) {
reply(config.packetDecoded, client);
} else {
client.proccessProtocolPacket(message, header);
Expand Down

0 comments on commit c208d4f

Please sign in to comment.