all repos — wsabi @ 04e6bb49fd7bd6f8575872fb9ec2e6004f03b091

websocket proxy that sends stats to statsd

Maintain remote and local streams separately
Anirudh Oppiliappan x@icyphox.sh
Tue, 21 Jul 2020 19:18:45 +0530
commit

04e6bb49fd7bd6f8575872fb9ec2e6004f03b091

parent

40d0b91db3691306d76c06c22e94eab5a7ab6d20

1 files changed, 22 insertions(+), 11 deletions(-)

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

@@ -11,6 +11,7 @@

var server = newAsyncHttpServer() remote: WebSocket + client: WebSocket proc connectRemote(host: string) {.async.} = remote = await newWebSocket(host, protocol = "xmpp")

@@ -19,11 +20,19 @@ hostPort = remote.tcpSocket.getPeerAddr()

address = hostPort[0] port = hostPort[1].int echo fmt"connected to {address}:{port}" + +# Fetch from remote and send to client +proc commRemoteClient() {.async.} = + while remote.readyState == Open: + var data = await remote.receiveStrPacket() + echo "from remote: ", data + await client.send(data) + # Sends a message to, and returns a response from the remote -proc sendRemote(ws: WebSocket, data: string): Future[string] {.async.} = - await ws.send(data) - result = await ws.receiveStrPacket() +# proc sendRemote(ws: WebSocket, data: string): Future[string] {.async.} = +# await ws.send(data) +# result = await ws.receiveStrPacket() proc localServer(req: Request) {.async, gcsafe.} = if req.url.path == "/ws":

@@ -34,7 +43,7 @@ let bits = parseUri(remoteHost[0])

newreq.headers["host"] = @[fmt"{bits.hostname}:{bits.port}"] newreq.headers["hostname"] = @[bits.hostname] - var client = await newWebSocket(newreq, protocol = "xmpp") + client = await newWebSocket(newreq, protocol = "xmpp") try: while client.readyState == Open:

@@ -42,14 +51,13 @@ # TODO: don't hardcode these replacements

var packet = await client.receiveStrPacket() repacket = packet.replace("127.0.0.1", by=bits.hostname) - echo "local msg: " & repacket + echo "from local: " & repacket # TODO: don't hardcode these replacements - var - res = await sendRemote(remote, repacket) - reres = res.replace(bits.hostname, by="127.0.0.1") - echo "sending response: ", reres - await client.send(reres) + await remote.send(repacket) + # reres = res.replace(bits.hostname, by="127.0.0.1") + # echo "got response: ", reres + # await client.send(reres) except WebSocketError: echo "client closed socket: ", getCurrentExceptionMsg()

@@ -59,5 +67,8 @@ parseArgs()

echo "connecting to remote host..." waitFor connectRemote(remoteHost[0]) echo fmt"local server listening on ws://127.0.0.1:{localPort}" - waitFor server.serve(Port(localPort), localServer) + asyncCheck server.serve(Port(localPort), localServer) + asyncCheck commRemoteClient() + + runForever()