Skip to content

Commit

Permalink
Fix compose
Browse files Browse the repository at this point in the history
  • Loading branch information
annndruha committed Feb 24, 2024
1 parent 5b8530a commit 79845ef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Keenetic Wi-Fi Telegram Logger

Simple python script which sends a Telegram message when a device is connected to Wi-Fi or disconnected from Wi-Fi
Simple python script which sends you a Telegram message when a device connected to Wi-Fi or disconnected.
Work only with Keenetic routers. Script use REST (over CLI) Keenetic-API. For test propose check your router web-interface at `https://yourdomain.keenetic.pro/a`

## How to use (linux)

Expand All @@ -9,7 +10,7 @@ Clone repo:
git clone https://github.com/annndruha/keenetic_wifi_telegram_logger
```

Create in repo folder .env-file:
Create in .env-file repository folder :
```shell
touch .env
```
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ services:
wifi_logger:
pull_policy: always
image: ghcr.io/annndruha/keenetic_wifi_telegram_logger:latest
container_name: wifi_logger
restart: always
env_file: .env
volumes:
- .env:/app/.env
37 changes: 19 additions & 18 deletions wifi_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
import requests
from dotenv import dotenv_values

config = dotenv_values(".env")
WIFI_NAME = config["WIFI_NAME"]
WIFI_HOST = config["WIFI_HOST"]
WIFI_LOGIN = config["WIFI_LOGIN"]
WIFI_PASSWORD = config["WIFI_PASSWORD"]
TG_BOT_TOKEN = config["TG_BOT_TOKEN"]
TG_CHAT_ID = config["TG_CHAT_ID"]
config = dotenv_values('.env')
WIFI_NAME = config['WIFI_NAME']
WIFI_HOST = config['WIFI_HOST']
WIFI_LOGIN = config['WIFI_LOGIN']
WIFI_PASSWORD = config['WIFI_PASSWORD']
TG_BOT_TOKEN = config['TG_BOT_TOKEN']
TG_CHAT_ID = config['TG_CHAT_ID']

session = requests.session()
ACTIVE_CLIENTS = {}


def keen_auth(username, password):
r = session.get(f"{WIFI_HOST}/auth")
r = session.get(f'{WIFI_HOST}/auth')
if r.status_code == 401:
md5 = username + ":" + r.headers["X-NDM-Realm"] + ":" + password
md5 = username + ':' + r.headers['X-NDM-Realm'] + ':' + password
md5 = hashlib.md5(md5.encode('utf-8'))
sha = r.headers["X-NDM-Challenge"] + md5.hexdigest()
sha = r.headers['X-NDM-Challenge'] + md5.hexdigest()
sha = hashlib.sha256(sha.encode('utf-8'))
r_new = session.post(f"{WIFI_HOST}/auth", json={"login": username, "password": sha.hexdigest()})
r_new = session.post(f'{WIFI_HOST}/auth', json={'login': username, 'password': sha.hexdigest()})
if r_new.status_code == 200:
return True
elif r.status_code == 200:
Expand All @@ -38,9 +38,9 @@ def keen_auth(username, password):


def update_clients():
r = session.get(f"{WIFI_HOST}/rci/show/ip/hotspot")
r = session.get(f'{WIFI_HOST}/rci/show/ip/hotspot')
if r.status_code != 200:
raise ConnectionRefusedError(f"Status code: {r.status_code}")
raise ConnectionRefusedError(f'Status code: {r.status_code}')

json_clients = r.json()['host']
for client in json_clients:
Expand Down Expand Up @@ -68,13 +68,14 @@ def compare_states(old, new):
def send_message(message):
if not len(message):
return
requests.post(f"https://api.telegram.org/bot{TG_BOT_TOKEN}/sendMessage"
f"?chat_id={TG_CHAT_ID}"
f"&parse_mode=HTML"
f"&text={message}")
print(message)
requests.post(f'https://api.telegram.org/bot{TG_BOT_TOKEN}/sendMessage'
f'?chat_id={TG_CHAT_ID}'
f'&parse_mode=HTML'
f'&text={message}')


if __name__ == "__main__":
if __name__ == '__main__':
while True:
try:
while keen_auth(WIFI_LOGIN, WIFI_PASSWORD):
Expand Down

0 comments on commit 79845ef

Please sign in to comment.