Skip to content

Commit

Permalink
Changing the logic of IP and port settings
Browse files Browse the repository at this point in the history
  • Loading branch information
romanvht committed Oct 25, 2024
1 parent 4bcb7ef commit e7333ff
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 64 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdk = 21
targetSdk = 34
versionCode = 10
versionName = "1.3.8-beta2"
versionName = "1.3.8-beta3"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,22 @@ class MainActivity : AppCompatActivity() {
Log.i(TAG, "Updating status: $status, $mode")

val preferences = getPreferences()
val proxyIp = preferences.getStringNotNull("byedpi_proxy_ip", "127.0.0.1")
val proxyPort = preferences.getStringNotNull("byedpi_proxy_port", "1080")

val cmdEnable = preferences.getBoolean("byedpi_enable_cmd_settings", false)
val cmdArgs = if (cmdEnable) preferences.getStringNotNull("byedpi_cmd_args", "") else null
val args = cmdArgs?.split(" ") ?: emptyList()
val cmdIp = args.let { argsList ->
val ipIndex = argsList.indexOfFirst { it == "-i" || it == "--ip" }
if (ipIndex != -1) argsList[ipIndex + 1] else null
}
val cmdPort = args.let { argsList ->
val portIndex = argsList.indexOfFirst { it == "-p" || it == "--port" }
if (portIndex != -1) argsList[portIndex + 1] else null
}

val proxyIp = cmdIp ?: preferences.getStringNotNull("byedpi_proxy_ip", "127.0.0.1")
val proxyPort = cmdPort ?: preferences.getStringNotNull("byedpi_proxy_port", "1080")

binding.proxyAddress.text = getString(R.string.proxy_address, proxyIp, proxyPort)

when (status) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.dovecoteescapee.byedpi.core

import android.content.SharedPreferences
import android.util.Log
import io.github.dovecoteescapee.byedpi.utility.getStringNotNull
import io.github.dovecoteescapee.byedpi.utility.shellSplit

Expand All @@ -15,20 +16,38 @@ sealed interface ByeDpiProxyPreferences {
}

class ByeDpiProxyCmdPreferences(val args: Array<String>) : ByeDpiProxyPreferences {
constructor(cmd: String) : this(cmdToArgs(cmd))

constructor(preferences: SharedPreferences) : this(
preferences.getStringNotNull(
"byedpi_cmd_args",
""
cmdToArgs(
preferences.getStringNotNull("byedpi_cmd_args", ""),
preferences
)
)

companion object {
private fun cmdToArgs(cmd: String): Array<String> {
private fun cmdToArgs(cmd: String, preferences: SharedPreferences): Array<String> {
val firstArgIndex = cmd.indexOf("-")
val argsStr = (if (firstArgIndex > 0) cmd.substring(firstArgIndex) else cmd).trim()
return arrayOf("ciadpi") + shellSplit(argsStr)
val args = (if (firstArgIndex > 0) cmd.substring(firstArgIndex) else cmd).trim()

Log.i("ProxyPref", "Parse args: $args")

val hasIp = args.contains("-i ") || args.contains("--ip ")
val hasPort = args.contains("-p ") || args.contains("--port ")

val ip = preferences.getStringNotNull("byedpi_proxy_ip", "127.0.0.1")
val port = preferences.getStringNotNull("byedpi_proxy_port", "1080")

val prefix = buildString {
if (!hasIp) append("-i $ip ")
if (!hasPort) append("-p $port ")
}

Log.i("ProxyPref", "Added from settings: $prefix")

if (prefix.isNotEmpty()) {
return arrayOf("ciadpi") + shellSplit("$prefix$args")
}

return arrayOf("ciadpi") + shellSplit(args)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class ByeDpiUISettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.byedpi_ui_settings, rootKey)

setEditTextPreferenceListener("byedpi_proxy_ip") { checkIp(it) }
setEditTestPreferenceListenerPort("byedpi_proxy_port")
setEditTestPreferenceListenerInt(
"byedpi_max_connections",
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.github.dovecoteescapee.byedpi.fragments

import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Bundle
import android.provider.Settings
import android.util.Log
Expand All @@ -16,7 +15,6 @@ import io.github.dovecoteescapee.byedpi.data.Mode
import io.github.dovecoteescapee.byedpi.utility.AccessibilityUtils
import io.github.dovecoteescapee.byedpi.services.AutoStartAccessibilityService
import io.github.dovecoteescapee.byedpi.utility.*
import java.util.Locale

class MainSettingsFragment : PreferenceFragmentCompat() {
companion object {
Expand Down Expand Up @@ -61,6 +59,9 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.main_settings, rootKey)

setEditTextPreferenceListener("byedpi_proxy_ip") { checkIp(it) }
setEditTestPreferenceListenerPort("byedpi_proxy_port")

setEditTextPreferenceListener("dns_ip") {
it.isBlank() || checkNotLocalIp(it)
}
Expand All @@ -77,7 +78,7 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
true
}

val accessibilityStatusPref = findPreferenceNotNull<Preference>("accessibility_service_status")
val accessibilityStatus = findPreferenceNotNull<Preference>("accessibility_service_status")
val switchCommandLineSettings = findPreferenceNotNull<SwitchPreference>("byedpi_enable_cmd_settings")

val uiSettings = findPreferenceNotNull<Preference>("byedpi_ui_settings")
Expand All @@ -94,10 +95,11 @@ class MainSettingsFragment : PreferenceFragmentCompat() {

switchCommandLineSettings.setOnPreferenceChangeListener { _, newValue ->
setByeDpiSettingsMode(newValue as Boolean)
updatePreferences()
true
}

accessibilityStatusPref.setOnPreferenceClickListener {
accessibilityStatus.setOnPreferenceClickListener {
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
startActivity(intent)
true
Expand All @@ -112,13 +114,14 @@ class MainSettingsFragment : PreferenceFragmentCompat() {

findPreferenceNotNull<Preference>("version").summary = BuildConfig.VERSION_NAME

updateAccessibilityStatus(accessibilityStatusPref)
updateAccessibilityStatus()
updatePreferences()
}

override fun onResume() {
super.onResume()
sharedPreferences?.registerOnSharedPreferenceChangeListener(preferenceListener)
updateAccessibilityStatus()
updatePreferences()
}

Expand All @@ -128,59 +131,71 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
}

private fun updatePreferences() {
val mode = findPreferenceNotNull<ListPreference>("byedpi_mode")
.value.let { Mode.fromString(it) }
val mode = findPreferenceNotNull<ListPreference>("byedpi_mode").value.let { Mode.fromString(it) }
val dns = findPreferenceNotNull<EditTextPreference>("dns_ip")
val ipv6 = findPreferenceNotNull<SwitchPreference>("ipv6_enable")

val applist_type = findPreferenceNotNull<ListPreference>("applist_type")
val selected_apps = findPreferenceNotNull<Preference>("selected_apps")
val accessibilityStatusPref = findPreferenceNotNull<Preference>("accessibility_service_status")
val ip = findPreferenceNotNull<EditTextPreference>("byedpi_proxy_ip")
val port = findPreferenceNotNull<EditTextPreference>("byedpi_proxy_port")

val applistType = findPreferenceNotNull<ListPreference>("applist_type")
val selectedApps = findPreferenceNotNull<Preference>("selected_apps")

updateAccessibilityStatus(accessibilityStatusPref)
val cmdEnable = sharedPreferences?.getBoolean("byedpi_enable_cmd_settings", false)

if (cmdEnable == true) {
val cmdArgs = sharedPreferences?.getStringNotNull("byedpi_cmd_args", "")?.split(" ")
val ipIndex = cmdArgs?.indexOfFirst { it == "-i" || it == "--ip" }
val portIndex = cmdArgs?.indexOfFirst { it == "-p" || it == "--port" }

ip.isEnabled = ipIndex == -1
port.isEnabled = portIndex == -1
} else {
ip.isEnabled = true
port.isEnabled = true
}

when (mode) {
Mode.VPN -> {
dns.isVisible = true
ipv6.isVisible = true

when (applist_type.value) {
when (applistType.value) {
"disable" -> {
applist_type.isVisible = true
selected_apps.isVisible = false
applistType.isVisible = true
selectedApps.isVisible = false
}
"blacklist", "whitelist" -> {
applist_type.isVisible = true
selected_apps.isVisible = true
applistType.isVisible = true
selectedApps.isVisible = true
}
else -> {
applist_type.isVisible = true
selected_apps.isVisible = false
Log.w(TAG, "Unexpected applist_type value: ${applist_type.value}")
applistType.isVisible = true
selectedApps.isVisible = false
Log.w(TAG, "Unexpected applistType value: ${applistType.value}")
}
}
}

Mode.Proxy -> {
dns.isVisible = false
ipv6.isVisible = false
applist_type.isVisible = false
selected_apps.isVisible = false
applistType.isVisible = false
selectedApps.isVisible = false
}
}
}

private fun updateAccessibilityStatus(preference: Preference?) {
preference?.let {
val isEnabled = AccessibilityUtils.isAccessibilityServiceEnabled(
requireContext(),
AutoStartAccessibilityService::class.java
)
it.summary = if (isEnabled) {
getString(R.string.accessibility_service_enabled)
} else {
getString(R.string.accessibility_service_disabled)
}
private fun updateAccessibilityStatus() {
val accessibilityStatus = findPreferenceNotNull<Preference>("accessibility_service_status")
val isEnabled = AccessibilityUtils.isAccessibilityServiceEnabled(
requireContext(),
AutoStartAccessibilityService::class.java
)
accessibilityStatus.summary = if (isEnabled) {
getString(R.string.accessibility_service_enabled)
} else {
getString(R.string.accessibility_service_disabled)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,30 @@ class ByeDpiVpnService : LifecycleVpnService() {
}

val sharedPreferences = getPreferences()
val port = sharedPreferences.getString("byedpi_proxy_port", null)?.toInt() ?: 1080
val dns = sharedPreferences.getStringNotNull("dns_ip", "1.1.1.1")

val cmdEnable = sharedPreferences.getBoolean("byedpi_enable_cmd_settings", false)
val cmdArgs = if (cmdEnable) sharedPreferences.getStringNotNull("byedpi_cmd_args", "") else null
val args = cmdArgs?.split(" ") ?: emptyList()
val cmdIp = args.let { argsList ->
val ipIndex = argsList.indexOfFirst { it == "-i" || it == "--ip" }
if (ipIndex != -1) argsList[ipIndex + 1] else null
}
val cmdPort = args.let { argsList ->
val portIndex = argsList.indexOfFirst { it == "-p" || it == "--port" }
if (portIndex != -1) argsList[portIndex + 1] else null
}

val ip = cmdIp ?: sharedPreferences.getStringNotNull("byedpi_proxy_ip", "127.0.0.1")
val port = cmdPort ?: sharedPreferences.getStringNotNull("byedpi_proxy_port", "1080")
val dns = sharedPreferences.getStringNotNull("dns_ip", "8.8.8.8")
val ipv6 = sharedPreferences.getBoolean("ipv6_enable", false)

val tun2socksConfig = """
| misc:
| task-stack-size: 81920
| socks5:
| mtu: 8500
| address: 127.0.0.1
| address: $ip
| port: $port
| udp: udp
""".trimMargin("| ")
Expand All @@ -209,7 +223,7 @@ class ByeDpiVpnService : LifecycleVpnService() {

TProxyService.TProxyStartService(configPath.absolutePath, fd.fd)

Log.i(TAG, "Tun2Socks started")
Log.i(TAG, "Tun2Socks started. ip: $ip port: $port")
}

private fun stopTun2Socks() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<string name="theme_settings">Theme</string>
<string name="mode_setting">Mode</string>
<string name="dbs_ip_setting">DNS</string>
<string name="bye_dpi_proxy_ip_setting">Listening address</string>
<string name="bye_dpi_proxy_ip_setting">Address</string>
<string name="byedpi_proxy_port_setting">Port</string>
<string name="byedpi_max_connections_setting">Max connections</string>
<string name="byedpi_buffer_size_setting">Buffer size</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<string name="theme_settings">Тема</string>
<string name="mode_setting">Режим</string>
<string name="dbs_ip_setting">DNS</string>
<string name="bye_dpi_proxy_ip_setting">Адрес прослушивания</string>
<string name="bye_dpi_proxy_ip_setting">Адрес</string>
<string name="byedpi_proxy_port_setting">Порт</string>
<string name="byedpi_max_connections_setting">Максимальное к-во подключений</string>
<string name="byedpi_buffer_size_setting">Размер буфера</string>
Expand Down
13 changes: 0 additions & 13 deletions app/src/main/res/xml/byedpi_ui_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@
<androidx.preference.PreferenceCategory
android:title="@string/byedpi_proxy">

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_proxy_ip"
android:title="@string/bye_dpi_proxy_ip_setting"
android:defaultValue="127.0.0.1"
app:useSimpleSummaryProvider="true" />

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_proxy_port"
android:title="@string/byedpi_proxy_port_setting"
android:inputType="number"
android:defaultValue="1080"
app:useSimpleSummaryProvider="true" />

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_max_connections"
android:title="@string/byedpi_max_connections_setting"
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/xml/main_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@

</androidx.preference.PreferenceCategory>

<androidx.preference.PreferenceCategory
android:title="@string/byedpi_proxy">

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_proxy_ip"
android:title="@string/bye_dpi_proxy_ip_setting"
android:defaultValue="127.0.0.1"
app:useSimpleSummaryProvider="true" />

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_proxy_port"
android:title="@string/byedpi_proxy_port_setting"
android:inputType="number"
android:defaultValue="1080"
app:useSimpleSummaryProvider="true" />

</androidx.preference.PreferenceCategory>

<androidx.preference.PreferenceCategory
android:title="@string/automation">

Expand Down

0 comments on commit e7333ff

Please sign in to comment.