def __init__(self): """ Setup and build the bunny model and starts the read_packet_thread() """ self.inandout = SendRec() self.cryptor = AEScrypt() self.model = TrafficModel() # each item should be an full bunny message that can be passed to the .decrypt() method # TODO: put a upper bound of number of messages or a cleanup thread to clear out old messages # if not consumed. self.msg_queue = Queue.LifoQueue() # The out queue is a FiFo Queue because it maintaines the ordering of the bunny data # format: [data, Bool (relay or not)] self.out_queue = Queue.Queue() # The Deque is used because it is a thread safe iterable that can be filled with 'seen' # messages between the send and recv threads. self.msg_deque = [] # init the threads and name them self.workers = [BunnyReadThread(self.msg_queue, self.out_queue, self.inandout, self.model, self.cryptor), \ BroadCaster(self.out_queue, self.inandout, self.model)] self.workers[0].name = "BunnyReadThread" self.workers[1].name = "BroadCasterThread" # spin up the threads for worker in self.workers: worker.daemon = True worker.start()
def learnAPI(AuthToken, progress, memberSeq, lectureLearningSeq, schoolCode, key, IV, host): debug = readCfg()["debug"] import AEScrypt assembledData = AEScrypt.dataAssembly(str(memberSeq), str(lectureLearningSeq), str(progress)) cryptData = AEScrypt.encrypt(key, IV, assembledData) headers = { "User-Agent": userAgent, "X-AUTH-TOKEN": AuthToken, "Content-Type": "application/json;charset=UTF-8" } payload = { "encriptedProgressRate": cryptData, } payload = json.dumps(payload) data = requests.post( f"https://" + host + f".ebsoc.co.kr/lecture/api/v1/student/learning/{lectureLearningSeq}/{schoolCode}/progress", headers=headers, data=payload) if debug == "yes": print(data.content.decode("utf-8")) print(assembledData) print(cryptData) jsonData = json.loads(data.content.decode("utf-8")) return jsonData
class Bunny: """ High level send and receive for wrapping all the lower-level functions of bunny in paranoid mode. """ def __init__(self): """ Setup and build the bunny model and starts the read_packet_thread() """ self.inandout = SendRec() self.cryptor = AEScrypt() self.model = TrafficModel() # each item should be an full bunny message that can be passed to the .decrypt() method # TODO: put a upper bound of number of messages or a cleanup thread to clear out old messages # if not consumed. self.msg_queue = Queue.LifoQueue() # The out queue is a FiFo Queue because it maintaines the ordering of the bunny data # format: [data, Bool (relay or not)] self.out_queue = Queue.Queue() # The Deque is used because it is a thread safe iterable that can be filled with 'seen' # messages between the send and recv threads. self.msg_deque = [] # init the threads and name them self.workers = [BunnyReadThread(self.msg_queue, self.out_queue, self.inandout, self.model, self.cryptor), \ BroadCaster(self.out_queue, self.inandout, self.model)] self.workers[0].name = "BunnyReadThread" self.workers[1].name = "BroadCasterThread" # spin up the threads for worker in self.workers: worker.daemon = True worker.start() #TODO: can I add a 'isAlive()' checking loop here? def sendBunny(self, packet): """ Send a Bunny (paranoid) packet """ packet = self.cryptor.encrypt(packet) # Prepend the length of the packet as the first two bytes. # This allows for Bunny to know when to stop reading in packets. size = struct.pack("H", len(packet)) packet = "%s%s" % (size, packet) self.msg_deque.append([packet, time.time()]) self.out_queue.put([packet, False]) def recvBunny(self, timer=False): """ Grab the next bunny message in the queue and decrypt it and return the plaintext message Arg: timer If not false, bunny will timeout in the number of seconds in timer Returns: Decrypted bunny message or if timedout, False """ # this is looped just so if the message has been seen we can come back and keep trying. while True: relay = False if timer: try: data = self.msg_queue.get(True, timer) except Queue.Empty: return False else: data = self.msg_queue.get() # check if the message has already been seen # TODO: move this whole thing to a new thread cur_time = time.time() for message in self.msg_deque: if message[0] == data: if DEBUG: print "Already seen message, not sending to user" relay = True # remove old known messages if cur_time - message[1] > 60: self.msg_deque.remove(message) if relay == True: continue else: self.out_queue.put([data, True]) self.msg_deque.append([data, time.time()]) # remove the size data: data = data[2:] plaintext = self.cryptor.decrypt(data) if plaintext == False: continue else: return plaintext def killBunny(self): for worker in self.workers: worker.kill()