File size: 14,483 Bytes
494c89b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb7f347
 
 
 
494c89b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
WebSocket Connection Manager for real-time updates
Handles bidirectional communication with standalone web app
"""

from typing import List, Dict, Any, Optional
from fastapi import WebSocket
import json
import asyncio
from pathlib import Path


class ConnectionManager:
    """Manages WebSocket connections for broadcasting logs and status"""
    
    def __init__(self):
        self.active_connections: List[WebSocket] = []
        self._loop: asyncio.AbstractEventLoop = None
    
    async def connect(self, websocket: WebSocket):
        await websocket.accept()
        self.active_connections.append(websocket)
    
    def disconnect(self, websocket: WebSocket):
        if websocket in self.active_connections:
            self.active_connections.remove(websocket)
    
    async def send_personal(self, message: str, websocket: WebSocket):
        await websocket.send_text(message)
    
    async def send_json(self, data: Dict[str, Any], websocket: WebSocket):
        """Send JSON message to specific client"""
        await websocket.send_text(json.dumps(data))
    
    async def broadcast(self, message: Dict[str, Any]):
        """Broadcast message to all connected clients"""
        if not self.active_connections:
            return
        
        text = json.dumps(message)
        disconnected = []
        
        for connection in self.active_connections:
            try:
                await connection.send_text(text)
            except Exception:
                disconnected.append(connection)
        
        # Clean up disconnected
        for conn in disconnected:
            self.disconnect(conn)
    
    async def broadcast_log(self, log: str, level: str = "info"):
        """Broadcast a log message"""
        await self.broadcast({
            "type": "appendLog",
            "log": log,
            "level": level
        })
    
    async def broadcast_status(self, status: Dict[str, Any]):
        """Broadcast status update"""
        await self.broadcast({
            "type": "status",
            **status
        })
    
    async def broadcast_progress(self, step: int, total: int, name: str, detail: str = ""):
        """Broadcast progress update"""
        await self.broadcast({
            "type": "progress",
            "step": step,
            "totalSteps": total,
            "stepName": name,
            "detail": detail
        })
    
    def sync_broadcast(self, message: Dict[str, Any]):
        """Synchronous broadcast (for use in non-async code)"""
        if not self.active_connections:
            return
        
        try:
            loop = asyncio.get_event_loop()
            if loop.is_running():
                asyncio.create_task(self.broadcast(message))
            else:
                loop.run_until_complete(self.broadcast(message))
        except RuntimeError:
            # No event loop, skip
            pass
    
    def sync_log(self, log: str, level: str = "info"):
        """Synchronous log broadcast"""
        self.sync_broadcast({
            "type": "appendLog",
            "log": log,
            "level": level
        })


# Global instance
manager = ConnectionManager()


def get_manager() -> ConnectionManager:
    """Get the global WebSocket manager"""
    return manager


# === Profile Storage for Standalone ===

PROFILES_FILE = Path(__file__).parent / "profiles.json"


def load_profiles() -> Dict[str, Any]:
    """Load profiles from JSON file"""
    if PROFILES_FILE.exists():
        try:
            return json.loads(PROFILES_FILE.read_text(encoding='utf-8'))
        except Exception:
            pass
    return {"profiles": [], "activeProfileId": None}


def save_profiles(data: Dict[str, Any]):
    """Save profiles to JSON file"""
    PROFILES_FILE.write_text(json.dumps(data, indent=2), encoding='utf-8')


def get_active_profile() -> Optional[Dict[str, Any]]:
    """Get the currently active profile"""
    data = load_profiles()
    active_id = data.get("activeProfileId")
    if not active_id:
        return None
    for p in data.get("profiles", []):
        if p.get("id") == active_id:
            return p
    return None


# === Command Handler ===

async def handle_command(command: str, data: Dict[str, Any], websocket: WebSocket):
    """Handle incoming WebSocket commands from frontend"""
    ws = get_manager()
    
    try:
        if command == "ping":
            await ws.send_json({"type": "pong"}, websocket)
        
        elif command == "refresh":
            # Load and send accounts
            from services.token_service import TokenService
            token_service = TokenService()
            tokens = token_service.list_tokens()
            current = token_service.get_current_token()
            current_refresh = current.raw_data.get('refreshToken') if current else None
            
            accounts = []
            for token in tokens:
                is_active = (current_refresh and 
                            token.raw_data.get('refreshToken') == current_refresh)
                accounts.append({
                    "filename": token.path.name,
                    "isActive": is_active,
                    "isExpired": token.is_expired,
                    "needsRefresh": token.needs_refresh,
                    "tokenData": {
                        "accountName": token.account_name,
                        "email": token.email,
                        "expiresAt": token.expires_at.isoformat() if token.expires_at else None,
                        "refreshToken": token.raw_data.get('refreshToken') if token.raw_data else None,
                        "clientId": token.raw_data.get('_clientId') if token.raw_data else None,
                        "clientSecret": token.raw_data.get('_clientSecret') if token.raw_data else None
                    },
                    "usage": {
                        "currentUsage": -1,
                        "usageLimit": 500,
                        "percentageUsed": 0
                    }
                })
            
            await ws.send_json({
                "type": "accountsLoaded",
                "accounts": accounts
            }, websocket)
        
        elif command == "switchAccount":
            email = data.get("email")
            if email:
                from services.token_service import TokenService
                token_service = TokenService()
                token = token_service.get_token(email)
                if token:
                    token_service.activate_token(token)
                    await ws.broadcast_log(f"✓ Switched to {token.account_name}")
                    await ws.broadcast({"type": "toast", "message": f"Switched to {token.account_name}", "toastType": "success"})
        
        elif command == "deleteAccount":
            email = data.get("email")
            if email:
                from services.token_service import TokenService
                token_service = TokenService()
                token_service.delete_token(email)
                await ws.broadcast_log(f"🗑 Deleted {email}")
        
        elif command == "refreshToken":
            email = data.get("email")
            if email:
                from services.token_service import TokenService
                token_service = TokenService()
                token = token_service.get_token(email)
                if token:
                    token_service.refresh_and_save(token)
                    await ws.broadcast_log(f"🔄 Refreshed {token.account_name}")
        
        elif command == "startAutoReg":
            # Get active profile
            profile = get_active_profile()
            if not profile:
                await ws.broadcast({"type": "toast", "message": "No profile configured", "toastType": "error"})
                return
            
            # Start autoreg via API
            from app.api.autoreg import start_autoreg, AutoRegConfig
            from fastapi import BackgroundTasks
            
            imap = profile.get("imap", {})
            strategy = profile.get("strategy", {})
            
            config = AutoRegConfig(
                headless=False,
                spoofing=True,
                imapServer=imap.get("server"),
                imapUser=imap.get("user"),
                imapPassword=imap.get("password"),
                emailStrategy=strategy.get("type", "single")
            )
            
            # Run in background
            import asyncio
            from app.api.autoreg import run_autoreg
            asyncio.create_task(run_autoreg(config))
            
            await ws.broadcast({"type": "updateStatus", "status": json.dumps({"step": 0, "totalSteps": 8, "stepName": "Starting..."})})
        
        elif command == "stopAutoReg":
            from app.api.autoreg import stop_autoreg
            await stop_autoreg()
        
        elif command == "loadProfiles":
            data = load_profiles()
            await ws.send_json({
                "type": "profilesLoaded",
                "profiles": data.get("profiles", []),
                "activeProfileId": data.get("activeProfileId")
            }, websocket)
        
        elif command == "getActiveProfile":
            profile = get_active_profile()
            await ws.send_json({
                "type": "activeProfileLoaded",
                "profile": profile
            }, websocket)
        
        elif command == "setActiveProfile":
            profile_id = data.get("profileId")
            profiles_data = load_profiles()
            profiles_data["activeProfileId"] = profile_id
            save_profiles(profiles_data)
            
            # Find and send the profile
            profile = None
            for p in profiles_data.get("profiles", []):
                if p.get("id") == profile_id:
                    profile = p
                    break
            
            await ws.broadcast({
                "type": "profilesLoaded",
                "profiles": profiles_data.get("profiles", []),
                "activeProfileId": profile_id
            })
            await ws.broadcast({
                "type": "activeProfileLoaded",
                "profile": profile
            })
            await ws.broadcast({"type": "toast", "message": "Profile selected", "toastType": "success"})
        
        elif command == "createProfile":
            profile = data.get("profile", {})
            import uuid
            profile["id"] = str(uuid.uuid4())
            profile["stats"] = {"registered": 0, "failed": 0}
            
            profiles_data = load_profiles()
            profiles_data["profiles"].append(profile)
            
            # Auto-select if first profile
            if len(profiles_data["profiles"]) == 1:
                profiles_data["activeProfileId"] = profile["id"]
            
            save_profiles(profiles_data)
            
            await ws.broadcast({
                "type": "profilesLoaded",
                "profiles": profiles_data.get("profiles", []),
                "activeProfileId": profiles_data.get("activeProfileId")
            })
            await ws.broadcast({"type": "toast", "message": "Profile created", "toastType": "success"})
        
        elif command == "updateProfile":
            profile = data.get("profile", {})
            profile_id = profile.get("id")
            
            profiles_data = load_profiles()
            for i, p in enumerate(profiles_data.get("profiles", [])):
                if p.get("id") == profile_id:
                    # Preserve stats
                    profile["stats"] = p.get("stats", {"registered": 0, "failed": 0})
                    profiles_data["profiles"][i] = profile
                    break
            
            save_profiles(profiles_data)
            
            await ws.broadcast({
                "type": "profilesLoaded",
                "profiles": profiles_data.get("profiles", []),
                "activeProfileId": profiles_data.get("activeProfileId")
            })
            await ws.broadcast({"type": "toast", "message": "Profile updated", "toastType": "success"})
        
        elif command == "deleteProfile":
            profile_id = data.get("profileId")
            
            profiles_data = load_profiles()
            profiles_data["profiles"] = [p for p in profiles_data.get("profiles", []) if p.get("id") != profile_id]
            
            # Clear active if deleted
            if profiles_data.get("activeProfileId") == profile_id:
                profiles_data["activeProfileId"] = None
            
            save_profiles(profiles_data)
            
            await ws.broadcast({
                "type": "profilesLoaded",
                "profiles": profiles_data.get("profiles", []),
                "activeProfileId": profiles_data.get("activeProfileId")
            })
            await ws.broadcast({"type": "toast", "message": "Profile deleted", "toastType": "success"})
        
        elif command == "getPatchStatus":
            # Check if Kiro is patched
            try:
                from services.kiro_service import KiroService
                kiro = KiroService()
                is_patched = kiro.is_patched()
                machine_id = kiro.get_machine_id()
                await ws.send_json({
                    "type": "patchStatus",
                    "isPatched": is_patched,
                    "currentMachineId": machine_id
                }, websocket)
            except Exception as e:
                await ws.send_json({
                    "type": "patchStatus",
                    "isPatched": False,
                    "error": str(e)
                }, websocket)
        
        elif command == "getProfile":
            profile_id = data.get("profileId")
            profiles_data = load_profiles()
            profile = None
            for p in profiles_data.get("profiles", []):
                if p.get("id") == profile_id:
                    profile = p
                    break
            await ws.send_json({
                "type": "profileLoaded",
                "profile": profile
            }, websocket)
        
        else:
            await ws.broadcast_log(f"Unknown command: {command}", "warning")
    
    except Exception as e:
        await ws.broadcast_log(f"Error handling {command}: {str(e)}", "error")
        import traceback
        traceback.print_exc()