
    Dj                    x    S SK Jr  S SKrS SKrS SKrS SKrS SKJr  \" S5      rSr	S
S jr
SS jrSS jrSS	 jrg)    )annotationsN)
get_loggerzactions.nginxz
/etc/nginxc                $   U (       d  [        S5      e[        R                  R                  U 5      n[        R                  R                  [        5      nUR                  U[        R                  -   5      (       d  X:w  a  [        SU  S35      eU$ )z`Resolve the real path and verify it is strictly within NGINX_BASE_DIR to prevent path traversal.zconfig_path is requiredzAccess denied: z% is outside of Nginx config directory)
ValueErrorospathrealpathNGINX_BASE_DIR
startswithsepPermissionError)config_pathresolved_path
nginx_bases      7D:\Programs\laragon\www\wolf\agent\src\actions\nginx.py_verify_safe_pathr      ss    233GG$$[1M!!.1J##J$788]=X}<abcc    c                    U R                  SS5      n[        U5      n[        R                  R	                  U5      (       d  [        SU 35      e[        R                  R                  U5      nUS:  a  [        S5      e[        USSSS	9 nUR                  5       nS
S
S
5        SWUUS.$ ! , (       d  f       N= f! [         a.  n[        R                  SU5        S[        U5      S.s S
nA$ S
nAff = f)z/Read Nginx configuration file content securely.r    zConfiguration file not found:    z0Configuration file exceeds maximum size of 512KBrutf-8ignore)encodingerrorsNT)successcontentr   
size_byteszNginx config read failed: %sFr   error)getr   r   r   isfileFileNotFoundErrorgetsizer   openread	Exceptionlogr    str)payloadr   r   	file_sizefr   excs          r   handle_nginx_config_readr.      s    
kk-4)+6ww~~m,,#&D[M$RSSGGOOM2	z!OPP-wxHAffhG I #	
 	
 IH  
		0#6X
 	

s6   BB> B-B> -
B;7B> >
C6#C1+C61C6c                r	  ^ SnSnSnSn U R                  SS5      nU R                  S5      nUc  [        S5      e[        U5      n[        UR	                  S5      5      S:  a  [        S	5      e[
        R                  R                  [
        R                  R                  [        S
5      5      [
        R                  R                  [
        R                  R                  [        S5      5      /n[
        R                  R                  U5      m[        U4S jU 5       5      (       d  [        S5      e[
        R                  R                  U5      nU(       a9  U S[        [        R                  " 5       5       3n[         R"                  " X5        U S3n[%        USSS9 n	U	R'                  U5        SSS5        U S3nU(       a  [
        R(                  " X5        [
        R(                  " X5         [*        R,                  " / SQSSSSS9n
U
R.                  S:H  nSU
R0                   SU
R2                   3nU(       aG  U(       a:  [
        R                  R                  U5      (       a  [
        R8                  " U5        SSUS.$ [
        R                  R                  U5      (       a  [
        R8                  " U5        U(       a;  [
        R                  R                  U5      (       a  [
        R(                  " X15        OAU(       a:  [
        R                  R                  U5      (       a  [         R"                  " X!5        SSUS.$ ! , (       d  f       GN= f! [4         a  nSnS[7        U5       3n SnAGNCSnAff = f! [4         Ga#  n[:        R=                  SU5         U(       a  U S3n[
        R                  R                  U5      (       a  [
        R8                  " U5        U S3nU(       at  [
        R                  R                  U5      (       aP  [
        R                  R                  U5      (       a  [
        R8                  " U5        [
        R(                  " X15        O-! [4         a   n[:        R=                  SU5         SnAOSnAff = fS[7        U5      S .s SnA$ SnAff = f)!zVWrite Nginx configuration file content securely with validation, backup, and rollback.NFr   r   r   zcontent is requiredr   r   z3Configuration content exceeds maximum size of 512KBzsites-availablezconf.dc              3  ~   >#    U H3  nTR                  U[        R                  -   5      =(       d    TU:H  v   M5     g 7f)N)r   r   r   ).0d
parent_dirs     r   	<genexpr>,handle_nginx_config_write.<locals>.<genexpr>W   s2     ^Q]A:((RVV4G
aGQ]s   :=z8Directory is not allowed for writing configuration filesz.wolfpanel.bak.z.tmpw)r   z
.orig_temp)nginxz-tz-cz/etc/nginx/nginx.confTcapture_outputtextshellcheckr   zstdout:
z	
stderr:
z%Nginx test command execution failed: z6Nginx configuration written successfully and tested OK)r   messager   nginx_test_failed)r   r    nginx_outputzNginx config write failed: %sz:Nginx rollback failed during general exception handler: %sr   )r!   r   r   lenencoder   r   r	   joinr
   dirnameanyr   existsinttimeshutilcopy2r%   writerename
subprocessrun
returncodestdoutstderrr'   r)   remover(   r    )r*   r   backup_pathorig_temp_pathoriginal_existedr   r   allowed_dirstmp_pathr,   restest_successr?   er-   rollback_excr3   s                   @r   handle_nginx_config_writer[   <   s   MKNc
kk-4++i(?233 *+6 w~~g&'*4RSS GGRWW\\.:KLMGGRWW\\.(CD
 WW__]3
^Q]^^^!"\]] 77>>-8*O?3tyy{;K:LMKLL4 $_D)(C'2aGGG 3 *?*5IIm4
		(*	L..>#C  NNa/L&szzl+cjj\JL BGGNN>$B$B		.)S#  ww~~m,,		-(BGGNN>$B$B		.8!<!<[8 !, , W 32(  	L LB3q6(KL	L:  
		137	b+_D177>>(++IIh'$1?*!=#~(F(Fww~~m44		-0IIn< 	bIIRT`aa	b X
 	
!
s   FN %M7A N 8AM  =AN CN 
MN  
N*N :N  NN R6R1*CQ10R11
R;RR1RR1+R61R6c                    [         R                  " / SQSSSSS9nUR                  S:X  a,  SSUR                  =(       d    UR                  =(       d    SS.$ [         R                  " / SQSSSSS9nS	UR                   S
UR                   SUR                   SUR                   3nSSSUS.$ ! [
         a<  n[        R                  SU5        SS[        U5      S[        U5       3S.s SnA$ SnAff = f)z2Reload Nginx service using systemctl reload nginx.)	systemctlreloadr7   TFr8   r   z$Nginx service reloaded successfully.)r   reloadedoutput)r]   statusr7   zReload failed (code z):
z
Service Status:

reload_failed)r   r_   r    r`   zNginx reload failed: %sz*Exception occurred while reloading Nginx: N)	rL   rM   rN   rO   rP   r'   r(   r    r)   )r*   rW   
status_rescombined_outputr-   s        r   handle_nginx_service_reloadrf      s   '
nn,
 >>Q **\

\6\  $0#J 's~~&6d3::, G$$.$5$5#6b9J9J8KM 
 !!()	   
		+S1XB3s8*M	
 	

s%   AB/ AB/ /
C591C0*C50C5)r   r)   returnr)   )r*   dictrg   rh   )
__future__r   r   rH   rG   rL   loggerr   r(   r
   r   r.   r[   rf    r   r   <module>rl      s>    " 	    ! 
<j
Z)
r   