1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.fear1ess.reyunaditool;
import android.content.res.Resources;
import android.util.Log;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static com.fear1ess.reyunaditool.DoWorkThread.getAppInfoUrlStr;
public class NetWorkUtils {
public interface NetWorkCallback {
void onDownloadPkgNameGetSuccess(String name);
void onDownloadPkgNameGetfailed();
void onDownloadBytesUpdate (String name, int bytesDownloaded);
}
private static NetWorkCallback callback;
public static void setNetWorkCallback(NetWorkCallback cb) {
callback = cb;
}
private static Response get(String urlStr,HashMap<String,String> headers,OutputStream os){
if(os == null){
os = new ByteArrayOutputStream();
}
Response res = null;
MyLog.d("get =========== "+urlStr);
if(urlStr.startsWith("https://")) res = httpsReq(urlStr,headers,null,os);
else if(urlStr.startsWith("http://")) res = httpReq(urlStr,headers,null,os);
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
private static Response post(String urlStr,HashMap<String,String> headers,byte[] data,OutputStream os){
if(os == null){
os = new ByteArrayOutputStream();
}
Response res = null;
if(urlStr.startsWith("https://")) res = httpsReq(urlStr,headers,data,os);
else if(urlStr.startsWith("http://")) res = httpReq(urlStr,headers,data,os);
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
public static Response get(String urlStr,HashMap<String,String> headers){
return get(urlStr,headers,null);
}
public static Response post(String urlStr,HashMap<String,String> headers,byte[] data){
return post(urlStr,headers,data,null);
}
public static Response download(String urlStr,HashMap<String,String> headers,byte[] data,String downloadPath) {
File fi = new File(downloadPath);
try {
if(!fi.exists()) fi.createNewFile();
FileOutputStream fos = null;
fos = new FileOutputStream(fi);
if(data == null) return get(urlStr,headers,fos);
else return post(urlStr,headers,data,fos);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static Response httpReq(String urlStr, HashMap<String,String> headers, byte[] postData,OutputStream os) {
boolean isDownload = false;
if(os instanceof FileOutputStream) {
isDownload = true;
}
Response res = null;
HttpURLConnection huc = null;
try {
URL getAppInfoUrl = new URL(urlStr);
huc = (HttpURLConnection) getAppInfoUrl.openConnection();
if(postData == null) huc.setRequestMethod("GET");
else {
huc.setRequestMethod("POST");
huc.setRequestProperty("Content-Type", "application/json");
}
if(headers != null){
for(Map.Entry<String,String> entry:headers.entrySet()){
huc.setRequestProperty(entry.getKey(),entry.getValue());
}
}
huc.setConnectTimeout(10*1000);
huc.setReadTimeout(10*1000);
huc.connect();
if(postData != null){
OutputStream out = huc.getOutputStream();
out.write(postData);
out.flush();
}
String fileName = huc.getHeaderField("Content-Disposition");
if(fileName == null){
callback.onDownloadPkgNameGetfailed();
huc.disconnect();
return null;
}
String pkgName = fileName.replace("attachment; filename=","")
.replace(".apk", "");
if(isDownload){
callback.onDownloadPkgNameGetSuccess(pkgName);
}
InputStream is = huc.getInputStream();
byte[] data = new byte[8192];
int len = 0;
int totalBytes = 0;
while((len = is.read(data)) != -1){
os.write(data,0,len);
totalBytes += len;
if(isDownload){
if(fileName != null) {
callback.onDownloadBytesUpdate(pkgName, totalBytes);
}
}
}
os.flush();
byte[] resData = null;
if(os instanceof ByteArrayOutputStream) {
resData = ((ByteArrayOutputStream) os).toByteArray();
}
res = new Response(huc.getResponseCode(),huc.getResponseMessage(),resData,pkgName);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
huc.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
}finally {
Log.d("aditool", "close httpconnection.... ");
huc.disconnect();
return res;
}
}
}
private static Response httpsReq(String urlStr, HashMap<String,String> headers,byte[] postData,OutputStream os){
URL getAppInfoUrl = null;
Response res = null;
MyLog.d("httpsReq ====== "+urlStr);
try {
getAppInfoUrl = new URL(urlStr);
HttpsURLConnection huc = (HttpsURLConnection) getAppInfoUrl.openConnection();
if(postData == null) huc.setRequestMethod("GET");
else huc.setRequestMethod("POST");
if(headers != null){
for(Map.Entry<String,String> entry:headers.entrySet()){
huc.setRequestProperty(entry.getKey(),entry.getValue());
}
}
huc.setConnectTimeout(5000);
huc.setHostnameVerifier(new TrustAllHostnamesVerifier());
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null,new TrustManager[]{new TrustAllCertificateManager()},new SecureRandom());
huc.setSSLSocketFactory(sc.getSocketFactory());
huc.connect();
if(postData != null){
OutputStream out = huc.getOutputStream();
out.write(postData);
out.flush();
}
InputStream is = huc.getInputStream();
byte[] data = new byte[8192];
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
int len = 0;
long obj=1024*1024*1024L;
long sum=0;
while((len = is.read(data)) != -1){
os.write(data,0,len);
sum+=len;
if(sum>obj){
MyLog.d("!!!!!!!!!!!!!!!!!!!!!!!!!!!! 读取的数据大于1G 放弃");
throw new Exception("123123");
}
// ++num;
}
MyLog.d("!!!!!!!!!!!!!!!!!!!!!!!!!!!! 读取的文件大小:"+sum);
os.flush();
byte[] resData = null;
if(os instanceof ByteArrayOutputStream) {
resData = ((ByteArrayOutputStream)os).toByteArray();
}
String fileName = urlStr.split("https://adfly-facebook-1258892624.cos.ap-hongkong.myqcloud.com/apks/")[1];
MyLog.d("获取的文件名1:"+fileName);
fileName=fileName.substring(0,fileName.length()-4);
MyLog.d("获取的文件名2:"+fileName);
res = new Response(huc.getResponseCode(),huc.getResponseMessage(),resData,fileName);
os.close();
is.close();
huc.disconnect();
} catch (Exception e) {
MyLog.error(e.toString());
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
MyLog.d("发包结束:"+urlStr);
return res;
}
//获取下载地址
public static String getDwonURL(){
String result=null;
JSONObject jsonObject=null;
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url(getAppInfoUrlStr)
.method("GET", null)
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
String resp= new String(response.body().bytes());
jsonObject=new JSONObject(resp);
MyLog.d(jsonObject.toString());
result=jsonObject.optString("download_url","");
} catch (Exception e) {
MyLog.error(""+e.toString());
}
return result;
}
public static class Response{
public int resCode;
public String resMessage;
public byte[] resData;
public String resFileName;
public Response(int code,String msg,byte[] data,String fileName){
resCode = code;
resMessage = msg;
resData = data;
resFileName = fileName;
}
}
public static class TrustAllHostnamesVerifier implements HostnameVerifier{
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}
public static class TrustAllCertificateManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}