Ejemplo n.º 1
0
def fake_tftp_read(env, path):
    # connect to server
    f = client_sock_file(env['next-server'], WALT_SERVER_TCP_PORT)
    # send the request id
    Requests.send_id(f, Requests.REQ_FAKE_TFTP_GET)
    # wait for the READY message from the server
    f.readline()
    # write the parameters
    write_pickle(dict(node_mac=env['mac'], path=remote_absname(env, path)), f)
    # receive status
    status = f.readline().decode('UTF-8').strip()
    if status == 'OK':
        # read size
        size = int(f.readline().strip())
        print(path, size)
        # receive content
        content = b''
        while len(content) < size:
            content += f.read(size - len(content))
    else:
        content = None
    # close file and return
    f.close()
    print(path + " " + status)
    return content
Ejemplo n.º 2
0
 def open_channel_to_node(self):
     # connect
     server_host = conf['server']
     sock_file = client_sock_file(server_host, WALT_SERVER_TCP_PORT)
     # write request id
     Requests.send_id(sock_file, Requests.REQ_TCP_TO_NODE)
     # send parameters
     write_pickle(self.params, sock_file)
     # wait for the status message from the server
     status = sock_file.readline().strip()
     if status == 'OK':
         print 'New connection forwarded to node.'
         return sock_file
     else:
         sock_file.close()
         print status
         return None
Ejemplo n.º 3
0
 def open_channel_to_node(self):
     # connect
     server_host = conf['server']
     sock_file = client_sock_file(server_host, WALT_SERVER_TCP_PORT)
     # write request id
     Requests.send_id(sock_file, Requests.REQ_TCP_TO_NODE)
     # send parameters
     write_pickle(self.params, sock_file)
     # wait for the status message from the server
     status = sock_file.readline().strip()
     if status == b'OK':
         print('New connection forwarded to node.')
         return sock_file
     else:
         sock_file.close()
         print(status)
         return None
Ejemplo n.º 4
0
def run_transfer(req_id, dst_dir, dst_name, src_dir, src_name, tmp_name,
                            client_operand_index, **entity_params):
    params = dict(
        dst_dir = dst_dir,
        dst_name = dst_name,
        src_dir = src_dir,
        src_name = src_name,
        tmp_name = tmp_name,
        **entity_params
    )
    # connect to server
    f = client_sock_file(conf['server'], WALT_SERVER_TCP_PORT)
    # send the request id
    Requests.send_id(f, req_id)
    # wait for the READY message from the server
    f.readline()
    # write the parameters
    write_pickle(params, f)
    # handle client-side archiving / unarchiving
    with ProgressMessageThread('Transfering...') as message_thread:
        if client_operand_index == 0:
            # client is sending
            writer = SmartWriter(f)
            with tarfile.open(mode='w|', fileobj=writer, dereference=True) as archive:
                archive.add(os.path.join(src_dir, src_name), arcname=tmp_name)
            # let the other end know we are done writing
            writer.shutdown_write()
            # wait the other end to close
            writer.wait_remote_close()
            # ok done
            writer.close()
            # did we detect a message sent to us during the transfer?
            msg = writer.get_msg()
            if len(msg) > 0:
                # yes, interrupt the progress meter and print it
                message_thread.interrupt(msg)
        else:
            # client is receiving
            with tarfile.open(mode='r|', fileobj=f) as archive:
                archive.extractall(path=dst_dir)
            tmp_path = os.path.join(dst_dir, tmp_name)
            dst_path = os.path.join(dst_dir, dst_name)
            os.rename(tmp_path, dst_path)
    f.close()
Ejemplo n.º 5
0
def run_transfer(req_id, dst_dir, dst_name, src_dir, src_name, tmp_name,
                 client_operand_index, **entity_params):
    params = dict(dst_dir=dst_dir,
                  dst_name=dst_name,
                  src_dir=src_dir,
                  src_name=src_name,
                  tmp_name=tmp_name,
                  **entity_params)
    # connect to server
    f = client_sock_file(conf['server'], WALT_SERVER_TCP_PORT)
    # send the request id
    Requests.send_id(f, req_id)
    # wait for the READY message from the server
    f.readline()
    # write the parameters
    write_pickle(params, f)
    # handle client-side archiving / unarchiving
    with ProgressMessageThread('Transfering...') as message_thread:
        if client_operand_index == 0:
            # client is sending
            writer = SmartWriter(f)
            with tarfile.open(mode='w|', fileobj=writer,
                              dereference=True) as archive:
                archive.add(os.path.join(src_dir, src_name), arcname=tmp_name)
            # let the other end know we are done writing
            writer.shutdown_write()
            # wait the other end to close
            writer.wait_remote_close()
            # ok done
            writer.close()
            # did we detect a message sent to us during the transfer?
            msg = writer.get_msg()
            if len(msg) > 0:
                # yes, interrupt the progress meter and print it
                message_thread.interrupt(msg)
        else:
            # client is receiving
            with tarfile.open(mode='r|', fileobj=f) as archive:
                archive.extractall(path=dst_dir)
            tmp_path = os.path.join(dst_dir, tmp_name)
            dst_path = os.path.join(dst_dir, dst_name)
            os.rename(tmp_path, dst_path)
    f.close()
Ejemplo n.º 6
0
 def __init__(self, req_id, **params):
     if sys.stdout.isatty() and sys.stdin.isatty():
         self.client_tty = True
         self.tty_settings = TTYSettings()
         # send terminal width and provided parameters
         params.update(win_size=self.tty_settings.win_size)
     else:
         self.client_tty = False
     # tell server whether we are on a tty
     params.update(client_tty=self.client_tty)
     # connect
     server_host = conf['server']
     self.sock_file = client_sock_file(server_host, WALT_SERVER_TCP_PORT)
     # write request id
     Requests.send_id(self.sock_file, req_id)
     # wait for the READY message from the server
     self.sock_file.readline()
     write_pickle(params, self.sock_file)
     # provide read_available() method
     self.stdin_reader = SmartFile(unbuffered(stdin, 'rb'))
Ejemplo n.º 7
0
def read_vpn_node_image(entrypoint):
    # connect to server
    sock = client_sock_file(conf['server'], WALT_SERVER_TCP_PORT)
    # send the request id
    Requests.send_id(sock, Requests.REQ_VPN_NODE_IMAGE)
    # wait for the READY message from the server
    sock.readline()
    # write the parameters
    write_pickle(dict(model='rpi-3-b-plus', entrypoint=entrypoint), sock)
    # initial communication loop
    while True:
        line = sock.readline().decode('UTF-8').strip()
        words = line.split()
        if words[0] == 'MSG':
            print(' '.join(words[1:]))
        elif words[0] == 'ERR':
            print(' '.join(words[1:]), file=sys.stderr)
            return
        elif words[0] == 'START':
            break
        else:
            print('Unexpected communication issue with walt server!',
                  file=sys.stderr)
            return
    # starting transfer
    with open('rpi3bp-vpn.dd', 'wb') as f:
        while True:
            chunk = sock.read(2048)
            if len(chunk) == 0:
                break
            f.write(chunk)
    sock.close()
    print("A file 'rpi3bp-vpn.dd' has been generated in current directory.")
    print(
        "Flash it (using dd tool or similar) on the SD card of the rpi3b+ board you want to use as a VPN node."
    )
def fake_tftp_read(env, path):
    # connect to server
    f = client_sock_file(env['next-server'], WALT_SERVER_TCP_PORT)
    # send the request id
    Requests.send_id(f, Requests.REQ_FAKE_TFTP_GET)
    # wait for the READY message from the server
    f.readline()
    # write the parameters
    write_pickle(dict(
            node_mac=env['mac'],
            path=remote_absname(env, path)), f)
    # receive status
    status = f.readline().strip()
    if status == 'OK':
        # read size
        size = int(f.readline().strip())
        # receive content
        content = f.read(size)
    else:
        content = None
    # close file and return
    f.close()
    print(path + " " + status)
    return content
Ejemplo n.º 9
0
 def request_log_dump(self, **kwargs):
     Requests.send_id(self.f, Requests.REQ_DUMP_LOGS)
     write_pickle(kwargs, self.f)
Ejemplo n.º 10
0
 def request_log_dump(self, **kwargs):
     Requests.send_id(self.f, Requests.REQ_DUMP_LOGS)
     write_pickle(kwargs, self.f)