提交 be984402 编写于 作者: S Serge Rider

Net utils moved to model. Redundant icon ref removed.


Former-commit-id: f9ad66a0
上级 d8ceac9f
......@@ -47,7 +47,6 @@ import org.jkiss.dbeaver.registry.datatype.DataTypeProviderRegistry;
import org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry;
import org.jkiss.dbeaver.runtime.IPluginService;
import org.jkiss.dbeaver.runtime.jobs.KeepAliveJob;
import org.jkiss.dbeaver.runtime.net.GlobalProxyAuthenticator;
import org.jkiss.dbeaver.runtime.net.GlobalProxySelector;
import org.jkiss.dbeaver.runtime.qm.QMControllerImpl;
import org.jkiss.dbeaver.runtime.qm.QMLogFileWriter;
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.net;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.access.DBAAuthInfo;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBExecUtils;
import org.jkiss.dbeaver.model.impl.net.SocksConstants;
import org.jkiss.dbeaver.model.net.DBWHandlerConfiguration;
import org.jkiss.dbeaver.model.net.DBWHandlerType;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.registry.encode.EncryptionException;
import org.jkiss.dbeaver.registry.encode.SecuredPasswordEncrypter;
import org.jkiss.dbeaver.runtime.ui.DBUserInterface;
import org.jkiss.utils.CommonUtils;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
/**
* Global authenticator
*/
public class GlobalProxyAuthenticator extends Authenticator {
private SecuredPasswordEncrypter encrypter;
@Nullable
@Override
protected PasswordAuthentication getPasswordAuthentication() {
{
DBPPreferenceStore store = DBeaverCore.getGlobalPreferenceStore();
// 1. Check for drivers download proxy
final String proxyHost = store.getString(DBeaverPreferences.UI_PROXY_HOST);
if (!CommonUtils.isEmpty(proxyHost) && proxyHost.equalsIgnoreCase(getRequestingHost()) &&
store.getInt(DBeaverPreferences.UI_PROXY_PORT) == getRequestingPort())
{
String userName = store.getString(DBeaverPreferences.UI_PROXY_USER);
String userPassword = decryptPassword(store.getString(DBeaverPreferences.UI_PROXY_PASSWORD));
if (CommonUtils.isEmpty(userName) || CommonUtils.isEmpty(userPassword)) {
DBAAuthInfo authInfo = readCredentialsInUI("Auth proxy '" + proxyHost + "'", userName, userPassword);
if (authInfo != null) {
userName = authInfo.getUserName();
userPassword = authInfo.getUserPassword();
if (authInfo.isSavePassword()) {
// Save in preferences
store.setValue(DBeaverPreferences.UI_PROXY_USER, userName);
store.setValue(DBeaverPreferences.UI_PROXY_PASSWORD, encryptPassword(userPassword));
}
}
}
if (!CommonUtils.isEmpty(userName) && !CommonUtils.isEmpty(userPassword)) {
return new PasswordAuthentication(userName, userPassword.toCharArray());
}
}
}
{
// 2. Check for connections' proxies
String requestingProtocol = getRequestingProtocol();
if (SocksConstants.PROTOCOL_SOCKS5.equals(requestingProtocol) || SocksConstants.PROTOCOL_SOCKS4.equals(requestingProtocol)) {
DBCExecutionContext activeContext = DBExecUtils.findConnectionContext(getRequestingHost(), getRequestingPort(), getRequestingScheme());
if (activeContext != null) {
DBPDataSourceContainer container = activeContext.getDataSource().getContainer();
for (DBWHandlerConfiguration networkHandler : container.getConnectionConfiguration().getDeclaredHandlers()) {
if (networkHandler.isEnabled() && networkHandler.getType() == DBWHandlerType.PROXY) {
String userName = networkHandler.getUserName();
String userPassword = networkHandler.getPassword();
if (CommonUtils.isEmpty(userName) || CommonUtils.isEmpty(userPassword)) {
DBAAuthInfo authInfo = readCredentialsInUI(getRequestingPrompt(), userName, userPassword);
if (authInfo != null) {
userName = authInfo.getUserName();
userPassword = authInfo.getUserPassword();
if (authInfo.isSavePassword()) {
// Save DS config
networkHandler.setUserName(userName);
networkHandler.setPassword(userPassword);
networkHandler.setSavePassword(true);
container.getRegistry().flushConfig();
}
}
}
if (!CommonUtils.isEmpty(userName) && !CommonUtils.isEmpty(userPassword)) {
return new PasswordAuthentication(userName, userPassword.toCharArray());
}
}
}
}
}
}
return null;
}
private String encryptPassword(String password) {
try {
if (encrypter == null) {
encrypter = new SecuredPasswordEncrypter();
}
return encrypter.encrypt(password);
} catch (EncryptionException e) {
return password;
}
}
private String decryptPassword(String password) {
if (CommonUtils.isEmpty(password)) {
return password;
}
try {
if (encrypter == null) {
encrypter = new SecuredPasswordEncrypter();
}
return encrypter.decrypt(password);
} catch (EncryptionException e) {
return password;
}
}
private DBAAuthInfo readCredentialsInUI(String prompt, String userName, String userPassword)
{
return DBUserInterface.getInstance().promptUserCredentials(prompt, userName, userPassword, false);
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.core;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.access.DBAAuthInfo;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBExecUtils;
import org.jkiss.dbeaver.model.impl.net.SocksConstants;
import org.jkiss.dbeaver.model.net.DBWHandlerConfiguration;
import org.jkiss.dbeaver.model.net.DBWHandlerType;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.registry.encode.EncryptionException;
import org.jkiss.dbeaver.registry.encode.SecuredPasswordEncrypter;
import org.jkiss.dbeaver.runtime.ui.DBUserInterface;
import org.jkiss.utils.CommonUtils;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
/**
* Global authenticator
*/
public class GlobalProxyAuthenticator extends Authenticator {
private SecuredPasswordEncrypter encrypter;
@Nullable
@Override
protected PasswordAuthentication getPasswordAuthentication() {
{
DBPPreferenceStore store = DBeaverCore.getGlobalPreferenceStore();
// 1. Check for drivers download proxy
final String proxyHost = store.getString(DBeaverPreferences.UI_PROXY_HOST);
if (!CommonUtils.isEmpty(proxyHost) && proxyHost.equalsIgnoreCase(getRequestingHost()) &&
store.getInt(DBeaverPreferences.UI_PROXY_PORT) == getRequestingPort())
{
String userName = store.getString(DBeaverPreferences.UI_PROXY_USER);
String userPassword = decryptPassword(store.getString(DBeaverPreferences.UI_PROXY_PASSWORD));
if (CommonUtils.isEmpty(userName) || CommonUtils.isEmpty(userPassword)) {
DBAAuthInfo authInfo = readCredentialsInUI("Auth proxy '" + proxyHost + "'", userName, userPassword);
if (authInfo != null) {
userName = authInfo.getUserName();
userPassword = authInfo.getUserPassword();
if (authInfo.isSavePassword()) {
// Save in preferences
store.setValue(DBeaverPreferences.UI_PROXY_USER, userName);
store.setValue(DBeaverPreferences.UI_PROXY_PASSWORD, encryptPassword(userPassword));
}
}
}
if (!CommonUtils.isEmpty(userName) && !CommonUtils.isEmpty(userPassword)) {
return new PasswordAuthentication(userName, userPassword.toCharArray());
}
}
}
{
// 2. Check for connections' proxies
String requestingProtocol = getRequestingProtocol();
if (SocksConstants.PROTOCOL_SOCKS5.equals(requestingProtocol) || SocksConstants.PROTOCOL_SOCKS4.equals(requestingProtocol)) {
DBCExecutionContext activeContext = DBExecUtils.findConnectionContext(getRequestingHost(), getRequestingPort(), getRequestingScheme());
if (activeContext != null) {
DBPDataSourceContainer container = activeContext.getDataSource().getContainer();
for (DBWHandlerConfiguration networkHandler : container.getConnectionConfiguration().getDeclaredHandlers()) {
if (networkHandler.isEnabled() && networkHandler.getType() == DBWHandlerType.PROXY) {
String userName = networkHandler.getUserName();
String userPassword = networkHandler.getPassword();
if (CommonUtils.isEmpty(userName) || CommonUtils.isEmpty(userPassword)) {
DBAAuthInfo authInfo = readCredentialsInUI(getRequestingPrompt(), userName, userPassword);
if (authInfo != null) {
userName = authInfo.getUserName();
userPassword = authInfo.getUserPassword();
if (authInfo.isSavePassword()) {
// Save DS config
networkHandler.setUserName(userName);
networkHandler.setPassword(userPassword);
networkHandler.setSavePassword(true);
container.getRegistry().flushConfig();
}
}
}
if (!CommonUtils.isEmpty(userName) && !CommonUtils.isEmpty(userPassword)) {
return new PasswordAuthentication(userName, userPassword.toCharArray());
}
}
}
}
}
}
return null;
}
private String encryptPassword(String password) {
try {
if (encrypter == null) {
encrypter = new SecuredPasswordEncrypter();
}
return encrypter.encrypt(password);
} catch (EncryptionException e) {
return password;
}
}
private String decryptPassword(String password) {
if (CommonUtils.isEmpty(password)) {
return password;
}
try {
if (encrypter == null) {
encrypter = new SecuredPasswordEncrypter();
}
return encrypter.decrypt(password);
} catch (EncryptionException e) {
return password;
}
}
private DBAAuthInfo readCredentialsInUI(String prompt, String userName, String userPassword)
{
return DBUserInterface.getInstance().promptUserCredentials(prompt, userName, userPassword, false);
}
}
......@@ -18,9 +18,7 @@ package org.jkiss.dbeaver.runtime.jobs;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.resource.ImageDescriptor;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.DBPDataSourceUser;
......@@ -35,7 +33,7 @@ public abstract class DataSourceJob extends AbstractJob implements DBPDataSource
{
private final DBCExecutionContext executionContext;
protected DataSourceJob(String name, @Nullable ImageDescriptor image, @NotNull DBCExecutionContext executionContext)
protected DataSourceJob(String name, @NotNull DBCExecutionContext executionContext)
{
super(CommonUtils.truncateString(name, 1000)); // Trunkate just in case
this.executionContext = executionContext;
......
......@@ -59,7 +59,7 @@ public class InvalidateJob extends DataSourceJob
DBCExecutionContext context/*,
boolean reconnect*/)
{
super("Invalidate " + context.getDataSource().getContainer().getName(), null, context);
super("Invalidate " + context.getDataSource().getContainer().getName(), context);
// this.reconnect = reconnect;
}
......
......@@ -114,7 +114,7 @@ public class SQLQueryJob extends DataSourceJob implements Closeable
@NotNull SQLResultsConsumer resultsConsumer,
@Nullable SQLQueryListener listener)
{
super(name, DBeaverIcons.getImageDescriptor(UIIcon.SQL_SCRIPT_EXECUTE), executionContext);
super(name, executionContext);
this.dataContainer = dataContainer;
this.partSite = partSite;
this.queries = queries;
......
......@@ -23,8 +23,6 @@ import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBCExecutionSource;
import org.jkiss.dbeaver.model.struct.DBSDataContainer;
import org.jkiss.dbeaver.runtime.jobs.DataSourceJob;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
abstract class ResultSetJobAbstract extends DataSourceJob implements DBCExecutionSource {
......@@ -32,7 +30,7 @@ abstract class ResultSetJobAbstract extends DataSourceJob implements DBCExecutio
protected final ResultSetViewer controller;
protected ResultSetJobAbstract(String name, DBSDataContainer dataContainer, ResultSetViewer controller, DBCExecutionContext executionContext) {
super(name, DBeaverIcons.getImageDescriptor(UIIcon.SQL_EXECUTE), executionContext);
super(name, executionContext);
this.dataContainer = dataContainer;
this.controller = controller;
setUser(false);
......
......@@ -38,8 +38,6 @@ import org.jkiss.dbeaver.model.struct.DBSDataManipulator;
import org.jkiss.dbeaver.model.struct.DBSEntity;
import org.jkiss.dbeaver.model.struct.rdb.DBSManipulationType;
import org.jkiss.dbeaver.runtime.jobs.DataSourceJob;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.CommonUtils;
......@@ -391,7 +389,7 @@ class ResultSetPersister {
protected DataUpdaterJob(boolean generateScript, @Nullable DataUpdateListener listener, @NotNull DBCExecutionContext executionContext)
{
super(CoreMessages.controls_resultset_viewer_job_update, DBeaverIcons.getImageDescriptor(UIIcon.SQL_EXECUTE), executionContext);
super(CoreMessages.controls_resultset_viewer_job_update, executionContext);
this.generateScript = generateScript;
this.listener = listener;
}
......@@ -782,7 +780,7 @@ class ResultSetPersister {
private List<ResultSetRow> rows;
public RowRefreshJob(DBCExecutionContext context, DBSDataContainer dataContainer, DBDRowIdentifier rowIdentifier, List<ResultSetRow> rows) {
super("Refresh rows", null, context);
super("Refresh rows", context);
this.dataContainer = dataContainer;
this.rowIdentifier = rowIdentifier;
this.rows = new ArrayList<>(rows);
......
......@@ -45,8 +45,6 @@ import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.runtime.jobs.DataSourceJob;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.actions.navigator.NavigatorHandlerObjectOpen;
import org.jkiss.dbeaver.ui.data.IAttributeController;
......@@ -314,8 +312,7 @@ public class ReferenceValueEditor {
{
super(
CoreMessages.dialog_value_view_job_selector_name + valueController.getValueName() + " possible values",
DBeaverIcons.getImageDescriptor(UIIcon.SQL_EXECUTE),
valueController.getExecutionContext());
valueController.getExecutionContext());
setUser(false);
}
......
......@@ -156,7 +156,7 @@ public abstract class GenerateMultiSQLDialog<T extends DBSObject> extends Genera
generateObjectCommand(lines, object);
objectsSQL.put(object, lines);
}
final DataSourceJob job = new DataSourceJob(jobName, null, getExecutionContext()) {
final DataSourceJob job = new DataSourceJob(jobName, getExecutionContext()) {
public Exception objectProcessingError;
@Override
......
......@@ -112,7 +112,7 @@ public abstract class GenerateSQLDialog extends BaseSQLDialog {
{
final String jobName = getShell().getText();
final String[] scriptLines = generateSQLScript();
DataSourceJob job = new DataSourceJob(jobName, null, executionContext) {
DataSourceJob job = new DataSourceJob(jobName, executionContext) {
@Override
protected IStatus run(DBRProgressMonitor monitor)
{
......
......@@ -43,8 +43,6 @@ import org.jkiss.dbeaver.model.sql.SQLSyntaxManager;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.runtime.jobs.DataSourceJob;
import org.jkiss.dbeaver.runtime.properties.PropertyCollector;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase;
import org.jkiss.utils.ArrayUtils;
......@@ -398,7 +396,7 @@ public class SQLContextInformer
protected TablesFinderJob(@NotNull DBCExecutionContext executionContext, @NotNull DBSStructureAssistant structureAssistant, @Nullable String[] containerNames, @NotNull String objectName, boolean caseSensitive, @NotNull ObjectLookupCache cache)
{
super("Find object '" + objectName + "'", DBeaverIcons.getImageDescriptor(UIIcon.SQL_EXECUTE), executionContext);
super("Find object '" + objectName + "'", executionContext);
this.structureAssistant = structureAssistant;
// Transform container name case
this.containerNames = containerNames;
......
......@@ -108,7 +108,7 @@ public class DataSourceManagementToolbar implements DBPRegistryListener, DBPEven
private boolean enabled;
public DatabaseListReader(@NotNull DBCExecutionContext context) {
super(CoreMessages.toolbar_datasource_selector_action_read_databases, null, context);
super(CoreMessages.toolbar_datasource_selector_action_read_databases, context);
setSystem(true);
this.enabled = false;
}
......
......@@ -183,7 +183,7 @@ public abstract class ExasolBaseTableToolDialog
generateObjectCommand(lines, object);
objectsSQL.put(object, lines);
}
final DataSourceJob job = new DataSourceJob(jobName, null, getExecutionContext()) {
final DataSourceJob job = new DataSourceJob(jobName, getExecutionContext()) {
public Exception objectProcessingError;
@SuppressWarnings("rawtypes")
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.net;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBExecUtils;
import org.jkiss.dbeaver.model.impl.net.SocksConstants;
import org.jkiss.dbeaver.model.net.DBWHandlerConfiguration;
import org.jkiss.dbeaver.model.net.DBWHandlerType;
import org.jkiss.utils.CommonUtils;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Global proxy selector
*/
public class GlobalProxySelector extends ProxySelector {
private static final Log log = Log.getLog(GlobalProxySelector.class);
private final ProxySelector parent;
public GlobalProxySelector(ProxySelector parent) {
this.parent = parent;
}
@Override
public List<Proxy> select(URI uri) {
String scheme = uri.getScheme();
if (CommonUtils.isEmpty(scheme)) {
return parent.select(uri);
}
if (scheme.startsWith("http")) {
// 1. Check for drivers download proxy
}
if (SocksConstants.SOCKET_SCHEME.equals(scheme)) {
// 2. Check for connections' proxy config
DBCExecutionContext activeContext = DBExecUtils.findConnectionContext(uri.getHost(), uri.getPort(), uri.getPath());
if (activeContext != null) {
List<Proxy> proxies = null;
DBPDataSourceContainer container = activeContext.getDataSource().getContainer();
for (DBWHandlerConfiguration networkHandler : container.getConnectionConfiguration().getDeclaredHandlers()) {
if (networkHandler.isEnabled() && networkHandler.getType() == DBWHandlerType.PROXY) {
Map<String,String> proxyProps = networkHandler.getProperties();
String proxyHost = proxyProps.get(SocksConstants.PROP_HOST);
String proxyPort = proxyProps.get(SocksConstants.PROP_PORT);
if (!CommonUtils.isEmpty(proxyHost)) {
int portNumber = SocksConstants.DEFAULT_SOCKS_PORT;
if (!CommonUtils.isEmpty(proxyPort)) {
try {
portNumber = Integer.parseInt(proxyPort);
} catch (NumberFormatException e) {
log.warn("Bad proxy port number", e);
}
}
InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, portNumber);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
if (proxies == null) {
proxies = new ArrayList<>();
}
proxies.add(proxy);
log.debug("Use SOCKS proxy [" + proxyAddr + "]");
}
}
}
if (proxies != null) {
return proxies;
}
}
}
return parent.select(uri);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
parent.connectFailed(uri, sa, ioe);
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.runtime.net;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBExecUtils;
import org.jkiss.dbeaver.model.impl.net.SocksConstants;
import org.jkiss.dbeaver.model.net.DBWHandlerConfiguration;
import org.jkiss.dbeaver.model.net.DBWHandlerType;
import org.jkiss.utils.CommonUtils;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Global proxy selector
*/
public class GlobalProxySelector extends ProxySelector {
private static final Log log = Log.getLog(GlobalProxySelector.class);
private final ProxySelector parent;
public GlobalProxySelector(ProxySelector parent) {
this.parent = parent;
}
@Override
public List<Proxy> select(URI uri) {
String scheme = uri.getScheme();
if (CommonUtils.isEmpty(scheme)) {
return parent.select(uri);
}
if (scheme.startsWith("http")) {
// 1. Check for drivers download proxy
}
if (SocksConstants.SOCKET_SCHEME.equals(scheme)) {
// 2. Check for connections' proxy config
DBCExecutionContext activeContext = DBExecUtils.findConnectionContext(uri.getHost(), uri.getPort(), uri.getPath());
if (activeContext != null) {
List<Proxy> proxies = null;
DBPDataSourceContainer container = activeContext.getDataSource().getContainer();
for (DBWHandlerConfiguration networkHandler : container.getConnectionConfiguration().getDeclaredHandlers()) {
if (networkHandler.isEnabled() && networkHandler.getType() == DBWHandlerType.PROXY) {
Map<String,String> proxyProps = networkHandler.getProperties();
String proxyHost = proxyProps.get(SocksConstants.PROP_HOST);
String proxyPort = proxyProps.get(SocksConstants.PROP_PORT);
if (!CommonUtils.isEmpty(proxyHost)) {
int portNumber = SocksConstants.DEFAULT_SOCKS_PORT;
if (!CommonUtils.isEmpty(proxyPort)) {
try {
portNumber = Integer.parseInt(proxyPort);
} catch (NumberFormatException e) {
log.warn("Bad proxy port number", e);
}
}
InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, portNumber);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
if (proxies == null) {
proxies = new ArrayList<>();
}
proxies.add(proxy);
log.debug("Use SOCKS proxy [" + proxyAddr + "]");
}
}
}
if (proxies != null) {
return proxies;
}
}
}
return parent.select(uri);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
parent.connectFailed(uri, sa, ioe);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册