Permalink
Browse files

Add version check for certificate verification

Since default behaviour for SSL certificate verification was
changed since 2.7.9, I added some version checks so the fix is
only used in python versions from then on. This should keep
sickbeard compatible with older python versions.
  • Loading branch information...
arucard21 committed Jan 17, 2015
1 parent 6a6c36f commit f463f7d94ac466341bf94071c83813e7ff8d77b0
@@ -21,8 +21,10 @@
from __future__ import with_statement
import os.path
import sys
import ssl as sslModule
if sys.version_info >= (2, 7, 9):
import ssl as sslModule
# Try importing Python 2 modules using new names
try:
@@ -132,7 +134,10 @@ def processEpisode(dir_to_process, org_NZB_name=None):
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, username, password)
handler = HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler, urllib2.HTTPSHandler(context=sslModule._create_unverified_context()))
if sys.version_info >= (2, 7, 9):
opener = urllib2.build_opener(handler, urllib2.HTTPSHandler(context=sslModule._create_unverified_context()))
else:
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
result = opener.open(url).readlines()
View
@@ -1,6 +1,7 @@
from gntp import *
import urllib
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import Growl
def register_send(self):
@@ -56,7 +57,10 @@ def get_resource(self,key):
return self.resources.get(resource[1])['Data']
elif resource.startswith('http'):
resource = resource.replace(' ', '%20')
icon = urllib.urlopen(resource, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
icon = urllib.urlopen(resource, context=ssl._create_unverified_context())
else:
icon = urllib.urlopen(resource)
return icon.read()
else:
return None
@@ -19,7 +19,8 @@
import errno
import httplib
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import StringIO
from hashlib import md5
from threading import RLock
@@ -203,7 +204,10 @@ def geturl(self):
@locked_function
def recache(self):
new_request = urllib2.urlopen(self.url, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
new_request = urllib2.urlopen(self.url, context=ssl._create_unverified_context())
else:
new_request = urllib2.urlopen(self.url)
set_cache_header = store_in_cache(
self.cache_location,
new_request.url,
View
@@ -21,7 +21,8 @@
import sickbeard
import urllib
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import datetime
from common import USER_AGENT, Quality
@@ -31,7 +32,10 @@ class SickBeardURLopener(urllib.FancyURLopener):
def __init__(self):
# call the base class
urllib.FancyURLopener.__init__(self, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
urllib.FancyURLopener.__init__(self, context=ssl._create_unverified_context())
else:
urllib.FancyURLopener.__init__(self)
class AuthURLOpener(SickBeardURLopener):
"""
View
@@ -30,7 +30,8 @@
import traceback
import urllib
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import zlib
from httplib import BadStatusLine
@@ -172,8 +173,10 @@ def getURL(url, post_data=None, headers=[]):
"""
Returns a byte-string retrieved from the url provider.
"""
opener = urllib2.build_opener(urllib2.HTTPSHandler(context=sslModule._create_unverified_context())
if sys.version_info >= (2, 7, 9):
opener = urllib2.build_opener(urllib2.HTTPSHandler(context=ssl._create_unverified_context()))
else:
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', USER_AGENT), ('Accept-Encoding', 'gzip,deflate')]
for cur_header in headers:
opener.addheaders.append(cur_header)
@@ -20,7 +20,8 @@
import urllib
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import sickbeard
@@ -58,7 +59,10 @@ def _sendBoxcar2(self, title, msg, accessToken, sound):
# send the request to boxcar2
try:
req = urllib2.Request(API_URL)
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
else:
handle = urllib2.urlopen(req, data)
handle.close()
except urllib2.URLError, e:
View
@@ -18,7 +18,8 @@
import urllib
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import sickbeard
import telnetlib
import re
@@ -102,7 +103,10 @@ def _sendNMJ(self, host, database, mount=None):
try:
req = urllib2.Request(mount)
logger.log(u"NMJ: Try to mount network drive via url: %s" % (mount), logger.DEBUG)
handle = urllib2.urlopen(req, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle = urllib2.urlopen(req, context=ssl._create_unverified_context())
else:
handle = urllib2.urlopen(req)
except IOError, e:
if hasattr(e, 'reason'):
logger.log(u"NMJ: Could not contact Popcorn Hour on host %s: %s" % (host, e.reason), logger.WARNING)
@@ -128,7 +132,10 @@ def _sendNMJ(self, host, database, mount=None):
try:
req = urllib2.Request(updateUrl)
logger.log(u"NMJ: Sending NMJ scan update command via url: %s" % (updateUrl), logger.DEBUG)
handle = urllib2.urlopen(req, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle = urllib2.urlopen(req, context=ssl._create_unverified_context())
else:
handle = urllib2.urlopen(req)
response = handle.read()
except IOError, e:
if hasattr(e, 'reason'):
@@ -18,7 +18,8 @@
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
from xml.dom.minidom import parseString
import sickbeard
import time
@@ -46,7 +47,10 @@ def notify_settings(self, host, dbloc, instance):
try:
url_loc = "http://" + host + ":8008/file_operation?arg0=list_user_storage_file&arg1=&arg2=" + instance + "&arg3=20&arg4=true&arg5=true&arg6=true&arg7=all&arg8=name_asc&arg9=false&arg10=false"
req = urllib2.Request(url_loc)
handle1 = urllib2.urlopen(req, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle1 = urllib2.urlopen(req, context=ssl._create_unverified_context())
else:
handle1 = urllib2.urlopen(req)
response1 = handle1.read()
# TODO: convert to etree?
xml = parseString(response1)
@@ -56,7 +60,10 @@ def notify_settings(self, host, dbloc, instance):
xmlData = xmlTag.replace('<path>', '').replace('</path>', '').replace('[=]', '')
url_db = "http://" + host + ":8008/metadata_database?arg0=check_database&arg1=" + xmlData
reqdb = urllib2.Request(url_db)
handledb = urllib2.urlopen(reqdb, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handledb = urllib2.urlopen(reqdb, context=ssl._create_unverified_context())
else:
handledb = urllib2.urlopen(reqdb)
responsedb = handledb.read()
xmldb = parseString(responsedb)
returnvalue = xmldb.getElementsByTagName('returnValue')[0].toxml().replace('<returnValue>', '').replace('</returnValue>', '')
@@ -95,10 +102,16 @@ def _sendNMJ(self, host):
logger.log(u"NMJv2: Try to mount network drive via url: %s" % (host), logger.DEBUG)
prereq = urllib2.Request(url_scandir)
req = urllib2.Request(url_updatedb)
handle1 = urllib2.urlopen(prereq, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle1 = urllib2.urlopen(prereq, context=ssl._create_unverified_context())
else:
handle1 = urllib2.urlopen(prereq)
response1 = handle1.read()
time.sleep(0.5)
handle2 = urllib2.urlopen(req, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle2 = urllib2.urlopen(req, context=ssl._create_unverified_context())
else:
handle2 = urllib2.urlopen(req)
response2 = handle2.read()
except IOError, e:
logger.log(u"NMJv2: Could not contact Popcorn Hour on host %s: %s" % (host, e), logger.WARNING)
@@ -18,7 +18,8 @@
import urllib
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import base64
import sickbeard
@@ -78,8 +79,11 @@ def _send_to_plex(self, command, host, username=None, password=None):
logger.log(u"PLEX: Contacting (with auth header) via url: " + url, logger.DEBUG)
else:
logger.log(u"PLEX: Contacting via url: " + url, logger.DEBUG)
response = urllib2.urlopen(req, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
response = urllib2.urlopen(req, context=ssl._create_unverified_context())
else:
response = urllib2.urlopen(req)
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
@@ -166,7 +170,10 @@ def update_library(self, ep_obj=None):
url = "http://%s/library/sections" % sickbeard.PLEX_SERVER_HOST
try:
xml_tree = etree.parse(urllib.urlopen(url, context=ssl._create_unverified_context()))
if sys.version_info >= (2, 7, 9):
xml_tree = etree.parse(urllib.urlopen(url, context=ssl._create_unverified_context()))
else:
xml_tree = etree.parse(urllib.urlopen(url))
media_container = xml_tree.getroot()
except IOError, e:
logger.log(u"PLEX: Error while trying to contact Plex Media Server: " + ex(e), logger.ERROR)
@@ -181,7 +188,10 @@ def update_library(self, ep_obj=None):
if section.attrib['type'] == "show":
url = "http://%s/library/sections/%s/refresh" % (sickbeard.PLEX_SERVER_HOST, section.attrib['key'])
try:
urllib.urlopen(url, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
urllib.urlopen(url, context=ssl._create_unverified_context())
else:
urllib.urlopen(url)
except Exception, e:
logger.log(u"PLEX: Error updating library section for Plex Media Server: " + ex(e), logger.ERROR)
return False
@@ -18,7 +18,8 @@
import urllib
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import socket
import base64
@@ -44,7 +45,10 @@ def get_devices(self, accessToken=None):
req = urllib2.Request(DEVICEAPI_ENDPOINT)
base64string = base64.encodestring('%s:%s' % (accessToken, ''))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
handle = urllib2.urlopen(req, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle = urllib2.urlopen(req, context=ssl._create_unverified_context())
else:
handle = urllib2.urlopen(req)
if handle:
result = handle.read()
handle.close()
@@ -71,7 +75,10 @@ def _sendPushbullet(self, title, body, accessToken, device_iden):
req = urllib2.Request(PUSHAPI_ENDPOINT)
base64string = base64.encodestring('%s:%s' % (accessToken, ''))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
else:
handle = urllib2.urlopen(req, data)
handle.close()
except socket.timeout:
return False
@@ -20,7 +20,8 @@
import urllib
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
import time
import socket
import base64
@@ -51,7 +52,10 @@ def get_devices(self, userKey=None):
# get devices from pushover
try:
req = urllib2.Request(DEVICE_URL)
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
else:
handle = urllib2.urlopen(req, data)
if handle:
result = handle.read()
handle.close()
@@ -80,7 +84,10 @@ def _sendPushover(self, title, msg, userKey, priority, device, sound):
# send the request to pushover
try:
req = urllib2.Request(API_URL)
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
handle = urllib2.urlopen(req, data, context=ssl._create_unverified_context())
else:
handle = urllib2.urlopen(req, data)
handle.close()
except urllib2.URLError, e:
@@ -21,7 +21,8 @@
from urllib import urlencode
from urllib2 import Request, urlopen
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
from sickbeard import logger
from sickbeard.exceptions import ex
@@ -81,7 +82,10 @@ def update_library(self, ep_obj=None):
request = Request(requestUrl)
try:
response = urlopen(request, context=ssl._create_unverified_context()) # @UnusedVariable
if sys.version_info >= (2, 7, 9):
response = urlopen(request, context=ssl._create_unverified_context()) # @UnusedVariable
else:
response = urlopen(request) # @UnusedVariable
except IOError, e:
if hasattr(e, 'reason'):
logger.log(u"PYTIVO: Failed to reach server '%s' - %s" % (host, e.reason), logger.WARNING)
@@ -17,7 +17,8 @@
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
import urllib2
import ssl
if sys.version_info >= (2, 7, 9):
import ssl
from hashlib import sha1
@@ -76,7 +77,10 @@ def _notifyTrakt(self, method, api, username, password, data={}, force=False):
# request the URL from trakt and parse the result as json
try:
logger.log(u"TRAKT: Calling method http://api.trakt.tv/" + method + ", with data" + encoded_data, logger.DEBUG)
stream = urllib2.urlopen("http://api.trakt.tv/" + method, encoded_data, context=ssl._create_unverified_context())
if sys.version_info >= (2, 7, 9):
stream = urllib2.urlopen("http://api.trakt.tv/" + method, encoded_data, context=ssl._create_unverified_context())
else:
stream = urllib2.urlopen("http://api.trakt.tv/" + method, encoded_data)
resp = stream.read()
resp = json.loads(resp)
Oops, something went wrong.

0 comments on commit f463f7d

Please sign in to comment.