Skip to content

Commit

Permalink
fix: rsv1 error (#225)
Browse files Browse the repository at this point in the history
* chore: remove secret

* chore: allow lightningtipbot pubkey for zaps

* chore: add cloudflare remoteipheader

* chore: close client conn on error

* chore: terminate conn w/o subs

* chore: enable permessage-deflate

* fix: start logs
  • Loading branch information
cameri authored Feb 21, 2023
1 parent 6335496 commit 0954d84
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ services:
build: .
container_name: nostream
environment:
SECRET: ${SECRET}
RELAY_PORT: 8008
# Master
NOSTR_CONFIG_DIR: /home/node/.nostr
Expand Down
8 changes: 7 additions & 1 deletion resources/default-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ payments:
amount: 1000000
whitelists:
pubkeys:
- replace-with-your-pubkey-in-hex
- replace-with-your-pubkey-in-hex
# Allow the following Zap providers:
# LightningTipBot by Calle
- "fcd720c38d9ee337188f47aac845dcd8f590ccdb4a928b76dde18187b4c9d37d"
paymentsProcessors:
zebedee:
baseURL: https://api.zebedee.io/
Expand All @@ -27,7 +30,10 @@ paymentsProcessors:
callbackBaseURL: https://nostream.your-domain.com/callbacks/lnbits
network:
maxPayloadSize: 524288
# Comment the next line if using CloudFlare proxy
remoteIpHeader: x-forwarded-for
# Uncomment the next line if using CloudFlare proxy
# remoteIpHeader: cf-connecting-ip
workers:
count: 0
mirroring:
Expand Down
14 changes: 6 additions & 8 deletions src/adapters/web-socket-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
.on('error', (error) => {
if (error.name === 'RangeError' && error.message === 'Max payload size exceeded') {
console.error(`web-socket-adapter: client ${this.clientId} (${this.getClientAddress()}) sent payload too large`)
} else if (error.name === 'RangeError' && error.message === 'Invalid WebSocket frame: RSV1 must be clear') {
debug(`client ${this.clientId} (${this.getClientAddress()}) enabled compression`)
} else {
console.error(`web-socket-adapter: client error ${this.clientId} (${this.getClientAddress()}):`, error)
}

this.client.close()
})
.on('message', this.onClientMessage.bind(this))
.on('close', this.onClientClose.bind(this))
Expand Down Expand Up @@ -125,9 +129,9 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
}

public onHeartbeat(): void {
if (!this.alive) {
if (!this.alive && !this.subscriptions.size) {
console.error(`web-socket-adapter: pong timeout for client ${this.clientId} (${this.getClientAddress()})`)
this.terminate()
this.client.close()
return
}

Expand All @@ -140,12 +144,6 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
return new Map(this.subscriptions)
}

private terminate(): void {
debug('terminating client %s', this.clientId)
this.client.terminate()
debug('client %s terminated', this.clientId)
}

private async onClientMessage(raw: Buffer) {
this.alive = true
let abortable = false
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class App implements IRunnable {
MIRROR_INDEX: i.toString(),
})
}
logCentered(`${mirrors.length} maintenance worker started`, width)
logCentered(`${mirrors.length} static-mirroring worker started`, width)
}

debug('settings: %O', settings)
Expand Down
17 changes: 17 additions & 0 deletions src/factories/worker-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ export const workerFactory = (): AppWorker => {
const webSocketServer = new WebSocketServer({
server,
maxPayload: maxPayloadSize ?? 131072, // 128 kB
perMessageDeflate: {
zlibDeflateOptions: {
chunkSize: 1024,
memLevel: 7,
level: 3,
},
zlibInflateOptions: {
chunkSize: 10 * 1024,
},
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
serverMaxWindowBits: 10, // Defaults to negotiated value.
// Below options specified as default values.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024, // Size (in bytes) below which messages
// should not be compressed if context takeover is disabled.
},
})
const adapter = new WebSocketServerAdapter(
server,
Expand Down

0 comments on commit 0954d84

Please sign in to comment.