Skip to content

Commit

Permalink
WIP: secure sockets mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkabuki committed Oct 12, 2023
1 parent 8276fef commit 7f0af19
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/nest/socket/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
Community,
DeleteFilesFromChannelSocketPayload,
} from '@quiet/types'
import cors, { CorsOptions } from 'cors'
import EventEmitter from 'events'
import { CONFIG_OPTIONS, SERVER_IO_PROVIDER } from '../const'
import { ConfigOptions, ServerIoProviderTypes } from '../types'
Expand Down Expand Up @@ -132,6 +131,7 @@ export class SocketService extends EventEmitter implements OnModuleInit {
this.logger(`Creating network for community ${community.id}`)
this.emit(SocketActionTypes.CREATE_NETWORK, community)
})

socket.on(SocketActionTypes.LEAVE_COMMUNITY, async () => {
this.logger('leaving community')
this.emit(SocketActionTypes.LEAVE_COMMUNITY)
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/renderer/sagas/socket/socket.saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function subscribeSocketLifecycle(socket?: Socket) {
console.log('closing socket connection')
emit(socketActions.suspendConnection())
})
return () => {}
return () => { }
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters
withContext(Dispatchers.IO) {
// Get and store data port for usage in methods across the app
val dataPort = Utils.getOpenPort(11000)
val socketIOSecret = "secret"

// Init nodejs project
launch {
Expand All @@ -112,7 +113,7 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters
* In any case, websocket won't connect until data server starts listening
*/
delay(WEBSOCKET_CONNECTION_DELAY)
startWebsocketConnection(dataPort)
startWebsocketConnection(dataPort, socketIOSecret)
}

val dataPath = Utils.createDirectory(context)
Expand All @@ -122,7 +123,7 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters

val platform = "mobile"

startNodeProjectWithArguments("bundle.cjs --torBinary $torBinary --dataPath $dataPath --dataPort $dataPort --platform $platform")
startNodeProjectWithArguments("bundle.cjs --torBinary $torBinary --dataPath $dataPath --dataPort $dataPort --platform $platform --socketIOSecret $socketIOSecret")
}

println("FINISHING BACKEND WORKER")
Expand Down Expand Up @@ -190,10 +191,10 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters
notificationHandler.notify(message, username)
}

private fun startWebsocketConnection(port: Int) {
private fun startWebsocketConnection(port: Int, socketIOSecret: Any) {
Log.d("WEBSOCKET CONNECTION", "Starting on $port")
// Proceed only if data port is defined
val websocketConnectionPayload = WebsocketConnectionPayload(port)
val websocketConnectionPayload = WebsocketConnectionPayload(port, socketIOSecret)
CommunicationModule.handleIncomingEvents(
CommunicationModule.WEBSOCKET_CONNECTION_CHANNEL,
Gson().toJson(websocketConnectionPayload),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quietmobile.Scheme

data class WebsocketConnectionPayload (
val dataPort: Int
val dataPort: Int,
val socketIOSecret: Any
)
1 change: 1 addition & 0 deletions packages/mobile/src/store/init/init.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface InitCheckPayload {

export interface WebsocketConnectionPayload {
dataPort: number
socketIOSecret?: string
}

export interface CloseConnectionPayload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ import { io, Socket } from 'socket.io-client'
import { put, call, cancel, fork, takeEvery, FixedTask } from 'typed-redux-saga'
import { PayloadAction } from '@reduxjs/toolkit'
import { socket as stateManager } from '@quiet/state-manager'
import { encodeSecret } from '@quiet/common'
import { initActions } from '../init.slice'
import { eventChannel } from 'redux-saga'

export function* startConnectionSaga(
action: PayloadAction<ReturnType<typeof initActions.startWebsocketConnection>['payload']>
): Generator {
const { dataPort } = action.payload
const { dataPort, socketIOSecret } = action.payload

const socket = yield* call(io, `http://127.0.0.1:${dataPort}`)
yield* fork(handleSocketLifecycleActions, socket, dataPort)
console.log('socketIOSecret', socketIOSecret)

if (!socketIOSecret) return

const token = encodeSecret(socketIOSecret)
const socket = yield* call(io, `http://127.0.0.1:${dataPort}`, {
withCredentials: true,
extraHeaders: {
authorization: `Basic ${token}`,
},
})
yield* fork(handleSocketLifecycleActions, socket, dataPort)
// Handle opening/restoring connection
yield* takeEvery(initActions.setWebsocketConnected, setConnectedSaga, socket)
}
Expand Down Expand Up @@ -46,7 +56,7 @@ function subscribeSocketLifecycle(socket: Socket, dataPort: number) {
console.log('closing socket connection')
emit(initActions.suspendWebsocketConnection())
})
return () => {}
return () => { }
})
}

Expand Down

0 comments on commit 7f0af19

Please sign in to comment.