-
Notifications
You must be signed in to change notification settings - Fork 1
/
vote.py
87 lines (61 loc) · 2.28 KB
/
vote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import StringIO
import socket
import urllib
import pycurl
import json
import string
import random
import socks
import stem.process
from stem.control import Controller
from stem.util import term
SOCKS_PORT = 7000
# Set socks proxy and wrap the urllib module
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket
# Perform DNS resolution through the socket
def getaddrinfo(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo
def token_gen(size=32, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def query(url):
"""
Uses pycurl to fetch a site using the proxy on the SOCKS_PORT.
"""
output = StringIO.StringIO()
data = json.dumps({"token": token_gen(), "voteTag": "vote"})
headers = ['Accept: application/json',
'Host: www.smarttechchallenge.ca',
'Origin': 'http://www.smarttechchallenge.ca',
'Referer': 'http://www.smarttechchallenge.ca/entry/7879601',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36']
query = pycurl.Curl()
query.setopt(pycurl.URL, url)
query.setopt(pycurl.PROXY, 'localhost')
query.setopt(pycurl.PROXYPORT, SOCKS_PORT)
query.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
query.setopt(pycurl.HTTPHEADER, headers)
query.setopt(pycurl.WRITEFUNCTION, output.write)
query.setopt(pycurl.POST, 1)
query.setopt(pycurl.POSTFIELDS, data)
try:
for (i in range(0,10):
query.perform()
return output.getvalue()
except pycurl.error as exc:
return "Unable to reach %s (%s)" % (url, exc)
# Start an instance of Tor, and print Tor's bootstrap info as it starts
def print_bootstrap_lines(line):
if "Bootstrapped " in line:
print term.format(line, term.Color.BLUE)
print term.format("Starting Tor:\n", term.Attr.BOLD)
tor_process = stem.process.launch_tor_with_config(
config = {
'SocksPort': str(SOCKS_PORT)
},
init_msg_handler = print_bootstrap_lines,
)
print term.format("\nChecking our endpoint:\n", term.Attr.BOLD)
print term.format(query("http://www.smarttechchallenge.ca/js/vote/7879601/1"),term.Color.BLUE)
tor_process.kill() # stops tor