-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
161 lines (131 loc) · 4.54 KB
/
utils.py
File metadata and controls
161 lines (131 loc) · 4.54 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import subprocess
import os
import yaml
import aiohttp
def load_config():
# Get the directory where this script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(script_dir, "config.yaml")
if not os.path.exists(config_path):
print(f"please provide a config.yaml file at {config_path}")
exit()
with open(config_path, "r") as f:
config = yaml.safe_load(f)
return config
def get_root_path():
return os.path.dirname(os.path.abspath(__file__))
def get_data_path():
path = os.path.join(get_root_path(), "data")
if not os.path.exists(path):
os.mkdir(path)
return path
def result(obj, error=None):
output = {
"data": obj,
"status": "success" if not error else "error"
}
if error:
output["error"] = error
return output
def strip_filename(filename):
return filename.strip().lower().replace(" ", "_")
def remove_duplicates(lst: list):
# removes duplicates from a list
new_lst = []
for item in lst:
if item not in new_lst:
new_lst.append(item)
return new_lst
def console_log(text):
print(f"\033[1;34m[MCP] {text}\033[0m")
def sh_exec(cmd):
"""executes a command and returns the output as a string"""
console_log(f"sh: {cmd}")
try:
proc = subprocess.run(cmd.split(" "), capture_output=True, text=True)
except Exception as e:
return {"error": f"error while executing command '{cmd}': {e}"}
cmd_result = proc.stdout.splitlines()
if len(cmd_result) == 0:
# just return the output as a string if it was only one line
cmd_result = cmd_result[0]
if cmd_result:
return cmd_result
elif len(proc.stderr) > 0:
cmd_result = proc.stderr.splitlines()
else:
return f"unknown error!: {cmd_result}"
def sh_exec_result(cmd) -> dict:
return result({
"cmd": cmd,
"output": sh_exec(cmd)
})
async def http_request(url):
console_log("fetching remote content..")
async with aiohttp.ClientSession(
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.3"}
) as session:
async with session.get(url, timeout=10) as response:
if response.status != 200:
raise Exception(f"Request failed with status {response.status}")
return await response.read()
def sh_exec_sandbox(command, timeout=30, workdir=None) -> dict:
"""
Safely execute shell command with multiple layers of protection
"""
if workdir is None:
workdir = tempfile.mkdtemp()
# Command validation
dangerous_patterns = ['rm -rf', 'dd if=', 'mkfs', ':(){:|:&};:', 'wget', 'curl']
for pattern in dangerous_patterns:
if pattern in command.lower():
raise SecurityError(f"Dangerous command pattern detected: {pattern}")
# Restricted environment
safe_env = {
'PATH': '/usr/bin:/bin',
'HOME': workdir,
'USER': 'nobody',
'LOGNAME': 'nobody',
'SHELL': '/bin/sh'
}
try:
result = subprocess.run(
command,
shell=True,
env=safe_env,
timeout=timeout,
capture_output=True,
text=True,
cwd=workdir,
preexec_fn=os.setsid # New process group
)
return {
'returncode': result.returncode,
'stdout': result.stdout,
'stderr': result.stderr,
'timed_out': False
}
except subprocess.TimeoutExpired:
return {'returncode': -1, 'stdout': '', 'stderr': 'Command timed out', 'timed_out': True}
except Exception as e:
return {'returncode': -1, 'stdout': '', 'stderr': str(e), 'timed_out': False}
finally:
# Cleanup
if os.path.exists(workdir) and workdir.startswith('/tmp'):
subprocess.run(['rm', '-rf', workdir])
def sizeof_format(num, suffix="B"):
for unit in ("", "K", "M", "G", "T", "P", "E", "Z"):
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
def get_dir_size(start_path):
total_size = 0
console_log(f"getting dir size of {start_path}..")
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
# skip if it is symbolic link
if not os.path.islink(fp):
total_size += os.path.getsize(fp)
return total_size