Skip to content

Commit

Permalink
Refactor interface to use URI for lookup of proxy configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Dec 10, 2020
1 parent aea4dba commit f88e7ac
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 61 deletions.
Expand Up @@ -26,6 +26,7 @@
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.http.DisabledServiceUnavailableRetryStrategy;
import ch.cyberduck.core.proxy.ProxyFactory;
import ch.cyberduck.core.proxy.ProxyHostUrlProvider;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
Expand Down Expand Up @@ -57,7 +58,7 @@ public boolean retryRequest(final HttpResponse response, final int executionCoun
final Host bookmark = session.getHost();
final Credentials credentials = bookmark.getCredentials();
credentials.reset();
session.login(ProxyFactory.get().find(bookmark), prompt, new DisabledCancelCallback());
session.login(ProxyFactory.get().find(new ProxyHostUrlProvider().get(bookmark)), prompt, new DisabledCancelCallback());
if(credentials.isSaved()) {
store.save(bookmark);
}
Expand Down
Expand Up @@ -18,8 +18,6 @@
* dkocher@cyberduck.ch
*/

import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostUrlProvider;
import ch.cyberduck.core.library.Native;
import ch.cyberduck.core.preferences.Preferences;
import ch.cyberduck.core.preferences.PreferencesFactory;
Expand All @@ -37,18 +35,15 @@ public final class SystemConfigurationProxy extends AbstractProxyFinder implemen
Native.load("core");
}

private final HostUrlProvider provider
= new ProxyHostUrlProvider();

private final Preferences preferences
= PreferencesFactory.get();

@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
if(!preferences.getBoolean("connection.proxy.enable")) {
return Proxy.DIRECT;
}
final String route = this.findNative(provider.get(target));
final String route = this.findNative(target);
if(null == route) {
if(log.isInfoEnabled()) {
log.info(String.format("No proxy configuration found for target %s", target));
Expand Down
Expand Up @@ -55,7 +55,7 @@ public void testCreateSocketWithProxy() throws Exception {
final Socket socket = new ProxySocketFactory(new Host(new TestProtocol(), "localhost"), new DefaultSocketConfigurator(),
new ProxyFinder() {
@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
return new Proxy(Proxy.Type.SOCKS, "localhost", 7000);
}
}).createSocket();
Expand Down
@@ -1,8 +1,5 @@
package ch.cyberduck.core.proxy;

import ch.cyberduck.core.Host;
import ch.cyberduck.core.TestProtocol;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -12,18 +9,21 @@ public class SystemConfigurationProxyTest {
@Test
public void testFind() {
final SystemConfigurationProxy proxy = new SystemConfigurationProxy();
assertEquals(Proxy.Type.DIRECT, proxy.find(new Host(new TestProtocol(), "cyberduck.io")).getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("http://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("sftp://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("ftp://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("ftps://cyberduck.io").getType());
}

@Test
public void testExcludedLocalHost() {
final SystemConfigurationProxy proxy = new SystemConfigurationProxy();
assertEquals(Proxy.Type.DIRECT, proxy.find(new Host(new TestProtocol(), "cyberduck.local")).getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("http://cyberduck.local").getType());
}

@Test
public void testSimpleExcluded() {
final SystemConfigurationProxy proxy = new SystemConfigurationProxy();
assertEquals(Proxy.Type.DIRECT, proxy.find(new Host(new TestProtocol(), "simple")).getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("http://simple").getType());
}
}
Expand Up @@ -23,6 +23,7 @@
import ch.cyberduck.core.proxy.Proxy;
import ch.cyberduck.core.proxy.ProxyFactory;
import ch.cyberduck.core.proxy.ProxyFinder;
import ch.cyberduck.core.proxy.ProxyHostUrlProvider;
import ch.cyberduck.core.threading.CancelCallback;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void connect(final Session<?> session, final CancelCallback callback) thr
// Try to resolve the hostname first
final String hostname = HostnameConfiguratorFactory.get(bookmark.getProtocol()).getHostname(bookmark.getHostname());
listener.message(MessageFormat.format(LocaleFactory.localizedString("Resolving {0}", "Status"), hostname));
final Proxy proxy = this.proxy.find(bookmark);
final Proxy proxy = this.proxy.find(new ProxyHostUrlProvider().get(bookmark));
if(proxy == Proxy.DIRECT) {
// Only try to resolve target hostname if direct connection
if(null == JumpHostConfiguratorFactory.get(bookmark.getProtocol()).getJumphost(bookmark.getHostname())) {
Expand Down
Expand Up @@ -17,8 +17,6 @@
* Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
*/

import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostUrlProvider;
import ch.cyberduck.core.preferences.Preferences;
import ch.cyberduck.core.preferences.PreferencesFactory;

Expand All @@ -29,20 +27,17 @@
public class DefaultProxyFinder implements ProxyFinder {

private final ProxySelector selector
= ProxySelector.getDefault();
= ProxySelector.getDefault();

private final Preferences preferences
= PreferencesFactory.get();

private final HostUrlProvider provider
= new ProxyHostUrlProvider();
= PreferencesFactory.get();

@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
if(!preferences.getBoolean("connection.proxy.enable")) {
return Proxy.DIRECT;
}
for(java.net.Proxy proxy : selector.select(URI.create(provider.get(target)))) {
for(java.net.Proxy proxy : selector.select(URI.create(target))) {
switch(proxy.type()) {
case DIRECT: {
return Proxy.DIRECT;
Expand Down
Expand Up @@ -18,11 +18,9 @@
* feedback@cyberduck.io
*/

import ch.cyberduck.core.Host;

public class DisabledProxyFinder implements ProxyFinder {
@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
return Proxy.DIRECT;
}
}
@@ -1,6 +1,6 @@
package ch.cyberduck.core.proxy;

import ch.cyberduck.core.Host;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.preferences.Preferences;
import ch.cyberduck.core.preferences.PreferencesFactory;

Expand All @@ -17,11 +17,11 @@ public class EnvironmentVariableProxyFinder implements ProxyFinder {
= PreferencesFactory.get();

@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
if(!preferences.getBoolean("connection.proxy.enable")) {
return Proxy.DIRECT;
}
switch(target.getProtocol().getScheme()) {
switch(Scheme.valueOf(URI.create(target).getScheme())) {
case ftp:
case ftps:
case sftp:
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/ch/cyberduck/core/proxy/ProxyFinder.java
Expand Up @@ -19,8 +19,6 @@
* dkocher@cyberduck.ch
*/

import ch.cyberduck.core.Host;

public interface ProxyFinder {
Proxy find(Host target);
Proxy find(String target);
}
Expand Up @@ -85,7 +85,7 @@ public ProxySocketFactory withBlacklistedNetworkInterfaces(final List<String> na
* direct connection socket factory.
*/
protected SocketFactory factory(final String target) {
final Proxy proxy = proxyFinder.find(host);
final Proxy proxy = proxyFinder.find(target);
if(!types.contains(proxy.getType())) {
log.warn(String.format("Use of %s proxy is disabled for socket factory %s", proxy.getType(), this));
return new DefaultSocketFactory();
Expand Down
Expand Up @@ -35,6 +35,7 @@
import ch.cyberduck.core.pool.SessionPool;
import ch.cyberduck.core.preferences.PreferencesFactory;
import ch.cyberduck.core.proxy.ProxyFactory;
import ch.cyberduck.core.proxy.ProxyHostUrlProvider;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
Expand Down Expand Up @@ -170,7 +171,7 @@ protected boolean login(final Session<?> session, final LoginFailureException e)
log.debug(String.format("Re-authenticate with credentials %s", bookmark.getCredentials()));
}
// Try to authenticate again
service.authenticate(ProxyFactory.get().find(bookmark), session, progress, login, new CancelCallback() {
service.authenticate(ProxyFactory.get().find(new ProxyHostUrlProvider().get(bookmark)), session, progress, login, new CancelCallback() {
@Override
public void verify() throws ConnectionCanceledException {
if(SessionBackgroundAction.this.isCanceled()) {
Expand Down
Expand Up @@ -30,7 +30,7 @@ public boolean isConnected() {
final LoginConnectionService s = new LoginConnectionService(new DisabledLoginCallback(), new DisabledHostKeyCallback(), new DisabledPasswordStore(), new DisabledProgressListener(),
new DisabledProxyFinder() {
@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
return new Proxy(Proxy.Type.HTTP, "proxy.local", 6666);
}
});
Expand Down
Expand Up @@ -17,9 +17,6 @@
* Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
*/

import ch.cyberduck.core.Host;
import ch.cyberduck.core.TestProtocol;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -29,19 +26,23 @@ public class DefaultProxyFinderTest {
@Test
public void testFind() {
final DefaultProxyFinder proxy = new DefaultProxyFinder();
assertEquals(Proxy.Type.DIRECT, proxy.find(new Host(new TestProtocol(), "cyberduck.io")).getType());
// assertEquals(Proxy.Type.HTTP, proxy.find(new Host(ProtocolFactory.WEBDAV, "cyberduck.io")).getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("http://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("sftp://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("ftp://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("ftps://cyberduck.io").getType());
}

@Test
public void testExcludedLocalHost() {
final DefaultProxyFinder proxy = new DefaultProxyFinder();
assertEquals(Proxy.Type.DIRECT, proxy.find(new Host(new TestProtocol(), "cyberduck.local")).getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("http://cyberduck.local").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("sftp://cyberduck.local").getType());
}

@Test
public void testSimpleExcluded() {
final DefaultProxyFinder proxy = new DefaultProxyFinder();
assertEquals(Proxy.Type.DIRECT, proxy.find(new Host(new TestProtocol(), "simple")).getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("http://simple").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("sftp://simple").getType());
}
}
}
@@ -0,0 +1,32 @@
package ch.cyberduck.core.proxy;

/*
* Copyright (c) 2002-2020 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class EnvironmentVariableProxyFinderTest {

@Test
public void testFind() {
final DefaultProxyFinder proxy = new DefaultProxyFinder();
assertEquals(Proxy.Type.DIRECT, proxy.find("http://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("sftp://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("ftp://cyberduck.io").getType());
assertEquals(Proxy.Type.DIRECT, proxy.find("ftps://cyberduck.io").getType());
}
}
Expand Up @@ -19,8 +19,6 @@
* dkocher@cyberduck.ch
*/

import ch.cyberduck.core.Host;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
Expand All @@ -32,7 +30,7 @@ public class ProxyFinderTest {
public void testWildcard() {
AbstractProxyFinder p = new AbstractProxyFinder() {
@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
throw new UnsupportedOperationException();
}
};
Expand Down
Expand Up @@ -160,7 +160,7 @@ public void testProxyNoConnect() throws Exception {
new DisabledProgressListener(),
new ProxyFinder() {
@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
return new Proxy(Proxy.Type.HTTP, "localhost", 3128);
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ public Credentials prompt(final Host bookmark, final String username, final Stri
new DisabledProgressListener(),
new ProxyFinder() {
@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
return new Proxy(Proxy.Type.HTTP, "localhost", 3128);
}
}
Expand Down
Expand Up @@ -62,7 +62,7 @@ public void testConnectHttpProxyForbiddenHttpResponse() throws Exception {
new DisabledProgressListener(),
new ProxyFinder() {
@Override
public Proxy find(final Host target) {
public Proxy find(final String target) {
return new Proxy(Proxy.Type.HTTP, "localhost", 3128);
}
});
Expand Down
Expand Up @@ -19,7 +19,6 @@
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostKeyCallback;
import ch.cyberduck.core.HostParser;
import ch.cyberduck.core.ListService;
import ch.cyberduck.core.LoginCallback;
import ch.cyberduck.core.PreferencesUseragentProvider;
Expand Down Expand Up @@ -69,7 +68,7 @@ public DriveSession(final Host host, final X509TrustManager trust, final X509Key

@Override
protected Drive connect(final Proxy proxy, final HostKeyCallback callback, final LoginCallback prompt) throws HostParserException {
final HttpClientBuilder configuration = builder.build(ProxyFactory.get().find(HostParser.parse(host.getProtocol().getOAuthAuthorizationUrl())), this, prompt);
final HttpClientBuilder configuration = builder.build(ProxyFactory.get().find(host.getProtocol().getOAuthAuthorizationUrl()), this, prompt);
authorizationService = new OAuth2RequestInterceptor(configuration.build(), host.getProtocol())
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
Expand Down
Expand Up @@ -18,7 +18,6 @@
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostKeyCallback;
import ch.cyberduck.core.HostParser;
import ch.cyberduck.core.LoginCallback;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.exception.BackgroundException;
Expand Down Expand Up @@ -131,7 +130,7 @@ public OneDriveFolder toFolder(final Path file, final boolean resolveLastItem) t

@Override
protected OneDriveAPI connect(final Proxy proxy, final HostKeyCallback key, final LoginCallback prompt) throws HostParserException {
final HttpClientBuilder configuration = builder.build(ProxyFactory.get().find(HostParser.parse(host.getProtocol().getOAuthAuthorizationUrl())), this, prompt);
final HttpClientBuilder configuration = builder.build(ProxyFactory.get().find(host.getProtocol().getOAuthAuthorizationUrl()), this, prompt);
authorizationService = new OAuth2RequestInterceptor(configuration.build(), host.getProtocol()) {
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
Expand Down
Expand Up @@ -71,7 +71,7 @@ public Credentials get() throws BackgroundException {
final Path access = new Path(PathNormalizer.normalize(address.getDefaultPath()), EnumSet.of(Path.Type.file));
address.setDefaultPath(String.valueOf(Path.DELIMITER));
final DAVSession connection = new DAVSession(address, trust, key);
connection.withListener(transcript).open(ProxyFactory.get().find(address), new DisabledHostKeyCallback(), new DisabledLoginCallback());
connection.withListener(transcript).open(ProxyFactory.get().find(url), new DisabledHostKeyCallback(), new DisabledLoginCallback());
final InputStream in = new DAVReadFeature(connection).read(access, new TransferStatus(), new DisabledConnectionCallback());
try {
final Credentials credentials = this.parse(in);
Expand Down
Expand Up @@ -26,6 +26,7 @@
import ch.cyberduck.core.preferences.PreferencesFactory;
import ch.cyberduck.core.proxy.Proxy;
import ch.cyberduck.core.proxy.ProxyFactory;
import ch.cyberduck.core.proxy.ProxyHostUrlProvider;
import ch.cyberduck.core.proxy.ProxySocketFactory;
import ch.cyberduck.core.ssl.CustomTrustSSLProtocolSocketFactory;
import ch.cyberduck.core.ssl.ThreadLocalHostnameDelegatingTrustManager;
Expand Down Expand Up @@ -68,7 +69,7 @@ public InetAddress[] resolve(final String host) throws UnknownHostException {
this.setMaxErrorRetry(0);
this.setMaxConnections(1);
this.setUseGzip(PreferencesFactory.get().getBoolean("http.compression.enable"));
final Proxy proxy = ProxyFactory.get().find(host);
final Proxy proxy = ProxyFactory.get().find(new ProxyHostUrlProvider().get(host));
switch(proxy.getType()) {
case HTTP:
case HTTPS:
Expand Down

0 comments on commit f88e7ac

Please sign in to comment.