-
Notifications
You must be signed in to change notification settings - Fork 1
/
openredir-cli
executable file
·43 lines (31 loc) · 1.05 KB
/
openredir-cli
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
#!/usr/bin/env python
import os
import subprocess
import sys
from argparse import ArgumentParser
from tempfile import TemporaryDirectory
def main():
parser = ArgumentParser(description="openredir CLI helper")
parser.add_argument(
'-r', '--rule',
metavar="RULE",
action="append",
help='Specify a rule in ORIGIN:REPLACEMENT format, can be specified multiple times. (Example: /etc/resolv.conf:/tmp/my_resolv.conf)',
)
parser.add_argument(
'command',
metavar="COMMAND",
nargs="+",
help="Command to run")
options = parser.parse_args()
LUA_SRC = "function redirect(f, s)"
for rule in options.rule:
LUA_SRC += 's = s:gsub("{}", "{}")'.format(*rule.split(":"))
LUA_SRC += "return s end\n"
with TemporaryDirectory() as d:
with open(d + "/.openredir.lua", "w") as f:
f.write(LUA_SRC)
os.environ['OPENREDIR_CONFDIR'] = d
return subprocess.Popen(["openredir"] + options.command).wait()
if __name__ == '__main__':
sys.exit(main())