diff --git a/Dockerfile b/Dockerfile index 0eb3a9d..e30afaa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM python:3.6.13-alpine3.12 RUN apk add --no-cache gammu-dev RUN apk add --no-cache --virtual .build-deps gcc musl-dev \ - && pip install python-gammu paho-mqtt \ + && pip install python-gammu paho-mqtt certifi\ && apk del .build-deps gcc musl-dev WORKDIR /app diff --git a/README.md b/README.md index 454a95a..2affc4a 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,14 @@ docker run \ --device=/dev/ttyUSB0:/dev/mobile \ -e PIN="1234" \ -e HOST="192.168.1.x" \ - -e PORT=1883 \ + -e PORT=8883 \ -e PREFIX="sms2mqtt" \ -e CLIENTID="sms2mqttclid" \ -e USER="usr" \ -e PASSWORD="pass" \ + -e USETLS=true \ domochip/sms2mqtt -``` +``` For Docker-Compose, use the following yaml: ```yaml @@ -37,15 +38,16 @@ services: container_name: sms2mqtt image: domochip/sms2mqtt devices: - - /dev/serial/by-id/usb-HUAWEI_HUAWEI_Mobile-if00-port0:/dev/mobile + - /dev/serial/by-id/usb-HUAWEI_HUAWEI_Mobile-if00-port0:/dev/mobile environment: - - PIN=1234 - - HOST=10.0.0.2 - - PORT=1883 - - PREFIX=sms2mqtt - - CLIENTID=sms2mqttclid - - USER=mqtt_username - - PASSWORD=mqtt_password + - PIN=1234 + - HOST=10.0.0.2 + - PORT=8883 + - PREFIX=sms2mqtt + - CLIENTID=sms2mqttclid + - USER=mqtt_username + - PASSWORD=mqtt_password + - USETLS=true restart: always ``` @@ -67,6 +69,8 @@ services: * `CLIENTID`: **Optional**, MQTT client id to use * `USER`: **Optional**, MQTT username * `PASSWORD`: **Optional**, MQTT password +* `USETLS`: **Optional**, Enable TLS/SSL connection to MQTT broker (use 'true', '1' or 'yes' to enable) + ## Send diff --git a/sms2mqtt.py b/sms2mqtt.py index 3e44535..3c27bea 100644 --- a/sms2mqtt.py +++ b/sms2mqtt.py @@ -6,7 +6,7 @@ import paho.mqtt.client as mqtt import gammu import json - +import certifi # callback when the broker responds to our connection request. def on_mqtt_connect(client, userdata, flags, rc): @@ -145,6 +145,8 @@ def get_signal_info(): old_signal_info = "" + + # function used to obtain battery charge def get_battery_charge(): global old_battery_charge @@ -190,6 +192,16 @@ def shutdown(signum=None, frame=None): client.publish(f"{mqttprefix}/connected", "0", 0, True) client.disconnect() +def setup_mqtt_ssl(client, use_tls=False): + try: + if use_tls: + client.tls_set(ca_certs=certifi.where()) + logging.info("SSL/TLS configured successfully") + else: + logging.info("Connecting without SSL/TLS") + except Exception as e: + logging.error(f"Error configuring SSL/TLS: {e}") + exit(1) if __name__ == "__main__": logging.basicConfig( format="%(asctime)s: %(message)s", level=logging.INFO, datefmt="%H:%M:%S") @@ -222,6 +234,7 @@ def shutdown(signum=None, frame=None): mqttclientid = os.getenv("CLIENTID","sms2mqtt") mqttuser = os.getenv("USER") mqttpassword = os.getenv("PASSWORD") + use_tls = str(os.getenv("USETLS", "")).lower() in ('true', '1', 'yes') signal.signal(signal.SIGINT, shutdown) signal.signal(signal.SIGTERM, shutdown) @@ -256,6 +269,7 @@ def shutdown(signum=None, frame=None): client = mqtt.Client(mqttclientid) client.username_pw_set(mqttuser, mqttpassword) + setup_mqtt_ssl(client,use_tls) client.on_connect = on_mqtt_connect client.on_disconnect = on_mqtt_disconnect client.on_message = on_mqtt_message