From 0db9c53c4831ee0d0e1d8101c627a9affd0bc995 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Wed, 4 Oct 2017 11:55:23 +0200
Subject: [PATCH] =?UTF-8?q?[fix]=C2=A0#1129=20Allow=20additional=20`path`?=
 =?UTF-8?q?=20parameter=20for=20WS2P?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/lib/dto/ConfDTO.ts              | 2 ++
 app/modules/ws2p/index.ts           | 8 +++++++-
 app/modules/ws2p/lib/WS2PCluster.ts | 5 ++++-
 test/fast/modules/ws2p/host.ts      | 6 ++++--
 4 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/app/lib/dto/ConfDTO.ts b/app/lib/dto/ConfDTO.ts
index f2533f0cf..361eb49c9 100644
--- a/app/lib/dto/ConfDTO.ts
+++ b/app/lib/dto/ConfDTO.ts
@@ -67,6 +67,7 @@ export interface WS2PConfDTO {
     upnp?: boolean
     remotehost?: string|null
     remoteport?: number|null
+    remotepath?: string
     port?: number
     host?: string
     maxPublic?:number
@@ -140,6 +141,7 @@ export class ConfDTO implements CurrencyConfDTO, KeypairConfDTO, NetworkConfDTO,
       upnp?: boolean
       remotehost?: string|null
       remoteport?: number|null
+      remotepath?: string
       port?: number
       host?: string
       preferedNodes?: string[]
diff --git a/app/modules/ws2p/index.ts b/app/modules/ws2p/index.ts
index 2f1c745bf..433d62c69 100644
--- a/app/modules/ws2p/index.ts
+++ b/app/modules/ws2p/index.ts
@@ -19,6 +19,7 @@ export const WS2PDependency = {
       { value: '--ws2p-port <port>',           desc: 'Host to listen to.', parser: (val:string) => parseInt(val) },
       { value: '--ws2p-remote-host <address>', desc: 'Availabily host.' },
       { value: '--ws2p-remote-port <port>',    desc: 'Availabily port.', parser: (val:string) => parseInt(val) },
+      { value: '--ws2p-remote-path <path>',    desc: 'Availabily web path.' },
       { value: '--ws2p-max-private <count>',   desc: 'Maximum private connections count.', parser: (val:string) => parseInt(val) },
       { value: '--ws2p-max-public <count>',    desc: 'Maximum public connections count.', parser: (val:string) => parseInt(val) },
       { value: '--ws2p-private',               desc: 'Enable WS2P Private access.' },
@@ -50,6 +51,7 @@ export const WS2PDependency = {
         if (program.ws2pPort !== undefined)       conf.ws2p.port = parseInt(program.ws2pPort)
         if (program.ws2pRemotePort !== undefined) conf.ws2p.remoteport = program.ws2pRemotePort
         if (program.ws2pRemoteHost !== undefined) conf.ws2p.remotehost = program.ws2pRemoteHost
+        if (program.ws2pRemotePath !== undefined) conf.ws2p.remotepath = program.ws2pRemotePath
         if (program.ws2pUpnp !== undefined)       conf.ws2p.upnp = true
         if (program.ws2pNoupnp !== undefined)     conf.ws2p.upnp = false
         if (program.ws2pMaxPrivate !== undefined) conf.ws2p.maxPrivate = program.ws2pMaxPrivate
@@ -245,11 +247,15 @@ export class WS2PAPI extends stream.Transform {
       && this.server.conf.ws2p.uuid
       && this.server.conf.ws2p.remotehost
       && this.server.conf.ws2p.remoteport) {
-      return ['WS2P',
+      let ep = ['WS2P',
         this.server.conf.ws2p.uuid,
         this.server.conf.ws2p.remotehost,
         this.server.conf.ws2p.remoteport
       ].join(' ')
+      if (this.server.conf.ws2p.remotepath) {
+        ep += ` ${this.server.conf.ws2p.remotepath}`
+      }
+      return ep
     }
     else {
       return ''
diff --git a/app/modules/ws2p/lib/WS2PCluster.ts b/app/modules/ws2p/lib/WS2PCluster.ts
index 7b65e44af..a860036fd 100644
--- a/app/modules/ws2p/lib/WS2PCluster.ts
+++ b/app/modules/ws2p/lib/WS2PCluster.ts
@@ -26,10 +26,13 @@ export interface WS2PHead {
 
 export class WS2PCluster {
 
-  static getFullAddress(host: string, port: number, path: string): string {
+  static getFullAddress(host: string, port: number, path: string|null|undefined = null): string {
     if (host.match(CommonConstants.IPV6_REGEXP)) {
       host = "[" + host + "]"
     }
+    // Make the path be a string
+    path = path || ''
+    // Choose the web protocol depending on the port
     const protocol = port == 443 ? "wss://": "ws://"
     return [protocol, host, ':', port, path].join('')
   }
diff --git a/test/fast/modules/ws2p/host.ts b/test/fast/modules/ws2p/host.ts
index 4fcdae782..34073f4bd 100644
--- a/test/fast/modules/ws2p/host.ts
+++ b/test/fast/modules/ws2p/host.ts
@@ -4,8 +4,10 @@ import { WS2PCluster } from '../../../../app/modules/ws2p/lib/WS2PCluster';
 describe('WS2P IP functions', () => {
   
   it('should format correctly DNS endpoints', () => {
-    assert.equal(WS2PCluster.getFullAddress('my.host.com', 80, ''), 'ws://my.host.com:80')
-    assert.equal(WS2PCluster.getFullAddress('my.host.com', 443, ''), 'wss://my.host.com:443')
+    assert.equal(WS2PCluster.getFullAddress('my.host.com', 80), 'ws://my.host.com:80')
+    assert.equal(WS2PCluster.getFullAddress('my.host.com', 80, null), 'ws://my.host.com:80')
+    assert.equal(WS2PCluster.getFullAddress('my.host.com', 80, undefined), 'ws://my.host.com:80')
+    assert.equal(WS2PCluster.getFullAddress('my.host.com', 443, null), 'wss://my.host.com:443')
     assert.equal(WS2PCluster.getFullAddress('my.host.com', 80, '/'), 'ws://my.host.com:80/')
     assert.equal(WS2PCluster.getFullAddress('my.host.com', 80, '/path'), 'ws://my.host.com:80/path')
     assert.equal(WS2PCluster.getFullAddress('my.host.com', 80, '/super/long/path'), 'ws://my.host.com:80/super/long/path')
-- 
GitLab