Update app.py
Browse files
app.py
CHANGED
|
@@ -4,166 +4,303 @@ import threading
|
|
| 4 |
import queue
|
| 5 |
import sys
|
| 6 |
import io
|
|
|
|
| 7 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 8 |
from fastapi.responses import HTMLResponse
|
| 9 |
import uvicorn
|
|
|
|
| 10 |
import Istihbarat
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
output_queue = queue.Queue()
|
| 16 |
input_queue = queue.Queue()
|
| 17 |
|
| 18 |
-
class
|
|
|
|
|
|
|
| 19 |
def __init__(self):
|
| 20 |
-
|
| 21 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
self.running = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
sys.stdout = self.CaptureOutput()
|
| 28 |
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
Istihbarat.istihbarat_modulu()
|
| 32 |
except Exception as e:
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
finally:
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
class
|
|
|
|
| 38 |
def write(self, text):
|
| 39 |
-
if text.strip():
|
| 40 |
output_queue.put(text)
|
|
|
|
| 41 |
def flush(self):
|
| 42 |
pass
|
| 43 |
|
| 44 |
-
# Istihbarat
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
@app.websocket("/ws")
|
| 49 |
async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
| 50 |
await websocket.accept()
|
| 51 |
print("✅ İstemci bağlandı!")
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
try:
|
| 58 |
while True:
|
| 59 |
-
# İstemciden mesaj
|
| 60 |
try:
|
| 61 |
data = await asyncio.wait_for(websocket.receive_text(), timeout=0.1)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
await websocket.send_text(f"[
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
except asyncio.TimeoutError:
|
| 72 |
pass
|
| 73 |
except WebSocketDisconnect:
|
| 74 |
break
|
| 75 |
|
| 76 |
-
# Istihbarat çıktısını kontrol et
|
| 77 |
outputs = []
|
| 78 |
while not output_queue.empty():
|
| 79 |
try:
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
except:
|
| 82 |
break
|
| 83 |
|
| 84 |
if outputs:
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
-
await asyncio.sleep(0.
|
| 88 |
|
| 89 |
except WebSocketDisconnect:
|
| 90 |
print("❌ İstemci bağlantısı koptu!")
|
|
|
|
|
|
|
| 91 |
|
| 92 |
@app.get("/")
|
| 93 |
-
async def
|
|
|
|
| 94 |
return HTMLResponse("""
|
|
|
|
| 95 |
<html>
|
| 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 |
-
ws.onopen = function() {
|
| 127 |
-
outputDiv.innerHTML = '✅ Bağlantı kuruldu!\\n';
|
| 128 |
-
outputDiv.innerHTML += '💡 Istihbarat modülü çalışıyor...\\n';
|
| 129 |
-
outputDiv.innerHTML += '📝 Komut göndermek için: /input [komut]\\n';
|
| 130 |
-
outputDiv.innerHTML += '─────────────────────────────────\\n';
|
| 131 |
-
};
|
| 132 |
-
|
| 133 |
-
ws.onmessage = function(event) {
|
| 134 |
-
outputDiv.innerHTML += event.data + '\\n';
|
| 135 |
-
outputDiv.scrollTop = outputDiv.scrollHeight;
|
| 136 |
-
};
|
| 137 |
-
|
| 138 |
-
ws.onclose = function() {
|
| 139 |
-
outputDiv.innerHTML += '❌ Bağlantı koptu! Yeniden bağlanılıyor...\\n';
|
| 140 |
-
setTimeout(connect, 3000);
|
| 141 |
-
};
|
| 142 |
-
}
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
ws.send(cmd);
|
| 149 |
-
inputField.value = '';
|
| 150 |
-
} else {
|
| 151 |
-
outputDiv.innerHTML += '⚠️ Bağlantı yok!\\n';
|
| 152 |
-
}
|
| 153 |
-
}
|
| 154 |
-
}
|
| 155 |
|
| 156 |
-
|
| 157 |
-
outputDiv.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
}
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
</html>
|
| 168 |
""")
|
| 169 |
|
|
|
|
| 4 |
import queue
|
| 5 |
import sys
|
| 6 |
import io
|
| 7 |
+
import time
|
| 8 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 9 |
from fastapi.responses import HTMLResponse
|
| 10 |
import uvicorn
|
| 11 |
+
|
| 12 |
import Istihbarat
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
+
# Kuyruklar
|
| 17 |
output_queue = queue.Queue()
|
| 18 |
input_queue = queue.Queue()
|
| 19 |
|
| 20 |
+
class IstihbaratWrapper:
|
| 21 |
+
"""Istihbarat modülünü saran sınıf."""
|
| 22 |
+
|
| 23 |
def __init__(self):
|
| 24 |
+
self.thread = None
|
| 25 |
+
self.running = False
|
| 26 |
+
self.original_stdin = None
|
| 27 |
+
self.original_stdout = None
|
| 28 |
+
|
| 29 |
+
def start(self):
|
| 30 |
+
"""Istihbarat'ı başlat."""
|
| 31 |
+
if self.running:
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
self.running = True
|
| 35 |
+
self.thread = threading.Thread(target=self._run, daemon=True)
|
| 36 |
+
self.thread.start()
|
| 37 |
+
print("✅ Istihbarat başlatıldı!")
|
| 38 |
+
|
| 39 |
+
def _run(self):
|
| 40 |
+
"""Istihbarat modülünü çalıştır."""
|
| 41 |
+
# Orijinal stdin/stdout'u sakla
|
| 42 |
+
self.original_stdin = sys.stdin
|
| 43 |
+
self.original_stdout = sys.stdout
|
| 44 |
|
| 45 |
+
# Özel stdin/stdout oluştur
|
| 46 |
+
sys.stdin = self.CustomStdin()
|
| 47 |
+
sys.stdout = self.CustomStdout()
|
|
|
|
| 48 |
|
| 49 |
try:
|
| 50 |
+
print("[ISTI] Istihbarat modülü başlatılıyor...")
|
| 51 |
Istihbarat.istihbarat_modulu()
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"[ISTI] HATA: {str(e)}")
|
| 54 |
+
import traceback
|
| 55 |
+
traceback.print_exc()
|
| 56 |
finally:
|
| 57 |
+
# Orijinal stdin/stdout'u geri yükle
|
| 58 |
+
sys.stdin = self.original_stdin
|
| 59 |
+
sys.stdout = self.original_stdout
|
| 60 |
+
self.running = False
|
| 61 |
+
print("[ISTI] Istihbarat durduruldu.")
|
| 62 |
+
|
| 63 |
+
class CustomStdin:
|
| 64 |
+
"""Özel stdin - input_queue'den okur."""
|
| 65 |
+
def readline(self):
|
| 66 |
+
while True:
|
| 67 |
+
try:
|
| 68 |
+
# input_queue'den komut al (bloklama)
|
| 69 |
+
cmd = input_queue.get()
|
| 70 |
+
print(f"[DEBUG] stdin.readline: {repr(cmd)}")
|
| 71 |
+
return cmd
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"[DEBUG] stdin hatası: {e}")
|
| 74 |
+
return "\n"
|
| 75 |
+
|
| 76 |
+
def read(self, size=None):
|
| 77 |
+
return self.readline()
|
| 78 |
|
| 79 |
+
class CustomStdout:
|
| 80 |
+
"""Özel stdout - output_queue'ye yazar."""
|
| 81 |
def write(self, text):
|
| 82 |
+
if text and text.strip():
|
| 83 |
output_queue.put(text)
|
| 84 |
+
|
| 85 |
def flush(self):
|
| 86 |
pass
|
| 87 |
|
| 88 |
+
# Istihbarat'ı başlat
|
| 89 |
+
istihbarat = IstihbaratWrapper()
|
| 90 |
+
istihbarat.start()
|
| 91 |
|
| 92 |
@app.websocket("/ws")
|
| 93 |
async def websocket_endpoint(websocket: WebSocket):
|
| 94 |
+
"""WebSocket bağlantı noktası."""
|
| 95 |
await websocket.accept()
|
| 96 |
print("✅ İstemci bağlandı!")
|
| 97 |
|
| 98 |
+
# Hoş geldin mesajı
|
| 99 |
+
await websocket.send_text("✅ Bağlantı kuruldu!")
|
| 100 |
+
await websocket.send_text("📝 Komutlar:")
|
| 101 |
+
await websocket.send_text(" • Herhangi bir şey yaz → Istihbarat'a gönderilir")
|
| 102 |
+
await websocket.send_text(" • /status → Sunucu durumu")
|
| 103 |
+
await websocket.send_text(" • /clear → Çıktıyı temizle")
|
| 104 |
+
await websocket.send_text(" • /exit → Çıkış")
|
| 105 |
+
await websocket.send_text("─" * 50)
|
| 106 |
+
await websocket.send_text("🔑 Lütfen giriş yapın:")
|
| 107 |
|
| 108 |
try:
|
| 109 |
while True:
|
| 110 |
+
# İstemciden mesaj al
|
| 111 |
try:
|
| 112 |
data = await asyncio.wait_for(websocket.receive_text(), timeout=0.1)
|
| 113 |
+
|
| 114 |
+
if data == "/status":
|
| 115 |
+
await websocket.send_text(f"[STATUS] Istihbarat çalışıyor: {istihbarat.running}")
|
| 116 |
+
await websocket.send_text(f"[STATUS] Input kuyruğu: {input_queue.qsize()}")
|
| 117 |
+
await websocket.send_text(f"[STATUS] Output kuyruğu: {output_queue.qsize()}")
|
| 118 |
+
|
| 119 |
+
elif data == "/clear":
|
| 120 |
+
# Output kuyruğunu temizle
|
| 121 |
+
while not output_queue.empty():
|
| 122 |
+
try:
|
| 123 |
+
output_queue.get_nowait()
|
| 124 |
+
except:
|
| 125 |
+
break
|
| 126 |
+
await websocket.send_text("[CLEAR] Çıktı temizlendi!")
|
| 127 |
+
|
| 128 |
+
elif data == "/exit":
|
| 129 |
+
await websocket.send_text("👋 Bağlantı kapatılıyor...")
|
| 130 |
+
break
|
| 131 |
+
|
| 132 |
+
else:
|
| 133 |
+
# Normal mesajı Istihbarat'a gönder
|
| 134 |
+
if data.strip():
|
| 135 |
+
print(f"[WS] İstemciden gelen komut: {repr(data)}")
|
| 136 |
+
input_queue.put(data + "\n")
|
| 137 |
+
await websocket.send_text(f"[INPUT] Gönderildi: {data}")
|
| 138 |
+
|
| 139 |
except asyncio.TimeoutError:
|
| 140 |
pass
|
| 141 |
except WebSocketDisconnect:
|
| 142 |
break
|
| 143 |
|
| 144 |
+
# Istihbarat çıktısını kontrol et ve gönder
|
| 145 |
outputs = []
|
| 146 |
while not output_queue.empty():
|
| 147 |
try:
|
| 148 |
+
text = output_queue.get_nowait()
|
| 149 |
+
if text and text.strip():
|
| 150 |
+
outputs.append(text)
|
| 151 |
except:
|
| 152 |
break
|
| 153 |
|
| 154 |
if outputs:
|
| 155 |
+
for text in outputs:
|
| 156 |
+
await websocket.send_text(text)
|
| 157 |
|
| 158 |
+
await asyncio.sleep(0.05)
|
| 159 |
|
| 160 |
except WebSocketDisconnect:
|
| 161 |
print("❌ İstemci bağlantısı koptu!")
|
| 162 |
+
except Exception as e:
|
| 163 |
+
print(f"❌ WebSocket hatası: {e}")
|
| 164 |
|
| 165 |
@app.get("/")
|
| 166 |
+
async def root():
|
| 167 |
+
"""Ana sayfa."""
|
| 168 |
return HTMLResponse("""
|
| 169 |
+
<!DOCTYPE html>
|
| 170 |
<html>
|
| 171 |
+
<head>
|
| 172 |
+
<title>Dexo API</title>
|
| 173 |
+
<meta charset="UTF-8">
|
| 174 |
+
<style>
|
| 175 |
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 176 |
+
body {
|
| 177 |
+
background: #0a0a0f;
|
| 178 |
+
color: #00ff41;
|
| 179 |
+
font-family: 'Courier New', monospace;
|
| 180 |
+
height: 100vh;
|
| 181 |
+
display: flex;
|
| 182 |
+
flex-direction: column;
|
| 183 |
+
padding: 10px;
|
| 184 |
+
}
|
| 185 |
+
#header {
|
| 186 |
+
border-bottom: 1px solid #00ff41;
|
| 187 |
+
padding-bottom: 10px;
|
| 188 |
+
margin-bottom: 10px;
|
| 189 |
+
}
|
| 190 |
+
#header h1 { font-size: 18px; color: #00ff41; }
|
| 191 |
+
#header .status { color: #ffd700; font-size: 12px; }
|
| 192 |
+
#output {
|
| 193 |
+
flex: 1;
|
| 194 |
+
overflow-y: auto;
|
| 195 |
+
background: #0d0d1a;
|
| 196 |
+
padding: 10px;
|
| 197 |
+
border: 1px solid #00ff41;
|
| 198 |
+
border-radius: 5px;
|
| 199 |
+
white-space: pre-wrap;
|
| 200 |
+
font-size: 13px;
|
| 201 |
+
line-height: 1.5;
|
| 202 |
+
margin-bottom: 10px;
|
| 203 |
+
}
|
| 204 |
+
#input-area {
|
| 205 |
+
display: flex;
|
| 206 |
+
gap: 10px;
|
| 207 |
+
}
|
| 208 |
+
#input {
|
| 209 |
+
flex: 1;
|
| 210 |
+
background: #0d0d1a;
|
| 211 |
+
color: #00ff41;
|
| 212 |
+
border: 1px solid #00ff41;
|
| 213 |
+
padding: 10px;
|
| 214 |
+
border-radius: 5px;
|
| 215 |
+
font-family: 'Courier New', monospace;
|
| 216 |
+
font-size: 14px;
|
| 217 |
+
}
|
| 218 |
+
#input:focus { outline: none; border-color: #ffd700; }
|
| 219 |
+
button {
|
| 220 |
+
background: #00ff41;
|
| 221 |
+
color: #0a0a0f;
|
| 222 |
+
border: none;
|
| 223 |
+
padding: 10px 20px;
|
| 224 |
+
border-radius: 5px;
|
| 225 |
+
cursor: pointer;
|
| 226 |
+
font-weight: bold;
|
| 227 |
+
font-family: 'Courier New', monospace;
|
| 228 |
+
}
|
| 229 |
+
button:hover { background: #00cc33; }
|
| 230 |
+
.commands {
|
| 231 |
+
color: #ff6b6b;
|
| 232 |
+
font-size: 11px;
|
| 233 |
+
margin-top: 5px;
|
| 234 |
+
border-top: 1px solid #1a1a2e;
|
| 235 |
+
padding-top: 5px;
|
| 236 |
+
}
|
| 237 |
+
.commands span { margin-right: 15px; }
|
| 238 |
+
.cmd { color: #ffd700; }
|
| 239 |
+
</style>
|
| 240 |
+
</head>
|
| 241 |
+
<body>
|
| 242 |
+
<div id="header">
|
| 243 |
+
<h1>🖥️ DEXO API - Terminal</h1>
|
| 244 |
+
<span class="status">🟢 Çevrimiçi</span>
|
| 245 |
+
</div>
|
| 246 |
+
<div id="output">Bağlantı bekleniyor...</div>
|
| 247 |
+
<div id="input-area">
|
| 248 |
+
<input id="input" placeholder="Komut girin..." />
|
| 249 |
+
<button onclick="sendCommand()">Gönder</button>
|
| 250 |
+
<button onclick="clearOutput()">Temizle</button>
|
| 251 |
+
</div>
|
| 252 |
+
<div class="commands">
|
| 253 |
+
<span class="cmd">/status</span> Durum
|
| 254 |
+
<span class="cmd">/clear</span> Temizle
|
| 255 |
+
<span class="cmd">/exit</span> Çıkış
|
| 256 |
+
</div>
|
| 257 |
+
<script>
|
| 258 |
+
let ws;
|
| 259 |
+
const outputDiv = document.getElementById('output');
|
| 260 |
+
const inputField = document.getElementById('input');
|
| 261 |
+
|
| 262 |
+
function connect() {
|
| 263 |
+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 264 |
+
ws = new WebSocket(`${protocol}//${window.location.host}/ws`);
|
| 265 |
|
| 266 |
+
ws.onopen = function() {
|
| 267 |
+
outputDiv.innerHTML = '✅ Bağlantı kuruldu!\\n';
|
| 268 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
+
ws.onmessage = function(event) {
|
| 271 |
+
outputDiv.innerHTML += event.data + '\\n';
|
| 272 |
+
outputDiv.scrollTop = outputDiv.scrollHeight;
|
| 273 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
+
ws.onclose = function() {
|
| 276 |
+
outputDiv.innerHTML += '❌ Bağlantı koptu! 3 saniye sonra yeniden bağlanılıyor...\\n';
|
| 277 |
+
setTimeout(connect, 3000);
|
| 278 |
+
};
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
function sendCommand() {
|
| 282 |
+
const cmd = inputField.value.trim();
|
| 283 |
+
if (cmd) {
|
| 284 |
+
if (ws.readyState === WebSocket.OPEN) {
|
| 285 |
+
ws.send(cmd);
|
| 286 |
+
inputField.value = '';
|
| 287 |
+
} else {
|
| 288 |
+
outputDiv.innerHTML += '⚠️ Bağlantı yok!\\n';
|
| 289 |
+
}
|
| 290 |
}
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
function clearOutput() {
|
| 294 |
+
outputDiv.innerHTML = '';
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
inputField.addEventListener('keypress', function(e) {
|
| 298 |
+
if (e.key === 'Enter') sendCommand();
|
| 299 |
+
});
|
| 300 |
+
|
| 301 |
+
connect();
|
| 302 |
+
</script>
|
| 303 |
+
</body>
|
| 304 |
</html>
|
| 305 |
""")
|
| 306 |
|