Skip to content

Commit

Permalink
Add SSL to Websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
ajamias committed Nov 5, 2024
1 parent afad650 commit bcd05c0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
37 changes: 26 additions & 11 deletions esi_ui/api/esi_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio
from websockets.asyncio.server import serve
from websockets.exceptions import ConnectionClosed
from threading import Thread
import json
import concurrent.futures
from collections import defaultdict
import ssl

from django.conf import settings

Expand All @@ -21,6 +23,8 @@

DEFAULT_WEBSOCKET_PORT = 10000

DEFAULT_POLL_INTERVAL_SECONDS = 10


def get_session(token, project_id):
auth_url = getattr(settings, 'OPENSTACK_KEYSTONE_URL', DEFAULT_OPENSTACK_KEYSTONE_URL)
Expand Down Expand Up @@ -237,20 +241,31 @@ def delete_lease(request, lease):
return esiclient(request).lease.delete_lease(lease)


async def on_websocket_connect(websocket):
token = await websocket.recv(decode=True)
project_id = await websocket.recv(decode=True)
request = {"token": token, "project_id": project_id}
async def websocket_listen(ssl_context):
async def on_websocket_connect(websocket):
try:
token = await websocket.recv(decode=True)
project_id = await websocket.recv(decode=True)

except ConnectionClosed:
return
request = {"token": token, "project_id": project_id}

while True:
list_of_nodes = node_list(request, from_websocket=True)

while True:
list_of_nodes = node_list(request, from_websocket=True)
await websocket.send(json.dumps(list_of_nodes))
await asyncio.sleep(10)
try:
await websocket.send(json.dumps(list_of_nodes))
except ConnectionClosed:
break

await asyncio.sleep(getattr(settings, 'DEFAULT_POLL_INTERVAL_SECONDS', DEFAULT_POLL_INTERVAL_SECONDS))

async def websocket_listen():
async with serve(on_websocket_connect, 'localhost', getattr(settings, 'DEFAULT_WEBSOCKET_PORT', DEFAULT_WEBSOCKET_PORT)):
async with serve(on_websocket_connect, '0.0.0.0', getattr(settings, 'DEFAULT_WEBSOCKET_PORT', DEFAULT_WEBSOCKET_PORT), ssl=ssl_context):
await asyncio.get_running_loop().create_future()

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
ssl_context.load_cert_chain(settings.SSL_CERTIFICATE_PATH, keyfile=settings.SSL_KEY_PATH)
ssl_context.load_verify_locations(cafile=settings.SSL_CERTIFICATE_PATH)

Thread(target=asyncio.run, args=(websocket_listen(),)).start()
Thread(target=asyncio.run, args=(websocket_listen(ssl_context),)).start()
4 changes: 2 additions & 2 deletions esi_ui/static/dashboard/esi/esi.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
var stack = [];
var onmessageDefer;
var socket = {
socket: new WebSocket('ws://127.0.0.1:10000'),
socket: new WebSocket('wss://' + window.location.hostname + ':10000'),
send: function(data) {
data = JSON.stringify(data);
if (socket.socket.readyState === WebSocket.OPEN) {
Expand Down Expand Up @@ -168,4 +168,4 @@
}
}

})();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

////////////////

spinnerService.showModalSpinner('Getting Nodes');
init();

function init() {
Expand All @@ -89,6 +90,7 @@
});

esiService.socket().onmessage(function(message) {
spinnerService.hideModalSpinner();
$scope.$apply(function() {
ctrl.nodesSrc = JSON.parse(message.data);
ctrl.nodesDisplay = ctrl.nodesSrc.filter(function(node) {
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ esisdk>=0.5.0 # Apache-2.0
horizon>=24.0.0
metalsmith>=2.3.0
keystoneauth1>=4.3.1 # Apache-2.0
websockets>=13.1

0 comments on commit bcd05c0

Please sign in to comment.