def refresh_captcha_cooldown(): if app.config.get("CAPTCHA_COOLDOWN"): cooldown_id = session.get("cooldown_id") if cooldown_id is None: cooldown_id = _gen_cooldown_id() current_timestamp = str(int(time.time())) key_client = keystore.Keystore() key_client.set(cooldown_id, current_timestamp) session["cooldown_id"] = cooldown_id
def client_pump(): client_id = _gen_client_id() new_client_message = SSEMessage(client_id, NEW_CLIENT_EVENT) keystore_client = keystore.Keystore() keystore_client.set(client_id, CLIENT_JSON_TEMPLATE) yield new_client_message.encode() pubsub = keystore.Pubsub() pubsub.subscribe("new-post") pubsub.subscribe("new-thread") pubsub.subscribe("new-reply") sub_queue = gevent.queue.Queue() gevent.spawn( functools.partial(_aggregate_channel, pubsub, sub_queue, "new-post")) gevent.spawn( functools.partial(_aggregate_channel, pubsub, sub_queue, "new-thread")) gevent.spawn( functools.partial(_aggregate_channel, pubsub, sub_queue, "new-reply")) try: while True: try: channel, raw_message = sub_queue.get(timeout=30) except gevent.queue.Empty: # send a heartbeat every 30 seconds if we don't get any new updates # to keep the reverse proxy from closing the connection ping_message = SSEMessage("", "heartbeat") yield ping_message.encode() continue message = json.loads(raw_message) client_filter = json.loads(keystore_client.get(client_id)) if channel == "new-post": subscribed_threads = client_filter["threads"] if message["thread"] in subscribed_threads: new_post_message = SSEMessage(raw_message, "new-post") yield new_post_message.encode() elif channel == "new-thread": subscribed_boards = client_filter["boards"] if message["board"] in subscribed_boards: new_thread_message = SSEMessage( raw_message, "new-thread") yield new_thread_message.encode() elif channel == "new-reply": reply_to = message["reply_to"] reply_post = Post.query.get(reply_to) subscribed_threads = client_filter["threads"] for thread_id in subscribed_threads: if reply_post.thread == thread_id: new_reply_message = SSEMessage( raw_message, "new-reply") yield new_reply_message.encode() except GeneratorExit: # client closed connection keystore_client.delete(client_id)
def on_captcha_cooldown(): captcha_cooldown = False cooldown_id = session.get("cooldown_id") if app.config.get("CAPTCHA_COOLDOWN") and cooldown_id: key_client = keystore.Keystore() if key_client.exists(cooldown_id): current_timestamp = int(time.time()) cooldown_timestamp = int(key_client.get(cooldown_id)) if current_timestamp - cooldown_timestamp < app.config[ "CAPTCHA_COOLDOWN"]: captcha_cooldown = True return captcha_cooldown
def update_firehose(): client_request = request.get_json() client_id = client_request["client-id"] keystore_client = keystore.Keystore() sub_state = json.loads(keystore_client.get(client_id)) if "subscribe" in client_request: if "thread" in client_request["subscribe"]: current_subscribed_threads = sub_state["threads"] new_threads = client_request["subscribe"]["thread"] for thread_id in new_threads: current_subscribed_threads.append(thread_id) sub_state["threads"] = current_subscribed_threads if "board" in client_request["subscribe"]: current_subscribed_boards = sub_state["boards"] new_boards = client_request["subscribe"]["board"] for board_id in new_boards: current_subscribed_boards.append(board_id) sub_state["boards"] = current_subscribed_boards if "unsubscribe" in client_request: pass # TODO new_sub_state = json.dumps(sub_state) keystore_client.set(client_id, new_sub_state) return jsonify(success=True)
def __init__(self): self._keystore = keystore.Keystore()
def JSONDecode(o): if o['_class'] == 'KBucket': bucket = KBucket(o['prefix']) for x in o['peers']: bucket.append(x) return bucket else: return peer.Peer.JSONDecode(o) if __name__ == '__main__': import keystore import peer import json bucket = KBucket('') for x in range(20): d = keystore.Keystore() p = peer.Peer(d.addr,d.pk) bucket.append(p) print(len(bucket)) s = json.dumps(bucket, indent = 4, default = KBucket.JSONEncode) # print(s) # b = json.loads(s,object_hook = KBucket.JSONDecode) print(vars(b)) # p = json.dumps(bucket.getK(10),indent = 4,default = KBucketJSONEncode) # print(p) #R = bucket.split() #print(json.dumps(bucket,indent = 4, default = KBucketJSONEncode)) #print('---------------------------------------------------------') #print(json.dumps(R,indent = 4, default = KBucketJSONEncode))