Java proxies connection to postgres - java

Could someone help me or suggest a solution? I want to connect from a computer that has firewall to other where the postgres server run. The problem is that computer (client) has a firewall and I don't have access to configure it, or open ports, ping does not respond. The computer (server) where PostgreSQL has open ports but I cannot connect to it from another because of a firewall. I can only access the computer through proxy.
How I could with Java programming access remotely through proxy to postgres forgetting firewall?
Java has a connection with proxies. But I don't know how to put it together with postgres connection.
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "67.210.82.198" );
System.getProperties().put( "proxyPort", "80" );
URL validateURL = new URL("http://domain.com");
URLConnection urlConnection = validateURL.openConnection();
//how put together ???
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://ipPublica:5432/DataBase","user", "pass");

That can't be done. PostgreSQL connections are not HTTP connections. Yo cannot use an HTTP proxy for PostgreSQL. Maybe a socks proxy will do the work.

Try
System.setProperty("http.proxyHost", "67.210.82.198");
System.setPropery("http.proxyPort", "80");
String url = "jdbc:postgresql://host:port/database";
Properties props = new Properties();
props.setProperty("user","myUsername");
props.setProperty("password","myPassword");
props.setProperty("ssl","true");
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(url, props);
For more, see java networking & proxies.

This question is quite old. It also has been asked on the PostgreSQL mailing list:
http://archives.postgresql.org/pgsql-jdbc/2010-08/msg00021.php
The answers over there boils down to:
Use SOCKs
If (big IF!) you want to hack the JDBC driver source, you might (big "might"!) be able to make it work over a HTTP-Proxy using SSL.
Sidekick: Someone proposed an ssh tunnel.

You'll need a SOCKS proxy, rather than a HTTP proxy.
Then your code will look something like this:
System.setProperty("socksProxyHost", host);
System.setProperty("socksProxyPort", "1080");
Details:
http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html

Related

Can ojdbc connect to an Oracle database via a SOCKS proxy?

I am trying to connect to an oracle database using the ojdbc driver via a SOCKS proxy. I cannot find a definitive answer online as to whether this should work or not.
Does Oracle jdbc driver support SOCKS5 proxy? seems to suggest that the 18.1 release should have added SOCKS proxy support, but, using ojdbc8-18.3, the driver doesn't seem to honour the proxy settings and the latest documentation makes no mention of it.
Here is analogous code to that which I have tried (database details redacted).
System.setProperty("socksProxyHost", "myProxy");
System.setProperty("socksProxyPort", "1080");
Class.forName("oracle.jdbc.driver.OracleDriver");
try (
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:#myServer:1521/mySID", "myUser", "myPwd"
);
)
{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(" select * from my_table ");
while(rs.next())
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getInt(3));
​
}
catch (Exception e)
{
System.out.println(e);
}
In this case, the connection succeeds, but when I use Wireshark to check the outgoing traffic, I can see that the connection is made directly. I can further verify this by changing the proxy settings to...
System.setProperty("socksproxyHost", "should not resolve");
System.setProperty("socksproxyPort", "1080");
...and observing that the connection is still made and data is returned.
When doing the same thing with Microsoft SQL Server driver, I see the following error:
SQLServerException: The TCP/IP connection to the host , port 1433 has failed. Error: "Can't connect to SOCKS proxy:shouldnotresolve.
This is obviously specific to SQL Server, but I expected to see something similar for Oracle.
To enable socks proxy support in the oracle JDBC driver, you need to pass -Doracle.jdbc.javaNetNio=false to JVM or set the property accordingly in your code.
Does Oracle jdbc driver support SOCKS5 proxy? seems to suggest that the 18.1 release should have added SOCKS proxy support
I don't see how you're getting that, given that it said:
No the Oracle JDBC driver doesn't support SOCKS5 proxy. In the soon to be release 18.1 version of the thin driver there will be support for HTTPS proxy and websocket.
the latest documentation makes no mention of it
Sure it does, see the Oracle® Database, JDBC Developer's Guide, Release 18c.
Specifically section 8.2.2 Support for HTTPS Proxy Configuration.

Java PostgreSQL connection over VPN

I have a Java application that needs to connect to a remote PostgreSQL database over a VPN. Here is the relevant code:
Class.forName("org.postgresql.Driver");
Connection con = null;
con = DriverManager.getConnection("jdbc:postgresql://" + sqlHost + ":" + sqlPort + "/mydb", username, password);
This throws the error
org.postgresql.util.PSQLException: FATAL: pg_hba.conf rejects connection for host "172.16.7.5", user "xxxxx", database "xxxxx", SSL off
The Host IP address in sqlHost is actually 192.168.12.55, but if you notice the error message says that it is connecting to host 172.16.7.5 (which is the IP address assigned by the VPN).
I am able to connect to this PostgreSQL database using the exact same connection parameters on the exact same VPN using PGAdmin and using Python's psocopg2 module. Here is the equivalent Python code:
conn = psycopg2.connect("dbname=mydb user="+username+" password="+password+" host="+sqlHost+" port="+sqlPort)
Why in the world is only Java having problems with this? Since the connection works over PGAdmin and Python, I assume there is some setting in Java that I am using incorrectly, but I can't find anything.
EDIT: After reading into PostgreSQL docs a little more, I found that the issue with it listing the wrong hostname is not part of the issue but rather just the way PostgreSQL sees my computer over the VPN. Problem is still not solved, however.
Okay, I fixed this myself. The problem had nothing to do with the VPN but rather with the fact that Java by default does not try any sort of SSL connection by default whereas PGAdmin and psycopg2 do.
The solution was to add the following parameters to my connection url:
ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
I'm not sure if this might be the problem, but I've experienced similar problems in the past trying to connect to a database with VPN turned on.
Try running your application with this JVM argument passed at application launch time:
-Djava.net.preferIPv4Stack=true
See also this answer for a more permanent solution.

Can connect to remote database through MySQL Workbench, but not Java JBDC

I have a remote MySQL database that I can connect to with MySQL Workbench (screen shot below), but I ultimately need to connect to it via JBDC and everytime I try to connect, it throws an exception. I'm new to this, so could anyone provide me some insight on what could be wrong?
String host = "testdb.db.10682960.hostedresource.com";
String datab = "testdb";
String url = "jdbc:mysql://" + host + ":3306/" + datab;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, datab, "password");
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Edit: Not sure if this is relevant or not, but I'm running this on Android.
It looks like you're missing a user name. Try adding "?user=testdb" to the end of your url.
Edit: I didn't realize this was an Android app. Technically, it should be possible, but not advisable. Your network connection would be much less reliable, and you would be pushing server credentials out to your client. Use of a REST API to communicate from your Android to a web server in the same data center as the database is a much safer option.
I don't have experience trying this from Android... so I don't know what exception would be thrown if, for example, you can't reach port 3306 from the Android's connection to the network. Even if you get it working, though, do look into using a web service instead.
According to the jTDS FAQ (http://jtds.sourceforge.net/faq.html) the format of a JDBC URL is:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
If I were you, I would replace the '?' with a ';' to adhere to the above format. The "query string" is not like that of a "normal" URL.
This, of course, assumes that you are using the open source JDBC connector found via the link above.

Connecting to a MySQL Database with Java

I want to connect to my MySQL database with Java.
I am using JDBC and I have the driver installed. (com.mysql.jdbc.Driver)
The only problem is that I keep getting an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException The last
packet sent successfully to the server was 0 milliseconds ago.
Here is my code:
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:/mydomain.com/mydatabase", "username", "password");
I am not positive how to compose the URL (and where I get my username and password) but I have done A LOT of research.
I am the only person with acess to my database and domain, so there's no use asking the admin.
I use phpMyAdmin to create the database(s) and manage them. Do I use my phpMyAdmin username and password or what?
By the way, my site is hosted on Yahoo! Small Business.
So my questions are:
How do I make the connection URL?
What is my username and password?
I would say you are missing a forward slash on your URL.
Connection connection = DriverManager.getConnection("jdbc:mysql://mydomain.com/mydatabase", "username", "password");
Or I have a feeling that there is something wrong with your access privileges. This same thing happened to me also and it was a problem of Firewall blocking the port on the server. So verify this is not the case.
How do I make the connection URL?
Are you missing a forward slash in your URL? I would've assumed it would be something like:
jdbc:mysql://server/database
Load the drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
connect with the data base
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/widget_corp","dbuser","dbpassword");
.....jdbc:mysql://this is use as it is
.....127.0.0.1 this is the address of localhost You can use the word "localhost" insted of "127.0.0.1"
.....dbuser is the data base user
.....dbpassword is the password of the dbuser
.....3306 is the default port used for database

how to test proxy internet connection using java?

i have some code to test if the proxy server and port is working ,some of the code like this:
System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", "localhost");
System.getProperties().put("https.proxyPort", "1234");
System.getProperties().put("http.proxyHost", "localhost");
System.getProperties().put("http.proxyPort", "1234");
HttpURLConnection conn = (HttpURLConnection) new URL("https://www.google.com").openConnection();
conn.getContent();
conn.disconnect();
it seems that openConnection() method will do thing like this:
try to connect given URL using proxy.
if it fails to use proxy,it will connect URL directly without proxy.
that's the problem,i meant to test if the proxy is working,but this code won't stop if the proxy can not connect.
i also tried to use isReachable() method of InetAddress class,but i get the same result.
so how could i stop this connection if the proxy doesn't work ,in order to test if the proxy is reachable ?
System.getProperties().put("proxySet", "true");
That one doesn't do anything. It is an urban myth. It was part of the defunct 1997 HotJava bean and leaked from there into various books. It has never been part of any JDK. Try setting it to false in some situation where you need it on and see for yourself.
Sorry guys, I found out the way to do it.
I used java.net.Proxy class to open a connection via proxy.
It's easy to use and works fine. See Java Networking and Proxies

Categories