Improve fixing thrown errors
This commit is contained in:
parent
d8f2920ac2
commit
71b5b64ba8
4 changed files with 86 additions and 53 deletions
|
@ -4,6 +4,7 @@ import com.lbry.globe.object.Node;
|
||||||
import com.lbry.globe.object.Service;
|
import com.lbry.globe.object.Service;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.net.Inet6Address;
|
import java.net.Inet6Address;
|
||||||
|
@ -42,8 +43,10 @@ public class API{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadNodes(){
|
public static void loadNodes(){
|
||||||
|
File file = new File("nodes.json");
|
||||||
|
if(file.exists()){
|
||||||
try{
|
try{
|
||||||
BufferedReader br = new BufferedReader(new FileReader("nodes.json"));
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
while((line = br.readLine())!=null){
|
while((line = br.readLine())!=null){
|
||||||
|
@ -58,6 +61,7 @@ public class API{
|
||||||
API.LOGGER.log(Level.WARNING,"Failed loading nodes.",e);
|
API.LOGGER.log(Level.WARNING,"Failed loading nodes.",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void saveNodes(){
|
public static void saveNodes(){
|
||||||
try{
|
try{
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.lbry.globe.thread;
|
||||||
import com.lbry.globe.api.API;
|
import com.lbry.globe.api.API;
|
||||||
import com.lbry.globe.object.Node;
|
import com.lbry.globe.object.Node;
|
||||||
import com.lbry.globe.object.Service;
|
import com.lbry.globe.object.Service;
|
||||||
|
import com.lbry.globe.util.Environment;
|
||||||
import com.lbry.globe.util.GeoIP;
|
import com.lbry.globe.util.GeoIP;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -20,11 +21,13 @@ public class BlockchainNodeFinderThread implements Runnable{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(){
|
public void run(){
|
||||||
|
String rpcURL = Environment.getVariable("BLOCKCHAIN_RPC_URL");
|
||||||
|
if(rpcURL!=null){
|
||||||
while(true){
|
while(true){
|
||||||
try{
|
try{
|
||||||
HttpURLConnection conn = (HttpURLConnection) new URI(System.getenv("BLOCKCHAIN_RPC_URL")).toURL().openConnection();
|
HttpURLConnection conn = (HttpURLConnection) new URI(rpcURL).toURL().openConnection();
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((System.getenv("BLOCKCHAIN_USERNAME")+":"+System.getenv("BLOCKCHAIN_PASSWORD")).getBytes()));
|
conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((Environment.getVariable("BLOCKCHAIN_USERNAME")+":"+Environment.getVariable("BLOCKCHAIN_PASSWORD")).getBytes()));
|
||||||
conn.connect();
|
conn.connect();
|
||||||
conn.getOutputStream().write(new JSONObject().put("id",new Random().nextInt()).put("method","getnodeaddresses").put("params",new JSONArray().put(2147483647)).toString().getBytes());
|
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();
|
InputStream in = conn.getInputStream();
|
||||||
|
@ -49,6 +52,7 @@ public class BlockchainNodeFinderThread implements Runnable{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void manageBlockchainNodes(JSONArray nodes){
|
public void manageBlockchainNodes(JSONArray nodes){
|
||||||
Map<InetAddress,JSONObject> data = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
Map<InetAddress,JSONObject> data = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
||||||
|
|
17
src/main/java/com/lbry/globe/util/Environment.java
Normal file
17
src/main/java/com/lbry/globe/util/Environment.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ public class GeoIP{
|
||||||
|
|
||||||
private static final Map<InetAddress,JSONObject> CACHE = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
private static final Map<InetAddress,JSONObject> CACHE = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
||||||
private static final Logger LOGGER = Logger.getLogger("GeoIP");
|
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){
|
public static JSONObject getCachedGeoIPInformation(InetAddress ip){
|
||||||
JSONObject result = CACHE.get(ip);
|
JSONObject result = CACHE.get(ip);
|
||||||
|
@ -33,7 +33,12 @@ public class GeoIP{
|
||||||
public static JSONObject getGeoIPInformation(InetAddress ip) throws IOException,URISyntaxException{
|
public static JSONObject getGeoIPInformation(InetAddress ip) throws IOException,URISyntaxException{
|
||||||
HttpURLConnection conn = (HttpURLConnection) new URI("https://ipinfo.io/"+ip.getHostAddress()+"?token="+GeoIP.TOKEN).toURL().openConnection();
|
HttpURLConnection conn = (HttpURLConnection) new URI("https://ipinfo.io/"+ip.getHostAddress()+"?token="+GeoIP.TOKEN).toURL().openConnection();
|
||||||
conn.connect();
|
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){
|
if(in==null){
|
||||||
in = conn.getErrorStream();
|
in = conn.getErrorStream();
|
||||||
}
|
}
|
||||||
|
@ -56,8 +61,10 @@ public class GeoIP{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadCache(){
|
public static void loadCache(){
|
||||||
|
File file = new File("geoip.json");
|
||||||
|
if(file.exists()){
|
||||||
try{
|
try{
|
||||||
BufferedReader br = new BufferedReader(new FileReader("geoip.json"));
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
while((line = br.readLine())!=null){
|
while((line = br.readLine())!=null){
|
||||||
|
@ -72,6 +79,7 @@ public class GeoIP{
|
||||||
GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e);
|
GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void saveCache(){
|
public static void saveCache(){
|
||||||
try{
|
try{
|
||||||
|
|
Loading…
Add table
Reference in a new issue