From 713d3f115773621dcd3cf3b2e824247fde049ca7 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Thu, 2 Nov 2017 14:48:43 +0100
Subject: [PATCH] Network: detect endpoint WS2PTOR - fix #593

---
 www/i18n/locale-en-GB.json                   |  2 +-
 www/i18n/locale-en.json                      |  2 +-
 www/i18n/locale-es-ES.json                   |  2 +-
 www/i18n/locale-fr-FR.json                   |  2 +-
 www/i18n/locale-nl-NL.json                   |  2 +-
 www/js/services/bma-services.js              | 23 ++++++++++++++++----
 www/js/services/network-services.js          |  4 ++--
 www/plugins/es/js/services/http-services.js  |  1 -
 www/templates/network/item_content_peer.html |  3 +++
 9 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/www/i18n/locale-en-GB.json b/www/i18n/locale-en-GB.json
index 649f01379..7ccaed362 100644
--- a/www/i18n/locale-en-GB.json
+++ b/www/i18n/locale-en-GB.json
@@ -257,7 +257,7 @@
     "VIEW": {
       "MEDIAN_TIME": "Blockchain time",
       "LOADING_PEERS": "Loading peers...",
-      "NODE_ADDRESS": "Peer address:",
+      "NODE_ADDRESS": "Address:",
       "WS2PID": "Identitifer:",
       "POW_PREFIX": "Proof of work prefix:",
       "ENDPOINTS": {
diff --git a/www/i18n/locale-en.json b/www/i18n/locale-en.json
index b3ccb2ef6..20d7f5833 100644
--- a/www/i18n/locale-en.json
+++ b/www/i18n/locale-en.json
@@ -257,7 +257,7 @@
     "VIEW": {
       "MEDIAN_TIME": "Blockchain time",
       "LOADING_PEERS": "Loading peers...",
-      "NODE_ADDRESS": "Peer address:",
+      "NODE_ADDRESS": "Address:",
       "WS2PID": "Identitifer:",
       "POW_PREFIX": "Proof of work prefix:",
       "ENDPOINTS": {
diff --git a/www/i18n/locale-es-ES.json b/www/i18n/locale-es-ES.json
index 775675a8c..a6eb539f7 100644
--- a/www/i18n/locale-es-ES.json
+++ b/www/i18n/locale-es-ES.json
@@ -257,7 +257,7 @@
     "VIEW": {
       "MEDIAN_TIME": "Hora de la blockchain",
       "LOADING_PEERS": "Carga de los nodos...",
-      "NODE_ADDRESS": "Dirección del nodo",
+      "NODE_ADDRESS": "Dirección :",
       "WS2PID": "ID :",
       "POW_PREFIX": "Préfixe de preuve de travail :",
       "ENDPOINTS": {
diff --git a/www/i18n/locale-fr-FR.json b/www/i18n/locale-fr-FR.json
index f09236dcb..62c76e56d 100644
--- a/www/i18n/locale-fr-FR.json
+++ b/www/i18n/locale-fr-FR.json
@@ -257,7 +257,7 @@
     "VIEW": {
       "MEDIAN_TIME": "Heure de la blockchain",
       "LOADING_PEERS": "Chargement des noeuds...",
-      "NODE_ADDRESS": "Adresse du noeud :",
+      "NODE_ADDRESS": "Adresse :",
       "WS2PID": "Identifiant :",
       "POW_PREFIX": "Préfixe de preuve de travail :",
       "ENDPOINTS": {
diff --git a/www/i18n/locale-nl-NL.json b/www/i18n/locale-nl-NL.json
index d1d88ddac..09d92467b 100644
--- a/www/i18n/locale-nl-NL.json
+++ b/www/i18n/locale-nl-NL.json
@@ -229,7 +229,7 @@
     "VIEW": {
       "MEDIAN_TIME": "Blockchain tijd",
       "LOADING_PEERS": "Even geduld...",
-      "NODE_ADDRESS": "knooppunt adres",
+      "NODE_ADDRESS": "Adres :",
       "ENDPOINTS": {
         "BMAS": "Endpoint (SSL)",
         "BMATOR": "Endpoint TOR",
diff --git a/www/js/services/bma-services.js b/www/js/services/bma-services.js
index 1b06ac722..481141223 100644
--- a/www/js/services/bma-services.js
+++ b/www/js/services/bma-services.js
@@ -29,8 +29,9 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
         URI_WITH_PATH: "duniter://([a-zA-Z0-9-.]+.[a-zA-Z0-9-_:.]+)/([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{43,44})(?:/([A-Za-z0-9_-]+))?",
         BMA_ENDPOINT: "BASIC_MERKLED_API" + REGEX_ENDPOINT_PARAMS,
         BMAS_ENDPOINT: "BMAS" + REGEX_ENDPOINT_PARAMS,
-        WS2P_ENDPOINT: "WS2P ([0-9a-z]+)"+ REGEX_ENDPOINT_PARAMS,
-        BMATOR_ENDPOINT: "BMATOR" + "( ([a-z0-9-_.]+.onion))( ([0-9]+))?"
+        WS2P_ENDPOINT: "WS2P ([a-f0-9]{8})"+ REGEX_ENDPOINT_PARAMS,
+        BMATOR_ENDPOINT: "BMATOR ([a-z0-9-_.]*|[0-9.]+|[0-9a-f:]+.onion)(?: ([0-9]+))?",
+        WS2PTOR_ENDPOINT: "WS2PTOR ([a-f0-9]{8}) ([a-z0-9-_.]*|[0-9.]+|[0-9a-f:]+.onion)(?: ([0-9]+))?(?: (.+))?"
       },
       errorCodes = {
         REVOCATION_ALREADY_REGISTERED: 1002,
@@ -352,6 +353,7 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
         BMAS_ENDPOINT: exact(regexp.BMAS_ENDPOINT),
         WS2P_ENDPOINT: exact(regexp.WS2P_ENDPOINT),
         BMATOR_ENDPOINT: exact(regexp.BMATOR_ENDPOINT),
+        WS2PTOR_ENDPOINT: exact(regexp.WS2PTOR_ENDPOINT),
         // TX output conditions
         TX_OUTPUT_SIG: exact(SIG),
         TX_OUTPUT_FUNCTION: test(OUTPUT_FUNCTION),
@@ -553,8 +555,8 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
       matches = exports.regexp.BMATOR_ENDPOINT.exec(endpoint);
       if (matches) {
         return {
-          "dns": matches[2] || '',
-          "port": matches[4] || 80,
+          "dns": matches[1] || '',
+          "port": matches[2] || 80,
           "useSsl": false,
           "useTor": true,
           "useBma": true
@@ -574,6 +576,19 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
           "useWs2p": true
         };
       }
+      // Try WS2PTOR
+      matches = exports.regexp.WS2PTOR_ENDPOINT.exec(endpoint);
+      if (matches) {
+        return {
+          "ws2pid": matches[1] || '',
+          "dns": matches[2] || '',
+          "port": matches[3] || 80,
+          "path": matches[4] || '',
+          "useSsl": false,
+          "useTor": true,
+          "useWs2p": true
+        };
+      }
     };
 
     exports.copy = function(otherNode) {
diff --git a/www/js/services/network-services.js b/www/js/services/network-services.js
index 3c06c4bcd..d196327ca 100644
--- a/www/js/services/network-services.js
+++ b/www/js/services/network-services.js
@@ -1,7 +1,7 @@
 
 angular.module('cesium.network.services', ['ngApi', 'cesium.bma.services', 'cesium.http.services'])
 
-.factory('csNetwork', function($rootScope, $q, $interval, $timeout, $window, BMA, csHttp, Api, csCurrency) {
+.factory('csNetwork', function($rootScope, $q, $interval, $timeout, $window, BMA, csHttp, Api) {
   'ngInject';
 
   factory = function(id) {
@@ -601,7 +601,7 @@ angular.module('cesium.network.services', ['ngApi', 'cesium.bma.services', 'cesi
               $timeout(function() {
                 console.debug('[network] new block received by WS: will refresh peers');
                 loadPeers();
-              }, 2000);
+              }, 2000, false /*invokeApply*/);
             }
           }
         });
diff --git a/www/plugins/es/js/services/http-services.js b/www/plugins/es/js/services/http-services.js
index cbeb910a4..4d36329d6 100644
--- a/www/plugins/es/js/services/http-services.js
+++ b/www/plugins/es/js/services/http-services.js
@@ -248,7 +248,6 @@ angular.module('cesium.es.http.services', ['ngResource', 'ngApi', 'cesium.servic
         .then(function (confirm) {
           if (!confirm) return false; // stop the loop
 
-          UIUtils.loading.show();
           that.cleanCache();
 
           that.init(fallbackNode.host, fallbackNode.port, fallbackNode.wsPort, fallbackNode.useSsl || fallbackNode.port == 443);
diff --git a/www/templates/network/item_content_peer.html b/www/templates/network/item_content_peer.html
index 129e85aca..e46bbc7e8 100644
--- a/www/templates/network/item_content_peer.html
+++ b/www/templates/network/item_content_peer.html
@@ -43,6 +43,9 @@
         <div ng-if=":rebind:!peer.isWs2p()&&peer.isTor()" ng-click="showEndpointsPopover($event, peer, 'BMATOR')">
           <i class="ion-bma-tor-api"></i>
         </div>
+        <div ng-if=":rebind:peer.isWs2p()&&peer.isTor()" ng-click="showWs2pPopover($event, peer)">
+          <i class="ion-bma-tor-api"></i>
+        </div>
       </div>
       <div class="col col-20 no-padding text-center" ng-if="::expertMode && search.type != 'offline'">
         <h3 class="hidden-sm hidden-xs gray">
-- 
GitLab