Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

same ip with different device in lan #458

Open
Timor666 opened this issue Sep 13, 2023 · 1 comment
Open

same ip with different device in lan #458

Timor666 opened this issue Sep 13, 2023 · 1 comment

Comments

@Timor666
Copy link

需求:限制设备访问API接口时的速率,例如每30秒一次

我使用的安卓手机与PC的chorme浏览器,这两个设备共同连接的时同一个Wifi
先后进行访问API 如:http://101.101.101.100:1314/API/Test

实际上我得到的结果是,当PC访问后,安卓再进行访问,则是不行的。

服务器得到的输出,访问源的IP是同一个
我使用了以下代码来得到访问IP
Console.WriteLine(HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString());

我想知道的是,是什么原因?也许是设置的问题吧

@StrangerLi
Copy link

StrangerLi commented Sep 18, 2023

Since you asked the question in Chinese, I will answer it in Chinese。

一般情况下,这种是不会的,目前我所知的,下列情况中的【任何一个】都会导致你的问题:

  1. 如果你使用了反向代理,如Nginx、CDN等,在没有正确配置的情况下,会导致程序无法正确获取客户端IP地址。
  2. 如果你的服务器部署在公网,且你的局域网出口开启了NAT,会导致程序获取到的IP地址为你的出口公网地址。
  3. 如果你的服务器部署在内网,但是你的WIFI使用了NAT技术,会导致程序获取到的地址为WIFI的公共地址。
  4. 如果你手机使用的是PC的共享热点网络,会导致程序获取到的地址是PC的地址。

获取到的IP会是一下几种:

  1. 情况1,获取到的IP地址是反向代理服务器的IP,或CDN服务器组的IP。
  2. 情况2,获取到的IP地址是运营商分配给你公司的出口IP地址,即IP地址不属于10.0.0.0-10.255.255.255、172.16.0.0-172.31.255.255、192.168.0.0-192.168.255.255。
  3. 情况3,获取到的IP地址是WIFI的IP地址,且IP地址不是你手机或PC的任意一个IP地址,且属于10.0.0.0-10.255.255.255、172.16.0.0-172.31.255.255、192.168.0.0-192.168.255.255。
  4. 情况4,获取到的IP地址应当是你PC的IP地址。

上述情况可能会混合,如果有混合情况,会导致分析更加复杂。

针对情况1,可以在反向代理服务器上设置x-forwarded请求头(程序中依然需要配置解析)解决。
参考:https://learn.microsoft.com/zh-cn/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-7.0

其余情况请针对情况修改网络架构

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants