新增:`webhook`发送通道增加`PUT`/`PATCH`请求方式 #206

This commit is contained in:
pppscn 2022-09-22 19:59:52 +08:00
parent ff2be5553f
commit c00e61ece1
6 changed files with 61 additions and 24 deletions

View File

@ -11,10 +11,11 @@ data class WebhookSetting(
val headers: Map<String, String>?,
) : Serializable {
fun getMethodCheckId(): Int {
return if (method == null || method == "POST") {
R.id.rb_method_post
} else {
R.id.rb_method_get
return when (method) {
null, "POST" -> R.id.rb_method_post
"PUT" -> R.id.rb_method_put
"PATCH" -> R.id.rb_method_patch
else -> R.id.rb_method_get
}
}
}

View File

@ -49,13 +49,19 @@ class WebhookUtils {
val deviceMark: String = SettingUtils.extraDeviceMark ?: ""
val appVersion: String = AppUtils.getAppVersionName()
val simInfo: String = msgInfo.simInfo
@SuppressLint("SimpleDateFormat") val receiveTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()) //smsVo.getDate()
@SuppressLint("SimpleDateFormat") val receiveTime =
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()) //smsVo.getDate()
var sign = ""
if (!TextUtils.isEmpty(setting.secret)) {
val stringToSign = "$timestamp\n" + setting.secret
val mac = Mac.getInstance("HmacSHA256")
mac.init(SecretKeySpec(setting.secret?.toByteArray(StandardCharsets.UTF_8), "HmacSHA256"))
mac.init(
SecretKeySpec(
setting.secret?.toByteArray(StandardCharsets.UTF_8),
"HmacSHA256"
)
)
val signData = mac.doFinal(stringToSign.toByteArray(StandardCharsets.UTF_8))
sign = URLEncoder.encode(String(Base64.encode(signData, Base64.NO_WRAP)), "UTF-8")
}
@ -64,7 +70,8 @@ class WebhookUtils {
//支持HTTP基本认证(Basic Authentication)
val regex = "^(https?://)([^:]+):([^@]+)@(.+)"
val matches = Regex(regex, RegexOption.IGNORE_CASE).findAll(requestUrl).toList().flatMap(MatchResult::groupValues)
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]
@ -72,7 +79,10 @@ class WebhookUtils {
}
val request = if (setting.method == "GET" && TextUtils.isEmpty(webParams)) {
setting.webServer += (if (setting.webServer.contains("?")) "&" else "?") + "from=" + URLEncoder.encode(from, "UTF-8")
setting.webServer += (if (setting.webServer.contains("?")) "&" else "?") + "from=" + URLEncoder.encode(
from,
"UTF-8"
)
requestUrl += "&content=" + URLEncoder.encode(content, "UTF-8")
if (!TextUtils.isEmpty(sign)) {
requestUrl += "&timestamp=$timestamp"
@ -114,19 +124,28 @@ class WebhookUtils {
.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)
Log.d(TAG, "method = ${setting.method}, Url = $requestUrl, bodyMsg = $bodyMsg")
when (setting.method) {
"PUT" -> XHttp.put(requestUrl).keepJson(true).upJson(bodyMsg)
"PATCH" -> XHttp.patch(requestUrl).keepJson(true).upJson(bodyMsg)
else -> XHttp.post(requestUrl).keepJson(true).upJson(bodyMsg)
}
} else {
if (webParams == null || webParams.isEmpty()) {
webParams = "from=[from]&content=[content]&timestamp=[timestamp]"
if (!TextUtils.isEmpty(sign)) webParams += "&sign=[sign]"
}
val postRequest = XHttp.post(requestUrl).keepJson(true)
Log.d(TAG, "method = ${setting.method}, Url = $requestUrl")
val postRequest = when (setting.method) {
"PUT" -> XHttp.put(requestUrl).keepJson(true)
"PATCH" -> XHttp.patch(requestUrl).keepJson(true)
else -> 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)
postRequest.params(
param[0], param[1].replace("[from]", from)
.replace("[content]", content)
.replace("[msg]", content)
.replace("[org_content]", orgContent)
@ -136,7 +155,8 @@ class WebhookUtils {
.replace("[card_slot]", simInfo)
.replace("[receive_time]", receiveTime)
.replace("[timestamp]", timestamp.toString())
.replace("[sign]", sign))
.replace("[sign]", sign)
)
}
}
postRequest

View File

@ -81,6 +81,18 @@
android:layout_height="wrap_content"
android:text="@string/get" />
<RadioButton
android:id="@+id/rb_method_put"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/put" />
<RadioButton
android:id="@+id/rb_method_patch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/patch" />
</RadioGroup>
</LinearLayout>

View File

@ -395,6 +395,8 @@
<string name="mu_rule_app_tips">Example of multiple matching rules: (see wiki for syntax)\n\nAND IS PACKAGE_NAME EQUALS com.tencent.mm\n[space]OR IS PACKAGE_NAME EQUALS com.tencent.mm\nAND IS INFORM_CONTENT CONTAIN arrears\n\nThe above rules mean: Receive an APP notification, and (the APP package name is com.tencent.mm or the APP package name is com.tencent.mm), and the content of the notification includes forwarding the notification when the payment is in arrears\n\nNote: The space at the beginning of each line represents the level, too complex multiple rules may lead to a large memory usage!</string>
<string name="post">POST</string>
<string name="get">GET</string>
<string name="put">PUT</string>
<string name="patch">PATCH</string>
<!--CloneActivity-->
<string name="local_ip">Local IP: </string>
<string name="operating_instruction">Instructions: \n[Note] The APP version of the sender and receiver must be the same!\n1. Please keep the SOURCE and DESTINATION phones in the same Wi-Fi network, and do not turn on isolation. \n2. Tap "Send" on SOURCE mobile phone, and get "server IP" \n3. After filling in "Server IP" on DESTINATION phone, tap "Receive". \n [NOTE:] sender(s), forwarding rule(s) and log(s) will be overwritten after cloning!</string> <!-- 原文是“新旧手机”,英文翻译中处理为“源”手机和“目标”手机,因为担心“新旧”的表述引起混淆(有没一种可能就是用户就是用从新手机的设备复制到旧手机上去呢?)。 -->

View File

@ -396,6 +396,8 @@
<string name="mu_rule_app_tips">多重匹配规则示例语法参见wiki\n\n并且 是 APP包名 相等 com.tencent.mm\n[空格]或者 是 APP包名 相等 com.tencent.mm\n并且 是 通知内容 包含 欠费\n\n以上规则表示收到APP通知并且APP包名是com.tencent.mm 或者 APP包名是com.tencent.mm并且 通知内容 包含 欠费 时转发通知\n\n注意每行开始的空格代表层级太过复杂的多重规则可能导致内存占用很大</string>
<string name="post">POST</string>
<string name="get">GET</string>
<string name="put">PUT</string>
<string name="patch">PATCH</string>
<!--CloneActivity-->
<string name="local_ip">本机IP</string>
<string name="operating_instruction">严正声明:\n该功能仅限个人新旧手机切换使用用于非法用途后果自负\n\n操作说明\n1.新旧手机连接同一个WiFi网络(禁用AP隔离)如需穿透内网请先配置Frpc\n2.【二选一】旧手机点【推送】按钮,将本机的配置推送到服务端\n3.【二选一】新手机点【拉取】按钮,将拉取服务端的配置到本机\n\n注意事项\n1.客户端与服务端的APP版本必须一致才能克隆!\n2.导入成功后,发送通道、转发规则将完全被覆盖,清空历史记录!\n3.主动请求、保活措施、个性设置不在克隆范围</string>

View File

@ -42,7 +42,7 @@ versions.xui = "1.2.2"
versions.xupdate = "2.1.3"
versions.xaop = "1.1.0"
versions.xutil = "2.0.0"
versions.xhttp2 = "2.0.8"
versions.xhttp2 = "2.0.9"
versions.xpage = "3.3.0"
versions.xrouter = "1.0.1"