def request_send(sock,path=None,exactsize=None): if not exactsize: print "Requesting to send", path length = ceil(os.stat(path).st_size,16)*16 + 256 #now length is a generous estimate of ciphertext filesize message = makeheader(3,length) sock.send(message) #send request has no body else: print "Requesting to send", exactsize, "bytes" sock.send(makeheader(3,exactsize)) reply = sock.recv(1) if ord(reply) != 1: print "Upload request denied. Sorry." return False return True
def upload(socket, data): """ Sends a large string data to the server, using sha to ensure integrity """ cursor = 0 while cursor < len(data): if len(data) - cursor >= 256: block = data[cursor : cursor + 256] else: block = data[cursor:] socket.send(block) socket.send(sha(block)) cursor += 256 # wait for acknowledgement from server resend = "" while True: resend += socket.recv(1) if resend[-1] == chr(255): resend = [int(i) for i in resend.split(chr(0))[:-1]] break for i in resend: # resend corrupted blocks # print "resending block", i socket.send(makeheader(4, min(256, len(data) - i))) upload(data[i : min(i + 256, len(data))]) return len(resend)
def remote_create(sock,path): if os.path.isfile(path): if request_send(sock,path): send_file(sock,path) else: #user made a folder message = makeheader(2,getrel(path)) sock.send(message)
def send_file(sock, path): fin = open(repopath(sock) + "\\" + path, "rb") data = fin.read() exactlength = len(data) + ceil(len(data), 256) * 64 # length of file + number of hashes * 64 bytes per hash # SEND HEAD message = makeheader(4, path, exactlength) sock.send(message) # SEND BODY r = upload(sock, data) sock.send(sha(data)[:8])
def relay(sock, command, args): if command == 2: # a folder was made message = makeheader(2, args[0]) sock.send(message) if command == 4: # a file was added to a folder; now add it to yours if request_send(sock, exactsize=int(args[1]) + 256): send_file(sock, args[0]) elif command == 5: # a file was deleted from a folder; now delete it from yours message = makeheader(5, args[0]) sock.send(message) elif command == 6: # a file was moved in a folder; now move it in yours message = makeheader(6, args[0], args[1]) sock.send(message) elif command == 7: # a file was renamed in a folder; now rename it in yours message = makeheader(7, args[0], args[1]) sock.send(message)
def send_file(sock,path): cipher = [] print "Encrypting..." cipher = easyaes.encrypt(g.password,path=path) #easyaes needs your password to make an IV) print "Done" print list(sha(cipher)[:8]) exactlength = len(cipher)+ceil(len(cipher),256)*64 # length of file + number of hashes * 64 bytes per hash # SEND HEAD message = makeheader(4,getrel(path),exactlength) sock.send(message) # SEND BODY r = upload(sock,cipher) print "File sent,", r, "blocks resent"
def remote_rename(sock,pathold,pathnew): message = makeheader(7,pathold,pathnew) sock.send(message)
def remote_move(sock,pathold,pathnew): message = makeheader(6,pathold,pathnew) sock.send(message)
def remote_delete(sock,path): message = makeheader(5,getrel(path)) sock.send(message)