Advertisement
Guest User

Key server

a guest
Feb 4th, 2020
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs=require("fs"),
  2.     https=require("https"),
  3.     crypto=require("crypto"),
  4.     readline=require("readline");
  5. const store="store.json";
  6. const AllowManyRequests=true;
  7. var options={
  8.     key: fs.readFileSync("key.pem"),
  9.     cert: fs.readFileSync("cert.pem")
  10. };
  11. const rl=readline.createInterface({input:process.stdin,output:process.stdout});
  12. var enc;
  13. rl.question("Encryption passphrase:",(r)=> {
  14. var ehash=crypto.createHash("sha256");
  15. ehash.update(Buffer.from(r));
  16. enc=ehash.digest();
  17. if(!enc) return;
  18. var server=https.createServer(options,(req,res)=> {
  19.     if(req.method=="POST") {
  20.         var h=req.headers;
  21.         console.log("---\nIncoming: ",req.url,"\n",h);
  22.         var socket=req.socket; //socket.remoteAddress: IP
  23.         var data,f="",st={}; //data: buffer
  24.         if(fs.existsSync(store)) {
  25.             f=fs.readFileSync("store.json");
  26.             st=JSON.parse(f);
  27.             if(!st) res.end(500);
  28.         } else {
  29.             console.log("store.json does not exist! Creating file.");
  30.             fs.writeFileSync("store.json",JSON.stringify({}));
  31.         }
  32.         var key=-1,id=-1,paid=false,ip=socket.remoteAddress; //id=hash(key)
  33.         var RC=400,RH={},RD=""; //return data
  34.         if(!st.iplog) {
  35.             st.iplog={};
  36.             st.iplog[ip]=Date.now();
  37.             fs.writeFileSync(store,JSON.stringify(st));
  38.         }
  39.         var tl=Date.now()-st.iplog[ip]; //time since last request
  40.         if(tl<15000&&AllowManyRequests==false) {
  41.             RC=429; //Too Many Requests
  42.         } else {
  43.             st.iplog[ip]=Date.now();
  44.             fs.writeFileSync(store,JSON.stringify(st));
  45.         }
  46.         req.on("data",(chunk)=>{
  47.             data=Buffer.from(chunk);
  48.         });
  49.         req.on("end",()=>{
  50.         if(!data) RC=406;
  51.         else {
  52.             console.log("buf["+data.length+"] "+data.toString("hex"));
  53.         }
  54.         if(RC==429) { //prevent DDoS
  55.             //console.log("delaying request");
  56.         } else {
  57.             if(req.url=="/key") {
  58.                 if(data.length!=32) { //AES key length
  59.                     RC=406; //Not Acceptable
  60.                 } else {
  61.                     key=data;
  62.                     var hash=crypto.createHash("sha512");
  63.                     hash.update(data);
  64.                     id=hash.digest();
  65.                     var aes=crypto.createCipheriv("aes256",enc,Buffer.alloc(16));
  66.                     var key1=aes.update(data);
  67.                     var key2=aes.final();
  68.                     key=Buffer.concat([key1,key2]);
  69.                     var obj={
  70.                         "id": id.toString("hex"),
  71.                         "key": key.toString("hex"),
  72.                         "paid": false,
  73.                         "ip": ip
  74.                     };
  75.                     if(!st[id]) {
  76.                         console.log("-> Storing:\n",obj);
  77.                         st[id]=obj;
  78.                         fs.writeFileSync(store,JSON.stringify(st));
  79.                     }
  80.                     RC=201; //Created
  81.                     RH["Content-Type"]="application/octet-stream";
  82.                     RD=id; //give id
  83.                 } //end of else (key length valid)
  84.             } else if(req.url=="/id") {
  85.                 if(data.length!=512/8) { //SHA512 hash length
  86.                     RC=406;
  87.                 } else {
  88.                     id=data;
  89.                     if(st[id]) {
  90.                         key=Buffer.from(st[id]["key"],"hex"); //build buffer from hex string
  91.                         //check if hash(dec(key))=id
  92.                         var aes=crypto.createDecipheriv("aes256",enc,Buffer.alloc(16));
  93.                         var key1=aes.update(key);
  94.                         var key2=aes.final();
  95.                         key=Buffer.concat([key1,key2]);
  96.                         var hash=crypto.createHash("sha512");
  97.                         hash.update(key);
  98.                         var rev=hash.digest();
  99.                         console.log(id,rev,Buffer.compare(id,rev));
  100.                         if(Buffer.compare(id,rev)!=0) {
  101.                             RC=401; //Unauthorized
  102.                             console.log("The decrpyted key hash is NOT equal to the given hash! Wrong passphrase used?");
  103.                         } else {
  104.                             console.log("-> paid: ",st[id]["paid"]);
  105.                             if(st[id]["paid"]==true) {
  106.                                 RC=200;
  107.                                 RH["Content-Type"]="application/octet-stream";
  108.                                 RD=key;
  109.                             } else {
  110.                                 RC=402; //Payment Required
  111.                             }
  112.                         }
  113.                     } else {
  114.                         RC=404; //Not Found
  115.                     }
  116.                 }
  117.             }
  118.         } //end of else (not too many requests)
  119.         res.writeHead(RC,RH);
  120.         res.end(RD);
  121.         console.log("Outgoing: ",RC,"\n",RD,"\n---\n");
  122.         }); //end of req.on("end")
  123.     } else {
  124.         res.writeHead(405,"Method not allowed");
  125.         res.end();
  126.     }
  127.    
  128. }).listen(5500);
  129. }); //end of rl.question
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement