Ejemplo n.º 1
0
	def doauth(self,serverid):
		conn = secureconnection(authhost,authport)
		conn.sendmsg(self.username, self.password,'HEAD', '/', str(serverid)+"\r\nnokey")
		token = conn.recvmsg()
		conn.close()
		ticket, sessionkey, c = self.decrypttoken(token,password)
		return ticket, sessionkey
Ejemplo n.º 2
0
	def __init__(self, authhost, authport):
			username = '******'
			password = '******'
			self.numreplicas = 2 # number of replicas each file should have
		#register with auth server
			#generate key
			key = RSA.generate(1024)
			self.key = key.exportKey()
			msg = str(key)+'\r\n'
			
			#setup socket and get my port
			self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			self.s.bind(('', 0))
			self.port = self.s.getsockname()[1]
			self.host = socket.getfqdn()
			
			#formulate the registration request 
			request = self.host +'\r\n'+str(self.port)+'\r\n'+self.key

			#send data
			conn = secureconnection(authhost,authport)
			conn.sendmsg(username, password,'GET', '/', '0'+"\r\n"+request)
			token = conn.recvmsg()
			conn.close()
			
			#record assigned server id
			self.serverid = self.decrypttoken(token,password)
			print "my sid:", self.serverid
			
			#start a thread to look out for dead servers
			thread.start_new_thread(self.removedeadservers, ())
Ejemplo n.º 3
0
	def write(self, filePath, contents):
		
		#get token from Auth server
		conn = secureconnection(self.ahost, self.aport)
		conn.sendmsg(self.user, self.passwd,'POST', filePath, "0\r\nnokey")
		resp = conn.recvmsg()
		conn.close()
		
		#check ok
		code , a, b, c = self.parsehttp(resp)
		if code == 302:
			#extract details from token
			ticket, sessionkey, serverid = self.decrypttoken(resp)
			
			#make request to dir server
			code, dhost, dport, fserverid = self.parsehttp(resp)
			conn2 = secureconnection(dhost, dport)
			conn2.sendmsg(self.user, self.passwd,'POST', filePath, ticket+"\r\nnokey")
			resp = conn2.recvmsg()
			conn2.close()

			#make request to file server
			code, fhost, fport, fserverid = self.parsehttp(resp)
			print 'code: ', code
			if code == 302:
			#Authorize request
				#get token from Auth server
				conn = secureconnection(self.ahost, self.aport)
				conn.sendmsg(self.user, self.passwd,'POST', filePath, fserverid+"\r\nnokey")
				token = conn.recvmsg()
				conn.close()

				#extract ticket
				ticket, sessionkey, serverid = self.decrypttoken(token)

				#Make request
				conn2 = secureconnection(fhost, fport)
				conn2.sendmsg(self.user, self.passwd,'POST', filePath, ticket+contents)
				resp = conn2.recvmsg()
				conn2.close()
				
			return (self.parsehttp(resp)[3])[:-2]
		else:
			return "http error:" + str(code)
Ejemplo n.º 4
0
	def removedeadservers(self):
			while True:
				time.sleep(2)
				servers = dirservice.allservers[1:]
				for server in servers:
					host, port = server[1].split(':')[:2]
	#				try:
					conn2 = secureconnection(host, int(port))
					conn2.sendmsg(self.user, self.passwd,'TEST', '#','none')
					resp = conn2.recvmsg()
					conn2.close()
Ejemplo n.º 5
0
	def __init__(self, username, password, authhost, authport, rootdir):

		#setup
			#save some args			
			self.rootdir = rootdir
			self.username = username
			self.passwd = password

			#setup socket and get my port
			self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			self.s.bind(('', 0))
			port = self.s.getsockname()[1]
			host = socket.getfqdn()
			self.host = host+':'+str(port)
		
		#register with auth server
			
			#generate key
			key = RSA.generate(1024)
			self.key = key.exportKey()
			msg = str(key)+'\r\n'
			
			#send data
			conn = secureconnection(authhost,authport)
			conn.sendmsg(username, password,'GET', '/', '0'+"\r\n"+self.key)
			token = conn.recvmsg()
			conn.close()
			ticket, self.sessionkey, self.serverid = self.decrypttoken(token,password)
			self.dirhost, self.dirport = self.getdirloc(token)

			print "my sid:", self.serverid
			
		#register with dirserver
			request = self.host+'\r\n'+self.serverid+'\r\n'+str(self.getfolders())
			conn = secureconnection(self.dirhost, int(self.dirport))
			conn.sendmsg(username, password,'GET', '/', ticket+request)
			token = conn.recvmsg()
			conn.close()
Ejemplo n.º 6
0
	def getpeers(self, path, ticket, sessionkey):
		#make request
		conn = secureconnection(self.dirhost, self.dirport)
		conn.sendmsg(self.username, self.password,'HEAD', path , ticket)
		resp = conn.recvmsg()
		conn.close()
		#extract data 
		headers = resp.split('\r\n\r\n')[0]
		respline =  headers.split('\r\n')[0]
		code = int(respline.split(' ')[1]) # response code
		body = resp.split('\r\n\r\n')[1]
		#decript body		
		decryptor = ARC4.new(sessionkey)
		body = (decryptor.decrypt(body))		
		return body
Ejemplo n.º 7
0
	def writes(self, server, path, contents):
			#get server details
			fshost = server[0:server.index(':')]
			fsport = int(server[server.index(':')+1:server.rfind(':')])
			fssid  = server[server.rfind(':')+1:] 
			#quit if for this server
			if fssid == self.getid():
				pass
			else:
				print 'writing to a replica of', path, 'sid: ', fssid
				#get token from Auth server
				ticket, sessionkey = self.doauth(fssid)
				#make request to file server
				conn2 = secureconnection(fshost, fsport)
				conn2.sendmsg(self.username, self.password,'POST', path, ticket+contents)
				resp = conn2.recvmsg()
				conn2.close()