正在查看: КЛ£ v1.0.0 应用的 HttpStack.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: КЛ£ v1.0.0 应用的 HttpStack.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.ding.rtc.http;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import com.ding.rtc.task.SimpleTask;
import com.ding.rtc.task.TaskExecutor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import org.json.JSONObject;
import org.webrtc.mozi.Logging;
import org.webrtc.mozi.ProxyInfo;
public class HttpStack {
private static final String ALIYUN_DNS_HOST = "203.107.1.1";
private static final String TAG = "HttpStack";
private static void closeQuietly(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (IOException e) {
}
}
static boolean isIP(String host) {
String[] parts = host.split("\\.");
if (parts.length != 4) {
return false;
}
for (String part : parts) {
try {
int num = Integer.parseInt(part);
if (num < 0 || num > 255) {
return false;
}
} catch (NumberFormatException e) {
return false;
}
}
return true;
}
private static HttpURLConnection createConnection(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(HttpURLConnection.getFollowRedirects());
return connection;
}
private static HttpURLConnection openConnection(URL url, String method, Map<String, String> headers, byte[] body, int timeoutMs) throws IOException {
HttpURLConnection connection = createConnection(url);
connection.setRequestMethod(method);
connection.setConnectTimeout(timeoutMs);
connection.setReadTimeout(timeoutMs);
connection.setUseCaches(false);
connection.setDoInput(true);
configHttps(connection, null);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if (body != null && body.length > 0) {
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
os = new GZIPOutputStream(new BufferedOutputStream(os));
}
os.write(body);
os.flush();
closeQuietly(os);
}
return connection;
}
private static HttpURLConnection openConnection(URL url, String method, Map<String, String> headers, int timeoutMs) throws IOException {
HttpURLConnection connection = createConnection(url);
connection.setRequestMethod(method);
connection.setConnectTimeout(timeoutMs);
connection.setReadTimeout(timeoutMs);
connection.setUseCaches(false);
connection.setDoInput(true);
configHttps(connection, null);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
return connection;
}
public static HttpStackResponse doPostSNI(String path, Map<String, String> headers, byte[] body, int timeoutMs, String ip, String host) {
return doHttpMethod(path, headers, body, timeoutMs, ip, host, "POST");
}
public static HttpStackResponse doGetSNI(String path, Map<String, String> headers, byte[] body, int timeoutMs, String ip, String host) {
return doHttpMethod(path, headers, body, timeoutMs, ip, host, "GET");
}
public static boolean doAsyncGetUpload(final String url, final Map<String, String> headers, final String filePath, final int timeoutMs, final HttpAsyncResponse response) {
if (TextUtils.isEmpty(url) || response == null) {
return false;
}
TaskExecutor.execute(new SimpleTask() {
@Override
public void run() {
HttpStackResponse httpStackResponse = HttpStack.doGet(url, headers, HttpStack.getFileBytes(filePath), timeoutMs, filePath);
response.onHttpResult(httpStackResponse);
}
});
return true;
}
public static boolean doAsyncGet(final String url, final Map<String, String> headers, final byte[] body, final int timeoutMs, final String filePath, final HttpAsyncResponse response) {
if (TextUtils.isEmpty(url) || response == null) {
return false;
}
TaskExecutor.execute(new SimpleTask() {
@Override
public void run() {
HttpStackResponse httpStackResponse = HttpStack.doGet(url, headers, body, timeoutMs, filePath);
response.onHttpResult(httpStackResponse);
}
});
return true;
}
public static HttpStackResponse doGet(String path, Map<String, String> headers, byte[] body, int timeoutMs, String filePath) {
HttpStackResponse response = null;
HttpURLConnection urlConnection = null;
try {
try {
URL requestedUrl = new URL(path);
try {
urlConnection = openConnection(requestedUrl, "GET", headers, body, timeoutMs);
int responseCode = urlConnection.getResponseCode();
long lastModified = urlConnection.getLastModified();
response = new HttpStackResponse();
response.code = responseCode;
response.lastModified = lastModified;
Map<String, List<String>> headerlists = urlConnection.getHeaderFields();
Set<String> keys = headerlists.keySet();
JSONObject jsonObject = new JSONObject();
for (String key : keys) {
URL requestedUrl2 = requestedUrl;
String val = urlConnection.getHeaderField(key);
if (key == null) {
key = "httpversion";
}
jsonObject.put(key, val);
requestedUrl = requestedUrl2;
}
response.headers = jsonObject.toString();
if (TextUtils.isEmpty(filePath)) {
response.result = readFully(urlConnection.getInputStream());
} else {
response.result = filePath.getBytes(StandardCharsets.UTF_8);
saveFile(urlConnection.getInputStream(), filePath);
}
} catch (Exception e) {
ex = e;
ex.printStackTrace();
Logging.e(TAG, "http get error: " + ex.getMessage());
if (0 != 0) {
Logging.e(TAG, "http get error header: " + response.headers);
}
}
} catch (Throwable th) {
th = th;
if (0 != 0) {
urlConnection.disconnect();
}
throw th;
}
} catch (Exception e2) {
ex = e2;
} catch (Throwable th2) {
th = th2;
if (0 != 0) {
}
throw th;
}
}
public static boolean doAsyncPostUpload(final String url, final Map<String, String> headers, final String filePath, final int timeoutMs, final HttpAsyncResponse response) {
if (TextUtils.isEmpty(url) || response == null) {
return false;
}
TaskExecutor.execute(new SimpleTask() {
@Override
public void run() {
HttpStackResponse httpStackResponse = HttpStack.doPost(url, headers, HttpStack.getFileBytes(filePath), timeoutMs, filePath);
response.onHttpResult(httpStackResponse);
}
});
return true;
}
public static boolean doAsyncPost(final String url, final Map<String, String> headers, final byte[] body, final int timeoutMs, final String filePath, final HttpAsyncResponse response) {
if (TextUtils.isEmpty(url) || response == null) {
return false;
}
TaskExecutor.execute(new SimpleTask() {
@Override
public void run() {
HttpStackResponse httpStackResponse = new HttpStackResponse();
String newUrl = url;
try {
URL address = new URL(newUrl);
String host = address.getHost();
boolean isIPHost = HttpStack.isIP(host);
boolean isHttps = url.startsWith(ProxyInfo.TYPE_HTTPS);
if (isIPHost && isHttps && headers.containsKey("Host")) {
String header_host = (String) headers.get("Host");
response.onHttpResult(HttpStack.doPostSNI(newUrl.replaceFirst(host, header_host), headers, body, timeoutMs, host, header_host));
} else {
response.onHttpResult(HttpStack.doPost(url, headers, body, timeoutMs, filePath));
}
} catch (MalformedURLException e) {
httpStackResponse.code = -1;
httpStackResponse.result = "url wrong, malformed exception".getBytes();
response.onHttpResult(httpStackResponse);
}
}
});
return true;
}
public static HttpStackResponse doPost(String url, Map<String, String> headers, byte[] body, int timeoutMs, String filePath) {
HttpStackResponse response = null;
HttpURLConnection urlConnection = null;
try {
try {
URL requestedUrl = new URL(url);
try {
urlConnection = openConnection(requestedUrl, "POST", headers, body, timeoutMs);
int responseCode = urlConnection.getResponseCode();
long lastModified = urlConnection.getLastModified();
response = new HttpStackResponse();
response.code = responseCode;
response.lastModified = lastModified;
Map<String, List<String>> headerLists = urlConnection.getHeaderFields();
Set<String> keys = headerLists.keySet();
JSONObject jsonObject = new JSONObject();
for (String key : keys) {
URL requestedUrl2 = requestedUrl;
String val = urlConnection.getHeaderField(key);
if (key == null) {
key = "httpversion";
}
jsonObject.put(key, val);
requestedUrl = requestedUrl2;
responseCode = responseCode;
}
response.headers = jsonObject.toString();
if (TextUtils.isEmpty(filePath)) {
response.result = readFully(urlConnection.getInputStream());
} else {
response.result = filePath.getBytes(StandardCharsets.UTF_8);
saveFile(urlConnection.getInputStream(), filePath);
}
} catch (Exception e) {
ex = e;
ex.printStackTrace();
Logging.e(TAG, "http post " + url + ", error: " + ex.getMessage());
if (0 != 0) {
Logging.e(TAG, "http post error header: " + response.headers);
} else {
String exMsg = ex.getMessage() != null ? ex.getMessage() : "ex message null";
response = new HttpStackResponse();
response.code = -1;
if (response.result == null || response.result.length == 0) {
response.result = exMsg.getBytes();
}
}
}
} catch (Throwable th) {
th = th;
if (0 != 0) {
urlConnection.disconnect();
}
throw th;
}
} catch (Exception e2) {
ex = e2;
} catch (Throwable th2) {
th = th2;
if (0 != 0) {
}
throw th;
}
}
public static HttpStackResponse multipartPost(String url, String BOUNDARY, String LINE_FEED, Map<String, String> headers, MultipartWriter writer) {
HttpStackResponse response = null;
OutputStream outputStream = null;
PrintWriter printWriter = null;
HttpURLConnection conn = null;
try {
try {
URL requestedUrl = new URL(url);
conn = openConnection(requestedUrl, "POST", headers, 3000);
conn.setDoOutput(true);
outputStream = conn.getOutputStream();
printWriter = (headers == null || !"gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) ? new PrintWriter(outputStream) : new PrintWriter(new GZIPOutputStream(outputStream));
writer.addPart(printWriter, outputStream);
printWriter.append((CharSequence) LINE_FEED);
printWriter.append("--").append((CharSequence) BOUNDARY).append("--").append((CharSequence) LINE_FEED);
printWriter.flush();
int responseCode = conn.getResponseCode();
long lastModified = conn.getLastModified();
response = new HttpStackResponse();
response.code = responseCode;
response.result = readFully(conn.getInputStream());
response.lastModified = lastModified;
printWriter.close();
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} finally {
}
} catch (MalformedURLException e2) {
e2.printStackTrace();
if (printWriter != null) {
printWriter.close();
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
}
} catch (IOException e4) {
e4.printStackTrace();
if (printWriter != null) {
printWriter.close();
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e5) {
e5.printStackTrace();
}
}
}
}
private static byte[] readFully(InputStream inputStream) throws IOException {
if (inputStream == null) {
return new byte[0];
}
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (true) {
int available = bufferedInputStream.read(buffer);
if (available >= 0) {
byteArrayOutputStream.write(buffer, 0, available);
} else {
return byteArrayOutputStream.toByteArray();
}
}
} finally {
closeQuietly(bufferedInputStream);
}
}
public static byte[] getFileBytes(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return null;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while (true) {
int available = fis.read(buffer);
if (available < 0) {
byte[] result = byteArrayOutputStream.toByteArray();
return result;
}
byteArrayOutputStream.write(buffer, 0, available);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
closeQuietly(fis);
}
}
private static void saveFile(InputStream inputStream, String filePath) {
if (inputStream == null || TextUtils.isEmpty(filePath)) {
return;
}
FileOutputStream fos = null;
try {
try {
FileUtil.createFilePath(null, filePath);
File file = new File(filePath);
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while (true) {
int available = inputStream.read(buffer);
if (available < 0) {
break;
} else {
fos.write(buffer, 0, available);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} finally {
closeQuietly(fos);
closeQuietly(inputStream);
}
}
private static void configHttps(HttpURLConnection urlConnection, final String verifyHost) {
if (Build.VERSION.SDK_INT < 22) {
configHttpsOnPreLollipop(urlConnection, verifyHost);
return;
}
if (!(urlConnection instanceof HttpsURLConnection)) {
return;
}
HttpsURLConnection conn = (HttpsURLConnection) urlConnection;
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, null, null);
conn.setSSLSocketFactory(sslcontext.getSocketFactory());
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier defaultHostVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
if (defaultHostVerifier.verify(HttpStack.ALIYUN_DNS_HOST, session)) {
return true;
}
if (TextUtils.isEmpty(verifyHost)) {
return defaultHostVerifier.verify(hostname, session);
}
return verifyHost.equals(hostname) || defaultHostVerifier.verify(verifyHost, session);
}
};
conn.setHostnameVerifier(hostnameVerifier);
} catch (Exception e) {
e.printStackTrace();
Logging.e(TAG, "configHttps error:" + Log.getStackTraceString(e));
}
}
private static void configHttpsOnPreLollipop(HttpURLConnection urlConnection, final String verifyHost) {
if (!(urlConnection instanceof HttpsURLConnection) || Build.VERSION.SDK_INT >= 22) {
return;
}
HttpsURLConnection conn = (HttpsURLConnection) urlConnection;
try {
conn.setSSLSocketFactory(new PreLollipopTLSSocketFactory());
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier defaultHostVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
if (defaultHostVerifier.verify(HttpStack.ALIYUN_DNS_HOST, session)) {
return true;
}
if (TextUtils.isEmpty(verifyHost)) {
return defaultHostVerifier.verify(hostname, session);
}
return verifyHost.equals(hostname) || defaultHostVerifier.verify(verifyHost, session);
}
};
conn.setHostnameVerifier(hostnameVerifier);
} catch (Exception exc) {
Logging.e(TAG, "Error while setting TLS 1.2" + Log.getStackTraceString(exc));
}
}
private static HttpStackResponse doHttpMethod(String str, Map<String, String> map, byte[] bArr, int i, String str2, String str3, String str4) {
String str5;
?? r9;
URL url;
int responseCode;
int i2;
String str6;
HttpsURLConnection httpsURLConnection;
String sb;
HttpsURLConnection httpsURLConnection2;
OutputStream outputStream;
byte[] bArr2 = bArr;
?? r2 = 0;
?? r22 = 0;
HttpStackResponse httpStackResponse = null;
if (str == null || str.isEmpty() || str3 == null) {
str5 = TAG;
} else {
try {
if (!str3.isEmpty()) {
try {
url = new URL(str);
} catch (Exception e) {
e = e;
r9 = TAG;
}
if (!ProxyInfo.TYPE_HTTPS.equals(url.getProtocol())) {
String str7 = "doPost failed, " + url.getProtocol() + " is not https";
Logging.e(TAG, str7);
HttpStackResponse httpStackResponse2 = new HttpStackResponse();
httpStackResponse2.code = -1;
httpStackResponse2.result = str7.getBytes();
if (0 != 0) {
r2.disconnect();
}
return httpStackResponse2;
}
if (str2 != null && !str2.isEmpty() && url.getPort() == -1) {
Logging.i(TAG, "replace host:" + url.getHost() + " to ip:" + str2);
}
final HttpsURLConnection httpsURLConnection3 = (HttpsURLConnection) url.openConnection();
try {
httpsURLConnection3.setRequestProperty("Host", str3);
httpsURLConnection3.setRequestMethod(str4);
httpsURLConnection3.setConnectTimeout(i);
httpsURLConnection3.setReadTimeout(i);
httpsURLConnection3.setUseCaches(false);
httpsURLConnection3.setDoInput(true);
httpsURLConnection3.setInstanceFollowRedirects(false);
if (map != null) {
try {
for (Map.Entry<String, String> entry : map.entrySet()) {
httpsURLConnection3.setRequestProperty(entry.getKey(), entry.getValue());
}
} catch (Exception e2) {
e = e2;
r22 = httpsURLConnection3;
r9 = TAG;
e.printStackTrace();
Logging.e(r9, "http post sni " + str + ", ip:" + str2 + ", error: " + e.getMessage());
if (0 == 0) {
Logging.e(r9, "http post sni " + str + ", ip:" + str2 + ", error header: " + httpStackResponse.headers);
} else {
String str8 = e.getMessage() != null ? "sni-" + str2 + ", " + e.getMessage() : "ex message null";
httpStackResponse = new HttpStackResponse();
httpStackResponse.code = -1;
httpStackResponse.result = str8.getBytes();
}
if (r22 != 0) {
r22.disconnect();
}
return httpStackResponse;
} catch (Throwable th) {
th = th;
r2 = httpsURLConnection3;
if (r2 != 0) {
r2.disconnect();
}
throw th;
}
}
httpsURLConnection3.setSSLSocketFactory(new TlsSniSocketFactory(httpsURLConnection3));
httpsURLConnection3.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier defaultHostVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
if (defaultHostVerifier.verify(HttpStack.ALIYUN_DNS_HOST, session)) {
return true;
}
String host = httpsURLConnection3.getRequestProperty("Host");
if (host == null) {
host = httpsURLConnection3.getURL().getHost();
}
boolean verified = defaultHostVerifier.verify(host, session);
if (!verified) {
Logging.w(HttpStack.TAG, "host name verify failed");
}
return verified;
}
});
if (bArr2 != null && bArr2.length > 0) {
httpsURLConnection3.setDoOutput(true);
OutputStream outputStream2 = httpsURLConnection3.getOutputStream();
if (map != null) {
outputStream = outputStream2;
if ("gzip".equals(map.get(HttpHeaders.CONTENT_ENCODING))) {
outputStream = new GZIPOutputStream(new BufferedOutputStream(outputStream2));
}
} else {
outputStream = outputStream2;
}
outputStream.write(bArr2);
outputStream.flush();
closeQuietly(outputStream);
}
responseCode = httpsURLConnection3.getResponseCode();
try {
} catch (Exception e3) {
e = e3;
r22 = map;
r9 = bArr2;
} catch (Throwable th2) {
th = th2;
r2 = map;
}
} catch (Exception e4) {
e = e4;
r9 = TAG;
r22 = httpsURLConnection3;
} catch (Throwable th3) {
th = th3;
r2 = httpsURLConnection3;
}
if (!needRedirect(responseCode)) {
long lastModified = httpsURLConnection3.getLastModified();
httpStackResponse = new HttpStackResponse();
httpStackResponse.code = responseCode;
httpStackResponse.lastModified = lastModified;
Set<String> keySet = httpsURLConnection3.getHeaderFields().keySet();
JSONObject jSONObject = new JSONObject();
for (String str9 : keySet) {
String headerField = httpsURLConnection3.getHeaderField(str9);
if (str9 != null) {
i2 = responseCode;
str6 = str9;
} else {
i2 = responseCode;
str6 = "httpversion";
}
jSONObject.put(str6, headerField);
responseCode = i2;
lastModified = lastModified;
}
httpStackResponse.headers = jSONObject.toString();
httpStackResponse.result = readFully(httpsURLConnection3.getInputStream());
Logging.d(TAG, "doPost: response:" + httpStackResponse);
if (httpsURLConnection3 != null) {
httpsURLConnection3.disconnect();
}
return httpStackResponse;
}
String headerField2 = httpsURLConnection3.getHeaderField("Location");
if (headerField2 == null) {
headerField2 = httpsURLConnection3.getHeaderField("location");
}
if (!headerField2.startsWith("http://")) {
try {
if (!headerField2.startsWith("https://")) {
URL url2 = new URL(str);
StringBuilder sb2 = new StringBuilder();
httpsURLConnection = httpsURLConnection3;
try {
sb2.append(url2.getProtocol());
sb2.append("://");
sb2.append(url2.getHost());
sb2.append(headerField2);
sb = sb2.toString();
httpsURLConnection2 = httpsURLConnection;
HttpStackResponse doPostSNI = doPostSNI(sb, map, bArr, i, null, str3);
if (httpsURLConnection2 != null) {
httpsURLConnection2.disconnect();
}
return doPostSNI;
} catch (Exception e5) {
e = e5;
r9 = TAG;
r22 = httpsURLConnection;
e.printStackTrace();
Logging.e(r9, "http post sni " + str + ", ip:" + str2 + ", error: " + e.getMessage());
if (0 == 0) {
}
if (r22 != 0) {
}
return httpStackResponse;
} catch (Throwable th4) {
th = th4;
r2 = httpsURLConnection;
if (r2 != 0) {
}
throw th;
}
}
} catch (Exception e6) {
e = e6;
r9 = TAG;
r22 = httpsURLConnection3;
} catch (Throwable th5) {
th = th5;
r2 = httpsURLConnection3;
}
}
httpsURLConnection = httpsURLConnection3;
sb = headerField2;
httpsURLConnection2 = httpsURLConnection;
HttpStackResponse doPostSNI2 = doPostSNI(sb, map, bArr, i, null, str3);
if (httpsURLConnection2 != null) {
}
return doPostSNI2;
}
str5 = TAG;
} catch (Throwable th6) {
th = th6;
}
}
String str10 = "illegal argument, path:" + str + ", ip:" + str2 + ", host:" + str3;
Logging.e(str5, str10);
HttpStackResponse httpStackResponse3 = new HttpStackResponse();
httpStackResponse3.code = -1;
httpStackResponse3.result = str10.getBytes();
return httpStackResponse3;
}
private static boolean needRedirect(int code) {
return code >= 300 && code < 400;
}
}