Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 680BC632ADA for ; Mon, 9 Aug 2010 08:47:48 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 33317-03 for ; Mon, 9 Aug 2010 11:47:40 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.cisx.com (mail.cisx.com [213.133.202.139]) by mail.postgresql.org (Postfix) with ESMTP id 965D56329DB for ; Mon, 9 Aug 2010 08:47:39 -0300 (ADT) Received: from DEVELOP1 ([192.168.100.50]) by mail.cisx.com (8.13.1/8.13.1) with SMTP id o79BlIm1022299 for ; Mon, 9 Aug 2010 12:47:32 +0100 Message-ID: <7BF0F1662EE14F6FA6F3C8EFE951A69F@DEVELOP1> From: "Donald Fraser" To: "[JDBC]" References: <4C5EB971.7000403@postnewspapers.com.au> Subject: Re: Java proxies connection to postgres Date: Mon, 9 Aug 2010 12:47:18 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_001F_01CB37C1.002B3AC0" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5843 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-10 required=5 tests=BAYES_00=-1.9, MIME_QP_LONG_LINE=0.001 X-Spam-Level: X-Archive-Number: 201008/28 X-Sequence-Number: 20831 This is a multi-part message in MIME format. ------=_NextPart_000_001F_01CB37C1.002B3AC0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit Please look at the attached source taken from a patched version of the 7.4 driver, which is a little out of date with respect to the latest drivers. However it gives you the idea of what you need to do in order to modify one of the latest drivers. The main problem with all HTTP style proxies is that you can only specify ports 80, 443 and sometimes 8080 and or 8000. I've spent a lot of time with proxies and to get a reliable connection you need to use SSL on port 443. This means you must redirect incomming requests, on the firewall that serves requests to your PostgreSQL server, from port 443 to port 5432. Which also means that you cannot have a secure web-server on the same IP address as your PostgreSQL server! Regards Donald Fraser ------=_NextPart_000_001F_01CB37C1.002B3AC0 Content-Type: application/octet-stream; name="PGStream.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="PGStream.java" /*-----------------------------------------------------------------------= --=0A= *=0A= * PGStream.java=0A= * This class is used by Connection for communicating with the=0A= * backend.=0A= *=0A= * Copyright (c) 2003, PostgreSQL Global Development Group=0A= *=0A= * IDENTIFICATION=0A= * $Header: = /cvsroot/cisx/src/PostgreSQL/jdbc/org/postgresql/core/PGStream.java,v = 1.1 2004/06/30 16:46:39 DonaldF Exp $=0A= *=0A= = *------------------------------------------------------------------------= -=0A= */=0A= package org.postgresql.core;=0A= =0A= import org.postgresql.util.PSQLException;=0A= import org.postgresql.util.PSQLState;=0A= import java.io.BufferedInputStream;=0A= import java.io.BufferedOutputStream;=0A= import java.io.InputStream;=0A= import java.io.IOException;=0A= import java.io.OutputStream;=0A= import java.io.UnsupportedEncodingException;=0A= import java.net.Socket;=0A= import java.sql.*;=0A= =0A= =0A= public class PGStream=0A= {=0A= public String host;=0A= public int port;=0A= public Socket connection;=0A= public InputStream pg_input;=0A= public BufferedOutputStream pg_output;=0A= private byte[] byte_buf;=0A= =0A= /*=0A= * Constructor: Connect to the PostgreSQL back end and return=0A= * a stream connection.=0A= *=0A= * @param host the hostname to connect to=0A= * @param port the port number that the postmaster is sitting on=0A= * @exception IOException if an IOException occurs below it.=0A= */=0A= public PGStream(String p_host, int p_port, boolean bUseHTTPTunnel) = throws IOException=0A= {=0A= host =3D p_host;=0A= port =3D p_port;=0A= =0A= if (bUseHTTPTunnel)=0A= {=0A= String tunnelHost =3D System.getProperty("https.proxyHost");=0A= Integer tunnelPort =3D Integer.getInteger("https.proxyPort");=0A= String tunnelUserName =3D System.getProperty("https.proxyUser");=0A= String tunnelPassword =3D System.getProperty("https.proxyPassword");=0A= if (tunnelPort =3D=3D null)=0A= throw new IOException("No valid HTTPS proxy port (https.proxyPort) = specified for proxy tunnel connection");=0A= try=0A= {=0A= connection =3D new Socket(tunnelHost, tunnelPort.intValue());=0A= }=0A= catch (java.net.ConnectException cex)=0A= {=0A= throw new IOException("Error connecting to HTTPS proxy server: "+ = tunnelHost + ", port: " + tunnelPort + ". " + cex.getMessage());=0A= }=0A= // idealy we should connect to port 443 to ensure that the HTTPS=0A= // server allows us out. Many HTTPS server only allow connections=0A= // to ports: 443, 80 and 21=0A= doTunnelHandshake(tunnelHost, tunnelPort.intValue(), tunnelUserName, = tunnelPassword);=0A= }=0A= else=0A= {=0A= connection =3D new Socket(host, port);=0A= }=0A= =0A= // Submitted by Jason Venner adds a 10x speed=0A= // improvement on FreeBSD machines (caused by a bug in their TCP Stack)=0A= connection.setTcpNoDelay(true);=0A= =0A= // Buffer sizes submitted by Sverre H Huseby =0A= pg_input =3D new BufferedInputStream(connection.getInputStream(), = 8192);=0A= pg_output =3D new BufferedOutputStream(connection.getOutputStream(), = 8192);=0A= byte_buf =3D new byte[8*1024];=0A= }=0A= =0A= /*=0A= * Tell our tunnel where we want to CONNECT, and look for the=0A= * right reply. Throw IOException if anything goes wrong.=0A= */=0A= private void doTunnelHandshake(String tunnelHost, int tunnelPort, = String tunnelUserName, String tunnelPassword)=0A= throws IOException=0A= {=0A= OutputStream out =3D connection.getOutputStream();=0A= // we should use port 443 as most HTTP proxy servers will not allow=0A= // connections to ports other than 21, 80 or 443.=0A= String msg =3D "CONNECT " + host + ":" + port + " HTTP/1.0\n"=0A= + "User-Agent: "=0A= + sun.net.www.protocol.http.HttpURLConnection.userAgent;=0A= =0A= if (tunnelUserName !=3D null && tunnelPassword !=3D null && = tunnelUserName.length() > 0 && tunnelPassword.length() > 0)=0A= {=0A= //add basic authentication header for the proxy=0A= sun.misc.BASE64Encoder enc =3D new sun.misc.BASE64Encoder();=0A= String encodedPassword =3D enc.encode((tunnelUserName + ":" = + tunnelPassword).getBytes());=0A= msg =3D msg + "\nProxy-Authorization: Basic " + = encodedPassword;=0A= }=0A= msg =3D msg + "\nContent-Length: 0";=0A= msg =3D msg + "\nPragma: no-cache";=0A= msg =3D msg + "\r\n\r\n";=0A= byte b[];=0A= try=0A= {=0A= /*=0A= * We really do want ASCII7 -- the http protocol doesn't change=0A= * with locale.=0A= */=0A= b =3D msg.getBytes("ASCII7");=0A= } catch (UnsupportedEncodingException ignored)=0A= {=0A= /*=0A= * If ASCII7 isn't there, something serious is wrong, but=0A= * Paranoia Is Good (tm)=0A= */=0A= b =3D msg.getBytes();=0A= }=0A= out.write(b);=0A= out.flush();=0A= =0A= /*=0A= * We need to store the reply so we can create a detailed=0A= * error message to the user.=0A= */=0A= byte reply[] =3D new byte[200];=0A= int replyLen =3D 0;=0A= int newlinesSeen =3D 0;=0A= boolean headerDone =3D false; /* Done on first newline */=0A= =0A= InputStream in =3D connection.getInputStream();=0A= boolean error =3D false;=0A= =0A= while (newlinesSeen < 2)=0A= {=0A= int i =3D in.read();=0A= if (i < 0)=0A= {=0A= throw new IOException("Unexpected EOF from proxy");=0A= }=0A= if (i =3D=3D '\n')=0A= {=0A= headerDone =3D true;=0A= ++newlinesSeen;=0A= } else if (i !=3D '\r')=0A= {=0A= newlinesSeen =3D 0;=0A= if (!headerDone && replyLen < reply.length)=0A= {=0A= reply[replyLen++] =3D (byte) i;=0A= }=0A= }=0A= }=0A= =0A= /*=0A= * Converting the byte array to a string is slightly wasteful=0A= * in the case where the connection was successful, but it's=0A= * insignificant compared to the network overhead.=0A= */=0A= String replyStr;=0A= try=0A= {=0A= replyStr =3D new String(reply, 0, replyLen, "ASCII7");=0A= } catch (UnsupportedEncodingException ignored)=0A= {=0A= replyStr =3D new String(reply, 0, replyLen);=0A= }=0A= =0A= /* We asked for HTTP/1.0, so we should get that back */=0A= // if (!replyStr.startsWith("HTTP/1.0 200"))=0A= if(replyStr.toLowerCase().indexOf("200 connection established") =3D=3D = -1)=0A= {=0A= throw new IOException("Unable to tunnel through "=0A= + tunnelHost + ":" + tunnelPort=0A= + ". Proxy returns \"" + replyStr + "\"");=0A= }=0A= =0A= /* tunneling Handshake was successful! */=0A= }=0A= =0A= /*=0A= * Sends a single character to the back end=0A= *=0A= * @param val the character to be sent=0A= * @exception IOException if an I/O error occurs=0A= */=0A= public void SendChar(int val) throws IOException=0A= {=0A= pg_output.write((byte)val);=0A= }=0A= =0A= /*=0A= * Sends an integer to the back end=0A= *=0A= * @param val the integer to be sent=0A= * @param siz the length of the integer in bytes (size of structure)=0A= * @exception IOException if an I/O error occurs=0A= */=0A= public void SendInteger(int val, int siz) throws IOException=0A= {=0A= byte[] buf =3D new byte[siz];=0A= =0A= while (siz-- > 0)=0A= {=0A= buf[siz] =3D (byte)(val & 0xff);=0A= val >>=3D 8;=0A= }=0A= Send(buf);=0A= }=0A= =0A= /*=0A= * Sends an integer to the back end=0A= *=0A= * @param val the integer to be sent=0A= * @param siz the length of the integer in bytes (size of structure)=0A= * @exception IOException if an I/O error occurs=0A= */=0A= public void SendIntegerR(int val, int siz) throws IOException=0A= {=0A= byte[] buf =3D new byte[siz];=0A= =0A= for (int i =3D 0; i < siz; i++)=0A= {=0A= buf[i] =3D (byte)(val & 0xff);=0A= val >>=3D 8;=0A= }=0A= Send(buf);=0A= }=0A= =0A= /*=0A= * Send an array of bytes to the backend=0A= *=0A= * @param buf The array of bytes to be sent=0A= * @exception IOException if an I/O error occurs=0A= */=0A= public void Send(byte buf[]) throws IOException=0A= {=0A= pg_output.write(buf);=0A= }=0A= =0A= /*=0A= * Send an exact array of bytes to the backend - if the length=0A= * has not been reached, send nulls until it has.=0A= *=0A= * @param buf the array of bytes to be sent=0A= * @param siz the number of bytes to be sent=0A= * @exception IOException if an I/O error occurs=0A= */=0A= public void Send(byte buf[], int siz) throws IOException=0A= {=0A= Send(buf, 0, siz);=0A= }=0A= =0A= /*=0A= * Send an exact array of bytes to the backend - if the length=0A= * has not been reached, send nulls until it has.=0A= *=0A= * @param buf the array of bytes to be sent=0A= * @param off offset in the array to start sending from=0A= * @param siz the number of bytes to be sent=0A= * @exception IOException if an I/O error occurs=0A= */=0A= public void Send(byte buf[], int off, int siz) throws IOException=0A= {=0A= int i;=0A= =0A= pg_output.write(buf, off, ((buf.length - off) < siz ? (buf.length - = off) : siz));=0A= if ((buf.length - off) < siz)=0A= {=0A= for (i =3D buf.length - off ; i < siz ; ++i)=0A= {=0A= pg_output.write(0);=0A= }=0A= }=0A= }=0A= =0A= /*=0A= * Receives a single character from the backend=0A= *=0A= * @return the character received=0A= * @exception SQLException if an I/O Error returns=0A= */=0A= public int ReceiveChar() throws SQLException=0A= {=0A= int c =3D 0;=0A= =0A= try=0A= {=0A= c =3D pg_input.read();=0A= if (c < 0)=0A= throw new PSQLException("postgresql.stream.eof", = PSQLState.COMMUNICATION_ERROR);=0A= }=0A= catch (IOException e)=0A= {=0A= throw new PSQLException("postgresql.stream.ioerror", = PSQLState.COMMUNICATION_ERROR, e);=0A= }=0A= return c;=0A= }=0A= =0A= /*=0A= * Receives an integer from the backend=0A= *=0A= * @param siz length of the integer in bytes=0A= * @return the integer received from the backend=0A= * @exception SQLException if an I/O error occurs=0A= */=0A= public int ReceiveInteger(int siz) throws SQLException=0A= {=0A= int n =3D 0;=0A= =0A= try=0A= {=0A= for (int i =3D 0 ; i < siz ; i++)=0A= {=0A= int b =3D pg_input.read();=0A= =0A= if (b < 0)=0A= throw new PSQLException("postgresql.stream.eof", = PSQLState.COMMUNICATION_ERROR);=0A= n =3D n | (b << (8 * i)) ;=0A= }=0A= }=0A= catch (IOException e)=0A= {=0A= throw new PSQLException("postgresql.stream.ioerror", = PSQLState.COMMUNICATION_ERROR, e);=0A= }=0A= return n;=0A= }=0A= =0A= /*=0A= * Receives an integer from the backend=0A= *=0A= * @param siz length of the integer in bytes=0A= * @return the integer received from the backend=0A= * @exception SQLException if an I/O error occurs=0A= */=0A= public int ReceiveIntegerR(int siz) throws SQLException=0A= {=0A= int n =3D 0;=0A= =0A= try=0A= {=0A= for (int i =3D 0 ; i < siz ; i++)=0A= {=0A= int b =3D pg_input.read();=0A= =0A= if (b < 0)=0A= throw new PSQLException("postgresql.stream.eof", = PSQLState.COMMUNICATION_ERROR);=0A= n =3D b | (n << 8);=0A= }=0A= }=0A= catch (IOException e)=0A= {=0A= throw new PSQLException("postgresql.stream.ioerror", = PSQLState.COMMUNICATION_ERROR, e);=0A= }=0A= return n;=0A= }=0A= =0A= /*=0A= * Receives a null-terminated string from the backend. If we don't see a=0A= * null, then we assume something has gone wrong.=0A= *=0A= * @param encoding the charset encoding to use.=0A= * @return string from back end=0A= * @exception SQLException if an I/O error occurs, or end of file=0A= */=0A= public String ReceiveString(Encoding encoding)=0A= throws SQLException=0A= {=0A= int s =3D 0;=0A= byte[] rst =3D byte_buf;=0A= try=0A= {=0A= int buflen =3D rst.length;=0A= boolean done =3D false;=0A= while (!done)=0A= {=0A= while (s < buflen)=0A= {=0A= int c =3D pg_input.read();=0A= if (c < 0)=0A= throw new PSQLException("postgresql.stream.eof", = PSQLState.COMMUNICATION_ERROR);=0A= else if (c =3D=3D 0)=0A= {=0A= rst[s] =3D 0;=0A= done =3D true;=0A= break;=0A= }=0A= else=0A= {=0A= rst[s++] =3D (byte)c;=0A= }=0A= if (s >=3D buflen)=0A= { // Grow the buffer=0A= buflen =3D (int)(buflen * 2); // 100% bigger=0A= byte[] newrst =3D new byte[buflen];=0A= System.arraycopy(rst, 0, newrst, 0, s);=0A= rst =3D newrst;=0A= }=0A= }=0A= }=0A= }=0A= catch (IOException e)=0A= {=0A= throw new PSQLException("postgresql.stream.ioerror", = PSQLState.COMMUNICATION_ERROR, e);=0A= }=0A= return encoding.decode(rst, 0, s);=0A= }=0A= =0A= /*=0A= * Read a tuple from the back end. A tuple is a two dimensional=0A= * array of bytes=0A= *=0A= * @param nf the number of fields expected=0A= * @return null if the current response has no more tuples, otherwise=0A= * an array of strings=0A= * @exception SQLException if a data I/O error occurs=0A= */=0A= public byte[][] ReceiveTupleV3(int nf) throws SQLException=0A= {=0A= //TODO: use l_msgSize=0A= int l_msgSize =3D ReceiveIntegerR(4);=0A= int i;=0A= int l_nf =3D ReceiveIntegerR(2);=0A= byte[][] answer =3D new byte[l_nf][0];=0A= =0A= for (i =3D 0 ; i < l_nf ; ++i)=0A= {=0A= int l_size =3D ReceiveIntegerR(4);=0A= boolean isNull =3D l_size =3D=3D -1;=0A= if (isNull)=0A= answer[i] =3D null;=0A= else=0A= {=0A= answer[i] =3D Receive(l_size);=0A= }=0A= }=0A= return answer;=0A= }=0A= =0A= /*=0A= * Read a tuple from the back end. A tuple is a two dimensional=0A= * array of bytes=0A= *=0A= * @param nf the number of fields expected=0A= * @param bin true if the tuple is a binary tuple=0A= * @return null if the current response has no more tuples, otherwise=0A= * an array of strings=0A= * @exception SQLException if a data I/O error occurs=0A= */=0A= public byte[][] ReceiveTupleV2(int nf, boolean bin) throws SQLException=0A= {=0A= int i, bim =3D (nf + 7) / 8;=0A= byte[] bitmask =3D Receive(bim);=0A= byte[][] answer =3D new byte[nf][0];=0A= =0A= int whichbit =3D 0x80;=0A= int whichbyte =3D 0;=0A= =0A= for (i =3D 0 ; i < nf ; ++i)=0A= {=0A= boolean isNull =3D ((bitmask[whichbyte] & whichbit) =3D=3D 0);=0A= whichbit >>=3D 1;=0A= if (whichbit =3D=3D 0)=0A= {=0A= ++whichbyte;=0A= whichbit =3D 0x80;=0A= }=0A= if (isNull)=0A= answer[i] =3D null;=0A= else=0A= {=0A= int len =3D ReceiveIntegerR(4);=0A= if (!bin)=0A= len -=3D 4;=0A= if (len < 0)=0A= len =3D 0;=0A= answer[i] =3D Receive(len);=0A= }=0A= }=0A= return answer;=0A= }=0A= =0A= /*=0A= * Reads in a given number of bytes from the backend=0A= *=0A= * @param siz number of bytes to read=0A= * @return array of bytes received=0A= * @exception SQLException if a data I/O error occurs=0A= */=0A= public byte[] Receive(int siz) throws SQLException=0A= {=0A= byte[] answer =3D new byte[siz];=0A= Receive(answer, 0, siz);=0A= return answer;=0A= }=0A= =0A= /*=0A= * Reads in a given number of bytes from the backend=0A= *=0A= * @param buf buffer to store result=0A= * @param off offset in buffer=0A= * @param siz number of bytes to read=0A= * @exception SQLException if a data I/O error occurs=0A= */=0A= public void Receive(byte[] b, int off, int siz) throws SQLException=0A= {=0A= int s =3D 0;=0A= =0A= try=0A= {=0A= while (s < siz)=0A= {=0A= int w =3D pg_input.read(b, off + s, siz - s);=0A= if (w < 0)=0A= throw new PSQLException("postgresql.stream.eof", = PSQLState.COMMUNICATION_ERROR);=0A= s +=3D w;=0A= }=0A= }=0A= catch (IOException e)=0A= {=0A= throw new PSQLException("postgresql.stream.ioerror", = PSQLState.COMMUNICATION_ERROR, e);=0A= }=0A= }=0A= =0A= /*=0A= * This flushes any pending output to the backend. It is used primarily=0A= * by the Fastpath code.=0A= * @exception SQLException if an I/O error occurs=0A= */=0A= public void flush() throws SQLException=0A= {=0A= try=0A= {=0A= pg_output.flush();=0A= }=0A= catch (IOException e)=0A= {=0A= throw new PSQLException("postgresql.stream.flush", = PSQLState.COMMUNICATION_ERROR, e);=0A= }=0A= }=0A= =0A= /*=0A= * Closes the connection=0A= *=0A= * @exception IOException if a IO Error occurs=0A= */=0A= public void close() throws IOException=0A= {=0A= pg_output.close();=0A= pg_input.close();=0A= connection.close();=0A= }=0A= =0A= /**=0A= * Marks a position in the buffer so that it can be restored later.=0A= * Required by Notification listener thread.=0A= */=0A= public void Mark(int limit)=0A= {=0A= pg_input.mark(limit);=0A= }=0A= =0A= /**=0A= * Resets a previously marked position in the buffer.=0A= * Required by Notification listener thread.=0A= */=0A= public void Reset()=0A= throws IOException=0A= {=0A= pg_input.reset();=0A= }=0A= =0A= }=0A= ------=_NextPart_000_001F_01CB37C1.002B3AC0--