繁体   English   中英

nodejs和javascript useragent行为

[英]nodejs and javascript useragent behaviour

如何从原始的HTTP请求中获取JavaScript以传递用户代理信息(IP地址)? 当我运行下面的代码时,我总是收到服务器IP地址(例如1.2.3.4),就像在发出http请求一样。

的index.html

<!DOCTYPE html>
<html>
<title>ContactForm</title>
<body>
<h1>Contact Form</h1>
<form action="http://1.2.3.4:8080/myaction" method="post" target="_blank">
 Business Name <input type="text" name="businessname"><br>
 User Agent: <input type="text" id="UserAgent" name="useragent">
<input type="submit" value="Submit">
</form>`

node.js代码

app.use(bodyParser.urlencoded({ extended: true })); 
app.post('/myaction', function(req, res) {
  res.send('Thank you for your inquiry, someone will contact you shorty.');

app.get('/', function(request, response){
    response.sendFile('/home/ubuntu/index.html');
});

fs.appendFile(timeEntry+'submission.txt',  'useragent='+JSON.stringify(req.headers)+' ', function(err) {
  if (!err) {
      console.log('Wrote agent headers info to file.txt');
    } else {
      throw err;
    }
  });
app.listen(8080, function() {
  console.log('Server running at http://127.0.0.1:8080/');
});`

以下对我有用的作品:

app.post('/myaction', function(req, res) {
   console.log(req.ip)
   res.send('Thank you for your inquiry, someone will contact you shorty.');
});

输出:

::ffff:17.###.##.## 

值得一提的是,在我的设置中涉及到两个单独的机器(单独的IP),仅在这种情况下,用户代理IP与服务器IP地址不同。

如果浏览器(用户代理)和节点服务器是同一台机器,显然,您将获得与html action="http://17.###.##.##:7777/myaction"相同的IP。

下面是修改后的“ server.js”文件的内容:

var express = require('express'),
path = require('path'),
fs = require('fs'),
bodyParser = require('body-parser')

var app = express();
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.static(path.join(__dirname, 'server/public')));

app.post('/myaction', function(req, res) {
    console.log(req.ip)
    res.send('Thank you for your inquiry, someone will contact you shorty.');

    fs.appendFile('submission.txt',  'useragent='+JSON.stringify(req.headers)+' ', function(err) {
      if (!err) {
          console.log('Wrote agent headers info to file.txt');
        } else {
          throw err;
        }
      });
});

app.get('/', function(request, response){
    console.log(request.headers)
    response.sendFile('/index.html');
});

app.listen(7777, function() {
  console.log('Server running at http://127.0.0.1:7777/');
});

在结果数据submission.txt (这里没有用户代理IP,因为它不是在req.headers ):

useragent={"host":"17.###.##.##:7777","content-type":"application/x-www-form-urlencoded","origin":"http://localhost:7777","content-length":"24","connection":"keep-alive","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9","referer":"http://localhost:7777/","accept-language":"en-us","accept-encoding":"gzip, deflate"} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM