Skip to content

Commit

Permalink
New: Custom UA for redirect rules
Browse files Browse the repository at this point in the history
Fix: Keep the original link when no rule matched
Misc: Add F-Droid icon and screenshot
  • Loading branch information
lz233 committed Jun 28, 2024
1 parent a399ff5 commit 0475b03
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 19 deletions.
18 changes: 9 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
minSdk 27
targetSdk 34
versionCode = date.format("yyyyMMdd").toInteger()
versionName "1.6.2"
versionName "1.6.5"

ksp {
arg("room.schemaLocation", "$projectDir/schemas")
Expand Down Expand Up @@ -75,15 +75,15 @@ dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.room:room-runtime:2.6.1'
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta02'
implementation 'androidx.viewpager2:viewpager2:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.preference:preference-ktx:1.2.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.2'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
implementation 'androidx.window:window:1.3.0-beta01'
implementation 'androidx.window:window:1.3.0'
ksp 'androidx.room:room-compiler:2.6.1'

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
Expand All @@ -95,7 +95,7 @@ dependencies {
coolapkImplementation 'com.microsoft.appcenter:appcenter-analytics:5.0.1'
coolapkImplementation 'com.microsoft.appcenter:appcenter-crashes:5.0.1'

implementation 'com.google.android.material:material:1.11.0'
implementation 'com.google.android.material:material:1.12.0'
implementation "dev.rikka.rikkax.material:material-preference:2.0.0"

implementation 'com.guolindev.permissionx:permissionx:1.7.1'
Expand All @@ -109,6 +109,6 @@ dependencies {
implementation 'org.lsposed.hiddenapibypass:hiddenapibypass:4.3'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
5 changes: 3 additions & 2 deletions app/src/main/java/cn/ac/lz233/tarnhelm/logic/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import cn.ac.lz233.tarnhelm.logic.module.meta.RegexRule

@Database(
entities = [RegexRule::class, ParameterRule::class, RedirectRule::class, Extension::class],
version = 4,
version = 5,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 3, to = 4)
AutoMigration(from = 3, to = 4),
AutoMigration(from = 4, to = 5)
]
)
abstract class AppDatabase : RoomDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data class RedirectRule(
@PrimaryKey val id: Int,
val description: String,
val domain: String,
val userAgent: String?,
val author: String,
val sourceType: Int, // 0:manual 1:paste 2:??
val enabled: Boolean,
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/cn/ac/lz233/tarnhelm/ui/rules/RulesActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ import cn.ac.lz233.tarnhelm.logic.module.meta.RedirectRule
import cn.ac.lz233.tarnhelm.logic.module.meta.RegexRule
import cn.ac.lz233.tarnhelm.ui.SecondaryBaseActivity
import cn.ac.lz233.tarnhelm.ui.rules.parameter.ParameterRulesFragment
import cn.ac.lz233.tarnhelm.ui.rules.regex.RegexRulesFragment
import cn.ac.lz233.tarnhelm.ui.rules.redirect.RedirectRulesFragment
import cn.ac.lz233.tarnhelm.ui.rules.regex.RegexRulesFragment
import cn.ac.lz233.tarnhelm.util.LogUtil
import cn.ac.lz233.tarnhelm.util.ktx.*
import cn.ac.lz233.tarnhelm.util.ktx.decodeBase64
import cn.ac.lz233.tarnhelm.util.ktx.decodeURL
import cn.ac.lz233.tarnhelm.util.ktx.getModeId
import cn.ac.lz233.tarnhelm.util.ktx.getString
import cn.ac.lz233.tarnhelm.util.ktx.insertToParameterRules
import cn.ac.lz233.tarnhelm.util.ktx.insertToRedirectRules
import cn.ac.lz233.tarnhelm.util.ktx.insertToRegexRules
import cn.ac.lz233.tarnhelm.util.ktx.toJSONArray
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayoutMediator
Expand Down Expand Up @@ -92,6 +99,7 @@ class RulesActivity : SecondaryBaseActivity() {
}
}
}

1 -> {
val dialogBinding = DialogRegexRuleAddBinding.inflate(layoutInflater)
val dialog = MaterialAlertDialogBuilder(this)
Expand Down Expand Up @@ -125,6 +133,7 @@ class RulesActivity : SecondaryBaseActivity() {
}
}
}

2 -> {
val dialogBinding = DialogRedirectRuleAddBinding.inflate(layoutInflater)
val dialog = MaterialAlertDialogBuilder(this)
Expand All @@ -134,6 +143,7 @@ class RulesActivity : SecondaryBaseActivity() {
App.redirectRuleDao.getMaxId() + 1,
dialogBinding.descriptionEditText.text.toString(),
dialogBinding.domainEditText.text.toString(),
dialogBinding.userAgentEditText.text.toString(),
dialogBinding.authorEditText.text.toString(),
0,
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RedirectRulesAdapter(private val rulesList: MutableList<RedirectRule>) : R
val ruleEnableSwitch: MaterialSwitch = view.findViewById(R.id.ruleEnableSwitch)
val descriptionContentTextView: AppCompatTextView = view.findViewById(R.id.descriptionContentTextView)
val domainContentTextView: AppCompatTextView = view.findViewById(R.id.domainContentTextView)
val userAgentContentTextView: AppCompatTextView = view.findViewById(R.id.userAgentContentTextView)
val authorContentTextView: AppCompatTextView = view.findViewById(R.id.authorContentTextView)
}

Expand All @@ -44,6 +45,7 @@ class RedirectRulesAdapter(private val rulesList: MutableList<RedirectRule>) : R
val base64Text = (if (SettingsDao.exportRulesAsLink) "tarnhelm://rule?redirect=" else "") + rule.toJSONObject().toString().encodeBase64().encodeURL()
dialogBinding.descriptionEditText.setText(rule.description)
dialogBinding.domainEditText.setText(rule.domain)
dialogBinding.userAgentEditText.setText(rule.userAgent)
dialogBinding.authorEditText.setText(rule.author)
dialogBinding.authorEditText.isEnabled = (rule.sourceType == 0) or BuildConfig.DEBUG
val dialog = MaterialAlertDialogBuilder(holder.itemView.context)
Expand All @@ -53,6 +55,7 @@ class RedirectRulesAdapter(private val rulesList: MutableList<RedirectRule>) : R
rule.id,
dialogBinding.descriptionEditText.text.toString(),
dialogBinding.domainEditText.text.toString(),
dialogBinding.userAgentEditText.text.toString(),
dialogBinding.authorEditText.text.toString(),
rule.sourceType,
rule.enabled
Expand Down Expand Up @@ -89,6 +92,7 @@ class RedirectRulesAdapter(private val rulesList: MutableList<RedirectRule>) : R
rule.id,
rule.description,
rule.domain,
rule.userAgent,
rule.author,
rule.sourceType,
b
Expand All @@ -99,6 +103,7 @@ class RedirectRulesAdapter(private val rulesList: MutableList<RedirectRule>) : R
}
holder.descriptionContentTextView.text = rule.description
holder.domainContentTextView.text = rule.domain
holder.userAgentContentTextView.text = rule.userAgent
holder.authorContentTextView.text = rule.author
}

Expand All @@ -112,6 +117,7 @@ class RedirectRulesAdapter(private val rulesList: MutableList<RedirectRule>) : R
toRule.id,
fromRule.description,
fromRule.domain,
fromRule.userAgent,
fromRule.author,
fromRule.sourceType,
fromRule.enabled
Expand All @@ -120,6 +126,7 @@ class RedirectRulesAdapter(private val rulesList: MutableList<RedirectRule>) : R
fromRule.id,
toRule.description,
toRule.domain,
toRule.userAgent,
toRule.author,
toRule.sourceType,
toRule.enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fun JSONObject.insertToRedirectRules(type: Int = 1, enabled: Boolean = true): Re
App.redirectRuleDao.getMaxId() + 1,
this.getString("a"),
this.getString("e"),
this.getString("h"),
this.getString("d"),
type,
enabled
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/cn/ac/lz233/tarnhelm/util/ktx/OkHttp.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package cn.ac.lz233.tarnhelm.util.ktx

import android.webkit.WebSettings
import cn.ac.lz233.tarnhelm.App
import cn.ac.lz233.tarnhelm.logic.Network
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Request
import okhttp3.internal.commonToString
import org.json.JSONObject
import java.io.IOException

fun HttpUrl.followRedirect(): HttpUrl {
fun HttpUrl.followRedirect(userAgent: String?): HttpUrl {
val response = Network.okHttpClientNoRedirect
.newCall(Request.Builder().url(this).build())
.newCall(
Request.Builder()
.url(this)
.addHeader("User-Agent", if (userAgent.isNullOrEmpty()) WebSettings.getDefaultUserAgent(App.context) else userAgent)
.build()
)
.execute()
return if (response.isSuccessful) {
// bilibili returns 404 but still using 200 header
Expand All @@ -22,8 +30,8 @@ fun HttpUrl.followRedirect(): HttpUrl {
this
} else if (response.isRedirect) {
(response.header("Location")?.toHttpUrlOrNull() ?: response.header("location")?.toHttpUrlOrNull())
?.followRedirect() ?: this
?.followRedirect(userAgent) ?: this
} else {
throw IOException(response.body.string())
throw IOException(response.commonToString())
}
}
1 change: 1 addition & 0 deletions app/src/main/java/cn/ac/lz233/tarnhelm/util/ktx/Rule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ fun RegexRule.toJSONObject() = JSONObject().apply {
fun RedirectRule.toJSONObject() = JSONObject().apply {
put("a", this@toJSONObject.description)
put("e", this@toJSONObject.domain)
put("h", this@toJSONObject.userAgent)
put("d", this@toJSONObject.author)
}
5 changes: 3 additions & 2 deletions app/src/main/java/cn/ac/lz233/tarnhelm/util/ktx/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fun String.doTarnhelm(): Triple<CharSequence, Boolean, List<String>> {
.setProgress(100, 0, true)
.build()
App.notificationManager.notify(234, notification)
httpUrl = httpUrl.followRedirect()
httpUrl = httpUrl.followRedirect(rule.userAgent)
}
}
result = httpUrl.toString()
Expand Down Expand Up @@ -117,6 +117,7 @@ fun String.doTarnhelm(): Triple<CharSequence, Boolean, List<String>> {
}
}
}
if (targetRules.isEmpty()) result = this
LogUtil._d("Result: $result")
LogUtil._d("TargetRules: $targetRules")
return Triple(result, hasTimeConsumingOperation, targetRules)
Expand All @@ -128,7 +129,7 @@ fun CharSequence.doTarnhelms(): Triple<CharSequence, Boolean, List<List<String>>
var hasTimeConsumingOperation = false
val targetRules = mutableListOf<List<String>>()
methodResult =
//Regex("""((https|http)://)(\p{L}|\p{Nd})+\.\p{L}+(:\p{Nd})?(\p{Ll}|\p{Lu}|\p{Nd}|/|\?|\+|&|=|\.|-|_|#|%)*""")
//Regex("""((https|http)://)(\p{L}|\p{Nd})+\.\p{L}+(:\p{Nd})?(\p{Ll}|\p{Lu}|\p{Nd}|/|\?|\+|&|=|\.|-|_|#|%)*""")
Regex("""(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s\uFF01-\uFF5E\u4e00-\u9fff\u3400-\u4DBF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u3000-\u303F]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s\uFF01-\uFF5E\u4e00-\u9fff\u3400-\u4DBF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u3000-\u303F]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s\uFF01-\uFF5E\u4e00-\u9fff\u3400-\u4DBF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u3000-\u303F]{2,}|www\.[a-zA-Z0-9]+\.[^\s\uFF01-\uFF5E\u4e00-\u9fff\u3400-\u4DBF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u3000-\u303F]{2,})""")
// PatternsCompat.AUTOLINK_WEB_URL.toRegex() cannot recognize some irregular sharing texts
// 73 xx发布了一篇小红书笔记,快来看吧! 😆 xxxxxxxxxxxxx 😆 http://xhslink.com/xxxxxx,复制本条信息,打开【小红书】App查看精彩内容!
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/dialog_redirect_rule_add.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:singleLine="true"

android:text="@string/redirectRulesAddDialogTitle"
android:textAppearance="@style/TextAppearance.Material3.TitleLarge"
android:textColor="?attr/colorOnSecondaryContainer" />
Expand Down Expand Up @@ -70,6 +71,22 @@

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/redirectRulesItemUserAgent"
app:endIconMode="clear_text">

<cn.ac.lz233.tarnhelm.view.EditText
android:id="@+id/userAgentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/dialog_redirect_rule_edit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/redirectRulesItemUserAgent"
app:endIconMode="clear_text">

<cn.ac.lz233.tarnhelm.view.EditText
android:id="@+id/userAgentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/item_redirect_rule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@
android:textAppearance="@style/TextAppearance.Material3.BodyLarge"
android:textColor="?attr/colorOnTertiaryContainer" />

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/redirectRulesItemUserAgent"
android:textAppearance="@style/TextAppearance.Material3.TitleLarge"
android:textColor="?attr/colorOnSecondaryContainer" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/userAgentContentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.Material3.BodyLarge"
android:textColor="?attr/colorOnTertiaryContainer" />

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="redirectRulesEditDialogTitle">编辑一个重定向规则</string>
<string name="redirectRulesItemDescription">描述</string>
<string name="redirectRulesItemDomain">域名</string>
<string name="redirectRulesItemUserAgent">用户代理</string>
<string name="redirectRulesItemAuthor">作者</string>
<string name="redirectRulesDialogPositiveButton">确认</string>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="redirectRulesEditDialogTitle">編輯一個重定向規則</string>
<string name="redirectRulesItemDescription">描述</string>
<string name="redirectRulesItemDomain">域名</string>
<string name="redirectRulesItemUserAgent">用戶代理</string>
<string name="redirectRulesItemAuthor">作者</string>
<string name="redirectRulesDialogPositiveButton">確認</string>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="redirectRulesEditDialogTitle">編輯一個重定向規則</string>
<string name="redirectRulesItemDescription">描述</string>
<string name="redirectRulesItemDomain">域名</string>
<string name="redirectRulesItemUserAgent">用戶代理</string>
<string name="redirectRulesItemAuthor">作者</string>
<string name="redirectRulesDialogPositiveButton">確認</string>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<string name="redirectRulesEditDialogTitle">编辑一个重定向规则</string>
<string name="redirectRulesItemDescription">描述</string>
<string name="redirectRulesItemDomain">域名</string>
<string name="redirectRulesItemUserAgent">用户代理</string>
<string name="redirectRulesItemAuthor">作者</string>
<string name="redirectRulesDialogPositiveButton">确认</string>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="redirectRulesEditDialogTitle">Edit a Redirect Rule</string>
<string name="redirectRulesItemDescription">Description</string>
<string name="redirectRulesItemDomain">Domain</string>
<string name="redirectRulesItemUserAgent">User Agent</string>
<string name="redirectRulesItemAuthor">Author</string>
<string name="redirectRulesDialogPositiveButton">OK</string>

Expand Down
Binary file added fastlane/metadata/android/en-US/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0475b03

Please sign in to comment.