From: <pga...@vu...> - 2010-06-09 01:21:16
|
Revision: 24890 Author: pgardner Date: 2010-06-08 18:20:53 -0700 (Tue, 08 Jun 2010) Log Message: ----------- playing with udp socks Modified Paths: -------------- client/trunk/azureus2/src/com/aelitis/net/udp/uc/PRUDPPacketHandler.java client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerFactoryImpl.java client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerImpl.java client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TRTrackerBTAnnouncerImpl.java client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TrackerStatus.java Added Paths: ----------- client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerSocks.java Modified: client/trunk/azureus2/src/com/aelitis/net/udp/uc/PRUDPPacketHandler.java =================================================================== --- client/trunk/azureus2/src/com/aelitis/net/udp/uc/PRUDPPacketHandler.java 2010-06-09 01:20:27 UTC (rev 24889) +++ client/trunk/azureus2/src/com/aelitis/net/udp/uc/PRUDPPacketHandler.java 2010-06-09 01:20:53 UTC (rev 24890) @@ -138,4 +138,15 @@ public PRUDPPacketHandlerStats getStats(); + + public PRUDPPacketHandler + openSession( + InetSocketAddress target ) + + throws PRUDPPacketHandlerException; + + public void + closeSession() + + throws PRUDPPacketHandlerException; } Modified: client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerFactoryImpl.java =================================================================== --- client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerFactoryImpl.java 2010-06-09 01:20:27 UTC (rev 24889) +++ client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerFactoryImpl.java 2010-06-09 01:20:53 UTC (rev 24890) @@ -62,7 +62,7 @@ if ( receiver == null ){ - receiver = new PRUDPPacketHandlerImpl( port, bind_ip ); + receiver = new PRUDPPacketHandlerImpl( port, bind_ip, null ); receiver_map.put( f_port, receiver ); } @@ -99,7 +99,7 @@ if ( receiver == null ){ - receiver = new PRUDPPacketHandlerImpl( port, null ); + receiver = new PRUDPPacketHandlerImpl( port, null, null ); receiver_map.put( f_port, receiver ); } Modified: client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerImpl.java =================================================================== --- client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerImpl.java 2010-06-09 01:20:27 UTC (rev 24889) +++ client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerImpl.java 2010-06-09 01:20:53 UTC (rev 24890) @@ -32,6 +32,8 @@ import java.util.*; import org.bouncycastle.util.encoders.Base64; +import org.gudy.azureus2.core3.config.COConfigurationManager; +import org.gudy.azureus2.core3.config.ParameterListener; import org.gudy.azureus2.core3.logging.LogAlert; import org.gudy.azureus2.core3.logging.LogEvent; import org.gudy.azureus2.core3.logging.LogIDs; @@ -53,6 +55,28 @@ private static final long MAX_SEND_QUEUE_DATA_SIZE = 2*1024*1024; private static final long MAX_RECV_QUEUE_DATA_SIZE = 1*1024*1024; + private static boolean use_socks; + + static{ + COConfigurationManager.addAndFireParameterListeners( + new String[]{ + + }, + new ParameterListener() + { + public void + parameterChanged( + String parameter_name ) + { + boolean enable_proxy = COConfigurationManager.getBooleanParameter("Enable.Proxy"); + boolean enable_socks = COConfigurationManager.getBooleanParameter("Enable.SOCKS"); + + use_socks = enable_proxy && enable_socks; + } + }); + } + + private int port; private DatagramSocket socket; @@ -103,14 +127,17 @@ private PRUDPPacketHandlerImpl altProtocolDelegate; + private final PacketTransformer packet_transformer; protected PRUDPPacketHandlerImpl( - int _port, - InetAddress _bind_ip ) + int _port, + InetAddress _bind_ip, + PacketTransformer _packet_transformer ) { port = _port; explicit_bind_ip = _bind_ip; + packet_transformer = _packet_transformer; default_bind_ip = NetworkAdmin.getSingleton().getSingleHomedServiceBindAddress(); @@ -201,6 +228,11 @@ public int getPort() { + if ( port == 0 && socket != null ){ + + return( socket.getLocalPort()); + } + return( port ); } @@ -295,7 +327,7 @@ if(altAddress != null && altProtocolDelegate == null) { - altProtocolDelegate = new PRUDPPacketHandlerImpl(port,altAddress); + altProtocolDelegate = new PRUDPPacketHandlerImpl(port,altAddress,packet_transformer); altProtocolDelegate.stats = stats; altProtocolDelegate.primordial_handler = primordial_handler; altProtocolDelegate.request_handler = request_handler; @@ -441,7 +473,7 @@ DatagramPacket packet = new DatagramPacket( buffer, buffer.length, address ); - socket.receive( packet ); + receiveFromSocket( packet ); long receive_time = SystemTime.getCurrentTime(); @@ -1034,7 +1066,7 @@ // synchronous write holding lock to block senders - socket.send( dg_packet ); + sendToSocket( dg_packet ); stats.packetSent( buffer.length ); @@ -1136,7 +1168,7 @@ r.sent(); - socket.send( p ); + sendToSocket( p ); stats.packetSent( p.getLength() ); @@ -1180,8 +1212,9 @@ request.sent(); if (dg_packet == null) {throw new NullPointerException("dg_packet is null");} - socket.send( dg_packet ); + sendToSocket( dg_packet ); + // System.out.println( "sent:" + buffer.length ); stats.packetSent( buffer.length ); @@ -1289,7 +1322,7 @@ + request_packet.getString())); } - socket.send( dg_packet ); + sendToSocket( dg_packet ); stats.packetSent( buffer.length ); @@ -1418,7 +1451,7 @@ "PRUDPPacketHandler: reply packet sent: " + buffer.length + " to " + target )); } - socket.send( dg_packet ); + sendToSocket( dg_packet ); stats.primordialPacketSent( buffer.length ); @@ -1428,6 +1461,37 @@ } } + private void + sendToSocket( + DatagramPacket p ) + + throws IOException + { + if ( packet_transformer != null ){ + + packet_transformer.transformSend( p ); + + System.out.println( "sending to " + p.getAddress() + ":" +p.getPort()); + + } + + socket.send( p ); + } + + private void + receiveFromSocket( + DatagramPacket p ) + + throws IOException + { + socket.receive( p ); + + if ( packet_transformer != null ){ + + packet_transformer.transformReceive( p ); + } + } + public PRUDPPacketHandlerStats getStats() { @@ -1440,9 +1504,49 @@ destroyed = true; PRUDPPacketHandlerImpl delegate = altProtocolDelegate; - if(delegate != null) + + if (delegate != null ){ + delegate.destroy(); - + } + destroy_sem.reserve(); } + + + public PRUDPPacketHandler + openSession( + InetSocketAddress target ) + + throws PRUDPPacketHandlerException + { + if ( use_socks ){ + + return( new PRUDPPacketHandlerSocks( target )); + + }else{ + + return( this ); + } + } + + public void + closeSession() + + throws PRUDPPacketHandlerException + { + } + + protected interface + PacketTransformer + { + public void + transformSend( + DatagramPacket packet ); + + public void + transformReceive( + DatagramPacket packet ); + + } } Added: client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerSocks.java =================================================================== --- client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerSocks.java (rev 0) +++ client/trunk/azureus2/src/com/aelitis/net/udp/uc/impl/PRUDPPacketHandlerSocks.java 2010-06-09 01:20:53 UTC (rev 24890) @@ -0,0 +1,468 @@ +/* + * Created on Jun 8, 2010 + * Created by Paul Gardner + * + * Copyright 2010 Vuze, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + + +package com.aelitis.net.udp.uc.impl; + +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.PasswordAuthentication; +import java.net.Proxy; +import java.net.Socket; + +import org.gudy.azureus2.core3.config.COConfigurationManager; +import org.gudy.azureus2.core3.config.ParameterListener; +import org.gudy.azureus2.core3.util.Debug; +import org.gudy.azureus2.core3.util.HostNameToIPResolver; + +import com.aelitis.azureus.core.proxy.AEProxyFactory; +import com.aelitis.net.udp.uc.PRUDPPacket; +import com.aelitis.net.udp.uc.PRUDPPacketHandler; +import com.aelitis.net.udp.uc.PRUDPPacketHandlerException; +import com.aelitis.net.udp.uc.PRUDPPacketHandlerStats; +import com.aelitis.net.udp.uc.PRUDPPacketReceiver; +import com.aelitis.net.udp.uc.PRUDPPrimordialHandler; +import com.aelitis.net.udp.uc.PRUDPRequestHandler; + +public class +PRUDPPacketHandlerSocks + implements PRUDPPacketHandler, PRUDPPacketHandlerImpl.PacketTransformer +{ + private static String socks_host; + private static int socks_port; + private static String socks_user; + private static String socks_password; + + static{ + COConfigurationManager.addAndFireParameterListeners( + new String[]{ + + }, + new ParameterListener() + { + public void + parameterChanged( + String parameter_name ) + { + socks_host = COConfigurationManager.getStringParameter("Proxy.Host").trim(); + socks_port = Integer.parseInt(COConfigurationManager.getStringParameter("Proxy.Port").trim()); + socks_user = COConfigurationManager.getStringParameter("Proxy.Username").trim(); + socks_password = COConfigurationManager.getStringParameter("Proxy.Password").trim(); + + if ( socks_user.equalsIgnoreCase("<none>")){ + socks_user = ""; + } + + } + }); + } + + final private InetSocketAddress target; + + private Socket control_socket; + + private InetSocketAddress relay; + + private PRUDPPacketHandler delegate; + + private byte[] packet_out_header; + + protected + PRUDPPacketHandlerSocks( + InetSocketAddress _target ) + + throws PRUDPPacketHandlerException + { + target = _target; + + boolean ok = false; + + try{ + delegate = new PRUDPPacketHandlerImpl( 0, null, this ); + + control_socket = new Socket( Proxy.NO_PROXY ); + + control_socket.connect( new InetSocketAddress( socks_host, socks_port )); + + DataOutputStream dos = new DataOutputStream( control_socket.getOutputStream()); + DataInputStream dis = new DataInputStream( control_socket.getInputStream()); + + dos.writeByte( (byte)5 ); // socks 5 + dos.writeByte( (byte)2 ); // 2 methods + dos.writeByte( (byte)0 ); // no auth + dos.writeByte( (byte)2 ); // user/pw + + dos.flush(); + + dis.readByte(); // version byte + + byte method = dis.readByte(); + + if ( method != 0 && method != 2 ){ + + throw new IOException( "SOCKS 5: no valid method [" + method + "]" ); + } + + // auth + + if ( method == 2 ) { + + dos.writeByte( (byte)1 ); // user/pw version + dos.writeByte( (byte)socks_user.length() ); // user length + dos.write( socks_user.getBytes() ); + dos.writeByte( (byte)socks_password.length() ); // password length + dos.write( socks_password.getBytes() ); + + dos.flush(); + + dis.readByte(); // version byte + + byte status = dis.readByte(); + + if ( status != 0 ){ + + throw( new IOException( "SOCKS 5: authentication fails [status=" +status+ "]" )); + } + } + + String mapped_ip; + + if ( target.isUnresolved() || target.getAddress() == null ){ + + // deal with long "hostnames" that we get for, e.g., I2P destinations + + mapped_ip = AEProxyFactory.getAddressMapper().internalise( target.getHostName() ); + + }else{ + + mapped_ip = target.getAddress().getHostName(); + } + + dos.writeByte( (byte)5 ); // version + dos.writeByte( (byte)3 ); // udp associate + dos.writeByte( (byte)0 ); // reserved + + dos.writeByte((byte)1); + dos.write( new byte[4] ); + + dos.writeShort( (short)delegate.getPort()); // port + + dos.flush(); + + dis.readByte(); // ver + + byte reply = dis.readByte(); + + if ( reply != 0 ){ + + throw( new IOException( "SOCKS 5: udp association fails [reply=" +reply+ "]" )); + } + + dis.readByte(); // reserved + + InetAddress relay_address; + + byte atype = dis.readByte(); + + if ( atype == 1 ){ + + byte[] bytes = new byte[4]; + + dis.readFully( bytes ); + + relay_address = InetAddress.getByAddress( bytes ); + + }else if ( atype == 3 ){ + + byte len = dis.readByte(); + + byte[] bytes = new byte[(int)len&0xff ]; + + dis.readFully( bytes ); + + relay_address = InetAddress.getByName( new String( bytes )); + + }else{ + + byte[] bytes = new byte[16]; + + dis.readFully( bytes ); + + relay_address = InetAddress.getByAddress( bytes ); + + } + + int relay_port = (((dis.readByte()&0xff)<<8) | dis.readByte()) & 0x0000ffff; + + relay = new InetSocketAddress( relay_address, relay_port ); + + // use the maped ip for dns resolution so we don't leak the + // actual address if this is a secure one (e.g. I2P one) + + ByteArrayOutputStream baos_temp = new ByteArrayOutputStream(); + DataOutputStream dos_temp = new DataOutputStream( baos_temp ); + + dos_temp.writeByte(0); // resv + dos_temp.writeByte(0); // resv + dos_temp.writeByte(0); // frag (none) + + try { + byte[] ip_bytes = HostNameToIPResolver.syncResolve( mapped_ip ).getAddress(); + + dos_temp.writeByte( ip_bytes.length==4?(byte)1:(byte)4 ); + dos_temp.write( ip_bytes ); + + + }catch( Throwable e ){ + + dos_temp.writeByte( (byte)3 ); // address type = domain name + dos_temp.writeByte( (byte)mapped_ip.length() ); // address type = domain name + dos_temp.write( mapped_ip.getBytes() ); + + } + + dos_temp.writeShort( (short)target.getPort() ); // port + + dos_temp.flush(); + packet_out_header = baos_temp.toByteArray(); + + + ok = true; + + Thread.sleep(1000); + + }catch( Throwable e ){ + + throw( new PRUDPPacketHandlerException( "socks setup failed", e)); + + }finally{ + + if ( !ok ){ + + try{ + control_socket.close(); + + }catch( Throwable e ){ + + Debug.out( e ); + + }finally{ + + control_socket = null; + } + } + } + } + + public void + transformSend( + DatagramPacket packet ) + { + System.out.println( "xform out" ); + + byte[] data = packet.getData(); + int data_len = packet.getLength(); + + byte[] new_data = new byte[data_len+packet_out_header.length]; + + System.arraycopy( packet_out_header, 0, new_data, 0, packet_out_header.length ); + System.arraycopy( data, 0, new_data, packet_out_header.length, data_len); + + packet.setData( new_data ); + } + + public void + transformReceive( + DatagramPacket packet ) + { + System.out.println( "xform in" ); + } + + private void + checkAddress( + InetSocketAddress destination ) + + throws PRUDPPacketHandlerException + { + if ( !destination.equals( target )){ + + throw( new PRUDPPacketHandlerException( "Destination mismatch" )); + } + } + + public void + sendAndReceive( + PRUDPPacket request_packet, + InetSocketAddress destination_address, + PRUDPPacketReceiver receiver, + long timeout, + int priority ) + + throws PRUDPPacketHandlerException + { + checkAddress( destination_address ); + + delegate.sendAndReceive( request_packet, relay, receiver, timeout, priority ); + } + + public PRUDPPacket + sendAndReceive( + PasswordAuthentication auth, + PRUDPPacket request_packet, + InetSocketAddress destination_address ) + + throws PRUDPPacketHandlerException + { + checkAddress( destination_address ); + + return( delegate.sendAndReceive( auth, request_packet, relay)); + } + + public PRUDPPacket + sendAndReceive( + PasswordAuthentication auth, + PRUDPPacket request_packet, + InetSocketAddress destination_address, + long timeout_millis ) + + throws PRUDPPacketHandlerException + { + checkAddress( destination_address ); + + return( delegate.sendAndReceive(auth, request_packet, relay, timeout_millis )); + } + + public PRUDPPacket + sendAndReceive( + PasswordAuthentication auth, + PRUDPPacket request_packet, + InetSocketAddress destination_address, + long timeout_millis, + int priority ) + + throws PRUDPPacketHandlerException + { + checkAddress( destination_address ); + + return( delegate.sendAndReceive(auth, request_packet, relay, timeout_millis, priority )); + } + + public void + send( + PRUDPPacket request_packet, + InetSocketAddress destination_address ) + + throws PRUDPPacketHandlerException + { + checkAddress( destination_address ); + + delegate.send( request_packet, relay ); + } + + public PRUDPRequestHandler + getRequestHandler() + { + return( delegate.getRequestHandler()); + } + + public void + setRequestHandler( + PRUDPRequestHandler request_handler ) + { + delegate.setRequestHandler( request_handler ); + } + + public void + primordialSend( + byte[] data, + InetSocketAddress target ) + + throws PRUDPPacketHandlerException + { + throw( new PRUDPPacketHandlerException( "not imp" )); + } + + public void + setPrimordialHandler( + PRUDPPrimordialHandler handler ) + { + } + + public int + getPort() + { + return( delegate.getPort()); + } + + public void + setDelays( + int send_delay, + int receive_delay, + int queued_request_timeout ) + { + delegate.setDelays(send_delay, receive_delay, queued_request_timeout); + } + + public void + setExplicitBindAddress( + InetAddress address ) + { + delegate.setExplicitBindAddress( address ); + } + + public PRUDPPacketHandlerStats + getStats() + { + return( delegate.getStats()); + } + + public PRUDPPacketHandler + openSession( + InetSocketAddress target ) + + throws PRUDPPacketHandlerException + { + throw( new PRUDPPacketHandlerException( "not supported" )); + } + + public void + closeSession() + + throws PRUDPPacketHandlerException + { + if ( control_socket != null ){ + + try{ + control_socket.close(); + + control_socket = null; + + }catch( Throwable e ){ + + Debug.out( e ); + } + } + } +} Modified: client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TRTrackerBTAnnouncerImpl.java =================================================================== --- client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TRTrackerBTAnnouncerImpl.java 2010-06-09 01:20:27 UTC (rev 24889) +++ client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TRTrackerBTAnnouncerImpl.java 2010-06-09 01:20:53 UTC (rev 24890) @@ -1516,221 +1516,229 @@ PRUDPPacketHandler handler = PRUDPPacketHandlerFactory.getHandler( UDPNetworkManager.getSingleton().getUDPNonDataListeningPortNumber()); InetSocketAddress destination = new InetSocketAddress(reqUrl.getHost(),reqUrl.getPort()==-1?80:reqUrl.getPort()); + + handler = handler.openSession( destination ); - for (int retry_loop=0;retry_loop<PRUDPPacketTracker.DEFAULT_RETRY_COUNT;retry_loop++){ - - try{ - - PRUDPPacket connect_request = new PRUDPPacketRequestConnect(); - - PRUDPPacket reply = handler.sendAndReceive( auth, connect_request, destination, timeout ); - - if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_CONNECT ){ - - PRUDPPacketReplyConnect connect_reply = (PRUDPPacketReplyConnect)reply; - - long my_connection = connect_reply.getConnectionId(); - - PRUDPPacketRequest request; - - if ( PRUDPPacketTracker.VERSION == 1 ){ - - PRUDPPacketRequestAnnounce announce_request = new PRUDPPacketRequestAnnounce( my_connection ); - - request = announce_request; - - // bit of a hack this... + try{ + + for (int retry_loop=0;retry_loop<PRUDPPacketTracker.DEFAULT_RETRY_COUNT;retry_loop++){ + + try{ + + PRUDPPacket connect_request = new PRUDPPacketRequestConnect(); + + PRUDPPacket reply = handler.sendAndReceive( auth, connect_request, destination, timeout ); + + if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_CONNECT ){ + + PRUDPPacketReplyConnect connect_reply = (PRUDPPacketReplyConnect)reply; - String url_str = reqUrl.toString(); + long my_connection = connect_reply.getConnectionId(); + + PRUDPPacketRequest request; - int p_pos = url_str.indexOf("?"); - - url_str = url_str.substring(p_pos+1); - - String event_str = getURLParam( url_str, "event" ); - - int event = PRUDPPacketRequestAnnounce.EV_UPDATE; - - if ( event_str != null ){ + if ( PRUDPPacketTracker.VERSION == 1 ){ - if ( event_str.equals( "started" )){ - - event = PRUDPPacketRequestAnnounce.EV_STARTED; - - }else if ( event_str.equals( "stopped" )){ - - event = PRUDPPacketRequestAnnounce.EV_STOPPED; - - }else if ( event_str.equals( "completed" )){ - - event = PRUDPPacketRequestAnnounce.EV_COMPLETED; - } - } - - String ip_str = getURLParam( url_str, "ip" ); - - int ip = 0; - - if ( ip_str != null ){ + PRUDPPacketRequestAnnounce announce_request = new PRUDPPacketRequestAnnounce( my_connection ); + + request = announce_request; - ip = PRHelpers.addressToInt( ip_str); - } + // bit of a hack this... + + String url_str = reqUrl.toString(); + + int p_pos = url_str.indexOf("?"); + + url_str = url_str.substring(p_pos+1); + + String event_str = getURLParam( url_str, "event" ); + + int event = PRUDPPacketRequestAnnounce.EV_UPDATE; + + if ( event_str != null ){ + + if ( event_str.equals( "started" )){ + + event = PRUDPPacketRequestAnnounce.EV_STARTED; + + }else if ( event_str.equals( "stopped" )){ + + event = PRUDPPacketRequestAnnounce.EV_STOPPED; + + }else if ( event_str.equals( "completed" )){ + + event = PRUDPPacketRequestAnnounce.EV_COMPLETED; + } + } + + String ip_str = getURLParam( url_str, "ip" ); + + int ip = 0; + + if ( ip_str != null ){ + + ip = PRHelpers.addressToInt( ip_str); + } + + announce_request.setDetails( + torrent_hash.getBytes(), + tracker_peer_id, + getLongURLParam( url_str, "downloaded" ), + event, + ip, + (int)getLongURLParam( url_str, "numwant" ), + getLongURLParam( url_str, "left" ), + (short)getLongURLParam( url_str, "port" ), + getLongURLParam( url_str, "uploaded" )); - announce_request.setDetails( - torrent_hash.getBytes(), - tracker_peer_id, - getLongURLParam( url_str, "downloaded" ), - event, - ip, - (int)getLongURLParam( url_str, "numwant" ), - getLongURLParam( url_str, "left" ), - (short)getLongURLParam( url_str, "port" ), - getLongURLParam( url_str, "uploaded" )); - - }else{ - PRUDPPacketRequestAnnounce2 announce_request = new PRUDPPacketRequestAnnounce2( my_connection ); - - request = announce_request; - - // bit of a hack this... - - String url_str = reqUrl.toString(); - - int p_pos = url_str.indexOf("?"); - - url_str = url_str.substring(p_pos+1); - - String event_str = getURLParam( url_str, "event" ); - - int event = PRUDPPacketRequestAnnounce.EV_UPDATE; - - if ( event_str != null ){ + }else{ + PRUDPPacketRequestAnnounce2 announce_request = new PRUDPPacketRequestAnnounce2( my_connection ); - if ( event_str.equals( "started" )){ - - event = PRUDPPacketRequestAnnounce.EV_STARTED; - - }else if ( event_str.equals( "stopped" )){ - - event = PRUDPPacketRequestAnnounce.EV_STOPPED; - - }else if ( event_str.equals( "completed" )){ - - event = PRUDPPacketRequestAnnounce.EV_COMPLETED; - } - } - - String ip_str = getURLParam( url_str, "ip" ); - - int ip = 0; - - if ( ip_str != null ){ + request = announce_request; - ip = PRHelpers.addressToInt( ip_str); + // bit of a hack this... + + String url_str = reqUrl.toString(); + + int p_pos = url_str.indexOf("?"); + + url_str = url_str.substring(p_pos+1); + + String event_str = getURLParam( url_str, "event" ); + + int event = PRUDPPacketRequestAnnounce.EV_UPDATE; + + if ( event_str != null ){ + + if ( event_str.equals( "started" )){ + + event = PRUDPPacketRequestAnnounce.EV_STARTED; + + }else if ( event_str.equals( "stopped" )){ + + event = PRUDPPacketRequestAnnounce.EV_STOPPED; + + }else if ( event_str.equals( "completed" )){ + + event = PRUDPPacketRequestAnnounce.EV_COMPLETED; + } + } + + String ip_str = getURLParam( url_str, "ip" ); + + int ip = 0; + + if ( ip_str != null ){ + + ip = PRHelpers.addressToInt( ip_str); + } + + announce_request.setDetails( + torrent_hash.getBytes(), + tracker_peer_id, + getLongURLParam( url_str, "downloaded" ), + event, + ip, + helper.getUDPKey(), + (int)getLongURLParam( url_str, "numwant" ), + getLongURLParam( url_str, "left" ), + (short)getLongURLParam( url_str, "port" ), + getLongURLParam( url_str, "uploaded" )); } - announce_request.setDetails( - torrent_hash.getBytes(), - tracker_peer_id, - getLongURLParam( url_str, "downloaded" ), - event, - ip, - helper.getUDPKey(), - (int)getLongURLParam( url_str, "numwant" ), - getLongURLParam( url_str, "left" ), - (short)getLongURLParam( url_str, "port" ), - getLongURLParam( url_str, "uploaded" )); - } - - reply = handler.sendAndReceive( auth, request, destination ); - - if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_ANNOUNCE ){ - - if ( auth != null ){ - - SESecurityManager.setPasswordAuthenticationOutcome( UDP_REALM, reqUrl, true ); - } - - if ( PRUDPPacketTracker.VERSION == 1 ){ - PRUDPPacketReplyAnnounce announce_reply = (PRUDPPacketReplyAnnounce)reply; + reply = handler.sendAndReceive( auth, request, destination ); + + if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_ANNOUNCE ){ - Map map = new HashMap(); - - map.put( "interval", new Long( announce_reply.getInterval())); - - int[] addresses = announce_reply.getAddresses(); - short[] ports = announce_reply.getPorts(); - - List peers = new ArrayList(); - - map.put( "peers", peers ); - - for (int i=0;i<addresses.length;i++){ + if ( auth != null ){ - Map peer = new HashMap(); - - peers.add( peer ); - - peer.put( "ip", PRHelpers.intToAddress(addresses[i]).getBytes()); - peer.put( "port", new Long( ports[i])); + SESecurityManager.setPasswordAuthenticationOutcome( UDP_REALM, reqUrl, true ); } - byte[] data = BEncoder.encode( map ); - - message.write( data ); - - return( null ); - - } - - PRUDPPacketReplyAnnounce2 announce_reply = (PRUDPPacketReplyAnnounce2)reply; - - Map map = new HashMap(); - - map.put( "interval", new Long( announce_reply.getInterval())); - - int[] addresses = announce_reply.getAddresses(); - short[] ports = announce_reply.getPorts(); - - map.put( "complete", new Long(announce_reply.getSeeders())); - map.put( "incomplete", new Long(announce_reply.getLeechers())); - - List peers = new ArrayList(); - - map.put( "peers", peers ); - - for (int i=0;i<addresses.length;i++){ - - Map peer = new HashMap(); - - peers.add( peer ); - - peer.put( "ip", PRHelpers.intToAddress(addresses[i]).getBytes()); - peer.put( "port", new Long( ports[i])); + if ( PRUDPPacketTracker.VERSION == 1 ){ + PRUDPPacketReplyAnnounce announce_reply = (PRUDPPacketReplyAnnounce)reply; + + Map map = new HashMap(); + + map.put( "interval", new Long( announce_reply.getInterval())); + + int[] addresses = announce_reply.getAddresses(); + short[] ports = announce_reply.getPorts(); + + List peers = new ArrayList(); + + map.put( "peers", peers ); + + for (int i=0;i<addresses.length;i++){ + + Map peer = new HashMap(); + + peers.add( peer ); + + peer.put( "ip", PRHelpers.intToAddress(addresses[i]).getBytes()); + peer.put( "port", new Long( ports[i])); + } + + byte[] data = BEncoder.encode( map ); + + message.write( data ); + + return( null ); + } - byte[] data = BEncoder.encode( map ); + PRUDPPacketReplyAnnounce2 announce_reply = (PRUDPPacketReplyAnnounce2)reply; + + Map map = new HashMap(); + + map.put( "interval", new Long( announce_reply.getInterval())); + + int[] addresses = announce_reply.getAddresses(); + short[] ports = announce_reply.getPorts(); + + map.put( "complete", new Long(announce_reply.getSeeders())); + map.put( "incomplete", new Long(announce_reply.getLeechers())); + + List peers = new ArrayList(); + + map.put( "peers", peers ); + + for (int i=0;i<addresses.length;i++){ + + Map peer = new HashMap(); + + peers.add( peer ); + + peer.put( "ip", PRHelpers.intToAddress(addresses[i]).getBytes()); + peer.put( "port", new Long( ports[i])); + } + + byte[] data = BEncoder.encode( map ); + + message.write( data ); + + return( null ); - message.write( data ); - - return( null ); - - } - - failure_reason = ((PRUDPPacketReplyError)reply).getMessage(); - - }else{ - - failure_reason = ((PRUDPPacketReplyError)reply).getMessage(); - } - }catch( PRUDPPacketHandlerException e ){ - - if ( e.getMessage() == null || e.getMessage().indexOf("timed out") == -1 ){ - - throw( e ); - } - } - } + } + + failure_reason = ((PRUDPPacketReplyError)reply).getMessage(); + + }else{ + + failure_reason = ((PRUDPPacketReplyError)reply).getMessage(); + } + }catch( PRUDPPacketHandlerException e ){ + + if ( e.getMessage() == null || e.getMessage().indexOf("timed out") == -1 ){ + + throw( e ); + } + } + } + }finally{ + + handler.closeSession(); + } }catch( Throwable e ){ Modified: client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TrackerStatus.java =================================================================== --- client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TrackerStatus.java 2010-06-09 01:20:27 UTC (rev 24889) +++ client/trunk/azureus2/src/org/gudy/azureus2/core3/tracker/client/impl/bt/TrackerStatus.java 2010-06-09 01:20:53 UTC (rev 24890) @@ -1200,145 +1200,152 @@ PRUDPPacketHandler handler = PRUDPPacketHandlerFactory.getHandler( port ); InetSocketAddress destination = new InetSocketAddress(reqUrl.getHost(),reqUrl.getPort()==-1?80:reqUrl.getPort()); + + handler = handler.openSession( destination ); + + try{ + String failure_reason = null; + + for (int retry_loop=0;retry_loop<PRUDPPacketTracker.DEFAULT_RETRY_COUNT;retry_loop++){ + + try{ + PRUDPPacket connect_request = new PRUDPPacketRequestConnect(); + + PRUDPPacket reply = handler.sendAndReceive( auth, connect_request, destination ); + + if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_CONNECT ){ + + PRUDPPacketReplyConnect connect_reply = (PRUDPPacketReplyConnect)reply; + + long my_connection = connect_reply.getConnectionId(); + + PRUDPPacketRequestScrape scrape_request = new PRUDPPacketRequestScrape( my_connection, hashes ); + + reply = handler.sendAndReceive( auth, scrape_request, destination ); + + if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_SCRAPE ){ - String failure_reason = null; + auth_ok = true; - for (int retry_loop=0;retry_loop<PRUDPPacketTracker.DEFAULT_RETRY_COUNT;retry_loop++){ - - try{ - PRUDPPacket connect_request = new PRUDPPacketRequestConnect(); - - PRUDPPacket reply = handler.sendAndReceive( auth, connect_request, destination ); - - if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_CONNECT ){ - - PRUDPPacketReplyConnect connect_reply = (PRUDPPacketReplyConnect)reply; - - long my_connection = connect_reply.getConnectionId(); - - PRUDPPacketRequestScrape scrape_request = new PRUDPPacketRequestScrape( my_connection, hashes ); + if ( PRUDPPacketTracker.VERSION == 1 ){ + PRUDPPacketReplyScrape scrape_reply = (PRUDPPacketReplyScrape)reply; + + /* + int interval = scrape_reply.getInterval(); + + if ( interval != 0 ){ - reply = handler.sendAndReceive( auth, scrape_request, destination ); - - if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_SCRAPE ){ - - auth_ok = true; - - if ( PRUDPPacketTracker.VERSION == 1 ){ - PRUDPPacketReplyScrape scrape_reply = (PRUDPPacketReplyScrape)reply; - - /* - int interval = scrape_reply.getInterval(); - - if ( interval != 0 ){ + map.put( "interval", new Long(interval )); + } + */ - map.put( "interval", new Long(interval )); - } - */ - - byte[][] reply_hashes = scrape_reply.getHashes(); - int[] complete = scrape_reply.getComplete(); - int[] downloaded = scrape_reply.getDownloaded(); - int[] incomplete = scrape_reply.getIncomplete(); - - - for (int i=0;i<reply_hashes.length;i++){ + byte[][] reply_hashes = scrape_reply.getHashes(); + int[] complete = scrape_reply.getComplete(); + int[] downloaded = scrape_reply.getDownloaded(); + int[] incomplete = scrape_reply.getIncomplete(); - Map file = new HashMap(); + + for (int i=0;i<reply_hashes.length;i++){ + + Map file = new HashMap(); + + byte[] resp_hash = reply_hashes[i]; + + // System.out.println("got hash:" + ByteFormatter.nicePrint( resp_hash, true )); - byte[] resp_hash = reply_hashes[i]; + files.put( new String(resp_hash, Constants.BYTE_ENCODING), file ); + + file.put( "complete", new Long(complete[i])); + file.put( "downloaded", new Long(downloaded[i])); + file.put( "incomplete", new Long(incomplete[i])); + } + byte[] data = BEncoder.encode( rootMap ); + + message.write( data ); + + return true; + }else{ + PRUDPPacketReplyScrape2 scrape_reply = (PRUDPPacketReplyScrape2)reply; + + + /* + int interval = scrape_reply.getInterval(); + + if ( interval != 0 ){ + + map.put( "interval", new Long(interval )); + } + */ + + int[] complete = scrape_reply.getComplete(); + int[] downloaded = scrape_reply.getDownloaded(); + int[] incomplete = scrape_reply.getIncomplete(); + + int i=0; + for(Iterator it = hashes.iterator();it.hasNext() && i < complete.length;i++) + { + HashWrapper hash = (HashWrapper)it.next(); + Map file = new HashMap(); + file.put( "complete", new Long(complete[i])); + file.put( "downloaded", new Long(downloaded[i])); + file.put( "incomplete", new Long(incomplete[i])); + files.put( new String(hash.getBytes(), Constants.BYTE_ENCODING), file ); + } + // System.out.println("got hash:" + ByteFormatter.nicePrint( resp_hash, true )); - - files.put( new String(resp_hash, Constants.BYTE_ENCODING), file ); - file.put( "complete", new Long(complete[i])); - file.put( "downloaded", new Long(downloaded[i])); - file.put( "incomplete", new Long(incomplete[i])); + byte[] data = BEncoder.encode( rootMap ); + + message.write( data ); + + return true; } - - byte[] data = BEncoder.encode( rootMap ); - - message.write( data ); - - return true; }else{ - PRUDPPacketReplyScrape2 scrape_reply = (PRUDPPacketReplyScrape2)reply; + failure_reason = ((PRUDPPacketReplyError)reply).getMessage(); - /* - int interval = scrape_reply.getInterval(); + if (Logger.isEnabled()) + Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, + "Response from scrape interface "+ reqUrl +" : " + failure_reason)); - if ( interval != 0 ){ - - map.put( "interval", new Long(interval )); - } - */ - - int[] complete = scrape_reply.getComplete(); - int[] downloaded = scrape_reply.getDownloaded(); - int[] incomplete = scrape_reply.getIncomplete(); - - int i=0; - for(Iterator it = hashes.iterator();it.hasNext() && i < complete.length;i++) - { - HashWrapper hash = (HashWrapper)it.next(); - Map file = new HashMap(); - file.put( "complete", new Long(complete[i])); - file.put( "downloaded", new Long(downloaded[i])); - file.put( "incomplete", new Long(incomplete[i])); - files.put( new String(hash.getBytes(), Constants.BYTE_ENCODING), file ); - } - - // System.out.println("got hash:" + ByteFormatter.nicePrint( resp_hash, true )); - - byte[] data = BEncoder.encode( rootMap ); - - message.write( data ); - - return true; + break; } }else{ - + failure_reason = ((PRUDPPacketReplyError)reply).getMessage(); if (Logger.isEnabled()) - Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, - "Response from scrape interface "+ reqUrl +" : " + failure_reason)); - + Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "Response from scrape interface "+ reqUrl +" : " + + ((PRUDPPacketReplyError) reply).getMessage())); + break; } - }else{ - - failure_reason = ((PRUDPPacketReplyError)reply).getMessage(); + + }catch( PRUDPPacketHandlerException e ){ - if (Logger.isEnabled()) - Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "Response from scrape interface "+ reqUrl +" : " - + ((PRUDPPacketReplyError) reply).getMessage())); - - break; - } - - }catch( PRUDPPacketHandlerException e ){ - - if ( e.getMessage() == null || e.getMessage().indexOf("timed out") == -1 ){ + if ( e.getMessage() == null || e.getMessage().indexOf("timed out") == -1 ){ + + throw( e ); + } - throw( e ); + failure_reason = "Timeout"; } + } + + if ( failure_reason != null ){ - failure_reason = "Timeout"; + rootMap.put( "failure reason", failure_reason.getBytes()); + rootMap.remove("files"); + + byte[] data = BEncoder.encode( rootMap ); + message.write( data ); } - } - - if ( failure_reason != null ){ + }finally{ - rootMap.put( "failure reason", failure_reason.getBytes()); - rootMap.remove("files"); - - byte[] data = BEncoder.encode( rootMap ); - message.write( data ); + handler.closeSession(); } - + return false; }finally{ if ( auth != null ){ |