From af72bcb51f26cbc19a87fbb321648a181da5e26c Mon Sep 17 00:00:00 2001 From: bitscuit Date: Sat, 30 Oct 2021 14:25:33 +0000 Subject: [PATCH] added code to upload files --- .gitignore | 3 ++ html/index.html | 88 +++++++++++++++++++++++++++++++++++++++++- launch | 4 ++ main.js | 100 ++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 181 insertions(+), 14 deletions(-) create mode 100755 launch diff --git a/.gitignore b/.gitignore index fc9b519..2fbcd35 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# ---> Fawkes +fawkes/ + # ---> Vim # Swap [._]*.s[a-v][a-z] diff --git a/html/index.html b/html/index.html index 42da516..507ef3e 100644 --- a/html/index.html +++ b/html/index.html @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - This project is hosted on https://git.bitscuit.be/bitscuit/Scrawl + This project is hosted on https://git.bitscuit.be/bitscuit/Fawkes --> @@ -30,10 +30,39 @@ -
+

Fawkes

Protect your images from facial recognition.

+

TODO: add more info

+ +
+

The table below shows the status for each image you've uploaded. You can select the files you want to protect using the Browse and Upload buttons below.

+ +
+
+ + +
+
+ +
+ +
+ + + + + + + + + +
Image NameStatus
+

You haven't uploaded any images yet.

+ +
+ @@ -43,11 +72,66 @@ diff --git a/launch b/launch new file mode 100755 index 0000000..4768bc2 --- /dev/null +++ b/launch @@ -0,0 +1,4 @@ +#!/bin/bash +cd $(dirname $0) +rm fawkes/files/* -rf +node main.js diff --git a/main.js b/main.js index 9f555ac..96ae0cd 100644 --- a/main.js +++ b/main.js @@ -28,8 +28,7 @@ const crypto = require("crypto"); const hostname = "192.168.0.128"; // Enter your local IP address here const port = 8001; // Enter a port number here -var ids = []; -var sockets = []; +var files = {}; // Create HTTP server @@ -62,27 +61,104 @@ io.on("connection", (socket) => { console.log("New user connected!"); // Give user unique ID - sockets.push(socket); var userId = sha256("USER"+rand(0,9999999)+"TIME"+Date.now()).substr(0, 10); - ids.push(userId); console.log("userId = "+userId); + + // Init directory structure + fs.mkdirSync("fawkes/files/"+userId); // Init socket listeners // Disconnect socket.on("disconnect", () => { - console.log("User "+userId+" disconnected!"); + console.log(userId+": disconnect"); - // Remove from user lists - var index = sockets.indexOf(socket); - if(index != -1){ - // When in users list - ids.splice(index, 1); - sockets.splice(index, 1); - } + // Remove all files from this user + fs.rmSync("fawkes/files/"+userId, {recursive: true}); }); + // File upload + socket.on("FILE_START", (data) => { + console.log(userId+": file upload ("+data.name+", "+data.length+" bytes)"); + // Check length + if(data.length > 20000000){ + console.log(userId+": file too large (data.length = "+data.length+")"); + socket.emit("FILE_ERROR", {name:data.name, error:"File too large. Max size = 20MB"}); + return; + } + + // Check if exists + if((userId+data.name) in files){ + console.log(userId+": file already exists (data.name = "+data.name+")"); + socket.emit("FILE_ERROR", {name:data.name, error:"File had already been uploaded."}); + return; + } + + // Create file + var name = data.name; + var path = "fawkes/files/"+userId+"/"+name; + var file = { + length: data.length, + name: data.name, + path: path, + numSegments: data.length/100000 + 1, + nextSegment: 0 + }; + files[userId+data.name] = file; + + // Ask client to begin upload + socket.emit("FILE_ACK", {name:file.name, nextSegment:file.nextSegment, numSegments:file.numSegments}); + }); + + // File contents upload + socket.on("FILE_DATA", (data) => { + console.log(userId+": file data ("+data.name+", segment "+data.segment+", "+data.data.length+" bytes)"); + + // Check if file exists + if(!((userId+data.name) in files)){ + console.log(userId+": received segment for unknown file (data.name = "+data.name+")"); + socket.emit("FILE_ERROR", {name:data.name, error:"Received segment for unknown file."}); + return; + } + + // Get file + var file = files[userId+data.name]; + + // Check size + if(data.data.length > 100000){ + console.log(userId+": segment too large (data.name = "+data.name+", data.data.length = "+data.data.length+")"); + socket.emit("FILE_ERROR", {name:data.name, error:"Segment too large."}); + return; + } + + // Check segment number + if(data.segment != file.nextSegment || data.segment >= file.numSegments){ + console.log(userId+": unexpected segment (data.name = "+data.name+", data.segment = "+data.segment+")"); + socket.emit("FILE_ERROR", {name:data.name, error:"Unexpected segment."}); + return; + } + + // Write data + fs.appendFile(file.path, data.data, 'Binary', function(err){ + if(err){ + // Error + console.log(userId+": couldn't write to file (error = "+err+")"); + socket.emit("FILE_ERROR", {name:data.name, error:"Internal server error. Couldn't write to file."}); + + }else{ + file.nextSegment++; + if(file.nextSegment < file.numSegments-1){ + // Ask for new segment + socket.emit("FILE_ACK", {name:file.name, nextSegment:file.nextSegment, numSegments:file.numSegments}); + + }else{ + // When file fully uploaded + socket.emit("FILE_DONE", {name:file.name}); + } + } + }); + }); });