diff --git a/src/main/java/com/lbry/globe/api/API.java b/src/main/java/com/lbry/globe/api/API.java index 5683b31..567dc83 100644 --- a/src/main/java/com/lbry/globe/api/API.java +++ b/src/main/java/com/lbry/globe/api/API.java @@ -4,6 +4,7 @@ import com.lbry.globe.object.Node; import com.lbry.globe.object.Service; import java.io.BufferedReader; +import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.net.Inet6Address; @@ -42,20 +43,23 @@ public class API{ } public static void loadNodes(){ - try{ - BufferedReader br = new BufferedReader(new FileReader("nodes.json")); - StringBuilder sb = new StringBuilder(); - String line; - while((line = br.readLine())!=null){ - sb.append(line); + File file = new File("nodes.json"); + if(file.exists()){ + try{ + BufferedReader br = new BufferedReader(new FileReader(file)); + StringBuilder sb = new StringBuilder(); + String line; + while((line = br.readLine())!=null){ + sb.append(line); + } + JSONObject obj = new JSONObject(sb.toString()); + for(String key : obj.keySet()){ + API.NODES.put(InetAddress.getByName(key),Node.fromJSONObject(obj.getJSONObject(key))); + } + br.close(); + }catch(Exception e){ + API.LOGGER.log(Level.WARNING,"Failed loading nodes.",e); } - JSONObject obj = new JSONObject(sb.toString()); - for(String key : obj.keySet()){ - API.NODES.put(InetAddress.getByName(key),Node.fromJSONObject(obj.getJSONObject(key))); - } - br.close(); - }catch(Exception e){ - API.LOGGER.log(Level.WARNING,"Failed loading nodes.",e); } } diff --git a/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java b/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java index 355dee9..bba189b 100644 --- a/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java +++ b/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java @@ -3,6 +3,7 @@ package com.lbry.globe.thread; import com.lbry.globe.api.API; import com.lbry.globe.object.Node; import com.lbry.globe.object.Service; +import com.lbry.globe.util.Environment; import com.lbry.globe.util.GeoIP; import java.io.BufferedReader; @@ -19,33 +20,36 @@ import org.json.JSONObject; public class BlockchainNodeFinderThread implements Runnable{ @Override - public void run() { - while(true){ - try{ - HttpURLConnection conn = (HttpURLConnection) new URI(System.getenv("BLOCKCHAIN_RPC_URL")).toURL().openConnection(); - conn.setDoOutput(true); - conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((System.getenv("BLOCKCHAIN_USERNAME")+":"+System.getenv("BLOCKCHAIN_PASSWORD")).getBytes())); - conn.connect(); - conn.getOutputStream().write(new JSONObject().put("id",new Random().nextInt()).put("method","getnodeaddresses").put("params",new JSONArray().put(2147483647)).toString().getBytes()); - InputStream in = conn.getInputStream(); - if(in==null){ - in = conn.getErrorStream(); + public void run(){ + String rpcURL = Environment.getVariable("BLOCKCHAIN_RPC_URL"); + if(rpcURL!=null){ + while(true){ + try{ + HttpURLConnection conn = (HttpURLConnection) new URI(rpcURL).toURL().openConnection(); + conn.setDoOutput(true); + conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((Environment.getVariable("BLOCKCHAIN_USERNAME")+":"+Environment.getVariable("BLOCKCHAIN_PASSWORD")).getBytes())); + conn.connect(); + conn.getOutputStream().write(new JSONObject().put("id",new Random().nextInt()).put("method","getnodeaddresses").put("params",new JSONArray().put(2147483647)).toString().getBytes()); + InputStream in = conn.getInputStream(); + if(in==null){ + in = conn.getErrorStream(); + } + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + StringBuilder sb = new StringBuilder(); + String line; + while((line = br.readLine())!=null){ + sb.append(line); + } + JSONObject json = new JSONObject(sb.toString()); + manageBlockchainNodes(json.getJSONArray("result")); + }catch(Exception e){ + e.printStackTrace(); } - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - StringBuilder sb = new StringBuilder(); - String line; - while((line = br.readLine())!=null){ - sb.append(line); + try { + Thread.sleep(10_000); + } catch (InterruptedException e) { + throw new RuntimeException(e); } - JSONObject json = new JSONObject(sb.toString()); - manageBlockchainNodes(json.getJSONArray("result")); - }catch(Exception e){ - e.printStackTrace(); - } - try { - Thread.sleep(10_000); - } catch (InterruptedException e) { - throw new RuntimeException(e); } } } diff --git a/src/main/java/com/lbry/globe/util/Environment.java b/src/main/java/com/lbry/globe/util/Environment.java new file mode 100644 index 0000000..60e5565 --- /dev/null +++ b/src/main/java/com/lbry/globe/util/Environment.java @@ -0,0 +1,17 @@ +package com.lbry.globe.util; + +public class Environment{ + + public static String getVariable(String key){ + return Environment.getVariable(key,null); + } + + public static String getVariable(String key,String defaultValue){ + String value = System.getenv(key); + if(value==null){ + return defaultValue; + } + return value; + } + +} \ No newline at end of file diff --git a/src/main/java/com/lbry/globe/util/GeoIP.java b/src/main/java/com/lbry/globe/util/GeoIP.java index c512f19..3389996 100644 --- a/src/main/java/com/lbry/globe/util/GeoIP.java +++ b/src/main/java/com/lbry/globe/util/GeoIP.java @@ -14,7 +14,7 @@ public class GeoIP{ private static final Map CACHE = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress)); private static final Logger LOGGER = Logger.getLogger("GeoIP"); - private static final String TOKEN = System.getenv("IPINFO_TOKEN"); + private static final String TOKEN = Environment.getVariable("IPINFO_TOKEN"); public static JSONObject getCachedGeoIPInformation(InetAddress ip){ JSONObject result = CACHE.get(ip); @@ -33,7 +33,12 @@ public class GeoIP{ public static JSONObject getGeoIPInformation(InetAddress ip) throws IOException,URISyntaxException{ HttpURLConnection conn = (HttpURLConnection) new URI("https://ipinfo.io/"+ip.getHostAddress()+"?token="+GeoIP.TOKEN).toURL().openConnection(); conn.connect(); - InputStream in = conn.getInputStream(); + InputStream in = null; + try{ + in = conn.getInputStream(); + }catch(Exception e){ + GeoIP.LOGGER.log(Level.WARNING,"GeoIP service returned status code '"+conn.getResponseCode()+"'."); + } if(in==null){ in = conn.getErrorStream(); } @@ -56,20 +61,23 @@ public class GeoIP{ } public static void loadCache(){ - try{ - BufferedReader br = new BufferedReader(new FileReader("geoip.json")); - StringBuilder sb = new StringBuilder(); - String line; - while((line = br.readLine())!=null){ - sb.append(line); + File file = new File("geoip.json"); + if(file.exists()){ + try{ + BufferedReader br = new BufferedReader(new FileReader(file)); + StringBuilder sb = new StringBuilder(); + String line; + while((line = br.readLine())!=null){ + sb.append(line); + } + JSONObject obj = new JSONObject(sb.toString()); + for(String key : obj.keySet()){ + GeoIP.CACHE.put(InetAddress.getByName(key),obj.getJSONObject(key)); + } + br.close(); + }catch(Exception e){ + GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e); } - JSONObject obj = new JSONObject(sb.toString()); - for(String key : obj.keySet()){ - GeoIP.CACHE.put(InetAddress.getByName(key),obj.getJSONObject(key)); - } - br.close(); - }catch(Exception e){ - GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e); } }