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

Avoid Proxyception. Bypassing the system proxy on the Android device when testing ping to the proxy. #69

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;

import com.danikula.videocache.file.DiskUsage;
import com.danikula.videocache.file.FileNameGenerator;
import com.danikula.videocache.file.Md5FileNameGenerator;
import com.danikula.videocache.file.TotalCountLruDiskUsage;
import com.danikula.videocache.file.TotalSizeLruDiskUsage;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.util.Locale;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -114,20 +122,35 @@ private void makeSureServerWorks() {

private boolean pingServer() throws ProxyCacheException {
String pingUrl = appendToProxyUrl(PING_REQUEST);
HttpUrlSource source = new HttpUrlSource(pingUrl);

StringBuilder sb = new StringBuilder();
InputStream is = null;

try {
byte[] expectedResponse = PING_RESPONSE.getBytes();
source.open(0);
byte[] response = new byte[expectedResponse.length];
source.read(response);
boolean pingOk = Arrays.equals(expectedResponse, response);
Log.d(LOG_TAG, "Ping response: `" + new String(response) + "`, pinged? " + pingOk);
return pingOk;
} catch (ProxyCacheException e) {
Log.e(LOG_TAG, "Error reading ping response", e);
//Don't try to use a proxy to access our proxy. Proxyception.
HttpURLConnection connection = (HttpURLConnection) new URL(pingUrl).openConnection(Proxy.NO_PROXY);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this fix resolves issue? Bypassing system proxy allows only ping cacheproxy, but MediaPlayer will not whatever.


is = new BufferedInputStream(connection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String inputLine;
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
return PING_RESPONSE.equals(sb.toString());
}
catch (Exception e) {
Log.i(LOG_TAG, "Error reading InputStream");
return false;
} finally {
source.close();
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException e) {
Log.i(LOG_TAG, "Error closing InputStream");
}
}
}
}

Expand Down