all repos — wsabi @ d351f6e665f889ccc51c7dc808734c44e1f296a3

websocket proxy that sends stats to statsd

src/wsabi.nim (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
import
  ws, asyncdispatch, asynchttpserver, strformat, asyncnet, httpclient,
  strutils, json, uri
import wsabi/[httputils, args, stats]


type
  Client = object
    ws: WebSocket
    remote: WebSocket
    connected: bool

var
  server = newAsyncHttpServer()
  client: Client
  clients = newSeq[Client]()


proc commRemoteClient(c: Client) {.async.} =
  ## 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 c.ws.send(data)
  except OSError:
    discard
  except WebSocketError:
    discard

proc localServer(req: Request) {.async, gcsafe.} =
  ## Listen on localhost:PORT/ws
  if req.url.path == "/ws":
    var newreq = req

    # TODO: loop through for all remote hosts
    let bits = parseUri(remoteHost[0])
    newreq.headers["host"] = @[fmt"{bits.hostname}:{bits.port}"]
    newreq.headers["hostname"] = @[bits.hostname]

    try:
      client.ws = await newWebSocket(newreq, protocol = "xmpp")
      client.connected = true
      echo "connecting to remote host..."
      client.remote = await newWebSocket(remoteHost[0], protocol = "xmpp")
      let (address, port) = client.remote.tcpSocket.getPeerAddr()
      echo fmt"connected to {address}:{port.int}"
      clients.add client

      while client.ws.readyState == Open:
        let clientAddress = 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)

        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

  ## Health check endpoint 
  ## The 'uri' can be either 'ws' or 'http'.
  if req.url.path == "/check-health":
    if req.reqMethod == HttpPost:
      type 
        HealthCheck = object
          uri: string
          host: string
          port: int
      try:
        let
          hcJson = parseJson(req.body)
          hc = to(hcJson, HealthCheck)

        ## Check if host is a websocket, and try for the xmpp protocol
        if hc.uri in ["ws", "wss"]:
          let hostStr = &"{hc.uri}://{hc.host}:{hc.port}/ws/"
          echo &"connecting to {hostStr}"
          var ws = await newWebSocket(hostStr, protocol = "xmpp")
          echo "sending test packet"
          echo &"""<open xmlns="urn:ietf:params:xml:ns:xmpp-framing" to="{hc.host}" version="1.0" />"""
          await ws.send(&"""<open xmlns="urn:ietf:params:xml:ns:xmpp-framing" to="{hc.host}" version="1.0" />""")

          echo await ws.receiveStrPacket()
          ws.close()
          let r = %*{"status": "OK"}

          await req.makeResponse(Http200, $r)

        if hc.uri in ["http", "https"]:
          let hostStr = &"{hc.uri}://{hc.host}:{hc.port}/"
          echo &"connecting to {hostStr}"
          var tmpClient = newAsyncHttpClient()
          echo await tmpClient.getContent(hostStr)
          let r = %*{"status": "OK"}

          await req.makeResponse(Http200, $r)

        else:
          let r = %*{"status": "error", "msg": &"unknown URI {hc.uri}"}
          await req.makeResponse(Http400, $r)

      except OSError as e:
        echo "unable to reach specified host:port pair"
        let r = %*{
          "status": "error",
          "msg": e.msg
        }
        await req.makeResponse(Http400, $r)

      except KeyError as e:
        let r = %*{"status": "error", "msg": e.msg}
        await req.makeResponse(Http400, $r)

      except WebSocketError as e:
        let r = %*{"status": "error", "msg": e.msg}
        await req.makeResponse(Http500, $r)


when isMainModule:
  parseArgs()
  echo fmt"local server running at ws://127.0.0.1:{localPort}/ws"
  asyncCheck server.serve(Port(localPort), localServer)

  runForever()