Skip to content
Snippets Groups Projects
Commit 39b9d18b authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[enh] #1084 WS2P: add WS2P logging

parent 7625c2a1
No related branches found
No related tags found
No related merge requests found
...@@ -84,6 +84,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth { ...@@ -84,6 +84,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth {
async sendACK(ws: any): Promise<void> { async sendACK(ws: any): Promise<void> {
const challengeMessage = `WS2P:ACK:${this.pair.pub}:${this.challenge}` const challengeMessage = `WS2P:ACK:${this.pair.pub}:${this.challenge}`
Logger.log('sendACK >>> ' + challengeMessage)
const sig = this.pair.signSync(challengeMessage) const sig = this.pair.signSync(challengeMessage)
await ws.send(JSON.stringify({ await ws.send(JSON.stringify({
auth: 'ACK', auth: 'ACK',
...@@ -98,6 +99,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth { ...@@ -98,6 +99,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth {
return false return false
} }
const challengeMessage = `WS2P:CONNECT:${pub}:${challenge}` const challengeMessage = `WS2P:CONNECT:${pub}:${challenge}`
Logger.log('registerCONNECT >>> ' + challengeMessage)
const verified = verify(challengeMessage, sig, pub) const verified = verify(challengeMessage, sig, pub)
if (verified) { if (verified) {
this.challenge = challenge this.challenge = challenge
...@@ -108,6 +110,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth { ...@@ -108,6 +110,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth {
async registerOK(sig: string): Promise<boolean> { async registerOK(sig: string): Promise<boolean> {
const challengeMessage = `WS2P:OK:${this.remotePub}:${this.challenge}` const challengeMessage = `WS2P:OK:${this.remotePub}:${this.challenge}`
Logger.log('registerOK >>> ' + challengeMessage)
this.authenticatedByRemote = verify(challengeMessage, sig, this.remotePub) this.authenticatedByRemote = verify(challengeMessage, sig, this.remotePub)
if (!this.authenticatedByRemote) { if (!this.authenticatedByRemote) {
this.serverAuthReject("Wrong signature from remote OK") this.serverAuthReject("Wrong signature from remote OK")
...@@ -151,6 +154,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth { ...@@ -151,6 +154,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth {
async sendCONNECT(ws:any): Promise<void> { async sendCONNECT(ws:any): Promise<void> {
const challengeMessage = `WS2P:CONNECT:${this.pair.pub}:${this.challenge}` const challengeMessage = `WS2P:CONNECT:${this.pair.pub}:${this.challenge}`
Logger.log('sendCONNECT >>> ' + challengeMessage)
const sig = this.pair.signSync(challengeMessage) const sig = this.pair.signSync(challengeMessage)
await ws.send(JSON.stringify({ await ws.send(JSON.stringify({
auth: 'CONNECT', auth: 'CONNECT',
...@@ -167,6 +171,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth { ...@@ -167,6 +171,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth {
return false return false
} }
const challengeMessage = `WS2P:ACK:${pub}:${this.challenge}` const challengeMessage = `WS2P:ACK:${pub}:${this.challenge}`
Logger.log('registerACK >>> ' + challengeMessage)
this.authenticated = verify(challengeMessage, sig, pub) this.authenticated = verify(challengeMessage, sig, pub)
if (!this.authenticated) { if (!this.authenticated) {
this.serverAuthReject("Wrong signature from server ACK") this.serverAuthReject("Wrong signature from server ACK")
...@@ -178,6 +183,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth { ...@@ -178,6 +183,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth {
async sendOK(ws:any): Promise<void> { async sendOK(ws:any): Promise<void> {
const challengeMessage = `WS2P:OK:${this.pair.pub}:${this.challenge}` const challengeMessage = `WS2P:OK:${this.pair.pub}:${this.challenge}`
Logger.log('sendOK >>> ' + challengeMessage)
const sig = this.pair.signSync(challengeMessage) const sig = this.pair.signSync(challengeMessage)
await ws.send(JSON.stringify({ await ws.send(JSON.stringify({
auth: 'OK', auth: 'OK',
...@@ -274,6 +280,7 @@ export class WS2PConnection { ...@@ -274,6 +280,7 @@ export class WS2PConnection {
const onWsClosed:Promise<void> = new Promise(res => { const onWsClosed:Promise<void> = new Promise(res => {
websocket.on('close', () => res()) websocket.on('close', () => res())
}) })
websocket.on('error', () => websocket.close())
return new WS2PConnection(websocket, onWsOpened, onWsClosed, messageHandler, localAuth, remoteAuth, options, expectedPub) return new WS2PConnection(websocket, onWsOpened, onWsClosed, messageHandler, localAuth, remoteAuth, options, expectedPub)
} }
...@@ -573,9 +580,20 @@ export class WS2PConnection { ...@@ -573,9 +580,20 @@ export class WS2PConnection {
private async errorDetected(cause:WS2P_ERR) { private async errorDetected(cause:WS2P_ERR) {
this.nbErrors++ this.nbErrors++
// console.error('>>> WS ERROR: %s', WS2P_ERR[cause]) Logger.error('>>> WS ERROR: %s', WS2P_ERR[cause])
if (this.nbErrors >= MAXIMUM_ERRORS_COUNT) { if (this.nbErrors >= MAXIMUM_ERRORS_COUNT) {
this.ws.terminate() this.ws.terminate()
} }
} }
} }
class Logger {
static log(message:string) {
// console.log('WS2P >>> ' + message)
}
static error(message:string, obj:any) {
console.error('WS2P >>> ' + message, obj)
}
}
\ No newline at end of file
...@@ -29,6 +29,7 @@ export class WS2PServer { ...@@ -29,6 +29,7 @@ export class WS2PServer {
/****************** /******************
* A NEW CONNECTION * A NEW CONNECTION
******************/ ******************/
this.server.logger.info('WS2P: new incoming connection from %s:%s!', ws._sender._socket._handle.owner.remoteAddress, ws._sender._socket._handle.owner.remotePort)
const c = WS2PConnection.newConnectionFromWebSocketServer( const c = WS2PConnection.newConnectionFromWebSocketServer(
ws, ws,
new WS2PServerMessageHandler(this.server), new WS2PServerMessageHandler(this.server),
...@@ -40,7 +41,9 @@ export class WS2PServer { ...@@ -40,7 +41,9 @@ export class WS2PServer {
this.connections.push(c) this.connections.push(c)
c.connect().catch((e:any) => console.error('WS2P: cannot connect to incoming WebSocket connection: %s', e)) c.connect()
.then(() => this.server.logger.info('WS2P: established incoming connection from %s:%s', ws._sender._socket._handle.owner.remoteAddress, ws._sender._socket._handle.owner.remotePort))
.catch((e:any) => console.error('WS2P: cannot connect to incoming WebSocket connection: %s', e))
// Broadcasting // Broadcasting
const ws2pStreamer = new WS2PStreamer(c) const ws2pStreamer = new WS2PStreamer(c)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment