From 89a716bfac62b6a7a521ceca25f00cbdc39eea88 Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Tue, 31 Oct 2017 03:02:43 +0100 Subject: [PATCH] rewrite proxiesConf type --- app/lib/proxy.ts | 104 +++++++++++------------------------------------ index.ts | 4 +- server.ts | 4 +- 3 files changed, 28 insertions(+), 84 deletions(-) diff --git a/app/lib/proxy.ts b/app/lib/proxy.ts index 8c7016890..051a57d3a 100644 --- a/app/lib/proxy.ts +++ b/app/lib/proxy.ts @@ -1,97 +1,41 @@ const SocksProxyAgent = require('socks-proxy-agent'); -const DEFAULT_PROXY_TIMEOUT:number = 30000 -const TOR_PROXY_TIMEOUT:number = 60000 -const HTTP_ENDPOINT_ONION_REGEX = new RegExp('(?:https?:\/\/)?(?:www)?(\S*?\.onion)(\/[-\w]*)*') -const WS_ENDPOINT_ONION_REGEX = new RegExp('(?:wss?:\/\/)?(?:www)?(\S*?\.onion)(\/[-\w]*)*') +const HOST_ONION_REGEX = new RegExp('(\S*?\.onion)$'); +const WS_ENDPOINT_ONION_REGEX = new RegExp('(?:wss?:\/\/)?(?:www)?(\S*?\.onion)(\/[-\w]*)*'); -export interface Proxies { - proxySocks: Proxy|undefined, - proxyTor: Proxy|undefined -} +export class ProxiesConf { + public proxySocksAddress: string|undefined + public proxyTorAddress: string|undefined + public alwaysUseTor: boolean|undefined -export interface ProxyConf { - proxySocksAddress: string|undefined, - proxyTorAddress: string|undefined, - alwaysUseTor: boolean|undefined, - proxies: Proxies|undefined -} - -export class Proxy { - private agent: any - private url:string - - constructor(proxy:string, type:string = "socks", private timeout:number = DEFAULT_PROXY_TIMEOUT) { - if (type === "socks") { - this.agent = SocksProxyAgent("socks://"+proxy) - this.url = "socks://"+proxy - } - else { - this.agent = undefined - this.url = "" - } + constructor () { + this.proxySocksAddress = undefined + this.proxyTorAddress = undefined + this.alwaysUseTor = undefined } - getAgent() { - return this.agent; + static canReachTorEndpoint(proxyConf: ProxiesConf|undefined):boolean { + return (proxyConf !== undefined && (proxyConf.alwaysUseTor === true || (proxyConf.proxyTorAddress !== undefined) ) ) } - getUrl() { - return this.url; - } - - getTimeout() { - return this.timeout; - } - - static defaultConf():ProxyConf { - return { - proxySocksAddress: undefined, - proxyTorAddress: undefined, - alwaysUseTor: undefined, - proxies: undefined - } + static httpProxy(url:string, proxyConf: ProxiesConf|undefined):string|undefined { + return ProxiesConf.chooseProxyAgent(url, proxyConf, HOST_ONION_REGEX) } - static canReachTorEndpoint(proxyConf: ProxyConf|undefined):boolean { - return (proxyConf !== undefined && (proxyConf.alwaysUseTor === true || (proxyConf.proxies !== undefined && proxyConf.proxies.proxyTor !== undefined) ) ) + static wsProxy(address:string, proxyConf: ProxiesConf|undefined):string|undefined { + return ProxiesConf.chooseProxyAgent(address, proxyConf, WS_ENDPOINT_ONION_REGEX) } - static createProxies(proxyConf: ProxyConf|undefined) : Proxies|undefined - { + private static chooseProxyAgent(address:string, proxyConf: ProxiesConf|undefined, onionRegex:RegExp):string|undefined { if (proxyConf !== undefined) { - return { - proxySocks: (proxyConf.proxySocksAddress !== undefined) ? new Proxy(proxyConf.proxySocksAddress, "socks"):undefined, - proxyTor: (proxyConf.proxyTorAddress !== undefined) ? new Proxy(proxyConf.proxyTorAddress, "socks", TOR_PROXY_TIMEOUT):undefined + if ( proxyConf.proxyTorAddress !== undefined && (proxyConf.alwaysUseTor || address.match(onionRegex))) + { + return proxyConf.proxyTorAddress + } + else if (proxyConf.proxySocksAddress !== undefined) { + return proxyConf.proxySocksAddress } - } else { - return undefined - } - } - - static httpProxy(url:string, proxyConf: ProxyConf|undefined) { - return Proxy.chooseProxy(url, proxyConf, HTTP_ENDPOINT_ONION_REGEX) - } - - static wsProxy(address:string, proxyConf: ProxyConf|undefined, mySelf:boolean = false) { - return Proxy.chooseProxy(address, proxyConf, WS_ENDPOINT_ONION_REGEX, mySelf) - } - - private static chooseProxy(address:string, proxyConf: ProxyConf|undefined, onionRegex:RegExp, mySelf:boolean = false): Proxy|undefined { - if (proxyConf !== undefined) { - if (proxyConf.proxies === undefined) { - proxyConf.proxies = Proxy.createProxies(proxyConf) - } - if (proxyConf.proxies !== undefined) { - if ( proxyConf.proxies.proxyTor !== undefined && proxyConf.proxies.proxyTor.agent !== undefined && (proxyConf.alwaysUseTor || address.match(onionRegex)) && !mySelf ) - { - return proxyConf.proxies.proxyTor - } - else if (proxyConf.proxies.proxySocks !== undefined && proxyConf.proxies.proxySocks.agent !== undefined) { - return proxyConf.proxies.proxySocks - } - } } return undefined } -} +} \ No newline at end of file diff --git a/index.ts b/index.ts index ada66317c..70b7bd28a 100644 --- a/index.ts +++ b/index.ts @@ -8,7 +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'; +import { ProxiesConf } from './app/lib/proxy'; const path = require('path'); const _ = require('underscore'); @@ -469,7 +469,7 @@ function commandLineConf(program:any, conf:any = {}) { // Declare proxyConf if (cli.proxies.proxySocks || cli.proxies.proxyTor || cli.proxies.torAlways || cli.proxies.torMixed || cli.proxies.rmProxies) { - conf.proxyConf = Proxy.defaultConf() + conf.proxyConf = new ProxiesConf() } // Update conf diff --git a/server.ts b/server.ts index 89772dbf0..9eca4b855 100644 --- a/server.ts +++ b/server.ts @@ -24,7 +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'; +import { ProxiesConf } from './app/lib/proxy'; export interface HookableServer { generatorGetJoinData: (...args:any[]) => Promise<any> @@ -149,7 +149,7 @@ 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 = this.conf.proxyConf === undefined ? new ProxiesConf() : 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 -- GitLab