<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vasyl K</title>
    <description>The latest articles on DEV Community by Vasyl K (@codert0109).</description>
    <link>https://dev.to/codert0109</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F889461%2F2439e104-7a7e-4293-a7f9-c93334346576.png</url>
      <title>DEV Community: Vasyl K</title>
      <link>https://dev.to/codert0109</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codert0109"/>
    <language>en</language>
    <item>
      <title>SSL context creation crashes of c++ native module in Electron application</title>
      <dc:creator>Vasyl K</dc:creator>
      <pubDate>Mon, 17 Jun 2024 02:33:16 +0000</pubDate>
      <link>https://dev.to/codert0109/ssl-context-creation-crashes-of-c-native-module-in-electron-application-307a</link>
      <guid>https://dev.to/codert0109/ssl-context-creation-crashes-of-c-native-module-in-electron-application-307a</guid>
      <description>&lt;p&gt;I am building a C++ native module to be used in an Electron application. The native module is responsible for communicating with a WebSocket server. I am using the WebSocketPP library and the following sample code:&lt;/p&gt;

&lt;p&gt;index.cc&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;websocketpp/config/asio_client.hpp&amp;gt;   // TLS
#include &amp;lt;websocketpp/client.hpp&amp;gt;
typedef websocketpp::client&amp;lt;websocketpp::config::asio_tls_client&amp;gt; client;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr&amp;lt;boost::asio::ssl::context&amp;gt; context_ptr;
using websocketpp::lib::bind;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
//
...
//
class WebSocketHandler
{
public:
    void set(const std::string &amp;amp;url, const std::string &amp;amp;token)
    {
        ws_url = url;
        authorizationHeader = "Bearer " + token;
        // Initialize ASIO
        _webSocket.init_asio();

        // Set logging to be pretty verbose (everything except message payloads)
        _webSocket.set_access_channels(websocketpp::log::alevel::all);
        _webSocket.clear_access_channels(websocketpp::log::alevel::frame_payload);

        // Set open handler
        _webSocket.set_open_handler(bind(&amp;amp;WebSocketHandler::on_open, this, std::placeholders::_1));

        // Set close handler
        _webSocket.set_close_handler(bind(&amp;amp;WebSocketHandler::on_close, this, std::placeholders::_1));

        // Set fail handler
        _webSocket.set_fail_handler(bind(&amp;amp;WebSocketHandler::on_fail, this, std::placeholders::_1));

        // Set message handler
        _webSocket.set_message_handler(bind(&amp;amp;WebSocketHandler::on_message, this, std::placeholders::_1, std::placeholders::_2));
        // Set TLS handler
        _webSocket.set_tls_init_handler(bind(&amp;amp;WebSocketHandler::on_tls_init, this, std::placeholders::_1));
    }

    void start()
    {
        websocketpp::lib::error_code ec;

        client::connection_ptr con = _webSocket.get_connection(ws_url, ec);
        if (ec)
        {
            std::cout &amp;lt;&amp;lt; "Could not create connection because: " &amp;lt;&amp;lt; ec.message() &amp;lt;&amp;lt; std::endl;
            return;
        }

        // Set the authorization header
        con-&amp;gt;replace_header("Authorization", authorizationHeader);

        // Connect to server
        _webSocket.connect(con);

        // Start the ASIO io_service run loop
        _thread.reset(new websocketpp::lib::thread(&amp;amp;client::run, &amp;amp;_webSocket));
    }

    void stop()
    {
        _webSocket.stop();
        if (_thread &amp;amp;&amp;amp; _thread-&amp;gt;joinable())
        {
            _thread-&amp;gt;join();
        }
    }

private:
    context_ptr on_tls_init(websocketpp::connection_hdl hdl)
    {
        context_ptr ctx = websocketpp::lib::make_shared&amp;lt;boost::asio::ssl::context&amp;gt;(boost::asio::ssl::context::sslv23);  // crash at this line

        try {
            // Simplified SSL options for testing
            ctx-&amp;gt;set_options(boost::asio::ssl::context::default_workarounds |
                             boost::asio::ssl::context::no_sslv2 |
                             boost::asio::ssl::context::no_sslv3 |
                             boost::asio::ssl::context::single_dh_use);
            std::cout &amp;lt;&amp;lt; "SSL options set successfully" &amp;lt;&amp;lt; std::endl;
        } catch (std::exception &amp;amp;e) {
            std::cout &amp;lt;&amp;lt; "Exception during set_options: " &amp;lt;&amp;lt; e.what() &amp;lt;&amp;lt; std::endl;
        }

        return ctx;
    }

    void on_open(websocketpp::connection_hdl hdl)
    {
        std::cout &amp;lt;&amp;lt; "connection opened" &amp;lt;&amp;lt; std::endl;
    }

    void on_close(websocketpp::connection_hdl hdl)
    {
        std::cout &amp;lt;&amp;lt; "connection closed" &amp;lt;&amp;lt; std::endl;
    }

    void on_fail(websocketpp::connection_hdl hdl)
    {
        std::cout &amp;lt;&amp;lt; "connection failed" &amp;lt;&amp;lt; std::endl;
    }

    void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg)
    {
        std::cout &amp;lt;&amp;lt; "message arrived" &amp;lt;&amp;lt; std::endl;
    }

    client _webSocket;
    std::string ws_url;
    std::string authorizationHeader;
};
//
...
//
WebSocketHandler handler;
handler.set("wss://echo.websocket.org/", "Token_xxxx");
handler.start();
....
handler.stop();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;binding.gyp&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "targets": [
    {
      "target_name": "binding",
      "include_dirs": [
        "&amp;lt;!@(node -p \"require('node-addon-api').include\")",
        "&amp;lt;(module_root_dir)/include"
      ],
      "conditions": [
        ['OS=="win"', {
          "sources": [
            "./src/index.cc"
          ],
          "configurations": {
            "Debug": {
              "msvs_settings": {
                "VCCLCompilerTool": {
                  "RuntimeLibrary": "0", 
                  "ExceptionHandling": "1"
                },
              },
            },
            "Release": {
              "msvs_settings": {
                "VCCLCompilerTool": {
                  "RuntimeLibrary": "0", 
                  "ExceptionHandling": "1"
                },
              },
            },
          },
          "libraries": [
            "-lws2_32",
            "-lShlwapi"
          ]
        }]
      ]
    }
  ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test Script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const engine = require("../bin/binding.node");
const test = async () =&amp;gt; {
    try {
        engine.startConnection();
    } catch (err) {
        console.log("Error occurred", err);
    }
};
test();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problem The module works correctly in a JavaScript test script but crashes in Electron at this line:&lt;/p&gt;

&lt;p&gt;context_ptr ctx = websocketpp::lib::make_shared&lt;a&gt;boost::asio::ssl::context&lt;/a&gt;(boost::asio::ssl::context::sslv23);&lt;br&gt;
I suspect the issue might be related to the way SSL libraries are linked. I feel linking SSL libraries statically might resolve the issue, but I am unsure how to achieve this. I tested with other several libraries based in boost but the result was same. It keeps crahsed in ssl context creation part only in electron application.&lt;/p&gt;

&lt;p&gt;Environment&lt;/p&gt;

&lt;p&gt;C++14/17&lt;br&gt;
Electron v23(version upgrade doesn't help)&lt;br&gt;
WebSocketPP 0.8.2&lt;br&gt;
Node 16.14.2/18.x.x&lt;br&gt;
Dependencies installed using vcpkg: OpenSSL, WebSocketPP, Boost&lt;br&gt;
Question&lt;/p&gt;

&lt;p&gt;How can I link SSL libraries statically in my project to potentially fix this issue? Are there any other possible solutions or insights regarding this problem?&lt;/p&gt;

&lt;p&gt;Thank you for your assistance!&lt;/p&gt;

</description>
      <category>question</category>
      <category>cpp</category>
      <category>websocketpp</category>
      <category>ssl</category>
    </item>
    <item>
      <title>Hi, everybody, Nice to meet you.</title>
      <dc:creator>Vasyl K</dc:creator>
      <pubDate>Sun, 10 Jul 2022 09:27:04 +0000</pubDate>
      <link>https://dev.to/codert0109/hi-everybody-nice-to-meet-you-4m7p</link>
      <guid>https://dev.to/codert0109/hi-everybody-nice-to-meet-you-4m7p</guid>
      <description>&lt;p&gt;Hi.&lt;br&gt;
I am Tani Kichiro from Japan.&lt;br&gt;
Nice to meet you.&lt;br&gt;
I want to develop my technical skills with you.&lt;br&gt;
I am full stack developer who has experience over 5 years.&lt;br&gt;
Mostly in ReactJS, VueJS, ExpressJS, MongoDB, MSSQL, NextJS and so on.&lt;br&gt;
I am learning React Native which is one of the hottest libraries in mobile app development.&lt;br&gt;
Thanks in advance to help me with technical issue and help me getting some jobs with above stacks.&lt;br&gt;
Thanks again for reading my first blog.&lt;br&gt;
Best Regards.&lt;/p&gt;

</description>
      <category>react</category>
      <category>mongodb</category>
      <category>vue</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
