def send_help(ip, port, other_hash): files = os.listdir(file_dir) logging.debug(f'port: {port}') with socket.create_connection((ip, port)) as s: for fname in files: h = sha1(fname.encode('utf-8')).digest() h = int.from_bytes(h, byteorder='little') print(h) if other_hash > my_id: if my_id < h < other_hash or leaving: fname = '/'.join((file_dir, fname)) f = open(fname, 'rb') data = b64encode(f.read()) data = data.decode('utf-8') f.close() os.unlink(fname) data = (dumps({'fname': fname, 'data': data})).encode('utf-8') req = {'op': 0, 'length': len(data)} req = difuse_request.build(req) s.sendall(req+data) else: if h < other_hash or h > my_id or leaving: fname = '/'.join((file_dir, fname)) f = open(fname, 'rb') data = b64encode(f.read()) data = data.decode('utf-8') f.close() os.unlink(fname) data = (dumps({'fname': fname, 'data': data})).encode('utf-8') req = {'op': 0, 'length': len(data)} req = difuse_request.build(req) s.sendall(req+data)
def leave(): s = socket.socket() s.connect((argv[1], int(argv[2]))) h = difuse_request.build({'op': 0x4, 'length': 0}) s.sendall(h) h = s.recv(difuse_response.sizeof()) h = difuse_response.parse(h) data = s.recv(h.length) data = loads(data.decode('utf-8')) print('leave', data) if data: s = socket.socket() s.connect((data['ip'], 8080)) data = dumps({'hash': myhash}).encode('utf-8') h = difuse_request.build({'op': 0x17, 'length': len(data)}) s.sendall(h + data)
def recv_help(ip, my_hash): logging.debug(f'ip: {ip}') logging.debug(f'hash: {my_hash}') s = socket.create_connection((ip, 8080)) listenfd = socket.socket() listenfd.bind(('0.0.0.0', 0)) listenfd.listen() port = listenfd.getsockname() logging.debug(f'port: {port[1]}') data = dumps({'port': port[1], 'hash': my_hash}).encode('utf-8') req = {} req['op'] = 0x16 req['length'] = len(data) req = difuse_request.build(req) s.sendall(req+data) s.close() s, conn = listenfd.accept() while(1): h = s.recv(difuse_request.sizeof()) if not h: break h = difuse_request.parse(h) data = s.recv(h.length) data = loads(data) data['data'] = data['data'].encode('utf-8') data['data'] = b64decode(data['data']) f = open(data['fname'], 'wb') f.write(data['data']) f.close() s.close() listenfd.close()
def reqboot(op, data): serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.connect((argv[1], int(argv[2]))) data = dumps(data).encode('utf-8') res = {} res['op'] = op res['length'] = len(data) request = difuse_request.build(res) serversocket.sendall(request + data)
def join(): global my_id s = socket.socket() s.connect((argv[1], int(argv[2]))) h = difuse_request.build({'op': 0x3, 'length': 0}) s.sendall(h) h = s.recv(difuse_response.sizeof()) h = difuse_response.parse(h) data = s.recv(h.length) logging.debug(f'data: {data}') data = loads(data.decode('utf-8')) my_id = data['id'] if 'ip' in data: recv_files(data['ip'], data['id'])
def list_dir(fd, addr, req): ips = hash2ip.values() file_list = [] for ip in ips: with socket.create_connection((ip, 8080)) as s: req = {} req['op'] = 0x18 req['length'] = 0 req = difuse_request.build(req) s.sendall(req) res = difuse_response.parse(s.recv(difuse_response.sizeof())) file_list += loads(s.recv(res.length)) data = dumps(file_list).encode('utf-8') res = {} res['status'] = 0 res['length'] = len(data) fd.sendall(difuse_response.build(res)+data)
def requestread(self, op, data, ip, port): serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.connect((ip, port)) data = dumps(data) res = {} res['op'] = op res['length'] = len(data) request = difuse_request.build(res) req_data = request + data.encode('utf-8') serversocket.sendall(req_data) res = serversocket.recv(difuse_response.sizeof()) res_header = difuse_response.parse(res) if res_header.status == 1: raise FuseOSError(ENOENT) st = {} if (res_header.length): st = serversocket.recv(res_header.length) return st