-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconsumers.py
More file actions
231 lines (188 loc) · 6.83 KB
/
consumers.py
File metadata and controls
231 lines (188 loc) · 6.83 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import fcntl
import json
import logging
import os
import pty
import re
import select
import shutil
import signal
import struct
import subprocess
import termios
import threading
from channels.generic.websocket import WebsocketConsumer
from django.apps import apps
from django.conf import settings
from django.contrib.admin.models import CHANGE, LogEntry
from .models import TerminalCommand
DEFAULT_COMMANDS = [
["./manage.py", "shell_plus"],
["./manage.py", "shell"],
["/bin/bash"],
]
class TerminalConsumer(WebsocketConsumer):
child_pid = None
fd = None
shell = None
command = []
user = None
subprocess = None
authorized = False
connected = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
configured_commands = getattr(
settings, "DJANGO_ADMIN_SHELLX_COMMANDS", DEFAULT_COMMANDS
)
# Check if each command is available in the system
for command in configured_commands:
path = shutil.which(command[0])
if path:
if "shell_plus" in command:
if apps.is_installed("django_extensions"):
self.command = command
break
continue
self.command = command
break
def run_command(self):
master_fd, slave_fd = pty.openpty()
self.fd = master_fd
with subprocess.Popen( # pylint: disable=subprocess-popen-preexec-fn
self.command,
stdin=slave_fd,
stdout=slave_fd,
stderr=slave_fd,
preexec_fn=os.setsid,
) as proc:
self.subprocess = proc
self.child_pid = proc.pid
proc.wait()
# Subprocess has finished, close the websocket when the process
# exits, either by using exit() or by failing.
self.subprocess = None
self.child_pid = None
if self.connected:
self.connected = False
self.close(4030)
def connect(self):
if "user" not in self.scope:
self.close(4401)
return
self.user = self.scope["user"]
if not self.user.is_authenticated:
self.close(4401)
return
if getattr(settings, "DJANGO_ADMIN_SHELLX_SUPERUSER_ONLY", True):
if not self.user.is_superuser:
self.close(4403)
return
if self.child_pid is not None:
return
if self.user.is_authenticated:
self.connected = True
self.authorized = True
self.accept()
# Daemonize the thread so it automatically dies when the main thread exits
thread = threading.Thread(target=self.run_command, daemon=True)
thread.start()
thread = threading.Thread(target=self.read_from_pty, daemon=True)
thread.start()
def read_from_pty(self):
while True:
select.select([self.fd], [], [])
output = os.read(self.fd, 1024)
if not output:
break
message = output.decode(errors="ignore")
self.send(text_data=json.dumps({"message": message}))
def resize(self, row, col, xpix=0, ypix=0):
winsize = struct.pack("HHHH", row, col, xpix, ypix)
fcntl.ioctl(self.fd, termios.TIOCSWINSZ, winsize)
def write_to_pty(self, message):
os.write(self.fd, message.encode())
def kill_pty(self):
if self.subprocess is not None:
os.killpg(os.getpgid(self.child_pid), signal.SIGTERM)
self.subprocess = None
self.child_pid = None
def disconnect(self, code):
self.connected = False
self.kill_pty()
def map_terminal_prompt(self, terminal_prompt):
if "reverse-i-search" in terminal_prompt or "I-search" in terminal_prompt:
return None, None, True
mapped = False
command = None
prompt = None
# pattern >>> TerminalCommand.objects.all()
match_1 = re.match(r">>> ?(.*)", terminal_prompt)
# pattern In [2]: TerminalCommand.objects.all()'
match_2 = re.match(r"In \[.*\]: ?(.*)", terminal_prompt)
# [adin@adin test]$ echo 'hello world'
match_3 = re.match(r".*[#|$] ?(.+)", terminal_prompt)
if match_1:
command = match_1.group(1)
prompt = "django-shell"
mapped = True
elif match_2:
command = match_2.group(1)
prompt = "django-shell"
mapped = True
elif match_3:
command = match_3.group(1)
prompt = "shell"
mapped = True
else:
logging.debug("Could not extract command from prompt: %s", terminal_prompt)
return command, prompt, mapped
def save_command_history(self, command):
command, prompt, mapped = self.map_terminal_prompt(command)
# Ignore successful mappings but empty command
# e.g user pressing enter or using search history
if not command and mapped:
logging.debug("Ignoring empty command")
return
if not command:
logging.warning("No command to save")
return
tc, _ = TerminalCommand.objects.get_or_create(
command=command, prompt=prompt, defaults={"created_by": self.user}
)
tc.execution_count += 1
tc.save()
# Create a log entry for the command
LogEntry.objects.log_actions(
user_id=self.user.id,
queryset=TerminalCommand.objects.filter(pk=tc.pk),
action_flag=CHANGE,
change_message={"changed": {"name": "action", "object": tc.command}},
single_object=True,
)
def receive(self, text_data=None, bytes_data=None):
if not self.authorized:
return
if not text_data:
logging.debug("No data received")
return
try:
text_data_json = json.loads(text_data)
except json.JSONDecodeError:
logging.warning("Could not decode JSON: %s", text_data)
return
action = text_data_json.get("action")
if action == "resize":
self.resize(text_data_json["data"]["rows"], text_data_json["data"]["cols"])
elif action in ["input", "save_history"]:
if action == "input":
message = text_data_json["data"]["message"]
self.write_to_pty(message)
else:
if text_data_json["data"]["command"]:
self.save_command_history(text_data_json["data"]["command"])
elif action == "kill":
self.kill_pty()
self.send(text_data=json.dumps({"message": "Terminal killed"}))
else:
logging.info("Unknown action: %s,", text_data_json["action"])