Last active
September 19, 2018 05:29
-
-
Save DylanLacey/22b7e462ecfd9fd48007efec6fd42960 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! python | |
# encoding: utf-8 | |
import sys | |
import logging | |
import logging.handlers | |
import urllib | |
from urlparse import urlsplit, urlunsplit, urljoin | |
import monocle | |
from monocle import _o, Return | |
monocle.init("twisted") | |
from monocle.stack import eventloop | |
from monocle.stack.network import add_service | |
from monocle.stack.network.http import (HttpServer, HttpClient, HttpHeaders, | |
write_request, read_response, VERSION) | |
import re | |
PORTS = range(50000, 50050) | |
PROXY = {"host": "127.0.0.1", "port": 5555} | |
pattern = r"PROXY (\d+\.\d+\.\d+\.\d+):(\d+)" | |
f = open("pac.js", "r") | |
for line in f: | |
matched = re.search(pattern, line) | |
if matched: | |
PROXY["host"] = matched.groups()[0] | |
PROXY["port"] = int(matched.groups()[1]) | |
log = logging.getLogger("localhost_web_server") | |
mlog = logging.getLogger("monocle") | |
@_o | |
def send_log(message): | |
url = "http://localhost:8080/" | |
client = HttpClient() | |
yield client.connect("localhost", 8080, 'http', 60) | |
yield client.request(url, None, 'POST', message) | |
# class ProxyingHttpClient(HttpClient): | |
# @_o | |
# def request(self, url, headers=None, method='GET', body=None): | |
# if not headers: | |
# headers = HttpHeaders() | |
# headers.setdefault('User-Agent', 'monocle/%s' % VERSION) | |
# if body is not None: | |
# if method is 'CONNECT': | |
# del headers['Content-Length'] | |
# yield write_request(self.client, method, url, headers, body) | |
# response = yield read_response(self.client) | |
# yield Return(response) | |
# @_o | |
# def proxy(req): | |
# client = ProxyingHttpClient() | |
# headers = HttpHeaders() | |
# for key, vals in req.requestHeaders.getAllRawHeaders(): | |
# for val in vals: | |
# headers.add(key, val) | |
# body = req.content.getvalue() | |
# urldict = urlsplit(req.uri, 'http') | |
# scheme = urldict.scheme | |
# try: | |
# yield client.connect(PROXY['host'], PROXY['port'], scheme=scheme) | |
# except Exception as error: | |
# yield send_log(error) | |
# log.info(error) | |
# content = ("""<div style="font-size: 30px; text-align: center">""" | |
# """ Couldn't contact Sauce Connect.</div>""") | |
# yield Return(200, {}, content) | |
# path=urldict.path | |
# yield send_log("Requesting (%s) %s from %s" % (req.method, path, headers['host'])) | |
# log.debug("Requesting (%s) %s from %s" % (req.method, path, headers['host'])) | |
# resp = yield client.request(path, | |
# method=req.method, | |
# headers=headers, | |
# body=body) | |
# yield send_log("response from tunnel for localhost proxy: %s" % resp.code) | |
# log.info("response from tunnel for localhost proxy: %s", resp.code) | |
# yield Return(int(resp.code), resp.headers, resp.body) | |
def main(): | |
ch = logging.StreamHandler(sys.stdout) | |
ch.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
ch.setFormatter(formatter) | |
log.addHandler(ch) | |
log.setLevel("DEBUG") | |
mlog.addHandler(ch) | |
mlog.setLevel("INFO") | |
log.info("Proxying to %s on %s" % (PROXY["host"], PROXY["port"])) | |
send_log("Proxying to %s on %s" % (PROXY["host"], PROXY["port"])) | |
for port in PORTS: | |
try: | |
if monocle.VERSION < '0.26': | |
# log.debug("Adding port %s", port) | |
send_log("Adding port %s" % port) | |
add_service(HttpServer(proxy, port)) | |
else: | |
# log.debug("Adding port %s", port) | |
send_log("Adding port %s" % port) | |
add_service(HttpServer(port, proxy)) | |
except Exception: | |
log.exception("Failed to listen on port %s, skipping", port) | |
main() | |
eventloop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment