File: forward_server.py

package info (click to toggle)
python-pika 1.3.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,088 kB
  • sloc: python: 20,890; makefile: 134
file content (532 lines) | stat: -rw-r--r-- 20,362 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""TCP/IP forwarding/echo service for testing."""

from __future__ import print_function

import array
from datetime import datetime
import errno
from functools import partial
import logging
import multiprocessing
import os
import socket
import struct
import sys
import threading
import traceback

import pika.compat

if pika.compat.PY3:

    def buffer(object, offset, size):  # pylint: disable=W0622
        """array etc. have the buffer protocol"""
        return object[offset:offset + size]


try:
    import SocketServer
except ImportError:
    import socketserver as SocketServer  # pylint: disable=F0401


def _trace(fmt, *args):
    """Format and output the text to stderr"""
    print((fmt % args) + "\n", end="", file=sys.stderr)


class ForwardServer(object):  # pylint: disable=R0902
    """ Implement a TCP/IP forwarding/echo service for testing. Listens for
    an incoming TCP/IP connection, accepts it, then connects to the given
    remote address and forwards data back and forth between the two
    endpoints.

    This is similar to a subset of `netcat` functionality, but without
    dependency on any specific flavor of netcat

    Connection forwarding example; forward local connection to default
      rabbitmq addr, connect to rabbit via forwarder, then disconnect
      forwarder, then attempt another pika operation to see what happens

        with ForwardServer(("localhost", 5672)) as fwd:
            params = pika.ConnectionParameters(
                host=fwd.server_address[0],
                port=fwd.server_address[1])
            conn = pika.BlockingConnection(params)

        # Once outside the context, the forwarder is disconnected

        # Let's see what happens in pika with a disconnected server
        channel = conn.channel()

    Echo server example
        def produce(sock):
            sock.sendall("12345")
            sock.shutdown(socket.SHUT_WR)

        with ForwardServer(None) as echo:
            sock = socket.socket()
            sock.connect(echo.server_address)

            worker = threading.Thread(target=produce,
                                      args=[sock])
            worker.start()

            data = sock.makefile().read()
            assert data == "12345", data

        worker.join()

    """
    # Amount of time, in seconds, we're willing to wait for the subprocess
    _SUBPROC_TIMEOUT = 10

    def __init__(
            self,  # pylint: disable=R0913
            remote_addr,
            remote_addr_family=socket.AF_INET,
            remote_socket_type=socket.SOCK_STREAM,
            server_addr=("127.0.0.1", 0),
            server_addr_family=socket.AF_INET,
            server_socket_type=socket.SOCK_STREAM,
            local_linger_args=None):
        """
        :param tuple remote_addr: remote server's IP address, whose structure
          depends on remote_addr_family; pair (host-or-ip-addr, port-number).
          Pass None to have ForwardServer behave as echo server.
        :param remote_addr_family: socket.AF_INET (the default), socket.AF_INET6
          or socket.AF_UNIX.
        :param remote_socket_type: only socket.SOCK_STREAM is supported at this
          time
        :param server_addr: optional address for binding this server's listening
          socket; the format depends on server_addr_family; defaults to
          ("127.0.0.1", 0)
        :param server_addr_family: Address family for this server's listening
          socket; socket.AF_INET (the default), socket.AF_INET6 or
          socket.AF_UNIX; defaults to socket.AF_INET
        :param server_socket_type: only socket.SOCK_STREAM is supported at this
          time
        :param tuple local_linger_args: SO_LINGER sockoverride for the local
          connection sockets, to be configured after connection is accepted.
          None for default, which is to not change the SO_LINGER option.
          Otherwise, its a two-tuple, where the first element is the `l_onoff`
          switch, and the second element is the `l_linger` value, in seconds
        """
        self._logger = logging.getLogger(__name__)

        self._remote_addr = remote_addr
        self._remote_addr_family = remote_addr_family
        assert remote_socket_type == socket.SOCK_STREAM, remote_socket_type
        self._remote_socket_type = remote_socket_type

        assert server_addr is not None
        self._server_addr = server_addr

        assert server_addr_family is not None
        self._server_addr_family = server_addr_family

        assert server_socket_type == socket.SOCK_STREAM, server_socket_type
        self._server_socket_type = server_socket_type

        self._local_linger_args = local_linger_args

        self._subproc = None

    @property
    def running(self):
        """Property: True if ForwardServer is active"""
        return self._subproc is not None

    @property
    def server_address_family(self):
        """Property: Get listening socket's address family

        NOTE: undefined before server starts and after it shuts down
        """
        assert self._server_addr_family is not None, "Not in context"

        return self._server_addr_family

    @property
    def server_address(self):
        """ Property: Get listening socket's address; the returned value
        depends on the listening socket's address family

        NOTE: undefined before server starts and after it shuts down
        """
        assert self._server_addr is not None, "Not in context"

        return self._server_addr

    def __enter__(self):
        """ Context manager entry. Starts the forwarding server

        :returns: self
        """
        return self.start()

    def __exit__(self, *args):
        """ Context manager exit; stops the forwarding server
        """
        self.stop()

    def start(self):
        """ Start the server

        NOTE: The context manager is the recommended way to use
        ForwardServer. start()/stop() are alternatives to the context manager
        use case and are mutually exclusive with it.

        :returns: self
        """
        queue = multiprocessing.Queue()

        self._subproc = multiprocessing.Process(
            target=_run_server,
            kwargs=dict(
                local_addr=self._server_addr,
                local_addr_family=self._server_addr_family,
                local_socket_type=self._server_socket_type,
                local_linger_args=self._local_linger_args,
                remote_addr=self._remote_addr,
                remote_addr_family=self._remote_addr_family,
                remote_socket_type=self._remote_socket_type,
                queue=queue))
        self._subproc.daemon = True
        self._subproc.start()

        try:
            # Get server socket info from subprocess
            self._server_addr_family, self._server_addr = queue.get(
                block=True, timeout=self._SUBPROC_TIMEOUT)
            queue.close()
        except Exception:  # pylint: disable=W0703
            try:
                self._logger.exception(
                    "Failed while waiting for local socket info")
                # Preserve primary exception and traceback
                raise
            finally:
                # Clean up
                try:
                    self.stop()
                except Exception:  # pylint: disable=W0703
                    # Suppress secondary exception in favor of the primary
                    self._logger.exception(
                        "Emergency subprocess shutdown failed")

        return self

    def stop(self):
        """Stop the server

        NOTE: The context manager is the recommended way to use
        ForwardServer. start()/stop() are alternatives to the context manager
        use case and are mutually exclusive with it.
        """
        self._logger.info("ForwardServer STOPPING")

        try:
            self._subproc.terminate()
            self._subproc.join(timeout=self._SUBPROC_TIMEOUT)
            if self._subproc.is_alive():
                self._logger.error(
                    "ForwardServer failed to terminate, killing it")
                os.kill(self._subproc.pid)
                self._subproc.join(timeout=self._SUBPROC_TIMEOUT)
                assert not self._subproc.is_alive(), self._subproc

            # Log subprocess's exit code; NOTE: negative signal.SIGTERM (usually
            # -15) is normal on POSIX systems - it corresponds to SIGTERM
            exit_code = self._subproc.exitcode
            self._logger.info("ForwardServer terminated with exitcode=%s",
                              exit_code)
        finally:
            self._subproc = None


def _run_server(
        local_addr,
        local_addr_family,
        local_socket_type,  # pylint: disable=R0913
        local_linger_args,
        remote_addr,
        remote_addr_family,
        remote_socket_type,
        queue):
    """ Run the server; executed in the subprocess

    :param local_addr: listening address
    :param local_addr_family: listening address family; one of socket.AF_*
    :param local_socket_type: listening socket type; typically
        socket.SOCK_STREAM
    :param tuple local_linger_args: SO_LINGER sockoverride for the local
        connection sockets, to be configured after connection is accepted.
        Pass None to not change SO_LINGER. Otherwise, its a two-tuple, where the
        first element is the `l_onoff` switch, and the second element is the
        `l_linger` value in seconds
    :param remote_addr: address of the target server. Pass None to have
        ForwardServer behave as echo server
    :param remote_addr_family: address family for connecting to target server;
        one of socket.AF_*
    :param remote_socket_type: socket type for connecting to target server;
        typically socket.SOCK_STREAM
    :param multiprocessing.Queue queue: queue for depositing the forwarding
        server's actual listening socket address family and bound address. The
        parent process waits for this.
    """

    # NOTE: We define _ThreadedTCPServer class as a closure in order to
    # override some of its class members dynamically
    # NOTE: we add `object` to the base classes because `_ThreadedTCPServer`
    # isn't derived from `object`, which prevents `super` from working properly
    class _ThreadedTCPServer(SocketServer.ThreadingMixIn,
                             SocketServer.TCPServer, object):
        """Threaded streaming server for forwarding"""

        # Override TCPServer's class members
        address_family = local_addr_family
        socket_type = local_socket_type
        allow_reuse_address = True

        def __init__(self):

            handler_class_factory = partial(
                _TCPHandler,
                local_linger_args=local_linger_args,
                remote_addr=remote_addr,
                remote_addr_family=remote_addr_family,
                remote_socket_type=remote_socket_type)

            super(_ThreadedTCPServer, self).__init__(
                local_addr, handler_class_factory, bind_and_activate=True)

    server = _ThreadedTCPServer()

    # Send server socket info back to parent process
    queue.put([server.socket.family, server.server_address])

    queue.close()

    server.serve_forever()


# NOTE: we add `object` to the base classes because `StreamRequestHandler` isn't
# derived from `object`, which prevents `super` from working properly
class _TCPHandler(SocketServer.StreamRequestHandler, object):
    """TCP/IP session handler instantiated by TCPServer upon incoming
    connection. Implements forwarding/echo of the incoming connection.
    """

    _SOCK_RX_BUF_SIZE = 16 * 1024

    def __init__(
            self,  # pylint: disable=R0913
            request,
            client_address,
            server,
            local_linger_args,
            remote_addr,
            remote_addr_family,
            remote_socket_type):
        """
        :param request: for super
        :param client_address: for super
        "paarm server:  for super
        :param tuple local_linger_args: SO_LINGER sockoverride for the local
            connection sockets, to be configured after connection is accepted.
            Pass None to not change SO_LINGER. Otherwise, its a two-tuple, where
            the first element is the `l_onoff` switch, and the second element is
            the `l_linger` value in seconds
        :param remote_addr: address of the target server. Pass None to have
            ForwardServer behave as echo server.
        :param remote_addr_family: address family for connecting to target
            server; one of socket.AF_*
        :param remote_socket_type: socket type for connecting to target server;
            typically socket.SOCK_STREAM
        :param **kwargs: kwargs for super class
        """
        self._local_linger_args = local_linger_args
        self._remote_addr = remote_addr
        self._remote_addr_family = remote_addr_family
        self._remote_socket_type = remote_socket_type

        super(_TCPHandler, self).__init__(
            request=request, client_address=client_address, server=server)

    def handle(self):  # pylint: disable=R0912
        """Connect to remote and forward data between local and remote"""
        local_sock = self.connection

        if self._local_linger_args is not None:
            # Set SO_LINGER socket options on local socket
            l_onoff, l_linger = self._local_linger_args
            local_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                                  struct.pack('ii', l_onoff, l_linger))

        if self._remote_addr is not None:
            # Forwarding set-up
            remote_dest_sock = remote_src_sock = socket.socket(
                family=self._remote_addr_family,
                type=self._remote_socket_type,
                proto=socket.IPPROTO_IP)
            remote_dest_sock.connect(self._remote_addr)
            _trace("%s _TCPHandler connected to remote %s", datetime.utcnow(),
                   remote_dest_sock.getpeername())
        else:
            # Echo set-up
            # NOTE: Use pika.compat._nonblocking_socketpair() since
            # socket.socketpair() isn't available on Windows under python 2 yet.
            remote_dest_sock, remote_src_sock = \
                    pika.compat._nonblocking_socketpair()
            # We rely on blocking I/O
            remote_dest_sock.setblocking(True)
            remote_src_sock.setblocking(True)

        try:
            local_forwarder = threading.Thread(
                target=self._forward, args=(
                    local_sock,
                    remote_dest_sock,
                ))
            local_forwarder.setDaemon(True)
            local_forwarder.start()

            try:
                self._forward(remote_src_sock, local_sock)
            finally:
                # Wait for local forwarder thread to exit
                local_forwarder.join()
        finally:
            try:
                try:
                    _safe_shutdown_socket(remote_dest_sock, socket.SHUT_RDWR)
                finally:
                    if remote_src_sock is not remote_dest_sock:
                        _safe_shutdown_socket(remote_src_sock, socket.SHUT_RDWR)
            finally:
                remote_dest_sock.close()
                if remote_src_sock is not remote_dest_sock:
                    remote_src_sock.close()

    def _forward(self, src_sock, dest_sock):  # pylint: disable=R0912
        """Forward from src_sock to dest_sock"""
        src_peername = src_sock.getpeername()

        _trace("%s forwarding from %s to %s", datetime.utcnow(), src_peername,
               dest_sock.getpeername())
        try:
            # NOTE: python 2.6 doesn't support bytearray with recv_into, so
            # we use array.array instead; this is only okay as long as the
            # array instance isn't shared across threads. See
            # http://bugs.python.org/issue7827 and
            # groups.google.com/forum/#!topic/comp.lang.python/M6Pqr-KUjQw
            rx_buf = array.array("B", [0] * self._SOCK_RX_BUF_SIZE)

            while True:
                try:
                    nbytes = src_sock.recv_into(rx_buf)
                except pika.compat.SOCKET_ERROR as exc:
                    if exc.errno == errno.EINTR:
                        continue
                    elif exc.errno == errno.ECONNRESET:
                        # Source peer forcibly closed connection
                        _trace("%s errno.ECONNRESET from %s", datetime.utcnow(),
                               src_peername)
                        break
                    else:
                        _trace("%s Unexpected errno=%s from %s\n%s",
                               datetime.utcnow(), exc.errno, src_peername,
                               "".join(traceback.format_stack()))
                        raise

                if not nbytes:
                    # Source input EOF
                    _trace("%s EOF on %s", datetime.utcnow(), src_peername)
                    break

                try:
                    dest_sock.sendall(buffer(rx_buf, 0, nbytes))
                except pika.compat.SOCKET_ERROR as exc:
                    if exc.errno == errno.EPIPE:
                        # Destination peer closed its end of the connection
                        _trace("%s Destination peer %s closed its end of "
                               "the connection: errno.EPIPE", datetime.utcnow(),
                               dest_sock.getpeername())
                        break
                    elif exc.errno == errno.ECONNRESET:
                        # Destination peer forcibly closed connection
                        _trace("%s Destination peer %s forcibly closed "
                               "connection: errno.ECONNRESET",
                               datetime.utcnow(), dest_sock.getpeername())
                        break
                    else:
                        _trace("%s Unexpected errno=%s in sendall to %s\n%s",
                               datetime.utcnow(), exc.errno,
                               dest_sock.getpeername(), "".join(
                                   traceback.format_stack()))
                        raise
        except:
            _trace("forward failed\n%s", "".join(traceback.format_exc()))
            raise
        finally:
            _trace("%s done forwarding from %s", datetime.utcnow(),
                   src_peername)
            try:
                # Let source peer know we're done receiving
                _safe_shutdown_socket(src_sock, socket.SHUT_RD)
            finally:
                # Let destination peer know we're done sending
                _safe_shutdown_socket(dest_sock, socket.SHUT_WR)


def echo(port=0):
    """ This function implements a simple echo server for testing the
    Forwarder class.

    :param int port: port number on which to listen

    We run this function and it prints out the listening socket binding.
    Then, we run Forwarder and point it at this echo "server".
    Then, we run telnet and point it at forwarder and see if whatever we
    type gets echoed back to us.

    This function waits for the client to connect and exits after the client
    closes the connection
    """
    lsock = socket.socket()
    lsock.bind(("", port))
    lsock.listen(1)
    _trace("Listening on sockname=%s", lsock.getsockname())

    sock, remote_addr = lsock.accept()
    try:
        _trace("Connection from peer=%s", remote_addr)
        while True:
            try:
                data = sock.recv(4 * 1024)  # pylint: disable=E1101
            except pika.compat.SOCKET_ERROR as exc:
                if exc.errno == errno.EINTR:
                    continue
                else:
                    raise

            if not data:
                break

            sock.sendall(data)  # pylint: disable=E1101
    finally:
        try:
            _safe_shutdown_socket(sock, socket.SHUT_RDWR)
        finally:
            sock.close()


def _safe_shutdown_socket(sock, how=socket.SHUT_RDWR):
    """ Shutdown a socket, suppressing ENOTCONN
    """
    try:
        sock.shutdown(how)
    except pika.compat.SOCKET_ERROR as exc:
        if exc.errno != errno.ENOTCONN:
            raise