-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
371 lines (296 loc) · 12.5 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import eventlet
eventlet.monkey_patch(thread=True, time=True)
from flask import Flask, render_template, jsonify, request, send_from_directory, send_file
from flask_socketio import SocketIO
from flask_cors import CORS
import os
from apscheduler.schedulers.background import BackgroundScheduler
from bitarray import bitarray
import base64
import json
import time
from datetime import datetime
from contextlib import contextmanager
MAX_LOGS_PER_DAY = 400_000_000
TOTAL_CHECKBOXES = 1_000_000
REACT_BUILD_DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), 'dist'))
# I found this by portscanning my own VPC because the DNS record wouldn't work lmfao
REDIS_REPLICA_IP="10.108.0.13"
app = Flask(__name__, static_folder=REACT_BUILD_DIRECTORY)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
scheduler = BackgroundScheduler()
# Configuration
USE_REDIS = os.environ.get('USE_REDIS', 'false').lower() == 'true'
class RedisRateLimiter:
def __init__(self, pool, limit, window):
self.pool = pool
self.limit = limit
self.window = window
def is_allowed(self, key: str) -> bool:
with get_redis_connection(self.pool) as redis_client:
pipe = redis_client.pipeline()
now = int(time.time())
key = f'rate_limit:{key}:{self.window}' # Include window in the key
pipe.zadd(key, {now: now})
pipe.zremrangebyscore(key, 0, now - self.window)
pipe.zcard(key)
pipe.expire(key, self.window)
_, _, count, _ = pipe.execute()
return count <= self.limit
if USE_REDIS:
import redis
from redis import ConnectionPool
# Create a connection pool
pool = ConnectionPool(
host=os.environ.get('REDIS_HOST', 'localhost'),
port=int(os.environ.get('REDIS_PORT', 6379)),
username=os.environ.get('REDIS_USERNAME', 'default'),
password=os.environ.get('REDIS_PASSWORD', ''),
db=0,
connection_class=redis.SSLConnection,
max_connections=425 # Adjust this number based on your needs
)
replica_pool = ConnectionPool(
host=REDIS_REPLICA_IP,
port=int(os.environ.get('REDIS_PORT', 6379)),
username=os.environ.get('REDIS_USERNAME', 'default'),
password=os.environ.get('REDIS_PASSWORD', ''),
db=0,
connection_class=redis.SSLConnection,
max_connections=425 # Adjust this number based on your needs
)
@contextmanager
def get_redis_connection(pool):
connection = redis.Redis(connection_pool=pool)
try:
yield connection
finally:
connection.close()
# redis_client = redis.Redis(connection_pool=pool)
# print("connected to redis")
# replica_client = redis.Redis(connection_pool=replica_pool)
# print("connected to replica")
def initialize_redis():
with get_redis_connection(pool) as redis_client:
if not redis_client.exists('truncated_bitset'):
redis_client.set('truncated_bitset', b'\x00' * (TOTAL_CHECKBOXES // 8))
if not redis_client.exists('count'):
redis_client.set('count', '0')
initialize_redis()
# pubsub = replica_client.pubsub(ignore_subscribe_messages=True)
pubsub = redis.Redis(connection_pool=replica_pool).pubsub(ignore_subscribe_messages=True)
pubsub.subscribe('bit_toggle_channel')
# Lua script for atomic bit setting and count update
set_bit_script = """
local key = KEYS[1]
local index = tonumber(ARGV[1])
local value = tonumber(ARGV[2])
local current = redis.call('getbit', key, index)
local diff = value - current
redis.call('setbit', key, index, value)
redis.call('incrby', 'count', diff)
return diff"""
new_set_bit_script="""
local key = KEYS[1]
local count_key = KEYS[2]
local index = tonumber(ARGV[1])
local max_count = tonumber(ARGV[2])
local current_count = tonumber(redis.call('get', count_key) or "0")
if current_count >= max_count then
return {redis.call('getbit', key, index), 0} -- Return current count, current bit value, and 0 to indicate no change
end
local current_bit = redis.call('getbit', key, index)
local new_bit = 1 - current_bit -- Toggle the bit
local diff = new_bit - current_bit
if diff > 0 and current_count + diff > max_count then
return { current_bit, 0} -- Return current count, current bit value, and 0 to indicate no change
end
redis.call('setbit', key, index, new_bit)
local new_count = current_count + diff
redis.call('set', count_key, new_count)
return {new_bit, diff} -- new bit value, and the change (1, 0, or -1)"""
# set_bit_sha = redis_client.script_load(set_bit_script)
# new_set_bit_sha = redis_client.script_load(new_set_bit_script)
with get_redis_connection(pool) as redis_client:
new_set_bit_sha = redis_client.script_load(new_set_bit_script)
def get_bit(index):
with get_redis_connection(pool) as redis_client:
return bool(redis_client.getbit('truncated_bitset', index))
def set_bit(index, value):
with get_redis_connection(pool) as redis_client:
[_count, diff] = redis_client.evalsha(new_set_bit_sha, 1, 'truncated_bitset', index, int(value))
return diff != 0
def _toggle_internal(index):
with get_redis_connection(pool) as redis_client:
result = redis_client.evalsha(
new_set_bit_sha,
2, # number of keys
'truncated_bitset', # key for bitset
'count', # key for count
index, # index to toggle
TOTAL_CHECKBOXES # max count
)
new_bit_value, diff = result
if diff == 0:
return [False, None]
return [True, new_bit_value]
def get_full_state():
with get_redis_connection(replica_pool) as replica_client:
raw_data = replica_client.get("truncated_bitset")
return base64.b64encode(raw_data).decode('utf-8')
def get_count():
with get_redis_connection(replica_pool) as replica_client:
return int(replica_client.get('count') or 0)
def emit_toggle(index, new_value, timestamp):
with get_redis_connection(pool) as redis_client:
redis_client.publish('bit_toggle_channel', json.dumps([index, new_value, timestamp]))
one_second_limiter = RedisRateLimiter(pool, limit=7, window=1)
fifteen_second_limiter = RedisRateLimiter(pool, limit=80, window=15)
sixty_second_limiter = RedisRateLimiter(pool, limit=240, window=60)
connection_limiter = RedisRateLimiter(pool, limit=20, window=15)
limiters = [one_second_limiter, fifteen_second_limiter, sixty_second_limiter]
def allow_toggle(key):
return all(limiter.is_allowed(key) for limiter in limiters)
def allow_connection(key):
return connection_limiter.is_allowed(key)
def log_checkbox_toggle(remote_ip, checkbox_index, checked_state):
timestamp = datetime.now().isoformat()
log_entry = f"{timestamp}|{remote_ip}|{checkbox_index}|{checked_state}"
# Use the current date as part of the key
key = f"checkbox_logs:{datetime.now().strftime('%Y-%m-%d')}"
with get_redis_connection(pool) as redis_client:
pipeline = redis_client.pipeline()
pipeline.rpush(key, log_entry)
pipeline.ltrim(key, 0, MAX_LOGS_PER_DAY - 1)
pipeline.execute()
else:
# In-memory storage
in_memory_storage = {'bitset': bitarray('0' * TOTAL_CHECKBOXES), 'count': 0}
def get_bit(index):
return bool(in_memory_storage['bitset'][index])
def set_bit(index, value):
current = in_memory_storage['bitset'][index]
count = in_memory_storage['count']
if count >= TOTAL_CHECKBOXES:
return False
in_memory_storage['bitset'][index] = value
in_memory_storage['count'] += value - current
return True
def _toggle_internal(index):
print("here")
current = in_memory_storage['bitset'][index]
count = in_memory_storage['count']
if count >= TOTAL_CHECKBOXES:
return [False, None]
new_value = not current
in_memory_storage['bitset'][index] = new_value
in_memory_storage['count'] += 1 if new_value else -1
return [True, new_value]
def get_full_state():
return base64.b64encode(in_memory_storage['bitset'].tobytes()).decode('utf-8')
def get_count():
return in_memory_storage['count']
def emit_toggle(index, new_value, timestamp):
update = [[index], [], timestamp] if new_value else [[], [index], timestamp]
socketio.emit('batched_bit_toggles', update)
limiters = []
def allow_toggle(key):
return True
def allow_connection(key):
return True
def log_checkbox_toggle(remote_ip, checkbox_index, checked_state):
pass
def state_snapshot():
full_state = get_full_state()
count = get_count()
timestamp = int(time.time() * 1000) # Current time in milliseconds
return {'full_state': full_state, 'count': count, "timestamp": timestamp}
@app.route('/api/initial-state')
def get_initial_state():
return jsonify(state_snapshot())
def emit_full_state():
print("Emitting full state")
socketio.emit('full_state', state_snapshot())
@socketio.on('toggle_bit')
def handle_toggle(data):
if not allow_toggle(request.sid):
print(f"Rate limiting toggle request for {request.sid}")
return False
try:
index = int(data['index'])
except:
return False
if index >= TOTAL_CHECKBOXES:
return False
did_toggle, new_value = _toggle_internal(index)
timestamp = int(time.time() * 1000) # Current time in milliseconds
if did_toggle != 0:
forwarded_for = request.headers.get('X-Forwarded-For') or "UNKNOWN_IP"
log_checkbox_toggle(forwarded_for, index, new_value)
emit_toggle(index, new_value, timestamp)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
if path != "" and os.path.exists(os.path.join(app.static_folder, path)):
return send_from_directory(app.static_folder, path)
else:
return send_file(os.path.join(app.static_folder, 'index.html'))
#@socketio.on('connect')
#def handle_connect():
#forwarded_for = request.headers.get('X-Forwarded-For') or request.remote_addr
#if forwarded_for and allow_connection(forwarded_for):
#return True
#else:
#print(f"Rate limiting connection for {forwarded_for}")
#return False
def emit_state_updates():
scheduler.add_job(emit_full_state, 'interval', seconds=45)
scheduler.start()
emit_state_updates()
def handle_redis_messages():
if USE_REDIS:
message_count = 0
updates = []
while True:
message = pubsub.get_message(timeout=0.01)
if message is None:
# No more messages available
break
if message['type'] == 'message':
try:
data = json.loads(message['data'])
updates.append(data)
message_count += 1
except json.JSONDecodeError:
print(f"Failed to decode message: {message['data']}")
if message_count >= 600:
break
if message_count > 0:
true_updates = []
false_updates = []
max_timestamp = 0
for update in updates:
if len(update) != 3: # backwards compatibility
continue
else:
index, value, timestamp = update
max_timestamp = max(max_timestamp, timestamp)
if value:
true_updates.append(index)
else:
false_updates.append(index)
to_broadcast = [true_updates, false_updates, max_timestamp]
socketio.emit('batched_bit_toggles', to_broadcast)
# print(f"Processed {message_count} messages")
def setup_redis_listener():
if USE_REDIS:
print("Redis listener job added to scheduler")
scheduler.add_job(handle_redis_messages, 'interval', seconds=0.2)
setup_redis_listener()
if __name__ == '__main__':
set_bit(0, True)
set_bit(1, True)
set_bit(100, True)
set_bit(101, True)
socketio.run(app, host="0.0.0.0", port=5001)