Ejemplo n.º 1
0
def send_random_data(socket, no_blocks=100):
    print("Send random data...")
    i_block = 0  # if setting i_block to sth higher, printed results will incorrect
    max_stop_block = 10000
    stop_block = no_blocks
    cur_marker = 0
    n_samples_waiting = 49
    n_to_next_marker = n_samples_waiting
    rng = RandomState(3948394)
    assert stop_block < max_stop_block
    while i_block < stop_block:
        # chan x time
        arr = rng.randn(TCP_SENDER_EEG_NCHANS, TCP_SENDER_EEG_NSAMPLES).astype(np.float32)

        arr[-1,:] = cur_marker
        socket.send(arr.tobytes(order='F'))
        assert arr.shape == (TCP_SENDER_EEG_NCHANS, TCP_SENDER_EEG_NSAMPLES)
        i_block += 1
        gevent.sleep(0.03)
        n_to_next_marker -= TCP_SENDER_EEG_NSAMPLES
        if n_to_next_marker <= 0:
            n_to_next_marker = n_samples_waiting
            if cur_marker  == 0:
                cur_marker = float(rng.randint(1,6))
            else:
                cur_marker = 0
Ejemplo n.º 2
0
def client_service(socket):
    while True:
        print("2")
        data = socket.recv(1024)
        if not data:
            socket.close()
            return
        else:
            send_data = "return : ".encode("gb2312") + data
            socket.send(send_data)
def client_service():
    for socket in client_list:
        print("2")
        data = socket.recv(1024)
        if not data:
            socket.close()
            return
        else:
            send_data = "return : ".encode("gb2312")+ data
            socket.send(send_data)
Ejemplo n.º 4
0
def test_leader_adds_node_to_cluster_by_hostip():
    cm = ClusterManagerMock()
    cm.is_leader = True
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline()
        socket.send("\n")
        line = sockfile.readline()
        assert '127.0.0.1' in cm.cluster, \
            "Leader did not add host to cluster: %s" % line
Ejemplo n.º 5
0
def app(socket, address):
    checked_order, filepath = get_order(socket)
    print checked_order
    if not checked_order:
        ss = socket.send('No file need send.')
        socket.close()
        return
    ss = socket.send(json.dumps(checked_order))
    for file in checked_order:
        status = trans_file(socket, os.path.join(filepath, file[0]), file[1], file[2])
        print status
    socket.close()
    return
Ejemplo n.º 6
0
def test_leader_adds_node_to_cluster_by_name():
    node_name = 'node-a'
    cm = ClusterManagerMock()
    cm.is_leader = True
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline()
        socket.send("%s\n" % node_name)
        line = sockfile.readline()
        assert node_name in cm.cluster, \
            "Leader did not add name to cluster: %s" % line
    del socket
    del sockfile
Ejemplo n.º 7
0
    def handle_connection(self, socket, address):

        start = datetime.datetime.now()
        data = socket.recv(104857600)
        end = datetime.datetime.now()

        data = data.split('\n')

        if self.debug: print("New incoming {} connection from {}".format(data[0], address))
        if data[0] == 'ECHO':
            socket.send(data[0])
            socket.close()

        elif data[0] == 'UPDATE':
            data = json.loads(data[1])

            self.handle_update(data)
            self.propagate_update(data)

            socket.close()

        elif data[0] == 'DATA':
            print start
            print end
            delay = (((end - start) / 2).microseconds) / 1000.0
            print delay

            data = json.loads(data[1])

            self.handle_data(data)
            self.propagate_data(data)

            socket.close();

        elif data[0] == 'STATUS':
            status = 'Node: ' + self.this_node + '\n'
            status += 'Connected nodes: ' + str(self.g.nodes()) + '\n'

            for a,b in self.g.edges():
                status += str(a) + ' ' + str(b) + ' ' + str(self.g[a][b]['weight']) + '\n'

            if self.debug: print status
            socket.send(status)
            socket.close()

        else:
            print 'UNRECOG: '
            print data
Ejemplo n.º 8
0
def test_drop_node_on_disconnect():
    node_name = 'node'
    cm = ClusterManagerMock()
    cm.is_leader = True
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline() # cluster list pre-join
        socket.send("%s\n" % node_name)
        line = sockfile.readline() # cluster list post-join
        assert node_name in cm.cluster, \
            "Leader did not add host to cluster: %s" % line
        socket.shutdown(0)
        # Make sure to cleanup file descriptors:
        del sockfile
        del socket
        yield_(2) # Yield to let PeerServer catch the disconnect
        assert not node_name in cm.cluster, \
            "Leader did not remove host from cluster."
Ejemplo n.º 9
0
        def send_rsp():
            while not is_connection_closed[0]:
                try:
                    meta_info, rsp = rsp_queue.get(timeout=1)
                except gevent.queue.Empty:
                    continue

                serialized_rsp = serialize_message(meta_info, rsp)
                sent_bytes = 0
                try:
                    while sent_bytes < len(serialized_rsp):
                        sent_bytes += socket.send(serialized_rsp[sent_bytes:])
                except soc_error as e:
                    logging.warning('socket send error: ' + str(e))
                    break
Ejemplo n.º 10
0
def handleTCP(socket, address):
    global ttl, httpData
    socket.settimeout(ttl)
    httpFlag = False
    ip, port = address
    buf = ""
    dport = 0
    try:
        dport = int(
            os.popen(
                "grep \"src=%s\" /proc/net/nf_conntrack | grep tcp | grep \"sport=%d\"| tail -n 1"
                % (
                    ip,
                    port,
                )).read().split("dport=", 1)[1].split(" ", 1)[0])
    except:
        pass

    if dport == 0:
        try:
            dport = int(
                os.popen(
                    "grep \"src=%s\" /proc/net/ip_conntrack | grep tcp | grep \"sport=%d\"| tail -n 1"
                    % (
                        ip,
                        port,
                    )).read().split("dport=", 1)[1].split(" ", 1)[0])
        except:
            pass
    log = "[+] TCP Connection on Port: %d from %s:%d Time: %s\n" % (
        dport, ip, port, datetime.utcnow().isoformat())
    print log,
    with open("logs.txt", "a") as f:
        f.write(log)
        f.close()
    try:
        if dport in [443] or 443 == dport % 1000:
            context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            context.load_cert_chain(certfile="ssl.crt", keyfile="ssl.key")
            context.options = ssl.OP_ALL
            try:
                sslsock = context.wrap_socket(socket, server_side=True)
                while True:
                    buffer = sslsock.read()
                    if not buffer:
                        sslsock.send(httpData)
                        break
                    buf += buffer
                    if checkHTTP(buf):
                        httpFlag = True

            except Exception as e:
                print "[-] Error: %s" % (e, )
            finally:
                try:
                    sslsock.close()
                except:
                    pass
        elif dport in [23]:
            socket.send("login: "******"\n":
                    buf += buffer
                    break
                else:
                    buf += buffer
            if not socket.closed:
                socket.send("Password: "******"\n":
                    buf += buffer
                    socket.send("XM# ")
                    break
                else:
                    buf += buffer
            while not socket.closed:

                buffer = recv(socket)
                if not buffer:
                    socket.close()
                    break
                elif buffer == "\n":
                    tosend = telnetparse(buf.split("\n")[-1])
                    if tosend <> None:
                        socket.send(tosend)
                        socket.send("\n")
                    buf += buffer
                    socket.send("XM# ")
                else:
                    buf += buffer

        else:
            while not socket.closed:
                buffer = recv(socket)
                if not buffer:
                    if httpFlag:
                        socket.send(httpData)
                    socket.close()
                    break
                else:
                    buf += buffer
                    if checkHTTP(buf):
                        httpFlag = True

    except Exception as e:
        print "[-] Error : %s " % (e, )
    with open(
            "captures/tcp/%d_%s_%d_%s.txt" %
        (
            dport,
            ip,
            port,
            datetime.utcnow().isoformat().replace(":", "-").replace(".", "-"),
        ), "wb") as file:
        file.write(buf)
        file.close()
    grabHTTP(buf, "tcp", ip, port, dport)
Ejemplo n.º 11
0
def send_command(MAC, socket, connection, cursor, command, response_length=0, response_type=0, times=5):

    """通过链接发送命令,等待响应,返回有效响应数据

    这个函数默认发送五次命令,如果其中有一次发送成功,同时没有其它错误,则返回有效响应数据(删除响应头,命令类型以及校验和),结束循环,函数返回.

    下面介绍每次循环的流程:

        首先通过链接尝试发送数据,如果出现异常,关闭连接,清理资源;
        然后通过链接尝试接收数据,如果出现异常,关闭连接,清理资源,如果等待超时,进入下次循环,重新发送命令;
        获得响应数据,如果响应数据为空,关闭连接,清理资源;
        然后检验响应数据类型是否与期望相符,如果相符,则验证校验和,如果校验通过,返回有效响应数据,否则进入下次循环,重新发送命令;
        如果响应数据类型与期望不符,把设备响应信息当作上报信息处理

        *因为处理上报信息过程中发送的命令无需返回,所以发送之后,函数直接返回,不进行后续操作*

    如果五次循环过后,仍未获得有效响应数据,关闭链接,清理资源

    :param MAC: 设备的物理地址(唯一标志)
    :param socket: 设备和程序之间的链接
    :param connection: 当前协程的数据库链接(每个协程使用独立的数据库链接)
    :param cursor:  当前协程的数据库游标(每个协程使用独立的数据库游标)
    :param command: 待发送的命令
    :param response_length: 因为设备返回的响应数据不遵循标准,所以需要根据协议手动获取有效响应数据
    :param response_type: 期望的响应数据的类型,用来检查响应数据是否是上报信息
    :param times: 循环发送命令的次数,默认值`5`
    :return: 如果一切正常,返回有效响应数据;如果指定循环次数过后,仍未获得有效响应数据,返回`None`

    """

    for _ in xrange(times):

        try:

            socket.send(set_checksum(command))

        except:

            cursor.execute("UPDATE device SET online = 0 WHERE mac = %s", MAC)

            logging.debug(cursor._last_executed)

            logging.critical(sys.exc_info()[1][1])

            cursor.close()

            connection.close()

            socket.close()

            return

        if response_length == 0 and response_type == 0:

            return

        else:

            while 1:

                logging.info("%s 尝试接收数据" % MAC)

                try:

                    response = socket.recv(4096)

                except gevent.socket.timeout:

                    break

                except gevent.socket.error:

                    cursor.execute("UPDATE device SET online = 0 WHERE mac = %s", MAC)

                    logging.debug(cursor._last_executed)

                    logging.critical(sys.exc_info()[1][1])

                    cursor.close()

                    connection.close()

                    socket.close()

                    return

                logging.info("%s 发送 %s" % (MAC, str(hexlify(response[:response_length]))))

                if response:

                    data = translate(response, "client")

                    if data[2] != response_type:

                        handle_report(MAC, socket, connection, cursor, response)

                        break

                    else:

                        if not get_checksum(response[:response_length]):

                            logging.error("%s 响应校验出错" % MAC)

                            break

                        else:

                            return data[3:-1]
                else:

                    cursor.execute("UPDATE device SET online = 0 WHERE mac = %s", MAC)

                    logging.debug(cursor._last_executed)

                    logging.critical("%s 设备返回空值" % MAC)

                    cursor.close()

                    connection.close()

                    socket.close()

                    return

    cursor.execute("UPDATE device SET online = 0 WHERE mac = %s", MAC)

    logging.debug(cursor._last_executed)

    logging.critical("%s 等待响应超时" % MAC)

    cursor.close()

    connection.close()

    socket.close()

    return None
Ejemplo n.º 12
0
def send_command(MAC,
                 socket,
                 connection,
                 cursor,
                 command,
                 response_length=0,
                 response_type=0,
                 times=5):
    """通过链接发送命令,等待响应,返回有效响应数据

    这个函数默认发送五次命令,如果其中有一次发送成功,同时没有其它错误,则返回有效响应数据(删除响应头,命令类型以及校验和),结束循环,函数返回.

    下面介绍每次循环的流程:

        首先通过链接尝试发送数据,如果出现异常,关闭连接,清理资源;
        然后通过链接尝试接收数据,如果出现异常,关闭连接,清理资源,如果等待超时,进入下次循环,重新发送命令;
        获得响应数据,如果响应数据为空,关闭连接,清理资源;
        然后检验响应数据类型是否与期望相符,如果相符,则验证校验和,如果校验通过,返回有效响应数据,否则进入下次循环,重新发送命令;
        如果响应数据类型与期望不符,把设备响应信息当作上报信息处理

        *因为处理上报信息过程中发送的命令无需返回,所以发送之后,函数直接返回,不进行后续操作*

    如果五次循环过后,仍未获得有效响应数据,关闭链接,清理资源

    :param MAC: 设备的物理地址(唯一标志)
    :param socket: 设备和程序之间的链接
    :param connection: 当前协程的数据库链接(每个协程使用独立的数据库链接)
    :param cursor:  当前协程的数据库游标(每个协程使用独立的数据库游标)
    :param command: 待发送的命令
    :param response_length: 因为设备返回的响应数据不遵循标准,所以需要根据协议手动获取有效响应数据
    :param response_type: 期望的响应数据的类型,用来检查响应数据是否是上报信息
    :param times: 循环发送命令的次数,默认值`5`
    :return: 如果一切正常,返回有效响应数据;如果指定循环次数过后,仍未获得有效响应数据,返回`None`

    """

    for _ in xrange(times):

        try:

            socket.send(set_checksum(command))

        except:

            cursor.execute("UPDATE device SET online = 0 WHERE mac = %s", MAC)

            logging.debug(cursor._last_executed)

            logging.critical(sys.exc_info()[1][1])

            cursor.close()

            connection.close()

            socket.close()

            return

        if response_length == 0 and response_type == 0:

            return

        else:

            while 1:

                logging.info("%s 尝试接收数据" % MAC)

                try:

                    response = socket.recv(4096)

                except gevent.socket.timeout:

                    break

                except gevent.socket.error:

                    cursor.execute(
                        "UPDATE device SET online = 0 WHERE mac = %s", MAC)

                    logging.debug(cursor._last_executed)

                    logging.critical(sys.exc_info()[1][1])

                    cursor.close()

                    connection.close()

                    socket.close()

                    return

                logging.info("%s 发送 %s" %
                             (MAC, str(hexlify(response[:response_length]))))

                if response:

                    data = translate(response, "client")

                    if data[2] != response_type:

                        handle_report(MAC, socket, connection, cursor,
                                      response)

                        break

                    else:

                        if not get_checksum(response[:response_length]):

                            logging.error("%s 响应校验出错" % MAC)

                            break

                        else:

                            return data[3:-1]
                else:

                    cursor.execute(
                        "UPDATE device SET online = 0 WHERE mac = %s", MAC)

                    logging.debug(cursor._last_executed)

                    logging.critical("%s 设备返回空值" % MAC)

                    cursor.close()

                    connection.close()

                    socket.close()

                    return

    cursor.execute("UPDATE device SET online = 0 WHERE mac = %s", MAC)

    logging.debug(cursor._last_executed)

    logging.critical("%s 等待响应超时" % MAC)

    cursor.close()

    connection.close()

    socket.close()

    return None
Ejemplo n.º 13
0
def handleTCP(socket, address):
    global ttl, httpData
    socket.settimeout(ttl)
    httpFlag = False
    ip, port = address
    buf = ""
    dport = 0
    try:
        dport = int(os.popen("grep \"src=%s\" /proc/net/nf_conntrack | grep tcp | grep \"sport=%d\"| tail -n 1" % (ip, port,)).read().split("dport=", 1)[1].split(" ", 1)[0])
    except:
        pass

    if dport == 0:
        try:
            dport = int(os.popen("grep \"src=%s\" /proc/net/ip_conntrack | grep tcp | grep \"sport=%d\"| tail -n 1" % (ip, port,)).read().split("dport=", 1)[1].split(" ", 1)[0])
        except:
            pass
    log = "[+] TCP Connection on Port: %d from %s:%d Time: %s\n" % (dport, ip, port, datetime.utcnow().isoformat())
    print log,
    with open("logs.txt", "a") as f:
        f.write(log)
        f.close()
    try:
        if dport in [443] or 443 == dport%1000:
            context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            context.load_cert_chain(certfile="ssl.crt", keyfile="ssl.key")
            context.options = ssl.OP_ALL
            try:
                sslsock = context.wrap_socket(socket, server_side=True)
                while True:
                    buffer = sslsock.read()
                    if not buffer:
                        sslsock.send(httpData)
                        break
                    buf+=buffer
                    if checkHTTP(buf):
                        httpFlag = True

            except Exception as e:
                print "[-] Error: %s" % (e,)
            finally:
                    try:
                        sslsock.close()
                    except:
                        pass
        elif dport in [23]:
            socket.send("login: "******"\n":
                    buf+= buffer
                    break
                else:
                    buf+=buffer
            if not socket.closed:
                socket.send("Password: "******"\n":
                    buf+= buffer
                    socket.send("XM# ")
                    break
                else:
                    buf+=buffer
            while not socket.closed:
                
                buffer = recv(socket)
                if not buffer:
                    socket.close()
                    break
                elif buffer == "\n":
                    tosend = telnetparse(buf.split("\n")[-1])
                    if tosend <> None:
                        socket.send(tosend)
                        socket.send("\n")
                    buf+= buffer
                    socket.send("XM# ")
                else:
                    buf+= buffer 
                
                   

        else:
            while not socket.closed:
                buffer = recv(socket)
                if not buffer:
                    if httpFlag:
                        socket.send(httpData)
                    socket.close()
                    break
                else:
                    buf+= buffer
                    if checkHTTP(buf):
                        httpFlag = True
       
    except Exception as e:
        print "[-] Error : %s " % (e,)
    with open("captures/tcp/%d_%s_%d_%s.txt" % (dport, ip, port, datetime.utcnow().isoformat().replace(":", "-").replace(".", "-"),) , "wb") as file:
        file.write(buf)
        file.close()    
Ejemplo n.º 14
0
def shandler(socket,address):
    print address

    print socket.recv(10)

    socket.send("good")
Ejemplo n.º 15
0
 def outgoing(self, data):
     socket = self.endpoints[self.current_endpoint]
     socket.send(data)
     socket.flush()
     print "Sent data to %s: %s" % (self.current_endpoint, data)
Ejemplo n.º 16
0
def handle(socket, address):
    socket.send("Hello from a telnet!")
    socket.close()
Ejemplo n.º 17
0
def handle(socket, address):
    socket.send("Hello from a telnet!")
    socket.close()
Ejemplo n.º 18
0
def tcp_handler(socket, address, conn_data, accept_count):
    need_to_close = False
    need_to_send_data = False
    accepted_name = conn_data['name'] + '-%d' % accept_count
    conn_greenlets[accepted_name] = {
        'greenlet': Greenlet.getcurrent(),
        'result': None,
        'data': None
    }
    DBG('new tcp accepted from %s:%s conn_data = %r' %
        (address[0], address[1], conn_data))
    data_pack = dict(name=conn_data['name'],
                     accepted_name=accepted_name,
                     op="accepted",
                     remote_ip=address[0],
                     remote_port=address[1],
                     local_port=conn_data['specific_port'],
                     type='tcpserver')
    re = guifeeder_socket.sendto(json.dumps(data_pack), GUIFEEDER_ADDRESS)
    DBG("sent to gui feeder %r" % re)

    while True:
        try:
            if need_to_close:
                DBG('tcp_handler need_to_close')
                socket.close()
                conn_greenlets[accepted_name]['result'].set({'result': 'ok'})
                return
            if need_to_send_data:
                try:
                    re = socket.send(
                        base64.b64decode(
                            conn_greenlets[accepted_name]['data']))
                    conn_greenlets[accepted_name]['result'].set('ok')
                    DBG("sent to remote %r" % re)
                    need_to_send_data = False
                except:
                    DBG_TRACE()
                    conn_greenlets[accepted_name]['result'].set('error')

            data = socket.recv(8012)

            if not data:  # closed by remote
                DBG('tcp_handler got nothing')
                socket.close()
                data_pack = {"name": accepted_name, "op": "disconnected"}
                re = guifeeder_socket.sendto(json.dumps(data_pack),
                                             GUIFEEDER_ADDRESS)
                del conn_greenlets[accepted_name]
                DBG("sent to gui feeder %r" % re)
                return  # die

            else:
                DBG(type(data))
                DBG('tcp_handler %s got %r' % (time.ctime(), bytearray(data)))
                data_pack = {
                    "name": accepted_name,
                    "op": "recvdata",
                    "text": base64.b64encode(bytearray(data))
                }
                # socket.send('hi')
            re = guifeeder_socket.sendto(json.dumps(data_pack),
                                         GUIFEEDER_ADDRESS)
            DBG("sent to gui feeder %r" % re)
        except CloseSocket:
            DBG("tcp_handler CloseSocket")
            need_to_close = True
        except SendData:
            DBG("tcp_handler SendData")
            need_to_send_data = True
        except:
            DBG_TRACE()
            data_pack = {
                "name": conn_data['name'],
                'type': conn_data['type'],
                "op": "error",
                'msg': base64.b64encode(str(sys.exc_info()[1]))
            }
            socket.close()
            del conn_greenlets[conn_data['name']]
            re = guifeeder_socket.sendto(json.dumps(data_pack),
                                         GUIFEEDER_ADDRESS)
            DBG("sent to gui feeder %r" % re)
            return
Ejemplo n.º 19
0
 def handle(self, socket, address):
     if select.select([socket], [], [], 10)[0]:
         data, addr = socket.recvfrom(1024)  # buffer size is 1024 bytes
         print(data)
         socket.send(b"HOI!\n")
         socket.close()