From 42e2f036528ea787dc2f0456d3cd06d942f60bc2 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Fri, 24 Jun 2022 23:01:17 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=9A=E9=81=93`Telegram`=E5=90=AF=E7=94=A8Socks5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=94=A8=E6=88=B7=E5=AF=86=E7=A0=81=E9=89=B4?= =?UTF-8?q?=E6=9D=83=20#172?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forwarder/utils/sender/TelegramUtils.kt | 152 +++++++++--------- versions.gradle | 6 +- 2 files changed, 76 insertions(+), 82 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt index 07b3f816ac..885733eb21 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt @@ -1,6 +1,5 @@ package com.idormy.sms.forwarder.utils.sender -import android.os.Looper import android.text.TextUtils import android.util.Log import com.google.gson.Gson @@ -10,15 +9,18 @@ import com.idormy.sms.forwarder.entity.result.TelegramResult import com.idormy.sms.forwarder.entity.setting.TelegramSetting import com.idormy.sms.forwarder.utils.SendUtils import com.idormy.sms.forwarder.utils.SettingUtils -import okhttp3.* -import java.io.IOException -import java.net.InetSocketAddress -import java.net.Proxy -import java.net.URLEncoder -import java.util.concurrent.TimeUnit +import com.xuexiang.xhttp2.XHttp +import com.xuexiang.xhttp2.cache.model.CacheMode +import com.xuexiang.xhttp2.callback.SimpleCallBack +import com.xuexiang.xhttp2.exception.ApiException +import okhttp3.Credentials +import okhttp3.Response +import okhttp3.Route +import java.net.* + @Suppress("PrivatePropertyName", "UNUSED_PARAMETER", "unused") -class TelegramUtils { +class TelegramUtils private constructor() { companion object { private val TAG: String = TelegramUtils::class.java.simpleName @@ -35,52 +37,17 @@ class TelegramUtils { msgInfo.getContentForSend(SettingUtils.smsTemplate.toString()) } - val requestUrl = if (setting.apiToken.startsWith("http")) { + var requestUrl = if (setting.apiToken.startsWith("http")) { setting.apiToken } else { "https://api.telegram.org/bot" + setting.apiToken + "/sendMessage" } Log.i(TAG, "requestUrl:$requestUrl") - val clientBuilder = OkHttpClient.Builder() - //设置代理 - if ((setting.proxyType == Proxy.Type.HTTP || setting.proxyType == Proxy.Type.SOCKS) && !TextUtils.isEmpty(setting.proxyHost) && !TextUtils.isEmpty(setting.proxyPort)) { - //代理服务器的IP和端口号 - clientBuilder.proxy(Proxy(setting.proxyType, setting.proxyPort?.let { InetSocketAddress(setting.proxyHost, it.toInt()) })) - - //代理的鉴权账号密码 - if (setting.proxyAuthenticator == true && (!TextUtils.isEmpty(setting.proxyUsername) || !TextUtils.isEmpty(setting.proxyPassword))) { - clientBuilder.proxyAuthenticator { _: Route?, response: Response -> - //设置代理服务器账号密码 - val credential = Credentials.basic(setting.proxyUsername.toString(), setting.proxyPassword.toString()) - response.request().newBuilder() - .header("Proxy-Authorization", credential) - .build() - } - } - } - - //设置重试拦截器 - val retryTimes: Int = SettingUtils.requestRetryTimes - if (retryTimes > 0) { - val delayTime: Long = SettingUtils.requestDelayTime.toLong() - val retryInterceptor: RetryInterceptor = RetryInterceptor.Builder().executionCount(retryTimes).retryInterval(delayTime).logId(0).build() - clientBuilder.addInterceptor(retryInterceptor) - } - - - //设置读取超时时间 - val client = clientBuilder - .readTimeout(SettingUtils.requestTimeout.toLong(), TimeUnit.SECONDS) - .writeTimeout(SettingUtils.requestTimeout.toLong(), TimeUnit.SECONDS) - .connectTimeout(SettingUtils.requestTimeout.toLong(), TimeUnit.SECONDS) - .build() - - val request: Request - if (setting.method != null && setting.method == "GET") { - request = Request.Builder() - .url(requestUrl + "?chat_id=" + setting.chatId + "&text=" + URLEncoder.encode(content, "UTF-8")) - .build() + val request = if (setting.method != null && setting.method == "GET") { + requestUrl += "?chat_id=" + setting.chatId + "&text=" + URLEncoder.encode(content, "UTF-8") + Log.i(TAG, "requestUrl:$requestUrl") + XHttp.get(requestUrl) } else { val bodyMap: MutableMap = mutableMapOf() bodyMap["chat_id"] = setting.chatId @@ -89,43 +56,70 @@ class TelegramUtils { bodyMap["disable_web_page_preview"] = "true" val requestMsg: String = Gson().toJson(bodyMap) Log.i(TAG, "requestMsg:$requestMsg") - val requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), requestMsg) - request = Request.Builder() - .url(requestUrl) - .addHeader("Content-Type", "application/json; charset=utf-8") - .post(requestBody) - .build() + XHttp.post(requestUrl).upJson(requestMsg) } - client.newCall(request).enqueue(object : Callback { - - override fun onFailure(call: Call, e: IOException) { - e.printStackTrace() - Looper.prepare() - SendUtils.updateLogs(logId, 0, e.message.toString()) - //XToastUtils.error("发送失败:" + e.message) - Looper.loop() - } + //设置代理 + if ((setting.proxyType == Proxy.Type.HTTP || setting.proxyType == Proxy.Type.SOCKS) + && !TextUtils.isEmpty(setting.proxyHost) && !TextUtils.isEmpty(setting.proxyPort) + ) { + //代理服务器的IP和端口号 + request.okproxy(Proxy(setting.proxyType, setting.proxyPort?.let { InetSocketAddress(setting.proxyHost, it.toInt()) })) - @Throws(IOException::class) - override fun onResponse(call: Call, response: Response) { - val responseStr = response.body()?.string() - Log.d(TAG, "Response:" + response.code() + "," + responseStr) - - val resp = Gson().fromJson(responseStr, TelegramResult::class.java) - if (resp.ok == true) { - Looper.prepare() - SendUtils.updateLogs(logId, 2, responseStr.toString()) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) - Looper.loop() + //代理的鉴权账号密码 + if (setting.proxyAuthenticator == true + && (!TextUtils.isEmpty(setting.proxyUsername) || !TextUtils.isEmpty(setting.proxyPassword)) + ) { + Log.i(TAG, "proxyUsername = ${setting.proxyUsername}, proxyPassword = ${setting.proxyPassword}") + + if (setting.proxyType == Proxy.Type.HTTP) { + request.okproxyAuthenticator { _: Route?, response: Response -> + //设置代理服务器账号密码 + val credential = Credentials.basic(setting.proxyUsername.toString(), setting.proxyPassword.toString()) + response.request().newBuilder() + .header("Proxy-Authorization", credential) + .build() + } } else { - Looper.prepare() - SendUtils.updateLogs(logId, 0, responseStr.toString()) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) - Looper.loop() + Authenticator.setDefault(object : Authenticator() { + override fun getPasswordAuthentication(): PasswordAuthentication { + return PasswordAuthentication(setting.proxyUsername.toString(), setting.proxyPassword?.toCharArray()) + } + }) } } - }) + } + + request.keepJson(true) + .ignoreHttpsCert() + .timeOut((SettingUtils.requestTimeout * 1000).toLong()) //超时时间10s + .cacheMode(CacheMode.NO_CACHE) + .retryCount(SettingUtils.requestRetryTimes) //超时重试的次数 + .retryDelay(SettingUtils.requestDelayTime) //超时重试的延迟时间 + .retryIncreaseDelay(SettingUtils.requestDelayTime) //超时重试叠加延时 + .timeStamp(true) + .execute(object : SimpleCallBack() { + + override fun onError(e: ApiException) { + SendUtils.updateLogs(logId, 0, e.displayMessage) + Log.e(TAG, e.detailMessage) + //XToastUtils.error(e.displayMessage) + } + + override fun onSuccess(response: String) { + Log.i(TAG, response) + + val resp = Gson().fromJson(response, TelegramResult::class.java) + if (resp.ok == true) { + SendUtils.updateLogs(logId, 2, response) + //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) + } else { + SendUtils.updateLogs(logId, 0, response) + //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) + } + } + + }) } diff --git a/versions.gradle b/versions.gradle index 9d3cb25dab..67be586537 100644 --- a/versions.gradle +++ b/versions.gradle @@ -31,7 +31,7 @@ versions.rxbinding = "2.2.0" versions.butterknife = "10.2.3" versions.runner = "1.4.0" versions.gson = "2.9.0" -versions.okhttp3 = "3.12.13" //不可升级,支持 API 19 +versions.okhttp3 = "3.14.9" //不可升级,支持 API 19 versions.leakcanary = "2.8.1" versions.lifecycle = "2.2.0" versions.kotlin = '1.6.21' @@ -42,7 +42,7 @@ versions.xui = "1.2.0" //1.1.9 versions.xupdate = "2.1.1" versions.xaop = "1.1.0" versions.xutil = "2.0.0" -versions.xhttp2 = "2.0.4" +versions.xhttp2 = "2.0.6"//2.0.4 versions.xpage = "3.3.0" versions.xrouter = "1.0.1" @@ -138,7 +138,7 @@ xlibrary.xaop_runtime = "com.github.xuexiangjys.XAOP:xaop-runtime:$versions.xaop xlibrary.xaop_plugin = "com.github.xuexiangjys.XAOP:xaop-plugin:$versions.xaop" xlibrary.xutil_core = "com.github.xuexiangjys.XUtil:xutil-core:$versions.xutil" xlibrary.xutil_sub = "com.github.xuexiangjys.XUtil:xutil-sub:$versions.xutil" -xlibrary.xhttp2 = "com.github.xuexiangjys:XHttp2:$versions.xhttp2" +xlibrary.xhttp2 = "com.github.pppscn:XHttp2:$versions.xhttp2" //com.github.xuexiangjys:XHttp2 xlibrary.xpage_lib = "com.github.xuexiangjys.XPage:xpage-lib:$versions.xpage" xlibrary.xpage_compiler = "com.github.xuexiangjys.XPage:xpage-compiler:$versions.xpage" xlibrary.xrouter_runtime = "com.github.xuexiangjys.XRouter:xrouter-runtime:$versions.xrouter" From 439877c67414df2eb5c6eec6f714d1f32992ff9a Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Sun, 26 Jun 2022 13:29:13 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=BF=9C?= =?UTF-8?q?=E7=A8=8B=E6=9F=A5=E9=85=8D=E7=BD=AE=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8D=A1=E6=A7=BD=E4=BF=A1=E6=81=AF=E4=B8=8E=E5=A4=87?= =?UTF-8?q?=E6=B3=A8=20#174?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/idormy/sms/forwarder/entity/SimInfo.kt | 11 +++++++++-- .../forwarder/server/controller/ConfigController.kt | 13 +++++++++++++ .../idormy/sms/forwarder/server/model/ConfigData.kt | 9 +++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/entity/SimInfo.kt b/app/src/main/java/com/idormy/sms/forwarder/entity/SimInfo.kt index e34b177c71..344ccba0a1 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/entity/SimInfo.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/entity/SimInfo.kt @@ -1,20 +1,27 @@ package com.idormy.sms.forwarder.entity +import com.google.gson.annotations.SerializedName import java.io.Serializable //SIM卡信息 data class SimInfo( //运营商信息:中国移动 中国联通 中国电信 + @SerializedName("carrier_name") var mCarrierName: CharSequence? = null, - //卡槽ID,SimSerialNumber + //集成电路卡识别码即SIM卡卡号 + @SerializedName("icc_id") var mIccId: CharSequence? = null, //卡槽id:-1=没插入、 0=卡槽1 、1=卡槽2 + @SerializedName("sim_slot_index") var mSimSlotIndex: Int = 0, //号码 + @SerializedName("number") var mNumber: CharSequence? = null, - //城市 + //国家代码 + @SerializedName("country_iso") var mCountryIso: CharSequence? = null, //SIM的 Subscription Id (SIM插入顺序) + @SerializedName("subscription_id") var mSubscriptionId: Int = 0, ) : Serializable { override fun toString(): String { diff --git a/app/src/main/java/com/idormy/sms/forwarder/server/controller/ConfigController.kt b/app/src/main/java/com/idormy/sms/forwarder/server/controller/ConfigController.kt index 6c88396c66..938aecfce8 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/server/controller/ConfigController.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/server/controller/ConfigController.kt @@ -1,9 +1,12 @@ package com.idormy.sms.forwarder.server.controller import android.util.Log +import com.idormy.sms.forwarder.App import com.idormy.sms.forwarder.server.model.BaseRequest import com.idormy.sms.forwarder.server.model.ConfigData import com.idormy.sms.forwarder.utils.HttpServerUtils +import com.idormy.sms.forwarder.utils.PhoneUtils +import com.idormy.sms.forwarder.utils.SettingUtils import com.yanzhenjie.andserver.annotation.* @Suppress("PrivatePropertyName") @@ -19,6 +22,12 @@ class ConfigController { fun test(@RequestBody bean: BaseRequest<*>): ConfigData { Log.d(TAG, bean.data.toString()) + //获取卡槽信息 + if (App.SimInfoList.isEmpty()) { + App.SimInfoList = PhoneUtils.getSimMultiInfo() + } + Log.d(TAG, App.SimInfoList.toString()) + return ConfigData( HttpServerUtils.enableApiClone, HttpServerUtils.enableApiSmsSend, @@ -26,6 +35,10 @@ class ConfigController { HttpServerUtils.enableApiCallQuery, HttpServerUtils.enableApiContactQuery, HttpServerUtils.enableApiBatteryQuery, + SettingUtils.extraDeviceMark.toString(), + SettingUtils.extraSim1.toString(), + SettingUtils.extraSim2.toString(), + App.SimInfoList ) } diff --git a/app/src/main/java/com/idormy/sms/forwarder/server/model/ConfigData.kt b/app/src/main/java/com/idormy/sms/forwarder/server/model/ConfigData.kt index 935f9e1aa0..e560581a49 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/server/model/ConfigData.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/server/model/ConfigData.kt @@ -1,6 +1,7 @@ package com.idormy.sms.forwarder.server.model import com.google.gson.annotations.SerializedName +import com.idormy.sms.forwarder.entity.SimInfo import java.io.Serializable data class ConfigData( @@ -16,4 +17,12 @@ data class ConfigData( var enableApiContactQuery: Boolean = false, @SerializedName("enable_api_battery_query") var enableApiBatteryQuery: Boolean = false, + @SerializedName("extra_device_mark") + var extraDeviceMark: String = "", + @SerializedName("extra_sim1") + var extraSim1: String = "", + @SerializedName("extra_sim2") + var extraSim2: String = "", + @SerializedName("sim_info_list") + var simInfoList: MutableMap = mutableMapOf(), ) : Serializable \ No newline at end of file From ea94e30347e9b8428861968dca9f60aaf2add681 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Mon, 27 Jun 2022 17:10:54 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=9A=E9=81=93`Telegram`=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E5=90=8D=E6=94=AF=E6=8C=81=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=20#172?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fragment/senders/TelegramFragment.kt | 18 ++++++++++++++---- .../forwarder/utils/sender/TelegramUtils.kt | 11 ++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/TelegramFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/TelegramFragment.kt index 19ffbf0f86..f22974c29c 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/TelegramFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/TelegramFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -164,10 +165,19 @@ class TelegramFragment : BaseFragment(), View.O when (v.id) { R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - TelegramUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + TelegramUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt index 885733eb21..4ad76dc7c2 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt @@ -13,6 +13,7 @@ import com.xuexiang.xhttp2.XHttp import com.xuexiang.xhttp2.cache.model.CacheMode import com.xuexiang.xhttp2.callback.SimpleCallBack import com.xuexiang.xhttp2.exception.ApiException +import com.xuexiang.xutil.net.NetworkUtils import okhttp3.Credentials import okhttp3.Response import okhttp3.Route @@ -64,7 +65,15 @@ class TelegramUtils private constructor() { && !TextUtils.isEmpty(setting.proxyHost) && !TextUtils.isEmpty(setting.proxyPort) ) { //代理服务器的IP和端口号 - request.okproxy(Proxy(setting.proxyType, setting.proxyPort?.let { InetSocketAddress(setting.proxyHost, it.toInt()) })) + Log.d(TAG, "proxyHost = ${setting.proxyHost}, proxyPort = ${setting.proxyPort}") + val proxyHost = if (NetworkUtils.isIP(setting.proxyHost)) setting.proxyHost else NetworkUtils.getDomainAddress(setting.proxyHost) + if (!NetworkUtils.isIP(proxyHost)) { + throw Exception("代理服务器主机名解析失败:proxyHost=$proxyHost") + } + val proxyPort: Int = setting.proxyPort?.toInt() ?: 7890 + + Log.d(TAG, "proxyHost = $proxyHost, proxyPort = $proxyPort") + request.okproxy(Proxy(setting.proxyType, InetSocketAddress(proxyHost, proxyPort))) //代理的鉴权账号密码 if (setting.proxyAuthenticator == true From b3b51a74cebbbd0b8ef84c0fd335ac3ab505f990 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Mon, 27 Jun 2022 17:12:43 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=8F=91=E9=80=81=E9=80=9A=E9=81=93/=E8=BD=AC?= =?UTF-8?q?=E5=8F=91=E8=A7=84=E5=88=99=E6=97=B6=E5=88=9B=E5=BB=BA=E5=AD=90?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E8=BF=90=E8=A1=8C=20&=20=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forwarder/fragment/RulesEditFragment.kt | 12 +- .../fragment/senders/BarkFragment.kt | 18 ++- .../fragment/senders/DingtalkFragment.kt | 18 ++- .../fragment/senders/EmailFragment.kt | 18 ++- .../fragment/senders/FeishuFragment.kt | 18 ++- .../fragment/senders/GotifyFragment.kt | 18 ++- .../fragment/senders/PushplusFragment.kt | 18 ++- .../fragment/senders/ServerchanFragment.kt | 18 ++- .../forwarder/fragment/senders/SmsFragment.kt | 18 ++- .../fragment/senders/WebhookFragment.kt | 18 ++- .../fragment/senders/WeworkAgentFragment.kt | 18 ++- .../fragment/senders/WeworkRobotFragment.kt | 18 ++- .../idormy/sms/forwarder/utils/SendUtils.kt | 109 ++++++++++-------- .../forwarder/utils/sender/WebhookUtils.kt | 6 +- .../sms/forwarder/workers/SendWorker.kt | 1 + 15 files changed, 228 insertions(+), 98 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/RulesEditFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/RulesEditFragment.kt index 37de0bf3ed..3d4a42b7dc 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/RulesEditFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/RulesEditFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment +import android.os.Looper import android.util.Log import android.view.LayoutInflater import android.view.View @@ -627,7 +628,16 @@ class RulesEditFragment : BaseFragment(), View.OnClic } override fun onSuccess(sender: Sender) { - SendUtils.sendMsgSender(msgInfo, rule, sender, 0L) + Thread { + try { + SendUtils.sendMsgSender(msgInfo, rule, sender, 0L) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() } }) diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/BarkFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/BarkFragment.kt index a0b7fe5cf5..fe27c603fa 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/BarkFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/BarkFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -183,10 +184,19 @@ class BarkFragment : BaseFragment(), View.OnClickLi } R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - BarkUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + BarkUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/DingtalkFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/DingtalkFragment.kt index 31f7f699c2..a0d2b121b9 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/DingtalkFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/DingtalkFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -158,10 +159,19 @@ class DingtalkFragment : BaseFragment(), View.O when (v.id) { R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - DingtalkUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + DingtalkUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/EmailFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/EmailFragment.kt index 64f3873961..cfd79a180b 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/EmailFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/EmailFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -212,10 +213,19 @@ class EmailFragment : BaseFragment(), View.OnClick } R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - EmailUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + EmailUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/FeishuFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/FeishuFragment.kt index b066f3ad4e..d950a09366 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/FeishuFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/FeishuFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -157,10 +158,19 @@ class FeishuFragment : BaseFragment(), View.OnCli } R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - FeishuUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + FeishuUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/GotifyFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/GotifyFragment.kt index 0f2af4a002..76f2f07b59 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/GotifyFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/GotifyFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -156,10 +157,19 @@ class GotifyFragment : BaseFragment(), View.OnCli } R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - GotifyUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + GotifyUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/PushplusFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/PushplusFragment.kt index b6fc62b9fb..40cff2a674 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/PushplusFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/PushplusFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -176,10 +177,19 @@ class PushplusFragment : BaseFragment(), View.O } R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - PushplusUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + PushplusUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/ServerchanFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/ServerchanFragment.kt index 3a96daa522..92217f8318 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/ServerchanFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/ServerchanFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -138,10 +139,19 @@ class ServerchanFragment : BaseFragment(), Vi when (v.id) { R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - ServerchanUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + ServerchanUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/SmsFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/SmsFragment.kt index 741b08dd42..5f6f961328 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/SmsFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/SmsFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -164,10 +165,19 @@ class SmsFragment : BaseFragment(), View.OnClickList when (v.id) { R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - SmsUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + SmsUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WebhookFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WebhookFragment.kt index 66cb35519a..50570314c2 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WebhookFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WebhookFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -150,10 +151,19 @@ class WebhookFragment : BaseFragment(), View.OnC when (v.id) { R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - WebhookUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + WebhookUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkAgentFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkAgentFragment.kt index 2cdc1b61f5..aca5a119ad 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkAgentFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkAgentFragment.kt @@ -1,6 +1,7 @@ package com.idormy.sms.forwarder.fragment.senders import android.annotation.SuppressLint +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -155,10 +156,19 @@ class WeworkAgentFragment : BaseFragment(), when (v.id) { R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - WeworkAgentUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + WeworkAgentUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkRobotFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkRobotFragment.kt index 8b778cfa77..659a5cc1d9 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkRobotFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/senders/WeworkRobotFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment.senders +import android.os.Looper import android.text.TextUtils import android.util.Log import android.view.LayoutInflater @@ -136,10 +137,19 @@ class WeworkRobotFragment : BaseFragment(), when (v.id) { R.id.btn_test -> { mCountDownHelper?.start() - val settingVo = checkSetting() - Log.d(TAG, settingVo.toString()) - val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) - WeworkRobotUtils.sendMsg(settingVo, msgInfo) + Thread { + try { + val settingVo = checkSetting() + Log.d(TAG, settingVo.toString()) + val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info)) + WeworkRobotUtils.sendMsg(settingVo, msgInfo) + } catch (e: Exception) { + e.printStackTrace() + if (Looper.myLooper() == null) Looper.prepare() + XToastUtils.error(e.message.toString()) + Looper.loop() + } + }.start() return } R.id.btn_del -> { diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/SendUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/SendUtils.kt index 010d38f33d..4d60a113cb 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/SendUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/SendUtils.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.utils +import android.os.Looper import android.util.Log import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager @@ -68,68 +69,76 @@ object SendUtils { //匹配发送通道发送消息 fun sendMsgSender(msgInfo: MsgInfo, rule: Rule, sender: Sender, logId: Long) { - when (sender.type) { - TYPE_DINGDING -> { - val settingVo = Gson().fromJson(sender.jsonSetting, DingtalkSetting::class.java) - DingtalkUtils.sendMsg(settingVo, msgInfo, rule, logId) + try { + when (sender.type) { + TYPE_DINGDING -> { + val settingVo = Gson().fromJson(sender.jsonSetting, DingtalkSetting::class.java) + DingtalkUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_EMAIL -> { + val settingVo = Gson().fromJson(sender.jsonSetting, EmailSetting::class.java) + EmailUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_BARK -> { + val settingVo = Gson().fromJson(sender.jsonSetting, BarkSetting::class.java) + BarkUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_WEBHOOK -> { + val settingVo = Gson().fromJson(sender.jsonSetting, WebhookSetting::class.java) + WebhookUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_WEWORK_ROBOT -> { + val settingVo = Gson().fromJson(sender.jsonSetting, WeworkRobotSetting::class.java) + WeworkRobotUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_WEWORK_AGENT -> { + val settingVo = Gson().fromJson(sender.jsonSetting, WeworkAgentSetting::class.java) + WeworkAgentUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_SERVERCHAN -> { + val settingVo = Gson().fromJson(sender.jsonSetting, ServerchanSetting::class.java) + ServerchanUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_TELEGRAM -> { + val settingVo = Gson().fromJson(sender.jsonSetting, TelegramSetting::class.java) + TelegramUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_SMS -> { + val settingVo = Gson().fromJson(sender.jsonSetting, SmsSetting::class.java) + SmsUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_FEISHU -> { + val settingVo = Gson().fromJson(sender.jsonSetting, FeishuSetting::class.java) + FeishuUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_PUSHPLUS -> { + val settingVo = Gson().fromJson(sender.jsonSetting, PushplusSetting::class.java) + PushplusUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + TYPE_GOTIFY -> { + val settingVo = Gson().fromJson(sender.jsonSetting, GotifySetting::class.java) + GotifyUtils.sendMsg(settingVo, msgInfo, rule, logId) + } + else -> {} } - TYPE_EMAIL -> { - val settingVo = Gson().fromJson(sender.jsonSetting, EmailSetting::class.java) - EmailUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_BARK -> { - val settingVo = Gson().fromJson(sender.jsonSetting, BarkSetting::class.java) - BarkUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_WEBHOOK -> { - val settingVo = Gson().fromJson(sender.jsonSetting, WebhookSetting::class.java) - WebhookUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_WEWORK_ROBOT -> { - val settingVo = Gson().fromJson(sender.jsonSetting, WeworkRobotSetting::class.java) - WeworkRobotUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_WEWORK_AGENT -> { - val settingVo = Gson().fromJson(sender.jsonSetting, WeworkAgentSetting::class.java) - WeworkAgentUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_SERVERCHAN -> { - val settingVo = Gson().fromJson(sender.jsonSetting, ServerchanSetting::class.java) - ServerchanUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_TELEGRAM -> { - val settingVo = Gson().fromJson(sender.jsonSetting, TelegramSetting::class.java) - TelegramUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_SMS -> { - val settingVo = Gson().fromJson(sender.jsonSetting, SmsSetting::class.java) - SmsUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_FEISHU -> { - val settingVo = Gson().fromJson(sender.jsonSetting, FeishuSetting::class.java) - FeishuUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_PUSHPLUS -> { - val settingVo = Gson().fromJson(sender.jsonSetting, PushplusSetting::class.java) - PushplusUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - TYPE_GOTIFY -> { - val settingVo = Gson().fromJson(sender.jsonSetting, GotifySetting::class.java) - GotifyUtils.sendMsg(settingVo, msgInfo, rule, logId) - } - else -> {} + } catch (e: Exception) { + e.printStackTrace() + updateLogs(logId, 0, e.message.toString()) } } //更新转发日志状态 fun updateLogs(logId: Long?, status: Int, response: String) { + + //测试的没有记录ID,这里取巧了 if (logId == null || logId == 0L) { - //测试的没有记录ID,这里取巧了 + if (Looper.myLooper() == null) Looper.prepare() if (status == 2) { XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } + Looper.loop() return } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt index 41b117d4a4..764a339a81 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt @@ -165,7 +165,7 @@ class WebhookUtils { client.newCall(requestBuilder.build()).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { //解决在子线程中调用Toast的异常情况处理 - Looper.prepare() + if (Looper.myLooper() == null) Looper.prepare() e.printStackTrace() SendUtils.updateLogs(logId, 0, e.message.toString()) //XToastUtils.error(ResUtils.getString(R.string.request_failed) + e.message) @@ -179,12 +179,12 @@ class WebhookUtils { //返回http状态200即为成功 if (200 == response.code()) { - Looper.prepare() + if (Looper.myLooper() == null) Looper.prepare() SendUtils.updateLogs(logId, 2, responseStr) //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) Looper.loop() } else { - Looper.prepare() + if (Looper.myLooper() == null) Looper.prepare() SendUtils.updateLogs(logId, 0, responseStr) //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) Looper.loop() diff --git a/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt b/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt index 39dacdb5e0..d2bab5ff12 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt @@ -98,6 +98,7 @@ class SendWorker( } catch (e: Exception) { e.printStackTrace() + return@withContext Result.failure(workDataOf("send" to e.message.toString())) } return@withContext Result.success() From 3d2406a44b3ce2f5e6eb146c970a36b7dff3e4be Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Mon, 27 Jun 2022 17:39:22 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E9=82=AE?= =?UTF-8?q?=E7=AE=B1=E5=8F=91=E9=80=81=E9=80=9A=E9=81=93=E6=94=B6=E4=BB=B6?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E4=B8=8D=E6=94=AF=E6=8C=81=E9=80=97=E5=8F=B7?= =?UTF-8?q?=E5=88=86=E9=9A=94Bug=EF=BC=88=E5=B7=B2=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=80=97=E5=8F=B7/=E5=88=86=E5=8F=B7=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/forwarder/utils/sender/EmailUtils.kt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt index 3ace1d17af..acc8948f49 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt @@ -114,14 +114,25 @@ class EmailUtils { else -> {} } - // 创建邮箱 + //收件地址 + val toAddressList = ArrayList() + val emailArray = setting.toEmail.toString().split("[,,;;]".toRegex()) + if (emailArray.isNotEmpty()) { + for (email in emailArray) { + toAddressList.add(email) + } + } else { + toAddressList.add(setting.toEmail.toString()) + } + + //创建邮箱 val mail = Mail().apply { mailServerHost = setting.host.toString() mailServerPort = setting.port.toString() fromAddress = setting.fromEmail.toString() fromNickname = msgInfo.getTitleForSend(setting.nickname.toString()) password = setting.pwd.toString() - toAddress = arrayListOf(setting.toEmail.toString()) + toAddress = toAddressList subject = title content = message openSSL = setting.ssl == true From f142f8d958674339d6ece7030afd1596ede03670 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Thu, 30 Jun 2022 11:02:29 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=9A=E9=81=93`webhook`=E6=94=AF=E6=8C=81HTTP?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E8=AE=A4=E8=AF=81=20=E3=80=90=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=EF=BC=9Ahttp://username:password@domain.com/uri?= =?UTF-8?q?=E3=80=91=20#175=20=E4=BC=98=E5=8C=96=EF=BC=9A=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=9A=E9=81=93`=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=BA=94=E7=94=A8`=E8=8E=B7=E5=8F=96access=5Ftoken?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=97=B6=E8=AE=B0=E5=BD=95=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=97=A5=E5=BF=97=20=E4=BC=98=E5=8C=96=EF=BC=9A=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=9A=E9=81=93`=E7=9F=AD=E4=BF=A1`=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=9D=83=E9=99=90=E6=9C=AA=E6=8E=88=E6=9D=83/?= =?UTF-8?q?=E4=BB=85=E5=BD=93=E6=97=A0=E7=BD=91=E7=BB=9C=E5=90=AF=E7=94=A8?= =?UTF-8?q?=E6=97=B6=E8=AE=B0=E5=BD=95=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../idormy/sms/forwarder/utils/CertUtils.kt | 2 + .../sms/forwarder/utils/sender/BarkUtils.kt | 5 +- .../forwarder/utils/sender/DingtalkUtils.kt | 5 +- .../sms/forwarder/utils/sender/EmailUtils.kt | 4 +- .../sms/forwarder/utils/sender/FeishuUtils.kt | 5 +- .../sms/forwarder/utils/sender/GotifyUtils.kt | 5 +- .../forwarder/utils/sender/PushplusUtils.kt | 5 +- .../utils/sender/RetryInterceptor.kt | 88 -------- .../forwarder/utils/sender/ServerchanUtils.kt | 5 +- .../sms/forwarder/utils/sender/SmsUtils.kt | 13 +- .../forwarder/utils/sender/TelegramUtils.kt | 5 +- .../forwarder/utils/sender/WebhookUtils.kt | 193 ++++++++---------- .../utils/sender/WeworkAgentUtils.kt | 12 +- .../utils/sender/WeworkRobotUtils.kt | 5 +- 14 files changed, 109 insertions(+), 243 deletions(-) delete mode 100644 app/src/main/java/com/idormy/sms/forwarder/utils/sender/RetryInterceptor.kt diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/CertUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/CertUtils.kt index b477859c43..8942218191 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/CertUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/CertUtils.kt @@ -7,8 +7,10 @@ import java.security.cert.X509Certificate import java.util.* import javax.net.ssl.* +@Suppress("unused") @SuppressLint("ALL") object CertUtils { + //获取这个SSLSocketFactory val sSLSocketFactory: SSLSocketFactory get() = try { diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/BarkUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/BarkUtils.kt index 277eb094db..24a5231d5e 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/BarkUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/BarkUtils.kt @@ -84,9 +84,8 @@ class BarkUtils { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -95,10 +94,8 @@ class BarkUtils { val resp = Gson().fromJson(response, BarkResult::class.java) if (resp.code == 200L) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/DingtalkUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/DingtalkUtils.kt index e1e3781790..3e6ed0bfec 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/DingtalkUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/DingtalkUtils.kt @@ -95,9 +95,8 @@ class DingtalkUtils private constructor() { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -106,10 +105,8 @@ class DingtalkUtils private constructor() { val resp = Gson().fromJson(response, DingtalkResult::class.java) if (resp.errcode == 0L) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt index acc8948f49..fa2efb2163 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/EmailUtils.kt @@ -141,14 +141,12 @@ class EmailUtils { MailSender.getInstance().sendMail(mail, object : MailSender.OnMailSendListener { override fun onError(e: Throwable) { - SendUtils.updateLogs(logId, 0, e.message.toString()) Log.e("MailSender", e.message.toString()) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + e.message.toString()) + SendUtils.updateLogs(logId, 0, e.message.toString()) } override fun onSuccess() { SendUtils.updateLogs(logId, 2, ResUtils.getString(R.string.request_succeeded)) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } }) diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/FeishuUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/FeishuUtils.kt index f0fe2e2c0e..52fa0d0d3d 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/FeishuUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/FeishuUtils.kt @@ -143,9 +143,8 @@ class FeishuUtils private constructor() { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -154,10 +153,8 @@ class FeishuUtils private constructor() { val resp = Gson().fromJson(response, FeishuResult::class.java) if (resp.code == 0L) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/GotifyUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/GotifyUtils.kt index e7bb2436ec..3a2f508275 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/GotifyUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/GotifyUtils.kt @@ -62,9 +62,8 @@ class GotifyUtils { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -73,10 +72,8 @@ class GotifyUtils { val resp = Gson().fromJson(response, GotifyResult::class.java) if (resp?.id != null) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/PushplusUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/PushplusUtils.kt index 6449b6d9e6..bd0b82436b 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/PushplusUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/PushplusUtils.kt @@ -79,9 +79,8 @@ class PushplusUtils private constructor() { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -90,10 +89,8 @@ class PushplusUtils private constructor() { val resp = Gson().fromJson(response, PushplusResult::class.java) if (resp.code == 200L) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/RetryInterceptor.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/RetryInterceptor.kt deleted file mode 100644 index 7887f66a1e..0000000000 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/RetryInterceptor.kt +++ /dev/null @@ -1,88 +0,0 @@ -package com.idormy.sms.forwarder.utils.sender - -import android.util.Log -import okhttp3.Interceptor -import okhttp3.Request -import okhttp3.Response -import java.io.IOException -import java.io.InterruptedIOException - -class RetryInterceptor internal constructor(builder: Builder) : Interceptor { - //重试的间隔 - private val retryInterval: Long - - //更新记录ID - private val logId: Long - - //最大重试次数 - private val executionCount: Int - - @Throws(IOException::class) - override fun intercept(chain: Interceptor.Chain): Response { - var retryTimes = 0 - val request = chain.request() - var response: Response - do { - if (retryTimes > 0 && retryInterval > 0) { - val delayTime = retryTimes * retryInterval - try { - Log.w(TAG, "第 $retryTimes 次重试,休眠 $delayTime 秒") - Thread.sleep(delayTime * 1000) - } catch (e: InterruptedException) { - Thread.currentThread().interrupt() - throw InterruptedIOException(e.message) - } - } - response = doRequest(chain, request, retryTimes)!! - retryTimes++ - } while ((!response.isSuccessful) && retryTimes <= executionCount) - - return response - } - - private fun doRequest(chain: Interceptor.Chain, request: Request, retryTimes: Int): Response? { - var response: Response? = null - try { - response = chain.proceed(request) - } catch (e: Exception) { - val resp = if (retryTimes > 0) "第" + retryTimes + "次重试:" + e.message else e.message!! - //LogUtils.updateLog(logId, 1, resp); - Log.w(TAG, resp) - } - return response - } - - class Builder { - var executionCount = 3 - var retryInterval: Long = 1000 - var logId: Long = 0 - fun executionCount(executionCount: Int): Builder { - this.executionCount = executionCount - return this - } - - fun retryInterval(retryInterval: Long): Builder { - this.retryInterval = retryInterval - return this - } - - fun logId(logId: Long): Builder { - this.logId = logId - return this - } - - fun build(): RetryInterceptor { - return RetryInterceptor(this) - } - } - - companion object { - const val TAG = "RetryInterceptor" - } - - init { - executionCount = builder.executionCount - retryInterval = builder.retryInterval - logId = builder.logId - } -} \ No newline at end of file diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/ServerchanUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/ServerchanUtils.kt index eb84a1ab78..058bdeb2f0 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/ServerchanUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/ServerchanUtils.kt @@ -57,9 +57,8 @@ class ServerchanUtils { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -68,10 +67,8 @@ class ServerchanUtils { val resp = Gson().fromJson(response, ServerchanResult::class.java) if (resp?.code == 0L) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/SmsUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/SmsUtils.kt index af17a8f9d6..7d53ac75a1 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/SmsUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/SmsUtils.kt @@ -30,6 +30,12 @@ class SmsUtils { ) { //仅当无网络时启用 && 判断是否真实有网络 if (setting.onlyNoNetwork == true && NetworkUtils.isHaveInternet() && NetworkUtils.isAvailableByPing()) { + SendUtils.updateLogs(logId, 0, ResUtils.getString(R.string.OnlyNoNetwork)) + return + } + + if (ActivityCompat.checkSelfPermission(XUtil.getContext(), Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { + SendUtils.updateLogs(logId, 0, ResUtils.getString(R.string.no_sms_sending_permission)) return } @@ -50,18 +56,11 @@ class SmsUtils { //TODO:取不到卡槽信息时,采用默认卡槽发送 val mSubscriptionId: Int = App.SimInfoList[simSlotIndex]?.mSubscriptionId ?: -1 - - if (ActivityCompat.checkSelfPermission(XUtil.getContext(), Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { - //XToastUtils.error(ResUtils.getString(R.string.no_sms_sending_permission)) - return - } val res: String? = PhoneUtils.sendSms(mSubscriptionId, setting.mobiles, content) if (res == null) { SendUtils.updateLogs(logId, 2, ResUtils.getString(R.string.request_succeeded)) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, res) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + res) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt index 4ad76dc7c2..4cdede0c4d 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/TelegramUtils.kt @@ -110,9 +110,8 @@ class TelegramUtils private constructor() { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -121,10 +120,8 @@ class TelegramUtils private constructor() { val resp = Gson().fromJson(response, TelegramResult::class.java) if (resp.ok == true) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt index 764a339a81..0d7cabcf92 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt @@ -1,7 +1,6 @@ package com.idormy.sms.forwarder.utils.sender import android.annotation.SuppressLint -import android.os.Looper import android.text.TextUtils import android.util.Base64 import android.util.Log @@ -9,17 +8,17 @@ import com.google.gson.Gson import com.idormy.sms.forwarder.database.entity.Rule import com.idormy.sms.forwarder.entity.MsgInfo import com.idormy.sms.forwarder.entity.setting.WebhookSetting -import com.idormy.sms.forwarder.utils.CertUtils import com.idormy.sms.forwarder.utils.SendUtils import com.idormy.sms.forwarder.utils.SettingUtils +import com.xuexiang.xhttp2.XHttp +import com.xuexiang.xhttp2.cache.model.CacheMode +import com.xuexiang.xhttp2.callback.SimpleCallBack +import com.xuexiang.xhttp2.exception.ApiException import com.xuexiang.xutil.app.AppUtils -import okhttp3.* -import java.io.IOException import java.net.URLEncoder import java.nio.charset.StandardCharsets import java.text.SimpleDateFormat import java.util.* -import java.util.concurrent.TimeUnit import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec @@ -42,8 +41,8 @@ class WebhookUtils { msgInfo.getContentForSend(SettingUtils.smsTemplate.toString()) } - var webServer: String = setting.webServer //推送地址 - Log.i(TAG, "requestUrl:$webServer") + var requestUrl: String = setting.webServer //推送地址 + Log.i(TAG, "requestUrl:$requestUrl") val timestamp = System.currentTimeMillis() val orgContent: String = msgInfo.content @@ -62,16 +61,25 @@ class WebhookUtils { } var webParams = setting.webParams?.trim() - val requestBuilder: Request.Builder - if (setting.method == "GET" && TextUtils.isEmpty(webParams)) { + + //支持HTTP基本认证(Basic Authentication) + val regex = "^(https?://)([^:]+):([^@]+)@(.+)" + val matches = Regex(regex, RegexOption.IGNORE_CASE).findAll(requestUrl).toList().flatMap(MatchResult::groupValues) + Log.i(TAG, "matches = $matches") + if (matches.isNotEmpty()) { + requestUrl = matches[1] + matches[4] + Log.i(TAG, "requestUrl:$requestUrl") + } + + val request = if (setting.method == "GET" && TextUtils.isEmpty(webParams)) { setting.webServer += (if (setting.webServer.contains("?")) "&" else "?") + "from=" + URLEncoder.encode(from, "UTF-8") - webServer += "&content=" + URLEncoder.encode(content, "UTF-8") - if (!TextUtils.isEmpty(setting.secret)) { - webServer += "×tamp=$timestamp" - webServer += "&sign=$sign" + requestUrl += "&content=" + URLEncoder.encode(content, "UTF-8") + if (!TextUtils.isEmpty(sign)) { + requestUrl += "×tamp=$timestamp" + requestUrl += "&sign=$sign" } - Log.d(TAG, "method = GET, Url = $webServer") - requestBuilder = Request.Builder().url(webServer).get() + Log.d(TAG, "method = GET, Url = $requestUrl") + XHttp.get(requestUrl).keepJson(true) } else if (setting.method == "GET" && !TextUtils.isEmpty(webParams)) { webParams = webParams.toString().replace("[from]", URLEncoder.encode(from, "UTF-8")) .replace("[content]", URLEncoder.encode(content, "UTF-8")) @@ -87,110 +95,83 @@ class WebhookUtils { webParams = webParams.replace("[timestamp]", timestamp.toString()) .replace("[sign]", URLEncoder.encode(sign, "UTF-8")) } - webServer += (if (webServer.contains("?")) "&" else "?") + webParams - Log.d(TAG, "method = GET, Url = $webServer") - requestBuilder = Request.Builder().url(webServer).get() - } else if (webParams != null && !TextUtils.isEmpty(webParams)) { - val bodyMsg: String - var contentType = "application/x-www-form-urlencoded" - if (webParams.startsWith("{")) { - 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) - .replace("[timestamp]", timestamp.toString()) - .replace("[sign]", sign) + requestUrl += if (webParams.startsWith("/")) { + webParams } else { - 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")) - .replace("[timestamp]", URLEncoder.encode(timestamp.toString(), "UTF-8")) - .replace("[sign]", URLEncoder.encode(sign, "UTF-8")) + (if (requestUrl.contains("?")) "&" else "?") + webParams } - val body = RequestBody.create(MediaType.parse(contentType), bodyMsg) - requestBuilder = Request.Builder() - .url(webServer) - .addHeader("Content-Type", contentType) - .method("POST", body) - Log.d(TAG, "method = POST webParams, Body = $bodyMsg") + Log.d(TAG, "method = GET, Url = $requestUrl") + XHttp.get(requestUrl).keepJson(true) + } else if (webParams != null && webParams.isNotEmpty() && webParams.startsWith("{")) { + val 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) + .replace("[timestamp]", timestamp.toString()) + .replace("[sign]", sign) + Log.d(TAG, "method = POST, Url = $requestUrl, bodyMsg = $bodyMsg") + XHttp.post(requestUrl).keepJson(true).upJson(bodyMsg) } else { - val builder = MultipartBody.Builder().setType(MultipartBody.FORM) - .addFormDataPart("from", from) - .addFormDataPart("content", content) - if (!TextUtils.isEmpty(setting.secret)) { - builder.addFormDataPart("timestamp", timestamp.toString()) - builder.addFormDataPart("sign", sign) + if (webParams == null || webParams.isEmpty()) { + webParams = "from=[from]&content=[content]×tamp=[timestamp]" + if (!TextUtils.isEmpty(sign)) webParams += "&sign=[sign]" + } + + val postRequest = XHttp.post(requestUrl).keepJson(true) + webParams.split("&").forEach { + val param = it.split("=") + if (param.size == 2) { + postRequest.params(param[0], param[1].replace("[from]", from) + .replace("[content]", content) + .replace("[msg]", content) + .replace("[org_content]", orgContent) + .replace("[device_mark]", deviceMark) + .replace("[app_version]", appVersion) + .replace("[title]", simInfo) + .replace("[card_slot]", simInfo) + .replace("[receive_time]", receiveTime) + .replace("[timestamp]", timestamp.toString()) + .replace("[sign]", sign)) + } } - val body: RequestBody = builder.build() - Log.d(TAG, "method = POST, Body = $body") - requestBuilder = Request.Builder().url(webServer).method("POST", body) + postRequest } + //添加headers for ((key, value) in setting.headers?.entries!!) { - requestBuilder.addHeader(key, value) + request.headers(key, value) } - val clientBuilder = OkHttpClient.Builder() - - //设置重试拦截器 - val retryTimes: Int = SettingUtils.requestRetryTimes - if (retryTimes > 0) { - val delayTime: Long = SettingUtils.requestDelayTime.toLong() - val retryInterceptor: RetryInterceptor = RetryInterceptor.Builder().executionCount(retryTimes).retryInterval(delayTime).logId(0).build() - clientBuilder.addInterceptor(retryInterceptor) + //支持HTTP基本认证(Basic Authentication) + if (matches.isNotEmpty()) { + request.addInterceptor(BasicAuthInterceptor(matches[2], matches[3])) } - //忽略https证书 - CertUtils.x509TrustManager?.let { clientBuilder.sslSocketFactory(CertUtils.sSLSocketFactory, it).hostnameVerifier(CertUtils.hostnameVerifier) } - - //设置读取超时时间 - val client = clientBuilder - .readTimeout(SettingUtils.requestTimeout.toLong(), TimeUnit.SECONDS) - .writeTimeout(SettingUtils.requestTimeout.toLong(), TimeUnit.SECONDS) - .connectTimeout(SettingUtils.requestTimeout.toLong(), TimeUnit.SECONDS) - .build() - - client.newCall(requestBuilder.build()).enqueue(object : Callback { - override fun onFailure(call: Call, e: IOException) { - //解决在子线程中调用Toast的异常情况处理 - if (Looper.myLooper() == null) Looper.prepare() - e.printStackTrace() - SendUtils.updateLogs(logId, 0, e.message.toString()) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + e.message) - Looper.loop() - } + request.ignoreHttpsCert() //忽略https证书 + .timeOut((SettingUtils.requestTimeout * 1000).toLong()) //超时时间10s + .cacheMode(CacheMode.NO_CACHE) + .retryCount(SettingUtils.requestRetryTimes) //超时重试的次数 + .retryDelay(SettingUtils.requestDelayTime) //超时重试的延迟时间 + .retryIncreaseDelay(SettingUtils.requestDelayTime) //超时重试叠加延时 + .timeStamp(true) + .execute(object : SimpleCallBack() { + + override fun onError(e: ApiException) { + Log.e(TAG, e.detailMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) + } - @Throws(IOException::class) - override fun onResponse(call: Call, response: Response) { - val responseStr = response.body().toString() - Log.d(TAG, "Response:" + response.code() + "," + responseStr) - - //返回http状态200即为成功 - if (200 == response.code()) { - if (Looper.myLooper() == null) Looper.prepare() - SendUtils.updateLogs(logId, 2, responseStr) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) - Looper.loop() - } else { - if (Looper.myLooper() == null) Looper.prepare() - SendUtils.updateLogs(logId, 0, responseStr) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) - Looper.loop() + override fun onSuccess(response: String) { + Log.i(TAG, response) + SendUtils.updateLogs(logId, 2, response) } - } - }) + + }) } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkAgentUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkAgentUtils.kt index 7da4e91e76..544db60487 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkAgentUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkAgentUtils.kt @@ -3,6 +3,7 @@ package com.idormy.sms.forwarder.utils.sender import android.text.TextUtils import android.util.Log import com.google.gson.Gson +import com.idormy.sms.forwarder.R import com.idormy.sms.forwarder.database.entity.Rule import com.idormy.sms.forwarder.entity.MsgInfo import com.idormy.sms.forwarder.entity.result.DingtalkResult @@ -15,6 +16,7 @@ import com.xuexiang.xhttp2.XHttp import com.xuexiang.xhttp2.cache.model.CacheMode import com.xuexiang.xhttp2.callback.SimpleCallBack import com.xuexiang.xhttp2.exception.ApiException +import com.xuexiang.xui.utils.ResUtils.getString @Suppress("PrivatePropertyName", "UNUSED_PARAMETER") class WeworkAgentUtils private constructor() { @@ -48,9 +50,8 @@ class WeworkAgentUtils private constructor() { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -62,7 +63,7 @@ class WeworkAgentUtils private constructor() { MMKVUtils.put("expires_in_" + setting.agentID, System.currentTimeMillis() + ((resp.expires_in ?: 7200) - 120) * 1000L) //提前2分钟过期 sendTextMsg(setting, msgInfo, rule, logId) } else { - //XToastUtils.error(String.format(getString(R.string.request_failed_tips), response)) + SendUtils.updateLogs(logId, 0, String.format(getString(R.string.request_failed_tips), response)) } } @@ -107,9 +108,8 @@ class WeworkAgentUtils private constructor() { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -118,10 +118,8 @@ class WeworkAgentUtils private constructor() { val resp = Gson().fromJson(response, DingtalkResult::class.java) if (resp.errcode == 0L) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(getString(R.string.request_failed) + response) } } diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkRobotUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkRobotUtils.kt index ab85925eff..c9916c3506 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkRobotUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WeworkRobotUtils.kt @@ -56,9 +56,8 @@ class WeworkRobotUtils private constructor() { .execute(object : SimpleCallBack() { override fun onError(e: ApiException) { - SendUtils.updateLogs(logId, 0, e.displayMessage) Log.e(TAG, e.detailMessage) - //XToastUtils.error(e.displayMessage) + SendUtils.updateLogs(logId, 0, e.displayMessage) } override fun onSuccess(response: String) { @@ -67,10 +66,8 @@ class WeworkRobotUtils private constructor() { val resp = Gson().fromJson(response, WeworkRobotResult::class.java) if (resp.errcode == 0L) { SendUtils.updateLogs(logId, 2, response) - //XToastUtils.success(ResUtils.getString(R.string.request_succeeded)) } else { SendUtils.updateLogs(logId, 0, response) - //XToastUtils.error(ResUtils.getString(R.string.request_failed) + response) } }