hostb / app.py
mnpx's picture
Update app.py
2b1ccda verified
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
import uvicorn
import subprocess
import threading
from pathlib import Path
app = FastAPI()
count_file = Path("/tmp/people_helped.txt")
count_file.write_text("0")
def increment_count():
current = int(count_file.read_text().strip() or "0")
count_file.write_text(str(current + 1))
print(f"✅ Counter increased! New total: {current + 1}")
def monitor_snowflake():
print("Starting Snowflake proxy and monitoring...")
proc = subprocess.Popen(
["snowflake-proxy", "-verbose", "-relay", "https://snowflake.torproject.org/"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
for line in proc.stdout:
line = line.strip()
if not line:
continue
print(f"LOG: {line}") # This will show in HF Logs
# Broader detection for client connections
lower_line = line.lower()
if any(x in lower_line for x in [
"client connected",
"accepted connection",
"new session",
"webrtc: peerconnection",
"data channel opened",
"proxy: client",
"established connection",
"starting session"
]):
increment_count()
# Start monitoring
threading.Thread(target=monitor_snowflake, daemon=True).start()
@app.get("/", response_class=HTMLResponse)
async def dashboard():
count = count_file.read_text().strip()
return f"""
<html>
<head>
<title>❄️ Living Snowflake Proxy</title>
<style>
body {{ font-family: monospace; text-align: center; padding: 40px; background: #0a0a0a; color: #00ff9d; }}
.counter {{ font-size: 6rem; margin: 20px 0; font-weight: bold; }}
</style>
<script>
async function update() {{
const res = await fetch('/count');
const data = await res.json();
document.getElementById('count').textContent = data.count;
}}
setInterval(update, 2000);
</script>
</head>
<body>
<h1>❄️ Living Snowflake Proxy</h1>
<p>People I've helped today:</p>
<div class="counter" id="count">{count}</div>
<p>Proxy is running 24/7</p>
</body>
</html>
"""
@app.get("/count")
async def get_count():
return {"count": int(count_file.read_text().strip() or "0")}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)