Skip to content
Snippets Groups Projects
Commit 89a716bf authored by Éloïs's avatar Éloïs
Browse files

rewrite proxiesConf type

parent 8198ed46
Branches
Tags
1 merge request!1178Add WS2PTOR features
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 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 = ""
}
}
getAgent() {
return this.agent;
}
getUrl() {
return this.url;
}
export class ProxiesConf {
public proxySocksAddress: string|undefined
public proxyTorAddress: string|undefined
public alwaysUseTor: boolean|undefined
getTimeout() {
return this.timeout;
constructor () {
this.proxySocksAddress = undefined
this.proxyTorAddress = undefined
this.alwaysUseTor = undefined
}
static defaultConf():ProxyConf {
return {
proxySocksAddress: undefined,
proxyTorAddress: undefined,
alwaysUseTor: undefined,
proxies: undefined
}
}
static canReachTorEndpoint(proxyConf: ProxyConf|undefined):boolean {
return (proxyConf !== undefined && (proxyConf.alwaysUseTor === true || (proxyConf.proxies !== undefined && proxyConf.proxies.proxyTor !== undefined) ) )
}
static createProxies(proxyConf: ProxyConf|undefined) : Proxies|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
}
} else {
return undefined
}
static canReachTorEndpoint(proxyConf: ProxiesConf|undefined):boolean {
return (proxyConf !== undefined && (proxyConf.alwaysUseTor === true || (proxyConf.proxyTorAddress !== undefined) ) )
}
static httpProxy(url:string, proxyConf: ProxyConf|undefined) {
return Proxy.chooseProxy(url, proxyConf, HTTP_ENDPOINT_ONION_REGEX)
static httpProxy(url:string, proxyConf: ProxiesConf|undefined):string|undefined {
return ProxiesConf.chooseProxyAgent(url, proxyConf, HOST_ONION_REGEX)
}
static wsProxy(address:string, proxyConf: ProxyConf|undefined, mySelf:boolean = false) {
return Proxy.chooseProxy(address, proxyConf, WS_ENDPOINT_ONION_REGEX, mySelf)
static wsProxy(address:string, proxyConf: ProxiesConf|undefined):string|undefined {
return ProxiesConf.chooseProxyAgent(address, proxyConf, WS_ENDPOINT_ONION_REGEX)
}
private static chooseProxy(address:string, proxyConf: ProxyConf|undefined, onionRegex:RegExp, mySelf:boolean = false): Proxy|undefined {
private static chooseProxyAgent(address:string, proxyConf: ProxiesConf|undefined, onionRegex:RegExp):string|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 )
if ( proxyConf.proxyTorAddress !== undefined && (proxyConf.alwaysUseTor || address.match(onionRegex)))
{
return proxyConf.proxies.proxyTor
}
else if (proxyConf.proxies.proxySocks !== undefined && proxyConf.proxies.proxySocks.agent !== undefined) {
return proxyConf.proxies.proxySocks
return proxyConf.proxyTorAddress
}
else if (proxyConf.proxySocksAddress !== undefined) {
return proxyConf.proxySocksAddress
}
}
return undefined
......
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment