HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/local/CyberCP/aiScanner/management/commands/__pycache__/run_scheduled_scans.cpython-310.pyc
o

��hYQ�@sLddlmZddlmZddlmZmZddlZddlZGdd�de�ZdS)�)�BaseCommand)�timezone)�datetime�	timedeltaNc@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�CommandzRun scheduled AI security scanscCsD|jdddd�|jdtdd�|jddd	d�|jd
ddd�dS)Nz--daemon�
store_truez8Run as daemon, checking for scheduled scans every minute)�action�helpz	--scan-id�#Run a specific scheduled scan by ID)�typer	z	--verbosez3Show detailed information about all scheduled scansz--forcezCForce run all active scheduled scans immediately, ignoring schedule)�add_argument�int)�self�parser�r�G/usr/local/CyberCP/aiScanner/management/commands/run_scheduled_scans.py�
add_argumentss(���
�zCommand.add_argumentscOs�|�dd�|_|�dd�|_|dr|j�d�|��dS|dr7|j�d|d�d��|�|d�dS|drG|j�d	�|��dS|j�d
�|��dS)N�verboseF�force�daemonz!Starting scheduled scan daemon...�scan_idzRunning scheduled scan ID z...z+Force running all active scheduled scans...z&Checking for scheduled scans to run...)	�getrr�stdout�write�
run_daemon�run_scheduled_scan_by_id�force_run_all_scans�check_and_run_scans)r�args�optionsrrr�handle!szCommand.handlec
Cs�	z|j�dt���d��d��|��t�d�WnAty+|j�d�YdSt	y\}z&|j
�dt|����dd	lm
}|�d
t|����t�d�WYd}~nd}~wwq)z.Run as daemon, checking for scans every minuteTz
[�%Y-%m-%d %H:%M:%S UTCz!] Checking for scheduled scans...�<z
Daemon stopped by userzError in daemon: r��CyberCPLogFileWriterz[Scheduled Scan Daemon] Error: N)rrr�now�strftimer�time�sleep�KeyboardInterrupt�	Exception�stderr�str�plogical.CyberCPLogFileWriterr$�writeToFile)r�e�loggingrrrr2s ���zCommand.run_daemoncCs�ddlm}ddlm}|jjdd�}|��dkr&|j�d�|�	d�dS|j�d	|���d
��|�	d|���d
��|D]$}|j�d|j
�d
|j�d��|�	d|j
�d
|j�d��|�|�q?dS)z0Force run all active scheduled scans immediatelyr��
ScheduledScanr#�active)�statuszNo active scheduled scans foundz6[Scheduled Scan Force] No active scheduled scans foundN�Found z$ active scheduled scans to force runz[Scheduled Scan Force] Found zForce running scheduled scan: � (ID: �)z5[Scheduled Scan Force] Force running scheduled scan: )
�aiScanner.modelsr2r-r$�objects�filter�countrrr.�name�id�execute_scheduled_scan)rr2r0�active_scans�scanrrrrBs
�zCommand.force_run_all_scanscCs�ddlm}ddlm}t��}|j��}|j�	d|�
����|�d|�
����|D�]`}|jr�|j�	d|j
�d|j�d��|j�	d	|jj���|j�	d
|j���|j�	d|j���|j�	d|j���|j�	d
d�|j����|j�	d|j�d����|jr�|j�	d|j�d����n|j�	d�|jdkr�d|j
�d|j�d|j�d�}|j�	d|���|�d|���q,|jdu�r%d|j
�d|j�d�}|j�	d|���|�d|���|j�r$|j�	d�z|��|_|��|j�	d|j�d����Wnt�y#}z|j�	dt|����WYd}~nd}~wwq,|j|k�r�|j|}t|��d�}	t|��dd �}
t|��d d!�}d"}|	dk�r_|	�d#|
�d$|�d%�}n|
�d$|�d%�}d|j
�d|j�d&|�d'|j�d���}|j�	d(|���|�d|���q,q,|jj d|d)�}
|
�
�dk�r�|j�	d*�|�d+�n|j�	d,|
�
��d-��|�d.|
�
��d-��|
D]%}|j�	d/|j
�d|j�d��|�d0|j
�d|j�d��|�!|��q�dS)1z*Check for scheduled scans that need to runrr1r#zTotal scheduled scans: z.[Scheduled Scan Check] Total scheduled scans: z
--- Scan Details: r6z) ---z	  Owner: z
  Frequency: z
  Scan Type: z
  Status: z  Domains: �, z  Created: r!z  Last Run: z  Last Run: Neverr3zScan "z" (ID: z) is not active (status: r7u  ❌ z[Scheduled Scan Check] Nz) has no next_run scheduledu/  🔧 Attempting to calculate next run time...u  ✅ Next run set to: u$  ❌ Failed to calculate next run: i�Qir"�zd zh �mz) scheduled to run in z at u  ⏰ )r4�
next_run__ltez.No scheduled scans are due to run at this timezE[Scheduled Scan Check] No scheduled scans are due to run at this timer5z scans due to runz[Scheduled Scan Check] Found �Running scheduled scan: z/[Scheduled Scan Check] Running scheduled scan: )"r8r2r-r$rr%r9�allrrr;r.rr<r=�admin�userName�	frequency�	scan_typer4�join�domain_list�
created_atr&�last_run�next_run�calculate_next_run�saver*r,r
�
total_secondsr:r>)rr2r0r%�	all_scansr@�reasonr/�time_until_run�days�hours�minutes�time_str�	due_scansrrrrWs�



"��

(���zCommand.check_and_run_scanscCsjddlm}z|jj|d�}|j�d|j���|�|�WdS|jy4|j	�d|�d��YdSw)r
rr1)r=rEzScheduled scan with ID z
 not foundN)
r8r2r9rrrr<r>�DoesNotExistr+)rrr2r@rrrr�s�z Command.run_scheduled_scan_by_idcCs�ddlm}m}ddlm}ddlm}ddlm}ddl	m
}|jj|dt
��d�}�zht
��|_|��|_|��g}	|j}
|jD]'}z|jj||
d	�}|	�|�WqC|jyj|�d
|�d|
j���YqCw|	s�d|_d
|_t
��|_|��|j�d|j���WdS|� |	�t!|	�|_"|��|�}
g}d}d}d}|	D]�}z�|j#�d|���Gdd�d�}||
j$||j%�}|
�&||
j$�}t'|d�r�ddl(}|�)|j*�+d��}n|}|�d��r|�d�}|�r
|�|�|d7}d|vr�|t,|d�7}|�d|�d|���n"|d7}|�d|���n|d7}|�dd�}|�d|�d |���Wn&t-�yS}z|d7}t.|�}|�d!|�d |���WYd}~nd}~wwt/�0d"�q�||_1||_2||_3|�4|�|dk�rod#nd#|_t
��|_|��|j5�r�|�6||�|j#�d$|�d%|�d&��WdSt-�y�}zSd|_t.|�|_t
��|_|��|�d'|j�d t.|����|j�d(|j�d t.|����|j5�r�|j7�r�|�8|t.|��WYd}~dSWYd}~dSWYd}~dSd}~ww))zExecute a scheduled scanr)�ScheduledScanExecution�ScanHistory)�AIScannerManager)�
Administrator)�Websitesr#�running)�scheduled_scanr4�
started_at)�domainrGz[Scheduled Scan] Domain z no longer accessible for user �failedz(No accessible domains found for scanningz)No accessible domains for scheduled scan NgzStarting scan for domain: c@seZdZdd�Zdd�ZdS)z3Command.execute_scheduled_scan.<locals>.FakeRequestcSs@d|i|_d|_||d�|_ddl}|�||d���d�|_dS)N�userID�POST)rdrJr�utf-8)�session�methodrg�json�dumps�encode�body)r�admin_idrdrJrkrrr�__init__�s
���z<Command.execute_scheduled_scan.<locals>.FakeRequest.__init__cSs.zddlm}|��}|�d�WSYdS)Nr)�
ACLManagerz:8090zlocalhost:8090)�plogical.aclrq�fetchIP)rrq�	server_iprrr�get_host�sz<Command.execute_scheduled_scan.<locals>.FakeRequest.get_hostN)�__name__�
__module__�__qualname__rprurrrr�FakeRequest�sry�contentrh�successr��
cost_estimatez+[Scheduled Scan] Successfully started scan z for z+[Scheduled Scan] Failed to get scan ID for �errorz
Unknown errorz*[Scheduled Scan] Failed to start scan for z: z-[Scheduled Scan] Exception starting scan for ��	completedzScheduled scan completed: z
 successful, z failedz2[Scheduled Scan] Failed to execute scheduled scan z!Failed to execute scheduled scan )9r8r\r]�aiScanner.aiScannerManagerr^�loginSystem.modelsr_�websiteFunctions.modelsr`r-r$r9�createrr%rNrPrOrQrGrLr�appendr[r.rHr4�
error_message�completed_atr+rr<�set_scanned_domains�len�total_scansr�pkrJ�	startScan�hasattrrk�loadsrz�decode�floatr*r,r'r(�successful_scans�failed_scans�
total_cost�set_scan_ids�email_notifications�send_notifications�notify_on_failure�send_failure_notification)rrbr\r]r^r_r`r0�	execution�domains_to_scanrGrd�website�sm�scan_idsr�r�r�ry�fake_request�resultrk�
response_datar�	error_msgr/rrrr>�s��


�





�"��


�

���zCommand.execute_scheduled_scanc
Cs�z1d}|jdkr|jrd}n|jdkr$|jrd}n|jr$|jdkr$|j}|r/|�||�WdSWdStyT}zddlm}|�	dt
|����WYd}~dSd}~ww)	z+Send email notifications for completed scanFreTr�rr#z.[Scheduled Scan] Failed to send notification: N)r4r��notify_on_completion�notify_on_threatsr��send_execution_notificationr*r-r$r.r,)rrbr��
should_notifyr/r0rrrr�Ns"
�"��zCommand.send_notificationscCsxz�|j}|s|jjr|jjgng}|sWdSd|j�d�}|j��}|jdkr6|jdkr/d}nd|j�d�}d	|j�d
|�d|j�d��d
|j	�d|j
�d|j�d|jd�dd�|j
��d|jrfd|j��nd�dd�|j��d�}ddlm}d}|�|||�ddlm}	|	�d|j�dt|��d��WdSty�}
zddlm}	|	�dt|
����WYd}
~
dSd}
~
ww) z*Send notification email for scan executionN�AI Scanner: Scheduled Scan "z" Completedr�rzCompleted SuccessfullyzCompleted with z	 failuresz/
Scheduled AI Security Scan Report

Scan Name: z	
Status: z
Execution Time: r!z

Results:
- Total Domains: z
- Successful Scans: z
- Failed Scans: z
- Total Cost: $z.4fz

Domains Scanned: rAz

zError Message: rBz

Scan IDs: zA

View detailed results in your CyberPanel AI Scanner dashboard.
��
mailUtilities�noreply@cyberpanel.localr#z'[Scheduled Scan] Notification sent for z to z recipientsz4[Scheduled Scan] Failed to send notification email: )�notification_email_listrG�emailr<r4�titler��execution_timer&r�r�r�rK�scanned_domainsr��scan_id_list�plogical.mailUtilitiesr��	SendEmailr-r$r.r�r*r,)rrbr��notification_emails�subject�status_text�messager��senderr0r/rrrr�fsV


��
��	�
��

��
�$"��z#Command.send_execution_notificationc

Cs�zO|j}|s|jjr|jjgng}|sWdSd|j�d�}d|j�dt���d��d|�d�}d	d
lm}d}|�	|||�d	dl
m}|�d
|j���WdSt
yr}	zd	dl
m}|�dt|	����WYd}	~	dSd}	~	ww)z(Send notification email for scan failureNr�z" Failedz0
Scheduled AI Security Scan Failure

Scan Name: z
Status: Failed
Time: r!z	

Error: zG

Please check your CyberPanel AI Scanner configuration and try again.
rr�r�r#z/[Scheduled Scan] Failure notification sent for z<[Scheduled Scan] Failed to send failure notification email: )r�rGr�r<rr%r&r�r�r�r-r$r.r*r,)
rrbr�r�r�r�r�r�r0r/rrrr��s0���
"��z!Command.send_failure_notificationN)rvrwrxr	rr rrrrr>r�r�r�rrrrrsR9r)	�django.core.management.baser�django.utilsrrrrkr'rrrrr�<module>s