How to use Twilio AndroidSDK behind sock proxy with Java System Property - android

I will use Twilio AndroidSDK. i need using Twilio AndroidSDK behind socks5 proxy.
I wrote this code under ClientActivity.java in twilio-mobile-quickstart
NEW IDEA: no use ProxySelector. only Systems Property Connecting..
protected void onCreate(Bundle savedInstanceState) {
System.setProperty("java.net.useSystemProxies","true");
System.setProperty("proxyHost","[Proxy Server Addr]");
System.setProperty("proxyPort","[Proxy Server Port]");
System.setProperty("http.proxyHost","[Proxy Server Addr]");
System.setProperty("http.proxyPort","[Proxy Server Port]");
System.setProperty("https.proxyHost","[Proxy Server Addr]");
System.setProperty("https.proxyPort","[Proxy Server Port]");
System.setProperty("socketsProxyHost","[SOCKS5 Server Addr]");
System.setProperty("socketsProxyPort","[SOCKS5 Server Port]");
I get new errors. can't connecting SSL.
i feel my device connect to host directlly.
Twilio this https connection can't connect with proxies? :(
in closed network, detect this log:
01-31 16:17:15.617 17711-30305/com.twilio.client.quickstart D/HttpLongPollThread: connecting to host matrix.twilio.com on port 443
01-31 16:17:15.697 17711-30305/com.twilio.client.quickstart W/HttpLongPollThread: long poller got exception
java.net.ConnectException: failed to connect to matrix.twilio.com/52.54.166.186 (port 443) after 30000ms: isConnected failed: ENETUNREACH (Network is unreachable)
at libcore.io.IoBridge.isConnected(IoBridge.java:234)
at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
at java.net.Socket.connect(Socket.java:884)
at com.twilio.client.impl.net.HttpLongPollThread.setUpSocket(HttpLongPollThread.java:62)
at com.twilio.client.impl.net.HttpLongPollThread.run(HttpLongPollThread.java:287)
Caused by: android.system.ErrnoException: isConnected failed: ENETUNREACH (Network is unreachable)
at libcore.io.IoBridge.isConnected(IoBridge.java:223)
at libcore.io.IoBridge.connectErrno(IoBridge.java:171) 
at libcore.io.IoBridge.connect(IoBridge.java:122) 
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452) 
at java.net.Socket.connect(Socket.java:884) 
at com.twilio.client.impl.net.HttpLongPollThread.setUpSocket(HttpLongPollThread.java:62) 
at com.twilio.client.impl.net.HttpLongPollThread.run(HttpLongPollThread.java:287) 
Anyone might have an idea where did i do wrong? Thanks in advance.

Twilio developer evangelist here.
I'm afraid the Twilio Client Android SDK does not currently support SOCKS proxies, which is why you are seeing these issues.
I recommend you get in touch with Twilio support with your requirements and they will be able to notify you if proxy support is added to the SDK.
Let me know if that helps at all.

Related

Android WearOS app SocketTimeoutException failing to connect to a local server when bluetooth connected

I am developing a smartwatch app which connects to an API and make requests. The thing is, when I try to connect to the server and my phone is not connected to the watch, it works well and it connects to my local pc serving a rails API in 192.168.1.132:3000. The problem comes when the watch is connected to the phone via bluetooth. I get this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.victorrubia.tfg, PID: 5079
java.net.SocketTimeoutException: failed to connect to /192.168.1.132 (port 3000) from /192.168.167.239 (port 37933) after 10000ms
at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
at libcore.io.IoBridge.connect(IoBridge.java:129)
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.kt:63)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
I can't understand how is possible the watch is connecting well to my server when it is only connected via WIFI and when it is also paired with the phone it cannot connect to my server. The smartwatch I am using is a TicWatch 3 connected via ADB WIFI.
I have tried to make a GET request to a remote domain such as google.com and it performs well in both cases (connected only to WIFI and WIFI+Bluetooth) so I would really appreciate if anyone could help me sort this out.
PD: I have also looked at this question but no solution was provided.
I'm not sure about the failure to connect specifically to local addresses over bluetooth, but you can request a specific network and set it as the default.
https://github.com/square/okhttp/issues/2404
or specify it using a socketfactory
https://programmer.ink/think/the-implementation-of-okhttp-multi-network-communication.html
public void onAvailable(final Network network) {
super.onAvailable(network);
//After the network is connected
SocketFactory socketFactory = network.getSocketFactory();
//If you use the eclipse MQTT package, you need to set the
mqttConnectOptions.setSocketFactory(socketFactory);
//If you use regular network connection, okhttp, you need to set
OkHttpClient okHttpClient = new OkHttpClient.Builder()
//.proxy(Proxy.NO_PROXY)
.socketFactory(socketFactory)
.dns(new Dns() {
#Override
public List<InetAddress> lookup(#NonNull String hostname) throws UnknownHostException {
return Arrays.asList(network.getAllByName(hostname));
}
})
.build();

RabbitMqtt not listening to 1883 port

I want to use rabbitMqtt and I programming my client by android.but in android code i have MqttException for this line of code mqttClient.connect(generateClientId(), cleanStart, keepAliveSeconds);
when I get cause of this exception, that return this error:
java.net.ConnectException: failed to connect to /192.168.0.86 (port 1883) after 1200000ms: isConnected failed: ECONNREFUSED (Connection refused)
I guessed this problem comes from that perhaps my RabbitMqtt broker not listen to port 1883. My rabbitMqtt is running and I performed the server but I can't see any port by name of 1883 in Resource Monitor. please help me

AsyncHttpClient doesn't use the HTTPS port set in the constructor

I'm trying to change the port I'm using for HTTPS connection between my Android client and my server, but the port doesn't get set for some reason. I initialize the HTTP client like this:
// Use port 8185 for HTTPS
client = new AsyncHttpClient(80, 8185);
... but I get an Exception:
org.apache.http.conn.HttpHostConnectException: Connection to https://192.168.1.35 refused
...
Cause by: java.net.ConnectException: failed to connect to /192.168.1.35 (port 443) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
Based on these errors, it seems that the default port 443 is still being used. Why is that? Why can't I use the non-default port 8185?
Edit: This is the library I'm using.
From what I've read in the Javadocs and on the lib website, perhaps you could try something like:
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder().setProxyServer(new ProxyServer(ProxyServer.Protocol.HTTPS,"IP_ADDRESS", 8185)).build();
AsyncHttpClient c = new AsyncHttpClient(cf);

XMPP messages lost on Android while switching data connectivity (e.g. turning Wifi of, GSM <-> Wifi switch)

I'm developing a chat application in android. Using XMPP server. PHP server has been migrated to ejabber.
My notification works properly in all scenarios. But in one case I am losing the XMPP messages. In fact I am not getting notifications.
In detail, I am turning off the WiFi or data connection. and after turning off WiFi or data connection any notification sent to me that is not getting. after when I turn ON the WiFi or data connection, I am not able to get the messages which were sent to me.
I have implemented service in background. that makes xmpp connection always live.
I getting this error while i turn OFF the WiFi connection.
java.net.SocketException: sendto failed: EPIPE (Broken pipe)
java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
at libcore.io.IoBridge.sendto(IoBridge.java:475)
at java.net.PlainSocketImpl.write(PlainSocketImpl.java:508)
at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:270)
at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
at java.io.BufferedWriter.flush(BufferedWriter.java:124)
at org.jivesoftware.smack.PacketWriter.writePackets(PacketWriter.java:286)
at org.jivesoftware.smack.PacketWriter.access$12(PacketWriter.java:270)
at org.jivesoftware.smack.PacketWriter$1.run(PacketWriter.java:137)
Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
at libcore.io.Posix.sendtoBytes(Native Method)
at libcore.io.Posix.sendto(Posix.java:146)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
at libcore.io.IoBridge.sendto(IoBridge.java:473)
... 9 more
at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:489)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241)
at java.io.InputStreamReader.read(InputStreamReader.java:244)
at java.io.BufferedReader.read(BufferedReader.java:310)
at org.jivesoftware.smack.XMPPConnection$AliveReader.read(XMPPConnection.java:1122)
at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1496)
at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:979)
at org.kxml2.io.KXmlParser.next(KXmlParser.java:346)
at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:321)
at org.jivesoftware.smack.PacketReader.access$1(PacketReader.java:216)
at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:70)
Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:131)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
... 13 more
So to install mod_archive I need to do this, the typical:
Write following line in cmd. (For cent Os)
Login as root
Create new directory:
mkdir ejabberd_archive_plugin
cd ejabberd_archive_plugin
now get the latest ejabberd modules using following command
yum https://svn.process-one.net/ejabberd-modules
cd ejabberd-modules/mod_archive/trunk
./build.sh
Now go to ebin directory using following cmd and get the list of all *.beam files for ejabberd archive plugins.
root#ns1 [/ejabberd_archive_plugin/ejabberd-modules/mod_archive/trunk/ebin]# ls –l
Using following cmd you will get all ejabberd *.beam files list when ejabberd setup and configure.
root#ns1 [/usr/lib64/ejabberd/ebin]# ls -l
Now copy all *.beam (archive plugins) files to ejabberd ebin directory using following cmd
root#ns1 [/]# cp /ejabberd_archive_plugin/ejabberd-modules/mod_archive/trunk/ebin /usr/lib64/ejabberd/ebin
Now go to following URL and copy all sql table queries and run in phpmyadmin (ejabberd database)
https://svn.process-one.net/ejabberd-modules/mod_archive/trunk/src/mod_archive_odbc_mysql.sql
Now configure ejjaberd.cfg using following cmd
root#ns1 [/etc/ejabberd]# vi ejabberd.cfg
Add the following line into ejabberd.cfg file in modules configuration section
{mod_archive_odbc, [{database_type, "mysql"}, {default_auto_save, true}, {enforce_default_auto_save, true}]},
Now restart the ejabberd service.
root#ns1 [/etc/ejabberd]# sudo service ejabberd restart
You need "Stream Management" XEP-198 enabled connections to achieve this. Georg wrote a good blog entry about that topic: http://op-co.de/blog/posts/XEP-0198/
Basically it allows for stanza acknowledgement and stream resumption, which is exaclty what you want to do in case of e.g a Wifi <-> GSM switch.
I conclude from the stacktrace that you are using aSmack (which is Smack ported to Android). There is a open feature request to implement Stream Management in Smack: SMACK-333. In fact, i've recently started working on the implementation, but can't name an ETA.
You have to create the separate web service for that. so thus why we can handle the history of the sent message. we can manage the offline message.
XEP-198 is the only solution. make plugin for your server and make that easy to manage the remain message that is lose. there may be other solution but AFAIK, this would be the solution.
you can modify archive plugin to save message ids in it.
When user comes online call a web service with last message id received. server can get all the later messages saved on the server. I created it i the same way and working perfectly well.
You should relogin after reconnecting to the internet everytime. But, keep in mind that if you relogin with same resource and same user even it is online then server will throw stream error (replaced by new connection) which will make you offline & you won't receive any message. For this, disconnect yourself properly first before relogin. Try searching on mod Amp module.

Android webservices connection refused

I am trying to access RESTful webservice from android using below code.
ClientConfig config= new DefaultClientConfig();
Client client=Client.create(config);
WebResource service=client.resource("http://localhost:8085/JerseyTest/jersey/test/testing123");
But am getting the following exception.
07-22 16:53:15.450: E/AndroidRuntime(1491): Caused by: com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8085): connect failed: ECONNREFUSED (Connection refused)
But the url is working well in broswers and i also tried with firewall turned off.Anyone could help me with this?
I think, You should change localhost by the ip of the computer where you have the WebService
Check whether u have added all the permission required and try IP address instead of localhost

Categories

Resources