From 747b4fbcf890626d7e7d9f4c744fb0f5802eaace Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 13 Sep 2019 18:50:01 +0200 Subject: [PATCH] import --- bin/safe-harbor.py | 27 +++++++++++++++++++++++++++ config.json.example | 14 ++++++++++++++ systemd/safe-harbor@.service | 12 ++++++++++++ 3 files changed, 53 insertions(+) create mode 100755 bin/safe-harbor.py create mode 100644 config.json.example create mode 100644 systemd/safe-harbor@.service diff --git a/bin/safe-harbor.py b/bin/safe-harbor.py new file mode 100755 index 0000000..8d18ed3 --- /dev/null +++ b/bin/safe-harbor.py @@ -0,0 +1,27 @@ +#! /usr/bin/env python3 +# encoding: utf-8 + +import jsmin +import json +import os +import sys + +if __name__ == "__main__": + if len(sys.argv) == 2: + cfg_path = sys.argv[1] + else: + cfg_path = "/etc/safe-harbor/config.json" + + with open(cfg_path) as f: + cfg = json.loads(jsmin.jsmin(f.read())) + + command = ["/usr/bin/ssh", "ssh", "-o", "ServerAliveInterval 45", "-o", "ServerAliveCountMax 2", "-o", "ExitOnForwardFailure yes", "-N"] + + for m in cfg["mapping"]: + command.append("-R") + command.append("%d:localhost:%d" %(m["remote"], m["local"])) + + command.append("%s@%s" %(cfg["user"], cfg["host"])) + + # replace this process, systemd will restart us if needed + os.execl(*command) diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..e6836ae --- /dev/null +++ b/config.json.example @@ -0,0 +1,14 @@ +{ + "host": "decade.cz", + "user": "tunnel", + "mapping": [ + { + "remote": 50022, + "local": 22 + }, + { + "remote": 8000, + "local": 80 + } + ] +} diff --git a/systemd/safe-harbor@.service b/systemd/safe-harbor@.service new file mode 100644 index 0000000..50e0110 --- /dev/null +++ b/systemd/safe-harbor@.service @@ -0,0 +1,12 @@ +[Unit] +Description=Safe Harbor SSH tunnel +After=network.target + +[Service] +User=$i +ExecStart=/usr/bin/python3 /usr/lib/safe-harbor/safe-harbor.py +Restart=always +RestartSec=90 + +[Install] +WantedBy=multi-user.target