1.1.2 • Published 9 months ago

qxbot v1.1.2

Weekly downloads
2
License
ISC
Repository
-
Last release
9 months ago

QX

End-to-end encrypted communication system for MQTT

QX is a secure, end-to-end encrypted communication system leveraging MQTT for real-time data exchange. Designed with privacy in mind, QX ensures that your communications remain confidential and protected from unauthorized access - even on public brokers.

Features

  • End-to-End Encryption: All messages are encrypted using robust cryptographic protocols to ensure privacy.
  • MQTT Integration: Utilizes public MQTT brokers for efficient message dissemination.
  • Real-Time Communication: Instant message delivery with support for online/offline status.
  • Discovery Mechanism: Easily discover and connect with peers.
  • Heartbeat Mechanism: Maintains active connections and detects offline peers.
  • Replay Protection: Prevents replay attacks to enhance security.

Prerequisites

  • Node.js: Ensure you have Node.js installed (v18+ recommended).
  • MQTT Broker: You need access to an MQTT broker. You can use a public broker or set up a local one (see section below).

Installation

npm install qx

Usage

const { Bot } = require("qx");

const bot = new Bot();

bot.on("connect", () => {
  console.log("Bot connected to MQTT broker");
});

bot.on("message", (data) => {
  console.log(`Received message from ${data.from}:`, data.json);
});

bot.start();

// Sending a message
bot
  .send("recipientId", { text: "Hello!" })
  .then(() => console.log("Message sent"))
  .catch((err) => console.error("Send failed:", err));

Configuration

QX can be configured using environment variables or by passing options during initialization. Below are the primary configuration options:

Environment Variables

  • MQTT_HOST: The MQTT broker URL (default: mqtt://localhost:1883)
  • VERBOSE: Enable verbose logging (true or false) (default: false)

Events

QX provides several events to handle different stages of communication:

EventDescriptionArguments
connectEmitted when the bot successfully connects to the MQTT broker.None
errorEmitted when there is an error during connection, subscription, or messaging.error: Error object or message.
offlineEmitted when a registered bot/client is detected to be offline.data: { hash } - The hash of the offline bot/client.
onlineEmitted when a bot/client becomes online.data: { hash } - The hash of the online bot/client.
messageEmitted when a new verified message is received.data: { from, message, id } - Details about the sender, message content, and message ID.
pingEmitted when a ping message is received.data: { from } - The hash of the sender.
sentEmitted when a receipt for a sent message is received.id: The unique ID of the message that was acknowledged.
replayEmitted when a replay attack is detected.message: Log message indicating the replay detection.
unknownEmitted when a message is received from an unregistered bot/client.data: { from } - The hash of the unknown sender.
discoveryEmitted when a valid discovery message is received.data: { id, hash, data } - Information about the discovered bot/client and additional data.
closeEmitted when the MQTT connection is closed.None

API Reference

Bot Methods

  • start(): Connect the bot to the MQTT broker and start listening for messages.
  • stop(): Disconnect the bot from the MQTT broker and clear all intervals.
  • send(to, message): Send a message to a peer. Returns a promise that resolves when the message is acknowledged.
  • sendSync(to, message): Send a message without waiting for an acknowledgment.
  • introduce(to, options): Introduce the bot to another peer with optional metadata.
  • ping(to): Send a ping message to keep the connection active.

Security

QX uses TweetNaCl to provide both encryption and authentication, ensuring that messages are confidential, integrity-protected, and authenticated. This means:

  • Confidentiality: Only the intended recipient can decrypt and read the message.
  • Integrity: The message cannot be tampered with without detection.
  • Authentication: The recipient can verify that the message genuinely originates from the sender.

Note: this library does not currently support non-repudiation, meaning that there is no mechanism to prevent the sender from denying that they sent a message. This was an intentional design decision.

Replay protection

To prevent replay attacks, QX implements a replay protection mechanism. Each message includes a counter that ensures messages are processed in the correct order and are not duplicated. If a message with a counter value lower than or equal to the last received message from a sender is detected, it is identified as a replay and discarded.

bot.on("replay", (message) => {
  console.warn("Replay detected:", message);
});

Setting Up a Local MQTT Broker

EMQX is a highly scalable, open-source MQTT broker. Follow the steps below to install and manage EMQX locally.

brew install emqx
emqx start
emqx stop
emqx -h

Dashboard: http://localhost:18083/

1.1.2

9 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

6 years ago

1.0.32

6 years ago

1.0.31

6 years ago

1.0.30

6 years ago

1.0.29

6 years ago

1.0.28

6 years ago

1.0.27

6 years ago

1.0.26

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago