§
     ¯½js  ã                  ó˜   — d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZ ddlmZmZmZ  G d„ de¦  «        Z G d„ d	¦  «        ZdS )
a“  
FreeExile Anti-Fog Memory Tampering & Anti-Exploit Verifier Engine.
Implements:
1. Fog Hash Challenge-Response (Cryptographic HMAC-SHA256 integrity verification).
2. Detection of Client RAM tampering (hacker forcing fog_matrix to VISIBLE).
3. Server-Authoritative Action Line-of-Sight (LoS) & Wall-Hack Sanity Validation.
4. Independent Security Division Integration (Automated Flagging & Quarantine).
é    )ÚannotationsN)ÚTupleÚDictÚAnyÚOptional)ÚFogStateÚMapGridDataÚPlayerVisibilityStatec                  ó   — e Zd ZdZdS )ÚFogTamperViolationExceptionzDRaised when client fog memory tampering is cryptographically proven.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    ú?C:\Projects\FreeExile\server\security\fog_integrity_verifier.pyr   r      s   € € € € € ØNÐNØ€Dr   r   c                  óL   — e Zd ZdZd"d#d„Zd$d	„Zd%d„Z	 d&d'd„Z	 d&d(d„Zd)d!„Z	dS )*ÚFogIntegrityVerifierzFServer-Authoritative Anti-Maphack and Memory Tamper Verification Core.ç      ,@Úmax_allowed_action_distanceÚfloatc                ó"   — || _         i | _        d S ©N)r   Úactive_challenges)Úselfr   s     r   Ú__init__zFogIntegrityVerifier.__init__    s   € Ø+FˆÔ(Ø13ˆÔÐÐr   Ú	player_idÚstrÚreturnc                óÄ   — t          j        d¦  «        }t          | j        ¦  «        dk    r)t	          t          | j        ¦  «        ¦  «        }| j        |= || j        |<   |S )zDGenerates a cryptographically random challenge nonce for the player.é   iPÃ  )ÚsecretsÚ	token_hexÚlenr   ÚnextÚiter)r   r   ÚnonceÚ
oldest_keys       r   Úgenerate_fog_challengez+FogIntegrityVerifier.generate_fog_challenge$   s]   € åÔ! "Ñ%Ô%ˆåˆtÔ%Ñ&Ô&¨%Ò/Ð/Ý�d 4Ô#9Ñ:Ô:Ñ;Ô;ˆJØÔ& zÐ2Ø,1ˆÔ˜yÑ)Øˆr   Ú	vis_stater
   r(   c                óÈ   — t          d„ |j        D ¦   «         ¦  «        }t          j        |                     d¦  «        |t
          j        ¦  «                             ¦   «         S )zRComputes the ground-truth HMAC-SHA256 hash of the authoritative server fog matrix.c                ó   — g | ]	}|D ]}|‘ŒŒ
S r   r   )Ú.0ÚrowÚcells      r   ú
<listcomp>zBFogIntegrityVerifier.compute_expected_fog_hash.<locals>.<listcomp>0   s%   € ÐQÐQÐQ SÈSÐQÐQÀT˜DÐQÐQÐQÐQr   zutf-8)ÚbytesÚ
fog_matrixÚhmacÚnewÚencodeÚhashlibÚsha256Ú	hexdigest)r   r+   r(   Ú
flat_bytess       r   Úcompute_expected_fog_hashz.FogIntegrityVerifier.compute_expected_fog_hash.   sO   € åÐQÐQ¨IÔ,@ÐQÑQÔQÑRÔRˆ
ÝŒx˜Ÿš WÑ-Ô-¨z½7¼>ÑJÔJ×TÒTÑVÔVÐVr   NÚclient_hashúOptional[str]Úboolc                óÀ   — | j                              |j        d¦  «        }|p|}|sdS |r
|r||k    rdS |                      ||¦  «        }t	          j        ||¦  «        S )a-  
        Cryptographically verifies whether client-submitted fog hash matches server reality.
        Consumes the active challenge nonce for the player to prevent replay attacks and leaks.
        Returns False immediately if hacker modified RAM (e.g. changing 0 to 2) or replayed old nonce.
        NF)r   Úpopr   r;   r4   Úcompare_digest)r   r+   r<   r(   Úactive_nonceÚeffective_nonceÚexpected_hashs          r   Úverify_client_fog_hashz+FogIntegrityVerifier.verify_client_fog_hash3   s|   € ð Ô-×1Ò1°)Ô2EÀtÑLÔLˆØÐ/ <ˆØð 	Ø�5àð 	�\ð 	 e¨|Ò&;Ð&;Ø�5à×6Ò6°yÀ/ÑRÔRˆÝÔ" =°+Ñ>Ô>Ð>r   Úmap_datar	   Ú
player_posúTuple[int, int]Ú
target_posÚ	max_rangeúOptional[float]úTuple[bool, str]c                óÈ  — |\  }}|\  }}	|p| j         }
|                     ||	¦  «        r|                     ||¦  «        sdS |                     ||	¦  «        rdS |j        |	         |         t          j        j        k    rdd|j        |	         |         › d�fS t          j        ||z
  |	|z
  ¦  «        }||
k    rdd|d›d|
d›d�fS |  	                    |||||	¦  «        sd	S d
S )z—
        Server-side sanity check for targeting skills, attacks, or chest interactions.
        Defeats wall-hacks and blind fog exploitation.
        )FÚTARGET_OUT_OF_BOUNDS)FÚTARGET_IS_OBSTACLE_WALLFz!TARGET_NOT_VISIBLE_IN_FOG (state=ú)zTARGET_OUT_OF_RANGE (dist=z.1fz > max=)FÚLINE_OF_SIGHT_OCCLUDED_BY_WALL)TÚIN_LINE_OF_SIGHT)
r   Úis_in_boundsÚblocks_visionr3   r   ÚVISIBLEÚvalueÚmathÚhypotÚ_check_direct_line_of_sight)r   r+   rF   rG   rI   rJ   ÚpxÚpyÚtxÚtyÚallowed_distÚdists               r   Úvalidate_player_action_losz/FogIntegrityVerifier.validate_player_action_losF   s7  € ð ‰ˆˆBØ‰ˆˆBØ ÐD DÔ$Dˆð ×$Ò$ R¨Ñ,Ô,ð 	3°H×4IÒ4IÈ"ÈbÑ4QÔ4Qð 	3Ø2Ð2ð ×!Ò! " bÑ)Ô)ð 	6Ø5Ð5ð Ô Ô# BÔ'­8Ô+;Ô+AÒAÐAØÐ^¸yÔ?SÐTVÔ?WÐXZÔ?[Ð^Ð^Ð^Ð_Ð_õ Œz˜"˜r™' 2¨¡7Ñ+Ô+ˆØ�,ÒÐØÐ\¸Ð\Ð\Ð\ÈÐ\Ð\Ð\Ð\Ð]Ð]ð ×/Ò/°¸"¸bÀ"ÀbÑIÔIð 	=Ø<Ð<à)Ð)r   Úx0ÚintÚy0Úx1Úy1c                óR  — t          ||z
  ¦  «        }t          ||z
  ¦  «        }||k     rdnd}||k     rdnd}	||z
  }
||}}	 ||k    s||k    r$||k    s||k    r|                     ||¦  «        rdS ||k    r||k    rn'd|
z  }|| k    r
|
|z  }
||z  }||k     r
|
|z  }
||	z  }ŒddS )zJBresenham raycast between two points to verify unobstructed line of sight.é   éÿÿÿÿTFé   )ÚabsrT   )r   rF   ra   rc   rd   re   ÚdxÚdyÚsxÚsyÚerrÚcurr_xÚcurr_yÚe2s                 r   rY   z0FogIntegrityVerifier._check_direct_line_of_sightn   sú   € õ ��b‘‰\Œ\ˆÝ��b‘‰\Œ\ˆØ�r’'�'ˆQˆQ˜rˆØ�r’'�'ˆQˆQ˜rˆØ�2‰gˆà˜R�ˆð	à˜"’� ¨"¢ °6¸R²<°<À6ÈRÂ<À<Ø×)Ò)¨&°&Ñ9Ô9ð !Ø ˜5à˜Š|ˆ| ¨"¢ Øà�S‘ˆBØ�R�CŠxˆxØ�r‘	�Ø˜"‘�Ø�BŠwˆwØ�r‘	�Ø˜"‘�ð	ð" ˆtr   )r   )r   r   )r   r   r    r   )r+   r
   r(   r   r    r   r   )r+   r
   r<   r   r(   r=   r    r>   )r+   r
   rF   r	   rG   rH   rI   rH   rJ   rK   r    rL   )rF   r	   ra   rb   rc   rb   rd   rb   re   rb   r    r>   )
r   r   r   r   r   r*   r;   rE   r`   rY   r   r   r   r   r      s²   € € € € € ØPÐPð4ð 4ð 4ð 4ð 4ðð ð ð ðWð Wð Wð Wð Z^ð?ð ?ð ?ð ?ð ?ð2 &*ð&*ð &*ð &*ð &*ð &*ðPð ð ð ð ð r   r   )r   Ú
__future__r   r4   r7   r#   rW   Útypingr   r   r   r   Úserver.world.map_data_typesr   r	   r
   Ú	Exceptionr   r   r   r   r   ú<module>rw      s  ððð ð #Ð "Ð "Ð "Ð "Ð "Ø €€€Ø €€€Ø €€€Ø €€€Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -ðð ð ð ð ð ð ð ð ð ð	ð 	ð 	ð 	ð 	 )ñ 	ô 	ð 	ð
mð mð mð mð mñ mô mð mð mð mr   