From 7625c2a13b3def24f8540fd21f93f77dfd69d295 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Thu, 7 Sep 2017 17:41:59 +0200
Subject: [PATCH] [enh] #1084 WS2P: add WS2P handling in PeerDTO + database
 search

---
 app/lib/common-libs/constants.ts |  2 ++
 app/lib/dal/fileDAL.ts           |  4 ++++
 app/lib/dal/sqliteDAL/PeerDAL.ts |  4 ++++
 app/lib/dto/ConfDTO.ts           | 11 ++++++++++-
 app/lib/dto/PeerDTO.ts           | 23 ++++++++++++++++++++++-
 5 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/app/lib/common-libs/constants.ts b/app/lib/common-libs/constants.ts
index 28bbae7e1..b3c263e79 100644
--- a/app/lib/common-libs/constants.ts
+++ b/app/lib/common-libs/constants.ts
@@ -27,6 +27,7 @@ const UNLOCK       = "(SIG\\(" + INTEGER + "\\)|XHX\\(" + XUNLOCK + "\\))"
 const CONDITIONS   = "(&&|\\|\\|| |[()]|(SIG\\(" + PUBKEY + "\\)|(XHX\\([A-F0-9]{64}\\)|CLTV\\(" + CLTV_INTEGER + "\\)|CSV\\(" + CSV_INTEGER + "\\))))*"
 
 const BMA_REGEXP  = /^BASIC_MERKLED_API( ([a-z_][a-z0-9-_.]*))?( ([0-9.]+))?( ([0-9a-f:]+))?( ([0-9]+))$/
+const WS2P_REGEXP = /^WS2P ([a-f0-9]{8}) ([a-z_][a-z0-9-_.]*|[0-9.]+|[0-9a-f:]+) ([0-9]+)$/
 
 const MAXIMUM_LEN_OF_COMPACT_TX = 100
 const MAXIMUM_LEN_OF_OUTPUT = 2000
@@ -76,6 +77,7 @@ export const CommonConstants = {
   SWITCH_ON_BRANCH_AHEAD_BY_X_BLOCKS: 3,
 
   BMA_REGEXP,
+  WS2P_REGEXP,
   PUBLIC_KEY: exact(PUBKEY),
   INTEGER: /^\d+$/,
   BASE58: exact(BASE58),
diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts
index 50fca302b..087623580 100644
--- a/app/lib/dal/fileDAL.ts
+++ b/app/lib/dal/fileDAL.ts
@@ -141,6 +141,10 @@ export class FileDAL {
     }
   }
 
+  async getWS2Peers() {
+    return this.peerDAL.getPeersWithEndpointsLike('WS2P ')
+  }
+
   async getBlock(number:number) {
     const block = await this.blockDAL.getBlock(number)
     return block || null;
diff --git a/app/lib/dal/sqliteDAL/PeerDAL.ts b/app/lib/dal/sqliteDAL/PeerDAL.ts
index 460330342..d38558fe6 100644
--- a/app/lib/dal/sqliteDAL/PeerDAL.ts
+++ b/app/lib/dal/sqliteDAL/PeerDAL.ts
@@ -93,6 +93,10 @@ export class PeerDAL extends AbstractSQLite<DBPeer> {
     return this.sqlFindOne({ pubkey: pubkey })
   }
 
+  getPeersWithEndpointsLike(str:string) {
+    return this.query('SELECT * FROM peer WHERE endpoints LIKE ?', ['%' + str + '%'])
+  }
+
   savePeer(peer:DBPeer) {
     return this.saveEntity(peer)
   }
diff --git a/app/lib/dto/ConfDTO.ts b/app/lib/dto/ConfDTO.ts
index f8db265cb..acaf52cde 100644
--- a/app/lib/dto/ConfDTO.ts
+++ b/app/lib/dto/ConfDTO.ts
@@ -60,6 +60,7 @@ export interface NetworkConfDTO {
 
 export interface WS2PConfDTO {
   ws2p?: {
+    uuid: string
     upnp?: boolean
     remotehost?: string|null
     remoteport?: number|null
@@ -68,7 +69,7 @@ export interface WS2PConfDTO {
   }
 }
 
-export class ConfDTO implements CurrencyConfDTO, KeypairConfDTO, NetworkConfDTO, BranchingDTO {
+export class ConfDTO implements CurrencyConfDTO, KeypairConfDTO, NetworkConfDTO, BranchingDTO, WS2PConfDTO {
 
   constructor(
     public loglevel: string,
@@ -124,6 +125,14 @@ export class ConfDTO implements CurrencyConfDTO, KeypairConfDTO, NetworkConfDTO,
     public upnp: boolean,
     public homename: string,
     public memory: boolean,
+    public ws2p?: {
+      uuid: string
+      upnp?: boolean
+      remotehost?: string|null
+      remoteport?: number|null
+      port?: number
+      host?: string
+    }
 ) {}
 
   static mock() {
diff --git a/app/lib/dto/PeerDTO.ts b/app/lib/dto/PeerDTO.ts
index d6df0f8b0..c19b4aed3 100644
--- a/app/lib/dto/PeerDTO.ts
+++ b/app/lib/dto/PeerDTO.ts
@@ -3,6 +3,12 @@ import {hashf} from "../common"
 import {CommonConstants} from "../common-libs/constants"
 import {Cloneable} from "./Cloneable"
 
+export interface WS2PEndpoint {
+  uuid:string
+  host:string
+  port:number
+}
+
 export class PeerDTO implements Cloneable {
 
   clone(): any {
@@ -86,7 +92,22 @@ export class PeerDTO implements Cloneable {
       }
     });
     return bma || {};
-  };
+  }
+
+  getWS2P() {
+    let api:any = null;
+    this.endpoints.forEach((ep) => {
+      const matches = !api && ep.match(CommonConstants.WS2P_REGEXP)
+      if (matches) {
+        api = {
+          uuid: matches[1],
+          host: matches[2] || '',
+          port: parseInt(matches[3]) || 0
+        }
+      }
+    })
+    return api || {}
+  }
 
   getDns() {
     const bma = this.getBMA();
-- 
GitLab