简体   繁体   English

在Java中,当我在代理后面时,网站如何找到我的真实IP地址?

[英]In Java, how can a website find my real IP address while I'm behind a proxy?

My program will use (http) proxy to send out http requests. 我的程序将使用(http)代理发送http请求。 And the proxy address is known to the program. 并且代理地址是程序已知的。 But the program also want to get the current IP address while behind proxy. 但是该程序还希望在代理之后获取当前IP地址。 What is the best way to retrieve that? 检索该内容的最佳方法是什么?

The only way you could do that would to have your program explicitly send its IP address to the server encoded in the request URL, a custom request header, or in the request body. 唯一的方法是让程序将其IP地址显式发送到请求URL,自定义请求标头或请求正文中编码的服务器。

If the proxy is under your control, you could configure it to add your client's IP address to the request. 如果代理处于您的控制之下,则可以对其进行配置,以将客户端的IP地址添加到请求中。 (It may already do this; eg "x-forwarded-for") (它可能已经这样做;例如,“ x-forwarded-for”)


The other problem (in general) is that the IP address of your machine could well be a private IP address. 另一个问题(通常)是您机器的IP地址很可能是私有IP地址。 It is not guaranteed that the IP will be addressable by the server. 不能保证服务器可以访问该IP。 Or if the server is using it to identify the client, then there is no guarantee the IP will be unique ... or reliable; 或者,如果服务器使用它来标识客户端,则不能保证IP的唯一性或可靠性。 eg not spoofed in some way. 例如不以某种方式欺骗。


There is one alternative though. 不过,还有一种选择。 If you configure your server and client correctly, the server can rely on an SSL client certificate to establish the identity of the client. 如果正确配置服务器和客户端,则服务器可以依靠SSL客户端证书来建立客户端的身份。 But this requires a few things: 但这需要一些注意事项:

  • The user's firewall must allow HTTPS connections. 用户的防火墙必须允许HTTPS连接。
  • The client application (or user's browser) must have a client certificate in its keystore. 客户端应用程序(或用户的浏览器)必须在其密钥库中具有客户端证书。
  • The server must be able to trust the client certificate and know what identity it establishes (a person? a machine?) 服务器必须能够信任客户端证书并知道其建立的身份(一个人或一台机器?)
  • The client application must know to send the certificate when establishing the HTTPS connection. 建立HTTPS连接时,客户端应用程序必须知道要发送证书。

There are also security issues with sending SSL/TLS through a proxy. 通过代理发送SSL / TLS时也存在安全问题。 There is the potential for "man in the middle" attacks if the proxy can't be trusted. 如果不能信任代理,则有可能发生“中间人”攻击。

All in all, this is a "difficult" approach. 总而言之,这是一种“困难”的方法。

You could query What is my ip address? 您可以查询我的IP地址是什么? , or any of the other websites that can tell you (eg google what is my ip address). ,或任何其他可以告诉您的网站(例如google,我的IP地址是什么)。

do like this 像这样

String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
}
System.out.println("ipAddress:" + ipAddress);

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

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