Getting no seeders and leechers for scrape from udp tracker - java

I am trying to implement the scrape method for an UDP tracker but I keep getting 0 seeders/leechers as response. I am still getting a 2 as the action so no error is reported.
I have hardcoded a hash here just to show that it has no effect on the result I am getting.
final ByteArrayOutputStream byteStream =new ByteArrayOutputStream();
final DataOutputStream dataStream =new DataOutputStream(byteStream);
dataStream.writeInt(connectResponse.get("connectionId0"));
dataStream.writeInt(connectResponse.get("connectionId1"));
dataStream.writeInt(2);
dataStream.write(connectResponse.get("transactionId"));
bencodeWriter.write(byteOut.toString());
dataStream.writeChars("1D19CC96C1A4965D184E4B215942DBC0A09FF8F2");
dataStream.close();
final byte[] scrapeBytes= byteStream.toByteArray();
I tried different trackers but get the same response. What might be the problem?
Edit: Added the hex dump of all the requests and responses:
Connect Request:
Connect Response:
Scrape Request:
Scrape Response:

In the Scrape request;
the transaction_id=0x36 is sent as a single byte instead of 4 bytes=0x00000036
and the info_hash is sent as a 80 bytes string that is hex-encoded were every character is prepended by a zero-byte 0x00 instead of as a raw 20 bytes string.
I.E. 0x0031004400310039... instead of 0x1D19...
The Scrape response has no peers, as there is no torrents with the info_hashes sent in the request.

Related

How to properly send HTTP response to Client in Java

I'm in process of making a Server to display a HTML page as a college assessment. All the files are stored locally. Using Firefox to connect to server (chrome seems to block images).
The code below works fine if i type a HTTP Response in the HTML file itself that's being transferred (I'm typing 'HTTP/1.1 200 OK' at start of HTML file)
{
byte[] pageToBytes = Files.readAllBytes(webContent.toPath());
os.write(pageToBytes);
os.flush();
os.close();
}
But if i try and send HTTP response first ,then HTML after, it refuses to load the images in my specified in my HTML code.
Here is Code i'm trying to figure out problem with:
{
byte[] pageToBytes = Files.readAllBytes(webContent.toPath());
String HttpOK = "HTTP/1.1 200 OK\n\r";
os.write(HttpOK.getBytes());
os.write(pageToBytes);
os.flush();
os.close();
}
Any insights would be much appreciated :)
You should read about HTTP requests, when the browser makes a request open a channel of communication between the server and the client, which is the stream you are writing to, this channel closes once the client has received a response.
In your code you are responding once, but the second time the stream is already closed that's why the response body is never reaching the client. Also the server automatically sends a 200 code when there is no error or the code says otherwise.
Since you are trying to make an http server it is good to look at here
it explains how to handle an http request & response.

HttpsUrlConnection response returned from servlet contains extra 'b''0''\r\n' characters when read through python library

I am using HttpsURLConnection to call a server and return the response returned from the HttpsURLConnection from my servlet. I am copying the response from HttpssURLConnection to HttpServletresponse using streams, copying bytes from the httpconnection response input stream to the response's output stream, checking the end by seeing if read returns < 0.
Following is the code for copying the response. The variable response is of type HttpServletResponse and the variable httpCon is of type HttpsURLConnection.
InputStream responseStream = httpCon.getInputStream();
if (responseStream != null)
{
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = responseStream.read(buffer)) >= 0)
{
os.write(buffer, 0, len);
}
os.flush();
os.close();
}
On the client side, I am using python requests library to read the response.
What I am seeing that if I use the curl to test my servlet, I am getting the proper response json, response = u'{"key":"value"}'.
If i read it from the requests python, it is putting some extra characters in the response , the response looks like the following
response = u'b0\r\n{"key":"value"}\r\n0\r\n\r\n'
Both the strings are unicode. But the second one has extra characters.
Same resonse if I try from curl/Postman restclient, I am able to get it properly. But from python requests, it is not working. I tried another livetest library in python, with that also, it is not working and the response has same characters. I also tried to change the accept-encoding header but it did not have any effect.
Because of this, I am not able to parse the json.
I don't want to change the client to parse this kind of string.
Can I change something on the server so that it will work correctly?
Did the response contain the below header "Transfer-Encoding: chunked"?
The response should be in Chunked transfer encoding
https://en.wikipedia.org/wiki/Chunked_transfer_encoding.
In this case, you get \r\n0\r\n\r\n at the end of the response is as expected since it is terminating symbol of this encoding. I guest curl/Postman just help us to handle Chunked transfer encoding, so you can't find these chunked symbols.

HttpUrlConnection reading chunked response

I'm working on a project where i have to use HttpUrlConnection (Android~) for reading the input stream.
It turns out that when i'm reading the input stream the data is malformed and has a bigger size from the original content (which is sent by the server). Now, the server response header contains both "Content-Length" and "Transfer-Encoding: chunked", which from what i know is an issue as both of them shouldn't coexist.
Aside from that the input stream received from HttpUrlConnection contains all body content (with chunks offset informations).
I have two questions:
Shouldn't the HttpUrlConnection handle chunked data?
How to get the data from input stream without chunked informations?
The HttpUrlConnection should be handling chunked data, you're correct. The fact that you're seeing these headers at all means they're probably being malformed somewhere, and something has already sent either a \n\n or \r\n\r\n, so the HttpUrlConnection views it as part of the actual transmission.
If you WANT to be getting the raw data, use a socket and connect to the url on the correct port (probably 80, 443 for ssl)
EDIT: java.net.URLConnection states under the connect() method
Interact with the resource; query header fields and contents.
This shows that a URLConnection, prior to reading anything in from any sort of provided reader, queries the header information. Pardon me for not including this the first time.

How to show response from udp server using DatagramSocket.receive as string?

I use DatagramSocket in android application. I am sending request from client to server. The server responds with a string value either 1 or 0 .
My code sample is as follow:
// client sends
socket.send(datagrampacket);
//Server have
socket.receive (datagrampacket);
I am using Logging to display the response coming from the server, but i don't know how to show the server response (the string received 0 or 1).
after server responding ..
how can i receive datagrampacket on client side (as string it might be 0 or 1) ?
socket.receive() will block until it received data. the received data will be in the datagrampacket byte-array.
to display it, convert the byte-array to a string, e.g. in java this will look like:
byte[]data=datagrampacket.getData();
int offset=datagrampacket.getOffset();
int length=datagrampacket.getLenght();
System.out.println( new String(data, offset, length) ) ;

Unable to transfer image using HTTP client

I have an image byte array which I need to send to a servlet on a server using HTTP client. We know how to send normal text data but unable to send the image data.
We created a string data from image byte array using the following code:
String imageData = new String(imagebyteArr);
And sent the above String to servlet through HTTP client, but when we again retrieve byte array from string using below code:
imageByteArr = imageData.toByteArray();
The resultant byte array is modified one, where in -127 is replaced on 63.
How to solve this unexpected behavior?
Strings get encoded. You have 2 posibilities: encode binary data as base64 (for example) send the base64 and decode on server side or binary upload with a PUT request.
I would totally discourage you with taking image byte array and converting to String as you will have to worry about character encoding.
One thing to do is send the byte array directly using ByteArrayEntity, as follows:
HttpPost post = new HttpPost(url);
post.setEntity(new ByteArrayEntity(bytes));
post.setHeader("Content-type", ""application/octet-stream");
Don't forget to set your Content-Type to the correct image appropriately.

Categories

Resources