0

I have an HttpServlet running on Google App Engine. My doGet() method looks like this:

public class IpServlet extends HttpServlet {
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws IOException {
    String ra = req.getRemoteAddr();
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    resp.getWriter().write(ra);
  }
}

I would like for this getRemoteAddr to always return an IPv4 address. The following answer appears to indicate that this is configurable:

HttpServletRequest.getRemoteAddr() in Tomcat returns IPv6 formatted IP address

Can I somehow add this parameter -Djava.net.preferIPv4Stack=true to my Google App Engine startup commandline? In appengine-web.xml or web.xml perhaps?

Edit:

After some more looking around, I added the following to system-properties in appengine-web.xml:

<property name="java.net.preferIPv4Stack" value="true"/>

But this doesn't appear to work as expected.

Community
  • 1
  • 1
waynemystir
  • 323
  • 3
  • 11
  • I cannot imagine any good reason to limit an application to ipv4. Would you care to elaborate on the reasons for this artificial limitation? You could simply drop any ipv6 connection in a filter servlet. But you'd lock out any users accessing your website through ipv6. – konqi Sep 29 '15 at 14:30
  • Good question @konqi. The unfortunate reason for this artificial limitation is a requirement from Expedia Affiliate Network http://developer.ean.com/docs/common. The parameter customerIpAddress must be in IPv4 format. I specifically asked them if they accept IPv6 and they replied with an unequivocal "no". – waynemystir Sep 29 '15 at 14:35
  • I am writing an iPhone app against the EAN API. Can I somehow require that the iPhone client use IPv4? – waynemystir Sep 29 '15 at 14:37
  • Okay now i understand. You need to give the customer ip (an ipv4 address) to that service in order for them to proceed. You could try and send them only the last 4 byte of the ipv6 address and see what happens. Aside from that i think that's an unusual use case you could possible set the prefered IP stack in a managed VM or contact Google Support and ask how you should handle this. Actually. You could also use one of those 'whatismyip' services and get the clients ipv4 address like that. – konqi Sep 29 '15 at 15:39
  • Many excellent points @konqi. Regarding your last point, I have been using http://ip-api.com/line/?fields=query for development purposes. I am awaiting their response to see if they can guarantee IPv4. I would rather use my own app residing on App Engine for this. I might try contacting Google Support. – waynemystir Sep 29 '15 at 16:27

0 Answers0