Skip to content

Commit

Permalink
Merge pull request #5 from Tester2009/master
Browse files Browse the repository at this point in the history
Add new feature: client_id and tls_insecure_set
  • Loading branch information
rpdswtk committed Aug 5, 2023
2 parents 9e4e8cf + 2b4c0f6 commit 3695fe5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ Simple cli tool for recording and replaying MQTT messages.
| -h, --help | Show help | | |
| --host | MQTT broker address | x | |
| --port | MQTT broker port | | 1883 |
| --client_id | MQTT Client ID | | |
| --mode | mode: record/replay | x | |
| --file | output/input csv file | x | |
| --loop | looping replay | | false |
| --qos | Quality of Service that will be used for subscriptions | | 0 |
| --topics | json file containing selected topics for subscriptions | | null |
| --enable_ssl | True to enable MQTTs support, False otherwise | | False |
| --tls_insecure| If certs is self-generated, change to True | | False |
| --ca_cert | Path to the Certificate Authority certificate files | | None |
| --certfile | Path to the client certificate | | None |
| --keyfile | Path to the client private key | | None |
Expand Down
20 changes: 19 additions & 1 deletion mqtt_recorder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
help='MQTT broker port'
)

parser.add_argument(
'--client_id',
type=str,
default=None,
required=False,
help='MQTT Client ID'
)


parser.add_argument(
'--username',
type=str,
Expand All @@ -45,6 +54,14 @@
help='True to enable MQTTs support, False otherwise'
)

parser.add_argument(
'--tls_insecure',
type=bool,
default=False,
required=False,
help='If certs is self-generated, change to True'
)

parser.add_argument(
'--ca_cert',
type=str,
Expand Down Expand Up @@ -123,10 +140,11 @@ def wait_for_keyboard_interrupt():

def main():
args = parser.parse_args()
sslContext = SslContext(args.enable_ssl, args.ca_cert, args.certfile, args.keyfile)
sslContext = SslContext(args.enable_ssl, args.ca_cert, args.certfile, args.keyfile, args.tls_insecure)
recorder = MqttRecorder(
args.host,
args.port,
args.client_id,
args.file,
args.username,
args.password,
Expand Down
10 changes: 6 additions & 4 deletions mqtt_recorder/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,32 @@

class SslContext():

def __init__(self, enable, ca_cert, certfile, keyfile):
def __init__(self, enable, ca_cert, certfile, keyfile, tls_insecure):
self.enable = enable
self.ca_cert = ca_cert
self.certfile = certfile
self.keyfile = keyfile
self.tls_insecure = tls_insecure


class MqttRecorder:

def __init__(self, host: str, port: int, file_name: str, username: str,
def __init__(self, host: str, port: int, client_id: str, file_name: str, username: str,
password: str, sslContext: SslContext, encode_b64: bool):
self.__recording = False
self.__messages = list()
self.__file_name = file_name
self.__last_message_time = None
self.__encode_b64 = encode_b64
self.__client = mqtt.Client()
self.__client = mqtt.Client(client_id=client_id)
self.__client.on_connect = self.__on_connect
self.__client.on_message = self.__on_message
if username is not None:
self.__client.username_pw_set(username, password)
if sslContext.enable:
self.__client.tls_set(sslContext.ca_cert, sslContext.certfile, sslContext.keyfile)

if sslContext.tls_insecure is True:
self.__client.tls_insecure_set(True)
self.__client.connect(host=host, port=port)
self.__client.loop_start()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="mqtt-recorder",
version="1.2.0",
version="1.3.0",
author="RPDSWTK",
description="MQTT recorder tool",
long_description=long_description,
Expand Down

0 comments on commit 3695fe5

Please sign in to comment.