Skip to content

Commit 4ee591c

Browse files
committed
Improve[run the ovpn management script]
1 parent 126e8b3 commit 4ee591c

1 file changed

Lines changed: 38 additions & 12 deletions

File tree

core/service/user_managment.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,58 @@
88
script_path = "/root/openvpn-install.sh"
99

1010

11-
def create_user_on_server(name) -> bool:
11+
def create_user_on_server(name, expiry_date) -> bool:
1212
try:
13-
os.system(f"chmod +x {script_path}")
14-
bash = pexpect.spawn(f"sudo bash {script_path}", encoding="utf-8", timeout=60)
15-
16-
bash.expect("Option:")
13+
if not os.path.exists(script_path):
14+
logger.error("script not found on ")
15+
return False
16+
17+
env = {"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
18+
bash = pexpect.spawn(
19+
"/usr/bin/bash",
20+
[script_path],
21+
env=env,
22+
encoding="utf-8",
23+
timeout=120,
24+
)
25+
26+
bash.expect(r"Option:", timeout=90)
1727
bash.sendline("1")
1828

19-
bash.expect("Name:")
29+
bash.expect(r"Name:", timeout=90)
2030
bash.sendline(name)
31+
bash.expect(pexpect.EOF, timeout=180)
2132

22-
bash.expect(pexpect.EOF)
33+
bash.close()
2334
return True
2435

25-
except pexpect.exceptions.TIMEOUT:
26-
logger.error("time out when read exceeds")
36+
except pexpect.TIMEOUT:
37+
logger.error("Timeout occurred while executing script!")
38+
return False
39+
except pexpect.EOF:
40+
logger.error("Script closed earlier than expected!")
2741
return False
2842
except Exception as e:
29-
logger.error(f"error when create a user on server: {e}")
43+
logger.error(f"Error occurred: {e}")
3044
return False
3145

3246

3347
async def delete_user_on_server(name) -> bool | str:
3448
try:
35-
os.system(f"chmod +x {script_path}")
36-
bash = pexpect.spawn(f"sudo bash {script_path}", encoding="utf-8", timeout=60)
49+
if not os.path.exists(script_path):
50+
logger.error("script not found on ")
51+
return False
52+
53+
env = {"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
54+
bash = pexpect.spawn(
55+
"/usr/bin/bash",
56+
[script_path],
57+
env=env,
58+
encoding="utf-8",
59+
timeout=120,
60+
)
61+
62+
bash = pexpect.spawn(f"bash {script_path}", encoding="utf-8", timeout=60)
3763

3864
bash.expect("Option:")
3965
bash.sendline("2")

0 commit comments

Comments
 (0)