Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to connect to server on wss protocol using libwebsockets #3333

Open
seniorHelloWorldDeveloper opened this issue Feb 8, 2025 · 5 comments

Comments

@seniorHelloWorldDeveloper
Copy link

seniorHelloWorldDeveloper commented Feb 8, 2025

I've tried using JavaScript to connect to websocket server on "wss://example.com/trade/s/
But now I am trying to rewrite it to C using libwebsockets, my code is:

static struct lws_protocols protocols[] = {
{ "ws-protocols", callback_websocket, 0, 4096 }
};

int initWebSocket() {

struct lws_context_creation_info info;
memset(&info, 0, sizeof(info));
info.port = 443;
info.gid = -1;
info.uid = -1;
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
info.protocols = protocols;

info.client_ssl_ca_filepath = "C:\\certs\\ca-bundle.crt";



struct lws_context* context = lws_create_context(&info);
if (!context) {
    printf("Error creating websocket context\n");
    return -1;
}
else {
    printf("Created websocket context");
}
struct lws_client_connect_info ccinfo;
memset(&ccinfo, 0, sizeof(ccinfo));
ccinfo.context = context;

ccinfo.address = "example.com";
ccinfo.port = 443;
ccinfo.path = "wss://example.com/trade/s/";
ccinfo.host = "example.com";
ccinfo.origin = "example.com";
ccinfo.protocol = "ws-protocols";
ccinfo.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
ccinfo.ssl_connection |= LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
ccinfo.ssl_connection |= LCCSCF_USE_SSL;

struct lws* wsi = lws_client_connect_via_info(&ccinfo);
if (!wsi) {
    printf("Failed to connect to the server\n");
    lws_context_destroy(context);
    return -1;
}

while (1) {
    lws_service(context, 0);
}

lws_context_destroy(context);
return 0;

}

But i keep getting 502 bad gateway.. I dont really get the difference between address, path, host, oorigin but I tried my best to find any info on the internet, also tried path: /trade/s/

@lws-team
Copy link
Member

lws-team commented Feb 8, 2025

ccinfo.path is not a url. It's just the path part, probably, "trade/s" in your case.

If example.com has real certs for its https server, you do not want ALLOW_SELFSIGNED or SKIP_SERVER_CERT_HOSTNAME_CHECK either.

@seniorHelloWorldDeveloper
Copy link
Author

Thank you, tried this one but still getting the same error, here my logs after importing certs

[2025/02/08 13:06:23:3063] D: _realloc: size 72: client ctx tcr
[2025/02/08 13:06:23:3073] I: lws_tls_client_create_vhost_context: vh default: created new client ctx 0
[2025/02/08 13:06:24:3654] I: loaded ssl_ca_filepath
[2025/02/08 13:06:24:3654] I: created client ssl context for default
[2025/02/08 13:06:24:3684] I: _lws_vhost_init_server_af: af 2
[2025/02/08 13:06:24:3694] N: [vh|0|default||443]: lws_socket_bind: source ads 0.0.0.0
[2025/02/08 13:06:24:3694] D: _realloc: size 1280: __lws_wsi_create_with_role
[2025/02/08 13:06:24:3704] D: : lws_role_transition: wsistate 0x200, ops listen
[2025/02/08 13:06:24:3714] D: _lws_vhost_init_server_af: lws_socket_bind says 443
[2025/02/08 13:06:24:3724] D: : lws_vhost_bind_wsi: vh default: wsi listen/ws-protocols, count_bound_wsi 1
[2025/02/08 13:06:24:3724] D: : __insert_wsi_socket_into_fds: tsi=0, sock=992, pos-in-fds=1
[2025/02/08 13:06:24:3734] N: __lws_lc_tag: ++ [wsi|1|listen|default||443] (2)
[2025/02/08 13:06:24:3734] I: Listening on (null):443
[2025/02/08 13:06:24:3744] I: _lws_vhost_init_server_af: af 23
[2025/02/08 13:06:24:3764] N: [vh|0|default||443]: lws_socket_bind: source ads ::
[2025/02/08 13:06:24:3764] D: _realloc: size 1280: __lws_wsi_create_with_role
[2025/02/08 13:06:24:3774] D: : lws_role_transition: wsistate 0x200, ops listen
[2025/02/08 13:06:24:3784] D: _lws_vhost_init_server_af: lws_socket_bind says 443
[2025/02/08 13:06:24:3804] D: : lws_vhost_bind_wsi: vh default: wsi listen/ws-protocols, count_bound_wsi 2
[2025/02/08 13:06:24:3834] D: : __insert_wsi_socket_into_fds: tsi=0, sock=996, pos-in-fds=2
[2025/02/08 13:06:24:3844] N: __lws_lc_tag: ++ [wsi|2|listen|default||443] (3)
[2025/02/08 13:06:24:3854] I: Listening on (null):443
[2025/02/08 13:06:24:3864] I: lws_create_context: mem: per-conn: 1280 bytes + protocol rx buf
[2025/02/08 13:06:24:3874] D: lws_cancel_service:
Created websocket context[2025/02/08 13:06:24:3894] I: lws_protocol_init:
[2025/02/08 13:06:24:3894] D: [vh|0|default||443]: lws_protocol_init_vhost: not instantiating ws-protocols
[2025/02/08 13:06:24:3914] I: [vh|0|default||443]: lws_protocol_init_vhost: init default.ws-protocols
[2025/02/08 13:06:24:3924] D: _realloc: size 1280: __lws_wsi_create_with_role
[2025/02/08 13:06:24:3934] D: : lws_role_transition: wsistate 0x200, ops (unset)
[2025/02/08 13:06:24:3954] D: : lws_vhost_bind_wsi: vh default: wsi none/none, count_bound_wsi 3
[2025/02/08 13:06:24:4014] D: _realloc: size 336: client ws struct
[2025/02/08 13:06:24:4024] D: : lws_role_transition: wsistate 0x10000200, ops h1
[2025/02/08 13:06:24:4034] I: : lws_client_connect_via_info: role binding to h1
[2025/02/08 13:06:24:4054] I: : lws_client_connect_via_info: vh default protocol binding to ws-protocols
[2025/02/08 13:06:24:4064] D: : lws_ensure_user_space: protocol pss 0, user_space=0000000000000000
[2025/02/08 13:06:24:4084] I: : lws_client_connect_via_info: : h1 ws-protocols entry
[2025/02/08 13:06:24:4104] D: _realloc: size 164: client stash
[2025/02/08 13:06:24:4144] N: __lws_lc_tag: ++ [wsicli|0|WS/h1/default/example.com] (1)
[2025/02/08 13:06:24:4154] I: lws_header_table_attach: [wsicli|0|WS/h1/default/example.com]: ah 0000000000000000 (tsi 0, count = 0) in
[2025/02/08 13:06:24:4175] D: _realloc: size 984: ah struct
[2025/02/08 13:06:24:4184] D: _realloc: size 4096: ah data
[2025/02/08 13:06:24:4204] I: _lws_create_ah: created ah 000001FCBEEFCE90 (size 4096): pool length 1
[2025/02/08 13:06:24:4214] I: lws_header_table_attach: did attach wsi [wsicli|0|WS/h1/default/example.com]: ah 000001FCBEEFCE90: count 1 (on exit)
[2025/02/08 13:06:24:4234] D: [wsicli|0|WS/h1/default/example.com]: __lws_set_timeout: 10 secs, reason 25
[2025/02/08 13:06:24:4254] D: [wsicli|0|WS/h1/default/example.com]: lws_http_client_connect_via_info2: stash 000001FCBEF5E880
[2025/02/08 13:06:24:4274] D: _realloc: size 21: strdup
[2025/02/08 13:06:24:4314] D: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_2_dnsreq: new conn on no pipeline flag
[2025/02/08 13:06:24:4334] D: [wsicli|0|WS/h1/default/example.com]: lwsi_set_state: lwsi_set_state 0x10000200 -> 0x10000201
[2025/02/08 13:06:24:4344] I: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_2_dnsreq: lookup example.com:443
[2025/02/08 13:06:24:4364] I: [wsicli|0|WS/h1/default/example.com]: lws_getaddrinfo46: getaddrinfo 'example.com' says 0
[2025/02/08 13:06:24:4374] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns: sort_dns: 000001FCBEF46E90
[2025/02/08 13:06:24:4384] D: _realloc: size 144: lws_sort_dns
[2025/02/08 13:06:24:4394] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns: unsorted entry (af 23) 2606:4700:4400::ac40:9079
[2025/02/08 13:06:24:4404] D: _realloc: size 144: lws_sort_dns
[2025/02/08 13:06:24:4434] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns: unsorted entry (af 23) 2606:4700:4400::6812:2b87
[2025/02/08 13:06:24:4484] D: _realloc: size 144: lws_sort_dns
[2025/02/08 13:06:24:4504] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns: unsorted entry (af 2) 104.18.43.135
[2025/02/08 13:06:24:4514] D: _realloc: size 144: lws_sort_dns
[2025/02/08 13:06:24:4524] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns: unsorted entry (af 2) 172.64.144.121
[2025/02/08 13:06:24:4534] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns_dump: 1: (23)2606:4700:4400::ac40:9079, gw (0)(unset), idi: 0, lbl: 0, prec: 0
[2025/02/08 13:06:24:4544] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns_dump: 2: (23)2606:4700:4400::6812:2b87, gw (0)(unset), idi: 0, lbl: 0, prec: 0
[2025/02/08 13:06:24:4554] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns_dump: 3: (2)104.18.43.135, gw (0)(unset), idi: 0, lbl: 0, prec: 0
[2025/02/08 13:06:24:4574] I: [wsicli|0|WS/h1/default/example.com]: lws_sort_dns_dump: 4: (2)172.64.144.121, gw (0)(unset), idi: 0, lbl: 0, prec: 0
[2025/02/08 13:06:24:4634] W: lws_plat_set_socket_options_ip: not implemented on windows platform
[2025/02/08 13:06:24:4634] D: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_3_connect: WAITING_CONNECT
[2025/02/08 13:06:24:4644] D: [wsicli|0|WS/h1/default/example.com]: lwsi_set_state: lwsi_set_state 0x10000201 -> 0x10000202
[2025/02/08 13:06:24:4654] D: [wsicli|0|WS/h1/default/example.com]: __insert_wsi_socket_into_fds: tsi=0, sock=1000, pos-in-fds=3
[2025/02/08 13:06:24:4664] D: [wsicli|0|WS/h1/default/example.com]: _lws_change_pollfd: fd 1000 events 768 -> 768
[2025/02/08 13:06:24:4674] D: [wsicli|0|WS/h1/default/example.com]: __lws_set_timeout: 20 secs, reason 2
[2025/02/08 13:06:24:4694] D: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_3_connect: connect: fd 1000 errno: 10035
[2025/02/08 13:06:24:4694] D: [wsicli|0|WS/h1/default/example.com]: _lws_change_pollfd: fd 1000 events 768 -> 784
[2025/02/08 13:06:24:4716] D: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_via_info: adoption cb 19 to h1 ws-protocols
[2025/02/08 13:06:24:4734] D: [wsicli|0|WS/h1/default/example.com]: _lws_change_pollfd: fd 1000 events 784 -> 768
[2025/02/08 13:06:24:4784] I: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_3_connect: source ads 2a0d:6fc2:5502:a901:708f:28a1:1da4:b2ee
[2025/02/08 13:06:24:4804] D: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_3_connect: going into connect_4
[2025/02/08 13:06:24:4804] I: [wsicli|0|WS/h1/default/example.com]: lws_client_connect_4_established: h1 ws-protocols client created own conn (raw 0) vh default st 0x202
[2025/02/08 13:06:24:4814] D: [wsicli|0|WS/h1/default/example.com]: lwsi_set_state: lwsi_set_state 0x10000202 -> 0x10000012
[2025/02/08 13:06:24:4824] D: [wsicli|0|WS/h1/default/example.com]: __lws_set_timeout: 15 secs, reason 8
[2025/02/08 13:06:24:4834] D: lws_http_client_socket_service: LRS_H1C_ISSUE_HANDSHAKE
[2025/02/08 13:06:24:4854] D: [wsicli|0|WS/h1/default/example.com]: _lws_change_pollfd: fd 1000 events 768 -> 768
[2025/02/08 13:06:24:4864] I: lws_tls_restrict_borrow: 0 -> 1
[2025/02/08 13:06:24:4874] N: lws_gate_accepts: on = 0
[2025/02/08 13:06:24:4944] I: lws_tls_reuse_session: no existing session for default_example.com_443
[2025/02/08 13:06:24:4954] I: h1 client conn using alpn list 'http/1.1'
[2025/02/08 13:06:24:5044] D: lws_ssl_get_error: 000001FCBEFB32E0 -1 -> 2 (errno 10035)
[2025/02/08 13:06:24:5054] D: [wsicli|0|WS/h1/default/example.com]: lwsi_set_state: lwsi_set_state 0x10000012 -> 0x10000204
[2025/02/08 13:06:24:5074] D: lws_client_create_tls: lws_ssl_client_connect1: 0
[2025/02/08 13:06:24:5094] D: _lws_state_transition: system: changed 1 'CONTEXT_CREATED' -> 2 'INITIALIZED'
[2025/02/08 13:06:24:5114] D: _lws_state_transition: system: changed 2 'INITIALIZED' -> 3 'IFACE_COLDPLUG'
[2025/02/08 13:06:24:5124] D: _lws_state_transition: system: changed 3 'IFACE_COLDPLUG' -> 4 'DHCP'
[2025/02/08 13:06:24:5134] D: _lws_state_transition: system: changed 4 'DHCP' -> 5 'CPD_PRE_TIME'
[2025/02/08 13:06:24:5144] D: _lws_state_transition: system: changed 5 'CPD_PRE_TIME' -> 6 'TIME_VALID'
[2025/02/08 13:06:24:5154] D: _lws_state_transition: system: changed 6 'TIME_VALID' -> 7 'CPD_POST_TIME'
[2025/02/08 13:06:24:5164] D: _lws_state_transition: system: changed 7 'CPD_POST_TIME' -> 8 'POLICY_VALID'
[2025/02/08 13:06:24:5184] D: _lws_state_transition: system: changed 8 'POLICY_VALID' -> 9 'REGISTERED'
[2025/02/08 13:06:24:5194] D: _lws_state_transition: system: changed 9 'REGISTERED' -> 10 'AUTH1'
[2025/02/08 13:06:24:5224] D: _lws_state_transition: system: changed 10 'AUTH1' -> 11 'AUTH2'
[2025/02/08 13:06:24:5244] D: _lws_state_transition: system: changed 11 'AUTH2' -> 12 'OPERATIONAL'
[2025/02/08 13:06:24:5254] I: lws_state_transition_steps: CONTEXT_CREATED -> OPERATIONAL
[2025/02/08 13:06:24:5564] D: lws_ssl_get_error: 000001FCBEFB32E0 1 -> 0 (errno 0)
[2025/02/08 13:06:24:5564] I: [wsicli|0|WS/h1/default/example.com]: lws_role_call_alpn_negotiated: 'http/1.1'
[2025/02/08 13:06:24:5594] D: rops_alpn_negotiated_h1: client 1
[2025/02/08 13:06:24:5604] I: client connect OK
[2025/02/08 13:06:24:5614] I: lws_openssl_describe_cipher: [wsicli|0|WS/h1/default/example.com]: TLS_AES_256_GCM_SHA384, TLS_AES_256_GCM_SHA384, 256 bits, TLSv1.3
[2025/02/08 13:06:24:5634] D: lws_ssl_client_connect2: SSL_connect says 0
[2025/02/08 13:06:24:5644] I: lws_tls_restrict_return_handshake: 1 -> 0
[2025/02/08 13:06:24:5654] N: lws_gate_accepts: on = 0
[2025/02/08 13:06:24:5675] I: lws_http_client_socket_service: HANDSHAKE2: [wsicli|0|WS/h1/default/example.com]: sending headers (wsistate 0x10000204), w sock 1000
[2025/02/08 13:06:24:5704] D: [wsicli|0|WS/h1/default/example.com]: lwsi_set_state: lwsi_set_state 0x10000204 -> 0x1000020b
[2025/02/08 13:06:24:5714] D: [wsicli|0|WS/h1/default/example.com]: __lws_set_timeout: 15 secs, reason 4
[2025/02/08 13:06:24:5724] D: [wsicli|0|WS/h1/default/example.com]: _lws_change_pollfd: fd 1000 events 768 -> 784
[2025/02/08 13:06:24:5734] D: [wsicli|0|WS/h1/default/example.com]: lwsi_set_state: lwsi_set_state 0x1000020b -> 0x1000020b
[2025/02/08 13:06:24:5744] D: [wsicli|0|WS/h1/default/example.com]: __lws_set_timeout: 15 secs, reason 4
[2025/02/08 13:06:24:5754] D: [wsicli|0|WS/h1/default/example.com]: _lws_change_pollfd: fd 1000 events 784 -> 768
[2025/02/08 13:06:24:5764] D: [wsicli|0|WS/h1/default/example.com]: _lws_change_pollfd: fd 1000 events 768 -> 768
[2025/02/08 13:06:26:3275] D: _realloc: size 113: lws_tls_session_add_entry
[2025/02/08 13:06:26:3275] I: lws_tls_session_new_cb: 000001FCBEDA72A0: [wsicli|0|WS/h1/default/example.com]: new default_example.com_443, ttl 7200s (default:1)
[2025/02/08 13:06:26:3315] I: lws_tls_session_new_cb: 000001FCBEECCCE0: [wsicli|0|WS/h1/default/example.com]: reuse default_example.com_443, ttl 7200s (default:1)
[2025/02/08 13:06:26:3325] D: [wsicli|0|WS/h1/default/example.com]: SSL_read says 785
[2025/02/08 13:06:26:3335] D: [wsicli|0|WS/h1/default/example.com]: lws_buflist_aware_read: lws_http_client_socket_service: ssl_capable_read 785
[2025/02/08 13:06:26:3355] D: lws_http_client_socket_service: buflist-aware-read 0 785
[2025/02/08 13:06:26:3365] D: lws_parse: unk header 16 'x-frame-options:'
[2025/02/08 13:06:26:3375] D: lws_parse: unk header 16 'referrer-policy:'
[2025/02/08 13:06:26:3385] D: lws_parse: unk header 24 'content-security-policy:'
[2025/02/08 13:06:26:3395] D: lws_parse: unk header 29 'cross-origin-embedder-policy:'
[2025/02/08 13:06:26:3415] D: lws_parse: unk header 27 'cross-origin-opener-policy:'
[2025/02/08 13:06:26:3425] D: lws_parse: unk header 29 'cross-origin-resource-policy:'
[2025/02/08 13:06:26:3435] D: lws_parse: unk header 7 'cf-ray:'
[2025/02/08 13:06:26:3455] D: lws_parse: unk header 8 'alt-svc:'
[2025/02/08 13:06:26:3465] I: lws_buflist_append_segment: len 15 first 1 0000000000000000
[2025/02/08 13:06:26:3485] D: _realloc: size 56: lws_buflist_append_segment
[2025/02/08 13:06:26:3525] D: [wsicli|0|WS/h1/default/example.com]: lws_buflist_aware_finished_consuming: added to rxflow list
[2025/02/08 13:06:26:3535] I: lws_client_interpret_server_handshake: incoming content length 15
[2025/02/08 13:06:26:3555] W: [wsicli|0|WS/h1/default/example.com]: lws_client_ws_upgrade: got bad HTTP response '502'
[2025/02/08 13:06:26:3565] I: closing connection (prot ws-protocols) due to bail2 connection error: HS: ws upgrade response not 101
[2025/02/08 13:06:26:3585] I: [wsicli|0|WS/h1/default/example.com]: __lws_close_free_wsi: caller: c hs interp
[2025/02/08 13:06:26:3605] I: [wsicli|0|WS/h1/default/example.com]: __lws_close_free_wsi: going down with stuff in buflist
[2025/02/08 13:06:26:3625] D: [wsicli|0|WS/h1/default/example.com]: __lws_close_free_wsi: real just_kill_connection A: (sockfd 1000)
[2025/02/08 13:06:26:3655] I: [wsicli|0|WS/h1/default/example.com]: __lws_close_free_wsi: real just_kill_connection: sockfd 1000
[2025/02/08 13:06:26:3675] D: [wsi|1|listen|default||443]: _lws_change_pollfd: fd 992 events 768 -> 768
[2025/02/08 13:06:26:3695] D: [wsi|2|listen|default||443]: _lws_change_pollfd: fd 996 events 768 -> 768
[2025/02/08 13:06:26:3705] D: [wsicli|0|WS/h1/default/example.com]: lwsi_set_state: lwsi_set_state 0x1000020b -> 0x10000020
[2025/02/08 13:06:26:3725] I: [wsicli|0|WS/h1/default/example.com]: __lws_close_free_wsi: cce=1
[2025/02/08 13:06:26:3745] I: lws_tls_restrict_return: 1 -> 0
[2025/02/08 13:06:26:3755] N: lws_gate_accepts: on = 0
[2025/02/08 13:06:26:3775] I: rops_destroy_role_h1: ah det due to close
[2025/02/08 13:06:26:3795] I: __lws_header_table_detach: [wsicli|0|WS/h1/default/example.com]: ah 000001FCBEEFCE90 (tsi=0, count = 1)
[2025/02/08 13:06:26:3835] I: __lws_header_table_detach: nobody usable waiting
[2025/02/08 13:06:26:3845] I: _lws_destroy_ah: freed ah 000001FCBEEFCE90 : pool length 0
[2025/02/08 13:06:26:3865] I: __lws_header_table_detach: [wsicli|0|WS/h1/default/example.com]: ah 000001FCBEEFCE90 (tsi=0, count = 0)
[2025/02/08 13:06:26:3865] D: [wsicli|0|WS/h1/default/example.com]: __lws_vhost_unbind_wsi: vh default: count_bound_wsi 2
[2025/02/08 13:06:26:3875] D: [wsicli|0|WS/h1/default/example.com]: __lws_free_wsi: tsi fds count 3
[2025/02/08 13:06:26:3885] N: __lws_lc_untag: -- [wsicli|0|WS/h1/default/example.com] (0) 1.974s
[2025/02/08 13:06:28:0073] I: [vh|0|default||443]: lws_tls_check_cert_lifetime: vhost default: no cert

@seniorHelloWorldDeveloper
Copy link
Author

It even gets into my callback to LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER and sets the headers I've set but still getting 502

@lws-team
Copy link
Member

lws-team commented Feb 8, 2025

... ultimately it's up to the server to choose if it likes what you sent or not, it's not up to lws.

A good trick is set -DLWS_TLS_LOG_PLAINTEXT_TX=1 -DLWS_TLS_LOG_PLAINTEXT_RX=1 on lws cmake and rebuild the lib, this will dump in plaintext what was sent and received on the link. It might be easier to see what's wrong then.

@seniorHelloWorldDeveloper
Copy link
Author

Sorry, my bad. The server uses socket-io server and after reading some issues about it I fount our that I need to do some stuff before, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants