websocket2mysql

0.0.4 • Public • Published

wss2mysql gateway

This is a simple secure webscoket to mysql gateway.

Use to run stored procedures from a secure weboskcet client on mysql.

For security reasons, only procedures are allowed.

See the source on index.js, the index.js has a complete example for a client and server implementation.

How to Test

Fist, set all ENV vars before runing the index.js Example:

#!/bin/bash
export WEBSOCKET_CLIENT_HOST = 'localhost';
export WEBSOCKET_PROTOCOL = 'ws2mysql-protocol';
export WEBSOCKET_PORT = 3001;
export WEBSOCKET_CERT_FILE = '/etc/ssl/certs/ssl-cert-snakeoil.pem';
export WEBSOCKET_CERT_KEY = '/etc/ssl/private/ssl-cert-snakeoil.key';
export WEBSOCKET_CERT_CA = '';
export MYSQL_HOST = 'localhost';
export MYSQL_USER = 'root';
export MYSQL_PASSWORD = '';
export MYSQL_DB = 'test';
export MYSQL_PORT = '3306';
nodejs ./index.js

How to configure the websocket connection:

You need to set the ws2mysql-protocol as websocket protocol.

You need to set a origin to authenticate, now you can use DEBUG-ORIGIN

Basic application flow:

Basic application flow:

This is script are initialized on main() funcion.

In the main() function you have more two functions:

- server(): that is, start the websocket server gateway.
- client(): a simple client to test this script.
    - NODE_ENV=testing is configured by default, when you run
      this script on a server just set NODE_ENV=prod to disable the client.

How to run from a websocket client:

Just send the following message via websocket to this server:

const message = '{"proc":"PROCEDURE_NAME","callback":"ON_MYSQL_DATA","args":["user@example.com","PASS"]}';

Then, server will respond to websocket client with this:

const response = '{"proc":"PROCEDURE_NAME","callback":"ON_MYSQL_DATA","args":[{"id":2,"name":"test","email":"user@example.com","pass":"test"}]}';

How to run this script a service on Ubuntu:

Just save the following contents in the file:

WS2MYSQL_CERT_FILE: you need to change to a valid ssl certificate, get one with Let's Encript. WS2MYSQL_CERT_KEY: you need to change to a valid ssl key.

/etc/systemd/system/nodejs.service

[Service]
ExecStart=/usr/bin/nodejs /home/ubuntu/websocket2mysql/index.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs
User=ubuntu
Environment=WS2MYSQL_MYSQL_HOST=localhost
Environment=WS2MYSQL_MYSQL_USER=test
Environment=WS2MYSQL_MYSQL_PASSWORD=test
Environment=WS2MYSQL_MYSQL_DB=test
Environment=WS2MYSQL_MYSQL_PORT=3306
Environment=WS2MYSQL_CERT_FILE=/etc/ssl/certs/ssl-cert-snakeoil.pem
Environment=WS2MYSQL_CERT_KEY=/etc/ssl/private/ssl-cert-snakeoil.key
Environment=WS2MYSQL_CERT_CA=/etc/ssl/gd_bundle-g2-g1.crt
Environment=WEBSOCKET_CLIENT_HOST=my-cool-notebook.example.com
Environment=NODE_ENV=pro
[Install]
WantedBy=multi-user.target

After, just run "service nodejs start" to reload

Package Sidebar

Install

npm i websocket2mysql

Weekly Downloads

2

Version

0.0.4

License

ISC

Last publish

Collaborators

  • wendelmaques