From cbcfd1b694b8e9763ebfed08681b965da08ee9cf Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Sun, 29 Oct 2017 04:47:01 +0100
Subject: [PATCH] [enh] add cli options for proxies conf

---
 app/cli.ts |  5 +++++
 index.ts   | 43 +++++++++++++++++++++++++------------------
 server.ts  |  5 ++++-
 3 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/app/cli.ts b/app/cli.ts
index cedf3a7fa..bab8e0d65 100644
--- a/app/cli.ts
+++ b/app/cli.ts
@@ -49,6 +49,11 @@ export const ExecuteCommand = () => {
         .option('--nostdout', 'Disable stdout printing for `export-bc` command')
         .option('--noshuffle', 'Disable peers shuffling for `sync` command')
 
+        .option('--proxy-socks <host:port>', 'Use Socks Proxy')
+        .option('--proxy-tor <host:port>', 'Use Tor Socks Proxy')
+        .option('--tor-always', 'Pass all outgoing requests through the tor network')
+        .option('--tor-mixed', 'Pass only ".onion" outgoing requests through the tor network. It\'s the default behavior')
+
         .option('--timeout <milliseconds>', 'Timeout to use when contacting peers', parseInt)
         .option('--httplogs', 'Enable HTTP logs')
         .option('--nohttplogs', 'Disable HTTP logs')
diff --git a/index.ts b/index.ts
index 3c6b04bc5..b1d842581 100644
--- a/index.ts
+++ b/index.ts
@@ -8,6 +8,7 @@ import {CrawlerDependency} from "./app/modules/crawler/index"
 import {BmaDependency} from "./app/modules/bma/index"
 import {WS2PDependency} from "./app/modules/ws2p/index"
 import {Constants} from "./app/modules/prover/lib/constants"
+import { Proxy } from './app/lib/proxy';
 
 const path = require('path');
 const _ = require('underscore');
@@ -437,10 +438,9 @@ class Stack {
   }
 }
 
-function commandLineConf(program:any, conf:any = {}) {
+function commandLineConf(program:any, conf:ConfDTO = ConfDTO.mock()) {
 
   conf = conf || {};
-  conf.sync = conf.sync || {};
   const cli = {
     currency: program.currency,
     cpu: program.cpu,
@@ -448,10 +448,11 @@ function commandLineConf(program:any, conf:any = {}) {
     server: {
       port: program.port,
     },
-    db: {
-      mport: program.mport,
-      mdb: program.mdb,
-      home: program.home
+    proxies: {
+      proxySocks: program.proxySocks,
+      proxyTor: program.proxyTor,
+      torAlways: program.torAlways,
+      torMixed: program.torMixed
     },
     logs: {
       http: program.httplogs,
@@ -465,19 +466,25 @@ function commandLineConf(program:any, conf:any = {}) {
     timeout: program.timeout
   };
 
+  // Declare proxyConf
+  if (cli.proxies.proxySocks || cli.proxies.proxyTor || cli.proxies.torAlways || cli.proxies.torMixed) {
+    conf.proxyConf = Proxy.defaultConf()
+  }
+
   // Update conf
-  if (cli.currency)                         conf.currency = cli.currency;
-  if (cli.server.port)                      conf.port = cli.server.port;
-  if (cli.cpu)                              conf.cpu = Math.max(0.01, Math.min(1.0, cli.cpu));
-  if (cli.prefix)                           conf.prefix = Math.max(Constants.MIN_PEER_ID, Math.min(Constants.MAX_PEER_ID, cli.prefix));
-  if (cli.logs.http)                        conf.httplogs = true;
-  if (cli.logs.nohttp)                      conf.httplogs = false;
-  if (cli.db.mport)                         conf.mport = cli.db.mport;
-  if (cli.db.home)                          conf.home = cli.db.home;
-  if (cli.db.mdb)                           conf.mdb = cli.db.mdb;
-  if (cli.isolate)                          conf.isolate = cli.isolate;
-  if (cli.timeout)                          conf.timeout = cli.timeout;
-  if (cli.forksize != null)                 conf.forksize = cli.forksize;
+  if (cli.currency)                             conf.currency = cli.currency;
+  if (cli.server.port)                          conf.port = cli.server.port;
+  if (cli.cpu)                                  conf.cpu = Math.max(0.01, Math.min(1.0, cli.cpu));
+  if (cli.prefix)                               conf.prefix = Math.max(Constants.MIN_PEER_ID, Math.min(Constants.MAX_PEER_ID, cli.prefix));
+  if (cli.proxies.proxySocks && conf.proxyConf) conf.proxyConf.proxySocksAddress = cli.proxies.proxySocks;
+  if (cli.proxies.proxyTor && conf.proxyConf)   conf.proxyConf.proxyTorAddress = cli.proxies.proxyTor;
+  if (cli.proxies.torAlways && conf.proxyConf)  conf.proxyConf.alwaysUseTor = true;
+  if (cli.proxies.torMixed && conf.proxyConf)   conf.proxyConf.alwaysUseTor = false;
+  if (cli.logs.http)                            conf.httplogs = true;
+  if (cli.logs.nohttp)                          conf.httplogs = false;
+  if (cli.isolate)                              conf.isolate = cli.isolate;
+  if (cli.timeout)                              conf.timeout = cli.timeout;
+  if (cli.forksize != null)                     conf.forksize = cli.forksize;
 
   return conf;
 }
diff --git a/server.ts b/server.ts
index 2f7fbc5c1..f91364b26 100644
--- a/server.ts
+++ b/server.ts
@@ -24,6 +24,7 @@ import {PeerDTO} from "./app/lib/dto/PeerDTO"
 import {OtherConstants} from "./app/lib/other_constants"
 import {WS2PCluster} from "./app/modules/ws2p/lib/WS2PCluster"
 import {DBBlock} from "./app/lib/db/DBBlock"
+import { Proxy } from './app/lib/proxy';
 
 export interface HookableServer {
   generatorGetJoinData: (...args:any[]) => Promise<any>
@@ -72,7 +73,7 @@ export class Server extends stream.Duplex implements HookableServer {
   TransactionsService:TransactionService
   private documentFIFO:GlobalFifoPromise
 
-  constructor(home:string, memoryOnly:boolean, private overrideConf:any) {
+  constructor(home:string, memoryOnly:boolean, private overrideConf:ConfDTO) {
     super({ objectMode: true })
 
     this.home = home;
@@ -148,6 +149,8 @@ export class Server extends stream.Duplex implements HookableServer {
     logger.debug('Loading conf...');
     this.conf = await this.dal.loadConf(this.overrideConf, useDefaultConf)
     // Default values
+    this.conf.proxyConf        = this.conf.proxyConf === undefined ?         Proxy.defaultConf()                           : this.conf.proxyConf
+    this.conf.proxyConf.alwaysUseTor = this.conf.proxyConf.alwaysUseTor === undefined ? false                 : this.conf.proxyConf.alwaysUseTor
     this.conf.remoteipv6       = this.conf.remoteipv6 === undefined ?        this.conf.ipv6                               : this.conf.remoteipv6
     this.conf.remoteport       = this.conf.remoteport === undefined ?        this.conf.port                               : this.conf.remoteport
     this.conf.c                = this.conf.c === undefined ?                 constants.CONTRACT.DEFAULT.C                 : this.conf.c
-- 
GitLab