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

[fix] Change Peer.api to string (to be able to extend the API list - e.g. for GCHANGE_API)

parent 6c2738de
No related branches found
No related tags found
No related merge requests found
Pipeline #9515 passed
Showing
with 51 additions and 39 deletions
......@@ -40,7 +40,9 @@ public class Endpoints {
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]+))(?: (/[^/]*))?$";
// Path regexp (can have no starting slash - see issue https://git.duniter.org/clients/cesium-grp/cesium-plus-pod/-/issues/41)
public static final String PATH_REGEXP = "\\/?[^\\/\\s]+(?:\\/[^\\/\\s]+)*";
public static final String EP_END_REGEXP = "(?: ([a-z0-9_ğĞ][a-z0-9-_.ğĞ]*))?(?: ([0-9.]+))?(?: ([0-9a-f:]+)(?:%[a-z0-9]+)?)?(?: ([0-9]+))(?: ("+PATH_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 WS2P_API_REGEXP = "^(WS2P(?:TOR)?) ([a-f0-9]{7,8})" + EP_END_REGEXP;
......@@ -63,7 +65,7 @@ public class Endpoints {
// BMA API
Matcher mather = bmaPattern.matcher(raw);
if (mather.matches()) {
endpoint.api = EndpointApi.BASIC_MERKLED_API;
endpoint.api = EndpointApi.BASIC_MERKLED_API.name();
parseDefaultFormatEndPoint(mather, endpoint, 1);
return Optional.of(endpoint);
}
......@@ -71,7 +73,7 @@ public class Endpoints {
// BMAS API
mather = bmasPattern.matcher(raw);
if (mather.matches()) {
endpoint.api = EndpointApi.BMAS;
endpoint.api = EndpointApi.BMAS.name();
parseDefaultFormatEndPoint(mather, endpoint, 1);
return Optional.of(endpoint);
}
......@@ -81,7 +83,7 @@ public class Endpoints {
if (mather.matches()) {
String api = mather.group(1);
try {
endpoint.api = EndpointApi.valueOf(api);
endpoint.api = EndpointApi.valueOf(api).name();
endpoint.id = mather.group(2);
parseDefaultFormatEndPoint(mather, endpoint, 3);
return Optional.of(endpoint);
......@@ -96,7 +98,7 @@ public class Endpoints {
if (mather.matches()) {
String api = mather.group(1);
try {
endpoint.api = EndpointApi.valueOf(api);
endpoint.api = api;
parseDefaultFormatEndPoint(mather, endpoint, 2);
return Optional.of(endpoint);
} catch(Exception e) {
......@@ -120,9 +122,13 @@ public class Endpoints {
endpoint.ipv6 = word;
} else if ((i == matcher.groupCount() || i == matcher.groupCount() -1) && word.matches("^\\d+$")){
endpoint.port = Integer.parseInt(word);
} else if (i == matcher.groupCount() && word.startsWith("/")) {
endpoint.path = word;
} else {
} else if (i == matcher.groupCount()) {
// Path without starting slash must e accepted - fix issue https://git.duniter.org/clients/cesium-grp/cesium-plus-pod/-/issues/41
if (word.startsWith("/") || word.matches(PATH_REGEXP)) {
endpoint.path = word;
}
}
else {
endpoint.dns = word;
}
}
......
......@@ -138,7 +138,7 @@ public class NetworkPeering implements Serializable {
}
public static class Endpoint implements Serializable {
public EndpointApi api;
public String api;
public String dns;
public String ipv4;
public String ipv6;
......@@ -147,11 +147,11 @@ public class NetworkPeering implements Serializable {
public String path;
public String raw;
public EndpointApi getApi() {
public String getApi() {
return api;
}
public void setApi(EndpointApi api) {
public void setApi(String api) {
this.api = api;
}
......@@ -220,7 +220,7 @@ public class NetworkPeering implements Serializable {
StringJoiner joiner = new StringJoiner(" ");
// API
if (api != null) {
joiner.add(api.name());
joiner.add(api);
}
// Id (use for WS2P)
if (StringUtils.isNotBlank(id)) {
......
......@@ -128,7 +128,7 @@ public class Peer implements LocalEntity<String>, Serializable {
public Builder setEndpoint(NetworkPeering.Endpoint source) {
Preconditions.checkNotNull(source);
if (source.api != null) {
setApi(source.api.name());
setApi(source.api);
}
if (StringUtils.isNotBlank(source.id)) {
setEpId(source.id);
......
......@@ -191,7 +191,7 @@ public final class Peers {
public static NetworkPeering.Endpoint toBmaEndpoint(Peer ep) {
NetworkPeering.Endpoint bmaEp = new NetworkPeering.Endpoint();
bmaEp.setApi(EndpointApi.valueOf(ep.getApi()));
bmaEp.setApi(ep.getApi());
bmaEp.setId(ep.getEpId());
bmaEp.setDns(ep.getDns());
bmaEp.setPort(ep.getPort());
......
......@@ -44,7 +44,7 @@ public interface NetworkRemoteService extends Service {
List<Ws2pHead> getWs2pHeads(Peer peer);
List<Peer> findPeers(Peer peer, String status, EndpointApi endpointApi, Integer currentBlockNumber, String currentBlockHash);
List<Peer> findPeers(Peer peer, String status, String endpointApi, Integer currentBlockNumber, String currentBlockHash);
WebsocketClientEndpoint addPeerListener(String currencyId, WebsocketClientEndpoint.MessageListener listener, boolean autoReconnect);
......
......@@ -126,7 +126,7 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N
}
@Override
public List<Peer> findPeers(Peer peer, String status, EndpointApi endpointApi, Integer currentBlockNumber, String currentBlockHash) {
public List<Peer> findPeers(Peer peer, String status, String endpointApi, Integer currentBlockNumber, String currentBlockHash) {
Preconditions.checkNotNull(peer);
List<Peer> result = Lists.newArrayList();
......@@ -142,7 +142,7 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N
for (NetworkPeering.Endpoint endpoint : remotePeer.endpoints) {
match = endpointApi == null || (endpoint != null && endpointApi == endpoint.api);
match = endpointApi == null || (endpoint != null && endpointApi.equals(endpoint.api));
if (match && endpoint != null) {
Peer childPeer = Peer.newBuilder()
......
......@@ -11,7 +11,7 @@ public class EndpointsTest {
// Parse valid endpoints
NetworkPeering.Endpoint ep = Endpoints.parse("BASIC_MERKLED_API g1.duniter.fr 81.81.81.81 80").orElse(null);
Assert.assertNotNull(ep);
Assert.assertEquals(EndpointApi.BASIC_MERKLED_API, ep.api);
Assert.assertEquals(EndpointApi.BASIC_MERKLED_API.name(), ep.api);
Assert.assertEquals("g1.duniter.fr", ep.dns);
Assert.assertEquals("81.81.81.81", ep.ipv4);
Assert.assertNotNull(ep.port);
......@@ -19,28 +19,37 @@ public class EndpointsTest {
Assert.assertNull(ep.id);
Assert.assertNull(ep.path);
ep = Endpoints.parse("BMAS g1.duniter.fr 443").orElse(null);
ep = Endpoints.parse("BMAS g1.duniter.org 443").orElse(null);
Assert.assertNotNull(ep);
Assert.assertEquals(EndpointApi.BMAS, ep.api);
Assert.assertEquals("g1.duniter.fr", ep.dns);
Assert.assertEquals(EndpointApi.BMAS.name(), ep.api);
Assert.assertEquals("g1.duniter.org", ep.dns);
Assert.assertNotNull(ep.port);
Assert.assertEquals(443, ep.port.intValue());
Assert.assertNull(ep.id);
Assert.assertNull(ep.path);
ep = Endpoints.parse("WS2P fb17fcd4 g1.duniter.fr 443 /ws2p").orElse(null);
ep = Endpoints.parse("WS2P fb17fcd4 g1.duniter.org 443 /ws2p").orElse(null);
Assert.assertNotNull(ep);
Assert.assertNotNull("fb17fcd4", ep.id);
Assert.assertEquals("g1.duniter.org", ep.dns);
Assert.assertEquals(EndpointApi.WS2P.name(), ep.api);
Assert.assertNotNull(ep.port);
Assert.assertEquals(443, ep.port.intValue());
Assert.assertEquals("/ws2p", ep.path);
ep = Endpoints.parse("WS2P fb17fcd4 g1.duniter.org 443 ws2p").orElse(null);
Assert.assertNotNull(ep);
Assert.assertNotNull(ep.id);
Assert.assertNotNull(ep.path);
Assert.assertEquals(EndpointApi.WS2P, ep.api);
Assert.assertEquals("g1.duniter.fr", ep.dns);
Assert.assertEquals(EndpointApi.WS2P.name(), ep.api);
Assert.assertEquals("g1.duniter.org", ep.dns);
Assert.assertNotNull(ep.port);
Assert.assertEquals(443, ep.port.intValue());
Assert.assertEquals("ws2p", ep.path);
// ws2pId on 7 characters
ep = Endpoints.parse("WS2P 90e9b12 duniter.g1.1000i100.fr 443 /ws2p").orElse(null);
Assert.assertNotNull(ep);
Assert.assertEquals(ep.api, EndpointApi.WS2P);
Assert.assertEquals(ep.api, EndpointApi.WS2P.name());
Assert.assertNotNull(ep.id);
Assert.assertNotNull(ep.path);
......@@ -48,14 +57,14 @@ public class EndpointsTest {
Assert.assertNotNull(ep);
Assert.assertNotNull(ep.id);
Assert.assertNull(ep.path);
Assert.assertEquals(EndpointApi.WS2PTOR, ep.api);
Assert.assertEquals(EndpointApi.WS2PTOR.name(), 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").orElse(null);
Assert.assertNotNull(ep);
Assert.assertEquals(ep.api, EndpointApi.GCHANGE_API);
Assert.assertEquals(ep.api, EndpointApi.GCHANGE_API.name());
Assert.assertNull(ep.id);
Assert.assertNull(ep.path);
......@@ -69,9 +78,6 @@ public class EndpointsTest {
ep = Endpoints.parse("WS2P R8t2sg7w g1.ambau.ovh 443").orElse(null);
Assert.assertNull(ep);
// This must failed (missing path first '/')
ep = Endpoints.parse("WS2P 90e9b12 duniter.g1.1000i100.fr 443 ws2p").orElse(null);
Assert.assertNull(ep);
}
}
......@@ -21,12 +21,12 @@ public class NetworkPeeringTest {
peering.setStatus("UP");
NetworkPeering.Endpoint epBma = new NetworkPeering.Endpoint();
epBma.setApi(EndpointApi.BASIC_MERKLED_API);
epBma.setApi(EndpointApi.BASIC_MERKLED_API.name());
epBma.setDns("g1.duniter.fr");
epBma.setPort(80);
NetworkPeering.Endpoint epWs2p = new NetworkPeering.Endpoint();
epWs2p.setApi(EndpointApi.WS2P);
epWs2p.setApi(EndpointApi.WS2P.name());
epWs2p.setDns("g1.duniter.fr");
epWs2p.setPath("/ws2p");
epWs2p.setId("fb17fcd4");
......
......@@ -70,7 +70,7 @@ public class NetworkRemoteServiceTest {
@Test
public void findPeers() throws Exception {
List<Peer> result = service.findPeers(peer, null, EndpointApi.BASIC_MERKLED_API, null, null);
List<Peer> result = service.findPeers(peer, null, EndpointApi.BASIC_MERKLED_API.name(), null, null);
Assert.assertNotNull(result);
Assert.assertTrue(result.size() > 0);
......@@ -94,9 +94,7 @@ public class NetworkRemoteServiceTest {
Assert.assertTrue(result.size() > 0);
// log
//result.stream().forEach(head ->
// System.out.println(head.toString())
//);
result.stream().forEach(head -> log.debug(head.toString()));
}
/* -- internal methods */
......
......@@ -59,6 +59,8 @@ public class NetworkServiceTest {
Assert.assertNotNull(peers);
Assert.assertTrue(peers.size() > 0);
peers.forEach(p -> log.debug(" Found peer: " + p.toString()));
}
/* -- internal methods */
......
duniter4j.node.host=g1-test.duniter.org
duniter4j.node.port=443
duniter4j.node.port=10900
duniter4j.node.elasticsearch.host=localhost
duniter4j.node.elasticsearch.port=9200
......@@ -18,7 +18,7 @@ log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c - %m%n
# duniter4j levels
log4j.logger.org.duniter=INFO
#log4j.logger.org.duniter.core.client.service=DEBUG
log4j.logger.org.duniter.core.client.service=DEBUG
#log4j.logger.org.duniter.core.client.service.bma=DEBUG
log4j.logger.org.duniter.core.beans=WARN
#log4j.logger.org.duniter.core.client.service=TRACE
......
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