📄 trtrackerserverprocessorudp.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: TRTrackerServerProcessorUDP.java
package org.gudy.azureus2.core3.tracker.server.impl.udp;
import com.aelitis.net.udp.uc.PRUDPPacket;
import com.aelitis.net.udp.uc.PRUDPPacketRequest;
import java.io.*;
import java.net.*;
import java.security.SecureRandom;
import java.util.*;
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.core3.tracker.protocol.PRHelpers;
import org.gudy.azureus2.core3.tracker.protocol.udp.*;
import org.gudy.azureus2.core3.tracker.server.impl.*;
import org.gudy.azureus2.core3.util.*;
// Referenced classes of package org.gudy.azureus2.core3.tracker.server.impl.udp:
// TRTrackerServerUDP
public class TRTrackerServerProcessorUDP extends TRTrackerServerProcessor
{
protected static class connectionData
{
protected String address;
protected long time;
protected String getAddress()
{
return address;
}
protected long getTime()
{
return time;
}
protected connectionData(String _address)
{
address = _address;
time = SystemTime.getCurrentTime();
}
}
private static final LogIDs LOGID;
public static final long CONNECTION_ID_LIFETIME = 0x2bf20L;
private TRTrackerServerUDP server;
private DatagramSocket socket;
private DatagramPacket request_dg;
private static Map connection_id_map = new LinkedHashMap();
private static SecureRandom random;
private static AEMonitor random_mon = new AEMonitor("TRTrackerServerUDP:rand");
protected TRTrackerServerProcessorUDP(TRTrackerServerUDP _server, DatagramSocket _socket, DatagramPacket _packet)
{
server = _server;
socket = _socket;
request_dg = _packet;
}
public void runSupport()
{
byte input_buffer[];
int packet_data_length;
String auth_user;
byte auth_user_bytes[];
byte auth_hash[];
DataInputStream is;
input_buffer = new byte[request_dg.getLength()];
System.arraycopy(request_dg.getData(), 0, input_buffer, 0, input_buffer.length);
packet_data_length = input_buffer.length;
auth_user = null;
auth_user_bytes = null;
auth_hash = null;
if (server.isTrackerPasswordEnabled())
{
if (input_buffer.length < 17)
{
Logger.log(new LogEvent(LOGID, 1, "TRTrackerServerProcessorUDP: packet received but authorisation missing"));
return;
}
packet_data_length -= 16;
auth_user_bytes = new byte[8];
auth_hash = new byte[8];
System.arraycopy(input_buffer, packet_data_length, auth_user_bytes, 0, 8);
int user_len;
for (user_len = 0; user_len < 8 && auth_user_bytes[user_len] != 0; user_len++);
auth_user = new String(auth_user_bytes, 0, user_len);
System.arraycopy(input_buffer, packet_data_length + 8, auth_hash, 0, 8);
}
is = new DataInputStream(new ByteArrayInputStream(input_buffer, 0, packet_data_length));
String client_ip_address = request_dg.getAddress().getHostAddress();
PRUDPPacketRequest request = PRUDPPacketRequest.deserialiseRequest(null, is);
Logger.log(new LogEvent(LOGID, (new StringBuilder()).append("TRTrackerServerProcessorUDP: packet received: ").append(request.getString()).toString()));
PRUDPPacket reply = null;
TRTrackerServerTorrentImpl torrent = null;
if (auth_user_bytes != null)
{
byte sha1_pw[] = null;
if (server.hasExternalAuthorisation())
{
try
{
URL resource = new URL((new StringBuilder()).append("udp://").append(server.getHost()).append(":").append(server.getPort()).append("/").toString());
sha1_pw = server.performExternalAuthorisation(resource, auth_user);
}
catch (MalformedURLException e)
{
Debug.printStackTrace(e);
}
if (sha1_pw == null)
{
Logger.log(new LogEvent(LOGID, 3, (new StringBuilder()).append("TRTrackerServerProcessorUDP: auth fails for user '").append(auth_user).append("'").toString()));
reply = new PRUDPPacketReplyError(request.getTransactionId(), "Access Denied");
}
} else
{
sha1_pw = server.getPassword();
}
if (reply == null)
{
SHA1Hasher hasher = new SHA1Hasher();
hasher.update(input_buffer, 0, packet_data_length);
hasher.update(auth_user_bytes);
hasher.update(sha1_pw);
byte digest[] = hasher.getDigest();
int i = 0;
do
{
if (i >= auth_hash.length)
break;
if (auth_hash[i] != digest[i])
{
Logger.log(new LogEvent(LOGID, 3, (new StringBuilder()).append("TRTrackerServerProcessorUDP: auth fails for user '").append(auth_user).append("'").toString()));
reply = new PRUDPPacketReplyError(request.getTransactionId(), "Access Denied");
break;
}
i++;
} while (true);
}
}
int request_type = -1;
if (reply == null)
if (server.isEnabled())
{
try
{
int type = request.getAction();
if (type == 0)
reply = handleConnect(client_ip_address, request);
else
if (type == 1)
{
Object x[] = handleAnnounceAndScrape(client_ip_address, request, 1);
reply = (PRUDPPacket)x[0];
torrent = (TRTrackerServerTorrentImpl)x[1];
request_type = 1;
} else
if (type == 2)
{
Object x[] = handleAnnounceAndScrape(client_ip_address, request, 2);
reply = (PRUDPPacket)x[0];
torrent = (TRTrackerServerTorrentImpl)x[1];
request_type = 2;
} else
{
reply = new PRUDPPacketReplyError(request.getTransactionId(), "unsupported action");
}
}
catch (Throwable e)
{
String error = e.getMessage();
if (error == null)
error = e.toString();
reply = new PRUDPPacketReplyError(request.getTransactionId(), error);
}
} else
{
System.out.println((new StringBuilder()).append("UDP Tracker: replying 'disabled' to ").append(client_ip_address).toString());
reply = new PRUDPPacketReplyError(request.getTransactionId(), "UDP Tracker disabled");
}
if (reply != null)
{
InetAddress address = request_dg.getAddress();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
reply.serialise(os);
byte output_buffer[] = baos.toByteArray();
DatagramPacket reply_packet = new DatagramPacket(output_buffer, output_buffer.length, address, request_dg.getPort());
socket.send(reply_packet);
server.updateStats(request_type, torrent, input_buffer.length, output_buffer.length);
}
Throwable e;
try
{
is.close();
}
// Misplaced declaration of an exception variable
catch (Throwable e) { }
break MISSING_BLOCK_LABEL_903;
e;
Logger.log(new LogEvent(LOGID, "TRTrackerServerProcessorUDP: processing fails", e));
try
{
is.close();
}
// Misplaced declaration of an exception variable
catch (Throwable e) { }
break MISSING_BLOCK_LABEL_903;
Exception exception;
exception;
try
{
is.close();
}
catch (Throwable e) { }
throw exception;
}
public void interruptTask()
{
}
protected long allocateConnectionId(String client_address)
{
long l;
random_mon.enter();
long id = random.nextLong();
Long new_key = new Long(id);
connectionData new_data = new connectionData(client_address);
Iterator it = connection_id_map.keySet().iterator();
do
{
if (!it.hasNext())
break;
Long key = (Long)it.next();
connectionData data = (connectionData)connection_id_map.get(key);
if (new_data.getTime() - data.getTime() <= 0x2bf20L)
break;
it.remove();
} while (true);
connection_id_map.put(new_key, new_data);
l = id;
random_mon.exit();
return l;
Exception exception;
exception;
random_mon.exit();
throw exception;
}
protected boolean checkConnectionId(String client_address, long id)
{
Long key;
connectionData data;
boolean flag;
random_mon.enter();
key = new Long(id);
data = (connectionData)connection_id_map.get(key);
if (data != null)
break MISSING_BLOCK_LABEL_48;
flag = false;
random_mon.exit();
return flag;
boolean flag1;
connection_id_map.remove(key);
boolean ok = data.getAddress().equals(client_address);
flag1 = ok;
random_mon.exit();
return flag1;
Exception exception;
exception;
random_mon.exit();
throw exception;
}
protected PRUDPPacket handleConnect(String client_ip_address, PRUDPPacketRequest request)
{
long conn_id = allocateConnectionId(client_ip_address);
PRUDPPacket reply = new PRUDPPacketReplyConnect(request.getTransactionId(), conn_id);
return reply;
}
protected Object[] handleAnnounceAndScrape(String client_ip_address, PRUDPPacketRequest request, int request_type)
throws Exception
{
if (!checkConnectionId(client_ip_address, request.getConnectionId()))
return null;
List hashbytes = new ArrayList();
HashWrapper peer_id = null;
int port = 0;
String event = null;
long uploaded = 0L;
long downloaded = 0L;
long left = 0L;
int num_want = -1;
String key = null;
if (request_type == 1)
{
if (PRUDPPacketTracker.VERSION == 1)
{
PRUDPPacketRequestAnnounce announce = (PRUDPPacketRequestAnnounce)request;
hashbytes.add(announce.getHash());
peer_id = new HashWrapper(announce.getPeerId());
port = announce.getPort();
int i_event = announce.getEvent();
switch (i_event)
{
case 2: // '\002'
event = "started";
break;
case 3: // '\003'
event = "stopped";
break;
case 1: // '\001'
event = "completed";
break;
}
uploaded = announce.getUploaded();
downloaded = announce.getDownloaded();
left = announce.getLeft();
num_want = announce.getNumWant();
int i_ip = announce.getIPAddress();
if (i_ip != 0)
client_ip_address = PRHelpers.intToAddress(i_ip);
} else
{
PRUDPPacketRequestAnnounce2 announce = (PRUDPPacketRequestAnnounce2)request;
hashbytes.add(announce.getHash());
peer_id = new HashWrapper(announce.getPeerId());
port = announce.getPort();
int i_event = announce.getEvent();
switch (i_event)
{
case 2: // '\002'
event = "started";
break;
case 3: // '\003'
event = "stopped";
break;
case 1: // '\001'
event = "completed";
break;
}
uploaded = announce.getUploaded();
downloaded = announce.getDownloaded();
left = announce.getLeft();
num_want = announce.getNumWant();
int i_ip = announce.getIPAddress();
if (i_ip != 0)
client_ip_address = PRHelpers.intToAddress(i_ip);
key = (new StringBuilder()).append("").append(announce.getKey()).toString();
}
} else
{
PRUDPPacketRequestScrape scrape = (PRUDPPacketRequestScrape)request;
hashbytes.addAll(scrape.getHashes());
}
Map root_out[] = new Map[1];
TRTrackerServerPeerImpl peer_out[] = new TRTrackerServerPeerImpl[1];
TRTrackerServerTorrentImpl torrent = processTrackerRequest(server, "", root_out, peer_out, request_type, (byte[][])(byte[][])hashbytes.toArray(new byte[0][0]), null, null, peer_id, false, (byte)0, key, event, false, port, 0, 0, client_ip_address, client_ip_address, downloaded, uploaded, left, num_want, (byte)0, (byte)1, 0, null);
Map root = root_out[0];
PRUDPPacketReplyScrape reply;
if (request_type == 1)
{
if (PRUDPPacketTracker.VERSION == 1)
{
reply = new PRUDPPacketReplyAnnounce(request.getTransactionId());
reply.setInterval(((Long)root.get("interval")).intValue());
List peers = (List)root.get("peers");
int addresses[] = new int[peers.size()];
short ports[] = new short[addresses.length];
for (int i = 0; i < addresses.length; i++)
{
Map peer = (Map)peers.get(i);
addresses[i] = PRHelpers.addressToInt(new String((byte[])(byte[])peer.get("ip")));
ports[i] = ((Long)peer.get("port")).shortValue();
}
reply.setPeers(addresses, ports);
return (new Object[] {
reply, torrent
});
}
reply = new PRUDPPacketReplyAnnounce2(request.getTransactionId());
reply.setInterval(((Long)root.get("interval")).intValue());
boolean local_scrape = client_ip_address.equals("127.0.0.1");
Map scrape_details = torrent.exportScrapeToMap("", client_ip_address, !local_scrape);
int seeders = ((Long)scrape_details.get("complete")).intValue();
int leechers = ((Long)scrape_details.get("incomplete")).intValue();
reply.setLeechersSeeders(leechers, seeders);
List peers = (List)root.get("peers");
int addresses[] = new int[peers.size()];
short ports[] = new short[addresses.length];
for (int i = 0; i < addresses.length; i++)
{
Map peer = (Map)peers.get(i);
addresses[i] = PRHelpers.addressToInt(new String((byte[])(byte[])peer.get("ip")));
ports[i] = ((Long)peer.get("port")).shortValue();
}
reply.setPeers(addresses, ports);
return (new Object[] {
reply, torrent
});
}
Map files;
if (PRUDPPacketTracker.VERSION == 1)
{
reply = new PRUDPPacketReplyScrape(request.getTransactionId());
files = (Map)root.get("files");
byte hashes[][] = new byte[files.size()][];
int s_complete[] = new int[hashes.length];
int s_downloaded[] = new int[hashes.length];
int s_incomplete[] = new int[hashes.length];
Iterator it = files.keySet().iterator();
for (int pos = 0; it.hasNext(); pos++)
{
String hash_str = (String)it.next();
hashes[pos] = hash_str.getBytes("ISO-8859-1");
Map details = (Map)files.get(hash_str);
s_complete[pos] = ((Long)details.get("complete")).intValue();
s_incomplete[pos] = ((Long)details.get("incomplete")).intValue();
s_downloaded[pos] = ((Long)details.get("downloaded")).intValue();
}
reply.setDetails(hashes, s_complete, s_downloaded, s_incomplete);
return (new Object[] {
reply, torrent
});
}
reply = new PRUDPPacketReplyScrape2(request.getTransactionId());
files = (Map)root.get("files");
int s_complete[] = new int[files.size()];
int s_downloaded[] = new int[s_complete.length];
int s_incomplete[] = new int[s_complete.length];
Iterator it = files.keySet().iterator();
for (int pos = 0; it.hasNext(); pos++)
{
String hash_str = (String)it.next();
Map details = (Map)files.get(hash_str);
s_complete[pos] = ((Long)details.get("complete")).intValue();
s_incomplete[pos] = ((Long)details.get("incomplete")).intValue();
s_downloaded[pos] = ((Long)details.get("downloaded")).intValue();
}
reply.setDetails(s_complete, s_downloaded, s_incomplete);
return (new Object[] {
reply, torrent
});
}
static
{
LOGID = LogIDs.TRACKER;
random = RandomUtils.SECURE_RANDOM;
PRUDPTrackerCodecs.registerCodecs();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -