Skip to content

Feat/daemon unix socket - #72

Open
zoic21 wants to merge 3 commits into
masterfrom
feat/daemon-unix-socket
Open

zoic21 wants to merge 3 commits into
masterfrom
feat/daemon-unix-socket

Conversation

@zoic21

@zoic21 zoic21 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Description

Le démon d'exemple du template suit le nouveau fonctionnement des démons, compatible Jeedom 4.6 et Jeedom 5 :

Jeedom → démon : socket Unix au lieu d'un port TCP avec clé API

  • Le démon écoute sur un socket Unix (--socket) créé en 700 : seul le compte de Jeedom peut l'ouvrir.
  • Il n'y a plus de clé API à transmettre ni à vérifier dans chaque message, plus de port réseau ouvert, et plus de clé visible dans ps ou dans le journal du démon (l'ancien demond.py l'y écrivait).
  • demond.py refuse de démarrer sans --socket, pour ne jamais retomber sur un port TCP non protégé.

Démon → Jeedom

  • Jeedom 5 (numero de version temporaire l'important c'est si la fonction est la) : API REST (/api/v1/plugins/<plugin>/daemon/messages) avec un jeton propre au démon, qu'il renouvelle lui-même (--rest-credentials). Si Jeedom supprime l'accès, le démon s'arrête et deamon_start lui en ouvre un nouveau au redémarrage.
  • Jeedom 4.6 : inchangé, core/php/jeetemplate.php avec la clé API du plugin. Ce fichier refuse tout quand le démon passe par l'API REST.
  • Dans les deux cas, le plugin reçoit les messages dans template::handleDaemonMessages(array $messages).

Pour les développeurs : les helpers gardent leur API

  • jeedom.py : jeedom_socket(path=...) pour le socket Unix ; jeedom_com(..., credentials=...) pour Jeedom 5. Le mode port TCP reste disponible pour les plugins existants.
  • jeedom.js : Jeedom.http.config(socket) et Jeedom.com.config(apikey, callback, cycle, credentials).
  • La classe template contient les méthodes classiques deamon_info, deamon_start et deamon_stop. S'y ajoutent daemonJeedomArguments() (choix entre REST et clé API), sendToDaemon() (envoi sur le socket avec socket_create(AF_UNIX…)) et handleDaemonMessages(). Il n'y a ni trait ni namespace.

Migration d'un plugin existant :

Où Avant Après
deamon_start --socketport … --callback … --apikey … --socket <chemin> + self::daemonJeedomArguments()
PHP → démon socket_create(AF_INET…) + clé API dans le message socket_create(AF_UNIX…) sans clé
demond.py jeedom_socket(port=…) + contrôle de la clé jeedom_socket(path=…), sans contrôle
demond.py jeedom_com(apikey, url, cycle) ajouter credentials=…
Réception traitement dans jeeX.php handleDaemonMessages(), appelée par jeeX.php (Jeedom 4) et par le Core (Jeedom 5)

Important : ne retirer le contrôle de la clé API dans le démon qu'en même temps que le passage au socket Unix.

Corrections au passage

  • jeedom_utils.stripped() plantait sous Python 3 sur les octets lus sur le socket, donc read_socket() ne pouvait pas fonctionner. Elle garde maintenant les caractères accentués.
  • jeedom.js : add_changes sans :: envoyait la clé littérale _key. Une clé __proto__ venue de l'extérieur pouvait aussi modifier les objets du démon.

Jeedom now reaches the daemon through a Unix socket that only the Jeedom
account can open, so the daemon no longer needs the plugin API key, a TCP
port or a key check on each message. The daemon sends its values with the
plugin API key on Jeedom 4.6, and through the Jeedom 5 REST API with its own
token, renewed by the daemon itself.

The existing helpers keep their API:
- jeedom.py: jeedom_socket(path=...) for the Unix socket and
  jeedom_com(..., credentials=...) for Jeedom 5;
- jeedom.js: Jeedom.http.config(socket) and
  Jeedom.com.config(apikey, callback, cycle, credentials).

The template class gains the usual deamon_info/start/stop methods,
sendToDaemon() over the socket and handleDaemonMessages(), called by
Jeedom 5 and by the Jeedom 4 entry point core/php/jeetemplate.php.

Also fixes jeedom_utils.stripped(), which failed on the bytes read from the
socket under Python 3, and keys such as __proto__ in jeedom.js add_changes.

Tested with demond.py and a jeedom.js daemon against a Jeedom 4.6.3 callback
and a Jeedom 5 REST endpoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GLu3j6PkiC7FxSiBPTZDfj
read_socket() no longer checks the API key, because only the Jeedom
account can open the Unix socket. Without --socket, jeedom_socket() would
fall back to a local TCP port with no check at all, so demond.py now exits
instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GLu3j6PkiC7FxSiBPTZDfj
deamon_info() reported the daemon running as soon as its pid was alive, so
a hung daemon, or a pid reused after a crash, was never restarted. It now
also asks the daemon over its Unix socket: jeedom_socket answers a "health"
line with "ok" without queuing it, and jeedom.js answers GET /health with
204. The README documents the rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GLu3j6PkiC7FxSiBPTZDfj

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants