commit c3a7ef378042d80445d3d7a988ba0cd5ff67260a Author: root Date: Tue Mar 24 22:22:28 2020 +0100 Initial commit diff --git a/certbot-webroot b/certbot-webroot new file mode 100644 index 0000000..c6c1612 --- /dev/null +++ b/certbot-webroot @@ -0,0 +1,7 @@ +# Port nodejs will use +# default 9000 +#NODE_PORT= + +# IP nodes will listen +# default 127.0.0.1 +#NODE_LISTEN_IP= diff --git a/certbot-webroot.js b/certbot-webroot.js new file mode 100644 index 0000000..6867c39 --- /dev/null +++ b/certbot-webroot.js @@ -0,0 +1,72 @@ +// certbot_webroot.js +// writted by Benoit LORAND +// +// webservice help certbot when using webroot +// Could be behind a reverse proxy (Apache, Nginx, haproxy) who do basic authentication +// +// inspired from https://stackoverflow.com/questions/16333790/node-js-quick-file-server-static-files-over-http +// +"use strict"; +const http = require('http'); +const url = require('url'); +const fs = require('fs'); +const path = require('path'); +const port = Number(process.env.NODE_PORT) || 9000; +const listenip = process.env.NODE_LISTEN_IP || '127.0.0.1'; +const base_dir = './data'; + +http.createServer(function (req, res) { + + // parse URL + const parsedUrl = url.parse(req.url); + // extract URL path + let pathname = `${parsedUrl.pathname}`; + // based on the URL path, extract the file extention. e.g. .js, .doc, ... + const ext = path.parse(pathname).ext; + // maps file extention to MIME typere + const map = { + '.ico': 'image/x-icon', + '.html': 'text/html', + '.js': 'text/javascript', + '.json': 'application/json', + '.css': 'text/css', + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.wav': 'audio/wav', + '.mp3': 'audio/mpeg', + '.svg': 'image/svg+xml', + '.pdf': 'application/pdf', + '.doc': 'application/msword' + }; + + fs.exists(base_dir + pathname, function (exist) { + if(!exist) { + // if the file is not found, return 404 + console.log(`certbot_validation_fqdn : ${req.method} ${req.url} - 404`); + res.statusCode = 404; + res.end(`File ${pathname} not found!`); + return; + } + + // if is a directory search for index file matching the extention + if (fs.statSync(base_dir + pathname).isDirectory()) pathname += '/index' + ext; + + // read file from file system + fs.readFile(base_dir + pathname, function(err, data){ + if(err){ + console.log(`certbot_validation_fqdn : ${req.method} ${req.url} - 500`); + res.statusCode = 500; + res.end(`Error getting the file: ${err}.`); + } else { + // if the file is found, set Content-type and send data + console.log(`certbot_validation_fqdn : ${req.method} ${req.url} - 200`); + res.setHeader('Content-type', map[ext] || 'text/plain' ); + res.end(data); + } + }); + }); + + +}).listen(parseInt(port, listenip)); + +console.log(`Server listening on port ${listenip}:${port}`); diff --git a/certbot-webroot.service b/certbot-webroot.service new file mode 100644 index 0000000..d69ef41 --- /dev/null +++ b/certbot-webroot.service @@ -0,0 +1,14 @@ +[Unit] +Description=certbot-webroot.js - webservice for helping certbot to validate FQDN +Documentation=https://www.blorand.org +After=network.target + +[Service] +EnvironmentFile=-/etc/default/certbot-webroot +Type=simple +User=www-data +ExecStart=/usr/bin/node /opt/certbot-webbroot/certbot-webroot.js +Restart=on-failure + +[Install] +WantedBy=multi-user.target diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..72eb424 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +certbot-webroot (1.0) stable; urgency=medium + + * Initial release. + + -- Benoit LORAND Tue, 24 Mar 2020 22:20:33 +0100 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..f599e28 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +10 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..c95c3b8 --- /dev/null +++ b/debian/control @@ -0,0 +1,17 @@ +Source: certbot-webroot +Maintainer: Benoit LORAND +Section: misc +Priority: optional +Standards-Version: 3.9.2 +Build-Depends: debhelper (>= 9) + +Package: certbot-webroot +Architecture: all +Depends: ${shlibs:Depends}, + ${misc:Depends}, + nodejs, + npm +Suggests: haproxy +Section: BLORAND +Priority: optional +Description: WebService pour aider certbot à faire les validations de FQDN diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..e69de29 diff --git a/debian/postinst b/debian/postinst new file mode 100755 index 0000000..169bbd9 --- /dev/null +++ b/debian/postinst @@ -0,0 +1,28 @@ +#! /bin/bash + +set -e + +case "$1" in +configure) + mkdir -p /opt/certbot-webroot/data + systemctl daemon-reload + systemctl --now enable certbot-webroot.service + + ;; + + abort-upgrade|abort-remove|abort-deconfigure) + + ;; + + *) + echo "postinst called with unknown argument \`$1'" >&2 + exit 0 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + + + +exit 0 diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..fd1539f --- /dev/null +++ b/debian/rules @@ -0,0 +1,9 @@ +#!/usr/bin/make -f +%: + dh $@ + +override_dh_auto_install: + install -D -m 0644 certbot-webroot.js $$(pwd)/debian/certbot-webroot/opt/certbot-webroot/certbot-webroot.js + install -D -m 0644 certbot-webroot $$(pwd)/debian/certbot-webroot/etc/default/certbot-webroot + install -D -m 0644 certbot-webroot.service $$(pwd)/debian/certbot-webroot/lib/systemd/system/certbot-webroot.service + diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..163aaf8 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt)