|
3 | 3 | import com.lbry.globe.Main; |
4 | 4 | import com.lbry.globe.api.API; |
5 | 5 |
|
| 6 | +import com.lbry.globe.util.DHT; |
| 7 | +import com.lbry.globe.util.Hex; |
| 8 | +import com.lbry.globe.util.TimeoutFutureManager; |
| 9 | +import com.lbry.globe.util.UDP; |
6 | 10 | import io.netty.buffer.ByteBuf; |
7 | 11 | import io.netty.buffer.Unpooled; |
8 | 12 | import io.netty.channel.ChannelHandlerContext; |
|
13 | 17 | import java.io.ByteArrayOutputStream; |
14 | 18 | import java.io.InputStream; |
15 | 19 | import java.io.IOException; |
| 20 | +import java.net.InetSocketAddress; |
16 | 21 | import java.net.URI; |
17 | 22 | import java.security.MessageDigest; |
18 | 23 | import java.util.*; |
| 24 | +import java.util.concurrent.CompletableFuture; |
19 | 25 | import java.util.logging.Level; |
20 | 26 | import java.util.logging.Logger; |
21 | 27 |
|
@@ -85,6 +91,106 @@ private void handleResponse(ChannelHandlerContext ctx){ |
85 | 91 | ctx.write(response); |
86 | 92 | return; |
87 | 93 | } |
| 94 | + if("/api/command".equals(uri.getPath())){ |
| 95 | + JSONObject json = new JSONObject(); |
| 96 | + |
| 97 | + String[] queryParts = uri.getQuery()!=null?uri.getQuery().split(";"):new String[]{""}; |
| 98 | + if("ping".equals(queryParts[0]) || "findNode".equals(queryParts[0]) || "findValue".equals(queryParts[0])){ |
| 99 | + //STORE IS NOT SUPPORTED |
| 100 | + json.put("query",queryParts); |
| 101 | + |
| 102 | + Map<InetSocketAddress,Boolean> peers = DHT.getPeers(); |
| 103 | + CompletableFuture<UDP.Packet>[] futures = new CompletableFuture[peers.size()]; |
| 104 | + int i=0; |
| 105 | + for(Map.Entry<InetSocketAddress,Boolean> entry : peers.entrySet()){ |
| 106 | + try{ |
| 107 | + if("ping".equals(queryParts[0])){ |
| 108 | + futures[i] = DHT.ping(DHT.getSocket(),entry.getKey()); |
| 109 | + } |
| 110 | + if("findNode".equals(queryParts[0])){ |
| 111 | + futures[i] = DHT.findNode(DHT.getSocket(),entry.getKey(),queryParts.length>=2?Hex.decode(queryParts[1]):new byte[48]); |
| 112 | + } |
| 113 | + if("findValue".equals(queryParts[0])){ |
| 114 | + futures[i] = DHT.findValue(DHT.getSocket(),entry.getKey(),queryParts.length>=2?Hex.decode(queryParts[1]):new byte[48]); |
| 115 | + } |
| 116 | + }catch(IOException ignored){} |
| 117 | + i++; |
| 118 | + } |
| 119 | + |
| 120 | + CompletableFuture<List<UDP.Packet>> total = TimeoutFutureManager.getBulk(futures); |
| 121 | + |
| 122 | + JSONObject jsonData = new JSONObject(); |
| 123 | + json.put("data",jsonData); |
| 124 | + try{ |
| 125 | + List<UDP.Packet> responses = total.get(); |
| 126 | + for(UDP.Packet resp : responses){ |
| 127 | + if(resp!=null){ |
| 128 | + DHT.Message<?> message = DHT.Message.fromBencode(resp.getData()); |
| 129 | + if("ping".equals(queryParts[0])){ |
| 130 | + String pong = (String) message.getPayload(); |
| 131 | + jsonData.put(resp.getAddress().getAddress().getHostAddress()+":"+resp.getAddress().getPort(),pong); |
| 132 | + } |
| 133 | + if("findNode".equals(queryParts[0])){ |
| 134 | + JSONArray payload = new JSONArray(); |
| 135 | + List<List<Object>> nodes = (List<List<Object>>) message.getPayload(); |
| 136 | + for(List<Object> node : nodes){ |
| 137 | + JSONObject p = new JSONObject(); |
| 138 | + p.put("nodeID", Hex.encode((byte[]) node.get(0))); |
| 139 | + p.put("hostname",node.get(1)); |
| 140 | + p.put("port",node.get(2)); |
| 141 | + payload.put(p); |
| 142 | + } |
| 143 | + jsonData.put(resp.getAddress().getAddress().getHostAddress()+":"+resp.getAddress().getPort(),payload); |
| 144 | + } |
| 145 | + if("findValue".equals(queryParts[0])){ |
| 146 | + Map<String,Object> map = (Map<String, Object>) message.getPayload(); |
| 147 | + JSONObject payload = new JSONObject(); |
| 148 | + payload.put("p",map.get("p")); |
| 149 | + payload.put("protocolVersion",map.get("protocolVersion")); |
| 150 | + JSONArray contacts = new JSONArray(); |
| 151 | + List<List<Object>> nodes = (List<List<Object>>) map.get("contacts"); |
| 152 | + for(List<Object> node : nodes){ |
| 153 | + JSONObject p = new JSONObject(); |
| 154 | + p.put("nodeID", Hex.encode((byte[]) node.get(0))); |
| 155 | + p.put("hostname",node.get(1)); |
| 156 | + p.put("port",node.get(2)); |
| 157 | + contacts.put(p); |
| 158 | + } |
| 159 | + payload.put("contacts",contacts); |
| 160 | + //payload.put("token",Hex.encode((byte[]) map.get("token"))); |
| 161 | + jsonData.put(resp.getAddress().getAddress().getHostAddress()+":"+resp.getAddress().getPort(),payload); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + }catch(Exception e){ |
| 166 | + e.printStackTrace(); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +// for(Map.Entry<InetSocketAddress,Boolean> dest : DHT.getPeers().entrySet()){ |
| 172 | +// if(!dest.getValue()){ |
| 173 | +// try{ |
| 174 | +// |
| 175 | +// UDP.Packet packet = DHT.ping(DHT.getSocket(),dest.getKey()).get(1, TimeUnit.SECONDS); |
| 176 | +// DHT.Message<?> message = DHT.Message.fromBencode(packet.getData()); |
| 177 | +// json.put(dest.getKey().toString(),message.getPayload()); |
| 178 | +// }catch(Exception e){ |
| 179 | +// json.put(dest.getKey().toString(),e.toString()); |
| 180 | +// } |
| 181 | +// } |
| 182 | +// } |
| 183 | + }else{ |
| 184 | + json.put("error","Expecting one of 'ping','findNode' or 'findValue'."); |
| 185 | + } |
| 186 | + |
| 187 | + ByteBuf responseContent = Unpooled.copiedBuffer(json.toString().getBytes()); |
| 188 | + FullHttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(),HttpResponseStatus.OK,responseContent); |
| 189 | + response.headers().add("Content-Length",responseContent.capacity()); |
| 190 | + response.headers().add("Content-Type","application/json"); |
| 191 | + ctx.write(response); |
| 192 | + return; |
| 193 | + } |
88 | 194 | byte[] fileData = null; |
89 | 195 | try{ |
90 | 196 | fileData = HTTPHandler.readResource(HTTPHandler.getResource(uri.getPath().substring(1))); |
|
0 commit comments