finished code for fawkes and archive creation

This commit is contained in:
bitscuit 2021-10-31 11:01:16 +00:00
parent 48fe04c4a9
commit 821163eeb3
2 changed files with 132 additions and 27 deletions

View File

@ -34,10 +34,10 @@
<h1>Fawkes</h1>
<p>Protect your images from facial recognition.</p>
<p>TODO: add more info</p>
<p>The table below shows the status for each image you've uploaded. You can select the files you want to protect using the <i>Browse</i> and <i>Upload</i> buttons below.</p>
</div>
<div class="container">
<p>The table below shows the status for each image you've uploaded. You can select the files you want to protect using the <i>Browse</i> and <i>Upload</i> buttons below.</p>
<div id="upload-area" class="container">
<div class="row">
<div class="twelve columns">
@ -48,7 +48,7 @@
<button id="button-upload" class="button button-primary"><i class="fas fa-upload"></i>&nbsp;&nbsp;&nbsp;Upload</button>
</div>
<div class="container">
<div id="table-area" class="container">
<table id="table-images" class="u-full-width">
<thead>
<tr>
@ -66,6 +66,9 @@
</div>
<div id="download-area" class="container">
</div>
<div class="footer">
<p>Many thanks to the people who made <a href="http://sandlab.cs.uchicago.edu/fawkes/">Fawkes</a>!<br/>Copyright &copy; 2021 Thomas Van Acker<br/>Project hosted on <a target="_blank" href="https://git.bitscuit.be/bitscuit/Fawkes">Gitscuit</a></p>
</div>
@ -161,8 +164,9 @@ document.getElementById("button-start").addEventListener("click", function(){
return;
}
// Disable button
// Disable button and upload
document.getElementById("button-start").style.display = "none";
document.getElementById("upload-area").style.display = "none";
// Send server request to start Fawkes
socket.emit("FAWKES_START");
@ -179,7 +183,7 @@ socket.on("FAWKES_STARTED", function(){
socket.on("FAWKES_STATE", function(data){
// Update state
if(data.done){
if(data.face > data.numFaces){
document.getElementById("status-"+data.name).innerHTML = "<i class='fas fa-check'></i>&nbsp;&nbsp;&nbsp;Processing Complete";
}else if(data.numFaces == 0){
document.getElementById("status-"+data.name).innerHTML = "<i class='fas fa-check'></i>&nbsp;&nbsp;&nbsp;No Faces Detected";
@ -188,6 +192,25 @@ socket.on("FAWKES_STATE", function(data){
}
});
socket.on("FAWKES_DONE", function(data){
// When fawkes exited
if(data.success){
// Download ready
var page = "";
page += "<h5><i class='fas fa-check'></i>&nbsp;&nbsp;&nbsp;Processing Complete</h5>";
page += "<p>Your images have been protected!</p>";
page += "<a class='button button-primary' href='"+data.downloadUrl+"' target='_blank'><i class='fas fa-download'></i>&nbsp;&nbsp;&nbsp;Download</a>";
document.getElementById("download-area").innerHTML = page;
}else{
// Error
var page = "";
page += "<h5><i class='fas fa-exclamation-triangle'></i>&nbsp;&nbsp;&nbsp;Processing Failed</h5>";
page += "<p>"+data.message+"</p>";
document.getElementById("download-area").innerHTML = page;
}
});
</script>

106
main.js
View File

@ -24,13 +24,14 @@ const fs = require("fs");
const util = require("util");
const crypto = require("crypto");
const spawn = require("child_process").spawn;
const exec = require("child_process").exec;
// Vars
const hostname = "192.168.0.128"; // Enter your local IP address here
const port = 8001; // Enter a port number here
var files = {};
var lastImageId;
var fawkes = {};
// Create HTTP server
@ -43,9 +44,15 @@ const server = http.createServer((req, res) => {
// Respond to requests
try{
// Return requested page
if(reqpath.startsWith("/dl/")){
res.statusCode = 200;
res.write(fs.readFileSync("fawkes/files/"+reqpath.substr(4)));
res.end();
}else{
res.statusCode = 200;
res.write(fs.readFileSync("html"+reqpath));
res.end();
}
}catch(error){
// 404 if page not found
@ -76,7 +83,12 @@ io.on("connection", (socket) => {
console.log(userId+": disconnect");
// Remove all files from this user
try{
fs.rmSync("fawkes/files/"+userId, {recursive: true});
fs.rmSync("fawkes/files/"+userId+"cloaked", {recursive: true});
}catch(error){
console.log(userId+": couldn't delete some files");
}
});
// File upload
@ -99,7 +111,7 @@ io.on("connection", (socket) => {
// Create file
var name = data.name;
var path = "fawkes/files/"+userId+"/"+name;
var path = "fawkes/files/"+userId+"/"+name+".png";
var file = {
length: data.length,
name: data.name,
@ -167,33 +179,79 @@ io.on("connection", (socket) => {
// Start binary
var process = spawn("fawkes/protection", ["-d", "fawkes/files/"+userId, "--mode", "low"]);
// Update vars
fawkes[userId] = {
running: true,
images: [],
imageSentDone: []
};
// Add listeners
process.stdout.setEncoding("utf8");
process.stdout.on("data", function(data){
// Received output from process
data = data.toString();
console.log("fawkes stdout: "+data);
// Parse output
data = data.replace("\n", " ");
var splices = data.split(" ");
if(data.startsWith("Find ")){
var lines = data.split("\n");
for(var l=0; l<lines.length; l++){
// For each line
var splices = lines[l].split(" ");
if(splices.length == 0) continue;
if(splices[0] == "Find"){
var numFaces = splices[1];
var imageId = splices[4];
var imageId = splices[4].substr(0, splices[4].indexOf("."));
console.log("fawkes found "+numFaces+" faces in image "+imageId);
// Update file info
files[userId+imageId].numFaces = numFaces;
lastImageId = imageId;
fawkes[userId].images.push(userId+imageId);
fawkes[userId].imageSentDone.push(numFaces == 0);
// Update client states
socket.emit("FAWKES_STATE", {name:imageId, face:1, numFaces:numFaces});
// Update state if no faces found
if(numFaces == 0){
var d = {name:imageId, face:0, numFaces:0};
console.log(d);
socket.emit("FAWKES_STATE", d);
}
}else if(splices[0] == "processing"){
var face = splices[2];
console.log("fawkes processing face "+face);
// Processing next face
console.log("fawkes processing face "+splices[2]);
// Calculate image
var image;
var f = splices[2] - 1;
for(var i=0; i<fawkes[userId].images.length; i++){
console.log("f = "+f);
if(f < files[fawkes[userId].images[i]].numFaces){
image = fawkes[userId].images[i];
console.log("image = "+image);
break;
}else{
f -= files[fawkes[userId].images[i]].numFaces;
// If didn't send it yet, send 'done' message for this image
if(!fawkes[userId].imageSentDone[i]){
fawkes[userId].imageSentDone[i] = true;
var nf = files[fawkes[userId].images[i]].numFaces;
var d = {name:fawkes[userId].images[i].substr(userId.length), face:nf+1, numFaces:nf};
console.log(d);
socket.emit("FAWKES_STATE", d);
}
}
}
// Update client states
socket.emit("FAWKES_STATE", {name:lastImageId, face:face, numFaces:files[lastImageId].numFaces});
var d = {name:image.substr(userId.length), face:f+1, numFaces:files[image].numFaces};
console.log(d);
socket.emit("FAWKES_STATE", d);
}
}
@ -206,6 +264,30 @@ io.on("connection", (socket) => {
process.on("close", function(code){
// Fawkes exited
console.log("Fawkes exited with code "+code);
// Check if success
if(code == 0){
// Create archive
console.log("creating archive...");
var archiveName = "fawkes-"+Date.now()+"-"+userId+".tar.gz";
var child = exec(
"mkdir fawkes/files/"+userId+"cloaked && "+
"mv fawkes/files/"+userId+"/*_cloaked* fawkes/files/"+userId+"cloaked/ && "+
"tar -czvf fawkes/files/"+archiveName+" -C fawkes/files/"+userId+"cloaked ."
, (error, stdout, stderr) => {
console.log("archive creation done: "+error+", "+stdout+", "+stderr);
// Zip done
if(error){
socket.emit("FAWKES_DONE", {success:false, message:"Internal server error. Couldn't create archive."});
}else{
socket.emit("FAWKES_DONE", {success:true, downloadUrl:"/dl/"+archiveName});
}
});
}else{
// Error
socket.emit("FAWKES_DONE", {success:false, message:"Internal server error. Fawkes exited with an exception."});
}
});
// Update file states