Sebebeb commited on
Commit
4f8904a
·
verified ·
1 Parent(s): e6c829c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +285 -184
app.py CHANGED
@@ -1,206 +1,307 @@
 
 
1
  import os
 
 
2
  import threading
 
 
 
3
 
4
- from flask import Flask, request, Response, jsonify
5
- from playwright.sync_api import sync_playwright
 
 
6
 
7
  app = Flask(__name__)
8
 
9
- VIEWPORT = {"width": 1280, "height": 800}
10
-
11
- _lock = threading.Lock()
12
- _state = {"playwright": None, "browser": None, "page": None}
13
-
14
- USER_AGENT = (
15
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
16
  "(KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
17
  )
18
-
19
- STEALTH_JS = """
20
- Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
21
- Object.defineProperty(navigator, 'plugins', {get: () => [1, 2, 3, 4, 5]});
22
- Object.defineProperty(navigator, 'languages', {get: () => ['en-US', 'en']});
23
- window.chrome = { runtime: {} };
24
- const originalQuery = window.navigator.permissions.query;
25
- window.navigator.permissions.query = (parameters) => (
26
- parameters.name === 'notifications'
27
- ? Promise.resolve({ state: Notification.permission })
28
- : originalQuery(parameters)
29
- );
30
- """
31
-
32
-
33
- def get_page():
34
- if _state["page"] is None:
35
- _state["playwright"] = sync_playwright().start()
36
- _state["browser"] = _state["playwright"].chromium.launch(
37
- headless=True,
38
- args=[
39
- "--no-sandbox",
40
- "--disable-dev-shm-usage",
41
- "--disable-blink-features=AutomationControlled",
42
- ],
43
- ignore_default_args=["--enable-automation"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  )
45
- context = _state["browser"].new_context(
46
- viewport=VIEWPORT,
47
- user_agent=USER_AGENT,
48
- locale="en-US",
49
- timezone_id="America/New_York",
50
- )
51
- context.add_init_script(STEALTH_JS)
52
- _state["page"] = context.new_page()
53
- _state["page"].goto("https://example.com", wait_until="domcontentloaded")
54
- return _state["page"]
55
-
56
-
57
- HOME = """<!DOCTYPE html>
58
- <html><head><meta charset="utf-8"><title>Remote Browser</title>
59
- <style>
60
- html,body{margin:0;height:100%;background:#111;
61
- font-family:-apple-system,Helvetica,Arial,sans-serif;}
62
- #bar{display:flex;gap:8px;padding:10px 14px;background:#1e1e1e;}
63
- #bar input{flex:1;padding:8px 10px;border-radius:6px;border:none;font-size:14px;}
64
- #bar button{padding:8px 16px;border-radius:6px;border:none;
65
- background:#4f8cff;color:#fff;font-size:14px;cursor:pointer;}
66
- #stage{width:100%;height:calc(100vh - 52px);overflow:auto;background:#000;
67
- display:flex;justify-content:center;}
68
- #view{cursor:pointer;max-width:100%;display:block;}
69
- </style></head>
70
- <body>
71
- <div id="bar">
72
- <input id="url" value="https://example.com" />
73
- <button onclick="go()">Go</button>
74
- <button onclick="back()">&#8592;</button>
75
- </div>
76
- <div id="stage"><img id="view" src="/screenshot?t=0" /></div>
77
- <script>
78
- const img = document.getElementById('view');
79
- const urlBox = document.getElementById('url');
80
- const vw = __VW__, vh = __VH__;
81
-
82
- function refresh() { img.src = '/screenshot?t=' + Date.now(); }
83
-
84
- async function go() {
85
- await fetch('/go', {method:'POST', headers:{'Content-Type':'application/json'},
86
- body: JSON.stringify({url: urlBox.value})});
87
- setTimeout(refresh, 400);
88
- }
89
- async function back() {
90
- await fetch('/back', {method:'POST'});
91
- setTimeout(refresh, 400);
92
- }
93
-
94
- img.addEventListener('click', async (e) => {
95
- const rect = img.getBoundingClientRect();
96
- const x = (e.clientX - rect.left) / rect.width * vw;
97
- const y = (e.clientY - rect.top) / rect.height * vh;
98
- await fetch('/click', {method:'POST', headers:{'Content-Type':'application/json'},
99
- body: JSON.stringify({x, y})});
100
- setTimeout(refresh, 150);
101
- });
102
-
103
- document.addEventListener('keydown', async (e) => {
104
- if (document.activeElement === urlBox) return;
105
- e.preventDefault();
106
- await fetch('/key', {method:'POST', headers:{'Content-Type':'application/json'},
107
- body: JSON.stringify({key: e.key})});
108
- refresh();
109
- });
110
-
111
- document.getElementById('stage').addEventListener('wheel', async (e) => {
112
- await fetch('/scroll', {method:'POST', headers:{'Content-Type':'application/json'},
113
- body: JSON.stringify({dy: e.deltaY})});
114
- refresh();
115
- });
116
-
117
- setInterval(refresh, 500);
118
- </script>
119
- </body></html>"""
120
-
121
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  @app.route("/")
123
  def home():
124
- html = HOME.replace("__VW__", str(VIEWPORT["width"])).replace(
125
- "__VH__", str(VIEWPORT["height"])
 
 
 
 
 
 
 
 
 
 
126
  )
127
- return html
128
-
129
-
130
- @app.route("/screenshot")
131
- def screenshot():
132
- with _lock:
133
- page = get_page()
134
- try:
135
- data = page.screenshot(type="jpeg", quality=100)
136
- except Exception:
137
- data = b""
138
- return Response(data, content_type="image/jpeg")
139
-
140
-
141
- @app.route("/go", methods=["POST"])
142
- def go():
143
- url = (request.json or {}).get("url", "").strip()
144
- if not url:
145
- return jsonify(ok=False)
146
- if not url.startswith(("http://", "https://")):
147
- url = "https://" + url
148
- with _lock:
149
- page = get_page()
150
- try:
151
- page.goto(url, wait_until="domcontentloaded", timeout=20000)
152
- except Exception as e:
153
- return jsonify(ok=False, error=str(e))
154
- return jsonify(ok=True)
155
-
156
-
157
- @app.route("/back", methods=["POST"])
158
- def back():
159
- with _lock:
160
- page = get_page()
161
- try:
162
- page.go_back(timeout=10000)
163
- except Exception:
164
- pass
165
- return jsonify(ok=True)
166
 
167
 
168
- @app.route("/click", methods=["POST"])
169
- def click():
170
- data = request.json or {}
171
- with _lock:
172
- page = get_page()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  try:
174
- page.mouse.click(data.get("x", 0), data.get("y", 0))
175
- except Exception:
176
- pass
177
- return jsonify(ok=True)
178
-
179
 
180
- @app.route("/key", methods=["POST"])
181
- def key():
182
- data = request.json or {}
183
- with _lock:
184
- page = get_page()
185
- try:
186
- page.keyboard.press(data.get("key", ""))
187
- except Exception:
188
- pass
189
- return jsonify(ok=True)
190
 
191
 
192
- @app.route("/scroll", methods=["POST"])
193
- def scroll():
194
- data = request.json or {}
195
- with _lock:
196
- page = get_page()
197
- try:
198
- page.mouse.wheel(0, data.get("dy", 0))
199
- except Exception:
200
- pass
201
- return jsonify(ok=True)
 
 
202
 
203
 
204
  if __name__ == "__main__":
205
- port = int(os.environ.get("PORT", 7860))
206
- app.run(host="0.0.0.0", port=port, threaded=False)
 
1
+ import ipaddress
2
+ import json
3
  import os
4
+ import re
5
+ import socket
6
  import threading
7
+ import time
8
+ import uuid
9
+ from urllib.parse import parse_qs, quote, urldefrag, urlencode, urljoin, urlparse
10
 
11
+ import requests
12
+ from bs4 import BeautifulSoup
13
+ from bs4.element import Doctype
14
+ from flask import Flask, Response, g, redirect, request, send_from_directory
15
 
16
  app = Flask(__name__)
17
 
18
+ HERE = os.path.dirname(os.path.abspath(__file__))
19
+ ALLOW_PRIVATE = os.environ.get("PX_ALLOW_PRIVATE") == "1" # testing only
20
+ DEFAULT_UA = (
 
 
 
21
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
22
  "(KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
23
  )
24
+ METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"]
25
+ URL_ATTRS = ("href", "src", "action", "poster", "data", "formaction", "background")
26
+ SKIP_PREFIXES = ("#", "data:", "blob:", "javascript:", "mailto:", "tel:", "about:")
27
+
28
+ # ---------------------------------------------------------------- sessions
29
+ # One server-side cookie jar per visitor, so logins made through the proxy
30
+ # stick. Cookies from sites never reach the visitor's browser.
31
+ SESSIONS = {}
32
+ S_LOCK = threading.Lock()
33
+ MAX_SESSIONS = 200
34
+
35
+
36
+ def get_session(sid):
37
+ now = time.time()
38
+ with S_LOCK:
39
+ if len(SESSIONS) > MAX_SESSIONS:
40
+ for k in sorted(SESSIONS, key=lambda k: SESSIONS[k][1])[:50]:
41
+ del SESSIONS[k]
42
+ entry = SESSIONS.get(sid)
43
+ if entry is None:
44
+ sess = requests.Session()
45
+ sess.trust_env = False
46
+ entry = SESSIONS[sid] = [sess, now]
47
+ entry[1] = now
48
+ return entry[0]
49
+
50
+
51
+ @app.before_request
52
+ def assign_sid():
53
+ g.sid = request.cookies.get("px_sid")
54
+ g.new_sid = False
55
+ if not g.sid:
56
+ g.sid = uuid.uuid4().hex
57
+ g.new_sid = True
58
+
59
+
60
+ @app.after_request
61
+ def persist_sid(resp):
62
+ if g.get("new_sid"):
63
+ resp.set_cookie(
64
+ "px_sid", g.sid, max_age=60 * 60 * 24 * 30,
65
+ httponly=True, secure=True, samesite="None",
66
  )
67
+ return resp
68
+
69
+
70
+ # ---------------------------------------------------------------- safety
71
+ def is_safe(url):
72
+ """Refuse non-http(s) and anything that resolves to a non-public address."""
73
+ p = urlparse(url)
74
+ if p.scheme not in ("http", "https") or not p.hostname:
75
+ return False
76
+ if ALLOW_PRIVATE:
77
+ return True
78
+ try:
79
+ infos = socket.getaddrinfo(p.hostname, p.port or (443 if p.scheme == "https" else 80))
80
+ except socket.gaierror:
81
+ return False
82
+ return all(ipaddress.ip_address(i[4][0]).is_global for i in infos)
83
+
84
+
85
+ # ---------------------------------------------------------------- rewriting
86
+ def px(url, base):
87
+ if not url or not url.strip():
88
+ return url
89
+ u = url.strip()
90
+ if u.lower().startswith(SKIP_PREFIXES):
91
+ return url
92
+ absu = urljoin(base, u)
93
+ if not absu.startswith(("http://", "https://")):
94
+ return url
95
+ absu, frag = urldefrag(absu)
96
+ return "/p?u=" + quote(absu, safe="") + ("#" + frag if frag else "")
97
+
98
+
99
+ CSS_URL = re.compile(r"""url\(\s*(['"]?)(.*?)\1\s*\)""", re.I)
100
+ CSS_IMPORT = re.compile(r"""@import\s+(['"])(.*?)\1""", re.I)
101
+
102
+
103
+ def rewrite_css(css, base):
104
+ css = CSS_URL.sub(lambda m: "url(%s%s%s)" % (m.group(1), px(m.group(2), base), m.group(1)), css)
105
+ css = CSS_IMPORT.sub(lambda m: "@import %s%s%s" % (m.group(1), px(m.group(2), base), m.group(1)), css)
106
+ return css
107
+
108
+
109
+ def rewrite_srcset(value, base):
110
+ out = []
111
+ for part in value.split(","):
112
+ bits = part.strip().split(None, 1)
113
+ if bits:
114
+ out.append(" ".join([px(bits[0], base)] + bits[1:]))
115
+ return ", ".join(out)
116
+
117
+
118
+ def rewrite_html(raw, doc_url):
119
+ soup = BeautifulSoup(raw, "html.parser")
120
+ base = doc_url
121
+
122
+ b = soup.find("base", href=True)
123
+ if b:
124
+ base = urljoin(doc_url, b["href"])
125
+ b.extract()
126
+
127
+ for m in soup.find_all("meta"):
128
+ equiv = (m.get("http-equiv") or "").lower()
129
+ if equiv == "content-security-policy":
130
+ m.extract()
131
+ elif equiv == "refresh" and m.get("content"):
132
+ mm = re.match(r"\s*(\d+)\s*;\s*url=(.*)", m["content"], re.I)
133
+ if mm:
134
+ m["content"] = "%s;url=%s" % (mm.group(1), px(mm.group(2).strip(" '\""), base))
135
+
136
+ for tag in soup.find_all(True):
137
+ if tag.name == "link" and {"preconnect", "dns-prefetch", "prefetch", "prerender"} & set(tag.get("rel") or []):
138
+ tag.extract() # these would make the visitor's browser contact the origin directly
139
+ continue
140
+ for attr in ("integrity", "nonce", "ping"):
141
+ tag.attrs.pop(attr, None)
142
+
143
+ if tag.name == "form" and (tag.get("method") or "get").lower() == "get":
144
+ # Browsers drop the query string of a GET form's action, so carry the
145
+ # real destination in a hidden field instead.
146
+ dest = urljoin(base, tag.get("action") or "")
147
+ tag["action"] = "/p"
148
+ tag.insert(0, soup.new_tag("input", attrs={"type": "hidden", "name": "__px_u", "value": dest}))
149
+ else:
150
+ for attr in URL_ATTRS:
151
+ v = tag.get(attr)
152
+ if isinstance(v, str) and v:
153
+ tag[attr] = px(v, base)
154
+
155
+ for attr in ("srcset", "imagesrcset"):
156
+ if isinstance(tag.get(attr), str):
157
+ tag[attr] = rewrite_srcset(tag[attr], base)
158
+ if isinstance(tag.get("style"), str):
159
+ tag["style"] = rewrite_css(tag["style"], base)
160
+ if tag.name == "style" and tag.string:
161
+ tag.string = rewrite_css(tag.string, base)
162
+
163
+ boot = soup.new_tag("script")
164
+ boot.string = "window.__PX_BASE__=%s;" % json.dumps(doc_url).replace("</", "<\\/")
165
+ shim = soup.new_tag("script", src="/shim.js")
166
+
167
+ head = soup.find("head")
168
+ if head is None:
169
+ head = soup.new_tag("head")
170
+ html = soup.find("html")
171
+ if html:
172
+ html.insert(0, head)
173
+ else:
174
+ soup.insert(1 if soup.contents and isinstance(soup.contents[0], Doctype) else 0, head)
175
+ head.insert(0, shim)
176
+ head.insert(0, boot)
177
+ return str(soup)
178
+
179
+
180
+ SHELL = """<!DOCTYPE html><html><head><meta charset="utf-8">
181
+ <meta name="viewport" content="width=device-width,initial-scale=1"><title>%(title)s</title>
182
+ <style>body{margin:0;background:#111;color:#ddd;font-family:-apple-system,Helvetica,Arial,sans-serif;
183
+ display:flex;align-items:center;justify-content:center;min-height:100vh}
184
+ main{text-align:center;max-width:460px;padding:24px;line-height:1.5}a{color:#7aa7ff}</style>
185
+ <script>window.__PX_BASE__="";</script><script src="/shim.js"></script></head>
186
+ <body><main>%(body)s</main></body></html>"""
187
+
188
+
189
+ def shell(title, body, status=200):
190
+ return Response(SHELL % {"title": title, "body": body}, status=status, content_type="text/html; charset=utf-8")
191
+
192
+
193
+ # ---------------------------------------------------------------- routes
194
  @app.route("/")
195
  def home():
196
+ links = " &middot; ".join(
197
+ '<a href="/p?u=%s">%s</a>' % (quote(u, safe=""), n)
198
+ for n, u in [
199
+ ("DuckDuckGo", "https://duckduckgo.com/html/"),
200
+ ("Wikipedia", "https://en.wikipedia.org/"),
201
+ ("Hacker News", "https://news.ycombinator.com/"),
202
+ ]
203
+ )
204
+ return shell(
205
+ "Browser",
206
+ "<h2>Type a URL or search in the bar</h2>"
207
+ "<p>Every request is made by this server. Rendering happens in your browser.</p><p>%s</p>" % links,
208
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
 
211
+ @app.route("/shim.js")
212
+ def shim_js():
213
+ resp = send_from_directory(HERE, "shim.js", mimetype="application/javascript")
214
+ resp.headers["Cache-Control"] = "no-cache"
215
+ return resp
216
+
217
+
218
+ @app.route("/p", methods=METHODS)
219
+ def proxy():
220
+ args = request.args
221
+ if "__px_u" in args: # GET form submission
222
+ fields = [(k, v) for k, v in args.items(multi=True) if k != "__px_u"]
223
+ target = urlparse(args["__px_u"])._replace(query=urlencode(fields), fragment="").geturl()
224
+ else:
225
+ target = args.get("u", "").strip()
226
+ if not target:
227
+ return redirect("/")
228
+ if not is_safe(target):
229
+ return shell("Blocked", "<h3>Blocked</h3><p>That address isn't reachable through this proxy.</p>", 403)
230
+
231
+ origin = "%s://%s" % (urlparse(target).scheme, urlparse(target).netloc)
232
+ headers = {
233
+ "User-Agent": request.headers.get("User-Agent", DEFAULT_UA),
234
+ "Accept": request.headers.get("Accept", "*/*"),
235
+ "Accept-Language": request.headers.get("Accept-Language", "en-US,en;q=0.9"),
236
+ "Accept-Encoding": "identity",
237
+ "Referer": origin + "/",
238
+ }
239
+ for h in ("Content-Type", "Range"):
240
+ if request.headers.get(h):
241
+ headers[h] = request.headers[h]
242
+ body = None
243
+ if request.method not in ("GET", "HEAD"):
244
+ headers["Origin"] = origin
245
+ body = request.get_data()
246
+
247
+ try:
248
+ r = get_session(g.sid).request(
249
+ request.method, target, headers=headers, data=body,
250
+ allow_redirects=False, stream=True, timeout=(10, 30),
251
+ )
252
+ except requests.RequestException as e:
253
+ return shell("Error", "<h3>Couldn't load that page</h3><p>%s</p>" % str(e).replace("<", "&lt;"), 502)
254
+
255
+ if r.status_code in (301, 302, 303, 307, 308) and r.headers.get("Location"):
256
+ resp = Response(status=r.status_code)
257
+ resp.headers["Location"] = px(urljoin(target, r.headers["Location"]), target)
258
+ r.close()
259
+ return resp
260
+
261
+ ctype = r.headers.get("Content-Type", "")
262
+ kind = ctype.split(";")[0].strip().lower()
263
+
264
+ if kind in ("text/html", "application/xhtml+xml"):
265
+ out = rewrite_html(r.content, target)
266
+ resp = Response(out, status=r.status_code, content_type="text/html; charset=utf-8")
267
+ resp.headers["Cache-Control"] = "no-store"
268
+ return resp
269
+
270
+ if kind == "text/css":
271
+ out = rewrite_css(r.content.decode(r.encoding or "utf-8", "replace"), target)
272
+ return Response(out, status=r.status_code, content_type="text/css; charset=utf-8")
273
+
274
+ keep = {}
275
+ for h in ("Content-Type", "Content-Range", "Accept-Ranges", "Content-Disposition", "Cache-Control"):
276
+ if r.headers.get(h):
277
+ keep[h] = r.headers[h]
278
+ if r.headers.get("Content-Length") and r.headers.get("Content-Encoding", "identity") == "identity":
279
+ keep["Content-Length"] = r.headers["Content-Length"]
280
+
281
+ def stream():
282
  try:
283
+ for chunk in r.iter_content(65536):
284
+ if chunk:
285
+ yield chunk
286
+ finally:
287
+ r.close()
288
 
289
+ return Response(stream(), status=r.status_code, headers=keep)
 
 
 
 
 
 
 
 
 
290
 
291
 
292
+ @app.route("/<path:path>", methods=METHODS)
293
+ def fallback(path):
294
+ """Catch root-relative requests that escaped rewriting (e.g. built in JS)
295
+ and re-aim them using the page they came from."""
296
+ ref = urlparse(request.headers.get("Referer", ""))
297
+ q = parse_qs(ref.query)
298
+ if ref.path == "/p":
299
+ page = (q.get("__px_u") or q.get("u") or [None])[0]
300
+ if page:
301
+ real = urljoin(page, request.full_path.rstrip("?"))
302
+ return redirect(px(real, page), code=307)
303
+ return shell("Not found", "<h3>Not found</h3>", 404)
304
 
305
 
306
  if __name__ == "__main__":
307
+ app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)), threaded=True)