Merge pull request #759 from minlexx/feature/proxy_auth
Add login/password for proxy authorization in network settings
This commit is contained in:
@@ -43,6 +43,7 @@ class ServerError(StandardError):
|
||||
class TimeoutError(StandardError):
|
||||
pass
|
||||
|
||||
|
||||
class Network():
|
||||
# Request constants - every request must supply this, as it is checked if
|
||||
# enabled or not via settings
|
||||
@@ -71,13 +72,31 @@ class Network():
|
||||
|
||||
# Set up some things for the request
|
||||
versionString = "{0} {1} - {2} {3}".format(config.version, config.tag, config.expansionName, config.expansionVersion)
|
||||
headers={"User-Agent" : "pyfa {0} (Python-urllib2)".format(versionString)}
|
||||
headers = {"User-Agent" : "pyfa {0} (Python-urllib2)".format(versionString)}
|
||||
|
||||
proxy = NetworkSettings.getInstance().getProxySettings()
|
||||
if proxy is not None:
|
||||
proxy = urllib2.ProxyHandler({'https': "{0}:{1}".format(*proxy)})
|
||||
opener = urllib2.build_opener(proxy)
|
||||
# proxy is a tuple of (host, port): (u'192.168.20.1', 3128)
|
||||
proxy_auth = NetworkSettings.getInstance().getProxyAuthDetails()
|
||||
# proxy_auth is a tuple of (login, password) or None
|
||||
if proxy_auth is not None:
|
||||
# add login:password@ in front of proxy address
|
||||
proxy_handler = urllib2.ProxyHandler({'https': '{0}:{1}@{2}:{3}'.format(
|
||||
proxy_auth[0], proxy_auth[1], proxy[0], proxy[1])})
|
||||
else:
|
||||
# build proxy handler with no login/pass info
|
||||
proxy_handler = urllib2.ProxyHandler({'https': "{0}:{1}".format(proxy[0], proxy[1])})
|
||||
opener = urllib2.build_opener(proxy_handler)
|
||||
urllib2.install_opener(opener)
|
||||
else:
|
||||
# This is a bug fix, explicitly disable possibly previously installed
|
||||
# opener with proxy, by urllib2.install_opener() a few lines above in code.
|
||||
# Now this explicitly disables proxy handler, "uninstalling" opener.
|
||||
# This is used in case when user had proxy enabled, so proxy_handler was already
|
||||
# installed globally, and then user had disabled the proxy, so we should clear that opener
|
||||
urllib2.install_opener(None)
|
||||
# another option could be installing a default opener:
|
||||
# urllib2.install_opener(urllib2.build_opener())
|
||||
|
||||
request = urllib2.Request(url, headers=headers, data=urllib.urlencode(data) if data else None)
|
||||
try:
|
||||
|
||||
@@ -113,6 +113,12 @@ class Settings():
|
||||
|
||||
class NetworkSettings():
|
||||
_instance = None
|
||||
|
||||
# constants for serviceNetworkDefaultSettings["mode"] parameter
|
||||
PROXY_MODE_NONE = 0 # 0 - No proxy
|
||||
PROXY_MODE_AUTODETECT = 1 # 1 - Auto-detected proxy settings
|
||||
PROXY_MODE_MANUAL = 2 # 2 - Manual proxy settings
|
||||
|
||||
@classmethod
|
||||
def getInstance(cls):
|
||||
if cls._instance == None:
|
||||
@@ -122,13 +128,18 @@ class NetworkSettings():
|
||||
|
||||
def __init__(self):
|
||||
|
||||
# mode
|
||||
# 0 - No proxy
|
||||
# 1 - Auto-detected proxy settings
|
||||
# 2 - Manual proxy settings
|
||||
serviceNetworkDefaultSettings = {"mode": 1, "type": "https", "address": "", "port": "", "access": 15}
|
||||
serviceNetworkDefaultSettings = {
|
||||
"mode": self.PROXY_MODE_AUTODETECT,
|
||||
"type": "https",
|
||||
"address": "",
|
||||
"port": "",
|
||||
"access": 15,
|
||||
"login": None,
|
||||
"password": None
|
||||
}
|
||||
|
||||
self.serviceNetworkSettings = SettingsProvider.getInstance().getSettings("pyfaServiceNetworkSettings", serviceNetworkDefaultSettings)
|
||||
self.serviceNetworkSettings = SettingsProvider.getInstance().getSettings(
|
||||
"pyfaServiceNetworkSettings", serviceNetworkDefaultSettings)
|
||||
|
||||
def isEnabled(self, type):
|
||||
if type & self.serviceNetworkSettings["access"]:
|
||||
@@ -198,13 +209,33 @@ class NetworkSettings():
|
||||
|
||||
def getProxySettings(self):
|
||||
|
||||
if self.getMode() == 0:
|
||||
if self.getMode() == self.PROXY_MODE_NONE:
|
||||
return None
|
||||
if self.getMode() == 1:
|
||||
if self.getMode() == self.PROXY_MODE_AUTODETECT:
|
||||
return self.autodetect()
|
||||
if self.getMode() == 2:
|
||||
if self.getMode() == self.PROXY_MODE_MANUAL:
|
||||
return (self.getAddress(), int(self.getPort()))
|
||||
|
||||
def getProxyAuthDetails(self):
|
||||
if self.getMode() == self.PROXY_MODE_NONE:
|
||||
return None
|
||||
if (self.serviceNetworkSettings["login"] is None) or (self.serviceNetworkSettings["password"] is None):
|
||||
return None
|
||||
# in all other cases, return tuple of (login, password)
|
||||
return (self.serviceNetworkSettings["login"], self.serviceNetworkSettings["password"])
|
||||
|
||||
def setProxyAuthDetails(self, login, password):
|
||||
if (login is None) or (password is None):
|
||||
self.serviceNetworkSettings["login"] = None
|
||||
self.serviceNetworkSettings["password"] = None
|
||||
return
|
||||
if login == "": # empty login unsets proxy auth info
|
||||
self.serviceNetworkSettings["login"] = None
|
||||
self.serviceNetworkSettings["password"] = None
|
||||
return
|
||||
self.serviceNetworkSettings["login"] = login
|
||||
self.serviceNetworkSettings["password"] = password
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user