Skip to content

Commit

Permalink
Use fetch from the MatrixHttpApi
Browse files Browse the repository at this point in the history
  • Loading branch information
hughns committed Oct 6, 2022
1 parent 6ef2880 commit c38549b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
12 changes: 8 additions & 4 deletions src/rendezvous/channels/ecdhV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class ECDHv1RendezvousChannel implements RendezvousChannel {

constructor(
public transport: RendezvousTransport,
private cli?: MatrixClient,
private client: MatrixClient,
private theirPublicKey?: Uint8Array,
) {
this.ourPrivateKey = ed.utils.randomPrivateKey();
Expand Down Expand Up @@ -138,18 +138,22 @@ export class ECDHv1RendezvousChannel implements RendezvousChannel {
return rendezvous;
}

private isLoggedIn(): boolean {
return !!this.client.getAccessToken();
}

async connect(): Promise<string> {
const isInitiator = !this.theirPublicKey;
const ourPublicKey = await this.getPublicKey();

if (this.cli && this.theirPublicKey) {
if (this.isLoggedIn && this.theirPublicKey) {
await this.send({
algorithm: SecureRendezvousChannelAlgorithm.ECDH_V1,
key: encodeBase64(ourPublicKey),
});
}

if (this.cli || !this.theirPublicKey) {
if (this.isLoggedIn || !this.theirPublicKey) {
logger.info('Waiting for other device to send their public key');
const res = await this.receive(); // ack
if (!res) {
Expand Down Expand Up @@ -177,7 +181,7 @@ export class ECDHv1RendezvousChannel implements RendezvousChannel {
}
}

if (!this.cli) {
if (!this.isLoggedIn) {
await this.send({
algorithm: SecureRendezvousChannelAlgorithm.ECDH_V1,
key: encodeBase64(ourPublicKey),
Expand Down
17 changes: 12 additions & 5 deletions src/rendezvous/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ export * from './cancellationReason';
export * from './transport';
export * from './channel';

/**
* Build a rendezvous channel from the a scanned code. e.g. from a QR
*
* @param code the scanned rendezvous code
* @param onCancelled cancellation callback
* @param client the client is needed even if it is only a temp one
* @returns the created channel and the intent of the initiator
*/
export async function buildChannelFromCode(
code: string,
onCancelled: RendezvousCancellationFunction,
cli?: MatrixClient,
client: MatrixClient,
): Promise<{ channel: RendezvousChannel, intent: RendezvousIntent }> {
let parsed: RendezvousCode;
try {
Expand All @@ -59,9 +67,8 @@ export async function buildChannelFromCode(

const transport = new SimpleHttpRendezvousTransport(
onCancelled,
undefined, // client
undefined, // hsUrl
undefined, // fallbackRzServer
client,
undefined, // fallbackRzServer not needed as we are parsing a code
transportDetails.uri);

if (rendezvous?.algorithm !== SecureRendezvousChannelAlgorithm.ECDH_V1) {
Expand All @@ -74,7 +81,7 @@ export async function buildChannelFromCode(

logger.info(`Building ECDHv1 rendezvous via HTTP from: ${code}`);
return {
channel: new ECDHv1RendezvousChannel(transport, cli, theirPublicKey),
channel: new ECDHv1RendezvousChannel(transport, client, theirPublicKey),
intent,
};
}
39 changes: 16 additions & 23 deletions src/rendezvous/transports/simpleHttpTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { sleep } from '../../utils';
import { BaseRendezvousTransport } from "./baseTransport";
import { RendezvousCancellationFunction, RendezvousCancellationReason } from '../cancellationReason';
import { RendezvousTransportDetails } from '../transport';
import { ClientPrefix, MatrixClient } from '../../matrix';
import { ClientPrefix, MatrixClient, Method } from '../../matrix';

export interface SimpleHttpRendezvousTransportDetails extends RendezvousTransportDetails {
type: 'http.v1';
Expand All @@ -32,9 +32,8 @@ export class SimpleHttpRendezvousTransport extends BaseRendezvousTransport {
private expiresAt?: Date;

constructor(
public onCancelled?: RendezvousCancellationFunction,
private client?: MatrixClient,
private hsUrl?: string,
public onCancelled: RendezvousCancellationFunction | undefined,
private client: MatrixClient,
private fallbackRzServer?: string,
rendezvousUri?: string,
) {
Expand All @@ -55,24 +54,15 @@ export class SimpleHttpRendezvousTransport extends BaseRendezvousTransport {
}

private async getPostEndpoint(): Promise<string | undefined> {
if (!this.client && this.hsUrl) {
// TODO: this will use the default fetch function which might not work in Node.js
this.client = new MatrixClient({
baseUrl: this.hsUrl,
});
}

if (this.client) {
try {
// eslint-disable-next-line camelcase
const { unstable_features } = await this.client.getVersions();
// eslint-disable-next-line camelcase
if (unstable_features?.['org.matrix.msc3886']) {
return `${this.client.baseUrl}${ClientPrefix.Unstable}/org.matrix.msc3886/rendezvous`;
}
} catch (err) {
logger.warn('Failed to get unstable features', err);
try {
// eslint-disable-next-line camelcase
const { unstable_features } = await this.client.getVersions();
// eslint-disable-next-line camelcase
if (unstable_features?.['org.matrix.msc3886']) {
return `${this.client.baseUrl}${ClientPrefix.Unstable}/org.matrix.msc3886/rendezvous`;
}
} catch (err) {
logger.warn('Failed to get unstable features', err);
}

return this.fallbackRzServer;
Expand All @@ -96,7 +86,8 @@ export class SimpleHttpRendezvousTransport extends BaseRendezvousTransport {
headers['if-match'] = this.etag;
}

const res = await fetch(uri, { method,
// we use fetch directly so that we get all HTTP response codes back
const res = await this.client.http.fetch(uri, { method,
headers,
body: data,
});
Expand Down Expand Up @@ -136,7 +127,9 @@ export class SimpleHttpRendezvousTransport extends BaseRendezvousTransport {
if (this.etag) {
headers['if-none-match'] = this.etag;
}
const poll = await fetch(this.uri, { method: "GET", headers });

// we use fetch directly so that we get all HTTP response codes back
const poll = await this.client.http.fetch(this.uri, { method: "GET", headers });

logger.debug(`Received polling response: ${poll.status} from ${this.uri}`);
if (poll.status === 404) {
Expand Down

0 comments on commit c38549b

Please sign in to comment.