Skip to content

Commit

Permalink
Use ToxCDN to get node info
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Sep 4, 2014
1 parent d37d666 commit 562b89f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 66 deletions.
Binary file removed app/libs/jsoup-1.7.3.jar
Binary file not shown.
113 changes: 47 additions & 66 deletions app/src/main/java/im/tox/antox/utils/DHTNodeDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.InetAddress;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
Expand All @@ -27,6 +27,29 @@

public class DHTNodeDetails extends AsyncTask<Void, Void, Void> {

private static class JsonReader {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
}

final String[] nodeDetails = new String[7];
final String TAG = "DHTNODEDETAILS";
Context ctx;
Expand All @@ -38,94 +61,53 @@ public DHTNodeDetails(Context ctx) {
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect("https://wiki.tox.im/Nodes").timeout(10000).get();
Elements nodeRows = document.getElementsByTag("tr");

File folder = new File(Environment.getExternalStorageDirectory() + "/Antox");
final String filename = folder.toString() + "/" + "Nodes.csv";
FileWriter fw = new FileWriter(filename);

for (Element nodeRow : nodeRows) {
Elements nodeElements = nodeRow.getElementsByTag("td");
int c = 0;
for (Element nodeElement : nodeElements)
nodeDetails[c++] = nodeElement.text();

if (nodeDetails[6] != null && nodeDetails[6].equals("WORK")) {
DhtNode.ipv4.add(nodeDetails[0]);
DhtNode.ipv6.add(nodeDetails[1]);
DhtNode.port.add(nodeDetails[2]);
DhtNode.key.add(nodeDetails[3]);
DhtNode.owner.add(nodeDetails[4]);
DhtNode.location.add(nodeDetails[5]);
fw.append(nodeDetails[0]+","+nodeDetails[1]+","+nodeDetails[2]+","+nodeDetails[3]
+","+nodeDetails[4]+","+nodeDetails[5]+"\n");
}
/* Get nodes from json-formatted data */
JSONObject json = JsonReader.readJsonFromUrl("http://jfk.us.cdn.libtoxcore.so/elizabeth_remote/config/Nodefile.json");
Log.d(TAG, json.toString());
JSONArray serverArray = json.getJSONArray("servers");
for(int i = 0; i < serverArray.length(); i++) {
JSONObject jsonObject = serverArray.getJSONObject(i);
DhtNode.owner.add(jsonObject.getString("owner"));
DhtNode.ipv6.add(jsonObject.getString("ipv6"));
DhtNode.key.add(jsonObject.getString("pubkey"));
DhtNode.ipv4.add(jsonObject.getString("ipv4"));
DhtNode.port.add(String.valueOf(jsonObject.getInt("port")));
}

fw.close();

Log.d(TAG, "Nodes fetched from online");

} catch (Exception e) {

String fileName = "AntoxNodes";
BufferedReader br;
String line;
String splitBy = ",";
} catch (Exception exp) {
Log.d(TAG, "Failed to connect to Tox CDN for nodes");

try {
br = new BufferedReader(new FileReader(fileName));
String[] node;
while((line = br.readLine()) != null) {
node = line.split(splitBy);
DhtNode.ipv4.add(node[0]);
DhtNode.ipv6.add(node[1]);
DhtNode.location.add(node[2]);
DhtNode.owner.add(node[3]);
DhtNode.port.add(node[4]);
DhtNode.key.add(node[5]);
}
br.close();
Log.d(TAG, "AntoxNodes file found and been read");
} catch (Exception exp) {
Log.d(TAG, "AntoxNodes file not found");
DhtNode.ipv4.add("192.254.75.98");
DhtNode.ipv6.add("2607:5600:284::2");
DhtNode.location.add("US");
DhtNode.owner.add("stqism");
DhtNode.port.add("33445");
DhtNode.key.add("951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F");

DhtNode.ipv4.add("144.76.60.215");
DhtNode.ipv6.add("2a01:4f8:191:64d6::1");
DhtNode.location.add("DE");
DhtNode.owner.add("sonofra");
DhtNode.port.add("33445");
DhtNode.key.add("04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F");

DhtNode.ipv4.add("37.187.46.132");
DhtNode.ipv6.add("2001:41d0:0052:0300::0507");
DhtNode.location.add("FR");
DhtNode.owner.add("mouseym");
DhtNode.port.add("33445");
DhtNode.key.add("A9D98212B3F972BD11DA52BEB0658C326FCCC1BFD49F347F9C2D3D8B61E1B927");

DhtNode.ipv4.add("109.169.46.133");
DhtNode.ipv6.add("");
DhtNode.location.add("UK");
DhtNode.ipv4.add("37.59.102.176");
DhtNode.ipv6.add("2001:41d0:51:1:0:0:0:cc");
DhtNode.owner.add("astonex");
DhtNode.port.add("33445");
DhtNode.key.add("7F31BFC93B8E4016A902144D0B110C3EA97CB7D43F1C4D21BCAE998A7C838821");
DhtNode.key.add("B98A2CEAA6C6A2FADC2C3632D284318B60FE5375CCB41EFA081AB67F500C1B0B");

DhtNode.ipv4.add("54.199.139.199");
DhtNode.ipv6.add("");
DhtNode.location.add("JP");
DhtNode.owner.add("aitjcize");
DhtNode.port.add("33445");
DhtNode.key.add("7F9C31FE850E97CEFD4C4591DF93FC757C7C12549DDD55F8EEAECC34FE76C029");
}
}

Log.d(TAG, "DhtNode size: " + DhtNode.ipv4.size());
Expand Down Expand Up @@ -175,7 +157,6 @@ protected Void doInBackground(Void... params) {
DhtNode.port.add(0, DhtNode.port.get(pos));
DhtNode.key.add(0, DhtNode.key.get(pos));
DhtNode.owner.add(0, DhtNode.owner.get(pos));
DhtNode.location.add(0, DhtNode.location.get(pos));
Log.d(TAG, "DHT Nodes have been sorted");
DhtNode.sorted = true;
}
Expand Down

0 comments on commit 562b89f

Please sign in to comment.