VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /usr/share/emscripten/tests/sockets/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/emscripten/tests/sockets/socket_relay.py
'''
Listens on 2 ports and relays between them.

Listens to ports A and B. When someone connects to port A, and then
sends some data to port A, that data is sent to someone who
connected to socket B. And so forth.

This is different than say socat which will listen to one port
and then make a connection to another port, and do bidirectional
communication. We need to actually listen on both ports.
'''

import os, sys, socket, time, threading, signal
from subprocess import Popen, PIPE, STDOUT

ports = [int(sys.argv[1]), int(sys.argv[2])]

class Listener(threading.Thread):
  def run(self):
    self.conn = None
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    global ports
    port = ports[0]
    ports = ports[1:]
    print 'listener binding to ', port
    s.bind(('127.0.0.1', port))
    s.listen(1)
    print 'listener', port, 'waiting for connection'
    conn, addr = s.accept()
    self.conn = conn
    while 1:
      time.sleep(0.5)
      print 'listener', port, 'waiting for data'
      data = conn.recv(20*1024)
      if not data:
        continue
      while not self.other.conn:
        print 'listener', port, 'waiting for other connection in order to send data'
        time.sleep(1)
      print 'listener', port, 'sending data', len(data)
      self.other.conn.send(data)

in_listener = Listener()
in_listener.daemon = True
in_listener.start()

out_listener = Listener()
out_listener.daemon = True
out_listener.start()

in_listener.other = out_listener
out_listener.other = in_listener

while 1:
  time.sleep(1)


VaKeR 2022