class LoopixClient(DatagramProtocol): jsonReader = JSONReader( os.path.join(os.path.dirname(__file__), 'config.json')) config_params = jsonReader.get_client_config_params() output_buffer = Queue() process_queue = ProcessQueue() reactor = reactor resolvedAdrs = {} def __init__(self, sec_params, name, port, host, provider_id, privk=None, pubk=None): self.name = name self.port = port self.host = host self.privk = privk or sec_params.group.G.order().random() self.pubk = pubk or (self.privk * sec_params.group.G.generator()) self.crypto_client = ClientCore((sec_params, self.config_params), self.name, self.port, self.host, self.privk, self.pubk) self.provider = Provider(name=provider_id) self.latency_file = open('latency.csv', 'a') def startProtocol(self): log.msg("[%s] > Started" % self.name) self.get_network_info() self.get_provider_data() self.subscribe_to_provider() self.turn_on_packet_processing() self.make_loop_stream() self.make_drop_stream() self.make_real_stream() def get_network_info(self): self.dbManager = DatabaseManager(self.config_params.DATABASE_NAME) self.register_mixes(self.dbManager.select_all_mixnodes()) self.register_providers(self.dbManager.select_all_providers()) self.register_friends(self.dbManager.select_all_clients()) self.provider = self.dbManager.select_provider_by_name( self.provider.name) log.msg("[%s] > Registered network information" % self.name) def get_provider_data(self): self.provider = self.dbManager.select_provider_by_name( self.provider.name) try: result = socket.gethostbyname(self.provider.host) self.provider = self.provider._replace(host=result) except socket.gaierror, err: print(err)
def main(): envPathLoader.load_env_paths() print('path:', VIDEO_LIST_PATH) video_data = jsonR.read_from_file(VIDEO_LIST_PATH) print(video_data) for group in video_data['groups']: print(group) print(group['group_video_list']) mp3Downloader.download_videos(directory_path="./songs/" + group['group_name'], video_list=group['group_video_list'])
class LoopixProvider(LoopixMixNode): jsonReader = JSONReader( os.path.join(os.path.dirname(__file__), 'config.json')) config_params = jsonReader.get_provider_config_params() storage_inbox = {} clients = {} def __init__(self, sec_params, name, port, host, privk=None, pubk=None): LoopixMixNode.__init__(self, sec_params, name, port, host, privk, pubk) self.privk = privk or sec_params.group.G.order().random() self.pubk = pubk or (self.privk * sec_params.group.G.generator()) self.crypto_node = ProviderCore((sec_params, self.config_params), self.name, self.port, self.host, self.privk, self.pubk) def subscribe_client(self, client_data): subscribe_key, subscribe_host, subscribe_port = client_data self.clients[subscribe_key] = (subscribe_host, subscribe_port) print "[%s] > Subscribed client" % self.name def read_packet(self, packet): try: decoded_packet = petlib.pack.decode(packet) if decoded_packet[0] == 'SUBSCRIBE': self.subscribe_client(decoded_packet[1:]) elif decoded_packet[0] == 'PULL': pulled_messages = self.pull_messages( client_id=decoded_packet[1]) #map(lambda (packet, addr): self.send(packet, addr), # zip(pulled_messages, itertools.repeat(self.clients[decoded_packet[1]]))) else: flag, decrypted_packet = self.crypto_node.process_packet( decoded_packet) if flag == "ROUT": delay, new_header, new_body, next_addr, next_name = decrypted_packet if self.is_assigned_client(next_name): self.send((new_header, new_body), self.clients[next_name]) #self.put_into_storage(next_name, (new_header, new_body)) else: self.reactor.callFromThread(self.send_or_delay, delay, (new_header, new_body), next_addr) elif flag == "LOOP": print "[%s] > Received loop message" % self.name elif flag == "DROP": print "[%s] > Received drop message" % self.name except Exception, exp: print "ERROR: ", str(exp)
from operator import attrgetter import itertools import math import os import numpy from sphinxmix.SphinxNode import sphinx_process from sphinxmix.SphinxClient import PFdecode, Nenc, create_forward_message, receive_forward from json_reader import JSONReader from petlib.ec import EcGroup jsonReader = JSONReader(os.path.join(os.path.dirname(__file__), 'config.json')) config = jsonReader.get_client_config_params() def setup(): ''' Setup the parameters of the mix crypto-system ''' group = EcGroup() order = group.order() generator = group.generator() o_bytes = int(math.ceil(math.log(float(int(order))) / math.log(256))) return group, order, generator, o_bytes def sample_from_exponential(lambda_param): return numpy.random.exponential(lambda_param, size=None) def generate_random_string(length): return numpy.random.bytes(length)
class LoopixMixNode(DatagramProtocol): jsonReader = JSONReader( os.path.join(os.path.dirname(__file__), 'config.json')) config_params = jsonReader.get_mixnode_config_params() reactor = reactor process_queue = ProcessQueue() resolvedAdrs = {} def __init__(self, sec_params, name, port, host, group, privk=None, pubk=None): self.name = name self.port = port self.host = host self.group = group self.privk = privk or sec_params.group.G.order().random() self.pubk = pubk or (self.privk * sec_params.group.G.generator()) self.crypto_node = MixCore((sec_params, self.config_params), self.name, self.port, self.host, self.privk, self.pubk) def startProtocol(self): print "[%s] > Started" % self.name self.get_network_info() self.turn_on_processing() self.make_loop_stream() def get_network_info(self): self.dbManager = DatabaseManager(self.config_params.DATABASE_NAME) mixes = self.dbManager.select_all_mixnodes() providers = self.dbManager.select_all_providers() self.register_mixes([m for m in mixes if not m.name == self.name]) self.register_providers( [p for p in providers if not p.name == self.name]) def register_mixes(self, mixes): self.pubs_mixes = group_layered_topology(mixes) def register_providers(self, providers): self.pubs_providers = providers def turn_on_processing(self): self.reactor.callLater(20.0, self.get_and_addCallback, self.handle_packet) def get_and_addCallback(self, function): self.process_queue.get().addCallback(function) def make_loop_stream(self): self.send_loop_message() self.schedule_next_call(self.config_params.EXP_PARAMS_LOOPS, self.make_loop_stream) def send_loop_message(self): path = self.generate_random_path() packet = self.crypto_node.create_loop_message(path) addr = (path[0].host, path[0].port) self.send(packet, addr) def generate_random_path(self): return self.construct_full_path() def construct_full_path(self): sequence = [] num_all_layers = len(self.pubs_mixes) layer = self.group + 1 while layer != self.group: mix = random.choice(self.pubs_mixes[layer % num_all_layers]) sequence.append(mix) layer = (layer + 1) % num_all_layers sequence.insert(num_all_layers - 1 - self.group, random.choice(self.pubs_providers)) return sequence def schedule_next_call(self, param, method): interval = sample_from_exponential(param) self.reactor.callLater(interval, method) def datagramReceived(self, data, (host, port)): self.process_queue.put(data)
class LoopixClient(DatagramProtocol): jsonReader = JSONReader(os.path.join(os.path.dirname(__file__), 'config.json')) config_params = jsonReader.get_client_config_params() output_buffer = Queue() process_queue = ProcessQueue() reactor = reactor resolvedAdrs = {} def __init__(self, sec_params, name, port, host, provider_id, privk=None, pubk=None): self.name = name self.port = port self.host = host self.privk = privk or sec_params.group.G.order().random() self.pubk = pubk or (self.privk * sec_params.group.G.generator()) self.crypto_client = ClientCore((sec_params, self.config_params), self.name, self.port, self.host, self.privk, self.pubk) self.provider = Provider(name=provider_id) def startProtocol(self): log.msg("[%s] > Started" % self.name) self.get_network_info() self.get_provider_data() self.subscribe_to_provider() self.turn_on_packet_processing() self.make_loop_stream() self.make_drop_stream() self.make_real_stream() def get_network_info(self): self.dbManager = DatabaseManager(self.config_params.DATABASE_NAME) self.register_mixes(self.dbManager.select_all_mixnodes()) self.register_providers(self.dbManager.select_all_providers()) self.register_friends(self.dbManager.select_all_clients()) self.provider = self.dbManager.select_provider_by_name(self.provider.name) log.msg("[%s] > Registered network information" % self.name) def get_provider_data(self): self.provider = self.dbManager.select_provider_by_name(self.provider.name) d = twisted.names.client.getHostByName(self.provider.host) d.addCallback(self.resolve_provider_address) def resolve_provider_address(self, result): self.provider = self.provider._replace(host = result) def subscribe_to_provider(self): lc = task.LoopingCall(self.send, ['SUBSCRIBE', self.name, self.host, self.port]) lc.start(self.config_params.TIME_PULL, now=True) def register_mixes(self, mixes): self.pubs_mixes = group_layered_topology(mixes) def register_providers(self, providers): self.pubs_providers = providers def register_friends(self, clients): self.befriended_clients = clients def turn_on_packet_processing(self): self.retrieve_messages() self.reactor.callLater(20.0, self.get_and_addCallback, self.handle_packet) log.msg("[%s] > Turned on retrieving and processing of messages" % self.name) def retrieve_messages(self): lc = task.LoopingCall(self.send, ['PULL', self.name]) lc.start(self.config_params.TIME_PULL, now=True) def get_and_addCallback(self, function): self.process_queue.get().addCallback(function) def datagramReceived(self, data, (host, port)): self.process_queue.put(data)
class LoopixClient(DatagramProtocol): jsonReader = JSONReader( os.path.join(os.path.dirname(__file__), 'config.json')) config_params = jsonReader.get_client_config_params() output_buffer = Queue() process_queue = ProcessQueue() reactor = reactor resolvedAdrs = {} number_of_chunks = 4 #depends on the quality/size of frame buffering = 200 #equals to pull size for simplicity on metrics stream_length = 1000000 #to chnage this consider adding more space to the body. 1 space for every digit stream_buffer = [[None for x in range(number_of_chunks)] for y in range(stream_length)] #hashtable for chunks chunk_count = [0] * stream_length #hashtable to know a frame is full frames_received = 0 frames_played = 0 playing = False video_ended = False def __init__(self, sec_params, name, port, host, provider_id, privk=None, pubk=None): self.name = name self.port = port self.host = host self.privk = privk or sec_params.group.G.order().random() self.pubk = pubk or (self.privk * sec_params.group.G.generator()) self.crypto_client = ClientCore((sec_params, self.config_params), self.name, self.port, self.host, self.privk, self.pubk) self.provider = Provider(name=provider_id) def startProtocol(self): log.msg("[%s] > Started" % self.name) self.get_network_info() self.get_provider_data() self.subscribe_to_provider() self.turn_on_packet_processing() #self.make_loop_stream() #self.make_drop_stream() self.make_real_stream() if self.name != 'Client1': self.send_video('../../example3.mp4', self.befriended_clients[0]) def get_network_info(self): self.dbManager = DatabaseManager(self.config_params.DATABASE_NAME) self.register_mixes(self.dbManager.select_all_mixnodes()) self.register_providers(self.dbManager.select_all_providers()) self.register_friends(self.dbManager.select_all_clients()) self.provider = self.dbManager.select_provider_by_name( self.provider.name) log.msg("[%s] > Registered network information" % self.name) def get_provider_data(self): self.provider = self.dbManager.select_provider_by_name( self.provider.name) d = twisted.names.client.getHostByName(self.provider.host) d.addCallback(self.resolve_provider_address) def resolve_provider_address(self, result): self.provider = self.provider._replace(host=result) def subscribe_to_provider(self): lc = task.LoopingCall(self.send, ['SUBSCRIBE', self.name, self.host, self.port]) lc.start(self.config_params.TIME_PULL, now=True) def register_mixes(self, mixes): self.pubs_mixes = group_layered_topology(mixes) def register_providers(self, providers): self.pubs_providers = providers def register_friends(self, clients): self.befriended_clients = clients def turn_on_packet_processing(self): self.retrieve_messages() self.reactor.callLater(20.0, self.get_and_addCallback, self.handle_packet) log.msg("[%s] > Turned on retrieving and processing of messages" % self.name) def retrieve_messages(self): lc = task.LoopingCall(self.send, ['PULL', self.name]) lc.start(self.config_params.TIME_PULL, now=True) def get_and_addCallback(self, function): self.process_queue.get().addCallback(function) def datagramReceived(self, data, (host, port)): self.process_queue.put(data)
def __init__(self, data_directory, schema_directory, logger, reader=JSONReader()): self.data_directory = data_directory self.schemes = Schema.load_schemes(schema_directory) self.logger = logger self.reader = reader