Example #1
0
def download(IPDatabase, localData, fileInfo):
	#file info = (filename , filepath , ip)
	ip=fileInfo[2]
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	i=0
	while i<=5:
		try:
			s.connect((ip, 44450))
			break
		except (socket.error):
			print('socket error')
			i+=1
			if i==5:
				return None
	
	if True: 
		utils.throw(s, '-3')
		port = int(utils.catch(s))
		s.close()
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.connect((ip, port))
		filePath = encrypt.encrypt(str(fileInfo[1]), IPDatabase.table[ip].key)
		utils.throw(s, filePath)
		download = localData.newDownload(fileInfo)
		
		FTS.recvFile(localData, download, s, fileInfo[0][3:], IPDatabase.table[ip].key)
Example #2
0
def handleSearch(IPDatabase, localData, ms, addr):
	user = IPDatabase.table[addr[0]]
	c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	c.bind(('', 0))
	port = str(c.getsockname()[1])
	utils.throw(ms, port)
	c.listen(1)
	s, addr = c.accept()
	
	term = encrypt.refract( utils.catch(s) , user.key)
	master=''
	for key in localData.files.keys():
		try:
			if term.lower() in localData.files[key].lower() or term.lower() in key.lower():
				master= master+localData.files[key]+','+key+"|"
		except UnicodeEncodeError:
			pass
	if len(master) >0:
		master=master[:-1]
	
	master=encrypt.encrypt(master, user.key)
	packetSize=1024
	segments = math.ceil(len(master)/packetSize)
	s.send(utils.formatNumber(segments).encode())
	position=0
	index=0
	while index<segments:
		segment=master[position:position+packetSize]
		s.send(segment.encode())
		position+=packetSize
		index+=1
Example #3
0
def sendSearch(IPDatabase, ip, term):
	
	user = IPDatabase.table[ip]
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.settimeout(1)
	i=0
	while i<=5:
		try:
			s.connect((ip, 44450))
			break
		except (socket.error):
			print('socket error')
			i+=1
			if i==5:
				return []
	try:
		temp='-2'
		utils.throw(s, temp)
		
		port = utils.catch(s) #get the new port
		s.close()
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.connect((ip, int(port))) #Connect
		eTerm = encrypt.encrypt(term, IPDatabase.table[ip].key)
		utils.throw(s, eTerm)
		#Send the term
	
		packets = int(s.recv(4).decode()) #get the total number of packets
		master=''
		index =0
		packetSize = 1024
		while index < packets:
			data= s.recv(packetSize)
			while len(data)< packetSize:
				if index == packets-1:
					break
				temp = s.recv(packetSize-len(data))
				data =data+temp
			master=master+data.decode()
			index+=1
		
		master = encrypt.refract(master, user.key)

		res=[]
		if master=='':
			return []
		for packet in master.split('|'):
				mid = packet.find(",")
				temp=(packet[:mid], packet[mid+1:])
				res.append(temp)
		
		s.close()
		
		return res
	
	except (socket.error):
		print("Connection Failed")
		s.close()
		return []
Example #4
0
def setUpConnection(IPDatabase, localData, ip, auto=False):
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.settimeout(1)
	i=0
	while i < 5:
		try:
			s.connect((ip, 44450))
			utils.throw(s, '-0')
			break
		except (socket.error):
			i+=1
			if auto == False:
				ip = input("Oops.. the IP that you entered was incorrect or the peer is not responding \nPlease enter a new IP or [0]' to exit: ")
			if auto == True:
				pass
			if ip.lower() == "0":
				return []
			if i >5:
				return []

	try:
		port = utils.catch(s)
		s.close()
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.settimeout(1)
		#print('connecting')
		s.connect((ip, int(port)))
		ip =s.getpeername()[0]
		#('making key')
		key = encrypt.genKey(s)
		#print('sending nickname')
		myNick = encrypt.encrypt(localData.nickName,key)
		utils.throw(s, myNick)
		
		#print('receiving nickname')
		data=utils.catch(s)
		nick=encrypt.refract(data, key)
		
		peerList=[]
		data=s.recv(4)
		for i in range(int(data.decode())): 
			peerList.append(encrypt.refract(utils.catch(s),key))
		
		result= IPDatabase.addIP(ip, key, nick)
		if auto == False:
			if result == 0:
				print('IP Table: Added \''+ IPDatabase.table[ip].nick +'\' with IP [' + IPDatabase.table[ip].ip+ '] correctly')
			else:
				print('IP Table: \'' +IPDatabase.table[ip].nick+'\' Already in table')
				s.close()
				return peerList
		s.close()
			
	except(socket.error):
		if auto== False:
			print('Connection failded: not adding new node')
		return []
Example #5
0
def removeConnection(IPDatabase, ip):
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.settimeout(1)
	try:
		s.connect((ip, 44450))
		utils.throw(s,'-1')
		s.close()
	except (socket.error):
		pass

	IPDatabase.rmIP(ip)
Example #6
0
def sendFile(s, fileLocation, key):
	try:
		f = open(fileLocation, 'rb')
		fileSize = os.stat(fileLocation).st_size
		dataSize=1024
		segments = math.ceil(fileSize/dataSize)
		utils.throw(s, str(segments))
		index =0
		while index < segments:
			segment = f.read(dataSize)
			s.send(segment)
			index +=1
		f.close()
	except(socket.error):
		return
Example #7
0
def handleDownload(IPDatabase, ms, addr):
	try:
		user = IPDatabase.table[addr[0]]
		c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		c.bind(('', 0))
		port = str(c.getsockname()[1])
		utils.throw(ms, str(port))
		c.listen(1)
		c, addr = c.accept()
		
		fileLocation = encrypt.refract(utils.catch(c), user.key)
		
		FTS.sendFile(c, fileLocation, user.key)
	
	except(socket.error):
		return
Example #8
0
def handleConnection(IPDatabase, localData, ms, addr):
	try:
		c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		c.bind(('', 0))
		
		port = str(c.getsockname()[1])
		utils.throw(ms, port)
		c.listen(1)
		s, addr = c.accept()
		
		key = encrypt.genKey(s, 'RECV')
		
		#print('receiving nickname')
		data=utils.catch(s)
		nick=encrypt.refract(data, key)
		
		myNick = encrypt.encrypt(localData.nickName,key)
		utils.throw(s, myNick)
		#print('sending connected nodes')
		master, connections = IPDatabase.currentConnections()
		s.send(utils.formatNumber(len(connections)).encode())
		for ip in connections:
			element = encrypt.encrypt( connections[ip], key)
			utils.throw(s , element)
		
		IPDatabase.addIP(addr[0], key, nick)
		
	except(socket.error):
		pass