update tunnelier again

This commit is contained in:
Q
2024-03-02 17:35:57 +02:00
parent fb0543dfe4
commit 2ce8bc5fd8
2 changed files with 30 additions and 23 deletions

View File

@@ -23,6 +23,6 @@ setup(
] ]
}, },
install_requires=[ install_requires=[
"psutil", "psutil", "pyyaml"
], ],
) )

View File

@@ -9,32 +9,33 @@ import sys
from argparse import ArgumentError, ArgumentParser from argparse import ArgumentError, ArgumentParser
import psutil import psutil
import yaml
__version__ = "2024.01.07" __version__ = "2024.03.02"
CONFDIR = os.path.expanduser("~/.config/ssh-tunnelier") CONFDIR = os.path.expanduser("~/.config/ssh-tunnelier")
CONF = os.path.join(CONFDIR, "tunnels.json") CONF_OLD = os.path.join(CONFDIR, "tunnels.json")
CONF = os.path.join(CONFDIR, "tunnels.yaml")
# Just over a year in minutes # Just over a year in minutes
MAGIC_TIME = 525601 MAGIC_TIME = 525601
LOCALHOSTSYMBOL = "💻" LOCALHOSTSYMBOL = "💻"
EXAMPLE_CONFIG = """{ EXAMPLE_CONFIG = {
"test": { "test": {
"host": "host to connect, or defaults to name", "host": "host to connect, or defaults to name",
"options": "-4 and other ssh options", "options": "-4 and other ssh options",
"auto-connect": false, "auto-connect": False,
"tunnels": [ "tunnels": [
{ {
"local_port": 1111, "local_port": 1111,
"remote_port": 8080, "remote_port": 8080,
"remote_address": "localhost", "remote_address": "localhost",
"reverse": false, "reverse": False,
"comment": "`comment`, `reverse` and `remote_address` are not required" "comment": "`comment`, `reverse` and `remote_address` are not required",
} }
] ],
} }
} }
"""
def args(): def args():
@@ -106,11 +107,17 @@ def load_config():
try: try:
os.makedirs(CONFDIR, exist_ok=True) os.makedirs(CONFDIR, exist_ok=True)
if not os.path.exists(CONF): if not os.path.exists(CONF):
if os.path.exists(CONF_OLD):
with open(CONF_OLD, "r") as fp:
config = json.load(fp)
with open(CONF, "w") as fp:
yaml.dump(config, fp, sort_keys=False)
else:
print("Creating example config: " + CONF) print("Creating example config: " + CONF)
with open(CONF, "w") as fp: with open(CONF, "w") as fp:
fp.write(EXAMPLE_CONFIG) yaml.dump(EXAMPLE_CONFIG, fp)
with open(CONF, "r") as fp: with open(CONF, "r") as fp:
config = json.load(fp) config = yaml.safe_load(fp)
for name in config: for name in config:
config[name]["host"] = config[name].get("host", name) config[name]["host"] = config[name].get("host", name)