keepassxc-proxy-client
keepassxc-proxy-client copied to clipboard
Add support for snap
Snap is a popular software packaging and distribution system. When keepassxc was distributed by snap, the proxy can not read the /tmp directory because it is sandboxed (as for flatpak packages). The socket can be read in the $SNAP_COMMON directory mentioned in this documentation : https://snapcraft.io/docs/data-locations
This PR aims at adding support for snap packages keepassxc by checking the existence of this directory before returning the default location for the socket (the same way for flatpak)
Very nice, thank you! As this socket location seems to be a common place for change. Could you maybe convert the code into a for loop that goes over all paths that we can think of, checks if they exist and returns them in that case and if it doesn't find anything throws an exception?
I tried to find something simple and generic, yet I had to preserve the if statements if I want to avoid errors on getting non set environment variables. Looks like this :
def get_socket_path():
socket_name = "org.keepassxc.KeePassXC.BrowserServer"
if platform.system() == "Windows":
socket_search_paths = [socket_name + "_" + getpass.getuser()]
else:
socket_search_paths = [os.path.join("/tmp", socket_name)]
if "TMPDIR" in os.environ:
socket_search_paths.append(os.path.join(os.environ["TMPDIR"], socket_name))
if "XDG_RUNTIME_DIR" in os.environ:
socket_search_paths.append(os.path.join(os.environ["XDG_RUNTIME_DIR"], "app/org.keepassxc.KeePassXC", socket_name))
if "HOME" in os.environ:
socket_search_paths.append(os.path.join(os.environ["HOME"], "snap/keepassxc/common", socket_name))
for socket_path in socket_search_paths:
if os.path.exists(socket_path):
return socket_path
raise Exception("Error: Could not find any keepassxc socket in the following locations : ${socket_search_paths}")
I will try to get something more compact and readable but I cant figure out now
proposal:
for var, path in [
("TMPDIR", ""),
("XDG_RUNTIME_DIR", "app/org.keepassxc.KeePassXC"),
("HOME", "snap/keepassxc/common"),
]:
if var in os.environ:
socket_path = os.path.join(os.environ[var], path, socket_name)
if os.path.exists(socket_path):
return socket_path
raise Exception("Error: Could not find any keepassxc socket")