java-apns icon indicating copy to clipboard operation
java-apns copied to clipboard

Proxy support does not work when using a IP address. Only works with hostname.

Open vijaygos opened this issue 11 years ago • 5 comments

The makeTunnel() function in TlsTunnelBuilder.java has a bug that does not allow a tunnel to be established with APNS when the proxy information available is an IP address and not a hostname.

vijaygos avatar Sep 02 '14 15:09 vijaygos

Hello, this bug has not been fixed yes?, the thing is that line 94 ( String proxyHost = proxyAddress.getAddress().toString().substring(0, proxyAddress.getAddress().toString().indexOf("/"));), of TlsTunnelBuilder class, is making a substring from position 0 to the charcarter "/", since the object InetSocketAddress adds an "/" to the IP addresses (I.E /192.168.243.155), the substring is returning an empty string,

rlinero avatar Sep 28 '15 02:09 rlinero

Are you able to use HTTP proxy successfully ?

today-not-someday avatar Mar 27 '17 09:03 today-not-someday

Yes we did, had to make some changes in code

On Mar 27, 2017 12:26 PM, "Kapil Nanda" [email protected] wrote:

Are you able to use HTTP proxy successfully ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/notnoop/java-apns/issues/186#issuecomment-289397755, or mute the thread https://github.com/notifications/unsubscribe-auth/AOLV2B-pjJbHSBfUcyvRAM4VQeuKVQgCks5rp34NgaJpZM4Cdkas .

rlinero avatar Mar 29 '17 01:03 rlinero

Provide a temp solution if anyone is in the same trouble

private class PushSocketAddress extends InetSocketAddress{
        public PushSocketAddress(String hostname, int port) {
            super(hostname, port);
        }

        @Override
        public String toString() {
            return getHostName()+"/";
        }
    }

in your code,you can use

Proxy proxy = new Proxy(Proxy.Type.HTTP, new PushSocketAddress(proxyHost, proxyPort));
apnsService = APNS.newService().withCert(p12CertContent, passwd)
                        .withAppleDestination(true).withProxy(proxy).build();

as you see, we override the toString method and successfully bypass the bug substring

String proxyHost = proxyAddress.getAddress().toString().substring(0, proxyAddress.getAddress().toString().indexOf("/"));

sound2gd avatar Sep 08 '17 11:09 sound2gd

The analysis by @rlinero is correct and points out the root cause.

com.notnoop.apns.internal.TlsTunnelBuilder::makeTunnel()

[...]
String proxyHost = proxyAddress.getAddress().toString().substring(0, proxyAddress.getAddress().toString().indexOf("/"));
[...]

java.net.InetSocketAddress::getAddress returns of the form: hostname / literal IP address If the host name is unresolved, no reverse name service lookup is performed. The hostname part will be represented by an empty string (see https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#toString() )

Better approach would be simply:

String proxyHost = proxyAddress.getHostString()

Returns the hostname, or the String form of the address if it doesn't have a hostname (it was created using a literal IP). But only works since Java 1.7.

More generic:

String proxyHost = proxyAddress.getAddress().getHostAddress()

... to use IP address (regardless if proxy information has been provided as IP address or hostname)

or

String proxyHost = proxyAddress.getAddress().getHostName()

... to use hostname (either provided) or via reverse lookup (if IP address was provided).

webstick79 avatar Oct 17 '18 12:10 webstick79