https custom certificate
Attempt #1 at implementing support for HTTPS custom ssl certificate
This commit is contained in:
parent
9800130e7f
commit
1ed674e4f1
2 changed files with 36 additions and 11 deletions
|
@ -8,8 +8,10 @@ import logging
|
||||||
|
|
||||||
import Utils as utils
|
import Utils as utils
|
||||||
from ClientInformation import ClientInformation
|
from ClientInformation import ClientInformation
|
||||||
|
from requests.packages.urllib3.exceptions import InsecureRequestWarning
|
||||||
|
|
||||||
# Disable requests logging
|
# Disable requests logging
|
||||||
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||||||
logging.getLogger("requests").setLevel(logging.WARNING)
|
logging.getLogger("requests").setLevel(logging.WARNING)
|
||||||
|
|
||||||
class DownloadUtils():
|
class DownloadUtils():
|
||||||
|
@ -34,7 +36,7 @@ class DownloadUtils():
|
||||||
|
|
||||||
def logMsg(self, msg, lvl=1):
|
def logMsg(self, msg, lvl=1):
|
||||||
|
|
||||||
utils.logMsg("%s %s" % (self.addonName, self.className), str(msg), int(lvl))
|
utils.logMsg("%s %s" % (self.addonName, self.className), msg, int(lvl))
|
||||||
|
|
||||||
def setUsername(self, username):
|
def setUsername(self, username):
|
||||||
# Reserved for UserClient only
|
# Reserved for UserClient only
|
||||||
|
@ -56,6 +58,11 @@ class DownloadUtils():
|
||||||
self.token = token
|
self.token = token
|
||||||
self.logMsg("Set token: %s" % token, 2)
|
self.logMsg("Set token: %s" % token, 2)
|
||||||
|
|
||||||
|
def setSSL(self, ssl):
|
||||||
|
# Reserved for UserClient only
|
||||||
|
self.ssl = ssl
|
||||||
|
self.logMsg("Set ssl path: %s" % ssl, 2)
|
||||||
|
|
||||||
def postCapabilities(self, deviceId):
|
def postCapabilities(self, deviceId):
|
||||||
|
|
||||||
# Get sessionId
|
# Get sessionId
|
||||||
|
@ -84,17 +91,20 @@ class DownloadUtils():
|
||||||
|
|
||||||
# User is identified from this point
|
# User is identified from this point
|
||||||
# Attach authenticated header to the session
|
# Attach authenticated header to the session
|
||||||
cert = None
|
|
||||||
header = self.getHeader()
|
header = self.getHeader()
|
||||||
|
cert = None
|
||||||
|
verify = None
|
||||||
|
|
||||||
if self.addon.getSetting('sslcert') != "None":
|
# If user has a custom certificate, verify the host certificate too
|
||||||
# If user uses HTTPS and has a custom client certificate
|
if (self.ssl != None):
|
||||||
cert = self.addon.getSetting('sslcert')
|
cert = self.ssl
|
||||||
|
verify = True
|
||||||
|
|
||||||
# Start session
|
# Start session
|
||||||
self.s = requests.Session()
|
self.s = requests.Session()
|
||||||
self.s.headers.update(header)
|
self.s.headers = header
|
||||||
self.s.cert = cert
|
self.s.cert = cert
|
||||||
|
self.s.verify = verify
|
||||||
# Retry connections to the server
|
# Retry connections to the server
|
||||||
self.s.mount("http://", requests.adapters.HTTPAdapter(max_retries=1))
|
self.s.mount("http://", requests.adapters.HTTPAdapter(max_retries=1))
|
||||||
self.s.mount("https://", requests.adapters.HTTPAdapter(max_retries=1))
|
self.s.mount("https://", requests.adapters.HTTPAdapter(max_retries=1))
|
||||||
|
@ -149,7 +159,7 @@ class DownloadUtils():
|
||||||
url = url.replace("{UserId}", self.userId, 1)
|
url = url.replace("{UserId}", self.userId, 1)
|
||||||
url = "%s&api_key=%s" % (url, self.token)
|
url = "%s&api_key=%s" % (url, self.token)
|
||||||
|
|
||||||
self.logMsg("URL: %s" % url, 1)
|
self.logMsg("URL: %s" % url, 2)
|
||||||
# Prepare request
|
# Prepare request
|
||||||
if type == "GET":
|
if type == "GET":
|
||||||
r = s.get(url, params=postBody, timeout=timeout)
|
r = s.get(url, params=postBody, timeout=timeout)
|
||||||
|
@ -168,7 +178,7 @@ class DownloadUtils():
|
||||||
if type == "GET":
|
if type == "GET":
|
||||||
r = requests.get(url, params=postBody, headers=header, timeout=timeout, verify=False)
|
r = requests.get(url, params=postBody, headers=header, timeout=timeout, verify=False)
|
||||||
elif type == "POST":
|
elif type == "POST":
|
||||||
r = requests.post(url, params=postBody, headers=header, timeout=timeout)
|
r = requests.post(url, params=postBody, headers=header, timeout=timeout, verify=False)
|
||||||
|
|
||||||
# Process the response
|
# Process the response
|
||||||
try:
|
try:
|
||||||
|
@ -176,14 +186,14 @@ class DownloadUtils():
|
||||||
|
|
||||||
if r.status_code == 204:
|
if r.status_code == 204:
|
||||||
# No response in body
|
# No response in body
|
||||||
self.logMsg("====== 204 Success ======", 1)
|
self.logMsg("====== 204 Success ======", 2)
|
||||||
return default_link
|
return default_link
|
||||||
# Response code 200
|
# Response code 200
|
||||||
elif r.status_code == requests.codes.ok:
|
elif r.status_code == requests.codes.ok:
|
||||||
try:
|
try:
|
||||||
# UTF-8 - JSON object
|
# UTF-8 - JSON object
|
||||||
r = r.json()
|
r = r.json()
|
||||||
self.logMsg("====== 200 Success ======", 1)
|
self.logMsg("====== 200 Success ======", 2)
|
||||||
return r
|
return r
|
||||||
except:
|
except:
|
||||||
self.logMsg("Unable to convert the response for: %s" % url, 1)
|
self.logMsg("Unable to convert the response for: %s" % url, 1)
|
||||||
|
@ -218,6 +228,10 @@ class DownloadUtils():
|
||||||
# Bad requests
|
# Bad requests
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
except requests.exceptions.SSLError as e:
|
||||||
|
self.logMsg("Invalid SSL certificate for: %s" % url, 0)
|
||||||
|
self.logMsg(e, 1)
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
self.logMsg("Unknown error connecting to: %s" % url, 0)
|
self.logMsg("Unknown error connecting to: %s" % url, 0)
|
||||||
self.logMsg(e, 1)
|
self.logMsg(e, 1)
|
||||||
|
|
|
@ -131,6 +131,15 @@ class UserClient(threading.Thread):
|
||||||
self.logMsg("No token found.")
|
self.logMsg("No token found.")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
def getSSL(self):
|
||||||
|
|
||||||
|
s_cert = self.addon.getSetting('sslcert')
|
||||||
|
|
||||||
|
if s_cert == "None":
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return s_cert
|
||||||
|
|
||||||
def getPublicUsers(self):
|
def getPublicUsers(self):
|
||||||
|
|
||||||
server = self.getServer()
|
server = self.getServer()
|
||||||
|
@ -156,6 +165,7 @@ class UserClient(threading.Thread):
|
||||||
self.currUserId = self.getUserId()
|
self.currUserId = self.getUserId()
|
||||||
self.currServer = self.getServer()
|
self.currServer = self.getServer()
|
||||||
self.currToken = self.getToken()
|
self.currToken = self.getToken()
|
||||||
|
self.ssl = self.getSSL()
|
||||||
|
|
||||||
# Set to windows property
|
# Set to windows property
|
||||||
WINDOW.setProperty("currUser", username)
|
WINDOW.setProperty("currUser", username)
|
||||||
|
@ -169,6 +179,7 @@ class UserClient(threading.Thread):
|
||||||
doUtils.setUserId(self.currUserId)
|
doUtils.setUserId(self.currUserId)
|
||||||
doUtils.setServer(self.currServer)
|
doUtils.setServer(self.currServer)
|
||||||
doUtils.setToken(self.currToken)
|
doUtils.setToken(self.currToken)
|
||||||
|
doUtils.setSSL(self.ssl)
|
||||||
# Start DownloadUtils session
|
# Start DownloadUtils session
|
||||||
doUtils.startSession()
|
doUtils.startSession()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue