Skip to content

Added QR Code for public IP #738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This will install `http-server` globally so that it may be run from the command
|`-S`, `--tls` or `--ssl` |Enable secure request serving with TLS/SSL (HTTPS)|`false`|
|`-C` or `--cert` |Path to ssl cert file |`cert.pem` |
|`-K` or `--key` |Path to ssl key file |`key.pem` |
|`--qr-code` |Show QR code for public IP | |,
|`-r` or `--robots` | Automatically provide a /robots.txt (The content of which defaults to `User-agent: *\nDisallow: /`) | `false` |
|`--no-dotfiles` |Do not show dotfiles| |
|`--mimetypes` |Path to a .types file for custom mimetype definition| |
Expand Down
44 changes: 26 additions & 18 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

'use strict';

var colors = require('colors/safe'),
os = require('os'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
opener = require('opener'),

fs = require('fs'),
url = require('url');
var colors = require('colors/safe'),
os = require('os'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
opener = require('opener'),
fs = require('fs'),
qrcode = require('qrcode-terminal'),
url = require('url');
var argv = require('minimist')(process.argv.slice(2), {
alias: {
tls: 'ssl'
Expand All @@ -18,7 +18,6 @@ var argv = require('minimist')(process.argv.slice(2), {
var ifaces = os.networkInterfaces();

process.title = 'http-server';

if (argv.h || argv.help) {
console.log([
'usage: http-server [path] [options]',
Expand Down Expand Up @@ -52,9 +51,10 @@ if (argv.h || argv.help) {
' --password Password for basic authentication [none]',
' Can also be specified with the env variable NODE_HTTP_SERVER_PASSWORD',
'',
' -S --tls --ssl Enable secure request serving with TLS/SSL (HTTPS)',
' -C --cert Path to TLS cert file (default: cert.pem)',
' -K --key Path to TLS key file (default: key.pem)',
' -S --ssl Enable https.',
' -C --cert Path to ssl cert file (default: cert.pem).',
' -K --key Path to ssl key file (default: key.pem).',
' --qr-code (Experimental) Show QR code for public IP',
'',
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' --no-dotfiles Do not show dotfiles',
Expand Down Expand Up @@ -94,8 +94,8 @@ if (!argv.s && !argv.silent) {
request: function (req, res, error) {
var date = utc ? new Date().toUTCString() : new Date();
var ip = argv['log-ip']
? req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress
: '';
? req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress
: '';
if (error) {
logger.info(
'[%s] %s "%s %s" Error (%s): "%s"',
Expand All @@ -115,8 +115,8 @@ if (!argv.s && !argv.silent) {
}
else if (colors) {
logger = {
info: function () {},
request: function () {}
info: function () { },
request: function () { }
};
}

Expand Down Expand Up @@ -153,7 +153,8 @@ function listen(port) {
showDotfiles: argv.dotfiles,
mimetypes: argv.mimetypes,
username: argv.username || process.env.NODE_HTTP_SERVER_USERNAME,
password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD
password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD,
showQR: argv['qr-code']
};

if (argv.cors) {
Expand Down Expand Up @@ -198,6 +199,7 @@ function listen(port) {
var server = httpServer.createServer(options);
server.listen(port, host, function () {
var protocol = tls ? 'https://' : 'http://';
var plainIp = protocol + host;

logger.info([
colors.yellow('Starting up http-server, serving '),
Expand Down Expand Up @@ -227,6 +229,7 @@ function listen(port) {
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
plainIp = protocol + details.address + ':' + port.toString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized, this is inside a loop! So it'll only be making a code for the last IP, which is usually the public IP, but I don't believe that's guaranteed. Some thoughts, and I welcome yours:

  • We could make a code for each IP (would get to be a lot)
  • We could print out which IP is linked next to/under/above the QR code as a label
  • I don't even know what else right now, I'm sure there are more options

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am out of ideas right now.

But I like the one's you proposed

I'm all onboard with either. We can use device identifier (like --qr-code lo or --qr-code wlp3s0) or index (like --qr-code 0 for local and --qr-code 1, --qr-code 2 ... for public) if we are going with that.

Or maybe the other one where we can print IP next to QR. But I have some concerns with this one as this won't give an option for local IP and if we try to make QR for all IPs that will fill up the screen quickly. Personally I like the info as clean as it was with addition of QR optionally.

What are your suggestions?

logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString())));
}
});
Expand All @@ -241,7 +244,12 @@ function listen(port) {
logger.info('Unhandled requests will be served from: ' + proxy);
}
}

if (options.showQR) {
logger.info(colors.yellow("\nWARNING: ") + "The QR Code is an experimental feature and is not guaranteed to work properly in all terminals or fonts.")
qrcode.generate(plainIp, { small: true }, function (qr) {
console.log(qr);
})
}
logger.info('Hit CTRL-C to stop the server');
if (argv.o) {
const openHost = host === '0.0.0.0' ? '127.0.0.1' : host;
Expand Down
4 changes: 4 additions & 0 deletions doc/http-server.1
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ Path to SSL key file.
If not specified, uses key.pem.
Passphrase will be read from NODE_HTTP_SERVER_SSL_PASSPHRASE (if set)

.TP
.BI \-\-qr\-code
(Experimental) Show QR code for public IP.

.TP
.BI \-r ", " \-\-robots " " [\fIUSER\-AGENT\fR]
Respond to /robots.txt request.
Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"minimist": "^1.2.5",
"opener": "^1.5.1",
"portfinder": "^1.0.28",
"qrcode-terminal": "^0.12.0",
"secure-compare": "3.0.1",
"union": "~0.5.0",
"url-join": "^4.0.1"
Expand Down