Skip to content
Snippets Groups Projects
Commit b5d25dc9 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[enh] Add endpoint API: WS2PTOR

[fix] Fix parsing of WS2P endpoint
parent c1a81941
No related branches found
No related tags found
No related merge requests found
Pipeline #4611 passed
Showing
with 185 additions and 19 deletions
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
.classpath .classpath
.settings .settings
.idea .idea
.local
target target
*/target */target
duniter4j.iml duniter4j.iml
......
#!/bin/bash
set JAVA_HOME=/usr/lib/jvm/java-8-oracle
CESIUM_PLUS_POD_DIR="${HOME}/git/duniter/cesium-plus-pod"
DEPLOY_DIR="${CESIUM_PLUS_POD_DIR}/cesium-plus-pod-assembly/target/es-run-home/plugins/cesium-plus-pod-core"
# Go to project root
cd ..
ROOT=`pwd`
echo "***************************************"
echo " Compiling core-* ... "
# Remove old JAR
rm duniter4j-core-client/target/*.jar
rm duniter4j-core-shared/target/*.jar
# Compile the core-client
mvn install --quiet -DskipTests
if [[ $? -ne 0 ]]; then
exit 1
fi
echo " Successfully compiled ! "
echo "***************************************"
echo " Installing into Cesium+ pod (target assembly)... "
# Copy jar
mkdir -p ${DEPLOY_DIR}
if [[ $? -ne 0 ]]; then
exit 1
fi
rm -f "${DEPLOY_DIR}/duniter4j-core-client-*.jar"
rm -f "${DEPLOY_DIR}/duniter4j-core-shared-*.jar"
if [[ $? -ne 0 ]]; then
exit 1
fi
cd ${ROOT}/duniter4j-core-client/target/
JAR_FILE=`ls *.jar`
cp -v ${JAR_FILE} ${DEPLOY_DIR}/
if [[ $? -ne 0 ]]; then
exit 1
fi
cd ${ROOT}/duniter4j-core-shared/target/
JAR_FILE=`ls *.jar`
cp -v ${JAR_FILE} ${DEPLOY_DIR}/
if [[ $? -ne 0 ]]; then
exit 1
fi
echo " Successfully deployed !"
echo "************************"
...@@ -8,7 +8,7 @@ DEPLOY_DIR="${CESIUM_PLUS_POD_DIR}/cesium-plus-pod-assembly/target/es-run-home/p ...@@ -8,7 +8,7 @@ DEPLOY_DIR="${CESIUM_PLUS_POD_DIR}/cesium-plus-pod-assembly/target/es-run-home/p
cd .. cd ..
echo "***************************************" echo "***************************************"
echo " Compiling duniter4j.core-client... " echo " Compiling core-client... "
# Remove old JAR # Remove old JAR
rm duniter4j-core-client/target/*.jar rm duniter4j-core-client/target/*.jar
......
...@@ -29,6 +29,7 @@ public enum EndpointApi { ...@@ -29,6 +29,7 @@ public enum EndpointApi {
BMAS, BMAS,
BMATOR, BMATOR,
WS2P, WS2P,
WS2PTOR,
ES_CORE_API, ES_CORE_API,
ES_USER_API, ES_USER_API,
ES_SUBSCRIPTION_API, ES_SUBSCRIPTION_API,
......
...@@ -24,6 +24,8 @@ package org.duniter.core.client.model.bma; ...@@ -24,6 +24,8 @@ package org.duniter.core.client.model.bma;
import org.duniter.core.util.StringUtils; import org.duniter.core.util.StringUtils;
import org.duniter.core.util.http.InetAddressUtils; import org.duniter.core.util.http.InetAddressUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -34,10 +36,13 @@ import java.util.regex.Pattern; ...@@ -34,10 +36,13 @@ import java.util.regex.Pattern;
*/ */
public class Endpoints { public class Endpoints {
public static final String EP_END_REGEXP = "(?: ([a-z_][a-z0-9-_.ğĞ]*))?(?: ([0-9.]+))?(?: ([0-9a-f:]+))?(?: ([0-9]+))(?: (/[^/]+))?$";
private static final Logger log = LoggerFactory.getLogger(Endpoints.class);
public static final String EP_END_REGEXP = "(?: ([a-z0-9_ğĞ][a-z0-9-_.ğĞ]*))?(?: ([0-9.]+))?(?: ([0-9a-f:]+)(?:%[a-z0-9]+)?)?(?: ([0-9]+))(?: (/[^/]*))?$";
public static final String BMA_API_REGEXP = "^BASIC_MERKLED_API" + EP_END_REGEXP; public static final String BMA_API_REGEXP = "^BASIC_MERKLED_API" + EP_END_REGEXP;
public static final String BMAS_API_REGEXP = "^BMAS" + EP_END_REGEXP; public static final String BMAS_API_REGEXP = "^BMAS" + EP_END_REGEXP;
public static final String WS2P_API_REGEXP = "^WS2P ([a-f0-9]{8})" + EP_END_REGEXP; public static final String WS2P_API_REGEXP = "^(WS2P(?:TOR)?) ([a-f0-9]{7,8})" + EP_END_REGEXP;
public static final String OTHER_API_REGEXP = "^([A-Z_-]+)" + EP_END_REGEXP; public static final String OTHER_API_REGEXP = "^([A-Z_-]+)" + EP_END_REGEXP;
private static Pattern bmaPattern = Pattern.compile(BMA_API_REGEXP); private static Pattern bmaPattern = Pattern.compile(BMA_API_REGEXP);
...@@ -49,12 +54,13 @@ public class Endpoints { ...@@ -49,12 +54,13 @@ public class Endpoints {
// helper class // helper class
} }
public static NetworkPeering.Endpoint parse(String ept) throws IOException { public static NetworkPeering.Endpoint parse(String raw) throws IOException {
NetworkPeering.Endpoint endpoint = new NetworkPeering.Endpoint(); NetworkPeering.Endpoint endpoint = new NetworkPeering.Endpoint();
endpoint.setRaw(raw);
// BMA API // BMA API
Matcher mather = bmaPattern.matcher(ept); Matcher mather = bmaPattern.matcher(raw);
if (mather.matches()) { if (mather.matches()) {
endpoint.api = EndpointApi.BASIC_MERKLED_API; endpoint.api = EndpointApi.BASIC_MERKLED_API;
parseDefaultFormatEndPoint(mather, endpoint, 1); parseDefaultFormatEndPoint(mather, endpoint, 1);
...@@ -62,7 +68,7 @@ public class Endpoints { ...@@ -62,7 +68,7 @@ public class Endpoints {
} }
// BMAS API // BMAS API
mather = bmasPattern.matcher(ept); mather = bmasPattern.matcher(raw);
if (mather.matches()) { if (mather.matches()) {
endpoint.api = EndpointApi.BMAS; endpoint.api = EndpointApi.BMAS;
parseDefaultFormatEndPoint(mather, endpoint, 1); parseDefaultFormatEndPoint(mather, endpoint, 1);
...@@ -70,16 +76,22 @@ public class Endpoints { ...@@ -70,16 +76,22 @@ public class Endpoints {
} }
// WS2P API // WS2P API
mather = ws2pPattern.matcher(ept); mather = ws2pPattern.matcher(raw);
if (mather.matches()) { if (mather.matches()) {
endpoint.api = EndpointApi.WS2P; String api = mather.group(1);
endpoint.id = mather.group(1); try {
parseDefaultFormatEndPoint(mather, endpoint, 2); endpoint.api = EndpointApi.valueOf(api);
return endpoint; endpoint.id = mather.group(2);
parseDefaultFormatEndPoint(mather, endpoint, 3);
return endpoint;
} catch(Exception e) {
// Unknown API
throw new IOException("Unable to deserialize endpoint: WS2P api [" + api + "]", e); // link the exception
}
} }
// Other API // Other API
mather = otherApiPattern.matcher(ept); mather = otherApiPattern.matcher(raw);
if (mather.matches()) { if (mather.matches()) {
String api = mather.group(1); String api = mather.group(1);
try { try {
...@@ -92,7 +104,8 @@ public class Endpoints { ...@@ -92,7 +104,8 @@ public class Endpoints {
} }
} }
return null; log.warn("Unable to parse Endpoint: " + raw);
return endpoint;
} }
public static void parseDefaultFormatEndPoint(Matcher matcher, NetworkPeering.Endpoint endpoint, int startGroup) { public static void parseDefaultFormatEndPoint(Matcher matcher, NetworkPeering.Endpoint endpoint, int startGroup) {
......
...@@ -29,6 +29,7 @@ import org.duniter.core.util.StringUtils; ...@@ -29,6 +29,7 @@ import org.duniter.core.util.StringUtils;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.stream.Stream;
/** /**
* Created by eis on 05/02/15. * Created by eis on 05/02/15.
...@@ -110,6 +111,8 @@ public class NetworkPeering implements Serializable { ...@@ -110,6 +111,8 @@ public class NetworkPeering implements Serializable {
} }
public String toString() { public String toString() {
if (StringUtils.isNotBlank(raw)) return raw;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// Version // Version
sb.append("Version: ").append(Protocol.VERSION).append("\n"); sb.append("Version: ").append(Protocol.VERSION).append("\n");
...@@ -124,9 +127,9 @@ public class NetworkPeering implements Serializable { ...@@ -124,9 +127,9 @@ public class NetworkPeering implements Serializable {
// Endpoints // Endpoints
sb.append("Endpoints:\n"); sb.append("Endpoints:\n");
if (CollectionUtils.isNotEmpty(endpoints)) { if (CollectionUtils.isNotEmpty(endpoints)) {
for (Endpoint ep: endpoints) { Stream.of(endpoints)
sb.append(ep.toString()).append("\n"); .filter(Objects::nonNull) // can be null
} .forEach(ep -> sb.append(ep.toString()).append("\n"));
} }
if (StringUtils.isNotBlank(signature)) { if (StringUtils.isNotBlank(signature)) {
sb.append(signature).append("\n"); sb.append(signature).append("\n");
...@@ -142,6 +145,7 @@ public class NetworkPeering implements Serializable { ...@@ -142,6 +145,7 @@ public class NetworkPeering implements Serializable {
public Integer port; public Integer port;
public String id; public String id;
public String path; public String path;
public String raw;
public EndpointApi getApi() { public EndpointApi getApi() {
return api; return api;
...@@ -199,15 +203,25 @@ public class NetworkPeering implements Serializable { ...@@ -199,15 +203,25 @@ public class NetworkPeering implements Serializable {
this.path = path; this.path = path;
} }
@JsonIgnore
public String getRaw() {
return raw;
}
@JsonIgnore
public void setRaw(String raw) {
this.raw = raw;
}
@Override @Override
public String toString() { public String toString() {
if (raw != null) return raw;
StringJoiner joiner = new StringJoiner(" "); StringJoiner joiner = new StringJoiner(" ");
// API // API
if (api != null) { if (api != null) {
joiner.add(api.name()); joiner.add(api.name());
} }
// Id (use for WS2P) // Id (use for WS2P)
if (StringUtils.isNotBlank(id)) { if (StringUtils.isNotBlank(id)) {
joiner.add(id); joiner.add(id);
......
...@@ -90,6 +90,8 @@ public class NetworkPeerings { ...@@ -90,6 +90,8 @@ public class NetworkPeerings {
result.setStatus("UP"); result.setStatus("UP");
result.setRaw(document);
return result; return result;
} }
catch(Exception e) { catch(Exception e) {
......
...@@ -63,5 +63,11 @@ public class NetworkPeers implements Serializable { ...@@ -63,5 +63,11 @@ public class NetworkPeers implements Serializable {
public void setLastTry(Long lastTry) { public void setLastTry(Long lastTry) {
this.lastTry = lastTry; this.lastTry = lastTry;
} }
@Override
@JsonIgnore
public String getRaw() {
return super.getRaw();
}
} }
} }
...@@ -58,7 +58,7 @@ public class EndpointDeserializer extends JsonDeserializer<NetworkPeering.Endpoi ...@@ -58,7 +58,7 @@ public class EndpointDeserializer extends JsonDeserializer<NetworkPeering.Endpoi
log.warn(e.getMessage(), e); // link the exception log.warn(e.getMessage(), e); // link the exception
} }
else { else {
log.debug(e.getMessage()); log.warn(e.getMessage());
} }
return null; return null;
} }
......
...@@ -36,6 +36,29 @@ public class EndpointsTest { ...@@ -36,6 +36,29 @@ public class EndpointsTest {
Assert.assertNotNull(ep.port); Assert.assertNotNull(ep.port);
Assert.assertEquals(443, ep.port.intValue()); Assert.assertEquals(443, ep.port.intValue());
// ws2pId on 7 characters
ep = Endpoints.parse("WS2P 90e9b12 duniter.g1.1000i100.fr 443 /ws2p");
Assert.assertNotNull(ep);
Assert.assertEquals(ep.api, EndpointApi.WS2P);
Assert.assertNotNull(ep.id);
Assert.assertNotNull(ep.path);
// path without slash
ep = Endpoints.parse("WS2P 90e9b12 duniter.g1.1000i100.fr 443 ws2p");
Assert.assertNotNull(ep);
Assert.assertEquals(ep.api, EndpointApi.WS2P);
Assert.assertNotNull(ep.id);
Assert.assertNotNull(ep.path);
ep = Endpoints.parse("WS2PTOR 1be86653 3k2zovlpihbt3j3g.onion 20901");
Assert.assertNotNull(ep);
Assert.assertNotNull(ep.id);
Assert.assertNull(ep.path);
Assert.assertEquals(EndpointApi.WS2PTOR, ep.api);
Assert.assertEquals("3k2zovlpihbt3j3g.onion", ep.dns);
Assert.assertNotNull(ep.port);
Assert.assertEquals(20901, ep.port.intValue());
ep = Endpoints.parse("GCHANGE_API data.gchange.fr 443"); ep = Endpoints.parse("GCHANGE_API data.gchange.fr 443");
Assert.assertNotNull(ep); Assert.assertNotNull(ep);
Assert.assertEquals(ep.api, EndpointApi.GCHANGE_API); Assert.assertEquals(ep.api, EndpointApi.GCHANGE_API);
......
package org.duniter.core.client.model.bma;
import org.junit.Assert;
import org.junit.Test;
public class NetworkPeeringTest {
@Test
public void toStringTest() throws Exception {
NetworkPeering peering = new NetworkPeering();
peering.setCurrency("g1");
Assert.assertNotNull(peering);
peering.setCurrency("g1");
peering.setVersion(new Integer(10));
peering.setPubkey("38MEAZN68Pz1DTvT3tqgxx4yQP6snJCQhPqEFxbDk4aE");
peering.setBlock("162694-0000067CAF81B13E4BD7AE72A06F9981D80EB957E4D46C23A67B4DF734E258ED");
peering.setSignature("U+obPZqDQ3WDDclyCrOhT80Dq/8sPZp0ng+hj4THPAaxKNQwc9cijNnfvwzSsQ/hZBJpZ6+Gzrzso+zprhNICQ==");
peering.setStatus("UP");
NetworkPeering.Endpoint epBma = new NetworkPeering.Endpoint();
epBma.setApi(EndpointApi.BASIC_MERKLED_API);
epBma.setDns("g1.duniter.fr");
epBma.setPort(80);
NetworkPeering.Endpoint epWs2p = new NetworkPeering.Endpoint();
epWs2p.setApi(EndpointApi.WS2P);
epWs2p.setDns("g1.duniter.fr");
epWs2p.setPath("/ws2p");
epWs2p.setId("fb17fcd4");
epWs2p.setPort(443);
peering.setEndpoints(new NetworkPeering.Endpoint[]{epBma, epWs2p});
String raw = peering.toString();
String expedtedRaw = "Version: 10\nType: Peer\nCurrency: g1\nPublicKey: 38MEAZN68Pz1DTvT3tqgxx4yQP6snJCQhPqEFxbDk4aE\nBlock: 162694-0000067CAF81B13E4BD7AE72A06F9981D80EB957E4D46C23A67B4DF734E258ED\nEndpoints:\nBASIC_MERKLED_API g1.duniter.fr 80\nWS2P fb17fcd4 g1.duniter.fr 443 /ws2p\nU+obPZqDQ3WDDclyCrOhT80Dq/8sPZp0ng+hj4THPAaxKNQwc9cijNnfvwzSsQ/hZBJpZ6+Gzrzso+zprhNICQ==\n";
Assert.assertEquals(expedtedRaw, raw);
}
}
...@@ -14,7 +14,7 @@ public class NetworkPeeringsTest { ...@@ -14,7 +14,7 @@ public class NetworkPeeringsTest {
Assert.assertNotNull(peering); Assert.assertNotNull(peering);
Assert.assertEquals("g1", peering.getCurrency()); Assert.assertEquals("g1", peering.getCurrency());
Assert.assertEquals("10", peering.getVersion()); Assert.assertEquals(new Integer(10), peering.getVersion());
Assert.assertEquals("38MEAZN68Pz1DTvT3tqgxx4yQP6snJCQhPqEFxbDk4aE", peering.getPubkey()); Assert.assertEquals("38MEAZN68Pz1DTvT3tqgxx4yQP6snJCQhPqEFxbDk4aE", peering.getPubkey());
Assert.assertEquals("162694-0000067CAF81B13E4BD7AE72A06F9981D80EB957E4D46C23A67B4DF734E258ED", peering.getBlock()); Assert.assertEquals("162694-0000067CAF81B13E4BD7AE72A06F9981D80EB957E4D46C23A67B4DF734E258ED", peering.getBlock());
Assert.assertEquals("U+obPZqDQ3WDDclyCrOhT80Dq/8sPZp0ng+hj4THPAaxKNQwc9cijNnfvwzSsQ/hZBJpZ6+Gzrzso+zprhNICQ==", peering.getSignature()); Assert.assertEquals("U+obPZqDQ3WDDclyCrOhT80Dq/8sPZp0ng+hj4THPAaxKNQwc9cijNnfvwzSsQ/hZBJpZ6+Gzrzso+zprhNICQ==", peering.getSignature());
......
# Version v1.2.10
## Network
- Add endpoint API: WS2PTOR
- Fix parsing of WS2P endpoint
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment