ianic / websocket.zig

websocket protocol in zig
MIT License
16 stars 4 forks source link

Index out of bounds in stream.zig #1

Open jdgriffith opened 9 months ago

jdgriffith commented 9 months ago

I'm experiencing an index out of bounds error and I'm not sure there is something I can do to correct this in my code since it merely listens for the next message.

https://github.com/ianic/websocket.zig/blob/main/src/stream.zig#L238

Screenshot 2023-11-20 at 9 30 51 AM

Let me know if there is more info I can give to help diagnose this.

ianic commented 9 months ago

Seams that Zig's tls implementation still has some open issues. I didn't connect any of them with this particular error but let's try to remove it as dependency.

We can use some sidecar tcp->tls proxy. I tried with envoy. Downloaded bin for my platform, make config and start it.

config.yml:

static_resources:
  listeners:
  - address:
      socket_address:
        address: 0.0.0.0
        port_value: 10000
    filter_chains:
    - filters:
      - name: envoy.filters.network.tcp_proxy
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
          cluster: service-https
          stat_prefix: wss_passthrough
  clusters:
  - name: service-https
    type: STRICT_DNS
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: service-https
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: socket.polygon.io
                port_value: 443
    transport_socket:
      name: envoy.transport_sockets.tls
      typed_config:
        "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
 ./envoy-1.28.0-linux-aarch_64 --config-path ./config.yaml

This example connect to the remote wss endpoint through local proxy which handles tls.

const std = @import("std");
const ws = @import("ws");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const uri = "wss://socket.polygon.io/stocks";
    const proxy_host = "127.0.0.1";
    const proxy_port = 10000;

    var tcp = try std.net.tcpConnectToHost(allocator, proxy_host, proxy_port);
    defer tcp.close();
    var cli = try ws.client(allocator, tcp.reader(), tcp.writer(), uri);
    defer cli.deinit();

    while (cli.nextMessage()) |msg| {
        defer msg.deinit();
        std.debug.print("{s}\n", .{msg.payload});
    }
}

Getting result:

[{"ev":"status","status":"connected","message":"Connected Successfully"}]
[{"ev":"status","status":"auth_timeout","message":"No authentication request received."}]
jdgriffith commented 9 months ago

Ok I will try this and let you know what happens.

jdgriffith commented 9 months ago

So from my testing today during market open, things are stable. I think you are right - this is related to open issues in Zig currently.