Skip to content

Commit

Permalink
feat(add-routing-info): initial commit (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpShell committed May 15, 2024
1 parent 2ae664c commit 0eaa5af
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/Socket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export const makeSocket = (config: SocketConfig) => {
url = new URL(`tcp://${MOBILE_ENDPOINT}:${MOBILE_PORT}`)
}

if(!config.mobile && url.protocol === 'wss' && authState?.creds?.routingInfo) {
url.searchParams.append('ED', authState.creds.routingInfo.toString('base64url'))
}

const ws = config.socket ? config.socket : config.mobile ? new MobileSocketClient(url, config) : new WebSocketClient(url, config)

ws.connect()
Expand All @@ -88,7 +92,8 @@ export const makeSocket = (config: SocketConfig) => {
keyPair: ephemeralKeyPair,
NOISE_HEADER: config.mobile ? MOBILE_NOISE_HEADER : NOISE_WA_HEADER,
mobile: config.mobile,
logger
logger,
routingInfo: authState?.creds?.routingInfo
})

const { creds } = authState
Expand Down Expand Up @@ -671,6 +676,14 @@ export const makeSocket = (config: SocketConfig) => {
end(new Boom('Multi-device beta not joined', { statusCode: DisconnectReason.multideviceMismatch }))
})

ws.on('CB:ib,,edge_routing', (node: BinaryNode) => {
const edgeRoutingNode = getBinaryNodeChild(node, 'edge_routing')
const routingInfo = getBinaryNodeChild(edgeRoutingNode, 'routing_info')
if(routingInfo?.content) {
authState.creds.routingInfo = Buffer.from(routingInfo?.content as Uint8Array)
}
})

let didStartBuffer = false
process.nextTick(() => {
if(creds.me?.id) {
Expand Down
1 change: 1 addition & 0 deletions src/Types/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type AuthenticationCreds = SignalCreds & {
registration: RegistrationOptions
pairingCode: string | undefined
lastPropHash: string | undefined
routingInfo: Buffer | undefined
}

export type SignalDataTypeMap = {
Expand Down
1 change: 1 addition & 0 deletions src/Utils/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,6 @@ export const initAuthCreds = (): AuthenticationCreds => {
registration: {} as never,
pairingCode: undefined,
lastPropHash: undefined,
routingInfo: undefined,
}
}
20 changes: 18 additions & 2 deletions src/Utils/noise-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export const makeNoiseHandler = ({
NOISE_HEADER,
mobile,
logger,
routingInfo
}: {
keyPair: KeyPair
NOISE_HEADER: Uint8Array
mobile: boolean
logger: Logger
routingInfo?: Buffer | undefined
}) => {
logger = logger.child({ class: 'ns' })

Expand Down Expand Up @@ -133,11 +135,25 @@ export const makeNoiseHandler = ({
data = encrypt(data)
}

const introSize = sentIntro ? 0 : NOISE_HEADER.length
let header: Buffer

if(routingInfo) {
header = Buffer.alloc(7)
header.write('ED', 0, 'utf8')
header.writeUint8(0, 2)
header.writeUint8(1, 3)
header.writeUint8(routingInfo.byteLength >> 16, 4)
header.writeUint16BE(routingInfo.byteLength & 65535, 5)
header = Buffer.concat([header, routingInfo, NOISE_HEADER])
} else {
header = Buffer.from(NOISE_HEADER)
}

const introSize = sentIntro ? 0 : header.length
const frame = Buffer.alloc(introSize + 3 + data.byteLength)

if(!sentIntro) {
frame.set(NOISE_HEADER)
frame.set(header)
sentIntro = true
}

Expand Down

0 comments on commit 0eaa5af

Please sign in to comment.