Skip to content

Commit

Permalink
Connect Finish, init cicly life reciver/uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
gilberto-009199 committed May 5, 2022
1 parent 05b1791 commit 32fc8fa
Show file tree
Hide file tree
Showing 12 changed files with 563 additions and 9 deletions.
Binary file added netinst.torrent
Binary file not shown.
1 change: 1 addition & 0 deletions sett.torrent
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d8:announce36:udp://bioquantum.co.za:6969/announce13:creation datei1651551466e8:encoding5:UTF-84:infod6:lengthi2027e4:name13:xsettings.xml12:piece lengthi32768e6:pieces20:�:ť�m�C��vq��C��<7:privatei0eee
9 changes: 2 additions & 7 deletions src/main/java/GivenTools/TorrentInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class TorrentInfo
/**
* The base URL of the tracker for client scrapes.
*/
public final URL announce_url;
public final String announce_url;

/**
* The default length of each piece in bytes.&nbsp; Note that the last piece may be irregularly-sized (less than the value of piece_length)
Expand Down Expand Up @@ -148,17 +148,12 @@ public TorrentInfo(byte[] torrent_file_bytes) throws BencodingException

try {
String url_string = new String(url_buff.array(), "ASCII");
URL announce_url = new URL(url_string);
this.announce_url = announce_url;
this.announce_url = url_string;
}
catch(UnsupportedEncodingException uee)
{
throw new BencodingException(uee.getLocalizedMessage());
}
catch(MalformedURLException murle)
{
throw new BencodingException(murle.getLocalizedMessage());
}

// Try to extract the info dictionary
ByteBuffer info_bytes = Bencoder2.getInfoBytes(torrent_file_bytes);
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/voyager/torrent/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package org.voyager.torrent;

import java.io.File;

import org.voyager.torrent.client.ClientTorrent;
import org.voyager.torrent.util.ReaderBencode;

import GivenTools.BencodingException;

public class Main {

public static void main(String[] args) {
ClientTorrent mytorrent = new ClientTorrent();

mytorrent.addTorentFile("./netinst.torrent");
try {
mytorrent.start();
} catch (BencodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
116 changes: 115 additions & 1 deletion src/main/java/org/voyager/torrent/client/ClientTorrent.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,129 @@
package org.voyager.torrent.client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.voyager.torrent.client.model.Peer;
import org.voyager.torrent.util.BinaryUtil;
import org.voyager.torrent.util.HttpUtil;
import org.voyager.torrent.util.ReaderBencode;

import GivenTools.Bencoder2;
import GivenTools.BencodingException;
import GivenTools.TorrentInfo;

public class ClientTorrent {

public static String separator = System.getProperty("file.separator");
public static String dirUser = new File(System.getProperty("user.home")).getAbsolutePath()+separator;
public static String dirRuntime = "."+separator;


// torrent info e connets info
public TorrentInfo torrent;
public static int uploaded;
public static int downloaded;

public ClientTorrent Build() {
return null;
}
}

public void start() throws BencodingException {
try {

// generate binarie 20
byte[] peerIdBinary = BinaryUtil.genBinaryArray(20);
String peerId = HttpUtil.toHexString(peerIdBinary);
// Identify torrent
String info_hash = HttpUtil.toHexString(torrent.info_hash.array());


Map<String, String> parameters = new HashMap<String,String>();
parameters.put("info_hash",info_hash);
parameters.put("peer_id", peerId); // identify par
parameters.put("uploaded", "0");
parameters.put("port", "-1"); // port connect
parameters.put("downloaded", "0");
parameters.put("left", torrent.file_length+"");
System.out.println(torrent.announce_url);

URL url_announce = new URL(torrent.announce_url+"?"+HttpUtil.getParamsString(parameters));

// get data for connect pars
HttpURLConnection con = (HttpURLConnection) url_announce.openConnection();
con.setRequestMethod("GET");

con.connect();

// read response
StringBuffer res = BinaryUtil.inputStreamReaderToStringBuffer( new InputStreamReader(con.getInputStream()) );
List<Peer> listPeers = new ArrayList<Peer>();
System.out.println(res);

Map<ByteBuffer,Object> map = ReaderBencode.bencodeToMap(res);

int interval = (Integer) map.get( BinaryUtil.stringToByteBuffer("interval") );
System.out.println( interval );

List<Map<ByteBuffer, Object>> peersList = (List<Map<ByteBuffer, Object>>) map.get(BinaryUtil.stringToByteBuffer("peers"));

for (Map<ByteBuffer, Object> rawPeer : peersList) {

int peerPort = ((Integer) rawPeer.get(BinaryUtil.stringToByteBuffer("port"))).intValue();

String ip = null;
try {
ip = new String(((ByteBuffer) rawPeer.get(BinaryUtil.stringToByteBuffer("ip"))).array(),
"ASCII");
} catch (UnsupportedEncodingException e) {
System.out.println("Unable to parse encoding");
continue;
}
//System.out.println("host: "+ip+"\t port: "+peerPort);

listPeers.add( new Peer( ip, peerPort, peerIdBinary, this) );
}

for(Peer peer : listPeers.subList(0, 1)) {
Thread thread = new Thread(peer);
System.out.println(peer);
thread.start();
}


} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public boolean addTorentFile(String arquivo){
return addTorentFile(new File(arquivo));
}
public boolean addTorentFile(File arquivo){
this.torrent = ReaderBencode.parseTorrentFile(arquivo);
if(this.torrent == null)return false;
else return true;
}

}
Loading

0 comments on commit 32fc8fa

Please sign in to comment.