From 80ac5c34e5a622a768399d5b540a40e5286c0869 Mon Sep 17 00:00:00 2001 From: Nieky Allen Date: Thu, 19 Oct 2023 00:57:50 -0500 Subject: [PATCH] progress on abstraction --- cli/actions/drop.ts | 8 ++++---- shared/handlers/drop.ts | 5 +++-- shared/types/common.ts | 18 ++++++++++++++++++ shared/types/drop.ts | 12 ++++-------- shared/types/grab.ts | 11 +++-------- shared/types/messages.ts | 4 +++- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/cli/actions/drop.ts b/cli/actions/drop.ts index 5a5bc8f..7f366ca 100644 --- a/cli/actions/drop.ts +++ b/cli/actions/drop.ts @@ -1,7 +1,6 @@ import inquirer from 'inquirer'; import { loader } from 'lib/loader'; import { displayWelcomeMessage, logDebug, logError, logInfo } from 'lib/log'; -import { initPeer } from 'lib/peer'; import { dropMachine, initDropContext } from '@shared/lib/machines/drop'; import { DropEventType, MessageType } from '@shared/lib/constants'; import { AnyDropEvent, InitDropEvent } from '@shared/types/drop'; @@ -40,12 +39,13 @@ export const drop = async (input: string | undefined, options: DropOptions) => { debug: logDebug, }, file: { - encrypt: encryptFile, + encrypt: (...args) => + encryptFile(args[0], args[1], args[2]).then((res) => res.data), hash: hashFile, }, - initPeer, cleanupSession, apiUri: process.env.DEADDROP_API_URL!, + peerServerUri: process.env.PEER_SERVER_URL!, }); ctx.message = input || options.input || null; @@ -65,7 +65,7 @@ export const drop = async (input: string | undefined, options: DropOptions) => { ctx.message = answer.input; } - await stagePayload(ctx.message as string, 'raw'); + await stagePayload(ctx.message as string, ctx.mode); loader.start('Initializing drop session...'); diff --git a/shared/handlers/drop.ts b/shared/handlers/drop.ts index f6815b1..616adde 100644 --- a/shared/handlers/drop.ts +++ b/shared/handlers/drop.ts @@ -28,6 +28,7 @@ import { DataConnection } from 'peerjs'; import { withMessageLock } from '../lib/messages'; import { deleteReq, post } from '../lib/fetch'; import { DROP_API_PATH } from '../config/paths'; +import { createPeer } from '../lib/peer'; export const createDropHandlers = ({ ctx, @@ -35,9 +36,9 @@ export const createDropHandlers = ({ sendEvent, logger, file, - initPeer, cleanupSession, apiUri, + peerServerUri, }: DropHandlerInputs) => { const dropApiUrl = apiUri + DROP_API_PATH; @@ -249,7 +250,7 @@ export const createDropHandlers = ({ const init = async () => { ctx.keyPair = await generateKeyPair(); - ctx.peer = await initPeer(); + ctx.peer = await createPeer(peerServerUri); ctx.peer.on('connection', onConnection); diff --git a/shared/types/common.ts b/shared/types/common.ts index a6a0121..804284a 100644 --- a/shared/types/common.ts +++ b/shared/types/common.ts @@ -1,5 +1,6 @@ import type { DataConnection } from 'peerjs'; import type Peer from 'peerjs'; +import { DropMessageMeta } from './messages'; export type BaseContext = { id: string | null; @@ -35,3 +36,20 @@ export type DropPayload = content: string; type: string; }; + +export type EncryptFile = ( + key: CryptoKey, + iv: string, + pathOrInput: InputType, +) => Promise; + +export type DecryptFile = ( + key: CryptoKey, + iv: string, + pathOrInput: InputType, + meta: DropMessageMeta, +) => Promise; + +export type HashFile = ( + pathOrInput: InputType, +) => Promise; diff --git a/shared/types/drop.ts b/shared/types/drop.ts index c19783f..9e41337 100644 --- a/shared/types/drop.ts +++ b/shared/types/drop.ts @@ -1,4 +1,4 @@ -import type { BaseContext } from './common'; +import type { BaseContext, EncryptFile, HashFile } from './common'; import type { EventObject } from 'xstate/lib/types'; import type Peer from 'peerjs'; import type { DataConnection } from 'peerjs'; @@ -68,14 +68,10 @@ export type DropHandlerInputs = { debug: (message: string) => void; }; file: { - encrypt: ( - key: CryptoKey, - iv: string, - pathOrInput: any, - ) => Promise; - hash: (pathOrInput: any) => Promise; + encrypt: EncryptFile; + hash: HashFile; }; - initPeer: () => Promise; cleanupSession: (ctx: DropContext) => void; apiUri: string; + peerServerUri: string; }; diff --git a/shared/types/grab.ts b/shared/types/grab.ts index 4429ee6..157580e 100644 --- a/shared/types/grab.ts +++ b/shared/types/grab.ts @@ -1,4 +1,4 @@ -import type { BaseContext } from './common'; +import type { BaseContext, DecryptFile, HashFile } from './common'; import { GrabEventType, MessageType } from '../lib/constants'; import type { EventObject } from 'xstate/lib/types'; import type Peer from 'peerjs'; @@ -45,13 +45,8 @@ export type GrabHandlerInputs = { debug: (message: string) => void; }; file: { - decrypt: ( - key: CryptoKey, - iv: string, - pathOrInput: any, - meta?: any, - ) => Promise; - hash: (pathOrInput: any) => Promise; + decrypt: DecryptFile; + hash: HashFile; }; initPeer: () => Promise; cleanupSession: (ctx: GrabContext) => void; diff --git a/shared/types/messages.ts b/shared/types/messages.ts index 4744c98..786945e 100644 --- a/shared/types/messages.ts +++ b/shared/types/messages.ts @@ -11,11 +11,13 @@ export interface HandshakeMessage extends BaseMessage { input: string; } +export type DropMessageMeta = { type: string; name: string }; + export interface DropMessage extends BaseMessage { type: MessageType.Payload; mode: PayloadMode; payload: string; - meta?: { type: string; name: string }; + meta?: DropMessageMeta; } export interface VerifyMessage extends BaseMessage {