Commit b5d25dc9 authored by Benoit Lavenier's avatar Benoit Lavenier

[enh] Add endpoint API: WS2PTOR

[fix] Fix parsing of WS2P endpoint
parent c1a81941
Pipeline #4611 passed with stage
in 31 seconds
......@@ -7,6 +7,7 @@
.classpath
.settings
.idea
.local
target
*/target
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
cd ..
echo "***************************************"
echo " Compiling duniter4j.core-client... "
echo " Compiling core-client... "
# Remove old JAR
rm duniter4j-core-client/target/*.jar
......
......@@ -29,6 +29,7 @@ public enum EndpointApi {
BMAS,
BMATOR,
WS2P,
WS2PTOR,
ES_CORE_API,
ES_USER_API,
ES_SUBSCRIPTION_API,
......
......@@ -24,6 +24,8 @@ package org.duniter.core.client.model.bma;
import org.duniter.core.util.StringUtils;
import org.duniter.core.util.http.InetAddressUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.regex.Matcher;
......@@ -34,10 +36,13 @@ import java.util.regex.Pattern;
*/
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 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;
private static Pattern bmaPattern = Pattern.compile(BMA_API_REGEXP);
......@@ -49,12 +54,13 @@ public class Endpoints {
// 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();
endpoint.setRaw(raw);
// BMA API
Matcher mather = bmaPattern.matcher(ept);
Matcher mather = bmaPattern.matcher(raw);
if (mather.matches()) {
endpoint.api = EndpointApi.BASIC_MERKLED_API;
parseDefaultFormatEndPoint(mather, endpoint, 1);
......@@ -62,7 +68,7 @@ public class Endpoints {
}
// BMAS API
mather = bmasPattern.matcher(ept);
mather = bmasPattern.matcher(raw);
if (mather.matches()) {
endpoint.api = EndpointApi.BMAS;
parseDefaultFormatEndPoint(mather, endpoint, 1);
......@@ -70,16 +76,22 @@ public class Endpoints {
}
// WS2P API
mather = ws2pPattern.matcher(ept);
mather = ws2pPattern.matcher(raw);
if (mather.matches()) {
endpoint.api = EndpointApi.WS2P;
endpoint.id = mather.group(1);
parseDefaultFormatEndPoint(mather, endpoint, 2);
return endpoint;
String api = mather.group(1);
try {
endpoint.api = EndpointApi.valueOf(api);
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
mather = otherApiPattern.matcher(ept);
mather = otherApiPattern.matcher(raw);
if (mather.matches()) {
String api = mather.group(1);
try {
......@@ -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) {
......
......@@ -29,6 +29,7 @@ import org.duniter.core.util.StringUtils;
import java.io.Serializable;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Stream;
/**
* Created by eis on 05/02/15.
......@@ -110,6 +111,8 @@ public class NetworkPeering implements Serializable {
}
public String toString() {
if (StringUtils.isNotBlank(raw)) return raw;
StringBuilder sb = new StringBuilder();
// Version
sb.append("Version: ").append(Protocol.VERSION).append("\n");
......@@ -124,9 +127,9 @@ public class NetworkPeering implements Serializable {
// Endpoints
sb.append("Endpoints:\n");
if (CollectionUtils.isNotEmpty(endpoints)) {
for (Endpoint ep: endpoints) {
sb.append(ep.toString()).append("\n");
}
Stream.of(endpoints)
.filter(Objects::nonNull) // can be null
.forEach(ep -> sb.append(ep.toString()).append("\n"));
}
if (StringUtils.isNotBlank(signature)) {
sb.append(signature).append("\n");
......@@ -142,6 +145,7 @@ public class NetworkPeering implements Serializable {
public Integer port;
public String id;
public String path;
public String raw;
public EndpointApi getApi() {
return api;
......@@ -199,15 +203,25 @@ public class NetworkPeering implements Serializable {
this.path = path;
}
@JsonIgnore
public String getRaw() {
return raw;
}
@JsonIgnore
public void setRaw(String raw) {
this.raw = raw;
}
@Override
public String toString() {
if (raw != null) return raw;
StringJoiner joiner = new StringJoiner(" ");
// API
if (api != null) {
joiner.add(api.name());
}
// Id (use for WS2P)
if (StringUtils.isNotBlank(id)) {
joiner.add(id);
......
......@@ -90,6 +90,8 @@ public class NetworkPeerings {
result.setStatus("UP");
result.setRaw(document);
return result;
}
catch(Exception e) {
......
......@@ -63,5 +63,11 @@ public class NetworkPeers implements Serializable {
public void setLastTry(Long lastTry) {
this.lastTry = lastTry;
}
@Override
@JsonIgnore
public String getRaw() {
return super.getRaw();
}
}
}
......@@ -58,7 +58,7 @@ public class EndpointDeserializer extends JsonDeserializer<NetworkPeering.Endpoi
log.warn(e.getMessage(), e); // link the exception
}
else {
log.debug(e.getMessage());
log.warn(e.getMessage());
}
return null;
}
......
......@@ -36,6 +36,29 @@ public class EndpointsTest {
Assert.assertNotNull(ep.port);
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");
Assert.assertNotNull(ep);
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 {
Assert.assertNotNull(peering);
Assert.assertEquals("g1", peering.getCurrency());
Assert.assertEquals("10", peering.getVersion());
Assert.assertEquals(new Integer(10), peering.getVersion());
Assert.assertEquals("38MEAZN68Pz1DTvT3tqgxx4yQP6snJCQhPqEFxbDk4aE", peering.getPubkey());
Assert.assertEquals("162694-0000067CAF81B13E4BD7AE72A06F9981D80EB957E4D46C23A67B4DF734E258ED", peering.getBlock());
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment