o
    j                     @   sR   d dl Z d dlZd dlmZ d dlmZ d dlZdefddZG dd deZ	dS )	    N)List)HTTPServerHandlermsgc                    s   t |  d S )N)print)r    r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asysocks/unicomm/protocol/server/http/httprelay.py	dummy_log   s   r   c                	       s   e Zd Zdddddddedededef fd	d
Zdd Zdd Zdd Z	defddZ
dg fdedee fddZdd Zdd Zdd Zdd Z  ZS )HTTPRelayHandlerFNTLMN)is_proxyauthtypeauthbodysuccessbodylog_callbackr   r   r   r   c          	         s   t  j|i | d| _|| _| | _| | _|dvr"td| |du r3d| _d| _	d| _
d| _nd	| _d
| _	d| _
d| _t|trH| }t|trQ| }|| _|| _|| _d S )N )r
   SPNEGOzUnsupported auth type %sTzProxy Authentication RequiredzProxy-AuthenticatezProxy-Authorizationi  UnauthorizedzWWW-AuthenticateAuthorization  )super__init__
webdav_urlrelay_handlergssapiupperr   	Exceptionunauthorized_reasonauthorization_keyclient_auth_keyunauthorized_status
isinstancestrencoder   r   r   )	selfr   r   r   r   r   r   argskwargs	__class__r   r   r      s.   



zHTTPRelayHandler.__init__c                 C   s8   | j  d}|jD ]}|d |kr|d   S qd S )Nasciir      )r   lowerr"   headers)r#   requestr   headerr   r   r   get_auth_header*   s   
z HTTPRelayHandler.get_auth_headerc                    s<   dd |D }d |}| jd ur| |I d H  d S d S )Nc                 S   s   g | ]}t |qS r   )r!   ).0xr   r   r   
<listcomp>1   s    z*HTTPRelayHandler.debug.<locals>.<listcomp> )joinr   )r#   r$   r   r   r   r   debug0   s   

zHTTPRelayHandler.debugc              
      s   |  dI d H  z[d}|  }|d | jd ur+|d |dtt| jf || j| jf tj	||d}| j
|I d H  | jd urW| j
tj| jdI d H  | j
t I d H  W d S  ty{ } zt  W Y d }~d S d }~ww )NzInitial responser   )
Connectionz
keep-aliveContent-Typez	text/htmlContent-Lengthstatus_coder+   data)r4   basic_headersappendr   r!   lenr   r   h11Response_wrappersendDataEndOfMessager   	traceback	print_exc)r#   r:   r+   reser   r   r   initial_response6   s&   



z!HTTPRelayHandler.initial_response	auth_datac                    sl   |  d| I d H  d}|  }|| j|f tj||d}| j|I d H  | jt I d H  d S )Nzsend_auth_progress %sr   r9   )	r4   r=   r>   r   r@   rA   rB   rC   rE   )r#   rK   r:   r+   rH   r   r   r   send_auth_progressH   s   z#HTTPRelayHandler.send_auth_progressextra_headersc                    s   |  d| I d H  d}|  }|| |d | jd ur,|dtt| jf |d ur8|| j|f tj	||d}| j
|I d H  | jd ur[| j
tj| jdI d H  | j
t I d H  d S )Nzsend_success %s   r6   r8   r9   r;   )r4   r=   extendr>   r   r!   r?   r   r@   rA   rB   rC   rD   rE   )r#   rK   rM   r:   r+   rH   r   r   r   send_successQ   s   



zHTTPRelayHandler.send_successc                    s    g d}| j |dI d H  d S )N))zAccess-Control-Allow-Methodsz8OPTIONS, PROPFIND, GET, PUT, DELETE, MKCOL, LOCK, UNLOCK)zAccess-Control-Allow-Origin*)zAccess-Control-Allow-HeaderszContent-Type, Authorization)rM   )rP   )r#   r,   rM   r   r   r   handle_optionsa   s   zHTTPRelayHandler.handle_optionsc           
   	      s   d}|j  }|dd }d}d}d| j d| d| d	| d
	}|  }|d |dtt|f tj	||d}	| j
|	I d H  | j
tj| dI d H  | j
t I d H  d S )N   /r   z2016-11-12T22:00:22ZzG<?xml version="1.0"?><D:multistatus xmlns:D="DAV:"><D:response><D:href>z-</D:href><D:propstat><D:prop><D:creationdate>z </D:creationdate><D:displayname>z$</D:displayname><D:getcontentlength>ag  </D:getcontentlength><D:getcontenttype></D:getcontenttype><D:getetag></D:getetag><D:getlastmodified>Mon, 20 Mar 2017 00:00:22 GMT</D:getlastmodified><D:resourcetype><D:collection></D:collection></D:resourcetype><D:supportedlock></D:supportedlock><D:ishidden>0</D:ishidden></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>r7   ztext/xmlr8   r9   r;   )targetdecodesplitr   r=   r>   r!   r?   r@   rA   rB   rC   rD   r"   rE   )
r#   r,   r:   request_targetrequest_filegetcontentlengthcreationdatecontentr+   rH   r   r   r   handle_propfindi   s   
 
z HTTPRelayHandler.handle_propfindc                    sN   |   }|d tjd|d}| j|I d H  | jt I d H  d S )NrV   rN   r9   )r=   r>   r@   rA   rB   rC   rE   )r#   r,   r+   rH   r   r   r   handle_headz   s   
zHTTPRelayHandler.handle_headc              
      s  | j  }d }| j|j | j dkr| j }n| j dkr"| j}z|| _| |}| d| I d H  |d u rs|j	dkrI| 
|I d H  W d S |j	dkrY| |I d H  W d S |j	dkri| |I d H  W d S |  I d H  W d S ||du rt|t|d d  }||I d H \}}}|d ur||du r|d ur| d	|t|f I d H  W d S d }	|d urd	|t|f }	| |	I d H  W d S td
|  ty }
 zt  |
d }
~
ww )Nr
   r   zauth_data %ss   OPTIONSs   PROPFINDs   HEADTr)   s   %s %sz#Authdata doesnt seem to be NTLM! %s)r   r"   r   set_connection_infostreamget_ntlmrB   r.   r4   methodrR   r_   r`   rJ   
startswithbase64	b64decoder?   authenticate_relay_serverrL   	b64encoderP   r   rF   rG   )r#   wrapperr,   r   authobjrK   rH   to_continueerrres_datarI   r   r   r   _process_request   sT   






z!HTTPRelayHandler._process_request)__name__
__module____qualname__boolr!   bytesr   r.   r4   rJ   rL   r   tuplerP   rR   r_   r`   ro   __classcell__r   r   r&   r   r	      s    ,		r	   )
rf   rF   typingr   0asysocks.unicomm.protocol.server.http.httpserverr   r@   r!   r   r	   r   r   r   r   <module>   s   