Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
fix: Added support for socksNonProxyHosts property (#975) (#985)
When a socksProxyHost is configured, there needs to be an escape value so that some hosts are resolved immediately. The Java Networking spec[1] does not specify such an option, but the socksNonProxyHosts is used in the sun.net.spi.DefaultProxySelector. The behavior is mentioned in bug report 5001942[2]. This commit reimplements the logic in the DefaultProxySelector.select method to determine if the host should be resolved immediately. [1] http://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html [2] http://bugs.java.com/bugdatabase/view_bug.do?bug_id=5001942
- Loading branch information
Showing
with
193 additions
and 3 deletions.
- +2 −3 pgjdbc/src/main/java/org/postgresql/core/PGStream.java
- +53 −0 pgjdbc/src/main/java/org/postgresql/util/HostSpec.java
- +18 −0 pgjdbc/src/test/java/org/postgresql/test/jre8/core/Jre8TestSuite.java
- +48 −0 pgjdbc/src/test/java/org/postgresql/test/jre8/core/SocksProxyTest.java
- +72 −0 pgjdbc/src/test/java/org/postgresql/test/util/HostSpecTest.java
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) 2017, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.test.jre8.core; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
|
||
/** | ||
* @author Joe Kutner on 10/24/17. | ||
* Twitter: @codefinger | ||
*/ | ||
@RunWith(Suite.class) | ||
@Suite.SuiteClasses({SocksProxyTest.class}) | ||
public class Jre8TestSuite { | ||
} |
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2017, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.test.jre8.core; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.postgresql.test.TestUtil; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
|
||
/** | ||
* @author Joe Kutner on 10/9/17. | ||
* Twitter: @codefinger | ||
*/ | ||
public class SocksProxyTest { | ||
|
||
@After | ||
public void cleanup() { | ||
System.clearProperty("socksProxyHost"); | ||
System.clearProperty("socksProxyPort"); | ||
System.clearProperty("socksNonProxyHosts"); | ||
} | ||
|
||
/** | ||
* Tests the connect method by connecting to the test database | ||
*/ | ||
@Test | ||
public void testConnectWithSocksNonProxyHost() throws Exception { | ||
System.setProperty("socksProxyHost", "fake-socks-proxy"); | ||
System.setProperty("socksProxyPort", "9999"); | ||
System.setProperty("socksNonProxyHosts", TestUtil.getServer()); | ||
|
||
TestUtil.initDriver(); // Set up log levels, etc. | ||
|
||
Connection con = | ||
DriverManager.getConnection(TestUtil.getURL(), TestUtil.getUser(), TestUtil.getPassword()); | ||
|
||
assertNotNull(con); | ||
con.close(); | ||
} | ||
} |
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2017, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.test.util; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.postgresql.util.HostSpec; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author Joe Kutner on 10/19/17. | ||
* Twitter: @codefinger | ||
*/ | ||
public class HostSpecTest { | ||
|
||
@After | ||
public void cleanup() { | ||
System.clearProperty("socksProxyHost"); | ||
System.clearProperty("socksProxyPort"); | ||
System.clearProperty("socksNonProxyHosts"); | ||
} | ||
|
||
@Test | ||
public void testShouldResolve() throws Exception { | ||
HostSpec hostSpec = new HostSpec("localhost", 5432); | ||
assertTrue(hostSpec.shouldResolve()); | ||
} | ||
|
||
@Test | ||
public void testShouldResolveWithSocksProxyHost() throws Exception { | ||
System.setProperty("socksProxyHost", "fake-socks-proxy"); | ||
HostSpec hostSpec = new HostSpec("example.com", 5432); | ||
assertFalse(hostSpec.shouldResolve()); | ||
} | ||
|
||
@Test | ||
public void testShouldResolveWithSocksProxyHostWithLocalhost() throws Exception { | ||
System.setProperty("socksProxyHost", "fake-socks-proxy"); | ||
HostSpec hostSpec = new HostSpec("localhost", 5432); | ||
assertTrue(hostSpec.shouldResolve()); | ||
} | ||
|
||
@Test | ||
public void testShouldResolveWithSocksNonProxyHost() throws Exception { | ||
System.setProperty("socksProxyHost", "fake-socks-proxy"); | ||
System.setProperty("socksNonProxyHosts", "example.com"); | ||
HostSpec hostSpec = new HostSpec("example.com", 5432); | ||
assertTrue(hostSpec.shouldResolve()); | ||
} | ||
|
||
@Test | ||
public void testShouldResolveWithSocksNonProxyHosts() throws Exception { | ||
System.setProperty("socksProxyHost", "fake-socks-proxy"); | ||
System.setProperty("socksNonProxyHosts", "example.com|localhost"); | ||
HostSpec hostSpec = new HostSpec("example.com", 5432); | ||
assertTrue(hostSpec.shouldResolve()); | ||
} | ||
|
||
@Test | ||
public void testShouldResolveWithSocksNonProxyHostsNotMatching() throws Exception { | ||
System.setProperty("socksProxyHost", "fake-socks-proxy"); | ||
System.setProperty("socksNonProxyHosts", "example.com|localhost"); | ||
HostSpec hostSpec = new HostSpec("example.org", 5432); | ||
assertFalse(hostSpec.shouldResolve()); | ||
} | ||
} |