Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django channels using secured WebSocket connection - WSS://

When I tried to run the Django application using sslserver as shown below,

python manage.py runsslserver

Errors:

Traceback:

Validating models...

System check identified no issues (0 silenced).
November 08, 2019 - 11:17:26
Django version 2.0.7, using settings 'dashboard_channels.settings'
Starting development server at https://127.0.0.1:8000/
Using SSL certificate: \lib\site-packages\sslserver\certs\development.crt
Using SSL key: \lib\site-packages\sslserver\certs\development.key
Quit the server with CTRL-BREAK.
[08/Nov/2019 11:18:33] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:34] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:35] "GET /static/js/jquery.js HTTP/1.1" 200 270575
Not Found: /ws/home
[08/Nov/2019 11:18:36] "GET /ws/home HTTP/1.1" 404 2134

Browser Console:

(index):31 WebSocket connection to 'wss://127.0.0.1:8000/ws/home' failed: Error during WebSocket handshake: Unexpected response code: 404
(index):41 error Event
(index):44 close CloseEvent

Code:

Javascript:

 var loc = window.location;
 var wsStart = 'ws://';
 if (loc.protocol == 'https:') {
     wsStart = 'wss://'
 }
 var endpoint = wsStart + loc.host + '/ws/home';

 var socket = new WebSocket(endpoint);

It's working fine with python manage.py runserver command, means for http it's working but not with https.

How to resolve this issue? (How to debug to sort out this issue?)

Is there any other way to deploy WebSockets on https portal?

Still facing this issue. Can anyone please help?

Anyhow this is for testing purposes, finally, I need to deploy it on Apache2.4 in the windows server machine. Where I have already set up for https but not for web sockets.

like image 942
shaik moeed Avatar asked Nov 08 '19 06:11

shaik moeed


People also ask

Is Django channels secure?

Django is a powerful Python framework for web development. It is fast, secure, and reliable. Channels allow Django projects to handle HTTP along with asynchronous protocols like WebSockets, MQTT, chatbots, and more.

What is WebSocket WSS?

The WebSocket protocol specification defines ws (WebSocket) and wss (WebSocket Secure) as two new uniform resource identifier (URI) schemes that are used for unencrypted and encrypted connections respectively.

Is Django good for WebSockets?

Django Channels facilitates support of WebSockets in Django in a manner similar to traditional HTTP views. It wraps Django's native asynchronous view support, allowing Django projects to handle not only HTTP, but also protocols that require long-running connections, such as WebSockets, MQTT, chatbots, etc.

How are WebSockets secured?

Like HTTPS, WSS (WebSockets over SSL/TLS) is encrypted, thus protecting against man-in-the-middle attacks. A variety of attacks against WebSockets become impossible if the transport is secured.


1 Answers

I found the answer, the runserver command correctly detects asgi.py file and run the Django channels application on WebSockets using daphne. Somehow runsslserver is not doing the same job, It's running the wsgi.py file instead of asgi.py file.

After reading different approaches, I get to know that we can handle HTTPS request using our normal development server (i.e., using wsgi.py file) and wss request by using Daphne(i.e., using asgi.py file).

Daphne was an officially designed server to handle django-channels(built on the top of the twisted module).

So, finally, we need to run two servers to handle https and wss separately.

# In command prompt 1 (For production, use Apache or Nginx to serve HTTP requests)
python manage.py runsslserver 0.0.0.0:8000

# In command prompt 2 (This works for production as well).
daphne -e ssl:8001:privateKey=cert\\private.pem:certKey=cert\\public.pem real_time_table.asgi:application

We should use the same SSL certificates used by runsslserver for testing.

Finally, in JavaScript:

var loc = window.location;
var wsStart = 'ws://';
if (loc.protocol == 'https:') {
     wsStart = 'wss://'
}
// var endpoint = wsStart + 'your_ip_address:port_given_to_daphne_server' + '/ws/home';
// For above command, it look like this
var endpoint = wsStart + 'xxx.xx.xx.xxx:8001' + '/ws/home';
// Note the websocket port is 8001
var socket = new WebSocket(endpoint);

I hope, this will save someone's time.

like image 97
shaik moeed Avatar answered Oct 27 '22 04:10

shaik moeed