Spaces:
Running
Running
File size: 14,768 Bytes
146624d 5027087 146624d d34c396 5027087 d34c396 5027087 146624d d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 146624d d34c396 146624d d34c396 146624d d34c396 146624d d34c396 146624d d34c396 146624d 3532d53 146624d d34c396 5027087 3532d53 5027087 3532d53 d34c396 146624d d34c396 146624d d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 3532d53 d34c396 146624d d34c396 146624d 5027087 d34c396 146624d 3532d53 1d78341 d34c396 5027087 |
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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
import asyncio
import aiohttp
import time
from datetime import datetime
from collections import deque, defaultdict
import json
import secrets
from aiohttp import web
class StreamManager:
def __init__(self):
self.streams = {} # Dictionary to store all stream instances
self.tokens = {} # Dictionary to store stream tokens
self.tasks = {} # Dictionary to store stream tasks
async def create_stream(self, retention_seconds):
# Generate unique stream ID and token
stream_id = secrets.token_urlsafe(8)
access_token = secrets.token_urlsafe(16)
# Create new stream instance
stream = EphemeralStreamReader(
path=stream_id,
retention_seconds=retention_seconds,
show_output=True
)
self.streams[stream_id] = stream
self.tokens[stream_id] = access_token
# Start the stream reader task
self.tasks[stream_id] = asyncio.create_task(stream.start_reading())
return stream_id, access_token
def get_stream(self, stream_id, token):
if stream_id in self.streams and self.tokens.get(stream_id) == token:
return self.streams[stream_id]
return None
async def cleanup_inactive_streams(self):
while True:
current_time = time.time()
inactive_streams = []
for stream_id, stream in self.streams.items():
# Clean up streams that haven't been accessed in 1 hour
if current_time - stream.last_access > 3600:
inactive_streams.append(stream_id)
for stream_id in inactive_streams:
if stream_id in self.tasks:
self.tasks[stream_id].cancel()
del self.tasks[stream_id]
del self.streams[stream_id]
del self.tokens[stream_id]
await asyncio.sleep(300) # Check every 5 minutes
class EphemeralStreamReader:
def __init__(self, piping_server_url="https://ppng.io", path="test123", retention_seconds=3600, show_output=False):
self.url = f"{piping_server_url}/{path}"
self.path = path
self.reconnect_delay = 1
self.show_output = show_output
self.retention_seconds = min(3600, max(0, retention_seconds)) # Limit between 0 and 3600 seconds
self.stored_data = deque()
self.last_cleanup = time.time()
self.last_access = time.time()
def cleanup_old_data(self):
current_time = time.time()
while self.stored_data and (current_time - self.stored_data[0]['timestamp']) > self.retention_seconds:
self.stored_data.popleft()
def get_stored_data(self):
self.cleanup_old_data()
self.last_access = time.time()
return list(self.stored_data)
def store_chunk(self, data, timestamp):
self.stored_data.append({
'timestamp': timestamp,
'data': data,
'formatted_time': datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
})
if time.time() - self.last_cleanup > 60:
self.cleanup_old_data()
self.last_cleanup = time.time()
async def start_reading(self):
while True:
try:
async with aiohttp.ClientSession() as session:
if self.show_output:
print(f"Connecting to {self.url}...")
async with session.get(self.url) as response:
if response.status == 200:
if self.show_output:
print("Connected! Reading stream...")
while True:
chunk = await response.content.read(1024)
if not chunk:
break
current_time = time.time()
try:
text = chunk.decode('utf-8')
self.store_chunk(text, current_time)
except UnicodeDecodeError:
self.store_chunk(str(chunk), current_time)
except aiohttp.ClientError as e:
if self.show_output:
print(f"Connection error: {e}")
except Exception as e:
if self.show_output:
print(f"Error: {e}")
# Always wait a bit before reconnecting
await asyncio.sleep(self.reconnect_delay)
class WebServer:
def __init__(self, stream_manager):
self.stream_manager = stream_manager
async def handle_create_stream(self, request):
try:
data = await request.post()
retention_minutes = float(data.get('retention_minutes', '60'))
retention_seconds = min(3600, max(0, retention_minutes * 60))
stream_id, token = await self.stream_manager.create_stream(retention_seconds)
html = self.create_stream_page(stream_id, token)
return web.Response(text=html, content_type='text/html')
except Exception as e:
return web.Response(text=f"Error creating stream: {str(e)}", status=400)
async def handle_view_stream(self, request):
stream_id = request.match_info.get('stream_id')
token = request.query.get('token')
stream = self.stream_manager.get_stream(stream_id, token)
if not stream:
return web.Response(text="Invalid stream ID or token", status=403)
html = self.create_viewer_page(stream, token)
return web.Response(text=html, content_type='text/html')
async def handle_home(self, request):
return web.Response(text=self.create_home_page(), content_type='text/html')
def create_home_page(self):
return """
<!DOCTYPE html>
<html>
<head>
<title>Create Ephemeral Stream</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.form-container {
margin: 20px 0;
padding: 20px;
border: 1px solid #ddd;
border-radius: 4px;
}
.input-group {
margin: 10px 0;
}
input, button {
padding: 8px;
margin: 5px 0;
}
button {
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.info {
margin: 20px 0;
padding: 15px;
background: #f5f5f5;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Create New Ephemeral Stream</h1>
<div class="form-container">
<form method="POST" action="/create">
<div class="input-group">
<label for="retention_minutes">Retention Time (minutes, max 60):</label><br>
<input type="number" id="retention_minutes" name="retention_minutes"
min="0" max="60" value="60" step="0.5" required>
</div>
<button type="submit">Create Stream</button>
</form>
</div>
<div class="info">
<h3>About Ephemeral Streams</h3>
<p>Create your private stream with custom retention time. Each stream:</p>
<ul>
<li>Has a unique ID and access token</li>
<li>Can only be viewed with the correct token</li>
<li>Automatically deletes data older than the specified retention time</li>
<li>Supports retention times from 0 to 60 minutes</li>
</ul>
</div>
</body>
</html>
"""
def create_stream_page(self, stream_id, token):
view_url = f"/view/{stream_id}?token={token}"
write_url = f"https://ppng.io/{stream_id}"
return f"""
<!DOCTYPE html>
<html>
<head>
<title>Stream Created</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}}
.info-box {{
background: #f5f5f5;
padding: 20px;
border-radius: 4px;
margin: 20px 0;
}}
.code-box {{
background: #2b2b2b;
color: #ffffff;
padding: 15px;
border-radius: 4px;
font-family: monospace;
white-space: pre-wrap;
word-break: break-all;
}}
.button {{
display: inline-block;
padding: 10px 20px;
background: #4CAF50;
color: white;
text-decoration: none;
border-radius: 4px;
margin: 10px 0;
}}
</style>
</head>
<body>
<h1>Stream Created Successfully</h1>
<div class="info-box">
<h3>Your Stream Information</h3>
<p><strong>Stream ID:</strong> {stream_id}</p>
<p><strong>Access Token:</strong> {token}</p>
<p><strong>View URL:</strong> <a href="{view_url}">{view_url}</a></p>
<h3>How to Write to Your Stream</h3>
<p>Use curl to write to your stream:</p>
<div class="code-box">
seq inf | curl -T- {write_url}
</div>
<h3>Important Notes</h3>
<ul>
<li>Keep your access token secret</li>
<li>Bookmark the view URL for easy access</li>
<li>Data older than your specified retention time will be automatically deleted</li>
</ul>
</div>
<a href="{view_url}" class="button">View Your Stream</a>
</body>
</html>
"""
def create_viewer_page(self, stream, token):
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>Stream Viewer</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
}}
.data-container {{
margin: 20px 0;
}}
.timestamp {{
color: #666;
font-size: 0.9em;
}}
.data-item {{
margin: 10px 0;
padding: 10px;
background: #f5f5f5;
border-radius: 4px;
}}
.refresh-btn {{
padding: 10px 20px;
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}}
.info {{
margin-bottom: 20px;
color: #666;
}}
.code-box {{
background: #2b2b2b;
color: #ffffff;
padding: 15px;
border-radius: 4px;
font-family: monospace;
margin: 10px 0;
white-space: pre-wrap;
word-break: break-all;
}}
</style>
<script>
function refreshData() {{
window.location.reload();
}}
// Auto refresh every 5 seconds
setInterval(refreshData, 5000);
</script>
</head>
<body>
<h1>Stream Viewer</h1>
<div class="info">
<p>Stream ID: {stream.path}</p>
<p>Retention Time: {stream.retention_seconds / 60:.1f} minutes</p>
<div class="code-box">To write to this stream: seq inf | curl -T- {stream.url}</div>
</div>
<button class="refresh-btn" onclick="refreshData()">Refresh Data</button>
<div class="data-container">
"""
data = stream.get_stored_data()
for item in reversed(data):
html += f"""
<div class="data-item">
<div class="timestamp">{item['formatted_time']}</div>
<pre>{item['data']}</pre>
</div>
"""
html += """
</div>
</body>
</html>
"""
return html
async def main():
stream_manager = StreamManager()
web_server = WebServer(stream_manager)
app = web.Application()
app.router.add_get('/', web_server.handle_home)
app.router.add_post('/create', web_server.handle_create_stream)
app.router.add_get('/view/{stream_id}', web_server.handle_view_stream)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '0.0.0.0', 7860)
await site.start()
print("\nWeb interface available at http://0.0.0.0:7860")
# Start the cleanup task
cleanup_task = asyncio.create_task(stream_manager.cleanup_inactive_streams())
try:
await asyncio.Event().wait() # Keep the server running indefinitely
finally:
cleanup_task.cancel()
if __name__ == "__main__":
print("Starting Ephemeral Stream Server...")
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nStopping server...") |