Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added MahsanHadi/MahsanHadi-problem123/.DS_Store
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions MahsanHadi/MahsanHadi-problem123/ctf_celery/celery_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from celery import Celery
import subprocess

#Configure Celery to use Redis as its message broker.
app = Celery(
'celery_tasks',
broker='redis://localhost:6379/0',
backend='redis://localhost:6379/1'
)

@app.task
def start_ctf_container(image_name, container_name):
try:
# check if container exists or not
result = subprocess.run(
['docker', 'ps', '-a', '--filter', f'name=^{container_name}$', '--format', '{{.Names}}'],
capture_output=True,
text=True
)
if container_name in result.stdout.strip():
# start if exists
subprocess.run(['docker', 'start', container_name], check=True)
return f"Existing container '{container_name}' started."
else:
# make if doesn't exist
subprocess.run(['docker', 'run', '-d', '--name', container_name, image_name], check=True)
return f"New container '{container_name}' started from image '{image_name}'."
except subprocess.CalledProcessError as e:
return f"Failed to start container: {e}"

@app.task
def stop_ctf_container(container_name):
try:
subprocess.run(['docker', 'stop', container_name], check=True)
return f"Container '{container_name}' stopped."
except subprocess.CalledProcessError as e:
return f"Failed to stop container: {e}"
5 changes: 5 additions & 0 deletions MahsanHadi/MahsanHadi-problem123/ctf_celery/problem3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PROBLEM 3

celery -A celery_tasks worker --loglevel=info دستور اجرای سلری

python3 run_tasks.py دستور اجرای تسک ها
11 changes: 11 additions & 0 deletions MahsanHadi/MahsanHadi-problem123/ctf_celery/run_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from celery_tasks import start_ctf_container, stop_ctf_container

# start a container for test
result1 = start_ctf_container.delay('nginx', 'my_nginx_ctf')

print("running...")
print(result1.get(timeout=10))

# stop a container for test
# result2 = stop_ctf_container.delay('my_nginx_ctf')
# print(result2.get(timeout=10))
26 changes: 26 additions & 0 deletions MahsanHadi/MahsanHadi-problem123/ctf_postgres/problem1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PROBLEM 1

docker volume create persistent_data ساخت volume برای ذخیره دائمی داده ها

docker run --name ctf_postgres \ نام کانتینر
-e POSTGRES_USER=mahsan \ نام کاربر دیتابیس
-e POSTGRES_PASSWORD=mahsan \ پسورد کاربر
-e POSTGRES_DB=ctf \ نام دیتابیس
-v ctf_postgres:/var/lib/postgresql/data \ تعیین volume برای ذخیره دائم داده های دیتابیس
-p 5432:5432 \ تعیین پورت
-d postgres اجرا در پس زمینه

docker exec -it ctf_postgres psql -U mahsan -d ctf اتصال به دیتابیسی که ساختیم با کاربری که تعریف کردیم در کانتینر

CREATE TABLE sample_table (
name VARCHAR(50) PRIMARY KEY
);

INSERT INTO sample_table (name) VALUES
('Mahsan'),
('Hadi');

SELECT * FROM sample_table;

docker stop ctf_postgres
docker rm ctf_postgres
23 changes: 23 additions & 0 deletions MahsanHadi/MahsanHadi-problem123/ctf_redis/consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)

# recieve key values
for key in ['Mahsan', 'Arman']:
value = r.get(key)
if value:
print(f"{key}: {value}")
else:
print(f"{key}: key not found.")

# subscribe redis channel
channel = 'notifications'
pubsub = r.pubsub()
pubsub.subscribe(channel)

print("Consumer: Waiting for messages...")
for message in pubsub.listen():
if message['type'] == 'message':
print(f"Received message: {message['data']}")
break

10 changes: 10 additions & 0 deletions MahsanHadi/MahsanHadi-problem123/ctf_redis/problem2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PROBLEM 2

docker run --name ctf_redis -p 6379:6379 -d redis اجرای کانتینر ردیس

python3 consumer.py اجرای برنامه برای دریافت پیام ها (منتظر میماند تا برنامه فرستنده اجرا شود)

مانیتور پیام و کلید ها با redis insight

python3 producer.py اجرای برنامه برای ارسال پیام ها
python3 consumer.py دریافت پیام ها
14 changes: 14 additions & 0 deletions MahsanHadi/MahsanHadi-problem123/ctf_redis/producer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import redis
import time

r = redis.Redis(host='localhost', port=6379, decode_responses=True)

# make key values
r.set('Mahsan', 'Player1')
r.set('Arman', 'Player2')

# send message
channel = 'notifications'
r.publish(channel, 'start!')

print("Producer: sent messages.")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions MahsanHadi/link_to_videos
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://iutbox.iut.ac.ir/index.php/s/aysqC4WHZ7w56mR