Пример #1
0
    def handle_request(self, c):
        funcname = result = request = None
        try:
            connection.deliver_challenge(c, self.authkey)
            connection.answer_challenge(c, self.authkey)
            request = c.recv()
            ignore, funcname, args, kwds = request
            assert funcname in self.public, '%r unrecognized' % funcname
            func = getattr(self, funcname)
        except Exception:
            msg = ('#TRACEBACK', format_exc())
        else:
            try:
                result = func(c, *args, **kwds)
            except Exception:
                msg = ('#TRACEBACK', format_exc())
            else:
                msg = ('#RETURN', result)

        try:
            c.send(msg)
        except Exception as e:
            try:
                c.send(('#TRACEBACK', format_exc()))
            except Exception:
                pass

            util.info('Failure to send message: %r', msg)
            util.info(' ... request was %r', request)
            util.info(' ... exception was %r', e)

        c.close()
Пример #2
0
    def ClientWithTimeout(self, address, authkey, timeout):
        with socket.socket(socket.AF_INET) as s:
            s.setblocking(True)
            s.connect(address)

            # We'd like to call s.settimeout(timeout) here, but that won't work.

            # Instead, prepare a C "struct timeval" to specify timeout. Note that
            # these field sizes may differ by platform.
            seconds = int(timeout)
            microseconds = int((timeout - seconds) * 1e6)
            timeval = struct.pack("@LL", seconds, microseconds)

            # And then set the SO_RCVTIMEO (receive timeout) option with this.
            s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval)

            # Now create the connection as normal.
            c = Connection(s.detach())

        # The following code will now fail if a socket timeout occurs.

        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)

        return c
Пример #3
0
    def handle_request(self, c):
        funcname = result = request = None
        try:
            connection.deliver_challenge(c, self.authkey)
            connection.answer_challenge(c, self.authkey)
            request = c.recv()
            ignore, funcname, args, kwds = request
            func = getattr(self, funcname)
        except Exception:
            msg = ('#TRACEBACK', format_exc())
        else:
            try:
                result = func(c, *args, **kwds)
            except Exception:
                msg = ('#TRACEBACK', format_exc())
            else:
                msg = ('#RETURN', result)

        try:
            c.send(msg)
        except Exception as e:
            try:
                c.send(('#TRACEBACK', format_exc()))
            except Exception:
                pass

            util.info('Failure to send message: %r', msg)
            util.info(' ... request was %r', request)
            util.info(' ... exception was %r', e)

        c.close()
        return
Пример #4
0
 def handle_request(self, c):
     '''
     Handle a new connection
     '''
     funcname = result = request = None
     try:
         connection.deliver_challenge(c, self.authkey)
         connection.answer_challenge(c, self.authkey)
         request = c.recv()
         ignore, funcname, args, kwds = request
         assert funcname in self.public, '%r unrecognized' % funcname
         func = getattr(self, funcname)
     except Exception:
         msg = ('#TRACEBACK', format_exc())
     else:
         try:
             result = func(c, *args, **kwds)
         except Exception:
             msg = ('#TRACEBACK', format_exc())
         else:
             msg = ('#RETURN', result)
     try:
         c.send(msg)
     except Exception, e:
         try:
             c.send(('#TRACEBACK', format_exc()))
         except Exception:
             pass
         util.info('Failure to send message: %r', msg)
         util.info(' ... request was %r', request)
         util.info(' ... exception was %r', e)
Пример #5
0
 def __init__(self, address, authkey=None):
     sock = socket.socket(socket.AF_UNIX)
     sock.setblocking(True)
     sock.connect(address)
     super(JsonClient, self).__init__(sock)
     if authkey is not None:
         connection.answer_challenge(self, authkey)
         connection.deliver_challenge(self, authkey)
Пример #6
0
 def __init__(self, address, authkey=None):
     sock = socket.socket(socket.AF_UNIX)
     sock.setblocking(True)
     sock.connect(address)
     super(JsonClient, self).__init__(sock)
     if authkey is not None:
         connection.answer_challenge(self, authkey)
         connection.deliver_challenge(self, authkey)
Пример #7
0
def _Client(address, authkey=None):
    """ Returns a connection to the address of a `Listener` """
    c = _SocketClient(address)
    if authkey is not None:
        if not isinstance(authkey, bytes):
            raise TypeError("Expected a byte string as an authentication key")
        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)
    return c
Пример #8
0
def _Client(address, authkey=None):
    """ Returns a connection to the address of a `Listener` """
    c = _SocketClient(address)
    if authkey is not None:
        if not isinstance(authkey, bytes):
            raise TypeError("Expected a byte string as an authentication key")
        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)
    return c
Пример #9
0
def Client(address, family=None, authkey=None, timeout=None):
    family = family or address_type(address)
    if family == 'AF_PIPE':
        c = PipeClient(address, timeout=timeout)
    else:
        c = SocketClient(address, timeout=timeout)

    if authkey is not None and not isinstance(authkey, bytes):
        raise TypeError, 'authkey should be a byte string'

    if authkey is not None:
        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)
    return c
Пример #10
0
def challenge():
    while (True):
        answer_challenge(supervisor_connection, authkey="hi")
        print "answered challenge"
Пример #11
0
if __name__ == '__main__':
    print "bro IP: {}, port: {}".format(sys.argv[1], sys.argv[2])

    address = (sys.argv[1], int(sys.argv[2]))
    print address
    listener = Listener(address,
                        authkey="hi")  #listener(address, authkey='pw')
    #try:
    supervisor_connection = listener.accept()
    #except:
    #	print "Connection to supervisor failed"
    #	exit()

    print "Connected to supervisor"
    temp = supervisor_connection.recv()
    print temp
    answer_challenge(supervisor_connection, authkey="hi")

    #p = Process(target=challenge)
    #p.start()

    while True:
        answer_challenge(supervisor_connection, authkey="hi")
        print "answered challenge"

    while True:
        time.sleep(10)

    supervisor_connection.close()
    listener.close()
Пример #12
0
                s.connect(address)
            except socket.error, e:
                if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                    debug('failed to connect to address %s', address)
                    raise
                time.sleep(0.01)
            else:
                break
        else:
            raise

        fd = duplicate(s.fileno())
        conn = _multiprocessing.Connection(fd)
        s.close()
        ## authenticate
        answer_challenge(conn, authkey)
        deliver_challenge(conn, authkey)
        return conn
    ## end of copy

    def connectClientIPv6(address, authkey):
        try:
            client = SocketClientv6(address, authkey)
        except socket.error:
            return None
        return SocketConnection(client)

else:
    def connectClientIPv6(*args):
        return BrokenConnection()