博士做的钓鱼研究。其中有一章节,“how easy is it to launch a phishing attack”。主要来讲,现在做一个钓鱼网站,是一件多么简单的事情。一般来讲,用php来搭建后端服务器。这样,前端复制过来的官网,把诱骗的用户信息传到自己的后端服务器上。但是呢,对于新手来说,还要多学一门语言。所以,我们可以舍弃用php,而用nodejs来搭建自己的后端服务器。
js越来越强大,nodejs用法基本和js一样,并且用来做后端。今天就用nodejs写一个简单的例子。可以接受前端发过来的数据。
首先,用到一个包,安装命令是:
npm install nodejs-websocket
前端html文件,命名为index.html, 里面有两个简单输入框,分别为username和password。然后定义了一个div,用来显示后端传过来的数据:
<h1>Echo Test</h1>
Username: <input id="username" type="text">
<br>
Password: <input id="password" type="text">
<br>
<button id="sendBtn">SEND</button>
<div id='recv'></div>
我们监听一个端口,用来通讯。通过button的onclick事件,用websocket.send(),把数据传到后端。
并且,可以用onmessage,来接受后端发来的数据。
所以,js代码为:
var websocket = new WebSocket("ws://localhost:9999/");
websocket.onopen = function(){
console.log('websocket open')
document.getElementById('recv').innerHTML = 'Connected'
}
websocket.onclose = function(){
console.log('websocket close')
}
//sending message from front to back
websocket.onmessage = function(e){
console.log("This is on message data: " + e.data)
document.getElementById('recv').innerHTML = e.data
}
document.getElementById('sendBtn').onclick = function(){
var username = document.getElementById('username').value
var password = document.getElementById('password').value
websocket.send(username+";"+password)
}
nodejs,为后端,建立监听端口。这个例子中,端口为9999。然后require需要的那个数据包。代码为:
var ws = require("nodejs-websocket")
var port = 9999
var server = ws.createServer(function(conn){
console.log("New connection")
conn.on("text", function(str){
console.log("Received message from front side:" + str)
conn.sendText(str)
})
conn.on("close", function(code,reason){
console.log("Connection close")
})
conn.on("error", function(err){
console.log("handle err")
console.log(err)
})
}).listen(port)
console.log("websocket server listening on port: " + port)
更多推荐
用Nodejs创建服务器后端,接收前端数据
发布评论