예제 #1
0
파일: app.py 프로젝트: mrcmrc/heroku_proxy
 def got_ip(self, addrinfo):
     try:
         sockaddr = addrinfo[0][0]
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
         self.right = iostream.IOStream(sock)
         self.right.set_close_callback(self.left.close)
         self.left.set_close_callback(self.right.close)
         self.right.write = debug(self.right.write)
         self.right.connect(af, self.get_header)
     except:
         traceback.print_exc()
         self.left.write(not_found_response, self.left.close)
         self.right.close()
         try:
             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
             self.right = iostream.IOStream(sock)
             self.right.set_close_callback(self.left.close)
             self.left.set_close_callback(self.right.close)
             self.right.write = debug(self.right.write)
             print host
             self.right.connect((socket.gethostbyname(host), 80),
                                self.get_header)
         except:
             traceback.print_exc()
             self.left.write(not_found_response, self.left.close)
             self.right.close()
예제 #2
0
def get_peers_dht_port(torrent_peers, info_hash, peer_id, callback):
    dht_list = []
    torrent_connections = []
    io_loop = ioloop.IOLoop.instance()

    def check_dht_list_status(start_time):
        if len(dht_list) >= MIN_NUMBER_OF_DHT_BOOTSTRAP_PEERS or time.time(
        ) >= HOW_LONG_TO_WAIT_FOR_DHT_PEERS + start_time:
            io_loop.stop()

            for con in torrent_connections:
                con.close()

            callback(dht_list)

    for (cur_ip, cur_port) in torrent_peers:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        stream = iostream.IOStream(s)
        tc = TorrentConnection(stream, info_hash, peer_id, cur_ip, dht_list)
        stream.connect((cur_ip, cur_port), tc.send_handshake)
        torrent_connections.append(tc)

    periodic_dht_list_check = ioloop.PeriodicCallback(
        partial(check_dht_list_status, time.time()),
        DHT_LIST_CHECK_PERIOD_IN_MS,
        io_loop=io_loop)
    periodic_dht_list_check.start()
    io_loop.start()
예제 #3
0
 def _handle_events(self, fd, events):
     while True:
         try:
             connection, address = self._sockets[fd].accept()
         except (socket.error, e):
             if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
                 return
             raise
         try:
             stream = iostream.IOStream(connection, io_loop=self.io_loop)
             log.info(
                 "Starting new connection for client connection from %s:%s"
                 % address)
             self._connections[address] = XMPPConnection(stream, address)
             if not self.checker._running:
                 self.checker.start()
         except (Exception, e):
             exc_type, exc_value, exc_traceback = sys.exc_info()
             log.error("Error in connection callback, %s" % str(e))
             for line in traceback.format_tb(exc_traceback):
                 if line.find("\n") >= 0:
                     for subline in line.split("\n"):
                         log.error(subline)
                 else:
                     log.error(line.rstrip("\n"))
예제 #4
0
    def _get_socket2(self, port, host, timeout):
        # sync get proxy connect
        if hasattr(self, '__get_socket'):
            return self.__get_socket
        if self.debuglevel > 0: print >> stderr, 'connect:', (host, port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        #s = socket.create_connection(("10.0.1.62", 3128), timeout)
        s.connect((self.proxy_host, self.proxy_port))

        fp = s.makefile("r+")
        # # _send_str = "CONNECT %s:%s HTTP/1.1\r\n\r\nhost: %s\r\n\r\n" % (host, port, host)
        # _send_str = "CONNECT %s:%s HTTP/1.1\r\n\r\nhost: %s\r\n\r\nProxy-Authorization: guest idealsee2016\r\n\r\n" % (self.proxy_host, self.proxy_port, self.proxy_host)
        # _send_str = _send_str.encode("UTF-8")
        # fp.write(_send_str)
        # fp.flush()

        # fp = s.makefile("r+")
        _send_str = "CONNECT %s:%s HTTP/1.1\r\n\r\nhost: %s\r\n\r\n" % (
            host, port, host)
        _send_str = _send_str.encode("UTF-8")
        fp.write(_send_str)
        fp.flush()

        status_line = fp.readline().rstrip("\r\n")
        if 'HTTP' in status_line and ('200' not in status_line):
            raise SMTPConnectError(
                200,
                'proxy(%s, %s) connect to (%s, %s) failed, status_line is : %s'
                % (self.proxy_host, self.proxy_port, host, port, status_line))
        stream = iostream.IOStream(s)
        self.__get_socket = stream
        return stream
예제 #5
0
    def open(self):
        self.instances.append(self)

        self.username = self.get_argument('username', 'notsent')
        self.fd = self.request.connection.stream.socket.fileno()
        self._read_buffer = collections.deque()
        self.handshaking = True
        self.is_closed = False
        self._nobinary = 'flash' in self.request.arguments

        parts = self.get_argument('target').split(':')
        self.target_host = str(parts[0])
        self.target_port = int(parts[1])
        logging.info('%s ws proxy open' % self)

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)

        self.target_stream = iostream.IOStream(s, io_loop=self.ioloop)
        self.target_stream._always_callback = True
        self.target_stream._buffer_grown_callback = self.target_has_new_data
        self.target_stream.set_close_callback(self.target_stream_closed)
        if 'timeout' in self.request.arguments:
            timeout = int(self.get_argument('timeout')) / 1000.0
        else:
            timeout = WebSocketProxyHandler.connect_timeout
        self.connect_timeout = self.ioloop.add_timeout(
            time.time() + timeout, self.check_target_connected)
        self.addr = (self.target_host, self.target_port)
        #self.addr = ('110.174.252.130', 20862)
        #self.addr = ('84.215.241.100',53566 )
        #logging.info('connecting to target %s, %s' % self.addr)
        self.target_stream.connect(self.addr,
                                   callback=self.connected_to_target)
예제 #6
0
파일: dnode.py 프로젝트: ybrs/pydnode
 def connect(self, ip, port):
     self.ip = ip
     self.port = port
     self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
     self.conn = iostream.IOStream(self.socket)
     self.conn.connect((self.ip, self.port), self.connect_callback)
     ioloop.IOLoop.instance().start()
예제 #7
0
 def _connect(self):
     self.state = 'connected'
     self.reconnect_times = 0
     self.socket = socket.socket()
     self.stream = iostream.IOStream(self.socket)
     self.stream.set_close_callback(self.on_close)
     self.stream.connect((self.conf['host'], self.conf['port']), self.send)
예제 #8
0
    def assert_response_matches_request(self, request_bytes, response_bytes):
        host = '127.0.0.1'
        port = 22322

        server = Server(io_loop=self.io_loop)
        server.set_handler('text')
        server.listen(port, address=host)

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        stream = iostream.IOStream(s, io_loop=self.io_loop)

        def start_test():
            stream.connect((host, port), send_request)

        def send_request():
            stream.write(request_bytes, write_finished)

        def write_finished(*args, **kwargs):
            stream.read_bytes(len(response_bytes), receive_response)

        def receive_response(data):
            self.assertEqual(data, response_bytes)
            stream.close()
            self.stop()

        self.io_loop.add_callback(start_test)

        self.wait()
예제 #9
0
 def connect(self):
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
     if self.timeout:
         s.settimeout(self.timeout)
     s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
     self.stream = iostream.IOStream(s, io_loop=self.io_loop)
     self.stream.connect((self.host, self.port), self.on_connect)
예제 #10
0
 def __init__(self, url, protocols=None, version='8'):
     WebSocketBaseClient.__init__(self,
                                  url,
                                  protocols=protocols,
                                  version=version)
     self.io = iostream.IOStream(
         socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0))
예제 #11
0
    def __init__(self, url, io_loop=None, extra_headers=None):
        ports = {'ws': 80, 'wss': 443}

        self.url = urlparse.urlparse(url)
        self.host = self.url.hostname
        self.port = self.url.port or ports[self.url.scheme]
        self.path = self.url.path or '/'

        if extra_headers is not None and len(extra_headers) > 0:
            header_set = []
            for k, v in extra_headers.iteritems():
                header_set.append("%s: %s" % (k, v))
            self.headers = "\r\n".join(header_set)
        else:
            self.headers = None

        self.client_terminated = False
        self.server_terminated = False
        self._final_frame = False
        self._frame_opcode = None
        self._frame_length = None
        self._fragmented_message_buffer = None
        self._fragmented_message_opcode = None
        self._waiting = None

        self.key = base64.b64encode(os.urandom(16))
        self.stream = iostream.IOStream(socket.socket(), io_loop)
        self.stream.connect((self.host, self.port), self._on_connect)
    def __init__(self, url, io_loop=None, headers=None):
        ports = {'ws': 80, 'wss': 443}

        self.url = urlparse.urlparse(url)
        self.host = self.url.hostname
        self.port = self.url.port or ports[self.url.scheme]
        self.path = self.url.path or '/'
        self.headers = headers or {}

        logging.debug(
            "WebSocket using host (%s), port (%s) and path (%s)." % (
                self.host, self.port, self.path))

        self.client_terminated = False
        self.server_terminated = False
        self._final_frame = False
        self._frame_opcode = None
        self._frame_length = None
        self._fragmented_message_buffer = None
        self._fragmented_message_opcode = None
        self._waiting = None

        self.key = base64.b64encode(os.urandom(16))
        self.stream = iostream.IOStream(socket.socket(), io_loop=io_loop)
        self.stream.connect((self.host, self.port), self._on_connect)
예제 #13
0
파일: tornado.py 프로젝트: housleyjk/motor
    def __init__(
            self, sock, io_loop, use_ssl,
            certfile, keyfile, ca_certs, cert_reqs):
        self.use_ssl = use_ssl
        self.timeout = None
        if self.use_ssl:
            # In Python 3, Tornado's ssl_options_to_context fails if
            # any options are None.
            ssl_options = {}
            if certfile:
                ssl_options['certfile'] = certfile

            if keyfile:
                ssl_options['keyfile'] = keyfile

            if ca_certs:
                ssl_options['ca_certs'] = ca_certs

            if cert_reqs:
                ssl_options['cert_reqs'] = cert_reqs

            self.stream = iostream.SSLIOStream(
                sock, ssl_options=ssl_options, io_loop=io_loop)
        else:
            self.stream = iostream.IOStream(sock, io_loop=io_loop)
예제 #14
0
 def __init__(self, name):
     self.name = name
     self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
     self.sock.setblocking(0)
     self.stream = iostream.IOStream(self.sock)
     self.stream.connect("uds_" + name)
     ioloop.IOLoop.instance().start()
예제 #15
0
    def open_connection(self, callback=None):
        self.socket_idle = False

        if (self.latest_callback):
            # There's a connection waiting, let's just send an error
            # for it to finish properly
            try:
                self.latest_callback("finish\0".encode("utf-8"))
            except Exception as e:
                logging.warn("[host] latest callback failed: %s" % str(e))

        self.latest_callback = None

        def check_response():
            if callback is not None:
                callback()
            if len(self.queue):
                self.process_queue()
            else:
                self.socket_idle = True
            #self.setup_monitor()

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s = iostream.IOStream(s)
        self.s.set_close_callback(self.open_connection)

        ioloop.IOLoop.instance().add_callback(lambda: self.s.connect(
            (self.address, self.port), check_response))
예제 #16
0
    def __init__(self, url, io_loop=None, subprotocol='', extra_headers=None):
        ports = {'ws': 80, 'wss': 443}

        self.url = urlparse(url)
        self.host = self.url.hostname
        self.port = self.url.port or ports[self.url.scheme]
        self.path = self.url.path or '/'

        self.subprotocol = subprotocol
        self.headers = None
        if extra_headers is not None and len(extra_headers) > 0:
            header_set = []
            for k, v in extra_headers.iteritems():
                header_set.append('%s: %s' % (k, v))
            self.headers = '\r\n'.join(header_set)

        self.client_terminated = False
        self.server_terminated = False
        self._final_frame = False
        self._frame_opcode = None
        self._frame_length = None
        self._fragmented_message_buffer = None
        self._fragmented_message_opcode = None
        self._waiting = None

        self.key = ''.join(random.choice(string.ascii_letters) for _ in range(24))
        self.stream = iostream.IOStream(socket.socket(), io_loop)
        self.stream.connect((self.host, self.port), self._on_connect)
예제 #17
0
    def __init__(self, url, protocols=None, extensions=None,
                 io_loop=None, ssl_options=None, headers=None):
        """
        .. code-block:: python

            from tornado import ioloop

            class MyClient(TornadoWebSocketClient):
                def opened(self):
                    for i in range(0, 200, 25):
                        self.send("*" * i)

                def received_message(self, m):
                    print((m, len(str(m))))

                def closed(self, code, reason=None):
                    ioloop.IOLoop.instance().stop()

            ws = MyClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
            ws.connect()

            ioloop.IOLoop.instance().start()
        """
        WebSocketBaseClient.__init__(self, url, protocols, extensions,
                                     ssl_options=ssl_options, headers=headers)
        self.ssl_options["do_handshake_on_connect"] = False
        if self.scheme == "wss":
            self.sock = ssl.wrap_socket(self.sock, **self.ssl_options)
            self.io = iostream.SSLIOStream(self.sock, io_loop)
        else:
            self.io = iostream.IOStream(self.sock, io_loop)
        self.io_loop = io_loop
예제 #18
0
파일: server.py 프로젝트: Dmdv/fileuploader
    def handle_accept(self, fd, events):
        log.debug('accepting')

        conn, addr = self._sock.accept()
        stream = iostream.IOStream(conn)
        handler = Handler(stream)
        stream.read_until('\r\n\r\n', handler)
예제 #19
0
def handle_connection(connection, address):
    stream = iostream.IOStream(connection)
    callback = functools.partial(_handle_headers, stream)
    stream.read_until("\r\n\r\n", callback)
    stream.write("HTTP/1.0 200 OK\r\n")
    stream.write("Content-Type: image/gif\r\n")
    stream.write("\r\n")

    stream.write(HEADER_DATA)
    stream.write(INITIAL_FRAME)

    #exec traceroute against host ip
    remote_ip = address[0]
    traceroute_proc = process.Subprocess(
        ['mtr', '-c', '3', '-r', '-o', 'LSD BAW', remote_ip],
        stdout=process.Subprocess.STREAM)
    traceroute_proc.initialize()

    output_buffer = []
    _cb = functools.partial(_handleResult, output_buffer)

    result = traceroute_proc.stdout.read_until_close(callback=_cb,
                                                     streaming_callback=_cb)

    if CLOSE_ON_TIMEOUT:
        callback = functools.partial(closestream, stream, traceroute_proc)
        ioloop.IOLoop.instance().add_timeout(
            datetime.timedelta(seconds=MAX_TIME), callback)

    streams.append((stream, traceroute_proc, output_buffer, 0))
예제 #20
0
 def begin(self):
     """Start the sending of a message, if we need a connection open it"""
     if not self.stream:
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
         self.stream = iostream.IOStream(s)
         self.stream.connect((self.host, self.port), self.connected)
     else:
         self.work_or_quit(self.process)
예제 #21
0
    def connect(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self._stream = iostream.IOStream(sock)

        self._stream.connect((self._host, self._port), self.start)

        self._stream.set_close_callback(self.on_close)
        self._stream.read_until('\n', self._on_read_line)
예제 #22
0
 def _connect(self):
     "Create a TCP socket connection"
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
     sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
     sock.settimeout(self.socket_timeout)
     sock.connect((self.host, self.port))
     self.stream = iostream.IOStream(sock)
     self.on_connect()
예제 #23
0
 def _startserver(self):
     #sock = socket.socket(socket.AF_INET,
     #                     socket.SOCK_DGRAM)
     #sock.setblocking(False)
     #sock.bind(('', self.port))
     # self.s = UDPStream(sock, chunk_size=96, in_ioloop=self.ioloop)
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.s = iostream.IOStream(sock)
     self.s.connect((self._ip, self.port), callback=self._fill_queue)
예제 #24
0
 def __init__(self, *sock_params):
     if sock_params:
         sock = socket.socket(*sock_params)
         self.sock = iostream.IOStream(sock)
     else:
         self.sock = None
     self._closed = True
     self.transcript = []
     self.response_buffer = StringIO()
class _Host:
    _DEAD_RETRY = 30  # number of seconds before retrying a dead server.

    def __init__(self, host, debugfunc=None):
        if isinstance(host, types.TupleType):
            host = host[0]
            self.weight = host[1]
        else:
            self.weight = 1

        if host.find(":") > 0:
            self.ip, self.port = host.split(":")
            self.port = int(self.port)
        else:
            self.ip, self.port = host, 11211

        if not debugfunc:
            debugfunc = lambda x: x
        self.debuglog = debugfunc

        self.deaduntil = 0
        self.socket = None
        self.stream = None

    def _check_dead(self):
        if self.deaduntil and self.deaduntil > time.time():
            return 1
        self.deaduntil = 0
        return 0

    def connect(self):
        if self._get_socket():
            return 1
        return 0

    def mark_dead(self, reason):
        print "MemCache: %s: %s.  Marking dead." % (self, reason)
        self.deaduntil = time.time() + _Host._DEAD_RETRY
        self.close_socket()

    def _get_socket(self):
        if self._check_dead():
            return None
        if self.socket:
            return self.socket
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Python 2.3-ism:  s.settimeout(1)
        try:
            s.connect((self.ip, self.port))
        except socket.error, msg:
            self.mark_dead("connect: %s" % msg[1])
            return None
        self.socket = s
        self.stream = iostream.IOStream(s)
        self.stream.debug = True
        return s
예제 #26
0
파일: tornadoclient.py 프로젝트: 3GA/nush
 def __init__(self, url, protocols=None, extensions=None, io_loop=None):
     WebSocketBaseClient.__init__(self, url, protocols, extensions)
     if self.scheme == "wss":
         self.sock = ssl.wrap_socket(self.sock,
                                     do_handshake_on_connect=False)
         self.io = iostream.SSLIOStream(self.sock, io_loop)
     else:
         self.io = iostream.IOStream(self.sock, io_loop)
     self.sender = self.io.write
     self.io_loop = io_loop
예제 #27
0
 def __init__(self,
              address,
              timeout,
              final_callback,
              socket_callback=None,
              io_loop=None):
     gntplib.BaseGNTPConnection.__init__(self, final_callback,
                                         socket_callback)
     sock = socket.create_connection(address, timeout=timeout)
     self.stream = iostream.IOStream(sock, io_loop=io_loop)
예제 #28
0
    def open_connection_if_needed(self, callback):
        if self.sock is not None:
            callback()
            return

        if isinstance(self.addr, list):
            self.sock = iostream.IOStream(
                socket.socket(socket.AF_INET, socket.SOCK_STREAM))
        else:
            if not os.path.exists(self.addr):
                return
            self.sock = iostream.IOStream(
                socket.socket(socket.AF_UNIX, socket.SOCK_STREAM))

        def check_response():
            callback()
            self.sock.read_until(b"\0", self.keep_reading)

        self.sock.connect(self.addr, check_response)
예제 #29
0
파일: server.py 프로젝트: shawwwn/scylla
    def connect(self):
        def read_from_client(data):
            upstream.write(data)

        def read_from_upstream(data):
            client.write(data)

        def client_close(data=None):
            if upstream.closed():
                return
            if data:
                upstream.write(data)
            upstream.close()

        def upstream_close(data=None):
            if client.closed():
                return
            if data:
                client.write(data)
            client.close()

        def start_tunnel():
            client.read_until_close(client_close, read_from_client)
            upstream.read_until_close(upstream_close, read_from_upstream)
            client.write(b'HTTP/1.1 200 Connection established\r\n\r\n')

        def on_connect(data=None):
            if data:
                first_line = data.splitlines()[0]
                http_v, status, text = first_line.split(None, 2)
                if 200 == int(status):
                    start_tunnel()
                    return
            self.set_status(500)
            self.finish()

        def start_proxy_tunnel():
            upstream.write(b'CONNECT %b HTTP/1.1\r\n' %
                           bytes(self.request.uri, 'utf8'))
            upstream.write(b'Host: %b\r\n' % bytes(self.request.uri, 'utf8'))
            upstream.write(b'Proxy-Connection: Keep-Alive\r\n\r\n')
            upstream.read_until(b'\r\n\r\n', on_connect)

        try:
            proxy = get_proxy(True)
            client = self.request.connection.stream
            upstream = iostream.IOStream(
                socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0))
            upstream.connect((proxy.ip, proxy.port), start_proxy_tunnel)
        except:
            self.set_status(500)
            self.write("Internal server error:\n" +
                       ''.join(traceback.format_exception(*sys.exc_info())))
            self.finish()
예제 #30
0
def connection_ready(sock, fd, events):
    while True:
        try:
            connection, address = sock.accept()
        except socket.error, e:
            if e[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
                raise
            return
        connection.setblocking(0)
        stream = iostream.IOStream(connection)
        stream.write("HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nPong!\r\n",
                     stream.close)