-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
578 lines (479 loc) · 20.6 KB
/
app.py
File metadata and controls
578 lines (479 loc) · 20.6 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#!/usr/bin/env python3
"""
Flask Web Application for ADB SQLite Query Tool
Provides a web interface to query SQLite database on Android device via ADB
"""
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
from dotenv import load_dotenv
import sys
import os
import subprocess
# Load environment variables
load_dotenv()
# Add the python_tools directory to the path
PYTHON_TOOLS_PATH = os.getenv('PYTHON_TOOLS_PATH', '/Users/siddharth/Desktop/all-old/own-projects/winitsoftware/application1/python_tools')
sys.path.insert(0, PYTHON_TOOLS_PATH)
from adb_sqlite_query_tool import SQLiteADBQueryTool
app = Flask(__name__)
CORS(app)
# Global configuration - can be modified at runtime via API
app_config = {
'package_name': os.getenv('PACKAGE_NAME', 'com.multiplex.winit'),
'db_name': os.getenv('DB_NAME', 'WINITSQLite.db'),
'db_path': os.getenv('DB_PATH', ''), # Custom path, empty means auto-detect
'device_serial': os.getenv('DEVICE_SERIAL', ''),
'use_cache': os.getenv('USE_CACHE', 'True').lower() == 'true',
'force_local': os.getenv('FORCE_LOCAL', 'False').lower() == 'true'
}
def get_adb_tool():
"""Get or create ADB tool instance with current configuration"""
return SQLiteADBQueryTool(
package_name=app_config['package_name'],
db_name=app_config['db_name'],
force_local=app_config['force_local'],
use_cache=app_config['use_cache'],
device_serial=app_config['device_serial'] if app_config['device_serial'] else None
)
@app.route('/')
def index():
"""Serve the main page"""
return render_template('index.html')
@app.route('/api/devices', methods=['GET'])
def get_devices():
"""Get list of connected ADB devices"""
try:
result = subprocess.run(['adb', 'devices', '-l'],
capture_output=True, text=True, timeout=10)
if result.returncode != 0:
return jsonify({'success': False, 'error': 'ADB command failed'})
devices = []
lines = result.stdout.strip().split('\n')[1:] # Skip header
for line in lines:
if not line.strip():
continue
parts = line.split()
if len(parts) >= 2 and parts[1] == 'device':
serial = parts[0]
# Parse device info from the -l output
model = ''
product = ''
for part in parts[2:]:
if part.startswith('model:'):
model = part.split(':')[1]
elif part.startswith('product:'):
product = part.split(':')[1]
devices.append({
'serial': serial,
'model': model,
'product': product,
'display_name': f"{model or product or serial} ({serial})"
})
return jsonify({
'success': True,
'devices': devices,
'current_device': app_config['device_serial']
})
except subprocess.TimeoutExpired:
return jsonify({'success': False, 'error': 'ADB command timed out'})
except FileNotFoundError:
return jsonify({'success': False, 'error': 'ADB not found. Make sure Android SDK is installed.'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/packages', methods=['GET'])
def get_packages():
"""Get list of debuggable packages on the selected device"""
try:
device_serial = request.args.get('device', app_config['device_serial'])
# Build ADB base command with optional device serial
adb_base = ['adb']
if device_serial:
adb_base.extend(['-s', device_serial])
# Single shell command: list third-party packages, then test each with run-as
# run-as only works on debuggable apps, so this filters automatically
# --user 0: target main user only (avoids SecurityException on multi-user/work profile devices)
# tr -d '\\r': strips carriage returns from Android's CRLF line endings
shell_script = (
'for p in $(pm list packages --user 0 -3 2>/dev/null | tr -d "\\r" | sed "s/package://"); do '
'run-as $p id 2>/dev/null 1>/dev/null && echo $p; '
'done'
)
cmd = adb_base + ['shell', shell_script]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
# Don't check returncode — the for loop returns the exit code of the last
# run-as which is non-zero if the last package isn't debuggable
packages = []
for line in result.stdout.strip().split('\n'):
package_name = line.strip()
if package_name:
packages.append(package_name)
# Sort packages alphabetically
packages.sort()
return jsonify({
'success': True,
'packages': packages,
'current_package': app_config['package_name']
})
except subprocess.TimeoutExpired:
return jsonify({'success': False, 'error': 'Scanning debuggable packages timed out (this can take a while on first load)'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/config', methods=['GET'])
def get_config():
"""Get current configuration"""
return jsonify({
'success': True,
'config': {
'device_serial': app_config['device_serial'],
'package_name': app_config['package_name'],
'db_name': app_config['db_name'],
'db_path': app_config['db_path'],
'use_cache': app_config['use_cache'],
'force_local': app_config['force_local']
}
})
@app.route('/api/config', methods=['POST'])
def update_config():
"""Update configuration settings"""
try:
data = request.json
# Update configuration with provided values
if 'device_serial' in data:
app_config['device_serial'] = data['device_serial']
if 'package_name' in data:
app_config['package_name'] = data['package_name']
if 'db_name' in data:
app_config['db_name'] = data['db_name']
if 'db_path' in data:
app_config['db_path'] = data['db_path']
if 'use_cache' in data:
app_config['use_cache'] = bool(data['use_cache'])
if 'force_local' in data:
app_config['force_local'] = bool(data['force_local'])
return jsonify({
'success': True,
'message': 'Configuration updated successfully',
'config': app_config
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/databases', methods=['GET'])
def get_databases():
"""Get list of SQLite databases for the selected package"""
try:
device_serial = request.args.get('device', app_config['device_serial'])
package_name = request.args.get('package', app_config['package_name'])
# Build ADB command
adb_cmd = ['adb']
if device_serial:
adb_cmd.extend(['-s', device_serial])
databases = []
# Check multiple possible database locations
db_locations = ['databases', 'files', 'files/SQLite']
for location in db_locations:
cmd = adb_cmd + ['shell', f'run-as {package_name} ls {location} 2>/dev/null']
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
if result.returncode == 0:
for file in result.stdout.strip().split('\n'):
file = file.strip()
if file and (file.endswith('.db') or file.endswith('.sqlite') or file.endswith('.sqlite3')):
databases.append({
'name': file,
'path': f'{location}/{file}'
})
# Remove duplicates based on name
seen = set()
unique_databases = []
for db in databases:
if db['name'] not in seen:
seen.add(db['name'])
unique_databases.append(db)
return jsonify({
'success': True,
'databases': unique_databases,
'current_db': app_config['db_name']
})
except subprocess.TimeoutExpired:
return jsonify({'success': False, 'error': 'ADB command timed out'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/search-databases', methods=['GET'])
def search_databases():
"""Search for SQLite database files within a package's data directory"""
try:
device_serial = request.args.get('device', app_config['device_serial'])
package_name = request.args.get('package', app_config['package_name'])
search_query = request.args.get('q', '').strip().lower()
# Build ADB command
adb_cmd = ['adb']
if device_serial:
adb_cmd.extend(['-s', device_serial])
# Use find to recursively search for db files
cmd = adb_cmd + ['shell', f'run-as {package_name} find . -name "*.db" -o -name "*.sqlite" -o -name "*.sqlite3" 2>/dev/null']
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
databases = []
if result.returncode == 0:
for line in result.stdout.strip().split('\n'):
file_path = line.strip()
if not file_path:
continue
# Clean up the path (remove leading ./)
if file_path.startswith('./'):
file_path = file_path[2:]
file_name = file_path.split('/')[-1]
# Skip journal/wal/shm files
if file_name.endswith('-journal') or file_name.endswith('-wal') or file_name.endswith('-shm'):
continue
databases.append({
'name': file_name,
'path': file_path
})
# Apply search filter if provided
if search_query:
databases = [db for db in databases if search_query in db['name'].lower() or search_query in db['path'].lower()]
# Sort by name
databases.sort(key=lambda x: x['name'].lower())
return jsonify({
'success': True,
'databases': databases,
'current_db': app_config['db_name']
})
except subprocess.TimeoutExpired:
return jsonify({'success': False, 'error': 'Search timed out'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/check-connection', methods=['GET'])
def check_connection():
"""Check if ADB device is connected"""
try:
tool = get_adb_tool()
if not tool.check_adb_connection():
return jsonify({'success': False, 'error': 'No ADB device connected'})
if not tool.check_database_exists():
return jsonify({'success': False, 'error': 'Database not found on device'})
return jsonify({'success': True, 'message': 'Connected to device'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/tables', methods=['GET'])
def get_tables():
"""Get list of all tables with row counts"""
try:
tool = get_adb_tool()
# Check connection first
if not tool.check_adb_connection():
return jsonify({'success': False, 'error': 'No ADB device connected'})
if not tool.check_database_exists():
return jsonify({'success': False, 'error': 'Database not found on device'})
# Get tables
tables = tool.get_table_list()
# Get row counts for each table
table_info = []
for table in tables:
# count = tool.get_table_count(table)
table_info.append({
'name': table,
'row_count': 0
})
return jsonify({'success': True, 'tables': table_info})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/table-structure/<table_name>', methods=['GET'])
def get_table_structure(table_name):
"""Get structure/schema of a specific table"""
try:
tool = get_adb_tool()
# Check connection first
if not tool.check_adb_connection():
return jsonify({'success': False, 'error': 'No ADB device connected'})
columns = tool.get_table_info(table_name)
if not columns:
return jsonify({'success': False, 'error': f'Table {table_name} not found'})
return jsonify({'success': True, 'columns': columns})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/query', methods=['POST'])
def execute_query():
"""Execute a SQL query"""
try:
data = request.json
query = data.get('query', '').strip()
limit = data.get('limit', 100)
if not query:
return jsonify({'success': False, 'error': 'No query provided'})
tool = get_adb_tool()
# Check connection first
if not tool.check_adb_connection():
return jsonify({'success': False, 'error': 'No ADB device connected'})
# Execute query
results = tool.execute_query(query, limit=limit)
if results is None:
error_msg = tool.last_error if tool.last_error else 'Query execution failed'
return jsonify({'success': False, 'error': error_msg})
# Get column names from first result if available
columns = list(results[0].keys()) if results else []
return jsonify({
'success': True,
'columns': columns,
'rows': results,
'row_count': len(results)
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/table-data/<table_name>', methods=['GET'])
def get_table_data(table_name):
"""Get data from a specific table"""
try:
limit = request.args.get('limit', 100, type=int)
offset = request.args.get('offset', 0, type=int)
# Support very large limits (for "All" option)
if limit > 100000:
limit = 100000 # Cap at 100k rows for safety
tool = get_adb_tool()
# Check connection first
if not tool.check_adb_connection():
return jsonify({'success': False, 'error': 'No ADB device connected'})
# Get table structure
columns_info = tool.get_table_info(table_name)
if not columns_info:
return jsonify({'success': False, 'error': f'Table {table_name} not found'})
# Get data
query = f"SELECT * FROM {table_name} LIMIT {limit} OFFSET {offset}"
results = tool.execute_query(query)
if results is None:
return jsonify({'success': False, 'error': 'Failed to fetch data'})
# Get total count
total_count = tool.get_table_count(table_name)
return jsonify({
'success': True,
'columns': [col['name'] for col in columns_info],
'rows': results,
'row_count': len(results),
'total_count': total_count,
'offset': offset,
'limit': limit
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/clear-cache', methods=['POST'])
def clear_cache():
"""Clear database cache only"""
try:
tool = get_adb_tool()
if tool.clear_cache():
return jsonify({
'success': True,
'message': 'Cache cleared successfully'
})
else:
return jsonify({
'success': False,
'error': 'Failed to clear cache'
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/force-pull', methods=['POST'])
def force_pull():
"""Force pull fresh database from device (clears cache first)"""
try:
import tempfile
from pathlib import Path
tool = get_adb_tool()
# Check connection first
if not tool.check_adb_connection():
return jsonify({'success': False, 'error': 'No ADB device connected'})
if not tool.check_database_exists():
return jsonify({'success': False, 'error': 'Database not found on device'})
# Clear cache first
tool.clear_cache()
# Force pull with cache enabled (so it saves to cache directory)
# but force_pull=True bypasses the cache check and re-pulls
tool_force = SQLiteADBQueryTool(
package_name=app_config['package_name'],
db_name=app_config['db_name'],
force_local=True,
use_cache=True,
force_pull=True,
device_serial=app_config['device_serial'] if app_config['device_serial'] else None
)
# Pull fresh database
if not tool_force.pull_database():
return jsonify({'success': False, 'error': 'Failed to pull database from device'})
# Fix gzip decompression issue
cache_dir = Path(tempfile.gettempdir()) / "adb_sqlite_cache"
db_file = cache_dir / f"{app_config['package_name']}_{app_config['db_name']}"
if db_file.exists():
# Check if file is gzipped (Windows compatible check)
try:
with open(db_file, 'rb') as f:
magic = f.read(2)
if magic == b'\x1f\x8b': # gzip magic number
print(f"Detected gzipped database, decompressing...")
import gzip
import shutil
gz_file = str(db_file) + '.gz'
db_file.rename(gz_file)
with gzip.open(gz_file, 'rb') as f_in:
with open(db_file, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
os.unlink(gz_file)
print(f"Database decompressed successfully")
except Exception as e:
print(f"Warning: Could not check/decompress file: {e}")
return jsonify({
'success': True,
'message': 'Fresh database pulled from device (cache bypassed)'
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/refresh-database', methods=['POST'])
def refresh_database():
"""Force refresh database from device"""
try:
import tempfile
from pathlib import Path
tool = get_adb_tool()
# Check connection first
if not tool.check_adb_connection():
return jsonify({'success': False, 'error': 'No ADB device connected'})
if not tool.check_database_exists():
return jsonify({'success': False, 'error': 'Database not found on device'})
# Clear cache and force pull
tool.clear_cache()
# Pull fresh database
if not tool.pull_database():
return jsonify({'success': False, 'error': 'Failed to pull database from device'})
# Fix gzip decompression issue
cache_dir = Path(tempfile.gettempdir()) / "adb_sqlite_cache"
db_file = cache_dir / f"{app_config['package_name']}_{app_config['db_name']}"
if db_file.exists():
# Check if file is gzipped (Windows compatible check)
try:
with open(db_file, 'rb') as f:
magic = f.read(2)
if magic == b'\x1f\x8b': # gzip magic number
print(f"Detected gzipped database, decompressing...")
import gzip
import shutil
gz_file = str(db_file) + '.gz'
db_file.rename(gz_file)
with gzip.open(gz_file, 'rb') as f_in:
with open(db_file, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
os.unlink(gz_file)
print(f"Database decompressed successfully")
except Exception as e:
print(f"Warning: Could not check/decompress file: {e}")
return jsonify({
'success': True,
'message': 'Database refreshed successfully from device'
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
if __name__ == '__main__':
# Load Flask configuration from environment
FLASK_HOST = os.getenv('FLASK_HOST', '0.0.0.0')
FLASK_PORT = int(os.getenv('FLASK_PORT', '5001'))
FLASK_DEBUG = os.getenv('FLASK_DEBUG', 'True').lower() == 'true'
print("Starting ADB SQLite Query Viewer...")
print(f"Open http://localhost:{FLASK_PORT} in your browser")
app.run(debug=FLASK_DEBUG, host=FLASK_HOST, port=FLASK_PORT)