proxy.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """
  18. The Proxy implementation.
  19. """
  20. class ProxyTypeFactory:
  21. """
  22. Factory for proxy types.
  23. """
  24. @staticmethod
  25. def make(ff_value, string):
  26. return {'ff_value': ff_value, 'string': string}
  27. class ProxyType:
  28. """
  29. Set of possible types of proxy.
  30. Each proxy type has 2 properties:
  31. 'ff_value' is value of Firefox profile preference,
  32. 'string' is id of proxy type.
  33. """
  34. DIRECT = ProxyTypeFactory.make(0, 'DIRECT') # Direct connection, no proxy (default on Windows).
  35. MANUAL = ProxyTypeFactory.make(1, 'MANUAL') # Manual proxy settings (e.g., for httpProxy).
  36. PAC = ProxyTypeFactory.make(2, 'PAC') # Proxy autoconfiguration from URL.
  37. RESERVED_1 = ProxyTypeFactory.make(3, 'RESERVED1') # Never used.
  38. AUTODETECT = ProxyTypeFactory.make(4, 'AUTODETECT') # Proxy autodetection (presumably with WPAD).
  39. SYSTEM = ProxyTypeFactory.make(5, 'SYSTEM') # Use system settings (default on Linux).
  40. UNSPECIFIED = ProxyTypeFactory.make(6, 'UNSPECIFIED') # Not initialized (for internal use).
  41. @classmethod
  42. def load(cls, value):
  43. if isinstance(value, dict) and 'string' in value:
  44. value = value['string']
  45. value = str(value).upper()
  46. for attr in dir(cls):
  47. attr_value = getattr(cls, attr)
  48. if isinstance(attr_value, dict) and \
  49. 'string' in attr_value and \
  50. attr_value['string'] is not None and \
  51. attr_value['string'] == value:
  52. return attr_value
  53. raise Exception("No proxy type is found for %s" % (value))
  54. class Proxy(object):
  55. """
  56. Proxy contains information about proxy type and necessary proxy settings.
  57. """
  58. proxyType = ProxyType.UNSPECIFIED
  59. autodetect = False
  60. ftpProxy = ''
  61. httpProxy = ''
  62. noProxy = ''
  63. proxyAutoconfigUrl = ''
  64. sslProxy = ''
  65. socksProxy = ''
  66. socksUsername = ''
  67. socksPassword = ''
  68. def __init__(self, raw=None):
  69. """
  70. Creates a new Proxy.
  71. :Args:
  72. - raw: raw proxy data. If None, default class values are used.
  73. """
  74. if raw is not None:
  75. if 'proxyType' in raw and raw['proxyType'] is not None:
  76. self.proxy_type = ProxyType.load(raw['proxyType'])
  77. if 'ftpProxy' in raw and raw['ftpProxy'] is not None:
  78. self.ftp_proxy = raw['ftpProxy']
  79. if 'httpProxy' in raw and raw['httpProxy'] is not None:
  80. self.http_proxy = raw['httpProxy']
  81. if 'noProxy' in raw and raw['noProxy'] is not None:
  82. self.no_proxy = raw['noProxy']
  83. if 'proxyAutoconfigUrl' in raw and raw['proxyAutoconfigUrl'] is not None:
  84. self.proxy_autoconfig_url = raw['proxyAutoconfigUrl']
  85. if 'sslProxy' in raw and raw['sslProxy'] is not None:
  86. self.sslProxy = raw['sslProxy']
  87. if 'autodetect' in raw and raw['autodetect'] is not None:
  88. self.auto_detect = raw['autodetect']
  89. if 'socksProxy' in raw and raw['socksProxy'] is not None:
  90. self.socks_proxy = raw['socksProxy']
  91. if 'socksUsername' in raw and raw['socksUsername'] is not None:
  92. self.socks_username = raw['socksUsername']
  93. if 'socksPassword' in raw and raw['socksPassword'] is not None:
  94. self.socks_password = raw['socksPassword']
  95. @property
  96. def proxy_type(self):
  97. """
  98. Returns proxy type as `ProxyType`.
  99. """
  100. return self.proxyType
  101. @proxy_type.setter
  102. def proxy_type(self, value):
  103. """
  104. Sets proxy type.
  105. :Args:
  106. - value: The proxy type.
  107. """
  108. self._verify_proxy_type_compatibility(value)
  109. self.proxyType = value
  110. @property
  111. def auto_detect(self):
  112. """
  113. Returns autodetect setting.
  114. """
  115. return self.autodetect
  116. @auto_detect.setter
  117. def auto_detect(self, value):
  118. """
  119. Sets autodetect setting.
  120. :Args:
  121. - value: The autodetect value.
  122. """
  123. if isinstance(value, bool):
  124. if self.autodetect is not value:
  125. self._verify_proxy_type_compatibility(ProxyType.AUTODETECT)
  126. self.proxyType = ProxyType.AUTODETECT
  127. self.autodetect = value
  128. else:
  129. raise ValueError("Autodetect proxy value needs to be a boolean")
  130. @property
  131. def ftp_proxy(self):
  132. """
  133. Returns ftp proxy setting.
  134. """
  135. return self.ftpProxy
  136. @ftp_proxy.setter
  137. def ftp_proxy(self, value):
  138. """
  139. Sets ftp proxy setting.
  140. :Args:
  141. - value: The ftp proxy value.
  142. """
  143. self._verify_proxy_type_compatibility(ProxyType.MANUAL)
  144. self.proxyType = ProxyType.MANUAL
  145. self.ftpProxy = value
  146. @property
  147. def http_proxy(self):
  148. """
  149. Returns http proxy setting.
  150. """
  151. return self.httpProxy
  152. @http_proxy.setter
  153. def http_proxy(self, value):
  154. """
  155. Sets http proxy setting.
  156. :Args:
  157. - value: The http proxy value.
  158. """
  159. self._verify_proxy_type_compatibility(ProxyType.MANUAL)
  160. self.proxyType = ProxyType.MANUAL
  161. self.httpProxy = value
  162. @property
  163. def no_proxy(self):
  164. """
  165. Returns noproxy setting.
  166. """
  167. return self.noProxy
  168. @no_proxy.setter
  169. def no_proxy(self, value):
  170. """
  171. Sets noproxy setting.
  172. :Args:
  173. - value: The noproxy value.
  174. """
  175. self._verify_proxy_type_compatibility(ProxyType.MANUAL)
  176. self.proxyType = ProxyType.MANUAL
  177. self.noProxy = value
  178. @property
  179. def proxy_autoconfig_url(self):
  180. """
  181. Returns proxy autoconfig url setting.
  182. """
  183. return self.proxyAutoconfigUrl
  184. @proxy_autoconfig_url.setter
  185. def proxy_autoconfig_url(self, value):
  186. """
  187. Sets proxy autoconfig url setting.
  188. :Args:
  189. - value: The proxy autoconfig url value.
  190. """
  191. self._verify_proxy_type_compatibility(ProxyType.PAC)
  192. self.proxyType = ProxyType.PAC
  193. self.proxyAutoconfigUrl = value
  194. @property
  195. def ssl_proxy(self):
  196. """
  197. Returns https proxy setting.
  198. """
  199. return self.sslProxy
  200. @ssl_proxy.setter
  201. def ssl_proxy(self, value):
  202. """
  203. Sets https proxy setting.
  204. :Args:
  205. - value: The https proxy value.
  206. """
  207. self._verify_proxy_type_compatibility(ProxyType.MANUAL)
  208. self.proxyType = ProxyType.MANUAL
  209. self.sslProxy = value
  210. @property
  211. def socks_proxy(self):
  212. """
  213. Returns socks proxy setting.
  214. """
  215. return self.socksProxy
  216. @socks_proxy.setter
  217. def socks_proxy(self, value):
  218. """
  219. Sets socks proxy setting.
  220. :Args:
  221. - value: The socks proxy value.
  222. """
  223. self._verify_proxy_type_compatibility(ProxyType.MANUAL)
  224. self.proxyType = ProxyType.MANUAL
  225. self.socksProxy = value
  226. @property
  227. def socks_username(self):
  228. """
  229. Returns socks proxy username setting.
  230. """
  231. return self.socksUsername
  232. @socks_username.setter
  233. def socks_username(self, value):
  234. """
  235. Sets socks proxy username setting.
  236. :Args:
  237. - value: The socks proxy username value.
  238. """
  239. self._verify_proxy_type_compatibility(ProxyType.MANUAL)
  240. self.proxyType = ProxyType.MANUAL
  241. self.socksUsername = value
  242. @property
  243. def socks_password(self):
  244. """
  245. Returns socks proxy password setting.
  246. """
  247. return self.socksPassword
  248. @socks_password.setter
  249. def socks_password(self, value):
  250. """
  251. Sets socks proxy password setting.
  252. :Args:
  253. - value: The socks proxy password value.
  254. """
  255. self._verify_proxy_type_compatibility(ProxyType.MANUAL)
  256. self.proxyType = ProxyType.MANUAL
  257. self.socksPassword = value
  258. def _verify_proxy_type_compatibility(self, compatibleProxy):
  259. if self.proxyType != ProxyType.UNSPECIFIED and self.proxyType != compatibleProxy:
  260. raise Exception(" Specified proxy type (%s) not compatible with current setting (%s)" % (compatibleProxy, self.proxyType))
  261. def add_to_capabilities(self, capabilities):
  262. """
  263. Adds proxy information as capability in specified capabilities.
  264. :Args:
  265. - capabilities: The capabilities to which proxy will be added.
  266. """
  267. proxy_caps = {}
  268. proxy_caps['proxyType'] = self.proxyType['string']
  269. if self.autodetect:
  270. proxy_caps['autodetect'] = self.autodetect
  271. if self.ftpProxy:
  272. proxy_caps['ftpProxy'] = self.ftpProxy
  273. if self.httpProxy:
  274. proxy_caps['httpProxy'] = self.httpProxy
  275. if self.proxyAutoconfigUrl:
  276. proxy_caps['proxyAutoconfigUrl'] = self.proxyAutoconfigUrl
  277. if self.sslProxy:
  278. proxy_caps['sslProxy'] = self.sslProxy
  279. if self.noProxy:
  280. proxy_caps['noProxy'] = self.noProxy
  281. if self.socksProxy:
  282. proxy_caps['socksProxy'] = self.socksProxy
  283. if self.socksUsername:
  284. proxy_caps['socksUsername'] = self.socksUsername
  285. if self.socksPassword:
  286. proxy_caps['socksPassword'] = self.socksPassword
  287. capabilities['proxy'] = proxy_caps