gpt4 book ai didi

javascript - Nodejs 和 javascript 用户代理行为

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:29 25 4
gpt4 key购买 nike

如何让 javascript 从原始 http 请求中传递用户代理信息(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 地址不同。

如果浏览器(useragent)和 Node 服务器是同一台机器,显然您将获得与 html 中相同的 IP action="http://17.###.##.## :7777/myaction"

修改后的“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"} 

关于javascript - Nodejs 和 javascript 用户代理行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35319986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com