Commit 6b022bce authored by Éloïs's avatar Éloïs
Browse files

[enh] sock proxy for ws2p private

parent 4e0c3bcb
import { WS2PCluster } from './WS2PCluster';
import {Server} from "../../../../server"
import {WS2PConnection, WS2PPubkeyLocalAuth, WS2PPubkeyRemoteAuth} from "./WS2PConnection"
import {Key} from "../../../lib/common-libs/crypto/keyring"
......@@ -5,18 +6,25 @@ import {WS2PMessageHandler} from "./impl/WS2PMessageHandler"
import {WS2PConstants} from "./constants"
import {WS2PStreamer} from "./WS2PStreamer"
import {WS2PSingleWriteStream} from "./WS2PSingleWriteStream"
import { Proxies, ProxyConf, Proxy } from '../../../lib/proxy';
import { server } from '../../../../test/integration/tools/toolbox';
export class WS2PClient {
private constructor(public connection:WS2PConnection) {}
static async connectTo(server:Server, fullEndpointAddress:string, messageHandler:WS2PMessageHandler, expectedPub:string, allowKey:(pub:string)=>Promise<boolean> ) {
static async connectTo(server:Server, fullEndpointAddress:string, uuid:string, messageHandler:WS2PMessageHandler, expectedPub:string, allowKey:(pub:string)=>Promise<boolean> ) {
const k2 = new Key(server.conf.pair.pub, server.conf.pair.sec)
let mySelf = false;
if (server.conf.ws2p && server.conf.ws2p.uuid === uuid) {
let mySelf = true;
}
const c = WS2PConnection.newConnectionToAddress(
fullEndpointAddress,
messageHandler,
new WS2PPubkeyLocalAuth(server.conf.currency , k2, allowKey),
new WS2PPubkeyRemoteAuth(server.conf.currency, k2, allowKey),
Proxy.wsProxy(fullEndpointAddress, server.conf.proxyConf, mySelf),
{
connectionTimeout: WS2PConstants.REQUEST_TIMEOUT,
requestTimeout: WS2PConstants.REQUEST_TIMEOUT
......
......@@ -293,7 +293,7 @@ export class WS2PCluster {
let pub = "--------"
try {
const fullEndpointAddress = WS2PCluster.getFullAddress(host, port, path)
const ws2pc = await WS2PClient.connectTo(this.server, fullEndpointAddress, messageHandler, expectedPub, (pub:string) => {
const ws2pc = await WS2PClient.connectTo(this.server, fullEndpointAddress, ws2pEndpointUUID, messageHandler, expectedPub, (pub:string) => {
const connectedPubkeys = this.getConnectedPubkeys()
return this.acceptPubkey(expectedPub, connectedPubkeys, () => this.clientsCount(), this.maxLevel1Size, (this.server.conf.ws2p && this.server.conf.ws2p.preferedNodes || []), ws2pEndpointUUID)
})
......@@ -626,7 +626,7 @@ export class WS2PCluster {
async startCrawling(waitConnection = false) {
// For connectivity
this.reconnectionInteval = setInterval(() => this.server.push({ ws2p: 'disconnected' }), 1000 * 60 * 10)
this.reconnectionInteval = setInterval(() => this.server.push({ ws2p: 'disconnected' }), 1000 * WS2PConstants.RECONNEXION_INTERVAL_IN_SEC)
// For blocks
if (this.syncBlockInterval)
clearInterval(this.syncBlockInterval);
......
......@@ -7,6 +7,7 @@ import {MembershipDTO} from "../../../lib/dto/MembershipDTO"
import {TransactionDTO} from "../../../lib/dto/TransactionDTO"
import {PeerDTO} from "../../../lib/dto/PeerDTO"
import {WS2PConstants} from "./constants"
import { Proxy } from '../../../lib/proxy';
const ws = require('ws')
const nuuid = require('node-uuid');
const logger = require('../../../lib/logger').NewLogger('ws2p')
......@@ -267,15 +268,22 @@ export class WS2PConnection {
messageHandler:WS2PMessageHandler,
localAuth:WS2PLocalAuth,
remoteAuth:WS2PRemoteAuth,
proxy:Proxy|undefined = undefined,
options:{
connectionTimeout:number
connectionTimeout:number,
requestTimeout:number
} = {
connectionTimeout: REQUEST_TIMEOUT_VALUE,
requestTimeout: REQUEST_TIMEOUT_VALUE
},
expectedPub:string = "") {
const websocket = new ws(address)
if (proxy !== undefined) {
options = {
connectionTimeout: proxy.getTimeout(),
requestTimeout: proxy.getTimeout()
}
}
const websocket = (proxy !== undefined) ? new ws(address, { agent: proxy.getAgent() }):new ws(address)
const onWsOpened:Promise<void> = new Promise(res => {
websocket.on('open', () => res())
})
......
......@@ -95,7 +95,7 @@ describe('WS2P', () => {
it('should refuse the connection if the server does not answer', async () => {
const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP')
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), {
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), undefined, {
connectionTimeout: 100,
requestTimeout: 100
})
......@@ -104,7 +104,7 @@ describe('WS2P', () => {
it('should refuse the connection if the server answers with a wrong signature', async () => {
const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP')
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), {
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), undefined, {
connectionTimeout: 100,
requestTimeout: 100
})
......@@ -113,7 +113,7 @@ describe('WS2P', () => {
it('should refuse the connection if the server refuses our signature', async () => {
const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP')
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), {
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), undefined, {
connectionTimeout: 100,
requestTimeout: 100
})
......@@ -123,7 +123,7 @@ describe('WS2P', () => {
it('should accept the connection if the server answers with a good signature', async () => {
const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP')
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PNoRemoteAuth(), {
const ws2p = WS2PConnection.newConnectionToAddress('ws://localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PNoRemoteAuth(), undefined, {
connectionTimeout: 1000,
requestTimeout: 1000
})
......
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