Last active
August 6, 2020 01:31
-
-
Save geraintwhite/11268473 to your computer and use it in GitHub Desktop.
A Node.js app to use NFC to unlock/lock Ubuntu.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.