优化网络劫持检测机制
This commit is contained in:
@ -7,13 +7,19 @@ import android.util.Log;
|
||||
import com.gh.common.util.HttpsUtils;
|
||||
import com.gh.common.util.Utils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class DownloadThread extends Thread {
|
||||
|
||||
@ -83,7 +89,19 @@ public class DownloadThread extends Thread {
|
||||
}
|
||||
String eTag2 = entry.getETag();
|
||||
if (!TextUtils.isEmpty(eTag2) && !eTag2.equals(eTag)) {
|
||||
//链接被劫持,抛出异常
|
||||
// 链接被劫持,抛出异常
|
||||
if (!entry.isChange()
|
||||
&&"download.ghzhushou.com".equals(new URL(entry.getUrl()).getHost())
|
||||
&& ("apk2.ghzhushou.com".equals(connection.getURL().getHost())
|
||||
|| "apk.ghzhushou.com".equals(connection.getURL().getHost()))) {
|
||||
String newETag = getETag(entry.getUrl());
|
||||
if (!TextUtils.isEmpty(newETag)) {
|
||||
entry.setETag(newETag);
|
||||
entry.setChange(true);
|
||||
download();
|
||||
return;
|
||||
}
|
||||
}
|
||||
Utils.log("eTag = " + eTag);
|
||||
Utils.log("eTag2 = " + eTag2);
|
||||
listener.onStatusChanged(DownloadStatus.hijack, connection.getURL().toString());
|
||||
@ -186,6 +204,37 @@ public class DownloadThread extends Thread {
|
||||
return connection;
|
||||
}
|
||||
|
||||
private String getETag(String url) {
|
||||
try {
|
||||
String newUrl = "http://download.ghzhushou.com/etag"
|
||||
+ "?url=" + URLEncoder.encode(url, "utf-8")
|
||||
+ "&" + System.currentTimeMillis();
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(newUrl).openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setConnectTimeout(CONNECT_TIME);
|
||||
connection.setReadTimeout(READ_TIME);
|
||||
connection.setDoInput(true);
|
||||
connection.connect();
|
||||
|
||||
int code = connection.getResponseCode();
|
||||
if (code == 200) {
|
||||
InputStream is = connection.getInputStream();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[2048];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, len);
|
||||
}
|
||||
baos.flush();
|
||||
JSONObject response = new JSONObject(baos.toString("utf-8"));
|
||||
return response.getString("etag");
|
||||
}
|
||||
} catch (IOException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setStatus(DownloadStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user