Subject: [pgjdbc/pgjdbc] issue #975: Version from 42.1.0 can't connect to 127.0.0.1 with System SOCKS proxy

From: "YuGe (@YuGe)" <[email-masked]>
Date: Wed, 04 Oct 2017 01:27:34 +0000

Hi,
I found Postgresql JDBC Driver cannot connect to local postgresql server when I set a system SOCKS proxy.

org.postgresql.util.PSQLException: The connection attempt failed.
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:275)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
	at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
	at org.postgresql.Driver.makeConnection(Driver.java:450)
	at org.postgresql.Driver.connect(Driver.java:252)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:208)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:201)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:196)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:93)
	at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:404)
.......

Caused by: java.net.UnknownHostException: 127.0.0.1
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at org.postgresql.core.PGStream.<init>(PGStream.java:68)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
	... 69 more

system:  macOS High Sierra
java: 1.8.0_144

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Wed, 04 Oct 2017 20:15:11 +0000

@YuGe  can you try with the latest code before I go chase this down ?

---

From: "jgangemi (@jgangemi)" <[email-masked]>
Date: Thu, 05 Oct 2017 14:14:10 +0000

seeing this same behavior with version `42.1.4`.
```
[HikariPool-1 connection adder] ERROR org.postgresql.ds.common.BaseDataSource - Failed to create a Non-Pooling DataSource from PostgreSQL JDBC Driver 42.1.4 for fwptools at jdbc:postgresql://172.16.3.128:32774/fwptools?loginTimeout=30: org.postgresql.util.PSQLException: The connection attempt failed.
[HikariPool-1 connection adder] ERROR org.postgresql.Driver - Connection error: 
org.postgresql.util.PSQLException: The connection attempt failed.
	at org.postgresql.Driver$ConnectThread.getResult(Driver.java:401)
	at org.postgresql.Driver.connect(Driver.java:259)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:94)
	at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:375)
	at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:204)
	at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:445)
	at com.zaxxer.hikari.pool.HikariPool.access$200(HikariPool.java:72)
	at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:632)
	at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:618)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.UnknownHostException: 172.16.3.128
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at org.postgresql.core.PGStream.<init>(PGStream.java:68)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
	at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
	at org.postgresql.Driver.makeConnection(Driver.java:450)
	at org.postgresql.Driver.access$100(Driver.java:60)
	at org.postgresql.Driver$ConnectThread.run(Driver.java:360)
	... 1 more
```
in this case `172.16.3.128` is the ip of my docker vm that the jvm is told to ignore via
```
-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=9998 -DsocksNonProxyHosts=172.16.3.128
```
the last driver version that works w/ this configuration is `42.0.0`

looking through the changelog history, i see a reference to #774 which i am guessing is the cause of this behavior.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Thu, 05 Oct 2017 14:20:13 +0000

so can you use the snapshot to test to make sure this fixes it?

---

From: "jgangemi (@jgangemi)" <[email-masked]>
Date: Thu, 05 Oct 2017 14:22:39 +0000

`42.1.5-SNAPSHOT`?

is it pushed to maven central?

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Thu, 05 Oct 2017 14:25:44 +0000

hmmm apparently not, somehow I thought it was. It is easy enough to compile though

---

From: "jgangemi (@jgangemi)" <[email-masked]>
Date: Thu, 05 Oct 2017 15:05:41 +0000

if you can get it pushed up there, i can test to see if it fixes the issue.

---

From: "jorsol (@jorsol)" <[email-masked]>
Date: Thu, 05 Oct 2017 15:18:58 +0000

Snapshots can be found on: https://oss.sonatype.org/index.html#view-repositories;snapshots~browsestorage~/org/postgresql/postgresql/42.1.5-SNAPSHOT/postgresql-42.1.5-20171003.181504-15.jar

---

From: "jgangemi (@jgangemi)" <[email-masked]>
Date: Thu, 05 Oct 2017 15:43:04 +0000

i compiled from source - unfortunately the problem still exists.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Thu, 05 Oct 2017 15:47:55 +0000

Can you try this without the pool in the mix ?

Dave Cramer

On 5 October 2017 at 11:43, Jae Gangemi <[email-masked]> wrote:

> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://github.com/pgjdbc/pgjdbc/issues/975#issuecomment-334506043>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAYz9lmKP6SQLDOCxpRUITcDFCUHR7xJks5spPkJgaJpZM4Ps_M0>
> .
>

---

From: "jgangemi (@jgangemi)" <[email-masked]>
Date: Thu, 05 Oct 2017 16:11:26 +0000

not at the moment, but maybe later today. i'm not sure how the pool would affect this given there are no issues w/ older version of the driver.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Thu, 05 Oct 2017 16:19:47 +0000

which older version worked ?

Dave Cramer

On 5 October 2017 at 12:11, Jae Gangemi <[email-masked]> wrote:

> not at the moment, but maybe later today. i'm not sure how the pool would
> affect this given there are no issues w/ older version of the driver.
>
> —
> <https://github.com/pgjdbc/pgjdbc/issues/975#issuecomment-334514579>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAYz9tgxrwSUTOEk4aOI8n9Z5xMPlubNks5spP-vgaJpZM4Ps_M0>
> .
>

---

From: "jgangemi (@jgangemi)" <[email-masked]>
Date: Thu, 05 Oct 2017 16:25:43 +0000

it works in `42.0.0` and then breaks in anything past it. `42.1.0` introduced PR #774 which is what i believe is causing the breakage.

---

From: "jorsol (@jorsol)" <[email-masked]>
Date: Thu, 05 Oct 2017 16:39:27 +0000

This might require a revert of that PR since it's giving problems.

@jkutner do you have any comments on this problem?

---

From: "jgangemi (@jgangemi)" <[email-masked]>
Date: Thu, 05 Oct 2017 16:51:49 +0000

i get what's trying to be accomplished here but i'm not sure how to easily fix it. i believe part of the problem is that the `socksNonProxyHosts` system property is not being taken into account. the code just checks to see if a proxy is in use and if so, immediately defer.

maybe the lookup could be deferred and if fails to resolve via the proxy it could be tried locally or vice versa.

one way i've worked around this is to just create entries in my local `/etc/hosts` files for the remote systems that need to be resolved. i realize that won't work for everyone but might help some.

---

From: "jkutner (@jkutner)" <[email-masked]>
Date: Fri, 06 Oct 2017 04:46:56 +0000

@jorsol thanks for the ping. I can make a patch quickly if that is preferable to reverting. however, I'm unsure of the best solution. I think we have three options:

* Explicitly check for `localhost`, or `127.0.0.1` and disable the [deferred host resolution](https://github.com/pgjdbc/pgjdbc/blob/acff9495b8745bce30d93d61e77caf81d9748b4b/pgjdbc/src/main/java/org/postgresql/core/PGStream.java#L66-L68).
* Attempt to resolve the hostname if the deferred resolution fails (i.e. try to resolve the hostname with `new InetSocketAddress` if `socket.connect` fails).
* Add an option to explicitly disable the deferred resolution (i.e. never use the socks proxy).

How do other libraries/frameworks deal with `localhost` and a `socksProxyHost`? There might be a precedent.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Fri, 06 Oct 2017 13:16:06 +0000

@jkutner  I'd prefer not to revert. 

I'm less inclined to add yet another option for this, so I'm inclined to go with checking for localhost

---

From: "jkutner (@jkutner)" <[email-masked]>
Date: Fri, 06 Oct 2017 15:25:13 +0000

@davecramer I'm happy to make that change. But I'm still uncertain if it's the correct behavior. If you configure a SOCKS proxy for the Safari browser on the Mac, it tries to resolve `localhost` through the proxy. There is an option in Preferences to "Bypass proxy settings" for certain Host & Domains.

I'd like to take some time to investigate how other JVM frameworks handle this case.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Fri, 06 Oct 2017 15:30:28 +0000

@jkutner thanks, that would be appreciated

---

From: "vlsi (@vlsi)" <[email-masked]>
Date: Fri, 06 Oct 2017 20:45:33 +0000

It looks like http://bugs.java.com/bugdatabase/view_bug.do?bug_id=5001942 was fixed in 1.8u65.
That is 1.8u65+ should be able to get "system settings" for proxy configuration, and it should be able to use `socksNonProxyHosts` if provided.

---

From: "jkutner (@jkutner)" <[email-masked]>
Date: Sat, 07 Oct 2017 15:10:38 +0000

@visi thanks for the info! I'll use this to make a patch.

---

From: "vitalyster (@vitalyster)" <[email-masked]>
Date: Wed, 15 Jan 2025 04:04:01 +0000

I have the same issue with pgjdbc 42.7.5 and macOS 15: even if "localhost" added to "Bypass proxy settings for these hosts" option, it is still throws. Same for "127.0.0.1". Version 42.0.0 works fine.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Wed, 15 Jan 2025 10:38:24 +0000

@vitalyster can you give me some sample code to replicate this problem ?

---

From: "vitalyster (@vitalyster)" <[email-masked]>
Date: Wed, 15 Jan 2025 11:19:30 +0000

@davecramer nothing special, just default Spring Boot template project + jdbc configuration: https://github.com/vitalyster/pgjdbc-proxy
You can run it with `./gradlew test` and receive `UnknownHostException: localhost`
If you uncomment `ext['postgresql.version']` in `build.gradle` to override pgjdbc version to 42.0.0 - it will pass.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Wed, 15 Jan 2025 12:21:34 +0000

I made one change to your code and it ran fine

```
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 107ea31..5413e06 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1,3 +1,3 @@
 spring.application.name=demo
 spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration
-spring.datasource.url=jdbc:postgresql://localhost:5432/vt
+spring.datasource.url=jdbc:postgresql://localhost:5432/test
```
```
./gradlew test
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2025-01-15T07:19:13.011-05:00  INFO 83909 --- [demo] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2025-01-15T07:19:13.012-05:00  INFO 83909 --- [demo] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
```

---

From: "vitalyster (@vitalyster)" <[email-masked]>
Date: Wed, 15 Jan 2025 12:56:12 +0000

Does it run with socks proxy in system settings?

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Wed, 15 Jan 2025 12:59:39 +0000

I've never used socks proxy. Is this on macos ? windows ?

---

From: "vitalyster (@vitalyster)" <[email-masked]>
Date: Wed, 15 Jan 2025 13:02:43 +0000

This is an issue with macOS system proxy as stated in the original issue

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Wed, 15 Jan 2025 16:28:21 +0000

Need to do more investigation, but bypassing localhost in the proxy configuration allows it to work

---

From: "vitalyster (@vitalyster)" <[email-masked]>
Date: Wed, 15 Jan 2025 17:01:45 +0000

Well, found the problem on my side: I had multiple bypass hosts, and hosts  in "bypass" string should be comma-separated without spaces. `github.com,localhost` works, `github.com, localhost` doesn't work.