Mise à jour de GitLab prévue ce samedi 23 octobre 2021 à partir de 9h00 CET

Commit 8db926f9 authored by Éloïs's avatar Éloïs
Browse files

[enh] WS2PTOR endpoint

parent d125e080
......@@ -32,6 +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 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}:))$/;
......@@ -90,6 +91,7 @@ export const CommonConstants = {
BMA_REGEXP,
WS2P_REGEXP,
WS2PTOR_REGEXP,
IPV4_REGEXP,
IPV6_REGEXP,
PUBLIC_KEY: exact(PUBKEY),
......
......@@ -143,7 +143,7 @@ export class FileDAL {
}
async getWS2Peers() {
return this.peerDAL.getPeersWithEndpointsLike('WS2P ')
return this.peerDAL.getPeersWithEndpointsLike('WS2P ').concat(this.peerDAL.getPeersWithEndpointsLike('WS2PTOR '))
}
async getBlock(number:number) {
......
......@@ -94,8 +94,9 @@ export class PeerDTO implements Cloneable {
return bma || {};
}
getWS2P() {
getWS2P(tor:boolean = false) {
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)
if (matches) {
......
"use strict";
import { WS2PConstants } from './lib/constants';
import {ConfDTO, WS2PConfDTO} from "../../lib/dto/ConfDTO"
import {Server} from "../../../server"
import * as stream from "stream"
import * as stream from 'stream';
import {WS2PCluster} from "./lib/WS2PCluster"
import {WS2PUpnp} from "./lib/ws2p-upnp"
import {CommonConstants} from "../../lib/common-libs/constants"
......@@ -242,22 +243,29 @@ export class WS2PAPI extends stream.Transform {
// If WS2P defined and enabled
if (this.server.conf.ws2p !== undefined && (this.server.conf.ws2p.publicAccess || this.server.conf.ws2p.privateAccess))
{
let endpointType = "WS2P"
if (this.server.conf.upnp && this.upnpAPI) {
const config = this.upnpAPI.getCurrentConfig()
return !config ? '' : ['WS2P', this.server.conf.ws2p.uuid, config.remotehost, config.port].join(' ')
if (config) {
if (config.remotehost.match(WS2PConstants.HOST_ONION_REGEX)) { endpointType += "TOR"; }
return [endpointType, this.server.conf.ws2p.uuid, config.remotehost, config.port].join(' ')
} else {
return ''
}
}
else if (this.server.conf.ws2p.uuid
&& this.server.conf.ws2p.remotehost
&& this.server.conf.ws2p.remoteport) {
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
if (this.server.conf.ws2p.remotehost.match(WS2PConstants.HOST_ONION_REGEX)) { endpointType += "TOR"; }
let ep = [endpointType,
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
}
}
return ''
......
......@@ -13,7 +13,7 @@ import {OtherConstants} from "../../../lib/other_constants"
import {Key, verify} from "../../../lib/common-libs/crypto/keyring"
import {WS2PServerMessageHandler} from "./interface/WS2PServerMessageHandler"
import {WS2PMessageHandler} from "./impl/WS2PMessageHandler"
import { CommonConstants } from "../../../lib/common-libs/constants";
import { CommonConstants } from '../../../lib/common-libs/constants';
import { Package } from "../../../lib/common/package";
import { Constants } from "../../prover/lib/constants";
......@@ -330,21 +330,44 @@ 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
peers.sort((a, b) => {
const aIsPrefered = prefered.indexOf(a.pubkey) !== -1
const bIsPrefered = prefered.indexOf(b.pubkey) !== -1
if ((aIsPrefered && bIsPrefered) || (!aIsPrefered && !bIsPrefered)) {
return 0
} else if (aIsPrefered) {
return -1
if (imTorPeer) {
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
if ( (aAtWs2pTorEnpoint && bAtWs2pTorEnpoint) || (!aAtWs2pTorEnpoint && !bAtWs2pTorEnpoint) ) {
if ((aIsPrefered && bIsPrefered) || (!aIsPrefered && !bIsPrefered)) {
return 0
} else if (aIsPrefered) {
return -1
} else {
return 1
}
} else {
if (aAtWs2pTorEnpoint) {
return -1
} else {
return 1
}
}
} else {
return 1
if ((aIsPrefered && bIsPrefered) || (!aIsPrefered && !bIsPrefered)) {
return 0
} else if (aIsPrefered) {
return -1
} else {
return 1
}
}
})
let i = 0
while (i < peers.length && this.clientsCount() < this.maxLevel1Size) {
const p = peers[i]
const api = p.getWS2P()
const api = p.getWS2P(imTorPeer !== undefined)
if (api) {
try {
await this.connectToRemoteWS(api.host, api.port, api.path, this.messageHandler, p.pubkey, api.uuid)
......
......@@ -37,5 +37,7 @@ export const WS2PConstants = {
+ '(' + CommonConstants.FORMATS.POW_PREFIX + ')'
+ '$'),
HOST_ONION_REGEX: new RegExp('(\S*?\.onion)$'),
HEADS_SPREAD_TIMEOUT: 100 // Wait 100ms before sending a bunch of signed heads
}
\ No newline at end of file
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