Skip to content

Commit

Permalink
fix: ws
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-ko committed Oct 25, 2023
1 parent 3603bde commit e39a919
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
SIGNING_PUBLIC_KEY: ${SIGNING_PUBLIC_KEY}
SIGNING_PRIVATE_KEY: ${SIGNING_PRIVATE_KEY}
ENCRYPTION_PUBLIC_KEY: ${ENCRYPTION_PUBLIC_KEY}
ENCRYPTION_PRIVATE_KEY: ${ENCRYPTION_PUBLIC_KEY}
ENCRYPTION_PRIVATE_KEY: ${ENCRYPTION_PRIVATE_KEY}
RPC: ${RPC}
PORT: 8081
LOG_LEVEL: 'debug'
Expand Down
4 changes: 3 additions & 1 deletion docker/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ http {
}

location /socket.io {
proxy_pass http://backend:8081/socket.io/;
proxy_pass http://backend:8081/socket.io;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
Expand All @@ -69,5 +69,7 @@ http {
proxy_redirect off;
proxy_set_header Host $host;
}


}
}
1 change: 1 addition & 0 deletions packages/backend/src/delivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default () => {
const idEnsName = await req.app.locals.db.getIdEnsName(
req.params.ensName,
);

const idContactEnsName = await req.app.locals.db.getIdEnsName(
req.params.contactEnsName,
);
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ global.logger = winston.createLogger({
app.locals.io = io;

app.locals.keys = readKeysFromEnv(process.env);

app.locals.deliveryServiceProperties = getDeliveryServiceProperties();

app.locals.db = await getDatabase();
Expand Down
23 changes: 14 additions & 9 deletions packages/backend/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const pendingMessageSchema = {
export function onConnection(app: express.Application & WithLocals) {
return (socket: Socket) => {
socket.on('disconnect', () => {
app.locals.logger.info({
global.logger.info({
method: 'WS DISCONNECT',
socketId: socket.id,
});
Expand All @@ -36,7 +36,7 @@ export function onConnection(app: express.Application & WithLocals) {
callback,
) => {
try {
app.locals.logger.info({
global.logger.info({
method: 'WS INCOMING MESSAGE',
});

Expand All @@ -48,13 +48,18 @@ export function onConnection(app: express.Application & WithLocals) {
if (!isSchemaValid) {
const error = 'invalid schema';

app.locals.logger.warn({
global.logger.warn({
method: 'WS SUBMIT MESSAGE',
error,
});
return callback({ error });
}

global.logger.info({
method: 'WS INCOMING MESSAGE',
keys: app.locals.keys.encryption,
});

await incomingMessage(
data,
app.locals.keys.signing,
Expand All @@ -75,7 +80,7 @@ export function onConnection(app: express.Application & WithLocals) {
),
callback({ response: 'success' });
} catch (error: any) {
app.locals.logger.warn({
global.logger.warn({
method: 'WS SUBMIT MESSAGE',
error: (error as Error).toString(),
});
Expand All @@ -90,7 +95,7 @@ export function onConnection(app: express.Application & WithLocals) {
if (!isSchemaValid) {
const error = 'invalid schema';

app.locals.logger.warn({
global.logger.warn({
method: 'WS PENDING MESSAGE',
error,
});
Expand All @@ -109,15 +114,15 @@ export function onConnection(app: express.Application & WithLocals) {
contactEnsName,
);
} catch (error) {
app.locals.logger.warn({
global.logger.warn({
method: 'WS PENDING MESSAGE',
error,
});

return callback({ error });
}

app.locals.logger.info({
global.logger.info({
method: 'WS PENDING MESSAGE',
ensName,
contactEnsName,
Expand All @@ -132,7 +137,7 @@ export function onConnection(app: express.Application & WithLocals) {
))
) {
const error = 'Token check failed';
app.locals.logger.warn({
global.logger.warn({
method: 'WS PENDING MESSAGE',
error,
});
Expand All @@ -143,7 +148,7 @@ export function onConnection(app: express.Application & WithLocals) {

callback({ response: 'success' });
} catch (error) {
app.locals.logger.warn({
global.logger.warn({
method: 'WS PENDING MESSAGE',
error: (error as Error).toString(),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function socketAuth(app: Express & WithLocals) {
socket.handshake.auth.account.ensName,
);

app.locals.logger.info({
global.logger.info({
method: 'WS CONNECT',
ensName,
socketId: socket.id,
Expand Down
17 changes: 8 additions & 9 deletions packages/lib/delivery/src/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export async function getMessages(
contactEnsName: string,
) {
const account = normalizeEnsName(ensName);

const contact = normalizeEnsName(contactEnsName);

const conversationId = getConversationId(contact, account);

const receivedMessages: EncryptionEnvelop[] = await loadMessages(
Expand All @@ -66,14 +68,8 @@ export async function getMessages(
const envelopContainers = await Promise.all(
receivedMessages.map(async (envelop) => ({
to: normalizeEnsName(
JSON.parse(
await decryptAsymmetric(
encryptionKeyPair,
JSON.parse(
envelop.metadata.deliveryInformation as string,
),
),
).to,
JSON.parse(JSON.stringify(envelop.metadata.deliveryInformation))
.to,
),
envelop,
})),
Expand Down Expand Up @@ -111,7 +107,10 @@ export async function incomingMessage(
getIdEnsName: (name: string) => Promise<string>,
getUsersNotificationChannels: GetNotificationChannels,
): Promise<void> {
logDebug({ text: 'incomingMessage', token });
logDebug({
text: 'incomingMessage',
token,
});
//Checks the size of the incoming message
if (messageIsToLarge(envelop, sizeLimit)) {
throw Error('Message is too large');
Expand Down
4 changes: 3 additions & 1 deletion packages/messenger-widget/src/components/DM3/DM3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ function DM3(props: Dm3Props) {
throw Error('Could not get account profile');
}

const socket = socketIOClient(deliveryServiceUrl, {


const socket = socketIOClient(deliveryServiceUrl.replace('/api', ''), {
autoConnect: false,
transports: ['websocket'],
});
Expand Down

0 comments on commit e39a919

Please sign in to comment.