@signalk/streams 6.1.0 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mdns-ws.d.ts CHANGED
@@ -16,6 +16,7 @@ interface MdnsWsOptions {
16
16
  type?: string;
17
17
  subscription?: string;
18
18
  selfHandling?: string;
19
+ token?: string;
19
20
  remoteSelf?: string;
20
21
  ignoreServers?: string[];
21
22
  selfsignedcert?: boolean;
@@ -31,6 +32,8 @@ export default class MdnsWs extends Transform {
31
32
  private signalkClient?;
32
33
  private fetchedMetaPaths;
33
34
  constructor(options: MdnsWsOptions);
35
+ private verifyRemoteToken;
36
+ private setProviderStatus;
34
37
  private connectClient;
35
38
  private fetchMetaIfNeeded;
36
39
  _transform(chunk: unknown, encoding: BufferEncoding, done: TransformCallback): void;
@@ -1 +1 @@
1
- {"version":3,"file":"mdns-ws.d.ts","sourceRoot":"","sources":["../src/mdns-ws.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAA;AAGrD,OAAO,EAAE,WAAW,EAAe,MAAM,SAAS,CAAA;AAElD,UAAU,aAAa;IACrB,GAAG,EAAE;QACH,MAAM,EAAE;YACN,mBAAmB,IAAI,MAAM,CAAA;YAC7B,eAAe,IAAI,MAAM,CAAA;SAC1B,CAAA;QACD,iBAAiB,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;QAChD,gBAAgB,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;KAChD,CAAA;IACD,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,YAAY,CAAC,EAAE,MAAM,CAAA;I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G,IAAI,CAAA;KAChD,CAAA;IACD,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;IACxB,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB;AAOD,MAAM,CAAC,OAAO,OAAO,MAAO,SAAQ,SAAS;IAC3C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA6B;IAC3D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,aAAa,CAA+B;IACpD,OAAO,CAAC,aAAa,CAAC,CAAQ;IAC9B,OAAO,CAAC,gBAAgB,CAAoB;gBAEhC,OAAO,EAAE,aAAa;IAuDlC,OAAO,CAAC,iBAAiB;IAyBzB,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,aAAa;IAmIrB,OAAO,CAAC,iBAAiB;IA0BzB,UAAU,CACR,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,iBAAiB,GACtB,IAAI;CAGR"}
package/dist/mdns-ws.js CHANGED
@@ -14,8 +14,13 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
17
20
  Object.defineProperty(exports, "__esModule", { value: true });
18
21
  const stream_1 = require("stream");
22
+ const http_1 = __importDefault(require("http"));
23
+ const https_1 = __importDefault(require("https"));
19
24
  const client_1 = require("@signalk/client");
20
25
  const signalk_schema_1 = require("@signalk/signalk-schema");
21
26
  class MdnsWs extends stream_1.Transform {
@@ -76,6 +81,38 @@ class MdnsWs extends stream_1.Transform {
76
81
  this.options.app.setProviderError(this.options.providerId, 'This connection is deprecated and must be deleted');
77
82
  }
78
83
  }
84
+ verifyRemoteToken() {
85
+ const protocol = this.options.type === 'wss' ? https_1.default : http_1.default;
86
+ return new Promise((resolve, reject) => {
87
+ const reqOptions = {
88
+ hostname: this.options.host,
89
+ port: this.options.port,
90
+ path: '/signalk/v1/api/self',
91
+ method: 'GET',
92
+ headers: {
93
+ Authorization: `JWT ${this.options.token}`
94
+ },
95
+ rejectUnauthorized: !(this.options.selfsignedcert === true)
96
+ };
97
+ const req = protocol.request(reqOptions, (response) => {
98
+ response.resume();
99
+ resolve(response.statusCode === 200);
100
+ });
101
+ req.on('error', (err) => reject(err));
102
+ req.setTimeout(10000, () => {
103
+ req.destroy(new Error('Token verification timed out'));
104
+ });
105
+ req.end();
106
+ });
107
+ }
108
+ setProviderStatus(message, isError) {
109
+ if (!isError) {
110
+ this.options.app.setProviderStatus(this.options.providerId, message);
111
+ }
112
+ else {
113
+ this.options.app.setProviderError(this.options.providerId, message);
114
+ }
115
+ }
79
116
  connectClient(client) {
80
117
  this.fetchedMetaPaths.clear();
81
118
  client
@@ -83,7 +120,25 @@ class MdnsWs extends stream_1.Transform {
83
120
  .then(() => {
84
121
  this.options.app.setProviderStatus(this.options.providerId, `ws connection connected to ${client.options.hostname}:${client.options.port}`);
85
122
  console.log(`ws connection connected to ${client.options.hostname}:${client.options.port}`);
86
- if (this.options.selfHandling === 'useRemoteSelf') {
123
+ if (this.options.token) {
124
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
125
+ const conn = client.connection;
126
+ conn.send(JSON.stringify({ token: this.options.token }));
127
+ conn.setAuthenticated(this.options.token, 'JWT');
128
+ this.debug('Sent authentication token to remote server');
129
+ this.verifyRemoteToken()
130
+ .then((isValid) => {
131
+ if (!isValid) {
132
+ this.setProviderStatus(`Authentication failed for ${client.options.hostname}:${client.options.port} — token may be invalid or revoked`, true);
133
+ conn.disconnect();
134
+ }
135
+ })
136
+ .catch((err) => {
137
+ this.debug('Token verification error: ' + err.message);
138
+ });
139
+ }
140
+ if (this.options.selfHandling !== 'manualSelf' &&
141
+ this.options.selfHandling !== 'noSelf') {
87
142
  client
88
143
  .API()
89
144
  .then((api) => api.get('/self'))
@@ -152,6 +207,15 @@ class MdnsWs extends stream_1.Transform {
152
207
  }
153
208
  }
154
209
  });
210
+ client.on('disconnect', () => {
211
+ const hint = this.options.token
212
+ ? ' — check that the token is valid on the remote server'
213
+ : ' — the remote server may require authentication';
214
+ this.setProviderStatus(`Disconnected from ${client.options.hostname}:${client.options.port}${hint}`, true);
215
+ });
216
+ client.on('error', (err) => {
217
+ this.setProviderStatus(`Connection error: ${err.message}`, true);
218
+ });
155
219
  }
156
220
  fetchMetaIfNeeded(client, context, path) {
157
221
  if ((0, signalk_schema_1.getMetadata)('vessels.self.' + path)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signalk/streams",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "Utilities for handling streams of Signal K data",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",