12 |
12 |
13 from PyQt6.QtCore import QCoreApplication, QUrl |
13 from PyQt6.QtCore import QCoreApplication, QUrl |
14 from PyQt6.QtNetwork import QNetworkProxy, QNetworkProxyFactory, QNetworkProxyQuery |
14 from PyQt6.QtNetwork import QNetworkProxy, QNetworkProxyFactory, QNetworkProxyQuery |
15 from PyQt6.QtWidgets import QDialog |
15 from PyQt6.QtWidgets import QDialog |
16 |
16 |
17 from eric7 import Preferences, Utilities |
17 from eric7 import EricUtilities |
|
18 from eric7.EricCore import EricPreferences |
18 from eric7.EricWidgets import EricMessageBox |
19 from eric7.EricWidgets import EricMessageBox |
19 from eric7.SystemUtilities import OSUtilities |
20 from eric7.SystemUtilities import OSUtilities |
20 |
21 |
21 |
22 |
22 def schemeFromProxyType(proxyType): |
23 def schemeFromProxyType(proxyType): |
51 """ |
52 """ |
52 from eric7.EricWidgets.EricAuthenticationDialog import EricAuthenticationDialog |
53 from eric7.EricWidgets.EricAuthenticationDialog import EricAuthenticationDialog |
53 |
54 |
54 info = QCoreApplication.translate( |
55 info = QCoreApplication.translate( |
55 "EricNetworkProxyFactory", "<b>Connect to proxy '{0}' using:</b>" |
56 "EricNetworkProxyFactory", "<b>Connect to proxy '{0}' using:</b>" |
56 ).format(Utilities.html_encode(proxy.hostName())) |
57 ).format(EricUtilities.html_encode(proxy.hostName())) |
57 |
58 |
58 dlg = EricAuthenticationDialog(info, proxy.user(), True) |
59 dlg = EricAuthenticationDialog(info, proxy.user(), True) |
59 dlg.setData(proxy.user(), proxy.password()) |
60 dlg.setData(proxy.user(), proxy.password()) |
60 if dlg.exec() == QDialog.DialogCode.Accepted: |
61 if dlg.exec() == QDialog.DialogCode.Accepted: |
61 username, password = dlg.getData() |
62 username, password = dlg.getData() |
62 auth.setUser(username) |
63 auth.setUser(username) |
63 auth.setPassword(password) |
64 auth.setPassword(password) |
64 if dlg.shallSave(): |
65 if dlg.shallSave(): |
65 scheme = schemeFromProxyType(proxy.type()) |
66 scheme = schemeFromProxyType(proxy.type()) |
66 if scheme and scheme != "NoProxy": |
67 if scheme and scheme != "NoProxy": |
67 Preferences.setUI("ProxyUser/{0}".format(scheme), username) |
68 EricPreferences.setNetworkProxy( |
68 Preferences.setUI("ProxyPassword/{0}".format(scheme), password) |
69 "ProxyUser/{0}".format(scheme), username |
|
70 ) |
|
71 EricPreferences.setNetworkProxy( |
|
72 "ProxyPassword/{0}".format(scheme), password |
|
73 ) |
69 proxy.setUser(username) |
74 proxy.setUser(username) |
70 proxy.setPassword(password) |
75 proxy.setPassword(password) |
71 |
76 |
72 |
77 |
73 class HostnameMatcher: |
78 class HostnameMatcher: |
161 """ |
166 """ |
162 if ( |
167 if ( |
163 query.queryType() == QNetworkProxyQuery.QueryType.UrlRequest |
168 query.queryType() == QNetworkProxyQuery.QueryType.UrlRequest |
164 and query.protocolTag() in ["http", "https", "ftp"] |
169 and query.protocolTag() in ["http", "https", "ftp"] |
165 ): |
170 ): |
166 # use proxy at all ? |
171 # use proxy at all? |
167 if not Preferences.getUI("UseProxy"): |
172 if not EricPreferences.getNetworkProxy("UseProxy"): |
168 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
173 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
169 |
174 |
170 # test for exceptions |
175 # test for exceptions |
171 exceptions = Preferences.getUI("ProxyExceptions") |
176 exceptions = EricPreferences.getNetworkProxy("ProxyExceptions") |
172 if exceptions != self.__exceptions: |
177 if exceptions != self.__exceptions: |
173 self.__setExceptions(exceptions) |
178 self.__setExceptions(exceptions) |
174 urlHost = query.url().host() |
179 urlHost = query.url().host() |
175 for matcher in self.__hostnameMatchers: |
180 for matcher in self.__hostnameMatchers: |
176 if matcher.match(urlHost): |
181 if matcher.match(urlHost): |
177 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
182 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
178 |
183 |
179 # determine proxy |
184 # determine proxy |
180 if Preferences.getUI("UseSystemProxy"): |
185 if EricPreferences.getNetworkProxy("UseSystemProxy"): |
181 proxyList = QNetworkProxyFactory.systemProxyForQuery(query) |
186 proxyList = QNetworkProxyFactory.systemProxyForQuery(query) |
182 if ( |
187 if ( |
183 not OSUtilities.isWindowsPlatform() |
188 not OSUtilities.isWindowsPlatform() |
184 and len(proxyList) == 1 |
189 and len(proxyList) == 1 |
185 and proxyList[0].type() == QNetworkProxy.ProxyType.NoProxy |
190 and proxyList[0].type() == QNetworkProxy.ProxyType.NoProxy |
214 scheme = schemeFromProxyType(proxyList[0].type()) |
219 scheme = schemeFromProxyType(proxyList[0].type()) |
215 if scheme == "": |
220 if scheme == "": |
216 scheme = "Http" |
221 scheme = "Http" |
217 if scheme != "NoProxy": |
222 if scheme != "NoProxy": |
218 proxyList[0].setUser( |
223 proxyList[0].setUser( |
219 Preferences.getUI("ProxyUser/{0}".format(scheme)) |
224 EricPreferences.getNetworkProxy( |
|
225 "ProxyUser/{0}".format(scheme) |
|
226 ) |
220 ) |
227 ) |
221 proxyList[0].setPassword( |
228 proxyList[0].setPassword( |
222 Preferences.getUI("ProxyPassword/{0}".format(scheme)) |
229 EricPreferences.getNetworkProxy( |
|
230 "ProxyPassword/{0}".format(scheme) |
|
231 ) |
223 ) |
232 ) |
224 return proxyList |
233 return proxyList |
225 else: |
234 else: |
226 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
235 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
227 else: |
236 else: |
228 if Preferences.getUI("UseHttpProxyForAll"): |
237 if EricPreferences.getNetworkProxy("UseHttpProxyForAll"): |
229 protocolKey = "Http" |
238 protocolKey = "Http" |
230 else: |
239 else: |
231 protocolKey = query.protocolTag().capitalize() |
240 protocolKey = query.protocolTag().capitalize() |
232 host = Preferences.getUI("ProxyHost/{0}".format(protocolKey)) |
241 host = EricPreferences.getNetworkProxy( |
|
242 "ProxyHost/{0}".format(protocolKey) |
|
243 ) |
233 if not host: |
244 if not host: |
234 EricMessageBox.critical( |
245 EricMessageBox.critical( |
235 None, |
246 None, |
236 QCoreApplication.translate( |
247 QCoreApplication.translate( |
237 "EricNetworkProxyFactory", "Proxy Configuration Error" |
248 "EricNetworkProxyFactory", "Proxy Configuration Error" |
251 else: |
262 else: |
252 proxyType = QNetworkProxy.ProxyType.HttpProxy |
263 proxyType = QNetworkProxy.ProxyType.HttpProxy |
253 proxy = QNetworkProxy( |
264 proxy = QNetworkProxy( |
254 proxyType, |
265 proxyType, |
255 host, |
266 host, |
256 Preferences.getUI("ProxyPort/" + protocolKey), |
267 EricPreferences.getNetworkProxy("ProxyPort/" + protocolKey), |
257 Preferences.getUI("ProxyUser/" + protocolKey), |
268 EricPreferences.getNetworkProxy("ProxyUser/" + protocolKey), |
258 Preferences.getUI("ProxyPassword/" + protocolKey), |
269 EricPreferences.getNetworkProxy( |
|
270 "ProxyPassword/" + protocolKey |
|
271 ), |
259 ) |
272 ) |
260 else: |
273 else: |
261 proxy = QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy) |
274 proxy = QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy) |
262 return [proxy, QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy)] |
275 return [proxy, QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy)] |
263 else: |
276 else: |