-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.py
More file actions
367 lines (324 loc) · 13.4 KB
/
Copy pathpatch.py
File metadata and controls
367 lines (324 loc) · 13.4 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
import re
import os
def patch_web_mirror():
path = "/home/cachy/github-p/fiver/src/fiver/web_mirror.py"
with open(path, "r") as f:
content = f.read()
# 1. PHONE_HTML
start_str = '# HTML template for the phone (Matte Black Theme, Instant Tap Permission)'
end_str = '"""\n\n# HTML template for the Desktop viewer'
new_html = """# HTML template for the phone (Matte Black Theme, Companion App Download)
PHONE_HTML = \"\"\"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="manifest" href="/manifest.json">
<title>Fiver Screen Mirror</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
width: 100%;
height: 100%;
}
body {
font-family: monospace, sans-serif;
background: #000000;
color: #ffffff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
text-align: center;
user-select: none;
-webkit-user-select: none;
}
.card {
background: #0a0a0a;
border: 1px solid #222222;
padding: 28px 24px;
max-width: 400px;
width: 100%;
}
h1 { font-size: 20px; margin-bottom: 12px; color: #ffffff; text-transform: uppercase; letter-spacing: 1px; }
p { font-size: 14px; color: #888888; margin-bottom: 24px; line-height: 1.5; }
button {
width: 100%;
padding: 16px;
background: #ffffff;
color: #000000;
border: 1px solid #333333;
font-family: monospace;
font-size: 15px;
font-weight: bold;
cursor: pointer;
text-transform: uppercase;
}
button:hover { background: #cccccc; }
.server-url {
margin-top: 20px;
font-size: 12px;
color: #555555;
word-break: break-all;
}
</style>
</head>
<body>
<div class="card">
<h1>FIVER COMPANION</h1>
<p>Download the companion app, install it, and open it to start screen sharing.</p>
<button onclick="window.location.href='/download/companion.apk'">DOWNLOAD & INSTALL</button>
<div class="server-url">Server: {{LOCAL_SERVER_URL}}</div>
</div>
</body>
</html>
\"\"\"
# HTML template for the Desktop viewer"""
start_idx = content.find(start_str)
end_idx = content.find(end_str) + len(end_str)
content = content[:start_idx] + new_html + content[end_idx:]
# 2. MirrorHandler attributes
attr_old = """class MirrorHandler(BaseHTTPRequestHandler):
latest_frame: Optional[bytes] = None
frame_event = threading.Event()
accepted = False
declined = False
requested = True"""
attr_new = """class MirrorHandler(BaseHTTPRequestHandler):
latest_frame: Optional[bytes] = None
frame_event = threading.Event()
accepted = False
declined = False
requested = True
apk_path = None"""
content = content.replace(attr_old, attr_new)
# 3. do_GET
doget_old = """ def do_GET(self):
if self.path == "/" or self.path == "/index.html":
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(PHONE_HTML.encode("utf-8"))"""
doget_new = """ def do_GET(self):
if self.path == "/" or self.path == "/index.html":
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
host = self.headers.get("Host", "localhost")
proto = "https" if isinstance(self.connection, ssl.SSLSocket) else "http"
html = PHONE_HTML.replace("{{LOCAL_SERVER_URL}}", f"{proto}://{host}")
self.wfile.write(html.encode("utf-8"))"""
content = content.replace(doget_old, doget_new)
# 4. /download/companion.apk endpoint
desk_old = """ elif self.path == "/desktop":
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(DESKTOP_HTML.encode("utf-8"))"""
desk_new = """ elif self.path == "/download/companion.apk":
if MirrorHandler.apk_path and os.path.exists(MirrorHandler.apk_path):
self.send_response(200)
self.send_header("Content-Type", "application/vnd.android.package-archive")
self.send_header("Content-Disposition", 'attachment; filename="companion.apk"')
with open(MirrorHandler.apk_path, "rb") as f:
apk_data = f.read()
self.send_header("Content-Length", str(len(apk_data)))
self.end_headers()
self.wfile.write(apk_data)
else:
self.send_error(404)
elif self.path == "/desktop":
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(DESKTOP_HTML.encode("utf-8"))"""
content = content.replace(desk_old, desk_new)
# 5. do_POST
post_old = """ def do_POST(self):
if self.path == "/api/frame":
content_length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(content_length).decode("utf-8")
if "," in body:
body = body.split(",", 1)[1]
try:
MirrorHandler.latest_frame = base64.b64decode(body)
MirrorHandler.accepted = True
MirrorHandler.frame_event.set()
self.send_response(200)
self.end_headers()
except Exception:
self.send_error(400)"""
post_new = """ def do_POST(self):
if self.path == "/api/frame":
content_length = int(self.headers.get("Content-Length", 0))
content_type = self.headers.get("Content-Type", "")
if content_type == "image/jpeg":
MirrorHandler.latest_frame = self.rfile.read(content_length)
MirrorHandler.accepted = True
MirrorHandler.frame_event.set()
self.send_response(200)
self.end_headers()
else:
body = self.rfile.read(content_length).decode("utf-8")
if "," in body:
body = body.split(",", 1)[1]
try:
MirrorHandler.latest_frame = base64.b64decode(body)
MirrorHandler.accepted = True
MirrorHandler.frame_event.set()
self.send_response(200)
self.end_headers()
except Exception:
self.send_error(400)"""
content = content.replace(post_old, post_new)
# 6. WebMirrorServer.set_apk
server_old = """class WebMirrorServer:
def __init__(self, host: str = "0.0.0.0", port: int = 8080):
self.host = host
self.port = port
self.server: Optional[ThreadedHTTPServer] = None
self.thread: Optional[threading.Thread] = None
self.tunnel_proc: Optional[subprocess.Popen] = None
self.public_url: Optional[str] = None
self.desktop_opened = False
self.is_ssl = False"""
server_new = """class WebMirrorServer:
def __init__(self, host: str = "0.0.0.0", port: int = 8080):
self.host = host
self.port = port
self.server: Optional[ThreadedHTTPServer] = None
self.thread: Optional[threading.Thread] = None
self.tunnel_proc: Optional[subprocess.Popen] = None
self.public_url: Optional[str] = None
self.desktop_opened = False
self.is_ssl = False
def set_apk(self, path: str):
MirrorHandler.apk_path = path"""
content = content.replace(server_old, server_new)
with open(path, "w") as f:
f.write(content)
def patch_tui():
path = "/home/cachy/github-p/fiver/src/fiver/tui.py"
with open(path, "r") as f:
content = f.read()
tui_old = ''' # Non-developer phone mode fallback (No USB debugging / no dev options needed)
self.call_from_thread(
self._update_status,
" [WEB MODE] ADB unavailable on {} - Starting Web Server & Cloudflare HTTPS Tunnel...".format(dev.ip),
)
from .web_mirror import WebMirrorServer, generate_ascii_qr, MirrorHandler
local_ip, _ = _local_subnet()
local_ip = local_ip or "127.0.0.1"
if self.web_server:
self.web_server.stop()
self.web_server = WebMirrorServer(port=8080)
self.web_server.start()
self.call_from_thread(
self._update_status,
" [CLOUDFLARE TUNNEL] Deploying secure HTTPS tunnel...",
)
public_url = self.web_server.start_cloudflare_tunnel()
proto = "https" if self.web_server.is_ssl else "http"
phone_url = public_url or f"{proto}://{local_ip}:{self.web_server.port}"
qr_box = generate_ascii_qr(phone_url)
# Open desktop viewer immediately so it's ready on PC
self.web_server.open_desktop_viewer(local_ip)
self.call_from_thread(
self._show_web_request,
dev,
phone_url,
qr_box,
)
# Poll until phone accepts or declines
while not MirrorHandler.accepted and not MirrorHandler.declined:
time.sleep(0.5)
if MirrorHandler.accepted:
self.call_from_thread(
self._update_status,
" [SUCCESS] Request accepted! Live mobile screen stream is active on desktop.",
)
else:
self.call_from_thread(
self._update_status,
" [DECLINED] Request declined by phone user.",
)'''
tui_new = ''' # Non-developer phone fallback — build companion APK and serve it
self.call_from_thread(
self._update_status,
" [COMPANION MODE] ADB unavailable — building companion app...",
)
from .web_mirror import WebMirrorServer, generate_ascii_qr, MirrorHandler
from .apk_builder import APKBuilder
local_ip, _ = _local_subnet()
local_ip = local_ip or "127.0.0.1"
# Start web server first to get the port
if self.web_server:
self.web_server.stop()
self.web_server = WebMirrorServer(port=8080)
server_addr = self.web_server.start()
# Build companion APK with server address embedded
server_url = f"http://{local_ip}:{self.web_server.port}"
try:
self.call_from_thread(
self._update_status,
" [BUILDING APK] Compiling companion app (first time may take a moment)...",
)
builder = APKBuilder()
apk_path = builder.build_apk(server_url)
self.web_server.set_apk(apk_path)
self.call_from_thread(
self._update_status,
" [APK READY] Companion app built successfully.",
)
except RuntimeError as e:
self.call_from_thread(
self._update_status,
f" [ERROR] Failed to build companion app: {e}",
)
return
# Start Cloudflare tunnel for QR code access
self.call_from_thread(
self._update_status,
" [CLOUDFLARE TUNNEL] Deploying secure HTTPS tunnel...",
)
public_url = self.web_server.start_cloudflare_tunnel()
phone_url = public_url or server_url
qr_box = generate_ascii_qr(phone_url)
# Open desktop viewer
self.web_server.open_desktop_viewer(local_ip)
self.call_from_thread(
self._show_web_request,
dev,
phone_url,
qr_box,
)
# Poll until phone starts streaming or user exits
while not MirrorHandler.accepted and not MirrorHandler.declined:
time.sleep(0.5)
if MirrorHandler.accepted:
self.call_from_thread(
self._update_status,
" [SUCCESS] Companion app connected! Live screen stream active.",
)
else:
self.call_from_thread(
self._update_status,
" [DECLINED] Request declined by phone user.",
)'''
content = content.replace(tui_old, tui_new)
# 7. update _show_web_request
show_old = ''' def _show_web_request(self, dev, url: str, qr_box: str):
self.query_one("#banner", Static).update("NON-DEVELOPER PHONE REQUEST SENT")
self.query_one("#hint", Label).update(f"Request sent to {dev.ip}! Open link on phone: {url}")
self.query_one("#phase", Label).update("Waiting for phone user to tap ACCEPT...")'''
show_new = ''' def _show_web_request(self, dev, url: str, qr_box: str):
self.query_one("#banner", Static).update("COMPANION APP")
self.query_one("#hint", Label).update(f"Scan QR code to download companion app for {dev.ip}!")
self.query_one("#phase", Label).update("Waiting for phone user to install and open app...")'''
content = content.replace(show_old, show_new)
with open(path, "w") as f:
f.write(content)
patch_web_mirror()
patch_tui()
print("Patching complete.")