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,20 +43,23 @@ public class API{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadNodes(){
|
public static void loadNodes(){
|
||||||
try{
|
File file = new File("nodes.json");
|
||||||
BufferedReader br = new BufferedReader(new FileReader("nodes.json"));
|
if(file.exists()){
|
||||||
StringBuilder sb = new StringBuilder();
|
try{
|
||||||
String line;
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||||
while((line = br.readLine())!=null){
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(line);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -19,33 +20,36 @@ import org.json.JSONObject;
|
||||||
public class BlockchainNodeFinderThread implements Runnable{
|
public class BlockchainNodeFinderThread implements Runnable{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run(){
|
||||||
while(true){
|
String rpcURL = Environment.getVariable("BLOCKCHAIN_RPC_URL");
|
||||||
try{
|
if(rpcURL!=null){
|
||||||
HttpURLConnection conn = (HttpURLConnection) new URI(System.getenv("BLOCKCHAIN_RPC_URL")).toURL().openConnection();
|
while(true){
|
||||||
conn.setDoOutput(true);
|
try{
|
||||||
conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((System.getenv("BLOCKCHAIN_USERNAME")+":"+System.getenv("BLOCKCHAIN_PASSWORD")).getBytes()));
|
HttpURLConnection conn = (HttpURLConnection) new URI(rpcURL).toURL().openConnection();
|
||||||
conn.connect();
|
conn.setDoOutput(true);
|
||||||
conn.getOutputStream().write(new JSONObject().put("id",new Random().nextInt()).put("method","getnodeaddresses").put("params",new JSONArray().put(2147483647)).toString().getBytes());
|
conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((Environment.getVariable("BLOCKCHAIN_USERNAME")+":"+Environment.getVariable("BLOCKCHAIN_PASSWORD")).getBytes()));
|
||||||
InputStream in = conn.getInputStream();
|
conn.connect();
|
||||||
if(in==null){
|
conn.getOutputStream().write(new JSONObject().put("id",new Random().nextInt()).put("method","getnodeaddresses").put("params",new JSONArray().put(2147483647)).toString().getBytes());
|
||||||
in = conn.getErrorStream();
|
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));
|
try {
|
||||||
StringBuilder sb = new StringBuilder();
|
Thread.sleep(10_000);
|
||||||
String line;
|
} catch (InterruptedException e) {
|
||||||
while((line = br.readLine())!=null){
|
throw new RuntimeException(e);
|
||||||
sb.append(line);
|
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
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,20 +61,23 @@ public class GeoIP{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadCache(){
|
public static void loadCache(){
|
||||||
try{
|
File file = new File("geoip.json");
|
||||||
BufferedReader br = new BufferedReader(new FileReader("geoip.json"));
|
if(file.exists()){
|
||||||
StringBuilder sb = new StringBuilder();
|
try{
|
||||||
String line;
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||||
while((line = br.readLine())!=null){
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(line);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue