-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel.py
executable file
·146 lines (117 loc) · 4.89 KB
/
tunnel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import argparse
import getpass
import os
import platform
import sys
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
SUDO = "sudo " if os.getuid() != 0 else ""
class Systemd:
TEMPLATE_FILE = os.path.join(BASE_DIR, "systemd", "ssh-tunnel.service")
SERVICE_DIR = "/etc/systemd/system/"
@classmethod
def make_tunnel(cls, name, ssh_args):
with open(cls.TEMPLATE_FILE, "r") as f:
template = f.read()
service_name = f"ssh-tunnel-{name}"
filename = f"{service_name}.service"
tmp_file = os.path.join("/tmp", filename)
with open(tmp_file, "w") as f:
f.write(
template.format(username=getpass.getuser(), ssh_args=" ".join(ssh_args))
)
print(f"Copy {service_name} to `{cls.SERVICE_DIR}`...")
assert os.system(f"{SUDO}cp {tmp_file} {cls.SERVICE_DIR}") == 0
print("Reload systemd daemon...")
assert os.system(f"{SUDO}systemctl daemon-reload") == 0
print(f"Start {service_name}...")
assert os.system(f"{SUDO}systemctl enable {service_name}") == 0
assert os.system(f"{SUDO}systemctl start {service_name}") == 0
@classmethod
def list_tunnels(cls):
os.system("systemctl list-units 'ssh-tunnel-*' --all")
@classmethod
def remove_tunnel(cls, name):
service_name = f"ssh-tunnel-{name}"
filename = f"{service_name}.service"
file = os.path.join(cls.SERVICE_DIR, filename)
if not os.path.exists(file):
print(f"Service `{service_name}` is not existed.")
sys.exit(1)
print(f"Stop {service_name}...")
assert os.system(f"{SUDO}systemctl stop {service_name}") == 0
assert os.system(f"{SUDO}systemctl disable {service_name}") == 0
print(f"Remove {service_name} from `{cls.SERVICE_DIR}`...")
assert os.system(f"{SUDO}rm {file}") == 0
print("Reload systemd daemon...")
assert os.system(f"{SUDO}systemctl daemon-reload") == 0
class Launchd:
TEMPLATE_FILE = os.path.join(BASE_DIR, "launchd", "com.ssh-tunnel.plist")
SERVICE_DIR = "/Library/LaunchDaemons/"
@classmethod
def make_tunnel(cls, name, ssh_args):
with open(cls.TEMPLATE_FILE, "r") as f:
template = f.read()
service_name = f"ssh-tunnel-{name}"
filename = f"com.{service_name}.plist"
tmp_file = os.path.join("/tmp", filename)
with open(tmp_file, "w") as f:
f.write(
template.format(
username=getpass.getuser(),
service_name=service_name,
ssh_args="</string>\n <string>".join(ssh_args),
)
)
file = os.path.join(cls.SERVICE_DIR, filename)
print(f"Copy {service_name} to `{cls.SERVICE_DIR}`...")
assert os.system(f"{SUDO}cp {tmp_file} {cls.SERVICE_DIR}") == 0
print(f"Start {service_name}...")
assert os.system(f"{SUDO}launchctl load -w {file}") == 0
@classmethod
def list_tunnels(cls):
os.system(f"{SUDO}launchctl list | grep com.ssh-tunnel-")
@classmethod
def remove_tunnel(cls, name):
service_name = f"ssh-tunnel-{name}"
filename = f"com.{service_name}.plist"
file = os.path.join(cls.SERVICE_DIR, filename)
if not os.path.exists(file):
print(f"Service `{service_name}` is not existed.")
sys.exit(1)
print(f"Stop {service_name}...")
assert os.system(f"{SUDO}launchctl unload -w {file}") == 0
print(f"Remove {service_name} from `{cls.SERVICE_DIR}`...")
assert os.system(f"{SUDO}rm {file}") == 0
def main():
parser = argparse.ArgumentParser()
parser.set_defaults(func=parser.print_help)
subparsers = parser.add_subparsers(dest="command")
parser_make = subparsers.add_parser("make", help="make tunnel")
parser_make.add_argument("name", help="tunnel name")
parser_make.add_argument("ssh_args", nargs=argparse.REMAINDER, help="ssh arguments")
subparsers.add_parser("list", help="list tunnels")
parser_remove = subparsers.add_parser("remove", help="remove tunnel")
parser_remove.add_argument("name", help="tunnel name")
args = parser.parse_args()
os_type = platform.system()
if os_type == "Darwin":
if args.command == "make":
Launchd.make_tunnel(args.name, args.ssh_args)
elif args.command == "list":
Launchd.list_tunnels()
elif args.command == "remove":
Launchd.remove_tunnel(args.name)
else:
parser.print_help()
else:
if args.command == "make":
Systemd.make_tunnel(args.name, args.ssh_args)
elif args.command == "list":
Systemd.list_tunnels()
elif args.command == "remove":
Systemd.remove_tunnel(args.name)
else:
parser.print_help()
if __name__ == "__main__":
main()