
    sj3                        d dl Z d dlZd dlZd dlZd dlZd dlmZ  eej                  j                  dd            Z	 eej                  j                  dd            Z
 ej                  e      Zej                  ej                          G d d      Z G d	 d
      Z G d d      Z G d d      Zd Z G d d      ZddZedk(  r e        yy)    N)webWSLINK_MAX_MSG_SIZE4194304WSLINK_HEART_BEAT30c                   B    e Zd Zd	dZd Zed        Zd Zd Zd Z	d Z
y)
WsClientConnectionc                     d | _         d | _        d | _        d| _        d | _        t        j                         j                         | _        || _	        y Nr   )
_url_session_ws
_connected_destinationasyncioget_running_loopcreate_future_readypropagate_disconnectselfr   s     J/DATA/.local/lib/python3.12/site-packages/wslink/backends/aiohttp/relay.py__init__zWsClientConnection.__init__   sG    	 ..0>>@$8!    c                     || _         y Nr   r   values     r   bindzWsClientConnection.bind"   
    !r   c                     | j                   S r   )r   r   s    r   readyzWsClientConnection.ready%   s    {{r   c                   K   t         j                  d|       || _        | j                  t	        j
                         4 d {   }t         j                  d       || _        	 |j                  | j                        4 d {   }t         j                  d       || _        | xj                  dz  c_        | j                  j                  d       |2 3 d {   }t         j                  d       | j                  dk  rt         j                  d        n[| j                  r9t         j                  d       | j                  j                  |       d {    t         j                  d	       | j                          | j                  j                  s"| j                  j!                          d {    d | _        d | _        d d d       d {    | j                  j                  d
       d d d       d {    t         j                  d       y 7 7 7 H7 6 7 p7 T# 1 d {  7  sw Y   dxY w# | j                  j                  d
       w xY w7 `# 1 d {  7  sw Y   pxY ww)Nzclient::connect::%szclient::connect::sessionzclient::connect::ws   Tzclient::connect::ws::msgz$client::connect::ws::msg::disconnectzclient::connect::ws::msg::sendz%ws-client: No destination for messageFzclient::connect::exit)loggerdebugr   r   aiohttpClientSession
ws_connectr   r   r   
set_resultr   senderror
disconnectclosedclose)r   urlsessionwsmsgs        r   connectzWsClientConnection.connect)   s    *C0	== ,,..'78 '2&11$))<<%:;#%1,..t4)+ 
V#"LL)CD#2 &-S T %#00 &-M N&*&7&7&<&<S&A A A &-T U )  $xx"&((.."222#'(,1  =<4 KK**51= /.@ 	,-A /  =

V !B *," 3-  =<<<4 KK**51= /...s   AI<HI<	I''IH"I
AH0H*H%
H*!A;H0H(
AH0;H,<H0IH.II'9I<I%I<"I%H*(H0*H0,H0.I0I	6H97I	>II""I'%I<'I9-I0.I95I<c                   K   | j                   dkD  r| j                  j                  st        j	                  d       |j
                  t        j                  j                  k(  r.| j                  j                  |j                         d {    y |j
                  t        j                  j                  k(  r.| j                  j                  |j                         d {    y |j
                  t        j                  j                  k(  r.| j                  j                  |j                         d {    y |j
                  t        j                  j                  k(  r.| j                  j!                  |j                         d {    y |j
                  t        j                  j"                  k(  r#| j                  j%                          d {    y t        j'                  d       y t        j'                  d       t        j'                  d| j                   | j                  j                         t        j'                  d       y 7 7 s7 !7 7 w)Nr   zclient::sendInvalid message to forwardzclient::send - NO SEND%s - %s<------------------------------------------------------------r   r   r0   r'   r(   typer)   	WSMsgTypeTEXTsend_strdataBINARY
send_bytesPINGpingPONGpongCLOSEr1   r.   r   r5   s     r   r-   zWsClientConnection.sendO   w    ??QtxxLL(xx7,,111hh''111W..555hh))#((333W..333hhmmCHH---W..333hhmmCHH---W..444hhnn&&&9:LL12LLDOOTXX__ELL" 23--&_   BI IAI #I$AI 8I9AI IA	I IA<I I I I I c                     t         j                  d| j                         | j                  dkD  r<d| _        | j                  r(| j                  r| j                  j                          y y y y )Nzclient::disconnect %sr   r'   r(   r   r   r   r/   r#   s    r   r/   zWsClientConnection.disconnectc   X    ,doo>??Q DO  T%>%>!!,,. &?  r   c                 n   K   | j                   #| j                   j                          d {    y y 7 wr   r   r1   r#   s    r   r1   zWsClientConnection.closej   ,     88((.."""  "   *535NT)__name__
__module____qualname__r   r    propertyr$   r6   r-   r/   r1    r   r   r	   r	      s5    9"  $.L#(/#r   r	   c                   2    e Zd ZddZd Zd Zd Zd Zd Zy)	WsServerConnectionc                 <    d | _         d | _        d| _        || _        y r   )r   r   r   r   r   s     r   r   zWsServerConnection.__init__s   s      $8!r   c                     || _         y r   r   r   s     r   r    zWsServerConnection.bindy   r!   r   c                 l  K   t         j                  d       t        j                  t        t
              | _        | j                  j                  |       d {    t         j                  d       | xj                  dz  c_        | j                  dkD  r| j                  2 3 d {   }t         j                  d       | j                  dk  r np| j                  rNt         j                  d       | j                  j                  |       d {    t         j                  d       t         j                  d	       | j                          | j                  j                  s"| j                  j                          d {    d | _        t         j                  d
       y 7 N7 7 6 o7 +w)Nzserver::connect)max_msg_size	heartbeatzserver::connect::preparer&   r   zserver::connect::ws::msgz$server::connect::ws::msg::send-beginz"server::connect::ws::msg::send-endz%ws-server: No destination for messagezserver::connect::exit)r'   r(   r   WebSocketResponseMAX_MSG_SIZE
HEART_BEATr   preparer   r   r-   r.   r/   r0   r1   )r   requestr5   s      r   r6   zWsServerConnection.connect|   s?    &'((%
 hhw'''/01??Q!XX 
Jc78??Q&$$LL!GH++00555LL!EFLL!HI 	 xx((..""",-3 	(

J 6 &" #s]   AF4F(A	F4$F0(F+)F0,A&F4F.A3F4F2"F4+F0.F40F42F4c                   K   | j                   dkD  r| j                  j                  st        j	                  d       |j
                  t        j                  j                  k(  r.| j                  j                  |j                         d {    y |j
                  t        j                  j                  k(  r.| j                  j                  |j                         d {    y |j
                  t        j                  j                  k(  r.| j                  j                  |j                         d {    y |j
                  t        j                  j                  k(  r.| j                  j!                  |j                         d {    y |j
                  t        j                  j"                  k(  r#| j                  j%                          d {    y t        j'                  d       y t        j'                  d       t        j'                  d| j                   | j                  j                         t        j'                  d       y 7 7 s7 !7 7 w)Nr   zserver::sendr8   zserver::send - NO SENDr9   r:   r;   rH   s     r   r-   zWsServerConnection.send   rI   rJ   c                     t         j                  d| j                         | j                  dkD  r<d| _        | j                  r(| j                  r| j                  j                          y y y y )Nzserver::disconnect %sr   rL   rM   r#   s    r   r/   zWsServerConnection.disconnect   rN   r   c                 n   K   | j                   #| j                   j                          d {    y y 7 wr   rP   r#   s    r   r1   zWsServerConnection.close   rQ   rR   NrS   )	rT   rU   rV   r   r    r6   r-   r/   r1   rX   r   r   rZ   rZ   r   s!    9".@#(/#r   rZ   c                       e Zd Zd Zd Zd Zy)ForwardConnectionc                     || _         || _        t               | _        t	               | _        | j
                  j                  | j                         | j                  j                  | j
                         y r   )_reqr   r	   
_ws_clientrZ   
_ws_serverr    )r   rd   r2   s      r   r   zForwardConnection.__init__   sP    		,.,.T__-T__-r   c                 D   K   t        j                   j                  j                   j                              }|j                   fd        j                  j                   d {     j                  j                   j                         d {    y 7 27 w)Nc                  8    j                   j                         S r   )rm   r/   )_r   s    r   <lambda>z+ForwardConnection.connect.<locals>.<lambda>   s    $//*D*D*Fr   )	r   create_taskrl   r6   r   add_done_callbackr$   rm   rk   )r   tasks   ` r   r6   zForwardConnection.connect   sq     ""4??#:#:499#EFFGoo####oo%%dii000 	$0s$   A&B )B*,B BB B c                 l    | j                   j                          | j                  j                          y r   )rl   r/   rm   r#   s    r   r/   zForwardConnection.disconnect   s"    ""$""$r   N)rT   rU   rV   r   r6   r/   rX   r   r   ri   ri      s    .1%r   ri   c                       e Zd Zd Zd Zd Zy)SinkConnectionc                 <    || _         d | _        d | _        d | _        y r   )_process_req_client_req_process_ws
_client_wsr   rd   s     r   r   zSinkConnection.__init__   s!    #r   c                 ^    | j                   |k(  ry| j                  |k(  ry| j                  d u S )NT)ry   rz   r|   r}   s     r   
can_handlezSinkConnection.can_handle   s3    'w&$&&r   c                   K   | j                   |k(  rat               | _        | j                  j                  |       d {    | j                  "| j                  j                          d {    y| j                  || _        t        d      | _        | j                  j                  | j                         | j                  j                  | j                         | j                  j                  |       d {    | j                  j                  d        | j                  j                  d        d | _        d | _        y7 
7 7 Nw)NTF)r   )ry   rZ   r{   r6   r|   r1   rz   r    r}   s     r   r6   zSinkConnection.connect   s    '13D""**7333*oo++---#&D0eLDOOO  !1!12!!$//2//))'222OO  &!!$'#D"DO' 4- 3s6   =EE	 -E-E.BE?E A
EEEN)rT   rU   rV   r   r   r6   rX   r   r   rw   rw      s    'r   rw   c                    K   | j                   r"t        j                  d| j                          S t        j                  d      S w)Nzindex.html?z
index.html)query_stringr   	HTTPFound)rd   s    r   _root_handlerr     s;     }}{7+?+?*@ABB==&&s   AAc                   &    e Zd Zd ZddZd Zd Zy)	WsHandlerc                      i | _         i | _        y r   )_forward_map
_relay_mapr#   s    r   r   zWsHandler.__init__  s    r   c                     t         j                  d|       |dk(  r| j                  S |dk(  r| j                  S t         j	                  d       y )Nzget_handler %sforwardrelayzNo handler !!!)r'   infoforward_connectrelay_connectr.   )r   modes     r   get_handlerzWsHandler.get_handler  sH    $d+9'''7?%%%%&r   c                   K   |j                   j                  dd      }t        |j                   j                  dd            }|j                   j                  dd      }d| d| d	| }t        j	                  d
|       || j
                  v rt        j                         t        ||      }|| j
                  |<   |j                          d {    | j
                  j                  |       y 7  w)Nhost	localhostport1234pathr4   zws://:/z=> %s)
match_infogetintr'   r   r   r   HTTPForbiddenri   r6   pop)r   rd   r   r   r   
target_url	forwarders          r   r   zWsHandler.forward_connect  s     !!%%fk:7%%))&&9:!!%%fd3TF!D64&1
GZ(***##%%%gz:	(1*%!!!j) 	"s   CC4C2!C4c                 J  K   |j                   }|| j                  vrt        |      }|| j                  |<   | j                  |   }|j                  |      st	        j
                         |j                  |       d {   r| j                  j                  |       y y 7 !wr   )r   r   rw   r   r   r   r6   r   )r   rd   idhandlers       r   r   zWsHandler.relay_connect0  s     \\T__$$W-G")DOOB//"%!!'*##%%)))OO# *s   A=B#?B! "B#N)r   )rT   rU   rV   r   r   r   r   rX   r   r   r   r     s    *&$r   r   c                 &   t               }t        j                  dt        j                        }|j	                  dt
        ddd       |j	                  dd	t        d
dd       |j	                  dt
        dd       |j	                  dt
        dd       |j	                  dt
        ddd       |j                         \  }}| |j                  } ||j                  }||j                  }||j                  }||j                  }g }	|Jt        j                  d|       |	j                  t!        j"                  ||j%                  |                   |dt        j                  d|       |	j                  t!        j"                  dt&                     |	j                  t!        j(                  d|             t        j                  d| |       t!        j*                         }
|
j-                  |	       t!        j.                  |
| |       y )Nz+Start ws relay with static content delivery)descriptionformatter_classz--hostr   z@the interface for the web-server to listen on (default: 0.0.0.0)r   )r<   defaulthelpdestz-pz--porti  z;port number for the web-server to listen on (default: 8080)r   z--moder   z0Working mode [forward, relay] (default: forward))r<   r   r   z--wwwzDirectory to servewww_path)r<   r   r   z--proxy-routezeProxy URL pattern (default: /proxy/{port}) mode::forward(ws://{host=localhost}:{port=1234}/{path=ws})z/proxy/{port}proxy_route)r<   r   r   r   zProxy route: %szWWW: %sr   zStarting relay server: %s %s)r   r   )r   argparseArgumentParserRawDescriptionHelpFormatteradd_argumentstrr   parse_known_argsr   r   r   r   r   r'   r   appendr   r   r   r   staticApplication
add_routesrun_app)r   r   r   r   r   wsRelayparserargsrp   routesweb_apps              r   mainr   E  s   kG $$A <<F O   J   ?	   c0D:V
t   %%'GD!|yy|yy|yy==&& F %{3cggk7+>+>t+DEF Ix(cggc=12cjjh/0 KK.d;ooGvKKd.r   __main__)NNNNN)r   r   loggingosr)   r   r   environr   ra   rb   	getLoggerrT   r'   setLevelINFOr	   rZ   ri   rw   r   r   r   rX   r   r   <module>r      s       	   2::>>"7CD 3T:;
			8	$  T# T#tG# G#Z% %(' '^'2$ 2$tH/^ zF r   