users@grizzly.java.net

Re: Make an http proxy with grizzly

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Tue, 21 Aug 2012 14:15:19 +0200

Hi,

if you can give me working project (maven2 based?) sources w/ all the
dependencies and proper proxy addresses, so I can run it as it is?
 From your sources I see that you don't send any data to proxy, in the
onDataAvailable method you read data and them it back to the sender.

WBR,
Alexey.

On 08/21/2012 12:16 PM, yann Blazart wrote:
> Well I made quickly a crapy code but it doesn't work, the NIO read 112
> byte from input stream, but no more
> To remember, I lust play with the socket gived by HttpProxyclient (du
> to NTLM auth)
> The logs are after the following code
>
>
>
>
> HttpHost target = new HttpHost(host, Integer.parseInt(port));
> Socket socket;
> if (proxyHost != null) {
> ProxyClient proxyClient = new ProxyClient();
> if (credentials instanceof NTCredentials) {
> proxyClient.getAuthSchemeRegistry().register("ntlm", new
> NTLMSchemeFactory());
> }
> System.out.println("Proxy tunnel");
> socket = proxyClient.tunnel(proxyHost, target,
> credentials);
> } else {
> socket = new Socket(target.getHostName(),
> target.getPort());
> }
> final Socket secSocket = socket;
>
> final NIOInputStream in = request.createInputStream();
> final OutputStream out = response.getOutputStream();
> out.write(("HTTP/1.1 200 Connection etablished\r\n" +
> CONNECT_OK_HEADERS).getBytes());
> out.flush();
> System.out.println("200 sended ");
>
>
> final WritableByteChannel outChannel =
> Channels.newChannel(out);
> final Set ended = new HashSet();
> in.notifyAvailable(new ReadHandler() {
> @Override
> public void onDataAvailable() throws Exception {
> System.out.println("DATA IN !!!!!!!");
> final Buffer buffer = in.readBuffer();
> final ByteBuffer byteBuffer =
> buffer.toByteBuffer();
> try {
> while (byteBuffer.hasRemaining()) {
> System.out.println("DATA IN !!!!!!!
> ---> " + byteBuffer.remaining());
> outChannel.write(byteBuffer);
> }
> } finally {
> buffer.tryDispose();
> }
> in.notifyAvailable(this);
> }
> @Override
> public void onError(Throwable thrwbl) {
> System.out.println("ERRRRRRORRR");
> response.setStatus(500, thrwbl.getMessage());
> ended.add(new Object());
> }
> @Override
> public void onAllDataRead() throws Exception {
> System.out.println("DATAENDDDDDD");
> final Buffer buffer = in.readBuffer();
> final ByteBuffer byteBuffer =
> buffer.toByteBuffer();
> try {
> while (byteBuffer.hasRemaining()) {
> out.write(byteBuffer.array());
> }
> } finally {
> buffer.tryDispose();
> }
> response.setStatus(HttpStatus.ACCEPTED_202);
> ended.add(new Object());
> }
> });
> Thread tread = new Thread(new Runnable() {
> @Override
> public void run() {
> try {
> System.out.println("read from socket");
> byte[] buffer = new byte[400];
> int readed = 0;
> while ((readed =
> secSocket.getInputStream().read(buffer)) > -1) {
> System.out.println("readed = " + readed);
> out.write(buffer, 0, readed);
> }
> System.out.println(" --> stop copy from
> socket to out : " + readed);
> } catch (IOException ex) {
> throw new RuntimeException(ex);
> }
> }
> });
> Thread twrite = new Thread(new Runnable() {
> @Override
> public void run() {
> try {
> while (ended.size() < 0) {
> Thread.sleep(100);
> }
> } catch (Exception ex) {
> throw new RuntimeException(ex);
> }
> }
> });
>
> tread.start();
> twrite.start();
> tread.join();
> twrite.join();
> IOUtils.closeQuietly(secSocket);
> return null;
> }
> throw new IllegalArgumentException("Unknown method : " +
> methodString);
> }
> }
>
>
>
> request = CONNECT,mail.google.com:443 <http://mail.google.com:443>
> - header host = mail.google.com <http://mail.google.com>
> - header proxy-connection = Keep-Alive
> - header user-agent = Apache-HttpClient/UNAVAILABLE (java 1.5)
> *Proxy tunnel
> 200 sended
> read from socket*
> Allow unsafe renegotiation: false
> Allow legacy hello messages: true
> Is initial handshake: true
> Is secure renegotiation: false
> %% No cached client session
> *** ClientHello, TLSv1
> RandomCookie: GMT: 1345543579 bytes = { 102, 194, 84, 234, 92,
> 156, 104, 221, 109, 82, 112, 194, 168, 78, 76, 125, 144, 46, 148,
> 91, 176, 66, 86, 40, 137, 92, 185, 11 }
> Session ID: {}
> Cipher Suites: [SSL_RSA_WITH_RC4_128_MD5,
> SSL_RSA_WITH_RC4_128_SHA, TLS_RSA_WITH_AES_128_CBC_SHA,
> TLS_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
> TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
> TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
> TLS_DHE_DSS_WITH_AES_256_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA,
> SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
> SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_DES_CBC_SHA,
> SSL_DHE_RSA_WITH_DES_CBC_SHA, SSL_DHE_DSS_WITH_DES_CBC_SHA,
> SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL_RSA_EXPORT_WITH_DES40_CBC_SHA,
> SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA,
> SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA,
> TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
> Compression Methods: { 0 }
> ***
> main, WRITE: TLSv1 Handshake, length = 81
> main, WRITE: SSLv2 client hello message, length = 110
> *DATA IN !!!!!!!
> DATA IN !!!!!!! ---> 112
> --> stop copy from socket to out : -1*
> *ERRRRRRORRR*
> main, handling exception: javax.net.ssl.SSLException: Unrecognized
> SSL message, plaintext connection?
> main, SEND TLSv1 ALERT: fatal, description = unexpected_message
> main, WRITE: TLSv1 Alert, length = 2
> main, called closeSocket()
> main, IOException in getSession(): javax.net.ssl.SSLException:
> Unrecognized SSL message, plaintext connection?
>
>