Skip to content

Instantly share code, notes, and snippets.

@geraintwhite
Last active August 6, 2020 01:31
Show Gist options
  • Save geraintwhite/11268473 to your computer and use it in GitHub Desktop.
Save geraintwhite/11268473 to your computer and use it in GitHub Desktop.
A Node.js app to use NFC to unlock/lock Ubuntu.
var http = require('http'),
url = require('url'),
exec = require('child_process').exec;
var locked = false;
var app = http.createServer(function(request, response) {
var url_parts = url.parse(request.url, true);
var path = url_parts.pathname.replace(/^\/|\/$/g, '');
if (request.method == 'GET') {
if (path == '') {
var get_data = url_parts.query;
response.writeHead(200, {'Content-Type': 'text/plain'});
if (get_data.secret == 'secret') {
console.log('Access Granted');
response.end('Access Granted');
exec(__dirname + '/unlock.sh ' + locked, function(error, stdout, stderr) {
var out = error ? stderr : stdout;
console.log(out);
locked = !locked;
});
} else {
console.log('Access Denied');
response.end('Access Denied');
}
}
}
}).listen(6001);
console.log('Server running at localhost:6001');
#!/bin/bash
export XAUTHORITY=/home/$USER/.Xauthority
export DISPLAY=:0.0
case "$1" in
true)
echo "Unlocking"
loginctl unlock-sessions
xset dpms force on
xset s reset
;;
false)
echo "Locking"
loginctl lock-sessions
xset dpms force off
;;
esac
@geraintwhite
Copy link
Author

Note: unlock.js must be run as root because loginctl requires root to run.

You can set up a script run as root which monitors the status of a file and locks/unlocks accordingly. This would require modifying unlock.js to set the state of the file and unlock.sh to monitor it, but you would have to then run two scripts instead of just the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment