all repos — wsabi @ d1e98d5c0415ebed969933008cf79135e672080b

websocket proxy that sends stats to statsd

Sent / recvd status in statsd
Anirudh Oppiliappan x@icyphox.sh
Fri, 07 Aug 2020 16:48:22 +0530
commit

d1e98d5c0415ebed969933008cf79135e672080b

parent

d351f6e665f889ccc51c7dc808734c44e1f296a3

2 files changed, 26 insertions(+), 17 deletions(-)

jump to
M src/wsabi.nimsrc/wsabi.nim

@@ -8,6 +8,7 @@ type

Client = object ws: WebSocket remote: WebSocket + netAddr: string connected: bool var

@@ -21,13 +22,11 @@ ## Fetch from remote and send to client

try: while c.remote.readyState == Open and c.connected: var data = await c.remote.receiveStrPacket() - await parseStanza(data) - echo "from remote: ", data + await parseStanza(data, client.netAddr, false) + echo data await c.ws.send(data) - except OSError: - discard except WebSocketError: - discard + echo getCurrentExceptionMsg() proc localServer(req: Request) {.async, gcsafe.} = ## Listen on localhost:PORT/ws

@@ -49,20 +48,21 @@ echo fmt"connected to {address}:{port.int}"

clients.add client while client.ws.readyState == Open: - let clientAddress = client.ws.tcpSocket.getPeerAddr()[0] + client.netAddr = client.ws.tcpSocket.getPeerAddr()[0] var packet = await client.ws.receiveStrPacket() - repacket = packet.replace(clientAddress, by=bits.hostname) - echo "from local: " & repacket - await parseStanza(repacket) + repacket = packet.replace(client.netAddr, by=bits.hostname) + echo repacket + await parseStanza(repacket, client.netAddr, true) await client.remote.send(repacket) echo "sent local packet" asyncCheck commRemoteClient(client) - except WebSocketError as e: - client.ws.close() - client.remote.close() - echo "client closed socket: ", e.msg + except WebSocketError: + echo "socket closed: ", getCurrentExceptionMsg() +# client.ws.close() +# client.remote.close() +# echo "client closed socket: ", e.msg ## Health check endpoint ## The 'uri' can be either 'ws' or 'http'.
M src/wsabi/stats.nimsrc/wsabi/stats.nim

@@ -3,15 +3,15 @@

import xmltree, xmlparser, asyncdispatch, asyncnet, nativesockets, - strformat, + strformat, strutils, tables var statsd: AsyncSocket clientAddr: string + prefix: string counter = initCountTable[string]() -const prefix = "wsabi.dev." let tags = @["message", "auth", "success", "failure"]

@@ -24,9 +24,18 @@ if counter[tag] != 0:

waitFor statsd.send(&"{prefix}{tag}:{counter[tag]}|c") counter[tag] = 0 -proc parseStanza*(stanza: string) {.async.} = +proc parseStanza*(stanza: string, ca: string, sent: bool) {.async.} = + ## 'sent' is bool -- either client sent, or recvd pkt try: - let p = parseXml(stanza) + let + p = parseXml(stanza) + reca = ca.replace(".", "-") + + if sent: + prefix = &"wsabi.dev.{reca}.sent." + else: + prefix = &"wsabi.dev.{reca}.recvd." + try: if p.tag in tags: counter.inc(p.tag)