forked from sshuttle/sshuttle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support sdnotify for better systemd integration
These changes introduce support for sdnotify allowing sshuttle to notify systemd when it finishes connecting to the server and installing firewall rules, and is ready to tunnel requests.
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import socket | ||
import os | ||
from sshuttle.helpers import debug1, debug2, debug3 | ||
|
||
def _notify(message): | ||
addr = os.environ.get("NOTIFY_SOCKET", None) | ||
|
||
if not addr or len(addr) == 1 or addr[0] not in ('/', '@'): | ||
return False | ||
|
||
addr = '\0' + addr[1:] if addr[0] == '@' else addr | ||
|
||
try: | ||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | ||
except socket.error as e: | ||
debug1("Error creating socket to notify to systemd: %s\n" % e) | ||
|
||
if not (sock and message): | ||
return False | ||
|
||
assert isinstance(message, bytes) | ||
|
||
try: | ||
return (sock.sendto(message, addr) > 0) | ||
except socket.error as e: | ||
debug1("Error notifying systemd: %s\n" % e) | ||
return False | ||
|
||
def send(*messages): | ||
return _notify(b'\n'.join(messages)) | ||
|
||
def ready(): | ||
return b"READY=1" | ||
|
||
def stop(): | ||
return b"STOPPING=1" | ||
|
||
def status(message): | ||
return b"STATUS=%s" % message.encode('utf8') |