コード例 #1
0
ファイル: user.py プロジェクト: rlguarino/PyShare
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 []
コード例 #2
0
ファイル: user.py プロジェクト: rlguarino/PyShare
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)
コード例 #3
0
ファイル: host.py プロジェクト: rlguarino/PyShare
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
コード例 #4
0
ファイル: host.py プロジェクト: rlguarino/PyShare
def main(IPDatabase, localData):
	"""
	The main loop of the host thread
	"""
	print("STARTING HOST SERVICES")
	HOST= ''
	PORT= 44450
	#socket.setdefaulttimeout(1)
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind((HOST, PORT))
	while True:
		#print('SERVER>> listening')
		s.listen(1)
		conn, addr = s.accept()
		#print('SERVER>> accepted')
		data=utils.catch(conn)
		
		if localData.kill == True:
			s.close()
			return
		#print('SERVER>> received : '+ str(data))
		if(data=='-0'): #new connection
			handleConnection(IPDatabase, localData, conn, addr)
		elif(data=='-1'): #disconnect
			handleDisconnect(IPDatabase, addr)
		elif(data=='-2'): #search
			#routing.route(IPDatabse, localData, conn, addr)
			handleSearch(IPDatabase, localData, conn, addr)
		elif(data == '-3'): #download
			threading.Thread(target=handleDownload, name='Host Thread', args=(IPDatabase, conn, addr)).start()
コード例 #5
0
ファイル: host.py プロジェクト: rlguarino/PyShare
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
コード例 #6
0
ファイル: user.py プロジェクト: rlguarino/PyShare
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 []
コード例 #7
0
ファイル: host.py プロジェクト: rlguarino/PyShare
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
コード例 #8
0
ファイル: FTS.py プロジェクト: rlguarino/PyShare
def recvFile(localData, download, s, fileName, key):
	try:
		location = localData.downloadLocation
	
		f=open(location+fileName, 'wb')
		packetSize =1024
		segments = int(utils.catch(s))
		download.setSize(segments)
		perHash = segments//40
		index =0
		while index < segments :
			if index%perHash == 0:
				download.updateBar()
				f.close()
				f=open(location+fileName, 'ab')
			data = s.recv(packetSize)
			i=0
			while len(data) != packetSize:
				if index == segments-1:
					break
				if i>1000:
					print('Download not finished')
					return
				temp = s.recv(packetSize-len(data))
				data = data+temp
				i=+1
		
			f.write(data)
			download.update()
			index += 1
		f.close()
		localData.finishDownload(download.ID)
		print("Download Finished!")
		
	except(socket.error):
		return