Skip to content

Commit f061629

Browse files
author
Alexandru Meterez
committed
Remove bmob proxy
1 parent 268c844 commit f061629

20 files changed

+81
-855
lines changed

fred/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from flask import Flask
2+
3+
4+
class MyServer(Flask):
5+
6+
def __init__(self, *args, **kwargs):
7+
super(MyServer, self).__init__(*args, **kwargs)
8+
9+
# instanciate your variables here
10+
self.proxy = None
11+
self.server = None
12+
13+
14+
app = MyServer(__name__, static_url_path='')
15+
# app = Flask(__name__, static_url_path='')

fred/config/proxy.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from browsermobproxy import Server
2+
3+
4+
def init(app):
5+
server = Server("utils/browsermob_proxy/bin/browsermob-proxy", options={'port': 8090})
6+
server.start()
7+
proxy = server.create_proxy()
8+
9+
app.proxy = proxy
10+
app.server = server
11+
12+
13+
def get_proxy(app):
14+
return app.proxy
15+
16+
17+
def get_server(app):
18+
return app.server

fred/data/collect.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
import json
66
import base64
7-
from run import proxy
7+
import requests
88

99
SCRIPT = """var body = document.body,
1010
html = document.documentElement;
@@ -38,19 +38,18 @@ def evaluate(script):
3838
return base64.b64decode(screenshot['data'])
3939

4040

41-
def collect_data(url, output_folder, output_filename):
41+
def collect_data(url, output_folder, output_filename, proxy_host, proxy_port):
4242
if not os.path.exists("./tmp"):
4343
os.mkdir("./tmp")
4444
output_folder = os.path.join("./tmp", output_folder)
4545
if not os.path.exists(output_folder):
4646
os.mkdir(output_folder)
47-
4847
options = webdriver.ChromeOptions()
4948
options.add_argument("--start-maximized")
5049
options.add_argument("--force-device-scale-factor=2")
5150
options.add_argument("--disable-infobars")
5251
options.add_argument("--headless")
53-
options.add_argument('--proxy-server=%s' % proxy.proxy)
52+
options.add_argument('--proxy-server=localhost:{}'.format(proxy_port))
5453
options.add_argument('--no-sandbox')
5554
options.add_argument('--disable-dev-shm-usage')
5655
capabilities = DesiredCapabilities.CHROME
@@ -60,14 +59,16 @@ def collect_data(url, output_folder, output_filename):
6059
desired_capabilities=capabilities, service_args=["--verbose"])
6160
driver.maximize_window()
6261
driver.fullscreen_window()
63-
proxy.new_har("Logs")
62+
# proxy.new_har("Logs")
63+
requests.put('%s/proxy/%s/har' % (proxy_host, proxy_port), {'initialPageTitle': 'Logs'})
6464
driver.get(url)
65-
65+
print(driver.get_log('browser'))
6666
logs = driver.get_log('browser')
6767
with open(os.path.join(output_folder, output_filename.split('.')[0] + '_js_log.json'), 'w') as f:
6868
json.dump(logs, f, indent=2)
6969
with open(os.path.join(output_folder, output_filename.split('.')[0] + '_network_log.json'), 'w') as f:
70-
json.dump(proxy.har, f, indent=2)
70+
r = requests.get('%s/proxy/%s/har' % (proxy_host, proxy_port))
71+
json.dump(r.json(), f, indent=2)
7172

7273
height = driver.execute_script(SCRIPT)
7374
for i in range(4):

fred/endpoints/verify.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44
import uuid
55
from utils.utils import get_time, check_unique_prefix
66
from config.status_codes import STATUS_CODES
7+
from config.proxy import get_proxy
8+
import socket
9+
from urllib.parse import urlparse
710

811
states = {}
912
id_to_urls = {}
1013

1114

1215
class Verify(Resource):
16+
def __init__(self, app):
17+
self.app = app
18+
1319
def post(self):
1420
baseline_url = request.json['baseline_url']
1521
updated_url = request.json['updated_url']
22+
proxy = get_proxy(self.app)
23+
baseline_url_host = urlparse(baseline_url)
24+
updated_url_host = urlparse(updated_url)
25+
print(baseline_url_host.netloc)
26+
print(socket.gethostbyname(baseline_url))
27+
# proxy.remap_hosts(baseline_url_host.netloc, socket.gethostbyname(baseline_url))
28+
# proxy.remap_hosts(updated_url_host.netloc, socket.gethostbyname(updated_url))
29+
proxy.remap_hosts(baseline_url_host.netloc, 'www.google.com')
30+
proxy.remap_hosts(updated_url_host.netloc, 'www.google.com')
1631
max_depth = request.json['max_depth']
1732
max_urls = request.json['max_urls']
1833
prefix = request.json['prefix']
@@ -29,7 +44,8 @@ def post(self):
2944
'--max-depth',
3045
max_depth, '--max-urls', max_urls, '--prefix', prefix, '--auth-baseline-username', auth_baseline_username,
3146
'--auth-baseline-password', auth_baseline_password, '--auth-updated-username', auth_updated_username,
32-
'--auth-updated-password', auth_updated_password])
47+
'--auth-updated-password', auth_updated_password, '--proxy-host', proxy.host, '--proxy-port',
48+
str(proxy.port)])
3349
if p.poll() is not None and p.poll() > 0:
3450
return {'Error': 'Failed to launch crawler'}, 406
3551
id = str(uuid.uuid4().hex)

fred/run.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from config.proxy import get_server, init
2+
13
from flask import Flask
24
from flask_restful import Api
35
from flask_cors import CORS
@@ -8,7 +10,10 @@
810
from endpoints.get_result import Result
911
from apscheduler.schedulers.background import BackgroundScheduler
1012
from flask import Flask, request, send_from_directory
11-
from browsermobproxy import Server
13+
import sys
14+
15+
sys.path.append('../')
16+
from fred import app
1217

1318

1419
def clear_ended():
@@ -18,31 +23,28 @@ def clear_ended():
1823
del id_to_urls[k]
1924

2025

21-
app = Flask(__name__, static_url_path='')
22-
23-
2426
@app.route('/static/<path:path>')
2527
def send_js(path):
2628
return send_from_directory('frontend', path)
2729

2830

29-
if app.config["DEBUG"]:
30-
@app.after_request
31-
def after_request(response):
32-
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
33-
response.headers["Expires"] = 0
34-
response.headers["Pragma"] = "no-cache"
35-
return response
36-
server = Server("utils/browsermob_proxy/bin/browsermob-proxy", options={'port': 8090})
37-
server.start()
38-
proxy = server.create_proxy()
39-
cors = CORS(app, resources={r"*": {"origins": "*"}})
40-
api = Api(app)
41-
api.add_resource(Verify, "/api/verify")
42-
api.add_resource(IDList, "/api/ids")
43-
api.add_resource(Shutdown, "/api/shutdown")
44-
api.add_resource(Result, "/api/result")
45-
4631
if __name__ == '__main__':
32+
if app.config["DEBUG"]:
33+
@app.after_request
34+
def after_request(response):
35+
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
36+
response.headers["Expires"] = 0
37+
response.headers["Pragma"] = "no-cache"
38+
return response
39+
cors = CORS(app, resources={r"*": {"origins": "*"}})
40+
# print(hex(id(app)))
41+
api = Api(app)
42+
api.add_resource(Verify, "/api/verify", resource_class_kwargs={'app': app})
43+
api.add_resource(IDList, "/api/ids")
44+
api.add_resource(Shutdown, "/api/shutdown")
45+
api.add_resource(Result, "/api/result")
46+
init(app)
47+
4748
app.run(host='0.0.0.0', debug=True)
49+
server = get_server(app)
4850
server.stop()

fred/utils/browsermob_proxy/LICENSE.txt

Lines changed: 0 additions & 203 deletions
This file was deleted.

0 commit comments

Comments
 (0)