Loading...

Socket-proxy guide

Basic tutorial
31.07.2024
8
4
2
Avatar
Author
Thomas Dalla Piazza

In this guide, you will be guided through the setup of the required socket proxy/bridge used for plc-playground.

Setup

The simulation models on plc-playground are running in the browser meaning that all the code is run in a sandbox for security reasons. This sandbox allows only limited access to the network through mainly two protocols, HTTP requests en Websockets. Plc-playground models uses the second to establish the communication between the plc and the simulated model.

Because standard industrial communications are build on raw sockets (TCP, UDP, etc...), a proxy translating the websocket to raw socket is needed. The simulated systems on plc-playground are setup to use the proxy/bridge shipped with the emscripten project. The source code of that bridge can be found on github in the tools section (websocket_to_posix_proxy).

This tool is made to run on POSIX sockets so users under windows will need to run it from WSL. At the day of writing, attemps to run that tool directly in windows or in a Docker container where unsuccessfull. So the method to run it from WSL is presented here. The transcription of this method for linux or Mac users is straight forward.

Install WSL

To install WSL on windows, simply follow the steps described on the windows lears dedicated page: https://learn.microsoft.com/en-us/windows/wsl/install.

This will install Ubuntu which is absolutely fine for our use.

Download and build websocket-to-posix-proxy tool

Start WSL and in the terminal, issue the following commands to first update Ubuntu to the latest versions of installed softwares:

sudo apt update
sudo apt upgrade -y

Now that your WSL system is updated, install the required prerequist softwares by issuing:

sudo apt install -y build-essential cmake git

Clone the emscripten repo to get the tool source code:

git clone https://github.com/emscripten-core/emscripten.git

Navigate to the websocket_to_posix_proxy directory:

cd emscripten/tools/websocket_to_posix_proxy

Create a build directory and move into it:

mkdir build && cd build

Finally configure and build the websocket_to_posix_proxy executable:

cmake ..
make

The first line configure the build and the second actually compile the tool. Check that the build is successfull. To test the proxy, still in the build directory issue the following command:

./websocket_to_posix_proxy 8090

The output of the terminal should be:

websocket_to_posix_proxy server is now listening for WebSocket connections to ws://localhost:8090/

Now when loading a model page for example that one, additionnal outputs should be printed in the terminal:

Established new proxy connection handler thread for incoming connection, at fd=4
hashing key: "E5Rp0jDjB3uFshtGIUnDoA==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
Sent handshake:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: O7+QZX+MK6T/sOx7P0jrqaBjKM8=


Unsupported MUSL SOCK_CLOEXEC passed!

The warning of the last line can be ignored.

Start the proxy at wsl startup (optional)

If you whish, you can add a line in your .bashrc file to start the proxy at each wsl startup simplifying the setup when you use plc-playground. To do so, issue the following command:

echo "~/emscripten/tools/websocket_to_posix_proxy/build/websocket_to_posix_proxy 8090 &" >> ~/.bashrc
35 min
Related content:
Top