@seriousme/opifex 1.6.3 → 1.7.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/package.json +12 -2
- package/.github/dependabot.yml +0 -7
- package/.github/workflows/closeStale.yml +0 -15
- package/.github/workflows/codeql-analysis.yml +0 -71
- package/.github/workflows/deno-ci.yml +0 -16
- package/.github/workflows/nodejs-ci.yml +0 -23
- package/.github/workflows/publish.yml +0 -16
- package/.github/workflows/update-license-year.sh +0 -14
- package/.github/workflows/update-license-year.yml +0 -13
- package/CHANGELOG.md +0 -80
- package/deno.json +0 -16
- package/deno.lock +0 -34
- package/dist/bin/conformanceServer.js +0 -97
- package/dist/bin/demoServer.js +0 -96
- package/dist/bin/mqtt.js +0 -256
- package/dist/client/ConnectionState.js +0 -16
- package/dist/client/client.js +0 -187
- package/dist/client/context.js +0 -152
- package/dist/client/deps.js +0 -5
- package/dist/client/handlers/handleConnack.js +0 -24
- package/dist/client/handlers/handlePacket.js +0 -57
- package/dist/client/handlers/handlePuback.js +0 -13
- package/dist/client/handlers/handlePubcomp.js +0 -16
- package/dist/client/handlers/handlePublish.js +0 -33
- package/dist/client/handlers/handlePubrec.js +0 -26
- package/dist/client/handlers/handlePubrel.js +0 -31
- package/dist/client/handlers/handleSuback.js +0 -14
- package/dist/client/handlers/handleUnsuback.js +0 -15
- package/dist/client/mod.js +0 -5
- package/dist/client/store/deps.js +0 -1
- package/dist/client/store/memoryStore.js +0 -58
- package/dist/client/store/store.js +0 -4
- package/dist/mqttConn/mqttConn.js +0 -104
- package/dist/mqttPacket/AuthenticationResult.js +0 -16
- package/dist/mqttPacket/BitMask.js +0 -10
- package/dist/mqttPacket/PacketType.js +0 -18
- package/dist/mqttPacket/connack.js +0 -27
- package/dist/mqttPacket/connect.js +0 -115
- package/dist/mqttPacket/decoder.js +0 -89
- package/dist/mqttPacket/disconnect.js +0 -14
- package/dist/mqttPacket/encoder.js +0 -56
- package/dist/mqttPacket/length.js +0 -43
- package/dist/mqttPacket/mod.js +0 -76
- package/dist/mqttPacket/pingreq.js +0 -15
- package/dist/mqttPacket/pingres.js +0 -14
- package/dist/mqttPacket/puback.js +0 -20
- package/dist/mqttPacket/pubcomp.js +0 -20
- package/dist/mqttPacket/publish.js +0 -51
- package/dist/mqttPacket/pubrec.js +0 -20
- package/dist/mqttPacket/pubrel.js +0 -20
- package/dist/mqttPacket/suback.js +0 -29
- package/dist/mqttPacket/subscribe.js +0 -52
- package/dist/mqttPacket/types.js +0 -1
- package/dist/mqttPacket/unsuback.js +0 -20
- package/dist/mqttPacket/unsubscribe.js +0 -36
- package/dist/mqttPacket/validators.js +0 -15
- package/dist/node/tcpClient.js +0 -81
- package/dist/node/tcpServer.js +0 -50
- package/dist/node/wrapNodeSocket.js +0 -56
- package/dist/persistence/deps.js +0 -3
- package/dist/persistence/memory/memoryPersistence.js +0 -118
- package/dist/persistence/persistence.js +0 -1
- package/dist/persistence/store.js +0 -1
- package/dist/server/context.js +0 -100
- package/dist/server/deps.js +0 -5
- package/dist/server/handlers/handleConnect.js +0 -80
- package/dist/server/handlers/handleDisconnect.js +0 -9
- package/dist/server/handlers/handlePacket.js +0 -65
- package/dist/server/handlers/handlePingreq.js +0 -11
- package/dist/server/handlers/handlePuback.js +0 -16
- package/dist/server/handlers/handlePubcomp.js +0 -14
- package/dist/server/handlers/handlePublish.js +0 -54
- package/dist/server/handlers/handlePubrec.js +0 -23
- package/dist/server/handlers/handlePubrel.js +0 -27
- package/dist/server/handlers/handleSubscribe.js +0 -56
- package/dist/server/handlers/handleUnsubscribe.js +0 -18
- package/dist/server/mod.js +0 -7
- package/dist/server/server.js +0 -47
- package/dist/socket/socket.js +0 -36
- package/dist/timer/timer.js +0 -40
- package/dist/trie/trie.js +0 -95
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/utils/args.js +0 -75
- package/dist/utils/assert.js +0 -20
- package/dist/utils/asyncQueue.js +0 -69
- package/dist/utils/deferred.js +0 -14
- package/dist/utils/logger.js +0 -58
- package/dist/utils/mod.js +0 -11
- package/dist/utils/nextTick.js +0 -18
- package/dist/utils/platform.js +0 -7
- package/tsconfig.json +0 -22
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seriousme/opifex",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "MQTT client & server for Deno & NodeJS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -21,6 +21,16 @@
|
|
|
21
21
|
"mqtt": "dist/bin/mqtt.js",
|
|
22
22
|
"demoServer": "dist/bin/demoServer.js"
|
|
23
23
|
},
|
|
24
|
+
"files": [
|
|
25
|
+
"package.json",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE.txt",
|
|
28
|
+
"**/*.ts",
|
|
29
|
+
"dist/**/*.js",
|
|
30
|
+
"dist/**/*.js.map",
|
|
31
|
+
"dist/**/*.d.ts",
|
|
32
|
+
"dist/**/*.d.ts.map"
|
|
33
|
+
],
|
|
24
34
|
"exports": {
|
|
25
35
|
"./server": "./dist/server/mod.js",
|
|
26
36
|
"./client": "./dist/client/mod.js",
|
|
@@ -53,6 +63,6 @@
|
|
|
53
63
|
},
|
|
54
64
|
"homepage": "https://github.com/seriousme/opifex#readme",
|
|
55
65
|
"devDependencies": {
|
|
56
|
-
"@types/node": "^22.
|
|
66
|
+
"@types/node": "^22.13.1"
|
|
57
67
|
}
|
|
58
68
|
}
|
package/.github/dependabot.yml
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
name: "Close stale issues"
|
|
2
|
-
on:
|
|
3
|
-
schedule:
|
|
4
|
-
- cron: "5 0 * * *"
|
|
5
|
-
|
|
6
|
-
jobs:
|
|
7
|
-
stale:
|
|
8
|
-
runs-on: ubuntu-latest
|
|
9
|
-
steps:
|
|
10
|
-
- uses: actions/stale@v9
|
|
11
|
-
with:
|
|
12
|
-
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
|
13
|
-
days-before-stale: 30
|
|
14
|
-
days-before-close: 5
|
|
15
|
-
stale-issue-message: This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days'
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
# For most projects, this workflow file will not need changing; you simply need
|
|
2
|
-
# to commit it to your repository.
|
|
3
|
-
#
|
|
4
|
-
# You may wish to alter this file to override the set of languages analyzed,
|
|
5
|
-
# or to provide custom queries or build logic.
|
|
6
|
-
#
|
|
7
|
-
# ******** NOTE ********
|
|
8
|
-
# We have attempted to detect the languages in your repository. Please check
|
|
9
|
-
# the `language` matrix defined below to confirm you have the correct set of
|
|
10
|
-
# supported CodeQL languages.
|
|
11
|
-
#
|
|
12
|
-
name: "CodeQL"
|
|
13
|
-
|
|
14
|
-
on:
|
|
15
|
-
push:
|
|
16
|
-
branches: [master]
|
|
17
|
-
pull_request:
|
|
18
|
-
# The branches below must be a subset of the branches above
|
|
19
|
-
branches: [master]
|
|
20
|
-
schedule:
|
|
21
|
-
- cron: "27 16 * * 3"
|
|
22
|
-
|
|
23
|
-
jobs:
|
|
24
|
-
analyze:
|
|
25
|
-
name: Analyze
|
|
26
|
-
runs-on: ubuntu-latest
|
|
27
|
-
permissions:
|
|
28
|
-
actions: read
|
|
29
|
-
contents: read
|
|
30
|
-
security-events: write
|
|
31
|
-
|
|
32
|
-
strategy:
|
|
33
|
-
fail-fast: false
|
|
34
|
-
matrix:
|
|
35
|
-
language: ["javascript"]
|
|
36
|
-
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
37
|
-
# Learn more:
|
|
38
|
-
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
39
|
-
|
|
40
|
-
steps:
|
|
41
|
-
- name: Checkout repository
|
|
42
|
-
uses: actions/checkout@v4
|
|
43
|
-
|
|
44
|
-
# Initializes the CodeQL tools for scanning.
|
|
45
|
-
- name: Initialize CodeQL
|
|
46
|
-
uses: github/codeql-action/init@v3
|
|
47
|
-
with:
|
|
48
|
-
languages: ${{ matrix.language }}
|
|
49
|
-
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
50
|
-
# By default, queries listed here will override any specified in a config file.
|
|
51
|
-
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
52
|
-
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
53
|
-
|
|
54
|
-
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
55
|
-
# If this step fails, then you should remove it and run the build manually (see below)
|
|
56
|
-
- name: Autobuild
|
|
57
|
-
uses: github/codeql-action/autobuild@v3
|
|
58
|
-
|
|
59
|
-
# ℹ️ Command-line programs to run using the OS shell.
|
|
60
|
-
# 📚 https://git.io/JvXDl
|
|
61
|
-
|
|
62
|
-
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
63
|
-
# and modify them (or add more) to build your code if your project
|
|
64
|
-
# uses a compiled language
|
|
65
|
-
|
|
66
|
-
#- run: |
|
|
67
|
-
# make bootstrap
|
|
68
|
-
# make release
|
|
69
|
-
|
|
70
|
-
- name: Perform CodeQL Analysis
|
|
71
|
-
uses: github/codeql-action/analyze@v3
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# This workflow will test on Deno lastest stable major and canary
|
|
2
|
-
# see https://github.com/marketplace/actions/setup-deno for details
|
|
3
|
-
name: Deno CI
|
|
4
|
-
on: [push, pull_request]
|
|
5
|
-
jobs:
|
|
6
|
-
build:
|
|
7
|
-
runs-on: ubuntu-latest
|
|
8
|
-
steps:
|
|
9
|
-
- uses: actions/checkout@v4
|
|
10
|
-
- uses: denoland/setup-deno@v2
|
|
11
|
-
with:
|
|
12
|
-
deno-version: vx.x.x # Run with latest stable Deno.
|
|
13
|
-
- run: deno install
|
|
14
|
-
- run: deno fmt --check
|
|
15
|
-
- run: deno lint
|
|
16
|
-
- run: deno test -A
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
|
|
2
|
-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
|
3
|
-
|
|
4
|
-
name: Node.js CI
|
|
5
|
-
|
|
6
|
-
on: [push, pull_request]
|
|
7
|
-
|
|
8
|
-
permissions: {}
|
|
9
|
-
|
|
10
|
-
jobs:
|
|
11
|
-
build:
|
|
12
|
-
runs-on: ubuntu-latest
|
|
13
|
-
|
|
14
|
-
strategy:
|
|
15
|
-
matrix:
|
|
16
|
-
node-version: ["22", "23"]
|
|
17
|
-
steps:
|
|
18
|
-
- uses: actions/checkout@v4
|
|
19
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
20
|
-
uses: actions/setup-node@v4
|
|
21
|
-
with:
|
|
22
|
-
node-version: ${{ matrix.node-version }}
|
|
23
|
-
- run: npm run test
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
name: Publish
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- main
|
|
7
|
-
|
|
8
|
-
jobs:
|
|
9
|
-
publish:
|
|
10
|
-
runs-on: ubuntu-latest
|
|
11
|
-
permissions:
|
|
12
|
-
contents: read
|
|
13
|
-
id-token: write # The OIDC ID token is used for authentication with JSR.
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v4
|
|
16
|
-
- run: npx jsr publish
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
|
|
3
|
-
license='LICENSE.txt'
|
|
4
|
-
prefix='2016-'
|
|
5
|
-
current_year=$(date +%Y)
|
|
6
|
-
sed -i -e "s/$prefix\([0-9\]\+\)/$prefix$current_year/" $license
|
|
7
|
-
|
|
8
|
-
if [ "$(git diff $license)" ]; then
|
|
9
|
-
git add $license
|
|
10
|
-
git commit -m "Update license year to $current_year"
|
|
11
|
-
git push
|
|
12
|
-
else
|
|
13
|
-
echo "No changes detected in $license"
|
|
14
|
-
fi
|
package/CHANGELOG.md
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [Unreleased]
|
|
4
|
-
|
|
5
|
-
### Changed
|
|
6
|
-
|
|
7
|
-
## [v1.6.3] 31-01-2025
|
|
8
|
-
|
|
9
|
-
### Changed
|
|
10
|
-
|
|
11
|
-
- fix: improve memory persistence backend on reconnects
|
|
12
|
-
|
|
13
|
-
## [v1.6.2] 19-01-2025
|
|
14
|
-
|
|
15
|
-
### Changed
|
|
16
|
-
|
|
17
|
-
- fix: added more JSdoc comments
|
|
18
|
-
|
|
19
|
-
## [v1.6.1] 18-01-2025
|
|
20
|
-
|
|
21
|
-
### Changed
|
|
22
|
-
|
|
23
|
-
- fix: fixed Deno lint type errors
|
|
24
|
-
|
|
25
|
-
## [v1.6.0] 18-01-2025
|
|
26
|
-
|
|
27
|
-
### Changed
|
|
28
|
-
|
|
29
|
-
- added: export TcpClient and TcpServer for both NodeJS and Deno
|
|
30
|
-
- added: export mqttPacket and utils
|
|
31
|
-
|
|
32
|
-
## [v1.5.1] 16-01-2025
|
|
33
|
-
|
|
34
|
-
### Changed
|
|
35
|
-
|
|
36
|
-
- fixed issue with QoS=1
|
|
37
|
-
|
|
38
|
-
## [v1.5.0] 02-01-2025
|
|
39
|
-
|
|
40
|
-
### Changed
|
|
41
|
-
|
|
42
|
-
- added client certificate support to mqtts.ts
|
|
43
|
-
|
|
44
|
-
## [v1.4.0] 02-01-2025
|
|
45
|
-
|
|
46
|
-
### Changed
|
|
47
|
-
|
|
48
|
-
- added client certificate support (w0otness)
|
|
49
|
-
|
|
50
|
-
## [v1.3.0] 19-11-2024
|
|
51
|
-
|
|
52
|
-
### Changed
|
|
53
|
-
|
|
54
|
-
- update: added architectural description of Opifex to documentation
|
|
55
|
-
- update: added comments to platform specific code to explain boundary between
|
|
56
|
-
platform agnostic and platform specific.
|
|
57
|
-
- update: set default clientId in client
|
|
58
|
-
- fix: improve handling of errors if client connect fails
|
|
59
|
-
|
|
60
|
-
## [v1.2.1] 01-11-2024
|
|
61
|
-
|
|
62
|
-
### Changed
|
|
63
|
-
|
|
64
|
-
- fix: unlock last BYOB request using respond(0)
|
|
65
|
-
|
|
66
|
-
## [v1.2.0] 01-11-2024
|
|
67
|
-
|
|
68
|
-
### Changed
|
|
69
|
-
|
|
70
|
-
- added support for NodeJS < 22.0.6 by adding transpiled Javascript in `/dist`
|
|
71
|
-
|
|
72
|
-
## [v1.1.0] 01-11-2024
|
|
73
|
-
|
|
74
|
-
### Changed
|
|
75
|
-
|
|
76
|
-
- added support for NodeJS >= 22.0.6 using --experimental-strip-types
|
|
77
|
-
|
|
78
|
-
## [v1.0.0] 09-10-2022
|
|
79
|
-
|
|
80
|
-
Initial version
|
package/deno.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@seriousme/opifex",
|
|
3
|
-
"version": "1.6.3",
|
|
4
|
-
"license": "MIT",
|
|
5
|
-
"exports": {
|
|
6
|
-
"./server": "./server/mod.ts",
|
|
7
|
-
"./client": "./client/mod.ts",
|
|
8
|
-
"./mqttPacket": "./mqttPacket/mod.ts",
|
|
9
|
-
"./utils": "./utils/mod.ts",
|
|
10
|
-
"./tcpClient": "./deno/tcpClient.ts",
|
|
11
|
-
"./tcpServer": "./deno/tcpServer.ts"
|
|
12
|
-
},
|
|
13
|
-
"exclude": [
|
|
14
|
-
"dist"
|
|
15
|
-
]
|
|
16
|
-
}
|
package/deno.lock
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "4",
|
|
3
|
-
"specifiers": {
|
|
4
|
-
"npm:@types/node@*": "22.5.4",
|
|
5
|
-
"npm:@types/node@^22.10.0": "22.10.0"
|
|
6
|
-
},
|
|
7
|
-
"npm": {
|
|
8
|
-
"@types/node@22.10.0": {
|
|
9
|
-
"integrity": "sha512-XC70cRZVElFHfIUB40FgZOBbgJYFKKMa5nb9lxcwYstFG/Mi+/Y0bGS+rs6Dmhmkpq4pnNiLiuZAbc02YCOnmA==",
|
|
10
|
-
"dependencies": [
|
|
11
|
-
"undici-types@6.20.0"
|
|
12
|
-
]
|
|
13
|
-
},
|
|
14
|
-
"@types/node@22.5.4": {
|
|
15
|
-
"integrity": "sha512-FDuKUJQm/ju9fT/SeX/6+gBzoPzlVCzfzmGkwKvRHQVxi4BntVbyIwf6a4Xn62mrvndLiml6z/UBXIdEVjQLXg==",
|
|
16
|
-
"dependencies": [
|
|
17
|
-
"undici-types@6.19.8"
|
|
18
|
-
]
|
|
19
|
-
},
|
|
20
|
-
"undici-types@6.19.8": {
|
|
21
|
-
"integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw=="
|
|
22
|
-
},
|
|
23
|
-
"undici-types@6.20.0": {
|
|
24
|
-
"integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"workspace": {
|
|
28
|
-
"packageJson": {
|
|
29
|
-
"dependencies": [
|
|
30
|
-
"npm:@types/node@^22.10.0"
|
|
31
|
-
]
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env -S node --experimental-strip-types
|
|
2
|
-
// this server is intended to be used with https://github.com/eclipse/iottestware
|
|
3
|
-
/**
|
|
4
|
-
* MQTT Server implementation with authentication and authorization
|
|
5
|
-
* @module mqttServer
|
|
6
|
-
*/
|
|
7
|
-
import { TcpServer } from "../node/tcpServer.js";
|
|
8
|
-
import { AuthenticationResult } from "../server/mod.js";
|
|
9
|
-
import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.js";
|
|
10
|
-
/**
|
|
11
|
-
* UTF-8 decoder for parsing passwords
|
|
12
|
-
*/
|
|
13
|
-
const utf8Decoder = new TextDecoder();
|
|
14
|
-
/**
|
|
15
|
-
* Map storing valid username/password combinations
|
|
16
|
-
*/
|
|
17
|
-
const userTable = new Map();
|
|
18
|
-
userTable.set("IoTester_1", "strong_password");
|
|
19
|
-
userTable.set("IoTester_2", "strong_password");
|
|
20
|
-
/**
|
|
21
|
-
* Regular expression for validating usernames
|
|
22
|
-
* Allows alphanumeric characters up to 23 chars long
|
|
23
|
-
*/
|
|
24
|
-
const strictUsername = new RegExp(/^[a-zA-Z0-9]{0,23}$/);
|
|
25
|
-
/**
|
|
26
|
-
* Flag to enable/disable username checking
|
|
27
|
-
* If false, all users are allowed access
|
|
28
|
-
* If true, only users in userTable are allowed access
|
|
29
|
-
*/
|
|
30
|
-
const checkUsername = true;
|
|
31
|
-
/**
|
|
32
|
-
* Authenticates MQTT clients
|
|
33
|
-
* @param {Context} _ctx - Connection context
|
|
34
|
-
* @param {string} clientId - Client identifier
|
|
35
|
-
* @param {string} username - Username to authenticate
|
|
36
|
-
* @param {Uint8Array} password - Password as byte array
|
|
37
|
-
* @returns {TAuthenticationResult} Authentication result
|
|
38
|
-
*/
|
|
39
|
-
function isAuthenticated(_ctx, clientId, username, password) {
|
|
40
|
-
const pwd = utf8Decoder.decode(password);
|
|
41
|
-
logger.info(`Verifying authentication of client '${clientId}' with username '${username}' and password '${pwd}'`);
|
|
42
|
-
if (!checkUsername) {
|
|
43
|
-
// allow all users access
|
|
44
|
-
return AuthenticationResult.ok;
|
|
45
|
-
}
|
|
46
|
-
// is the username valid according to MQTT specification
|
|
47
|
-
if (!strictUsername.test(username)) {
|
|
48
|
-
return AuthenticationResult.badUsernameOrPassword;
|
|
49
|
-
}
|
|
50
|
-
// Does user and password match an entry in userTable
|
|
51
|
-
if (userTable.has(username)) {
|
|
52
|
-
const pass = userTable.get(username);
|
|
53
|
-
if (pwd === pass) {
|
|
54
|
-
return AuthenticationResult.ok;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return AuthenticationResult.notAuthorized;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Checks if client is authorized to publish to a topic
|
|
61
|
-
* @param {Context} ctx - Connection context
|
|
62
|
-
* @param {Topic} topic - Topic to publish to
|
|
63
|
-
* @returns {boolean} True if authorized
|
|
64
|
-
*/
|
|
65
|
-
function isAuthorizedToPublish(ctx, topic) {
|
|
66
|
-
logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to publish on topic '${topic}'`);
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Checks if client is authorized to subscribe to a topic
|
|
71
|
-
* @param {Context} ctx - Connection context
|
|
72
|
-
* @param {Topic} topic - Topic to subscribe to
|
|
73
|
-
* @returns {boolean} True if authorized
|
|
74
|
-
*/
|
|
75
|
-
function isAuthorizedToSubscribe(ctx, topic) {
|
|
76
|
-
logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to subscribe to topic '${topic}'`);
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Parse command line arguments and start server
|
|
81
|
-
*/
|
|
82
|
-
const { _: [portNum] } = parseArgs(getArgs());
|
|
83
|
-
const port = Number(portNum ?? 1883);
|
|
84
|
-
const hostname = "::";
|
|
85
|
-
logger.level(LogLevel.info);
|
|
86
|
-
/**
|
|
87
|
-
* TCP Server instance with authentication and authorization handlers
|
|
88
|
-
*/
|
|
89
|
-
const tcpServer = new TcpServer({ port, hostname }, {
|
|
90
|
-
handlers: {
|
|
91
|
-
isAuthenticated,
|
|
92
|
-
isAuthorizedToPublish,
|
|
93
|
-
isAuthorizedToSubscribe,
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
tcpServer.start();
|
|
97
|
-
logger.info(`Server started on port ${tcpServer.port}`);
|
package/dist/bin/demoServer.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env -S node --experimental-strip-types
|
|
2
|
-
/**
|
|
3
|
-
* MQTT Server implementation with authentication and authorization
|
|
4
|
-
* @module mqttServer
|
|
5
|
-
*/
|
|
6
|
-
import { TcpServer } from "../node/tcpServer.js";
|
|
7
|
-
import { AuthenticationResult } from "../server/mod.js";
|
|
8
|
-
import { getArgs, logger, LogLevel, parseArgs } from "../utils/mod.js";
|
|
9
|
-
/**
|
|
10
|
-
* UTF-8 decoder for parsing passwords
|
|
11
|
-
*/
|
|
12
|
-
const utf8Decoder = new TextDecoder();
|
|
13
|
-
/**
|
|
14
|
-
* Map storing valid username/password combinations
|
|
15
|
-
*/
|
|
16
|
-
const userTable = new Map();
|
|
17
|
-
userTable.set("IoTester_1", "strong_password");
|
|
18
|
-
userTable.set("IoTester_2", "strong_password");
|
|
19
|
-
/**
|
|
20
|
-
* Regular expression for validating usernames
|
|
21
|
-
* Allows alphanumeric characters up to 23 chars long
|
|
22
|
-
*/
|
|
23
|
-
const strictUsername = new RegExp(/^[a-zA-Z0-9]{0,23}$/);
|
|
24
|
-
/**
|
|
25
|
-
* Flag to enable/disable username checking
|
|
26
|
-
* If false, all users are allowed access
|
|
27
|
-
* If true, only users in userTable are allowed access
|
|
28
|
-
*/
|
|
29
|
-
const checkUsername = false;
|
|
30
|
-
/**
|
|
31
|
-
* Authenticates MQTT clients
|
|
32
|
-
* @param {Context} _ctx - Connection context
|
|
33
|
-
* @param {string} clientId - Client identifier
|
|
34
|
-
* @param {string} username - Username to authenticate
|
|
35
|
-
* @param {Uint8Array} password - Password as byte array
|
|
36
|
-
* @returns {TAuthenticationResult} Authentication result
|
|
37
|
-
*/
|
|
38
|
-
function isAuthenticated(_ctx, clientId, username, password) {
|
|
39
|
-
const pwd = utf8Decoder.decode(password);
|
|
40
|
-
logger.info(`Verifying authentication of client '${clientId}' with username '${username}' and password '${pwd}'`);
|
|
41
|
-
if (!checkUsername) {
|
|
42
|
-
// allow all users access
|
|
43
|
-
return AuthenticationResult.ok;
|
|
44
|
-
}
|
|
45
|
-
// is the username valid according to MQTT specification
|
|
46
|
-
if (!strictUsername.test(username)) {
|
|
47
|
-
return AuthenticationResult.badUsernameOrPassword;
|
|
48
|
-
}
|
|
49
|
-
// Does user and password match an entry in userTable
|
|
50
|
-
if (userTable.has(username)) {
|
|
51
|
-
const pass = userTable.get(username);
|
|
52
|
-
if (pwd === pass) {
|
|
53
|
-
return AuthenticationResult.ok;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return AuthenticationResult.notAuthorized;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Checks if client is authorized to publish to a topic
|
|
60
|
-
* @param {Context} ctx - Connection context
|
|
61
|
-
* @param {Topic} topic - Topic to publish to
|
|
62
|
-
* @returns {boolean} True if authorized
|
|
63
|
-
*/
|
|
64
|
-
function isAuthorizedToPublish(ctx, topic) {
|
|
65
|
-
logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to publish on topic '${topic}'`);
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Checks if client is authorized to subscribe to a topic
|
|
70
|
-
* @param {Context} ctx - Connection context
|
|
71
|
-
* @param {Topic} topic - Topic to subscribe to
|
|
72
|
-
* @returns {boolean} True if authorized
|
|
73
|
-
*/
|
|
74
|
-
function isAuthorizedToSubscribe(ctx, topic) {
|
|
75
|
-
logger.debug(`Checking authorization of client '${ctx.store?.clientId}' to subscribe to topic '${topic}'`);
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Parse command line arguments and start server
|
|
80
|
-
*/
|
|
81
|
-
const { _: [portNum] } = parseArgs(getArgs());
|
|
82
|
-
const port = Number(portNum ?? 1883);
|
|
83
|
-
const hostname = "::";
|
|
84
|
-
logger.level(LogLevel.info);
|
|
85
|
-
/**
|
|
86
|
-
* TCP Server instance with authentication and authorization handlers
|
|
87
|
-
*/
|
|
88
|
-
const tcpServer = new TcpServer({ port, hostname }, {
|
|
89
|
-
handlers: {
|
|
90
|
-
isAuthenticated,
|
|
91
|
-
isAuthorizedToPublish,
|
|
92
|
-
isAuthorizedToSubscribe,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
tcpServer.start();
|
|
96
|
-
logger.info(`Server started on port ${tcpServer.port}`);
|