Nitin
Nitin

Reputation: 7667

How to create a HTTPS server that listens on my IP address

In Python Flask, I tried creating an ad hoc and OpenSSL based certificate as shared in https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https

I also tried the approach where we create root CA, trust it and create certificates as shared in https://github.com/dakshshah96/local-cert-generator/issues/13 with Python Flask and Express JS

But I am unable to talk to https://192.168.0.5:5000 hosted on my Mac, from a different device without manually approving the site on every load. Ultimately, I wish to talk to this server over a web socket, which fails as the site requires manual approval. Any other ideas on how to make this multi-system setup work?

Upvotes: 0

Views: 1403

Answers (1)

Aqib Mukhtar
Aqib Mukhtar

Reputation: 341

The example which I am going to deliver will run your application in the local network and it will be accessible using your computer (server) IP address over HTTPS.

You need to install OpenSSL to generate a self-signed certificate.

Create a project folder.

Open CMD and navigate to your project folder.

Run the following command on CMD

openssl req -nodes -new -x509 -keyout server.key -out server.cert

It will ask you some questions, answer them.

In your project folder, create app.js file and insert the following code:

let express = require('express')
let fs = require('fs')
let https = require('https')
let app = express()

app.get('/', function (req, res) {
  res.send('hello world')
})

https.createServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert')
}, app)
.listen(3000, function () {
  console.log('Example app listening on port 3000! Go to https://localhost:3000/')
})

Find your local IP address using ipconfig command. Your IP address will appear something like

IPv4 Address. . . . . . . . . . . : 192.168.1.124

Run your NodeJs server by using the command:

node app.js

You will get the following message:

Example app listening on port 3000! Go to https://localhost:3000/

Go to the browser and visit https://your_ip:3000/;

You will get a warning, ignore it. This is to alert that the certificate which you are using is a self-signed certificate and not issued by CA.

You can use the link to visit your application from any other device on your network. If you are tired of port 3000, you can use port 443 which is the default port for HTTPS. In that case, your URL will be simplified to something like https://your_ip/

I have verified through Wireshark that data is encrypted now!!

To answer this question, I took help from https://flaviocopes.com/express-https-self-signed-certificate/

Upvotes: 1

Related Questions