-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
269 lines (224 loc) · 7.75 KB
/
handler.py
File metadata and controls
269 lines (224 loc) · 7.75 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Handler.py
# For handling docker operation.
import docker
import random
import string
import time
import paramiko
import distro_handler
import logging
paramiko_logger = logging.getLogger("paramiko.transport")
paramiko_logger.addHandler(logging.NullHandler())
# Function to generate a random username from a list of words
def generate_username(word_list, length=2):
return "_".join(random.choices(word_list, k=length))
# Function to generate a random password with letters and numbers
def generate_password(length=12):
characters = string.ascii_letters + string.digits # Include both letters and digits
return "".join(random.choices(characters, k=length))
# Function to generate a random port number in the specified range
def generate_random_port(start=2280, end=2599):
return random.randint(start, end)
# Define environment variables
word_list = [
"demo",
"awesome",
"crazy",
"rod",
"cool",
"funny",
"smart",
"quick",
"jumpy",
"silly",
"happy",
"bright",
"bold",
"chill",
"zesty",
"fierce",
"mellow",
"sneaky",
"witty",
"zany",
"quirky",
"spunky",
"cheerful",
"daring",
"epic",
"fuzzy",
"glowy",
"hasty",
"jolly",
"lively",
"mighty",
"playful",
"radiant",
"sassy",
"tasty",
"vivid",
"wacky",
"yummy",
"zippy",
"bubbly",
"cuddly",
"dreamy",
"energetic",
"frosty",
"giddy",
"hilarious",
"jazzy",
"kooky",
"luminous",
"mirthful",
] # Expanded list of words for username generation
def is_container_running(container_name: str):
"""Verify the status of a container by its name.
:param container_name: The name of the container.
:return: True if running, False if stopped, None if not found.
"""
RUNNING = "running"
# Connect to Docker
docker_client = docker.from_env()
try:
# Get the container by name
container = docker_client.containers.get(container_name)
except docker.errors.NotFound as exc:
print(f"Check container name!\n{exc.explanation}")
return None
# Check the status of the container
container_state = container.attrs["State"]
return container_state["Status"] == RUNNING
def check_container_existence(container_name):
"""Check if a container exists."""
try:
client = docker.from_env()
client.containers.get(container_name)
except docker.errors.NotFound:
return False
return True
def fetch_container_state(container_name):
"""Fetch the state of the container. stopped or running Returns True if running, False if stopped, None if not found."""
try:
client = docker.from_env()
container = client.containers.get(container_name)
container_state = container.attrs["State"]
return container_state["Status"] == "running"
except docker.errors.NotFound:
return None
def test_docker_connection():
try:
client = docker.from_env()
client.containers.list()
return True
except:
return False
def check_ssh_connection(username, hostname, port=22, password=None):
"""
Check if an SSH connection can be established to a server.
Args:
username (str): The username for SSH authentication.
hostname (str): The hostname or IP address of the server.
port (int): The port number for SSH (default is 22).
password (str): The password for SSH authentication.
Returns:
bool: True if connection is successful, False otherwise.
"""
try:
# Create an SSH client
ssh = paramiko.SSHClient()
# Automatically add the server's host key (this is not recommended for production)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh.connect(hostname, port=port, username=username, password=password)
# If successful, return True
return True
except paramiko.AuthenticationException:
return False
except paramiko.SSHException:
return False
except:
return False
finally:
# Close the connection if it was established
ssh.close()
def create_container(web_dashboard_host, port, outsider_port, distro="alpine"):
username = generate_username(word_list)
password = generate_password()
environment_vars = {
"SUDO_ACCESS": "true",
"PASSWORD_ACCESS": "true",
"USER_PASSWORD": password,
"USER_NAME": username,
"INSTALL_SCRIPT_URL": web_dashboard_host,
"WELCOME_MESSAGE": "\033[36mWelcome to your temporary environment.\033[0m\nTemporary SSH Server made by kokofixcomputers\nGitHub: https://github.com/kokofixcomputers/TemporarySSHSessionManager.git \nLicensed Under the Mit License.\n\n\033[32mTo access sudo, run a command with sudo and enter the password shown in the dashboard when asked for password.\033[0m\n\n\n\033[1mBy Using the Container, You agree to the conditions below. \n1. You will REFRAIN from using anything illegal or unethical.\033[0m\n2. For and under any circumstances, You allow admins to connect and execute commands in your container.\n3. You agree that you allow the security and performance monitoring applications installed automaticlly to run.\n By using this container, You agree to the above notes. Please also refrain from using any confidential information in this enviroment.\n\nPlease Consider leaving a ⭐ Star on our repo!\n\033[0mHave fun!",
}
# Find image of distro
image = distro_handler.get_image(distro)
# Create and run the container
try:
client = docker.from_env()
time.sleep(1)
container = client.containers.run(
image,
detach=True, # REQUIRED for running in the background
environment=environment_vars,
network="stm",
# user=1000,
mem_limit="612m",
hostname=f"{username}@stm",
ports={"2222/tcp": port, "80": outsider_port}, # Add ports mapping
cpu_count=1,
)
except:
return None, None, None, None, None
restart_container(container.name)
can_connect = False
while not can_connect:
can_connect = check_ssh_connection(
username=username, password=password, hostname="localhost", port=port
)
time.sleep(2)
return container.name, username, password, port, outsider_port
def restart_container(name):
try:
client = docker.from_env()
container = client.containers.get(name)
container.restart()
time.sleep(1)
except docker.errors.NotFound:
return True # No such container. May have been removed already. Return True to update database.
except:
return None
return True
def stop_container(name):
try:
client = docker.from_env()
container = client.containers.get(name)
container.stop()
except docker.errors.NotFound:
return True # No such container. May have been removed already. Return True to update database.
except:
return None
return True
def start_container(name):
try:
client = docker.from_env()
container = client.containers.get(name)
container.start()
time.sleep(1)
except docker.errors.NotFound:
return True # No such container. May have been removed already. Return True to update database.
except:
return None
return True
def delete_container(name):
try:
client = docker.from_env()
container = client.containers.get(name)
container.remove(force=True)
except docker.errors.NotFound:
return True # No such container. May have been removed already. Return True to update database.
except:
return None
return True