From a64bc870cd70ed0f3098b0e15d054c723f05532d Mon Sep 17 00:00:00 2001
From: Gunananthaa K B <cb.en.u4cse19021@cb.students.amrita.edu>
Date: Mon, 2 May 2022 14:59:11 +0530
Subject: [PATCH] basic filtering done

---
 proxy/proxy.js | 118 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 106 insertions(+), 12 deletions(-)

diff --git a/proxy/proxy.js b/proxy/proxy.js
index a6556dd..bcc7f2f 100644
--- a/proxy/proxy.js
+++ b/proxy/proxy.js
@@ -1,27 +1,121 @@
 const http = require('http');
 
 const PORT = 8080;
+
+const filter = request=>{
+    if(request.url==='/good'){
+        return true;
+    }
+    return false;
+}
+
 const requestListener = (req,res)=>{
     const options = {
         hostname:'localhost',
         port:3000,
         path:'/'
     };
-    const get = http.get(options,response=>{
-        console.log(`statusCode: ${response.statusCode}`);
-        let buffer="";
-        response.on('data',d=>{buffer+=d});
-        response.on('error',err=>{console.log(`error:${err}`)});
-        response.on('end',()=>{
-            res.setHeader("Content-Type", "text/html");
-            res.writeHead(200);
-            res.end(buffer);
+    if(filter(req)){
+        const get = http.get(options,response=>{
+            console.log(`statusCode: ${response.statusCode}`);
+            let buffer="";
+            response.on('data',d=>{buffer+=d});
+            response.on('error',err=>{console.log(`error:${err}`)});
+            response.on('end',()=>{
+                res.setHeader("Content-Type", "text/html");
+                res.writeHead(200);
+                res.end(buffer);
+            });
         });
-    })
+    }
+    else{
+        res.setHeader("Content-Type", "text/html");
+        res.writeHead(403);
+        res.end(rejectionDisplay);
+    }
     // res.writeHead(200);
-    console.log(req.headers);
+    console.log(req.url);
     // res.end("hello");
 
 }
 const server = http.createServer(requestListener);
-server.listen(PORT);
\ No newline at end of file
+server.listen(PORT);
+
+const rejectionDisplay = `
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html">
+<meta name="id" content="siteBlocked">
+<title>Web Site Blocked</title>
+<style type="text/css">
+#shd { width:500px;position:relative;right:3px;top:3px;margin-right:3px;margin-bottom:3px;text-align:center; }
+#shd .second,
+#shd .third,
+#shd .box { position:relative;left:-1px;top:-1px; }
+#shd .first { background: #f1f0f1; }
+#shd .second { background: #dbdadb; }
+#shd .third { background: #b8b6b8; }
+#shd .box { background:#ffffff;border:1px solid #848284;height:350px; }
+.strip { width:100%;height:70px; }
+.warn {
+background-color:#f0d44d;
+filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#fae379', endColorstr='#eed145');
+background:-webkit-gradient(linear, left top, left bottom, from(#fae379), to(#eed145));
+background:-moz-linear-gradient(top,  #fae379,  #eed145);
+font-size:14px;
+font-weight:bold;
+}
+#nsa_banner { position:relative;top:20px;left:20px;float:left; }
+#alert_icon { position:relative;top:15px;left:20px;float:left; }
+#alert_text { float:left;position:relative;top:25px;left:40px;width:400px; }
+</style>
+<script type="text/JavaScript">
+var isIE7=false;
+function onLoadFunc()
+{
+var s = document.URL;
+s = s.replace(/</g, "&lt;").replace(/>/g, "&gt;");
+if (s.length < 50) { s='URL: <b>'+s+'</b>'; }
+else { s='URL: <b>'+s.substring(0,50)+'</b>...'; }
+var o=document.getElementById("urlp");
+if (o) { o.innerHTML=s; }
+if (isIE7==true) {
+var base="http://172.17.100.100/";
+if (base.indexOf("fw_interface")>=0) {
+base="";
+}
+o=document.getElementById("nsa_banner");
+if (o) { o.src=base+"nsa_banner.gif"; }
+o=document.getElementById("alert_icon");
+if (o) { o.src=base+"alert_icon.gif"; }
+}
+}
+</script>
+</head>
+<body onload="onLoadFunc();">
+<div style="width:100%;height:100px;"></div>
+<center>
+<div id="shd"><div class="first"><div class="second"><div class="third">
+<div class="box">
+<div class="strip">
+
+<div id="alert_text">
+This site has been blocked by the network administrator.
+</div>
+</div>
+<div>
+<p id="urlp">
+</p>
+<p>
+Block reason: <b>Advanced Computer Network's Firewall</b>
+</p>
+<p>
+This is an intentionally block based on web proxy server's filter. The only allowed endpoint as per the currnet rule is '/good'
+</p>
+</div>
+</div>
+</div></div></div></div>
+</center>
+</body>
+</html>
+`;
-- 
GitLab