From: snickersun Date: Sun, 1 Mar 2020 16:33:50 +0000 (+0800) Subject: 1、DnsUtil中加入500毫秒延迟,以便增加拿到地址的成功率。 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=cf291a055e0c1eddce5ec0670a591e2bdcb55123;p=icsupport.git 1、DnsUtil中加入500毫秒延迟,以便增加拿到地址的成功率。 2、把ftp协议上传log为http协议上传日志,解决电信卡无法上传日志的问题(ftp协议端口网路中似乎受限,移动卡都可以,就是电信卡不行)。 3、编译版本365 --- diff --git a/app/app.iml b/app/app.iml index 1a534bf2..51cd3ff0 100644 --- a/app/app.iml +++ b/app/app.iml @@ -103,8 +103,10 @@ + + diff --git a/app/src/main/java/com/enqualcomm/support/Config.java b/app/src/main/java/com/enqualcomm/support/Config.java index fa277157..d31186bf 100644 --- a/app/src/main/java/com/enqualcomm/support/Config.java +++ b/app/src/main/java/com/enqualcomm/support/Config.java @@ -285,6 +285,7 @@ public class Config { return json; } + public final static boolean upload_logs_via_ftp=false; final static String KeyProtocolServerIP="ProtocolServerIP"; final static String KeyProtocolServerHOST="ProtocolServerHOST"; final static String KeyProtocolServerPort="ProtoclServerPort";; diff --git a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java index 2244f4b6..9ac8e128 100644 --- a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java +++ b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java @@ -19,6 +19,7 @@ import com.enqualcomm.support.bean.Runtime; import com.enqualcomm.support.service.Deliver; import com.enqualcomm.support.utils.APNUtil; import com.enqualcomm.support.utils.DeviceInfoUtil; +import com.enqualcomm.support.utils.ICHttpUtil; import com.enqualcomm.support.utils.ICLogger; import com.enqualcomm.support.utils.ServiceUtil; import com.enqualcomm.support.utils.TimeUtil; @@ -36,6 +37,7 @@ import chestnut.utils.PhoneUtils; import rx.Observable; import rx.functions.Action1; +import static com.enqualcomm.support.Config.upload_logs_via_ftp; import static com.enqualcomm.support.Constants.DIALOG_ACTION; import static com.enqualcomm.support.utils.DeviceInfoUtil.getDeviceVersion; @@ -370,6 +372,7 @@ public class ICSmsManager { Observable.timer(500L, TimeUnit.MILLISECONDS).subscribe(new Action1() { public void call(Long param1Long) { sendSMS(phonenumber, "start Upload allLogfile [crashlog APILog log GPS LocationAlarm Battery] to " + Config.getFtpServer(mContext)); + String logtime=TimeUtil.getSystemTime3(); for (String file : filelist) { if (Util.LogfileExist(runtime, file)) { @@ -377,19 +380,30 @@ public class ICSmsManager { String filepath = Util.getLogfilePath(runtime, file); URL serverurl = null; try { - serverurl = new URL(Util.getServerLogfilePath(Config.getFtpServer(mContext), time, file)); + serverurl = new URL(Util.getServerLogfilePath(Config.getFtpServer(mContext), time, file,runtime)); } catch (MalformedURLException e) { e.printStackTrace(); } - if (ftpUtils.upload(serverurl, Config.getFtpUser(mContext), Config.getFtpPWD(mContext), new File(filepath))) { - sb.append(" ").append(file); - ICLogger.i("Upload file "+file+" success! "); + if (upload_logs_via_ftp){ + if (ftpUtils.upload(serverurl, Config.getFtpUser(mContext), Config.getFtpPWD(mContext), new File(filepath))) { + sb.append(" ").append(file); + ICLogger.i("Upload file "+file+" success! "); + }else{ + ICLogger.i("Upload file "+file+" failed! "); + + } }else{ - ICLogger.i("Upload file "+file+" failed! "); + if (ICHttpUtil.uploadFile(filepath,runtime,logtime)) { + sb.append(" ").append(file); + ICLogger.i("Upload file "+file+" success! "); + }else{ + ICLogger.i("Upload file "+file+" failed! "); + } } + } } if (sb.length()>0) { @@ -406,17 +420,27 @@ public class ICSmsManager { String filepath = Util.getLogfilePath(runtime, logfile); URL serverurl = null; try { - serverurl = new URL(Util.getServerLogfilePath(Config.getFtpServer(mContext), TimeUtil.getSystemTime3(), logfile)); + serverurl = new URL(Util.getServerLogfilePath(Config.getFtpServer(mContext), TimeUtil.getSystemTime3(), logfile,runtime)); } catch (MalformedURLException e) { e.printStackTrace(); } if (Util.LogfileExist(runtime, logfile)) { sendSMS(phonenumber, "start Upload " + logfile + " to " + Config.getFtpServer(mContext) +" ..."); - if( ftpUtils.upload(serverurl, Config.getFtpUser(mContext), Config.getFtpPWD(mContext), new File(filepath))) { - sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) +" success!"); + String logtime=TimeUtil.getSystemTime3(); + + if (upload_logs_via_ftp) { + if (ftpUtils.upload(serverurl, Config.getFtpUser(mContext), Config.getFtpPWD(mContext), new File(filepath))) { + sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) + " success!"); + } else { + sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) + " failed!"); + } }else{ - sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) +" failed!"); + if (ICHttpUtil.uploadFile(filepath,runtime,logtime)) { + sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) + " success!"); + } else { + sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) + " failed!"); + } } } else { sendSMS(phonenumber, logfile + " not exist !"); diff --git a/app/src/main/java/com/enqualcomm/support/utils/DnsUtil.java b/app/src/main/java/com/enqualcomm/support/utils/DnsUtil.java index bd01b615..20e6db97 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/DnsUtil.java +++ b/app/src/main/java/com/enqualcomm/support/utils/DnsUtil.java @@ -185,6 +185,11 @@ public class DnsUtil { }; runTask(temp, call); String ip; + try { + Thread.sleep(500L); + } catch (InterruptedException e) { + e.printStackTrace(); + } // ICLogger.i(temp.toString()); if (temp.domainIp!=null){ if (temp.hostIp!=null){ diff --git a/app/src/main/java/com/enqualcomm/support/utils/ICHttpUtil.java b/app/src/main/java/com/enqualcomm/support/utils/ICHttpUtil.java new file mode 100644 index 00000000..303d361e --- /dev/null +++ b/app/src/main/java/com/enqualcomm/support/utils/ICHttpUtil.java @@ -0,0 +1,115 @@ +package com.enqualcomm.support.utils; + +import android.content.Context; +import android.util.Log; + +import com.enqualcomm.support.Config; +import com.sina.util.dnscache.net.ApacheHttpClientNetworkRequests; +import com.enqualcomm.support.bean.Runtime; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.zip.CRC32; +import java.util.zip.CheckedOutputStream; +import java.util.zip.ZipOutputStream; + +import static android.content.ContentValues.TAG; + +public class ICHttpUtil { + public static boolean uploadFile(String filenames, Runtime runtime,String logtime) { + + try { + if (Util.zipFile(new File(filenames), runtime.getCacheDir().getAbsolutePath(), + new ZipOutputStream(new CheckedOutputStream(new FileOutputStream(filenames+".zip"),new CRC32())) + , filenames)) { + filenames += ".zip"; + } + }catch (Exception e){ + e.printStackTrace(); + } + String server= Config.getFotaServerIP(runtime.getContextProxy().context); + int port= Config.getFotaServerPort(runtime.getContextProxy().context); + String product=DeviceInfoUtil.getModule(); + String pkgver=DeviceInfoUtil.getSWVersionDisplayName(); + String imei=runtime.IMEI(); + String actionUrl = "http://"+server+":"+port + "/UploadFile.jsp?product=" + product + "&pkgver="+ pkgver + "&imei="+ imei+"&logtime="+logtime; + return upLoadFile(actionUrl, new File(filenames)); + + } + public static boolean upLoadFile(String url, File file) { + boolean result = false; + try { + if (null == url || null == file || !file.exists() || file.length() < 1) { + return false; + } + /** + * 第一部分 + */ + URL urlObj = new URL(url); + HttpURLConnection con = (HttpURLConnection) urlObj.openConnection(); + + /** + * 设置关键值 + */ + con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式 + con.setDoInput(true); + con.setDoOutput(true); + con.setUseCaches(false); // post方式不能使用缓存 + + // 设置请求头信息 + con.setRequestProperty("Connection", "Keep-Alive"); + con.setRequestProperty("Charset", "UTF-8"); + + // 设置边界 + String BOUNDARY = "----------" + System.currentTimeMillis(); + con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); + + // 请求正文信息 + + // 第一部分: + StringBuilder sb = new StringBuilder(); + sb.append("--"); // ////////必须多两道线 + sb.append(BOUNDARY); + sb.append("\r\n"); + sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n"); + sb.append("Content-Type:application/octet-stream\r\n\r\n"); + + byte[] head = sb.toString().getBytes("utf-8"); + + // 获得输出流 + OutputStream out = new DataOutputStream(con.getOutputStream()); + out.write(head); + + // 文件正文部分 + DataInputStream in = new DataInputStream(new FileInputStream(file)); + int bytes = 0; + byte[] bufferOut = new byte[1024]; + while ((bytes = in.read(bufferOut)) != -1) { + out.write(bufferOut, 0, bytes); + } + in.close(); + + // 结尾部分 + byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线 + out.write(foot); + out.flush(); + out.close(); + + int statusCode = con.getResponseCode(); + String msg=con.getResponseMessage(); + Log.e(TAG,msg); + if (statusCode == 200) { + result = true; + } + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } +} diff --git a/app/src/main/java/com/enqualcomm/support/utils/Util.java b/app/src/main/java/com/enqualcomm/support/utils/Util.java index 6783d340..e0ba83b2 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/Util.java +++ b/app/src/main/java/com/enqualcomm/support/utils/Util.java @@ -12,19 +12,30 @@ import android.net.wifi.WifiManager; import android.os.Build; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; 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.lang.reflect.Method; import java.net.InetAddress; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.Enumeration; import java.util.Iterator; +import java.util.List; import java.util.UUID; +import java.util.zip.CRC32; +import java.util.zip.CheckedOutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; import okio.BufferedSink; import okio.Okio; @@ -37,6 +48,10 @@ import com.enqualcomm.support.bean.voicePacket; import static android.content.Context.CONNECTIVITY_SERVICE; import static android.content.Context.WIFI_SERVICE; +import static chestnut.utils.FileUtils.closeIO; +import static chestnut.utils.FileUtils.createOrExistsDir; +import static chestnut.utils.FileUtils.createOrExistsFile; +import static chestnut.utils.StringUtils.isSpace; public class Util { public static Charset UTF_16 = Charset.forName("UTF-16LE"); @@ -377,9 +392,137 @@ public class Util { return file.getAbsolutePath(); } - public static String getServerLogfilePath(String serverhost,String timefolder,String logfilename) { + + private static int BUFFER_LEN=1024; + /** + * 压缩文件 + * @param srcFile 待压缩的源文件 + * @param rootPath 源文件的根路径 + * @param zos Zip输出流 + * @param comment 备注 + * @return 压缩成功返回true + * @throws IOException + */ + public static boolean zipFile(final File srcFile, String rootPath ,final ZipOutputStream zos ,final String comment) throws IOException { + rootPath = rootPath + (isSpace(rootPath) ? "" : File.separator) + srcFile.getName(); + + if (srcFile.isDirectory()) { + File[] fileList = srcFile.listFiles(); + if (fileList == null || fileList.length <= 0) { + ZipEntry entry = new ZipEntry(rootPath + '/'); + entry.setComment(comment); + zos.putNextEntry(entry); + zos.closeEntry(); + } else { + for (File file : fileList) { + if (!zipFile(file, rootPath, zos, comment)) return false; + } + } + } else { + InputStream is = null; + try { + is = new BufferedInputStream(new FileInputStream(srcFile)); + ZipEntry entry = new ZipEntry(rootPath); + entry.setComment(comment); + zos.putNextEntry(entry); + byte buffer[] = new byte[BUFFER_LEN]; + int len; + while ((len = is.read(buffer, 0, BUFFER_LEN)) != -1) { + zos.write(buffer, 0, len); + } + zos.closeEntry(); + } finally { + if(is!=null) + closeIO(is); + is=null; + } + } + return true; + } + /** + * Unzip the file by keyword. + * + * @param zipFile The ZIP file. + * @param destDir The destination directory. + * @param keyword The keyboard. + * @return the unzipped files + * @throws IOException if unzip unsuccessfully + */ + public static List unzipFileByKeyword(final File zipFile,final File destDir,final String keyword) throws IOException { + if (zipFile == null || destDir == null) return null; + List files = new ArrayList<>(); + ZipFile zip = new ZipFile(zipFile); + Enumeration entries = zip.entries(); + try { + if (isSpace(keyword)) { + while (entries.hasMoreElements()) { + ZipEntry entry = ((ZipEntry) entries.nextElement()); + String entryName = entry.getName(); + if (entryName.contains("../")) { + ICLogger.log("ZipUtils", "it's dangerous!");//防止被利用漏洞恶意修改文件 + return files; + } + if (!unzipChildFile(destDir, files, zip, entry)) return files; + } + } else { + while (entries.hasMoreElements()) { + ZipEntry entry = ((ZipEntry) entries.nextElement()); + String entryName = entry.getName(); + if (entryName.contains("../")) { + ICLogger.log("ZipUtils", "it's dangerous!"); + return files; + } + if (entryName.contains(keyword)) { + if (!unzipChildFile(destDir, files, zip, entry)) return files; + } + } + } + } finally { + zip.close(); + } + return files; + } + + private static boolean unzipChildFile(final File destDir, final List files, final ZipFile zip, final ZipEntry entry) + throws IOException { + File file = new File(destDir, entry.getName()); + files.add(file); + if (entry.isDirectory()) { + return createOrExistsDir(file);//创建文件夹 + } else { + if (!createOrExistsFile(file)) return false; + InputStream in = null; + OutputStream out = null; + try { + in = new BufferedInputStream(zip.getInputStream(entry)); + out = new BufferedOutputStream(new FileOutputStream(file)); + byte buffer[] = new byte[BUFFER_LEN]; + int len; + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + } finally { + if (in != null) { + in.close(); + } + if (out != null) { + out.close(); + } + } + } + return true; + } + + public static String getServerLogfilePath(String serverhost,String timefolder,String logfilename ,Runtime runtime) { StringBuilder urlbuilder=new StringBuilder(); - urlbuilder.append("ftp://").append(serverhost).append("/") + String srvIP=serverhost; + if (SimSignalUtil.checkNet(runtime.getContextProxy().context) ) { + srvIP=runtime.getDnsUtil().getServerIP_ByDNSCache("http://"+serverhost); + if (null == srvIP ) { + srvIP=serverhost; + } + } + urlbuilder.append("ftp://").append(srvIP).append("/") .append("ftp/log/") .append(DeviceInfoUtil.getModule()).append("/") .append(DeviceInfoUtil.getSWVersionDisplayName()).append("/") diff --git a/app/version_code.properties b/app/version_code.properties index 916a26d5..265e565b 100644 --- a/app/version_code.properties +++ b/app/version_code.properties @@ -1,2 +1,2 @@ -#Sat Feb 29 18:14:15 CST 2020 -VERSION_CODE=364 +#Mon Mar 02 00:29:11 CST 2020 +VERSION_CODE=365 diff --git a/dNSCache/src/main/java/com/sina/util/dnscache/net/ApacheHttpClientNetworkRequests.java b/dNSCache/src/main/java/com/sina/util/dnscache/net/ApacheHttpClientNetworkRequests.java index 9b2d3d5e..6d5b5fdb 100644 --- a/dNSCache/src/main/java/com/sina/util/dnscache/net/ApacheHttpClientNetworkRequests.java +++ b/dNSCache/src/main/java/com/sina/util/dnscache/net/ApacheHttpClientNetworkRequests.java @@ -3,6 +3,8 @@ */ package com.sina.util.dnscache.net; +import android.util.Log; + import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; @@ -18,6 +20,7 @@ import java.net.URI; import java.net.URL; import java.util.HashMap; import java.util.Map.Entry; +import java.util.logging.Logger; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; @@ -38,6 +41,8 @@ import org.apache.http.params.HttpParams; import com.sina.util.dnscache.Tools; +import static android.content.ContentValues.TAG; + /** * * 项目名称: DNSCache 类名称: ApacheHttpClientNetworkRequests @@ -255,6 +260,8 @@ public class ApacheHttpClientNetworkRequests implements INetworkRequests { out.close(); int statusCode = con.getResponseCode(); + String msg=con.getResponseMessage(); + Log.e(TAG,msg); if (statusCode == 200) { result = true; }