Skip to content

Instantly share code, notes, and snippets.

@hellais
Created May 8, 2015 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellais/995793f88fc42727fb92 to your computer and use it in GitHub Desktop.
Save hellais/995793f88fc42727fb92 to your computer and use it in GitHub Desktop.
get_bridges.py
from tempfile import NamedTemporaryFile
from base64 import b64decode
from HTMLParser import HTMLParser
import urllib2
from urllib import urlencode
def read_captcha(value):
dst_file = NamedTemporaryFile(suffix=".jpg")
base64_data = value.split(",")[1]
payload = b64decode(base64_data)
dst_file.write(payload)
return dst_file
class CaptchaPageParser(HTMLParser):
captcha = None
challenge = None
def handle_starttag(self, tag, attrs):
if tag == "img":
attrs = dict(attrs)
self.captcha = read_captcha(attrs["src"])
elif tag == "input":
attrs = dict(attrs)
if attrs["id"] == "captcha_challenge_field":
self.challenge = attrs["value"]
class BridgesPageParser(HTMLParser):
bridges = []
in_bridgelines = False
def handle_starttag(self, tag, attrs):
if tag == "div":
attrs = dict(attrs)
if attrs.get("id") == "bridgelines":
self.in_bridgelines = True
def handle_data(self, data):
if self.in_bridgelines == True:
self.bridges.append(data)
def handle_endtag(self, tag):
if tag == "div" and self.in_bridgelines == True:
self.in_bridgelines = False
def solve_captcha(bridge_type="obfs4"):
url = "https://bridges.torproject.org/bridges?transport=%s" % bridge_type
response = urllib2.urlopen(url)
captcha_parser = CaptchaPageParser()
captcha_parser.feed(response.read())
captcha_file_path = captcha_parser.captcha.name
captcha_challenge = captcha_parser.challenge
captcha_solution = raw_input("Open the file %s and solve the captcha: " % captcha_file_path)
return captcha_solution, captcha_challenge
def get_bridges(bridge_type="obfs4"):
captcha_solution, captcha_challenge = solve_captcha(bridge_type)
url = "https://bridges.torproject.org/bridges"
data = urlencode({
"captcha_response_field": captcha_solution,
"captcha_challenge_field": captcha_challenge,
"submit": "submit"
})
response = urllib2.urlopen(url, data=data)
bridges_parser = BridgesPageParser()
bridges_parser.feed(response.read())
if not bridges_parser.bridges:
print "You probably got the captcha wrong. Try again."
get_bridges(bridge_type)
print bridges_parser.bridges
get_bridges()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment