From f16baafa8811d76e827ed7beda165d09594ade23 Mon Sep 17 00:00:00 2001 From: Sewertronics Date: Fri, 8 Oct 2021 11:32:37 +0000 Subject: [PATCH 1/4] Support TLS client certificate --- deno/deno_client.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/deno/deno_client.ts b/deno/deno_client.ts index abaa8e0..68212e6 100644 --- a/deno/deno_client.ts +++ b/deno/deno_client.ts @@ -5,7 +5,12 @@ import { import { AnyPacket } from '../packets/mod.ts'; export type ClientOptions = BaseClientOptions & { + /** Path to the ca.crt file */ certFile?: string; + /** Content of the client.crt file */ + certChain?: string; + /** Content of the client.key file */ + privateKey?: string; }; const DEFAULT_BUF_SIZE = 4096; @@ -49,7 +54,9 @@ export class Client extends BaseClient { hostname: url.hostname, port: Number(url.port), certFile: this.options.certFile, - }); + certChain: this.options.certChain, + privateKey: this.options.privateKey + } as any); } else { throw new Error(`unknown URL protocol ${url.protocol.slice(0, -1)}`); } From 9e57203577d08ab41262b474a7ce29e4d7569fc9 Mon Sep 17 00:00:00 2001 From: sewertronics Date: Mon, 8 Nov 2021 14:33:10 +0100 Subject: [PATCH 2/4] TLS adapted to the new Deno version --- deno/deno_client.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/deno/deno_client.ts b/deno/deno_client.ts index 68212e6..bce2575 100644 --- a/deno/deno_client.ts +++ b/deno/deno_client.ts @@ -5,8 +5,8 @@ import { import { AnyPacket } from '../packets/mod.ts'; export type ClientOptions = BaseClientOptions & { - /** Path to the ca.crt file */ - certFile?: string; + /** Path to the ca.crt files */ + caCerts?: string[]; /** Content of the client.crt file */ certChain?: string; /** Content of the client.key file */ @@ -48,12 +48,10 @@ export class Client extends BaseClient { port: Number(url.port), }); } else if (url.protocol === 'mqtts:') { - // console.log(this.options.certFile); - conn = await Deno.connectTls({ hostname: url.hostname, port: Number(url.port), - certFile: this.options.certFile, + caCerts: this.options.caCerts, certChain: this.options.certChain, privateKey: this.options.privateKey } as any); From 974c6e874f8252a135d82893539df9953608e1a9 Mon Sep 17 00:00:00 2001 From: sewertronics Date: Wed, 10 Nov 2021 11:58:59 +0100 Subject: [PATCH 3/4] Fixed passing options by Client constructor --- client/base_client.ts | 6 ++++++ deno/deno_client.ts | 18 ++++-------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/client/base_client.ts b/client/base_client.ts index cac1334..635402b 100644 --- a/client/base_client.ts +++ b/client/base_client.ts @@ -34,6 +34,12 @@ export type ClientOptions = { incomingStore?: IncomingStore; outgoingStore?: OutgoingStore; logger?: (msg: string, ...args: unknown[]) => void; + /** Path to the ca.crt files */ + caCerts?: string[]; + /** Content of the client.crt file */ + certChain?: string; + /** Content of the client.key file */ + privateKey?: string; }; export type RetryOptions = { diff --git a/deno/deno_client.ts b/deno/deno_client.ts index bce2575..21b0ec1 100644 --- a/deno/deno_client.ts +++ b/deno/deno_client.ts @@ -1,25 +1,15 @@ import { Client as BaseClient, - ClientOptions as BaseClientOptions, + ClientOptions, } from '../client/base_client.ts'; import { AnyPacket } from '../packets/mod.ts'; -export type ClientOptions = BaseClientOptions & { - /** Path to the ca.crt files */ - caCerts?: string[]; - /** Content of the client.crt file */ - certChain?: string; - /** Content of the client.key file */ - privateKey?: string; -}; - const DEFAULT_BUF_SIZE = 4096; const utf8Encoder = new TextEncoder(); const utf8Decoder = new TextDecoder(); export class Client extends BaseClient { - declare options: ClientOptions; private conn: Deno.Conn | undefined; private closing = false; @@ -51,9 +41,9 @@ export class Client extends BaseClient { conn = await Deno.connectTls({ hostname: url.hostname, port: Number(url.port), - caCerts: this.options.caCerts, - certChain: this.options.certChain, - privateKey: this.options.privateKey + caCerts: this.options?.caCerts, + certChain: this.options?.certChain, + privateKey: this.options?.privateKey } as any); } else { throw new Error(`unknown URL protocol ${url.protocol.slice(0, -1)}`); From 3879b4de8f950becc099ba5d01a4654e467c7854 Mon Sep 17 00:00:00 2001 From: sewertronics Date: Mon, 20 Dec 2021 14:40:49 +0100 Subject: [PATCH 4/4] Fixed more than once connecting issue --- client/base_client.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/client/base_client.ts b/client/base_client.ts index 635402b..18da216 100644 --- a/client/base_client.ts +++ b/client/base_client.ts @@ -259,7 +259,10 @@ export abstract class Client { } public async connect(): Promise { + await this.disconnect(); switch (this.connectionState) { + case 'connecting': + case 'disconnecting': case 'offline': case 'disconnected': break; @@ -370,6 +373,7 @@ export abstract class Client { input: SubscriptionOption | string | (SubscriptionOption | string)[], qos?: QoS ): Promise { + await this.connect(); switch (this.connectionState) { case 'disconnecting': case 'disconnected': @@ -572,6 +576,8 @@ export abstract class Client { this.changeState('disconnected'); this.stopTimers(); break; + case 'disconnected': + break; default: throw new Error( `should not be disconnecting in ${this.connectionState} state`