Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added SSL support and certifi to Dockerfile #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

16 changes: 15 additions & 1 deletion sms2mqtt.py
Original file line number Diff line number Diff line change
@@ -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