Skip to content

Commit

Permalink
Gebruik url's in de vorm proxy.piratenpartij.nl/<site> ipv <site>.pro…
Browse files Browse the repository at this point in the history
…xy.piratenpartij.nl .

Dit omzeilt problemen met ssl certificaat hostnames + het gaat 'lekken' van info over bezochte sites naar ISP DNS-servers tegen.
  • Loading branch information
Oele committed Mar 23, 2014
1 parent d74dafd commit 9468aa3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 64 deletions.
5 changes: 3 additions & 2 deletions pirateproxy-0.4/CSSPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
class CSSPage:
BLKSIZE=65536

def __init__(self, config, ssl, reader, writer):
def __init__(self, config, ssl, reader, writer, remote_host):
self.config = config
self.ssl = ssl
self.reader = reader
self.writer = writer
self.input_buffer = ''
self.output_buffer = ''
self.remote_host = remote_host

def rewrite_re(self, m):
part1 = m.group(1) or ''
scheme = m.group(6) or ''
url = m.group(7) or ''
closer = m.group(9) or ''

return part1 + Util.rewrite_URL(scheme+"//"+url, self.config, self.ssl) + closer
return part1 + Util.rewrite_URL(scheme+"//"+url, self.config, self.ssl, self.remote_host) + closer

def rewrite(self):
pattern = r"(((background(-image)?\s*:)|@import)\s*(url)?\s*[('\"]+\s*)(https?:)?//([^\"')]+)(:\d+)?([)'\"]+)"
Expand Down
3 changes: 2 additions & 1 deletion pirateproxy-0.4/JSPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
re_url = re.compile(r"(https?:\\?/\\?/)?([a-zA-Z0-9\-\.]+\.(AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CW|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|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GBS0DH|XN--S9BRJ9C|XN--WGBH1C|XN--WGBL6A|XN--XKC2AL3HYE2A|XN--XKC2DL3A5EE0H|XN--YFRO4I67O|XN--YGBI2AMMX|XN--ZCKZAH|XXX|YE|YT|ZA|ZM|ZW))(?![a-zA-Z0-9\-\.])(:\d+)?", re.I)

class JSPage(object):
def __init__(self, config, ssl, reader, writer):
def __init__(self, config, ssl, reader, writer, remote_host):
self.config = config
self.ssl = ssl
self.reader = reader
Expand All @@ -20,6 +20,7 @@ def __init__(self, config, ssl, reader, writer):
self.input_pos = 0
self.eof = False
self.output_size = 0
self.remote_host = remote_host

# Read a block of data into the input buffer. Discard any data
# in the input buffer that has already been processed.
Expand Down
15 changes: 8 additions & 7 deletions pirateproxy-0.4/Page.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Page(HtmlParser):
simple_attriburl_regex = re.compile(r"((https?:)//[^ \r\n<]+)", re.I|re.M|re.S)
counter = 0

def __init__(self, config, ssl, reader, writer):
def __init__(self, config, ssl, reader, writer, remote_host):
HtmlParser.__init__(self)
self.config = config
self.ssl = ssl
Expand All @@ -28,6 +28,7 @@ def __init__(self, config, ssl, reader, writer):
self.output_buffer = ''
self.state = -1
self.tags_and_attribs = self.create_tabs_and_attribs()
self.remote_host = remote_host

# Based on a list of tuples, create a hash-table with tagnames plus
# attrib names as keys
Expand All @@ -41,7 +42,7 @@ def create_tabs_and_attribs(self):

# Rewrite a matched URL attribute value snippet
def rewrite_url(self, m):
return Util.rewrite_URL(m.group(0), self.config, self.ssl)
return Util.rewrite_URL(m.group(0), self.config, self.ssl, self.remote_host)

def rewrite(self):
while True:
Expand Down Expand Up @@ -74,9 +75,9 @@ def handle_tag(self):
# or CSS data anymore. This is all handled in the JSPage and
# CSSPage classes.
if self.tag() == 'script':
p = JSPage(self.config, self.ssl, self.parsing_reader, self.buffered_writer)
p = JSPage(self.config, self.ssl, self.parsing_reader, self.buffered_writer, self.remote_host)
elif self.tag() == 'style':
p = CSSPage(self.config, self.ssl, self.parsing_reader, self.buffered_writer)
p = CSSPage(self.config, self.ssl, self.parsing_reader, self.buffered_writer, self.remote_host)
p.rewrite()


Expand Down Expand Up @@ -136,7 +137,7 @@ def handle_attribute(self):
# this by using StringIO on the already read value
f = StringIO(self.input_buffer[:pos])
outf = StringIO()
p = CSSPage(self.config, self.ssl, f.read, outf.write)
p = CSSPage(self.config, self.ssl, f.read, outf.write, self.remote_host)
p.rewrite()
outf.seek(0)
self.output_buffer += outf.read()
Expand All @@ -148,7 +149,7 @@ def handle_attribute(self):
# this by using StringIO on the already read value
f = StringIO(self.input_buffer[:pos])
outf = StringIO()
p = JSPage(self.config, self.ssl, f.read, outf.write)
p = JSPage(self.config, self.ssl, f.read, outf.write, self.remote_host)
p.rewrite()
outf.seek(0)
self.output_buffer += outf.read() + self.input_buffer[pos]
Expand All @@ -163,7 +164,7 @@ def handle_attribute(self):
return

# Other URL-containing attributes
val = Util.rewrite_URL(self.input_buffer[:pos], self.config, self.ssl)
val = Util.rewrite_URL(self.input_buffer[:pos], self.config, self.ssl, self.remote_host)
self.output_buffer += val
self.write_output(False)
self.input_buffer = self.input_buffer[pos:]
Expand Down
46 changes: 13 additions & 33 deletions pirateproxy-0.4/Proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def handle_redirect(self, resp):
if resp.status >= 300 and resp.status <= 400:
location = resp.getheader('location', None)
if location:
location = Util.rewrite_URL(location, self.server.config, self.is_ssl())
location = Util.rewrite_URL(location, self.server.config, self.is_ssl(), self.remote_host)
resp.newlocation = location
else:
resp.newlocation = None
Expand Down Expand Up @@ -112,18 +112,12 @@ def do_GETPOST(self, post):
if client and self.server.config.use_forwarded_for:
client = client.split(',')[-1].strip()
self.client_address = (client, self.client_address[1])

host = self.headers.getheader('host') or ''
if host:
p = host.find("."+self.server.config.hostname)
if p != -1:
self.remote_host = host[:p]
else:
self.remote_host = host

self.server.reqs[threading.currentThread().name] = (self.remote_host, self.path)
if self.handle_own() or self.handle_robot_block():
return
self.remote_host = self.path.split('/')[1]
self.path = '/' + '/'.join(self.path.split('/')[2:])
self.server.reqs[threading.currentThread().name] = (self.remote_host, self.path)


# Redirect blocked hostnames and IP addresses to block target
if self.is_blocked(self.remote_host):
Expand Down Expand Up @@ -214,7 +208,6 @@ def do_GETPOST(self, post):
self.handle_redirect(resp)

content_type = resp.msg.gettype()

if content_type in ["text/html"]:
self.handle_rewritable(resp, Page)
elif content_type in ["application/xhtml+xml", "application/xml", "application/xhtml" ]:
Expand Down Expand Up @@ -257,11 +250,12 @@ def rewrite_cookie(self, cookie):

for cookiename in c:
domain = c[cookiename].get('domain')
if domain:
# Need to strip as sometimes at least ',' is retained
domain = domain.strip(' \t\r\n,;')
domain = domain + "." + self.server.config.hostname
c[cookiename]['domain'] = domain
# Need to strip as sometimes at least ',' is retained
domain = domain.strip(' \t\r\n,;')
#domain = domain + "." + self.server.config.hostname
c[cookiename]['path'] = '/' + self.remote_host
domain = self.server.config.hostname
c[cookiename]['domain'] = domain
cookie = c.output()
except Exception, e:
self.my_log_error(traceback.format_exc())
Expand All @@ -282,7 +276,6 @@ def handle_rewritable(self, resp, rewriter_class):
else:
self.gzip_from_server = False


self.my_log_request(resp.status, self.content_length)

# Write the response headers
Expand Down Expand Up @@ -313,7 +306,7 @@ def handle_rewritable(self, resp, rewriter_class):
# are given to the Page, JSPage or CSSPage instance to read blocks
# of data from the server response and write blocks of data to the
# client. Gzip-handling is done in the reader/writer.
p = rewriter_class(self.server.config, self.is_ssl(), self.reader, self.writer)
p = rewriter_class(self.server.config, self.is_ssl(), self.reader, self.writer, self.remote_host)
p.rewrite()


Expand Down Expand Up @@ -479,26 +472,13 @@ def handle_robot_block(self):
# hostnames or non-existing host-header. Returns true if request was
# handled here.
def handle_own(self):
host = self.headers.getheader('host')

if host:
a = host.rfind(":")
if a != -1 and len(host) > a:
if host[a+1:].isdigit():
host = host[:a]
else:
host = ''

if host.endswith(self.server.config.hostname) and host != self.server.config.hostname:
if self.path != '/' and self.path != '/index.html' and self.path != '/images/piratenpartijproxybanner.jpg' and self.path != '/favicon.ico':
return False

# This is for us, so handle it
try:
if os.path.isdir(self.server.config.files_location+"/"+self.path):
self.path += '/index.html'
except Exception, e:
pass

if self.path.find('..') != -1:
self.my_log_request(403, 0)
self.send_error(403)
Expand Down
39 changes: 19 additions & 20 deletions pirateproxy-0.4/Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import urlparse
import traceback


# Strip the proxy hostname part of the passed URL
def rewrite_URL_strip(url, config):
try:
Expand All @@ -23,7 +22,7 @@ def rewrite_URL_strip(url, config):
# Rewrite the URL and add the proxy's HTTP or HTTPS ports when necessary.
# For absolute URLs without scheme, use the same scheme as used to access
# the proxy (using the 'ssl' flag)
def rewrite_URL(url, config, ssl):
def rewrite_URL(url, config, ssl, remote_host):
try:
# Strip our own hostname for the rewrites to work
url = rewrite_URL_strip(url,config)
Expand Down Expand Up @@ -54,26 +53,26 @@ def rewrite_URL(url, config, ssl):
return url

# Handle absolute HTTP or HTTPS URL
if res[1] and (res[0] == '' or res[0] == 'http' or res[0] == 'https'):
newres = [ item for item in res ]
host = res[1].split(":")[0]
newres = [ item for item in res ]
host = res[1].split(":")[0]

# No scheme, use the scheme used to access proxy
if res[0] == '': # res[0] == scheme
if ssl:
newres[0]='https'
else:
newres[0]='http'
# No scheme, use the scheme used to access proxy
if res[0] == '': # res[0] == scheme
if ssl:
newres[0]='https'
else:
newres[0]='http'

# Add port of proxy
if newres[0] == 'http':
port = config.http_port
elif newres[0] == 'https':
port = config.https_port

newres[1] = host + "." + config.hostname + ":" + str(port)
url = urlparse.urlunsplit(newres)
# Add port of proxy
if newres[0] == 'http':
port = config.http_port
elif newres[0] == 'https':
port = config.https_port
newres[1] = config.hostname + ":" + str(port)
if host == '':
host = remote_host
newres[2] = host + newres[2]
url = urlparse.urlunsplit(newres)
except Exception, e:
pass

return url
2 changes: 1 addition & 1 deletion pirateproxy-0.4/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</style>
<script type="text/javascript">
function doclick() {
document.location="http://"+document.getElementById('url').value+".proxy.piratenpartij.nl";
document.location="http://proxy.piratenpartij.nl/"+document.getElementById('url').value;
}
</script>
</head>
Expand Down

0 comments on commit 9468aa3

Please sign in to comment.