-
Notifications
You must be signed in to change notification settings - Fork 6
/
sessions.py
161 lines (135 loc) · 5.73 KB
/
sessions.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import time
import os
import re
import pickle
import json
import copy
import shutil
import constants
import traceback
from random import uniform
from tqdm import tqdm
from bs4 import BeautifulSoup
from stem import Signal
from stem.control import Controller
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
class SessionBase(object):
def __init__(self, max_download_delay):
self.session = None
self.max_download_delay = max_download_delay
def open_new_session(self, *args, **kwargs):
raise NotImplementedError()
def get(self, url):
raise NotImplementedError()
def quit(self):
raise NotImplementedError()
def request(self, url, max_attempts=constants.MAX_RETRIES):
rand = uniform(0, self.max_download_delay)
for _ in range(max_attempts):
try:
self.get(url)
time.sleep(rand)
except:
try:
self.open_new_session()
except:
pass
else:
return
class TouchVPNSession(SessionBase):
def __init__(self, max_download_delay, firefox_profile_dir, touchvpn_loc, start_with_vpn=False):
super(TouchVPNSession, self).__init__(max_download_delay)
self.firefox_profile_dir = firefox_profile_dir
self.touchvpn_loc = touchvpn_loc
self.open_new_session(start_with_vpn)
def open_new_session(self, with_vpn=True):
self.quit()
if with_vpn:
profile = webdriver.FirefoxProfile(self.firefox_profile_dir)
profile.set_preference("dom.push.enabled", False)
self.session = webdriver.Firefox(profile)
self.session.maximize_window()
try:
self.session.get(self.touchvpn_loc)
time.sleep(5)
try:
# assume VPN is disconnected
self.session.find_element_by_xpath("//div[@id='ConnectionButton'][@class='disconnected']").click()
except:
# VPN is connected; disconnect first
self.session.find_element_by_xpath("//div[@id='ConnectionButton'][@class='postConnection']").click()
WebDriverWait(self.session, 50).until(
EC.presence_of_element_located(
(By.XPATH, "//div[@id='ConnectionButton'][@class='disconnected']")
)
)
# connect again
self.session.find_element_by_xpath("//div[@id='ConnectionButton'][@class='disconnected']").click()
finally:
WebDriverWait(self.session, 50).until(
EC.presence_of_element_located(
(By.XPATH, "//div[@id='ConnectionButton'][@class='postConnection']")
)
)
except:
self.quit()
# fall back to regular connection
pass
else:
# Successfully connected to VPN
time.sleep(5)
return
profile = webdriver.FirefoxProfile(self.firefox_profile_dir)
profile.set_preference("dom.push.enabled", False)
self.session = webdriver.Firefox(profile)
self.session.maximize_window()
def get(self, url):
self.session.get(url)
def quit(self):
if self.session:
self.session.quit()
class TorSeleniumSession(SessionBase):
def __init__(self, max_download_delay, firefox_profile_dir, password, start_with_tor=False):
super(TorSeleniumSession, self).__init__(max_download_delay)
self.firefox_profile_dir = firefox_profile_dir
self.tor_profile = self.get_tor_profile()
self.password = password
self.open_new_session(start_with_tor)
def get_tor_profile(self):
profile = webdriver.FirefoxProfile(self.firefox_profile_dir)
profile.set_preference("dom.push.enabled", False)
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference('permissions.default.image', 2)
profile.set_preference('browser.link.open_newwindow', 1)
return profile
def renew_tor_ip(self):
with Controller.from_port(port=9051) as controller:
controller.authenticate(password=self.password)
controller.signal(Signal.NEWNYM)
def open_new_session(self, with_tor=True):
self.quit()
if with_tor:
self.session = webdriver.Firefox(self.get_tor_profile())
self.session.maximize_window()
self.renew_tor_ip()
time.sleep(15)
return
profile = webdriver.FirefoxProfile(self.firefox_profile_dir)
self.session = webdriver.Firefox(profile)
self.session.maximize_window()
def get(self, url):
self.session.get(url)
def quit(self):
if self.session:
self.session.quit()
# TODO: Write a wrapper using requests