Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple invitation addresses #1686

Merged
merged 36 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8ba87a5
Implement utils for parsing and creating invitation code with multipl…
EmiM Aug 3, 2023
2e69186
Merge branch 'develop' into feature/1557
EmiM Aug 8, 2023
7510cab
Adjust code to multiple addresses
EmiM Aug 10, 2023
ee91ad1
Merge branch 'develop' into feature/1557
EmiM Aug 11, 2023
525383a
Adjust code to display multiple addresses in invitation url
EmiM Aug 11, 2023
cf60b7f
Merge branch 'feature/1556' into feature/1557
EmiM Aug 17, 2023
b3e59c5
Do not launch registrar, do not wait for certificates to join community
EmiM Aug 21, 2023
28ba290
Add eventlog DB for CSRs; Create invitation link basing on user's dat…
EmiM Aug 23, 2023
2e2d3b5
Remove tmp fix for sendCommand
EmiM Aug 24, 2023
289d9df
Merge branch 'develop' into feature/1557
EmiM Aug 24, 2023
30ec894
Merge branch 'fix/async-tor' into feature/1557
EmiM Aug 24, 2023
bb30494
Fix community double launch
vinkabuki Aug 25, 2023
f2972df
Simulate registrar by creating certificate on csr replication
EmiM Aug 27, 2023
ee55f92
Update own certificate on replication
vinkabuki Aug 29, 2023
17e2903
Merge branch 'develop' into feature/1557
EmiM Sep 4, 2023
51cbda4
Cleanup
EmiM Sep 4, 2023
61b78d8
Check if peerid and onion address are valid in deep link
EmiM Sep 4, 2023
fe1eac5
Validate invitation codes format in url pasted in join screen; cleanu…
EmiM Sep 4, 2023
56e4faf
Cleanup
EmiM Sep 6, 2023
6bb6f49
Add community metadata to database
EmiM Sep 7, 2023
0928c02
Do not save same csr twice
EmiM Sep 7, 2023
99e8a46
Merge branch 'develop' into feature/1557
EmiM Sep 7, 2023
dd11cd6
Fix handleInvitationCode test; revive warning modal when no code is v…
EmiM Sep 7, 2023
f1fe680
Temporarly do not show "invitation code invalid" modal
EmiM Sep 7, 2023
912e3de
Rename address -> onionAddress in InvitationPair type
EmiM Sep 7, 2023
985278d
Cleanup
EmiM Sep 7, 2023
974324a
Fix
EmiM Sep 7, 2023
1cd5094
Fix backend tests
EmiM Sep 7, 2023
9a2b402
Fix
EmiM Sep 7, 2023
aed8825
Fix state-manager tests
EmiM Sep 7, 2023
92182af
Fix or skip desktop tests
EmiM Sep 7, 2023
6232b9c
Uncomment deeplink test on mobile
EmiM Sep 7, 2023
d4d60e5
Adjust e2e tests
EmiM Sep 7, 2023
af8003b
Merge branch 'develop' into feature/1557
EmiM Sep 7, 2023
5022d62
Fix/skip deepLink mobile tests
EmiM Sep 11, 2023
7362b47
Update changelog
EmiM Sep 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions packages/backend/src/nest/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TestConfig } from '../const'
import logger from './logger'
import { createCertificatesTestHelper } from './client-server'
import { Libp2pNodeParams } from '../libp2p/libp2p.types'
import { createLibp2pAddress, createLibp2pListenAddress } from '@quiet/common'
const log = logger('test')

export interface Ports {
Expand Down Expand Up @@ -143,14 +144,6 @@ export const torDirForPlatform = (basePath?: string): string => {
return torPath
}

export const createLibp2pAddress = (address: string, peerId: string) => {
return `/dns4/${address}/tcp/80/ws/p2p/${peerId}`
}

export const createLibp2pListenAddress = (address: string) => {
return `/dns4/${address}/tcp/80/ws`
}

export const getUsersAddresses = async (users: User[]): Promise<string[]> => {
const peers = users.map(async (userData: User) => {
return createLibp2pAddress(userData.onionAddress, userData.peerId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import crypto from 'crypto'
import { CustomEvent } from '@libp2p/interfaces/events'
import { jest, beforeEach, describe, it, expect, afterEach } from '@jest/globals'
import { communities, getFactory, identity, prepareStore, Store } from '@quiet/state-manager'
import {
createLibp2pAddress,
createPeerId,
createTmpDir,
libp2pInstanceParams,
removeFilesFromDir,
tmpQuietDirPath,
} from '../common/utils'
import { createPeerId, createTmpDir, libp2pInstanceParams, removeFilesFromDir, tmpQuietDirPath } from '../common/utils'

import { NetworkStats, type Community, type Identity, type InitCommunityPayload } from '@quiet/types'
import { LazyModuleLoader } from '@nestjs/core'
Expand All @@ -37,6 +30,7 @@ import { DateTime } from 'luxon'
import waitForExpect from 'wait-for-expect'
import { Libp2pEvents } from '../libp2p/libp2p.types'
import { sleep } from '../common/sleep'
import { createLibp2pAddress } from '@quiet/common'

jest.setTimeout(100_000)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
if (community) {
await this.launchCommunity(community)
}
if (registrarData) {
await this.registrationService.launchRegistrar(registrarData)
}
}

public async closeAllServices(options: { saveTor: boolean } = { saveTor: false }) {
Expand Down Expand Up @@ -282,12 +279,14 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
}

public async createCommunity(payload: InitCommunityPayload) {
console.log('ConnectionsManager.createCommunity peers:', payload.peers)
await this.launchCommunity(payload)
this.logger(`Created and launched community ${payload.id}`)
this.serverIoProvider.io.emit(SocketActionTypes.NEW_COMMUNITY, { id: payload.id })
}

public async launchCommunity(payload: InitCommunityPayload) {
console.log('ConnectionsManager.launchCommunity peers:', payload.peers)
this.communityState = ServiceState.LAUNCHING
const communityData: InitCommunityPayload = await this.localDbService.get(LocalDBKeys.COMMUNITY)
if (!communityData) {
Expand Down Expand Up @@ -336,6 +335,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
const _peerId = await peerIdFromKeys(restoredRsa.marshalPubKey(), restoredRsa.marshalPrivKey())

let peers = payload.peers
console.log('LAUNCH COMMUNITY PAYLOAD PEERS', peers)
if (!peers || peers.length === 0) {
peers = [this.libp2pService.createLibp2pAddress(onionAddress, _peerId.toString())]
}
Expand All @@ -344,9 +344,6 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
peerId: _peerId,
listenAddresses: [this.libp2pService.createLibp2pListenAddress(onionAddress)],
agent: this.socksProxyAgent,
cert: payload.certs.certificate,
key: payload.certs.key,
ca: payload.certs.CA,
localAddress: this.libp2pService.createLibp2pAddress(onionAddress, _peerId.toString()),
targetPort: this.ports.libp2pHiddenService,
peers,
Expand Down Expand Up @@ -387,6 +384,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
})

this.registrationService.on(SocketActionTypes.CONNECTION_PROCESS_INFO, data => {
console.log('CONNECTION_PROCESS_INFO', data)
this.serverIoProvider.io.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, data)
})
}
Expand All @@ -395,24 +393,27 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.registrarState = payload
})
this.registrationService.on(SocketActionTypes.SAVED_OWNER_CERTIFICATE, payload => {
console.log('SAVED_OWNER_CERTIFICATE', payload)
this.serverIoProvider.io.emit(SocketActionTypes.SAVED_OWNER_CERTIFICATE, payload)
})
this.registrationService.on(RegistrationEvents.SPAWN_HS_FOR_REGISTRAR, async payload => {
await this.tor.spawnHiddenService({
targetPort: payload.port,
privKey: payload.privateKey,
virtPort: payload.targetPort,
})
})
// this.registrationService.on(RegistrationEvents.SPAWN_HS_FOR_REGISTRAR, async payload => {
// await this.tor.spawnHiddenService({
// targetPort: payload.port,
// privKey: payload.privateKey,
// virtPort: payload.targetPort,
// })
// })
this.registrationService.on(RegistrationEvents.ERROR, payload => {
emitError(this.serverIoProvider.io, payload)
})
this.registrationService.on(SocketActionTypes.SEND_USER_CERTIFICATE, payload => {
this.serverIoProvider.io.emit(SocketActionTypes.SEND_USER_CERTIFICATE, payload)
})
this.registrationService.on(RegistrationEvents.NEW_USER, async payload => {
await this.storageService?.saveCertificate(payload)
})
// this.registrationService.on(SocketActionTypes.SEND_USER_CERTIFICATE, payload => {
// console.log('SEND_USER_CERTIFICATE', payload)
// this.serverIoProvider.io.emit(SocketActionTypes.SEND_USER_CERTIFICATE, payload)
// })
// this.registrationService.on(RegistrationEvents.NEW_USER, async payload => {
// console.log('NEW_USER', payload)
// await this.storageService?.saveCertificate(payload)
// })
}
private attachsocketServiceListeners() {
// Community
Expand All @@ -434,9 +435,11 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
}
})
this.socketService.on(SocketActionTypes.CREATE_NETWORK, async (args: Community) => {
console.log('SocketActionTypes.CREATE_NETWORK')
await this.createNetwork(args)
})
this.socketService.on(SocketActionTypes.CREATE_COMMUNITY, async (args: InitCommunityPayload) => {
console.log('SocketActionTypes.CREATE_COMMUNITY')
await this.createCommunity(args)
})
this.socketService.on(SocketActionTypes.LAUNCH_COMMUNITY, async (args: InitCommunityPayload) => {
Expand All @@ -446,37 +449,56 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
await this.launchCommunity(args)
})
// Registration
this.socketService.on(SocketActionTypes.LAUNCH_REGISTRAR, async (args: LaunchRegistrarPayload) => {
this.logger(`socketService - ${SocketActionTypes.LAUNCH_REGISTRAR}`)

const communityData = await this.localDbService.get(LocalDBKeys.REGISTRAR)
if (!communityData) {
await this.localDbService.put(LocalDBKeys.REGISTRAR, args)
}
console.log('this.registrarState', this.registrarState)
if ([ServiceState.LAUNCHING, ServiceState.LAUNCHED].includes(this.registrarState)) return
this.registrarState = ServiceState.LAUNCHING
await this.registrationService.launchRegistrar(args)
})
// this.socketService.on(SocketActionTypes.LAUNCH_REGISTRAR, async (args: LaunchRegistrarPayload) => {
// this.logger(`socketService - ${SocketActionTypes.LAUNCH_REGISTRAR}`)

// const communityData = await this.localDbService.get(LocalDBKeys.REGISTRAR)
// if (!communityData) {
// await this.localDbService.put(LocalDBKeys.REGISTRAR, args)
// }
// console.log('this.registrarState', this.registrarState)
// if ([ServiceState.LAUNCHING, ServiceState.LAUNCHED].includes(this.registrarState)) return
// this.registrarState = ServiceState.LAUNCHING
// await this.registrationService.launchRegistrar(args)
// })
this.socketService.on(SocketActionTypes.SAVED_OWNER_CERTIFICATE, async (args: SaveOwnerCertificatePayload) => {
console.log('SAVED_OWNER_CERTIFICATE')
const saveCertificatePayload: SaveCertificatePayload = {
certificate: args.certificate,
rootPermsData: args.permsData,
}
await this.storageService?.saveCertificate(saveCertificatePayload)
})
this.socketService.on(SocketActionTypes.REGISTER_USER_CERTIFICATE, async (args: RegisterUserCertificatePayload) => {
console.log('!!!! REGISTER_USER_CERTIFICATE')
// if (!this.socksProxyAgent) {
// this.createAgent()
// }

await this.registrationService.sendCertificateRegistrationRequest(
args.serviceAddress,
args.userCsr,
args.communityId,
120_000,
this.socksProxyAgent
)
// await this.registrationService.sendCertificateRegistrationRequest(
// args.serviceAddress,
// args.userCsr,
// args.communityId,
// 120_000,
// this.socksProxyAgent
// )
// const response = await this.registrationService.registerUser(args.userCsr)
this.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CONNECTING_TO_COMMUNITY)
console.log('emitting SocketActionTypes.SEND_USER_CERTIFICATE')
this.serverIoProvider.io.emit(SocketActionTypes.SEND_USER_CERTIFICATE, {
// TEMPORARY
communityId: args.communityId,
payload: {
peers: [],
},
})
// this.emit(SocketActionTypes.SEND_USER_CERTIFICATE, {
// // TEMPORARY
// communityId: args.communityId,
// payload: {
// peers: [],
// },
// })
})
this.socketService.on(
SocketActionTypes.REGISTER_OWNER_CERTIFICATE,
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/nest/libp2p/libp2p.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { multiaddr } from '@multiformats/multiaddr'
import { ConnectionProcessInfo, PeerId, SocketActionTypes } from '@quiet/types'
import { SERVER_IO_PROVIDER, SOCKS_PROXY_AGENT } from '../const'
import { ServerIoProviderTypes } from '../types'
import { createLibp2pListenAddress, createLibp2pAddress } from './libp2p.utils'
import Logger from '../common/logger'
import { webSockets } from '../websocketOverTor'
import { all } from '../websocketOverTor/filters'
import { createLibp2pAddress, createLibp2pListenAddress } from '@quiet/common'

@Injectable()
export class Libp2pService extends EventEmitter {
Expand All @@ -45,6 +45,7 @@ export class Libp2pService extends EventEmitter {
}

public async createInstance(params: Libp2pNodeParams): Promise<any> {
console.log('Libp2p.createInstance::: peers:::', params.peers)
if (this.libp2pInstance) {
return this.libp2pInstance
}
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/nest/libp2p/libp2p.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export interface Libp2pNodeParams {
peerId: any
listenAddresses: string[]
agent: Agent
cert: string
key: string
ca: string[]
cert?: string
key?: string
ca?: string[]
EmiM marked this conversation as resolved.
Show resolved Hide resolved
localAddress: string
targetPort: number
peers: string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class RegistrationService extends EventEmitter implements OnModuleInit {
private _server: Server
private _port: number
public registrationService: any
public certificates: string[]
public certificates: string[] = []
private _permsData: PermsData
private _ownerCertificate: string

Expand All @@ -40,6 +40,7 @@ export class RegistrationService extends EventEmitter implements OnModuleInit {

onModuleInit() {
this.on(RegistrationEvents.SET_CERTIFICATES, certs => {
console.log('SET CERTIFICATES', certs)
this.setCertificates(certs)
})
this.setRouting()
Expand Down Expand Up @@ -123,7 +124,7 @@ export class RegistrationService extends EventEmitter implements OnModuleInit {
this.emit(response.eventType, response.data)
}

private async registerUser(csr: string): Promise<{ status: number; body: any }> {
public async registerUser(csr: string): Promise<{ status: number; body: any }> {
const result = await registerUser(csr, this._permsData, this.certificates, this._ownerCertificate)
if (result?.status === 200) {
this.emit(RegistrationEvents.NEW_USER, { certificate: result.body.certificate, rootPermsData: this._permsData })
Expand Down
10 changes: 5 additions & 5 deletions packages/backend/src/nest/socket/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class SocketService extends EventEmitter implements OnModuleInit {
})

socket.on(SocketActionTypes.REGISTER_USER_CERTIFICATE, async (payload: RegisterUserCertificatePayload) => {
this.logger(`Registering user certificate (${payload.communityId}) on ${payload.serviceAddress}`)
this.logger(`Registering user CSR (${payload.communityId}) on ${payload.serviceAddress}`)
this.emit(SocketActionTypes.REGISTER_USER_CERTIFICATE, payload)
await new Promise<void>(resolve => setTimeout(() => resolve(), 2000))
this.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.REGISTERING_USER_CERTIFICATE)
Expand All @@ -124,10 +124,10 @@ export class SocketService extends EventEmitter implements OnModuleInit {
this.emit(SocketActionTypes.LAUNCH_COMMUNITY, payload)
this.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.LAUNCHING_COMMUNITY)
})
socket.on(SocketActionTypes.LAUNCH_REGISTRAR, async (payload: LaunchRegistrarPayload) => {
this.logger(`Launching registrar for community ${payload.id}, user ${payload.peerId}`)
this.emit(SocketActionTypes.LAUNCH_REGISTRAR, payload)
})
// socket.on(SocketActionTypes.LAUNCH_REGISTRAR, async (payload: LaunchRegistrarPayload) => {
// this.logger(`Launching registrar for community ${payload.id}, user ${payload.peerId}`)
// this.emit(SocketActionTypes.LAUNCH_REGISTRAR, payload)
// })
socket.on(SocketActionTypes.CREATE_NETWORK, async (community: Community) => {
this.logger(`Creating network for community ${community.id}`)
this.emit(SocketActionTypes.CREATE_NETWORK, community)
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class StorageService extends EventEmitter {
return
}
for (const a of addr) {
this.logger(`Pubsub - subscribe to ${addr}`)
this.logger(`Pubsub - subscribe to ${a}`)
// @ts-ignore
await this.orbitDb._pubsub.subscribe(
a,
Expand Down
13 changes: 10 additions & 3 deletions packages/backend/src/nest/tor/tor-control.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class TorControl implements OnModuleInit {
})
}

private async disconnect() {
private disconnect() {
try {
this.connection?.end()
} catch (e) {
Expand All @@ -63,12 +63,19 @@ export class TorControl implements OnModuleInit {

// eslint-disable-next-line @typescript-eslint/ban-types
private async _sendCommand(command: string, resolve: Function, reject: Function) {
await this.connect()
try {
await this.connect()
} catch (e) {
console.error(`ERROR for ${command}`, e)
this.disconnect()
await this.connect()
}

const connectionTimeout = setTimeout(() => {
reject('TOR: Send command timeout')
}, 5000)
this.connection?.on('data', async data => {
await this.disconnect()
this.disconnect()
const dataArray = data.toString().split(/\r?\n/)
if (dataArray[0].startsWith('250')) {
resolve({ code: 250, messages: dataArray })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { multiaddr } from '@multiformats/multiaddr'
import getPort from 'get-port'
import { type DirResult } from 'tmp'
import { jest, describe, it, expect, afterEach, beforeAll, afterAll } from '@jest/globals'
import {
createLibp2pAddress,
torBinForPlatform,
torDirForPlatform,
createTmpDir,
tmpQuietDirPath,
} from '../common/utils'
import { torBinForPlatform, torDirForPlatform, createTmpDir, tmpQuietDirPath } from '../common/utils'
import { type CreateListenerOptions } from '@libp2p/interface-transport'
import { createServer } from 'it-ws/server'
import { createCertificatesTestHelper } from '../common/client-server'
Expand All @@ -22,6 +16,7 @@ import { Tor } from '../tor/tor.service'
import crypto from 'crypto'
import { TorControl } from '../tor/tor-control.service'
import { TorControlAuthType } from '../tor/tor.types'
import { createLibp2pAddress } from '@quiet/common'
jest.setTimeout(120000)

describe('websocketOverTor', () => {
Expand Down
Loading
Loading