简体   繁体   English

仅允许使用Express-ip-filter在同一网络上的计算机

[英]Only allow computers on the same network using Express-ip-filter

So I'm using localtunnel to expose my ports over the internet, but I only want to let devices on the same network as the server access the server. 因此,我正在使用localtunnel在Internet上公开我的端口,但我只想让与服务器位于同一网络上的设备访问服务器。

I'm using express-ip-filter to filter away anything that's on a different network. 我正在使用express-ip-filter过滤掉其他网络上的所有内容。 I tried a few things: first I tried using 192.168.1.0/24 as the only ips that could access the website, but that didn't work, as it didn't let anything in. I then tried using the ip I got from WhatsMyIp, but that wouldn't let any device in. I then found out that express-ip-filter spits out a message saying that a certain ip was not allowed and, on every device, independently on the network it was connected to, the address was 127.0.0.1 . 我尝试了几件事:首先,我尝试使用192.168.1.0/24作为唯一可以访问该网站的ip,但这没有用,因为它没有让任何东西进入。然后,我尝试使用从中获取的ip WhatsMyIp,但这不会让任何设备进入。然后我发现, express-ip-filter一条消息,指出不允许使用某个ip,并且在每个设备上,独立于它所连接的网络,地址是127.0.0.1 I tried confirming by only allowing 127.0.0.1 , and then every device could access the server. 我尝试仅通过允许127.0.0.1确认,然后每个设备都可以访问服务器。 Why would ip-filter only get 127.0.0.1 as ip? 为什么ip过滤器仅将127.0.0.1作为ip获得? Here's my code as a reference: 这是我的代码作为参考:

// Init dependencies 
var express = require('express'),
    ipfilter = require('express-ipfilter').IpFilter

app = express()

// Blacklist the following IPs 
var ips = ['192.168.1.0/24']

// Create the server 
app.use(ipfilter(ips, { mode: "allow" }))
app.get('/', function (req, res) {
    res.send('Hi')
})

app.listen(8080, () => console.log('Up'))

From my limited understanding of localtunnel it seems like it proxies users requests to you via the localtunnel software which causes all users to have the same IP. 根据我对localtunnel有限了解,似乎它通过localtunnel软件代理了用户对您的请求,这导致所有用户都具有相同的IP。 In laymans terms: 用外行术语来说:

  1. User connects to your site through localtunnel 用户通过localtunnel连接到您的站点
  2. localtunnel copies the users request and sends it to your computer localtunnel复制用户请求并将其发送到您的计算机
  3. Your application receives the request but it looks like all traffic is coming from localtunnel because it's incredibly difficult if not impossible for localtunnel to imitate someone else's IP. 您的应用程序收到了请求,但似乎所有流量都来自localtunnel因为localtunnel模仿别人的IP并非没有困难,这是非常困难的。

Why use localtunnel at all if you only want devices on the same network to connect, you don't need to do any port forwarding or DNS setup if you just want to access another machine on the same local network. 如果只希望连接同一网络上的设备,而只想访问同一本地网络上的另一台计算机,则无需进行任何端口转发或DNS设置,为什么要使用localtunnel

If you really do need to tunnel connections then there is a solution, not with localtunnel(Which as far as i can tell does not use forwading headers, although if someone knows if they do ill change my answer) but using https://ngrok.com instead which does exactly the same thing but also sends a little extra bit of data in every request which tells the application what the clients actual IP is. 如果您确实确实需要通过隧道建立连接,那么有一个解决方案,而不是使用localtunnel(据我所知,它不使用forwading标头,尽管有人知道它们是否会改变我的答案),而是使用https:// ngrok .com相反,它执行完全相同的操作,但是在每个请求中还会发送一点额外的数据,从而告诉应用程序客户端的实际IP是什么。

  1. Install ngrok 安装ngrok
  2. Run ngrok http -subdomain=(the subdomain you want) 80 运行ngrok http -subdomain=(the subdomain you want) 80
  3. Edit your application code to find the real client IP 编辑您的应用程序代码以查找真实的客户端IP

     var findProxyIP = function(req) { var realIP = req.header('x-forwarded-for'); return realIP; } app.use(ipfilter(ips, { mode: "allow", detectIP: findProxyIP })); 

    ngrok is much more complex and has a lot more features compared to localtunnel, however, it is freemium software and its free plan is quite limiting. 与localtunnel相比,ngrok更加复杂并且具有更多功能,但是,它是免费增值软件,其免费计划非常有限。

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

相关问题 使用socket.io在计算机之间(不一定在同一网络上)发送数据包的工作示例? - Working example of using socket.io to send packets of data between computers (not necessarily on the same network)? 仅允许快递形式的AJAX请求 - Allow only AJAX requests in express PHP / Javascript:告诉两台计算机除了同一个网络 - PHP/Javascript: Tell two computers apart from same network Angular项目在同一网络的某些计算机上显示空白页 - Angular project shows blank page on some computers at the same network 只允许使用 jQuery 检查同一 div 中的复选框 - Allow Only Checkboxes in same div to be Checked using jQuery 如果IP地址被批准,则仅允许页面上的用户 - Only allow users on page if IP address is approved 使用javascript ping网络上的计算机 - ping computers on a network with javascript 如何在没有express的情况下使用网络IP而不是本地主机与nodejs? - How to use a network IP instead of localhost with nodejs without express? 用于过滤器的 jQuery 自定义代码,仅使用最接近的()函数具有相同的字符串 - jQuery custom code for filter, with same string only using closest() function 仅过滤 object 中相同 ID 的最新数据,使用 Angular 中的时间戳 6 - Filter only the latest data for the same ID in an object, using timestamp in Angular 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM