termux-mcp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- termux_mcp-0.1.0/LICENSE +21 -0
- termux_mcp-0.1.0/MANIFEST.in +4 -0
- termux_mcp-0.1.0/PKG-INFO +48 -0
- termux_mcp-0.1.0/README.md +34 -0
- termux_mcp-0.1.0/pyproject.toml +27 -0
- termux_mcp-0.1.0/setup.cfg +24 -0
- termux_mcp-0.1.0/setup.py +23 -0
- termux_mcp-0.1.0/termux_mcp/__init__.py +17 -0
- termux_mcp-0.1.0/termux_mcp/config.py +18 -0
- termux_mcp-0.1.0/termux_mcp/handler.py +69 -0
- termux_mcp-0.1.0/termux_mcp/network.py +26 -0
- termux_mcp-0.1.0/termux_mcp/security.py +77 -0
- termux_mcp-0.1.0/termux_mcp/server.py +33 -0
- termux_mcp-0.1.0/termux_mcp/shell.py +121 -0
termux_mcp-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bhai4You
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: termux-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Powerful MCP server for Termux — let AI agents run shell commands on your Android device in real time.
|
|
5
|
+
Home-page: https://github.com/Bhai4You/Termux-mcp
|
|
6
|
+
Author: Bhai4You
|
|
7
|
+
Author-email: parixit1337@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# termux-mcp
|
|
16
|
+
|
|
17
|
+
Powerful MCP server for Termux — let AI agents run shell commands on your Android device in real time.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install termux-mcp
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import termux_mcp
|
|
29
|
+
|
|
30
|
+
# Your code here
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Requirements
|
|
34
|
+
|
|
35
|
+
No external dependencies required.
|
|
36
|
+
|
|
37
|
+
## Author
|
|
38
|
+
|
|
39
|
+
**Bhai4You** - [parixit1337@gmail.com](mailto:parixit1337@gmail.com)
|
|
40
|
+
|
|
41
|
+
## Links
|
|
42
|
+
|
|
43
|
+
- [Homepage](https://github.com/Bhai4You/Termux-mcp)
|
|
44
|
+
- [PyPI](https://pypi.org/project/termux-mcp/)
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# termux-mcp
|
|
2
|
+
|
|
3
|
+
Powerful MCP server for Termux — let AI agents run shell commands on your Android device in real time.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install termux-mcp
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import termux_mcp
|
|
15
|
+
|
|
16
|
+
# Your code here
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Requirements
|
|
20
|
+
|
|
21
|
+
No external dependencies required.
|
|
22
|
+
|
|
23
|
+
## Author
|
|
24
|
+
|
|
25
|
+
**Bhai4You** - [parixit1337@gmail.com](mailto:parixit1337@gmail.com)
|
|
26
|
+
|
|
27
|
+
## Links
|
|
28
|
+
|
|
29
|
+
- [Homepage](https://github.com/Bhai4You/Termux-mcp)
|
|
30
|
+
- [PyPI](https://pypi.org/project/termux-mcp/)
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "termux-mcp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Powerful MCP server for Termux — let AI agents run shell commands on your Android device in real time."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Bhai4You", email = "parixit1337@gmail.com" }
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.7"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/Bhai4You/Termux-mcp"
|
|
23
|
+
Issues = "https://github.com/Bhai4You/Termux-mcp/issues"
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
where = ["."]
|
|
27
|
+
include = ["termux_mcp*"]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = termux-mcp
|
|
3
|
+
version = 0.1.0
|
|
4
|
+
author = Bhai4You
|
|
5
|
+
author_email = parixit1337@gmail.com
|
|
6
|
+
description = Powerful MCP server for Termux — let AI agents run shell commands on your Android device in real time.
|
|
7
|
+
long_description = file: README.md
|
|
8
|
+
long_description_content_type = text/markdown
|
|
9
|
+
url = https://github.com/Bhai4You/Termux-mcp
|
|
10
|
+
classifiers =
|
|
11
|
+
Programming Language :: Python :: 3
|
|
12
|
+
License :: OSI Approved :: MIT License
|
|
13
|
+
Operating System :: OS Independent
|
|
14
|
+
license = MIT
|
|
15
|
+
|
|
16
|
+
[options]
|
|
17
|
+
packages = find:
|
|
18
|
+
python_requires = >=3.7
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
[options.packages.find]
|
|
22
|
+
exclude =
|
|
23
|
+
tests*
|
|
24
|
+
test*
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
with open("README.md", "r", encoding="utf-8") as fh:
|
|
4
|
+
long_description = fh.read()
|
|
5
|
+
|
|
6
|
+
setup(
|
|
7
|
+
name="termux-mcp",
|
|
8
|
+
version="0.1.0",
|
|
9
|
+
author="Bhai4You",
|
|
10
|
+
author_email="parixit1337@gmail.com",
|
|
11
|
+
description="Powerful MCP server for Termux — let AI agents run shell commands on your Android device in real time.",
|
|
12
|
+
long_description=long_description,
|
|
13
|
+
long_description_content_type="text/markdown",
|
|
14
|
+
url="https://github.com/Bhai4You/Termux-mcp",
|
|
15
|
+
packages=find_packages(),
|
|
16
|
+
classifiers=[
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
],
|
|
21
|
+
python_requires=">=3.7",
|
|
22
|
+
install_requires=[],
|
|
23
|
+
)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""
|
|
2
|
+
termux-mcp - Powerful MCP server for Termux — let AI agents run shell commands on your Android device in real time.
|
|
3
|
+
Author: Bhai4You <parixit1337@gmail.com>
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
__version__ = "0.1.0"
|
|
7
|
+
__author__ = "Bhai4You"
|
|
8
|
+
__email__ = "parixit1337@gmail.com"
|
|
9
|
+
|
|
10
|
+
from .config import *
|
|
11
|
+
from .handler import *
|
|
12
|
+
from .network import *
|
|
13
|
+
from .security import *
|
|
14
|
+
from .server import *
|
|
15
|
+
from .shell import *
|
|
16
|
+
|
|
17
|
+
__all__ = ["__version__", "__author__", "__email__"]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
PORT: int = int(os.environ.get("TERMUX_MCP_PORT", 8080))
|
|
4
|
+
HOST: str = os.environ.get("TERMUX_MCP_HOST", "0.0.0.0")
|
|
5
|
+
|
|
6
|
+
HOME: str = os.environ.get("HOME", "/data/data/com.termux/files/home")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
AUTO_INPUT_INTERVAL: float = 0.5
|
|
10
|
+
PORT_POLL_INTERVAL: float = 0.3
|
|
11
|
+
AUTO_YES_COMMANDS: list[str] = [
|
|
12
|
+
"pkg install",
|
|
13
|
+
"pkg upgrade",
|
|
14
|
+
"pkg update",
|
|
15
|
+
"apt install",
|
|
16
|
+
"apt upgrade",
|
|
17
|
+
"apt update",
|
|
18
|
+
]
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
from http.server import BaseHTTPRequestHandler
|
|
4
|
+
from urllib.parse import urlparse
|
|
5
|
+
|
|
6
|
+
from .shell import execute_streaming, get_current_dir
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MCPHandler(BaseHTTPRequestHandler):
|
|
12
|
+
protocol_version = "HTTP/1.1"
|
|
13
|
+
|
|
14
|
+
def log_message(self, fmt: str, *args) -> None:
|
|
15
|
+
logger.debug("[HTTP] " + fmt, *args)
|
|
16
|
+
|
|
17
|
+
def _log(self, msg: str):
|
|
18
|
+
logger.info(f"[MCP] {msg}")
|
|
19
|
+
|
|
20
|
+
def _read_json(self) -> dict:
|
|
21
|
+
try:
|
|
22
|
+
length = int(self.headers.get("Content-Length", 0))
|
|
23
|
+
raw = self.rfile.read(length).decode("utf-8", errors="ignore")
|
|
24
|
+
self._log(f"Request body: {raw}")
|
|
25
|
+
if not raw:
|
|
26
|
+
return {}
|
|
27
|
+
return json.loads(raw)
|
|
28
|
+
except Exception as e:
|
|
29
|
+
self._log(f"JSON read error: {e}")
|
|
30
|
+
return {}
|
|
31
|
+
|
|
32
|
+
def _json_response(self, status: int, payload: dict) -> None:
|
|
33
|
+
body = json.dumps(payload).encode("utf-8")
|
|
34
|
+
self.send_response(status)
|
|
35
|
+
self.send_header("Content-Type", "application/json")
|
|
36
|
+
self.send_header("Content-Length", str(len(body)))
|
|
37
|
+
self.end_headers()
|
|
38
|
+
self.wfile.write(body)
|
|
39
|
+
|
|
40
|
+
def do_GET(self) -> None:
|
|
41
|
+
path = urlparse(self.path).path
|
|
42
|
+
self._log(f"GET {path}")
|
|
43
|
+
|
|
44
|
+
if path == "/ping":
|
|
45
|
+
self._json_response(200, {
|
|
46
|
+
"status": "ok",
|
|
47
|
+
"cwd": get_current_dir(),
|
|
48
|
+
})
|
|
49
|
+
return
|
|
50
|
+
|
|
51
|
+
self._json_response(404, {"error": "Not found"})
|
|
52
|
+
|
|
53
|
+
def do_POST(self) -> None:
|
|
54
|
+
path = urlparse(self.path).path
|
|
55
|
+
self._log(f"POST {path}")
|
|
56
|
+
|
|
57
|
+
data = self._read_json()
|
|
58
|
+
self._log(f"Parsed JSON: {data}")
|
|
59
|
+
|
|
60
|
+
if path == "/run":
|
|
61
|
+
cmd = data.get("cmd", "").strip()
|
|
62
|
+
if not cmd:
|
|
63
|
+
self._json_response(400, {"error": "Missing 'cmd'"})
|
|
64
|
+
return
|
|
65
|
+
self._log(f"Executing: {cmd}")
|
|
66
|
+
execute_streaming(self, cmd)
|
|
67
|
+
return
|
|
68
|
+
|
|
69
|
+
self._json_response(404, {"error": "Not found"})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import socket
|
|
2
|
+
import subprocess
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
from .config import PORT_POLL_INTERVAL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def kill_port(port: int) -> None:
|
|
9
|
+
try:
|
|
10
|
+
result = subprocess.run(
|
|
11
|
+
f"lsof -t -i:{port}",
|
|
12
|
+
shell=True,
|
|
13
|
+
capture_output=True,
|
|
14
|
+
text=True,
|
|
15
|
+
)
|
|
16
|
+
for pid in result.stdout.strip().splitlines():
|
|
17
|
+
if pid:
|
|
18
|
+
subprocess.run(f"kill -9 {pid}", shell=True, check=False)
|
|
19
|
+
except Exception:
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
while True:
|
|
23
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
24
|
+
if sock.connect_ex(("127.0.0.1", port)) != 0:
|
|
25
|
+
return
|
|
26
|
+
time.sleep(PORT_POLL_INTERVAL)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import Tuple, Optional
|
|
3
|
+
|
|
4
|
+
class CommandRiskLevel:
|
|
5
|
+
SAFE = "safe"
|
|
6
|
+
WARNING = "warning"
|
|
7
|
+
DANGEROUS = "dangerous"
|
|
8
|
+
|
|
9
|
+
DANGEROUS_PATTERNS = [
|
|
10
|
+
r'rm\s+-rf\s+/?\s*$', # rm -rf /
|
|
11
|
+
r'rm\s+-rf\s+~', # rm -rf ~
|
|
12
|
+
r'rm\s+-rf\s+/\*', # rm -rf /*
|
|
13
|
+
r'rm\s+-rf\s+--no-preserve-root', # Attempts to bypass safety
|
|
14
|
+
|
|
15
|
+
r'dd\s+if=', # dd if=...
|
|
16
|
+
r'mkfs\.', # mkfs.ext4, mkfs.ntfs etc.
|
|
17
|
+
r'format\s+', # format commands
|
|
18
|
+
|
|
19
|
+
r':\(\)\s*\{\s*:\|\s*&\s*\};:', # Classic fork bomb
|
|
20
|
+
|
|
21
|
+
r'>\s*/dev/', # Redirect to /dev/*
|
|
22
|
+
r'echo\s+.*>\s*/dev/',
|
|
23
|
+
|
|
24
|
+
r'chmod\s+-R\s+777', # chmod -R 777 /
|
|
25
|
+
r'chmod\s+-R\s+000',
|
|
26
|
+
r'chown\s+-R\s+root',
|
|
27
|
+
|
|
28
|
+
r'pkg\s+remove\s+termux.*', # Removing core Termux packages
|
|
29
|
+
r'apt\s+purge\s+-y\s+.*termux',
|
|
30
|
+
|
|
31
|
+
r';\s*rm\s+-rf',
|
|
32
|
+
r'&&\s*rm\s+-rf',
|
|
33
|
+
r'\|\s*rm\s+-rf',
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
WARNING_PATTERNS = [
|
|
37
|
+
r'rm\s+-rf', # Any rm -rf (even on folders)
|
|
38
|
+
r'rm\s+-r', # Recursive remove
|
|
39
|
+
r'>>\s*/dev/null', # Overwriting logs aggressively
|
|
40
|
+
r'chmod\s+-R', # Recursive chmod
|
|
41
|
+
r'find\s+.*-delete', # Find + delete
|
|
42
|
+
r'> /', # Redirect to root
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
def is_dangerous_command(cmd: str) -> Tuple[bool, str, str]:
|
|
46
|
+
cmd_lower = cmd.strip().lower()
|
|
47
|
+
|
|
48
|
+
if not cmd_lower or len(cmd_lower) < 3:
|
|
49
|
+
return False, CommandRiskLevel.SAFE, ""
|
|
50
|
+
|
|
51
|
+
for pattern in DANGEROUS_PATTERNS:
|
|
52
|
+
if re.search(pattern, cmd_lower):
|
|
53
|
+
return True, CommandRiskLevel.DANGEROUS, f"Blocked dangerous command: {cmd}"
|
|
54
|
+
|
|
55
|
+
for pattern in WARNING_PATTERNS:
|
|
56
|
+
if re.search(pattern, cmd_lower):
|
|
57
|
+
return False, CommandRiskLevel.WARNING, f"High-risk command detected (confirmation recommended): {cmd}"
|
|
58
|
+
|
|
59
|
+
if "sudo" in cmd_lower and "rm" in cmd_lower:
|
|
60
|
+
return False, CommandRiskLevel.WARNING, "sudo + rm combination detected"
|
|
61
|
+
|
|
62
|
+
if cmd_lower.startswith(("reboot", "shutdown", "poweroff")):
|
|
63
|
+
return False, CommandRiskLevel.WARNING, "System shutdown/reboot command detected"
|
|
64
|
+
|
|
65
|
+
return False, CommandRiskLevel.SAFE, ""
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def get_risk_assessment(cmd: str) -> dict:
|
|
69
|
+
blocked, level, message = is_dangerous_command(cmd)
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
"command": cmd,
|
|
73
|
+
"risk_level": level,
|
|
74
|
+
"blocked": blocked,
|
|
75
|
+
"message": message or "Command appears safe",
|
|
76
|
+
"requires_confirmation": level == CommandRiskLevel.WARNING
|
|
77
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sys
|
|
3
|
+
from http.server import HTTPServer
|
|
4
|
+
|
|
5
|
+
from .config import HOST, PORT
|
|
6
|
+
from .handler import MCPHandler
|
|
7
|
+
from .network import kill_port
|
|
8
|
+
from .shell import get_current_dir
|
|
9
|
+
|
|
10
|
+
logging.basicConfig(
|
|
11
|
+
level=logging.INFO,
|
|
12
|
+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
13
|
+
datefmt="%H:%M:%S",
|
|
14
|
+
)
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def run() -> None:
|
|
19
|
+
logger.info("Freeing port %d if occupied…", PORT)
|
|
20
|
+
kill_port(PORT)
|
|
21
|
+
|
|
22
|
+
server = HTTPServer((HOST, PORT), MCPHandler)
|
|
23
|
+
|
|
24
|
+
logger.info("🚀 TermuxMCP running on http://localhost:%d", PORT)
|
|
25
|
+
logger.info("📂 Working dir: %s", get_current_dir())
|
|
26
|
+
logger.info("Press Ctrl+C to stop.\n")
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
server.serve_forever()
|
|
30
|
+
except KeyboardInterrupt:
|
|
31
|
+
logger.info("Shutting down…")
|
|
32
|
+
server.server_close()
|
|
33
|
+
sys.exit(0)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import subprocess
|
|
3
|
+
import threading
|
|
4
|
+
import time
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from .config import AUTO_INPUT_INTERVAL, AUTO_YES_COMMANDS, HOME
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from http.server import BaseHTTPRequestHandler
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
_current_dir: str = os.getcwd()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_current_dir() -> str:
|
|
17
|
+
return _current_dir
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def set_current_dir(path: str) -> None:
|
|
21
|
+
global _current_dir
|
|
22
|
+
_current_dir = path
|
|
23
|
+
|
|
24
|
+
def _inject_noninteractive(cmd: str) -> str:
|
|
25
|
+
return f"export DEBIAN_FRONTEND=noninteractive; {cmd}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _inject_auto_yes(cmd: str) -> str:
|
|
29
|
+
for trigger in AUTO_YES_COMMANDS:
|
|
30
|
+
if trigger in cmd and "-y" not in cmd:
|
|
31
|
+
cmd = cmd.replace(trigger, f"{trigger} -y")
|
|
32
|
+
return cmd
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def preprocess(cmd: str) -> str:
|
|
36
|
+
cmd = _inject_auto_yes(cmd)
|
|
37
|
+
cmd = _inject_noninteractive(cmd)
|
|
38
|
+
return cmd
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def handle_cd(parts: list[str]) -> tuple[bool, str]:
|
|
42
|
+
if len(parts) == 1:
|
|
43
|
+
set_current_dir(HOME)
|
|
44
|
+
return True, f"📂 {_current_dir}"
|
|
45
|
+
|
|
46
|
+
raw_path = parts[1].strip().replace("~", HOME, 1)
|
|
47
|
+
new_path = os.path.abspath(
|
|
48
|
+
raw_path if os.path.isabs(raw_path) else os.path.join(_current_dir, raw_path)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if os.path.isdir(new_path):
|
|
52
|
+
set_current_dir(new_path)
|
|
53
|
+
return True, f"📂 {_current_dir}"
|
|
54
|
+
|
|
55
|
+
return False, f"❌ Directory not found: {new_path}"
|
|
56
|
+
|
|
57
|
+
def _send_chunk(handler: "BaseHTTPRequestHandler", text: str) -> None:
|
|
58
|
+
data = text.encode()
|
|
59
|
+
size = hex(len(data))[2:].encode()
|
|
60
|
+
handler.wfile.write(size + b"\r\n" + data + b"\r\n")
|
|
61
|
+
handler.wfile.flush()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _finalize_chunks(handler: "BaseHTTPRequestHandler") -> None:
|
|
65
|
+
handler.wfile.write(b"0\r\n\r\n")
|
|
66
|
+
|
|
67
|
+
def _spawn_auto_input(process: subprocess.Popen) -> None:
|
|
68
|
+
def _worker() -> None:
|
|
69
|
+
try:
|
|
70
|
+
while process.poll() is None:
|
|
71
|
+
time.sleep(AUTO_INPUT_INTERVAL)
|
|
72
|
+
process.stdin.write("y\n")
|
|
73
|
+
process.stdin.flush()
|
|
74
|
+
except Exception:
|
|
75
|
+
pass
|
|
76
|
+
|
|
77
|
+
threading.Thread(target=_worker, daemon=True).start()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def execute_streaming(handler: "BaseHTTPRequestHandler", raw_cmd: str) -> None:
|
|
82
|
+
raw_cmd = raw_cmd.strip()
|
|
83
|
+
|
|
84
|
+
if raw_cmd.startswith("cd"):
|
|
85
|
+
ok, msg = handle_cd(raw_cmd.split(maxsplit=1))
|
|
86
|
+
handler.send_response(200)
|
|
87
|
+
handler.send_header("Content-Type", "text/plain")
|
|
88
|
+
handler.end_headers()
|
|
89
|
+
handler.wfile.write((msg + "\n").encode())
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
handler.send_response(200)
|
|
93
|
+
handler.send_header("Content-Type", "text/plain")
|
|
94
|
+
handler.send_header("Transfer-Encoding", "chunked")
|
|
95
|
+
handler.end_headers()
|
|
96
|
+
|
|
97
|
+
cmd = preprocess(raw_cmd)
|
|
98
|
+
|
|
99
|
+
process = subprocess.Popen(
|
|
100
|
+
f"export PAGER=cat; {cmd}",
|
|
101
|
+
shell=True,
|
|
102
|
+
stdout=subprocess.PIPE,
|
|
103
|
+
stderr=subprocess.STDOUT,
|
|
104
|
+
stdin=subprocess.PIPE,
|
|
105
|
+
text=True,
|
|
106
|
+
cwd=_current_dir,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
_spawn_auto_input(process)
|
|
110
|
+
|
|
111
|
+
for line in process.stdout:
|
|
112
|
+
_send_chunk(handler, line)
|
|
113
|
+
|
|
114
|
+
process.wait()
|
|
115
|
+
|
|
116
|
+
if process.returncode != 0:
|
|
117
|
+
_send_chunk(handler, f"\n❌ Exit code: {process.returncode}\n")
|
|
118
|
+
else:
|
|
119
|
+
_send_chunk(handler, "\n✅ Done\n")
|
|
120
|
+
|
|
121
|
+
_finalize_chunks(handler)
|