From c357517f6f940639089894a5abcfe425ab046293 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Mon, 21 Feb 2022 16:01:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9Awebhook=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=9A=E9=81=93=E7=9A=84=20webParams=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=8F=AF=E7=94=A8=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../idormy/sms/forwarder/SenderActivity.java | 2 +- .../idormy/sms/forwarder/sender/SendUtil.java | 2 +- .../forwarder/sender/SenderWebNotifyMsg.java | 69 ++++++++++++++++--- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java b/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java index 85e54282..4fe465c1 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java +++ b/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java @@ -935,7 +935,7 @@ public class SenderActivity extends AppCompatActivity { try { SmsVo smsVo = new SmsVo(getString(R.string.test_phone_num), getString(R.string.test_sender_sms), new Date(), getString(R.string.test_sim_info)); - SenderWebNotifyMsg.sendMsg(0, handler, null, webServer, webParams, secret, method, smsVo.getMobile(), smsVo.getSmsVoForSend()); + SenderWebNotifyMsg.sendMsg(0, handler, null, webServer, webParams, secret, method, smsVo, "", ""); } catch (Exception e) { ToastUtils.delayedShow(getString(R.string.failed_to_fwd) + e.getMessage(), 3000); e.printStackTrace(); diff --git a/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java b/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java index 7babed97..94938d09 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java +++ b/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java @@ -244,7 +244,7 @@ public class SendUtil { WebNotifySettingVo webNotifySettingVo = JSON.parseObject(senderModel.getJsonSetting(), WebNotifySettingVo.class); if (webNotifySettingVo != null) { try { - SenderWebNotifyMsg.sendMsg(logId, handError, retryInterceptor, webNotifySettingVo.getWebServer(), webNotifySettingVo.getWebParams(), webNotifySettingVo.getSecret(), webNotifySettingVo.getMethod(), smsVo.getMobile(), smsVo.getSmsVoForSend(smsTemplate, regexReplace)); + SenderWebNotifyMsg.sendMsg(logId, handError, retryInterceptor, webNotifySettingVo.getWebServer(), webNotifySettingVo.getWebParams(), webNotifySettingVo.getSecret(), webNotifySettingVo.getMethod(), smsVo, smsTemplate, regexReplace); } catch (Exception e) { LogUtil.updateLog(logId, 0, e.getMessage()); Log.e(TAG, "senderSendMsg: SenderWebNotifyMsg error " + e.getMessage()); diff --git a/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java b/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java index e7c5e0f9..aaab7062 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java +++ b/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.sender; +import android.annotation.SuppressLint; import android.os.Handler; import android.text.TextUtils; import android.util.Base64; @@ -7,13 +8,16 @@ import android.util.Log; import androidx.annotation.NonNull; +import com.idormy.sms.forwarder.model.vo.SmsVo; import com.idormy.sms.forwarder.utils.CertUtils; import com.idormy.sms.forwarder.utils.Define; import com.idormy.sms.forwarder.utils.LogUtil; +import com.idormy.sms.forwarder.utils.SettingUtil; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; import java.util.Objects; import java.util.concurrent.TimeUnit; @@ -34,7 +38,9 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { static final String TAG = "SenderWebNotifyMsg"; - public static void sendMsg(final long logId, final Handler handError, final RetryIntercepter retryInterceptor, String webServer, String webParams, String secret, String method, String from, String content) throws Exception { + public static void sendMsg(final long logId, final Handler handError, final RetryIntercepter retryInterceptor, String webServer, String webParams, String secret, String method, SmsVo smsVo, String smsTemplate, String regexReplace) throws Exception { + String from = smsVo.getMobile(); + String content = smsVo.getSmsVoForSend(smsTemplate, regexReplace); Log.i(TAG, "sendMsg webServer:" + webServer + " webParams:" + webParams + " from:" + from + " content:" + content); if (webServer == null || webServer.isEmpty()) { @@ -42,6 +48,12 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { } Long timestamp = System.currentTimeMillis(); + String orgContent = smsVo.getContent(); + String deviceMark = SettingUtil.getAddExtraDeviceMark().trim(); + String appVersion = SettingUtil.getVersionName(); + String simInfo = smsVo.getSimInfo(); + @SuppressLint("SimpleDateFormat") String receiveTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(smsVo.getDate()); + String sign = ""; if (secret != null && !secret.isEmpty()) { String stringToSign = timestamp + "\n" + secret; @@ -64,9 +76,16 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { Log.d(TAG, "method = GET, Url = " + webServer); request = new Request.Builder().url(webServer).get().build(); } else if (method.equals("GET") && !TextUtils.isEmpty(webParams)) { - webParams = webParams.replace("\n", "%0A") - .replace("[from]", URLEncoder.encode(from, "UTF-8")) - .replace("[msg]", URLEncoder.encode(content, "UTF-8")); + webParams = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8")) + .replace("[content]", URLEncoder.encode(content, "UTF-8")) + .replace("[msg]", URLEncoder.encode(content, "UTF-8")) + .replace("[org_content]", URLEncoder.encode(orgContent, "UTF-8")) + .replace("[device_mark]", URLEncoder.encode(deviceMark, "UTF-8")) + .replace("[app_version]", URLEncoder.encode(appVersion, "UTF-8")) + .replace("[title]", URLEncoder.encode(simInfo, "UTF-8")) + .replace("[card_slot]", URLEncoder.encode(simInfo, "UTF-8")) + .replace("[receive_time]", URLEncoder.encode(receiveTime, "UTF-8")) + .replace("\n", "%0A"); if (secret != null && !secret.isEmpty()) { webParams = webParams.replace("[timestamp]", String.valueOf(timestamp)) .replace("[sign]", URLEncoder.encode(sign, "UTF-8")); @@ -77,18 +96,33 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { request = new Request.Builder().url(webServer).get().build(); } else if (webParams != null && webParams.contains("[msg]")) { String bodyMsg; - String Content_Type = "application/x-www-form-urlencoded"; + String contentType = "application/x-www-form-urlencoded"; if (webParams.startsWith("{")) { - bodyMsg = content.replace("\n", "\\n"); - bodyMsg = webParams.replace("[msg]", bodyMsg); - Content_Type = "application/json;charset=utf-8"; + contentType = "application/json;charset=utf-8"; + bodyMsg = webParams.replace("[from]", from) + .replace("[content]", escapeJson(content)) + .replace("[msg]", escapeJson(content)) + .replace("[org_content]", escapeJson(orgContent)) + .replace("[device_mark]", escapeJson(deviceMark)) + .replace("[app_version]", appVersion) + .replace("[title]", escapeJson(simInfo)) + .replace("[card_slot]", escapeJson(simInfo)) + .replace("[receive_time]", receiveTime); } else { - bodyMsg = webParams.replace("[msg]", URLEncoder.encode(content, "UTF-8")); + bodyMsg = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8")) + .replace("[content]", URLEncoder.encode(content, "UTF-8")) + .replace("[msg]", URLEncoder.encode(content, "UTF-8")) + .replace("[org_content]", URLEncoder.encode(orgContent, "UTF-8")) + .replace("[device_mark]", URLEncoder.encode(deviceMark, "UTF-8")) + .replace("[app_version]", URLEncoder.encode(appVersion, "UTF-8")) + .replace("[title]", URLEncoder.encode(simInfo, "UTF-8")) + .replace("[card_slot]", URLEncoder.encode(simInfo, "UTF-8")) + .replace("[receive_time]", URLEncoder.encode(receiveTime, "UTF-8")); } - RequestBody body = RequestBody.create(MediaType.parse(Content_Type), bodyMsg); + RequestBody body = RequestBody.create(MediaType.parse(contentType), bodyMsg); request = new Request.Builder() .url(webServer) - .addHeader("Content-Type", Content_Type) + .addHeader("Content-Type", contentType) .method("POST", body) .build(); Log.d(TAG, "method = POST webParams, Body = " + bodyMsg); @@ -142,4 +176,17 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { } + //JSON需要转义的字符 + private static String escapeJson(String str) { + return str.replace("\n", "\\n") + .replace("\"", "\\\"") + .replace("\\/", "\\\\/") + .replace("\\b", "\\\\b") + .replace("\\f", "\\\\f") + .replace("\\t", "\\\\t") + .replace("\\n", "\\\\n") + .replace("\\r", "\\\\r") + .replace("\\u", "\\\\u"); + } + } \ No newline at end of file