Improve detecting blockchains
This commit is contained in:
parent
25e4f24ae9
commit
db65cd2355
6 changed files with 67 additions and 43 deletions
|
@ -7,6 +7,7 @@ import com.lbry.globe.thread.BlockchainNodeFinderThread;
|
||||||
import com.lbry.globe.thread.DHTNodeFinderThread;
|
import com.lbry.globe.thread.DHTNodeFinderThread;
|
||||||
import com.lbry.globe.thread.HubNodeFinderThread;
|
import com.lbry.globe.thread.HubNodeFinderThread;
|
||||||
import com.lbry.globe.util.GeoIP;
|
import com.lbry.globe.util.GeoIP;
|
||||||
|
import com.lbry.globe.util.NamedThreadFactory;
|
||||||
|
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
@ -17,7 +18,6 @@ import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import io.netty.handler.codec.http.HttpRequestDecoder;
|
import io.netty.handler.codec.http.HttpRequestDecoder;
|
||||||
import io.netty.handler.codec.http.HttpResponseEncoder;
|
import io.netty.handler.codec.http.HttpResponseEncoder;
|
||||||
import io.netty.util.concurrent.DefaultThreadFactory;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
@ -36,7 +36,7 @@ public class Main implements Runnable{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(){
|
public void run(){
|
||||||
EventLoopGroup group = new MultiThreadIoEventLoopGroup(new DefaultThreadFactory("Netty Event Loop"),NioIoHandler.newFactory());
|
EventLoopGroup group = new MultiThreadIoEventLoopGroup(new NamedThreadFactory("Netty Event Loop"),NioIoHandler.newFactory());
|
||||||
this.runTCPServerHTTP(group);
|
this.runTCPServerHTTP(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ public class Main implements Runnable{
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(GeoIP::saveCache,"Save Cache"));
|
Runtime.getRuntime().addShutdownHook(new Thread(GeoIP::saveCache,"Save Cache"));
|
||||||
GeoIP.loadCache();
|
GeoIP.loadCache();
|
||||||
Main.LOGGER.info("Starting finder thread for blockchain nodes");
|
Main.LOGGER.info("Starting finder thread for blockchain nodes");
|
||||||
new Thread(new BlockchainNodeFinderThread(),"Block Node Finder").start();
|
new BlockchainNodeFinderThread().run();
|
||||||
Main.LOGGER.info("Starting finder thread for DHT nodes");
|
Main.LOGGER.info("Starting finder thread for DHT nodes");
|
||||||
new DHTNodeFinderThread().run();
|
new DHTNodeFinderThread().run();
|
||||||
Main.LOGGER.info("Starting finder thread for hub nodes");
|
Main.LOGGER.info("Starting finder thread for hub nodes");
|
||||||
|
|
|
@ -13,7 +13,10 @@ import java.net.HttpURLConnection;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import com.lbry.globe.util.NamedThreadFactory;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -23,7 +26,8 @@ public class BlockchainNodeFinderThread implements Runnable{
|
||||||
public void run(){
|
public void run(){
|
||||||
String rpcURL = Environment.getVariable("BLOCKCHAIN_RPC_URL");
|
String rpcURL = Environment.getVariable("BLOCKCHAIN_RPC_URL");
|
||||||
if(rpcURL!=null){
|
if(rpcURL!=null){
|
||||||
while(true){
|
Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Blockchain Detector")).scheduleWithFixedDelay(() -> {
|
||||||
|
System.out.println("[BLOCKCHAIN] BULK PING");
|
||||||
try{
|
try{
|
||||||
HttpURLConnection conn = (HttpURLConnection) new URI(rpcURL).toURL().openConnection();
|
HttpURLConnection conn = (HttpURLConnection) new URI(rpcURL).toURL().openConnection();
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
|
@ -45,12 +49,8 @@ public class BlockchainNodeFinderThread implements Runnable{
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
try {
|
},0,10,TimeUnit.SECONDS);
|
||||||
Thread.sleep(10_000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.lbry.globe.object.Node;
|
||||||
import com.lbry.globe.object.Service;
|
import com.lbry.globe.object.Service;
|
||||||
import com.lbry.globe.util.DHT;
|
import com.lbry.globe.util.DHT;
|
||||||
import com.lbry.globe.util.GeoIP;
|
import com.lbry.globe.util.GeoIP;
|
||||||
|
import com.lbry.globe.util.NamedThreadFactory;
|
||||||
import com.lbry.globe.util.UDP;
|
import com.lbry.globe.util.UDP;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -14,7 +15,6 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import io.netty.util.concurrent.DefaultThreadFactory;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class DHTNodeFinderThread implements Runnable{
|
public class DHTNodeFinderThread implements Runnable{
|
||||||
|
@ -46,7 +46,7 @@ public class DHTNodeFinderThread implements Runnable{
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startSender(){
|
private void startSender(){
|
||||||
Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("DHT Sender")).scheduleWithFixedDelay(() -> {
|
Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("DHT Sender")).scheduleWithFixedDelay(() -> {
|
||||||
System.out.println("[DHT] BULK PING");
|
System.out.println("[DHT] BULK PING");
|
||||||
API.saveNodes();
|
API.saveNodes();
|
||||||
for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){
|
for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){
|
||||||
|
@ -126,7 +126,7 @@ public class DHTNodeFinderThread implements Runnable{
|
||||||
|
|
||||||
private void startReceiver(){
|
private void startReceiver(){
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
while(true) {
|
while(DHT.getSocket().isBound()) {
|
||||||
try {
|
try {
|
||||||
UDP.Packet receiverPacket = UDP.receive(DHT.getSocket());
|
UDP.Packet receiverPacket = UDP.receive(DHT.getSocket());
|
||||||
DHTNodeFinderThread.this.incoming.add(receiverPacket);
|
DHTNodeFinderThread.this.incoming.add(receiverPacket);
|
||||||
|
|
|
@ -4,23 +4,19 @@ 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.GeoIP;
|
import com.lbry.globe.util.GeoIP;
|
||||||
|
import com.lbry.globe.util.NamedThreadFactory;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import io.netty.util.concurrent.DefaultThreadFactory;
|
|
||||||
import io.netty.util.concurrent.ThreadPerTaskExecutor;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import javax.net.SocketFactory;
|
|
||||||
|
|
||||||
public class HubNodeFinderThread implements Runnable{
|
public class HubNodeFinderThread implements Runnable{
|
||||||
|
|
||||||
public static final String[] HUBS = {
|
public static final String[] HUBS = {
|
||||||
|
@ -46,11 +42,17 @@ public class HubNodeFinderThread implements Runnable{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(){
|
public void run(){
|
||||||
Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("Hub Sender")).scheduleWithFixedDelay(() -> {
|
Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Hub Sender")).scheduleWithFixedDelay(() -> {
|
||||||
System.out.println("[HUB] BULK PING");
|
System.out.println("[HUB] BULK PING");
|
||||||
for(String hostname : HubNodeFinderThread.HUBS){
|
for(String hostname : HubNodeFinderThread.HUBS){
|
||||||
|
InetAddress[] ips = null;
|
||||||
try{
|
try{
|
||||||
for(InetAddress ip : InetAddress.getAllByName(hostname)){
|
ips = InetAddress.getAllByName(hostname);
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if(ips!=null){
|
||||||
|
for(InetAddress ip : ips){
|
||||||
if(!HubNodeFinderThread.SOCKETS.containsKey(ip)){
|
if(!HubNodeFinderThread.SOCKETS.containsKey(ip)){
|
||||||
HubNodeFinderThread.SOCKETS.put(ip,new Socket());
|
HubNodeFinderThread.SOCKETS.put(ip,new Socket());
|
||||||
}
|
}
|
||||||
|
@ -69,20 +71,25 @@ public class HubNodeFinderThread implements Runnable{
|
||||||
}
|
}
|
||||||
System.out.println(" - [Hub] To: "+s.getRemoteSocketAddress());
|
System.out.println(" - [Hub] To: "+s.getRemoteSocketAddress());
|
||||||
|
|
||||||
|
try{
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
obj.put("id",new Random().nextInt());
|
obj.put("id",new Random().nextInt());
|
||||||
obj.put("method","server.banner");
|
obj.put("method","server.banner");
|
||||||
obj.put("params",new JSONArray());
|
obj.put("params",new JSONArray());
|
||||||
s.getOutputStream().write((obj+"\n").getBytes());
|
s.getOutputStream().write((obj+"\n").getBytes());
|
||||||
s.getOutputStream().flush();
|
s.getOutputStream().flush();
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
for(InetAddress ip : InetAddress.getAllByName(hostname)){
|
}
|
||||||
|
for(InetAddress ip : ips){
|
||||||
Socket s = HubNodeFinderThread.SOCKETS.get(ip);
|
Socket s = HubNodeFinderThread.SOCKETS.get(ip);
|
||||||
if(s==null || !s.isConnected() || s.isClosed()){
|
if(s==null || !s.isConnected() || s.isClosed()){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
System.out.println(" - [Hub] From: "+s.getRemoteSocketAddress());
|
System.out.println(" - [Hub] From: "+s.getRemoteSocketAddress());
|
||||||
|
|
||||||
|
try{
|
||||||
InputStream in = s.getInputStream();
|
InputStream in = s.getInputStream();
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
int b;
|
int b;
|
||||||
|
@ -95,11 +102,12 @@ public class HubNodeFinderThread implements Runnable{
|
||||||
if(successful){
|
if(successful){
|
||||||
LAST_SEEN.put(ip,System.currentTimeMillis());
|
LAST_SEEN.put(ip,System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<InetAddress> removeIPs = new ArrayList<>();
|
List<InetAddress> removeIPs = new ArrayList<>();
|
||||||
for(Map.Entry<InetAddress,Long> entry : HubNodeFinderThread.LAST_SEEN.entrySet()){
|
for(Map.Entry<InetAddress,Long> entry : HubNodeFinderThread.LAST_SEEN.entrySet()){
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.lbry.globe.util;
|
package com.lbry.globe.util;
|
||||||
|
|
||||||
import io.netty.util.concurrent.DefaultThreadFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
@ -14,7 +12,7 @@ public class DHT{
|
||||||
|
|
||||||
public static byte[] NODE_ID = new byte[48];
|
public static byte[] NODE_ID = new byte[48];
|
||||||
|
|
||||||
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("Timeout Future"));
|
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Timeout Future"));
|
||||||
private static final TimeoutFutureManager<RPCID,UDP.Packet> futureManager = new TimeoutFutureManager<>(executor);
|
private static final TimeoutFutureManager<RPCID,UDP.Packet> futureManager = new TimeoutFutureManager<>(executor);
|
||||||
private static final Map<InetSocketAddress,Boolean> peers = new ConcurrentHashMap<>();
|
private static final Map<InetSocketAddress,Boolean> peers = new ConcurrentHashMap<>();
|
||||||
private static final DatagramSocket socket;
|
private static final DatagramSocket socket;
|
||||||
|
|
18
src/main/java/com/lbry/globe/util/NamedThreadFactory.java
Normal file
18
src/main/java/com/lbry/globe/util/NamedThreadFactory.java
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package com.lbry.globe.util;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
|
||||||
|
public class NamedThreadFactory implements ThreadFactory{
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public NamedThreadFactory(String name){
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable r){
|
||||||
|
return new Thread(r,this.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue