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 133 134 135 136 137 138 139 140 |
import ws, asyncdispatch, asynchttpserver, strformat, asyncnet, wsabipkg/[args,httputils], httpclient, strutils, json, uri # var # server = newAsyncHttpServer() # remote: WebSocket # client: WebSocket # clients: newSeq[WebSocket]() type Client = object ws: WebSocket remote: WebSocket var server = newAsyncHttpServer() client: Client clients = newSeq[Client]() # proc connectRemote(remote: WebSocket, host: string) {.async.} = # remote = await newWebSocket(host, protocol = "xmpp") # let (address, port) = remote.tcpSocket.getPeerAddr() # echo fmt"connected to {address}:{port.int}" proc commRemoteClient(c: Client) {.async.} = ## Fetch from remote and send to client while c.remote.readyState == Open: echo "yes open" var data = await c.remote.receiveStrPacket() echo "from remote: ", data await c.ws.send(data 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] client.ws = await newWebSocket(newreq, protocol = "xmpp") 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 try: while client.ws.readyState == Open: # TODO: don't hardcode these replacements var packet = await client.ws.receiveStrPacket() repacket = packet.replace("127.0.0.1", by=bits.hostname) echo "from local: " & repacket await client.remote.send(repacket) echo "sent local packet" asyncCheck commRemoteClient(client) except WebSocketError: echo "client closed socket: ", getCurrentExceptionMsg() ## 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) echo "calling commRemoteClient" runForever() |