优化:发送通道Bark/Gotify支持HTTP基本认证 【格式:http://username:password@domain.com/uri】 #170

This commit is contained in:
pppscn 2022-06-23 20:29:34 +08:00
parent ad65e094b7
commit 271fa6a102
3 changed files with 46 additions and 4 deletions

View File

@ -41,8 +41,17 @@ class BarkUtils {
val requestUrl: String = setting.server //推送地址
Log.i(TAG, "requestUrl:$requestUrl")
val request = XHttp.post(requestUrl)
.params("title", title)
//支持HTTP基本认证(Basic Authentication)
val regex = "^(https?://)([^:]+):([^@]+)@(.+)"
val matches = Regex(regex, RegexOption.IGNORE_CASE).findAll(requestUrl).toList().flatMap(MatchResult::groupValues)
Log.i(TAG, "matches = $matches")
val request = if (matches.isNotEmpty()) {
XHttp.post(matches[1] + matches[4]).addInterceptor(BasicAuthInterceptor(matches[2], matches[3]))
} else {
XHttp.post(requestUrl)
}
request.params("title", title)
.params("body", content)
.params("isArchive", 1)
if (!TextUtils.isEmpty(setting.group)) request.params("group", setting.group)

View File

@ -0,0 +1,24 @@
package com.idormy.sms.forwarder.utils.sender
import okhttp3.Credentials
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
class BasicAuthInterceptor(user: String, password: String) : Interceptor {
private val credentials: String
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request: Request = chain.request()
val authenticatedRequest: Request = request.newBuilder()
.header("Authorization", credentials).build()
return chain.proceed(authenticatedRequest)
}
init {
credentials = Credentials.basic(user, password)
}
}

View File

@ -39,8 +39,17 @@ class GotifyUtils {
val requestUrl: String = setting.webServer //推送地址
Log.i(TAG, "requestUrl:$requestUrl")
XHttp.post(requestUrl)
.params("title", title)
//支持HTTP基本认证(Basic Authentication)
val regex = "^(https?://)([^:]+):([^@]+)@(.+)"
val matches = Regex(regex, RegexOption.IGNORE_CASE).findAll(requestUrl).toList().flatMap(MatchResult::groupValues)
Log.i(TAG, "matches = $matches")
val request = if (matches.isNotEmpty()) {
XHttp.post(matches[1] + matches[4]).addInterceptor(BasicAuthInterceptor(matches[2], matches[3]))
} else {
XHttp.post(requestUrl)
}
request.params("title", title)
.params("message", content)
.params("priority", setting.priority)
.keepJson(true)