Commit 02883fa2 authored by Éloïs's avatar Éloïs
Browse files

getWS2P apply tor regex

parent 291f32bc
......@@ -32,7 +32,7 @@ const CONDITIONS = "(&&|\\|\\|| |[()]|(SIG\\(" + PUBKEY + "\\)|(XHX\\([A-F0-9]
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 WS2PTOR_REGEXP = /^WS2PTOR ([a-f0-9]{8}) ([a-z_][a-z0-9-_.]*|[0-9.]+|[0-9a-f:]+).onion ([0-9]+)(?: (.+))?$/
const WS2PTOR_REGEXP = /^WS2PTOR ([a-f0-9]{8}) ([a-z0-9-_.]*|[0-9.]+|[0-9a-f:]+.onion) ([0-9]+)(?: (.+))?$/
const IPV4_REGEXP = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
const IPV6_REGEXP = /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|(([0-9A-Fa-f]{1,4}:){0,5}:((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|(::([0-9A-Fa-f]{1,4}:){0,5}((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/;
......
......@@ -98,7 +98,7 @@ export class PeerDTO implements Cloneable {
let api:{ uuid:string, host:string, port:number, path:string }|null = null
const endpointRegexp = (tor) ? CommonConstants.WS2PTOR_REGEXP:CommonConstants.WS2P_REGEXP
for (const ep of this.endpoints) {
const matches:any = !api && ep.match(CommonConstants.WS2P_REGEXP)
const matches:any = !api && ep.match(endpointRegexp)
if (matches) {
api = {
uuid: matches[1],
......
......@@ -21,25 +21,23 @@ export interface ProxyConf {
}
export class Proxy {
private agent: any
private timeout:number
public agent: any
private url:string
constructor(proxy:string, type:string = "socks", timeout:number = DEFAULT_PROXY_TIMEOUT) {
constructor(proxy:string, type:string = "socks", public timeout:number = DEFAULT_PROXY_TIMEOUT) {
if (type === "socks") {
this.agent = SocksProxyAgent("socks://"+proxy)
this.url = "socks://"+proxy
}
else {
this.url = ""
this.agent = undefined
}
this.timeout = timeout
}
getAgent() {
return this.agent;
}
getTimeout() {
return this.timeout;
getUrl() {
return this.url;
}
static defaultConf():ProxyConf {
......@@ -51,6 +49,10 @@ export class Proxy {
}
}
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) {
......@@ -77,11 +79,11 @@ export class Proxy {
proxyConf.proxies = Proxy.createProxies(proxyConf)
}
if (proxyConf.proxies !== undefined) {
if ( proxyConf.proxies.proxyTor !== undefined && proxyConf.proxies.proxyTor.getAgent() !== undefined && (proxyConf.alwaysUseTor || address.match(onionRegex)) && !mySelf )
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.getAgent() !== undefined) {
else if (proxyConf.proxies.proxySocks !== undefined && proxyConf.proxies.proxySocks.agent !== undefined) {
return proxyConf.proxies.proxySocks
}
}
......
......@@ -16,6 +16,7 @@ import {WS2PMessageHandler} from "./impl/WS2PMessageHandler"
import { CommonConstants } from '../../../lib/common-libs/constants';
import { Package } from "../../../lib/common/package";
import { Constants } from "../../prover/lib/constants";
import { ProxyConf, Proxy } from '../../../lib/proxy';
const es = require('event-stream')
const nuuid = require('node-uuid')
......@@ -291,6 +292,7 @@ export class WS2PCluster {
async connectToRemoteWS(host: string, port: number, path:string, messageHandler:WS2PMessageHandler, expectedPub:string, ws2pEndpointUUID:string = ""): Promise<WS2PConnection> {
const uuid = nuuid.v4()
let pub = "--------"
const api:string = (host.match(WS2PConstants.HOST_ONION_REGEX) !== null) ? 'WS2PTOR':'WS2P'
try {
const fullEndpointAddress = WS2PCluster.getFullAddress(host, port, path)
const ws2pc = await WS2PClient.connectTo(this.server, fullEndpointAddress, ws2pEndpointUUID, messageHandler, expectedPub, (pub:string) => {
......@@ -300,7 +302,7 @@ export class WS2PCluster {
this.ws2pClients[uuid] = ws2pc
pub = ws2pc.connection.pubkey
ws2pc.connection.closed.then(() => {
this.server.logger.info('WS2P: connection [%s `WS2P %s %s`] has been closed', pub.slice(0, 8), host, port)
this.server.logger.info(api+': connection [%s `'+api+' %s %s`] has been closed', pub.slice(0, 8), host, port)
this.server.push({
ws2p: 'disconnected',
peer: {
......@@ -311,7 +313,7 @@ export class WS2PCluster {
delete this.ws2pClients[uuid]
}
})
this.server.logger.info('WS2P: connected to peer %s using `WS2P %s %s`!', pub.slice(0, 8), host, port)
this.server.logger.info(api+': connected to peer %s using `'+api+' %s %s`!', pub.slice(0, 8), host, port)
this.server.push({
ws2p: 'connected',
to: { host, port, pubkey: pub }
......@@ -319,7 +321,7 @@ export class WS2PCluster {
await this.server.dal.setPeerUP(pub)
return ws2pc.connection
} catch (e) {
this.server.logger.info('WS2P: Could not connect to peer %s using `WS2P %s %s: %s`', pub.slice(0, 8), host, port, (e && e.message || e))
this.server.logger.info(api+': Could not connect to peer %s using `'+api+' %s %s: %s`', pub.slice(0, 8), host, port, (e && e.message || e))
throw e
}
}
......@@ -330,12 +332,12 @@ export class WS2PCluster {
const prefered = ((this.server.conf.ws2p && this.server.conf.ws2p.preferedNodes) || []).slice() // Copy
// Our key is also a prefered one, so we connect to our siblings
prefered.push(this.server.conf.pair.pub)
const imTorPeer = this.server.conf.proxyConf && this.server.conf.proxyConf.proxies && this.server.conf.proxyConf.proxies.proxyTor
const imCanReachTorEndpoint = Proxy.canReachTorEndpoint(this.server.conf.proxyConf)
peers.sort((a, b) => {
const aIsPrefered = prefered.indexOf(a.pubkey) !== -1
const bIsPrefered = prefered.indexOf(b.pubkey) !== -1
if (imTorPeer) {
if (imCanReachTorEndpoint) {
const aAtWs2pTorEnpoint = a.endpoints.filter(function (element) { return element.match(CommonConstants.WS2PTOR_REGEXP); }).length > 0
const bAtWs2pTorEnpoint = b.endpoints.filter(function (element) { return element.match(CommonConstants.WS2PTOR_REGEXP); }).length > 0
......@@ -367,7 +369,7 @@ export class WS2PCluster {
let i = 0
while (i < peers.length && this.clientsCount() < this.maxLevel1Size) {
const p = peers[i]
const api = p.getWS2P(imTorPeer !== undefined)
const api = p.getWS2P(imCanReachTorEndpoint !== undefined)
if (api) {
try {
await this.connectToRemoteWS(api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid)
......
......@@ -279,11 +279,11 @@ export class WS2PConnection {
expectedPub:string = "") {
if (proxy !== undefined) {
options = {
connectionTimeout: proxy.getTimeout(),
requestTimeout: proxy.getTimeout()
connectionTimeout: proxy.timeout,
requestTimeout: proxy.timeout
}
}
const websocket = (proxy !== undefined) ? new ws(address, { agent: proxy.getAgent() }):new ws(address)
const websocket = (proxy !== undefined) ? new ws(address, { agent: proxy.agent }):new ws(address)
const onWsOpened:Promise<void> = new Promise(res => {
websocket.on('open', () => res())
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment