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
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
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
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()
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()
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'))
def write_to_client(self, stream_id, senders_filtered=False, **record): try: if stream_id not in self.cache: self.cache[stream_id] = self.db.execute( """SELECT d.name as sender, s.name as stream FROM logstreams s, devices d WHERE s.id = %s AND s.sender_mac = d.mac """ % stream_id).fetchall()[0]._asdict() stream_info = self.cache[stream_id] # when data comes from the db, senders are already filtered, # while data coming from the hub has to be filtered. if not senders_filtered: if stream_info['sender'] not in self.params['senders']: return # filter out # matching the streams or the logline is always done here, otherwise # there may be inconsistencies between the regexp format in the # postgresql database and in python if self.streams_regexp: matches = self.streams_regexp.findall(stream_info['stream']) if len(matches) == 0: return # filter out if self.logline_regexp: matches = self.logline_regexp.findall(record['line']) if len(matches) == 0: return # filter out d = {} d.update(record) d.update(stream_info) if self.sock_file.closed: raise IOError() write_pickle(d, self.sock_file) except IOError as e: # the socket was supposedly closed. print "client log connection closing" # notify the hub that we should be removed. return False
def write_to_client(self, stream_id, senders_filtered=False, **record): try: if stream_id not in self.cache: self.cache[stream_id] = self.db.execute( """SELECT d.name as sender, s.name as stream FROM logstreams s, devices d WHERE s.id = %s AND s.sender_mac = d.mac """ % stream_id).fetchall()[0]._asdict() stream_info = self.cache[stream_id] # when data comes from the db, senders are already filtered, # while data coming from the hub has to be filtered. if not senders_filtered: if stream_info['sender'] not in self.params['senders']: return # filter out # matching the streams or the logline is always done here, otherwise # there may be inconsistencies between the regexp format in the # postgresql database and in python if self.streams_regexp: matches = self.streams_regexp.findall(stream_info['stream']) if len(matches) == 0: return # filter out if self.logline_regexp: matches = self.logline_regexp.findall(record['line']) if len(matches) == 0: return # filter out d = {} d.update(record) d.update(stream_info) if self.sock_file.closed: raise IOError() write_pickle(d, self.sock_file) except IOError as e: # the socket was supposedly closed. print("client log connection closing") # notify the hub that we should be removed. return False
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
def request_log_dump(self, **kwargs): Requests.send_id(self.f, Requests.REQ_DUMP_LOGS) write_pickle(kwargs, self.f)