Compare commits
	
		
			1 Commits
		
	
	
		
			31ec07ea15
			...
			dev
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 101df213dc | 
@ -82,13 +82,7 @@ def load(app):
 | 
			
		||||
    @page_blueprint.route("/admin/containers")
 | 
			
		||||
    @admins_only
 | 
			
		||||
    def admin_list_containers():
 | 
			
		||||
        page_type = request.args.get("type", "instances")
 | 
			
		||||
        if page_type == "logs":
 | 
			
		||||
            result = AdminContainers.logs()
 | 
			
		||||
            session['page_type'] = 'logs'
 | 
			
		||||
        else:
 | 
			
		||||
            result = AdminContainers.get()
 | 
			
		||||
            session['page_type'] = 'containers'
 | 
			
		||||
        result = AdminContainers.get()
 | 
			
		||||
        view_mode = request.args.get('mode', session.get('view_mode', 'list'))
 | 
			
		||||
        session['view_mode'] = view_mode
 | 
			
		||||
        return render_template("whale_containers.html",
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								api.py
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								api.py
									
									
									
									
									
								
							@ -46,25 +46,6 @@ class AdminContainers(Resource):
 | 
			
		||||
            'page_start': page_start,
 | 
			
		||||
        }}
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    @admins_only
 | 
			
		||||
    def logs():
 | 
			
		||||
        page = abs(request.args.get("page", 1, type=int))
 | 
			
		||||
        results_per_page = abs(request.args.get("per_page", 20, type=int))
 | 
			
		||||
        page_start = results_per_page * (page - 1)
 | 
			
		||||
        page_end = results_per_page * (page - 1) + results_per_page
 | 
			
		||||
 | 
			
		||||
        count = DBContainer.get_all_container_logs_count()
 | 
			
		||||
        containers = DBContainer.get_all_container_logs_page(
 | 
			
		||||
            page_start, page_end)
 | 
			
		||||
 | 
			
		||||
        return {'success': True, 'data': {
 | 
			
		||||
            'containers': containers,
 | 
			
		||||
            'total': count,
 | 
			
		||||
            'pages': int(count / results_per_page) + (count % results_per_page > 0),
 | 
			
		||||
            'page_start': page_start,
 | 
			
		||||
        }}
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    @admins_only
 | 
			
		||||
    def patch():
 | 
			
		||||
 | 
			
		||||
@ -122,7 +122,7 @@ class WhaleContainerLog(db.Model):
 | 
			
		||||
    def __init__(self, container):
 | 
			
		||||
        self.user_id = container.user_id
 | 
			
		||||
        self.challenge_id = container.challenge_id
 | 
			
		||||
        self.start_time = container.start_time
 | 
			
		||||
        self.start_time = container.challenge_id
 | 
			
		||||
        self.uuid = container.uuid
 | 
			
		||||
        self.flag = container.flag
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,44 +0,0 @@
 | 
			
		||||
<style>
 | 
			
		||||
    .info-card.card {
 | 
			
		||||
        height: 11rem;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .card-text {
 | 
			
		||||
        text-overflow: ellipsis;
 | 
			
		||||
        white-space: nowrap;
 | 
			
		||||
        overflow: hidden;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .card-text:hover {
 | 
			
		||||
        white-space: pre-line;
 | 
			
		||||
        overflow: visible;
 | 
			
		||||
    }
 | 
			
		||||
</style>
 | 
			
		||||
 | 
			
		||||
<div class="row">
 | 
			
		||||
    {% for container in containers %}
 | 
			
		||||
        <div class="col-sm-6 pb-3">
 | 
			
		||||
            <div class="info-card card">
 | 
			
		||||
                <div class="card-body">
 | 
			
		||||
                    <h5 class="d-inline-block card-title">
 | 
			
		||||
                        <a style="width: 5rem;"
 | 
			
		||||
                           href="{{ url_for('admin.challenges_detail', challenge_id=container.challenge.id) }}"
 | 
			
		||||
                        >{{ container.challenge.name | truncate(15) }}
 | 
			
		||||
                        </a>
 | 
			
		||||
                    </h5>
 | 
			
		||||
                    <h6 class="d-inline-block card-subtitle float-right">
 | 
			
		||||
                        <a style="width: 5rem;"
 | 
			
		||||
                           class="btn btn-outline-secondary rounded"
 | 
			
		||||
                           href="{{ url_for('admin.users_detail', user_id=container.user.id) }}"
 | 
			
		||||
                        >{{ container.user.name | truncate(5) }}
 | 
			
		||||
                        </a>
 | 
			
		||||
                    </h6>
 | 
			
		||||
                    <p class="card-text">UUID: {{ container.uuid }}</p>
 | 
			
		||||
                    <p class="card-text">{{ container.flag }}</p>
 | 
			
		||||
                    Time Started: {{ container.start_time }}
 | 
			
		||||
                    <span class="badge badge-secondary float-right">Log Record</span>
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
        </div>
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
</div>
 | 
			
		||||
@ -1,62 +0,0 @@
 | 
			
		||||
<div class="row">
 | 
			
		||||
    <div class="col-md-12">
 | 
			
		||||
        <table class="table table-striped border">
 | 
			
		||||
            <thead>
 | 
			
		||||
            <tr>
 | 
			
		||||
                <th class="border-right" data-checkbox>
 | 
			
		||||
                    <div class="form-check text-center"> 
 | 
			
		||||
                        <input type="checkbox" class="form-check-input" data-checkbox-all>
 | 
			
		||||
                    </div>
 | 
			
		||||
                </th>
 | 
			
		||||
                <th class="sort-col text-center"><b>ID</b></td>
 | 
			
		||||
                <th class="text-center"><b>User</b></td>
 | 
			
		||||
                <th class="sort-col text-center"><b>Challenge</b></td>
 | 
			
		||||
                <th class="text-center"><b>UUID</b></td>
 | 
			
		||||
                <th class="text-center"><b>Flag</b></td>
 | 
			
		||||
                <th class="sort-col text-center"><b>Startup Time</b></td>
 | 
			
		||||
                <th class="text-center"><b>Actions</b></td>
 | 
			
		||||
            </tr>
 | 
			
		||||
            </thead>
 | 
			
		||||
            <tbody>
 | 
			
		||||
            {% for container in containers %}
 | 
			
		||||
                <tr>
 | 
			
		||||
                    <td class="border-right" data-checkbox>
 | 
			
		||||
                        <div class="form-check text-center"> 
 | 
			
		||||
                            <input type="checkbox" class="form-check-input" data-user-id="{{ container.user.id }}">
 | 
			
		||||
                        </div>
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        {{ container.id }}
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        <a href="{{ url_for('admin.users_detail', user_id=container.user.id) }}">
 | 
			
		||||
                            {{ container.user.name | truncate(12) }}
 | 
			
		||||
                        </a>
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        <a href="{{ url_for('admin.challenges_detail', challenge_id=container.challenge.id) }}">
 | 
			
		||||
                            {{ container.challenge.name }}
 | 
			
		||||
                        </a>
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        <button class="btn btn-link p-0 click-copy" data-copy="{{ container.uuid }}">
 | 
			
		||||
                            <i class="fas fa-clipboard"></i>
 | 
			
		||||
                        </button>
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        <button class="btn btn-link p-0 click-copy" data-copy="{{ container.flag }}">
 | 
			
		||||
                            <i class="fas fa-clipboard"></i>
 | 
			
		||||
                        </button>
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        <span data-time="{{ container.start_time | isoformat }}"></span>
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td class="text-center">
 | 
			
		||||
                        <span class="badge badge-secondary">Log Record</span>
 | 
			
		||||
                    </td>
 | 
			
		||||
                </tr>
 | 
			
		||||
            {% endfor %}
 | 
			
		||||
            </tbody>
 | 
			
		||||
        </table>
 | 
			
		||||
    </div>
 | 
			
		||||
</div>
 | 
			
		||||
@ -5,12 +5,7 @@
 | 
			
		||||
        <a class="nav-link" href="/plugins/ctfd-whale/admin/settings">🔗 Settings</a>
 | 
			
		||||
    </li>
 | 
			
		||||
    <li class="nav-item">
 | 
			
		||||
        <a class="nav-link{{ ' active' if session.get('page_type', 'containers') == 'containers' else '' }}" 
 | 
			
		||||
        href="/plugins/ctfd-whale/admin/containers?type=instances">Instances</a>
 | 
			
		||||
    </li>
 | 
			
		||||
    <li class="nav-item">
 | 
			
		||||
        <a class="nav-link{{ ' active' if session.get('page_type') == 'logs' else '' }}" 
 | 
			
		||||
        href="/plugins/ctfd-whale/admin/containers?type=logs">Logs</a>
 | 
			
		||||
        <a class="nav-link active" href="#">Instances</a>
 | 
			
		||||
    </li>
 | 
			
		||||
 | 
			
		||||
    <li class="nav-item nav-link">
 | 
			
		||||
@ -30,7 +25,7 @@
 | 
			
		||||
        <ul class="pagination">
 | 
			
		||||
            <li class="page-item{{ ' disabled' if curr_page <= 1 else '' }}">
 | 
			
		||||
                <a class="page-link" aria-label="Previous"
 | 
			
		||||
                   href="/plugins/ctfd-whale/admin/containers?page={{ curr_page - 1 }}&type={{ session.get('page_type', 'containers') }}"
 | 
			
		||||
                   href="/plugins/ctfd-whale/admin/containers?page={{ curr_page - 1 }}"
 | 
			
		||||
                >
 | 
			
		||||
                    <span aria-hidden="true">«</span>
 | 
			
		||||
                    <span class="sr-only">Previous</span>
 | 
			
		||||
@ -41,13 +36,13 @@
 | 
			
		||||
            {% for page in range(range_l, range_r + 1) %}
 | 
			
		||||
                <li class="page-item{{ ' active' if curr_page == page }}">
 | 
			
		||||
                    <a class="page-link"
 | 
			
		||||
                       href="/plugins/ctfd-whale/admin/containers?page={{ page }}&type={{ request.args.get('type', 'instances') }}"
 | 
			
		||||
                       href="/plugins/ctfd-whale/admin/containers?page={{ page }}"
 | 
			
		||||
                    >{{ page }}</a>
 | 
			
		||||
                </li>
 | 
			
		||||
            {% endfor %}
 | 
			
		||||
            <li class="page-item{{ ' disabled' if curr_page >= pages else '' }}">
 | 
			
		||||
                <a class="page-link" aria-label="Next"
 | 
			
		||||
                   href="/plugins/ctfd-whale/admin/containers?page={{ curr_page + 1 }}&type={{ request.args.get('type', 'instances') }}"
 | 
			
		||||
                   href="/plugins/ctfd-whale/admin/containers?page={{ curr_page + 1 }}"
 | 
			
		||||
                >
 | 
			
		||||
                    <span aria-hidden="true">»</span>
 | 
			
		||||
                    <span class="sr-only">Next</span>
 | 
			
		||||
@ -58,22 +53,17 @@
 | 
			
		||||
    
 | 
			
		||||
    <li class="nav-item nav-link">
 | 
			
		||||
        {% if session['view_mode'] == 'card' %}
 | 
			
		||||
            <a href="?mode=list&type={{ session.get('page_type', 'containers') }}">Switch to list mode</a>
 | 
			
		||||
            <a href="?mode=list">Switch to list mode</a>
 | 
			
		||||
        {% else %}
 | 
			
		||||
            <a href="?mode=card&type={{ session.get('page_type', 'containers') }}">Switch to card mode</a>
 | 
			
		||||
            <a href="?mode=card">Switch to card mode</a>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
    </li>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block panel %}
 | 
			
		||||
    {% if session.get("page_type", "containers") == "containers" %}
 | 
			
		||||
        {% include "containers/" + session["view_mode"] + ".containers.html" %}
 | 
			
		||||
    {% elif session["page_type"] == "logs" %}
 | 
			
		||||
        {% include "containers/" + session["view_mode"] + ".logs.html" %}
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% include "containers/" + session["view_mode"] + ".containers.html" %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
{% block scripts %}
 | 
			
		||||
    <script defer src="{{ url_for('plugins.ctfd-whale.assets', path='containers.js') }}"></script>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										22
									
								
								utils/db.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								utils/db.py
									
									
									
									
									
								
							@ -2,7 +2,7 @@ import datetime
 | 
			
		||||
 | 
			
		||||
from CTFd.models import db
 | 
			
		||||
from CTFd.utils import get_config
 | 
			
		||||
from ..models import WhaleContainer, WhaleContainerLog, WhaleRedirectTemplate
 | 
			
		||||
from ..models import WhaleContainer,WhaleContainerLog, WhaleRedirectTemplate
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DBContainer:
 | 
			
		||||
@ -16,12 +16,10 @@ class DBContainer:
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def create_container_log(container):
 | 
			
		||||
        container = WhaleContainerLog(container)
 | 
			
		||||
        db.session.add(container)
 | 
			
		||||
        log = WhaleContainerLog(container=container)
 | 
			
		||||
        db.session.add(log)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
 | 
			
		||||
        return container
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_current_containers(user_id):
 | 
			
		||||
        q = db.session.query(WhaleContainer)
 | 
			
		||||
@ -63,14 +61,6 @@ class DBContainer:
 | 
			
		||||
        )
 | 
			
		||||
        return q.all()
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_all_container_logs_page(page_start, page_end):
 | 
			
		||||
        q = db.session.query(WhaleContainerLog)
 | 
			
		||||
        q = q.slice(page_start, page_end)
 | 
			
		||||
 | 
			
		||||
        return q.all()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_all_container():
 | 
			
		||||
        q = db.session.query(WhaleContainer)
 | 
			
		||||
@ -88,12 +78,6 @@ class DBContainer:
 | 
			
		||||
        q = q.slice(page_start, page_end)
 | 
			
		||||
        return q.all()
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_all_container_logs_count():
 | 
			
		||||
        q = db.session.query(WhaleContainerLog)
 | 
			
		||||
 | 
			
		||||
        return q.count()
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_all_alive_container_count():
 | 
			
		||||
        timeout = int(get_config("whale:docker_timeout", "3600"))
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user