Skip to content

Commit

Permalink
Improvements and fixes (#96)
Browse files Browse the repository at this point in the history
Improvements and fixes:
- Added Hosts to UI editor
- Allowed not to specify DNS
- Changed the level of saved logs to Debug
- Fixed bug with Reset button
  • Loading branch information
dovecoteescapee authored Aug 18, 2024
1 parent 31d7560 commit 88e1b75
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 16 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 = 8
versionName = "1.1.0-beta"
versionName = "1.1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/cpp/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ void clear_params(void);

char *ftob(const char *str, ssize_t *sl);

char *parse_cform(const char *str, ssize_t *size);

struct mphdr *parse_hosts(char *buffer, size_t size);

int parse_offset(struct part *part, const char *str);
56 changes: 49 additions & 7 deletions app/src/main/cpp/native-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
#include "utils.h"

const enum demode DESYNC_METHODS[] = {
DESYNC_NONE,
DESYNC_SPLIT,
DESYNC_DISORDER,
DESYNC_FAKE,
DESYNC_OOB,
DESYNC_NONE,
DESYNC_SPLIT,
DESYNC_DISORDER,
DESYNC_FAKE,
DESYNC_OOB,
};

enum hosts_mode {
HOSTS_DISABLE,
HOSTS_BLACKLIST,
HOSTS_WHITELIST,
};

JNIEXPORT jint JNI_OnLoad(
Expand Down Expand Up @@ -81,8 +87,9 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket(
jboolean host_remove_spaces,
jboolean tls_record_split,
jint tls_record_split_position,
jboolean tls_record_split_at_sni) {

jboolean tls_record_split_at_sni,
jint hosts_mode,
jstring hosts) {
struct sockaddr_ina s;

const char *address = (*env)->GetStringUTFChars(env, ip, 0);
Expand Down Expand Up @@ -112,6 +119,29 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket(
}
}

if (hosts_mode == HOSTS_WHITELIST) {
struct desync_params *dp = add(
(void *) &params.dp,
&params.dp_count,
sizeof(struct desync_params)
);
if (!dp) {
uniperror("add");
reset_params();
return -1;
}

const char *str = (*env)->GetStringUTFChars(env, hosts, 0);
dp->file_ptr = parse_cform(str, &dp->file_size);
(*env)->ReleaseStringUTFChars(env, hosts, str);
dp->hosts = parse_hosts(dp->file_ptr, dp->file_size);
if (!dp->hosts) {
perror("parse_hosts");
clear_params();
return -1;
}
}

struct desync_params *dp = add(
(void *) &params.dp,
&params.dp_count,
Expand All @@ -123,6 +153,18 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket(
return -1;
}

if (hosts_mode == HOSTS_BLACKLIST) {
const char *str = (*env)->GetStringUTFChars(env, hosts, 0);
dp->file_ptr = parse_cform(str, &dp->file_size);
(*env)->ReleaseStringUTFChars(env, hosts, str);
dp->hosts = parse_hosts(dp->file_ptr, dp->file_size);
if (!dp->hosts) {
perror("parse_hosts");
clear_params();
return -1;
}
}

dp->ttl = fake_ttl;
dp->proto =
IS_HTTP * desync_http |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MainActivity : AppCompatActivity() {
private fun collectLogs(): String? =
try {
Runtime.getRuntime()
.exec("logcat *:I -d")
.exec("logcat *:D -d")
.inputStream.bufferedReader()
.use { it.readText() }
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import io.github.dovecoteescapee.byedpi.R
import io.github.dovecoteescapee.byedpi.fragments.MainSettingsFragment
import io.github.dovecoteescapee.byedpi.utility.getPreferences
Expand Down Expand Up @@ -35,6 +36,7 @@ class SettingsActivity : AppCompatActivity() {
R.id.action_reset_settings -> {
getPreferences().edit().clear().apply()

supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, MainSettingsFragment())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class ByeDpiProxy {
tlsRecordSplit = preferences.tlsRecordSplit,
tlsRecordSplitPosition = preferences.tlsRecordSplitPosition,
tlsRecordSplitAtSni = preferences.tlsRecordSplitAtSni,
hostsMode = preferences.hostsMode.ordinal,
hosts = preferences.hosts
)
}

Expand Down Expand Up @@ -104,6 +106,8 @@ class ByeDpiProxy {
tlsRecordSplit: Boolean,
tlsRecordSplitPosition: Int,
tlsRecordSplitAtSni: Boolean,
hostsMode: Int,
hosts: String?
): Int

private external fun jniStartProxy(fd: Int): Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class ByeDpiProxyUIPreferences(
tlsRecordSplit: Boolean? = null,
tlsRecordSplitPosition: Int? = null,
tlsRecordSplitAtSni: Boolean? = null,
hostsMode: HostsMode? = null,
hosts: String? = null,
) : ByeDpiProxyPreferences {
val ip: String = ip ?: "127.0.0.1"
val port: Int = port ?: 1080
Expand All @@ -78,6 +80,12 @@ class ByeDpiProxyUIPreferences(
val tlsRecordSplit: Boolean = tlsRecordSplit ?: false
val tlsRecordSplitPosition: Int = tlsRecordSplitPosition ?: 0
val tlsRecordSplitAtSni: Boolean = tlsRecordSplitAtSni ?: false
val hostsMode: HostsMode =
if (hosts?.isBlank() != false) HostsMode.Disable
else hostsMode ?: HostsMode.Disable
val hosts: String? =
if (this.hostsMode == HostsMode.Disable) null
else hosts?.trim()

constructor(preferences: SharedPreferences) : this(
ip = preferences.getString("byedpi_proxy_ip", null),
Expand All @@ -103,6 +111,15 @@ class ByeDpiProxyUIPreferences(
tlsRecordSplitPosition = preferences.getString("byedpi_tlsrec_position", null)
?.toIntOrNull(),
tlsRecordSplitAtSni = preferences.getBoolean("byedpi_tlsrec_at_sni", false),
hostsMode = preferences.getString("byedpi_hosts_mode", null)
?.let { HostsMode.fromName(it) },
hosts = preferences.getString("byedpi_hosts_mode", null)?.let {
when (HostsMode.fromName(it)) {
HostsMode.Blacklist -> preferences.getString("byedpi_hosts_blacklist", null)
HostsMode.Whitelist -> preferences.getString("byedpi_hosts_whitelist", null)
else -> null
}
}
)

enum class DesyncMethod {
Expand All @@ -125,4 +142,21 @@ class ByeDpiProxyUIPreferences(
}
}
}

enum class HostsMode {
Disable,
Blacklist,
Whitelist;

companion object {
fun fromName(name: String): HostsMode {
return when (name) {
"disable" -> Disable
"blacklist" -> Blacklist
"whitelist" -> Whitelist
else -> throw IllegalArgumentException("Unknown hosts mode: $name")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dovecoteescapee.byedpi.services
package io.github.dovecoteescapee.byedpi.core

object TProxyService {
init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.preference.*
import io.github.dovecoteescapee.byedpi.R
import io.github.dovecoteescapee.byedpi.core.ByeDpiProxyUIPreferences
import io.github.dovecoteescapee.byedpi.core.ByeDpiProxyUIPreferences.DesyncMethod.*
import io.github.dovecoteescapee.byedpi.core.ByeDpiProxyUIPreferences.HostsMode.*
import io.github.dovecoteescapee.byedpi.utility.*

class ByeDpiUISettingsFragment : PreferenceFragmentCompat() {
Expand Down Expand Up @@ -65,7 +66,11 @@ class ByeDpiUISettingsFragment : PreferenceFragmentCompat() {
val desyncMethod =
findPreferenceNotNull<ListPreference>("byedpi_desync_method")
.value.let { ByeDpiProxyUIPreferences.DesyncMethod.fromName(it) }
val hostsMode = findPreferenceNotNull<ListPreference>("byedpi_hosts_mode")
.value.let { ByeDpiProxyUIPreferences.HostsMode.fromName(it) }

val hostsBlacklist = findPreferenceNotNull<EditTextPreference>("byedpi_hosts_blacklist")
val hostsWhitelist = findPreferenceNotNull<EditTextPreference>("byedpi_hosts_whitelist")
val desyncHttp = findPreferenceNotNull<CheckBoxPreference>("byedpi_desync_http")
val desyncHttps = findPreferenceNotNull<CheckBoxPreference>("byedpi_desync_https")
val desyncUdp = findPreferenceNotNull<CheckBoxPreference>("byedpi_desync_udp")
Expand All @@ -83,6 +88,23 @@ class ByeDpiUISettingsFragment : PreferenceFragmentCompat() {
findPreferenceNotNull<EditTextPreference>("byedpi_tlsrec_position")
val splitTlsRecAtSni = findPreferenceNotNull<CheckBoxPreference>("byedpi_tlsrec_at_sni")

when (hostsMode) {
Disable -> {
hostsBlacklist.isVisible = false
hostsWhitelist.isVisible = false
}

Blacklist -> {
hostsBlacklist.isVisible = true
hostsWhitelist.isVisible = false
}

Whitelist -> {
hostsBlacklist.isVisible = false
hostsWhitelist.isVisible = true
}
}

when (desyncMethod) {
None -> {
desyncHttp.isVisible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.main_settings, rootKey)

setEditTextPreferenceListener("dns_ip") { checkIp(it) }
setEditTextPreferenceListener("dns_ip") {
it.isBlank() || checkIp(it)
}

findPreferenceNotNull<DropDownPreference>("app_theme")
.setOnPreferenceChangeListener { _, newValue ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.github.dovecoteescapee.byedpi.R
import io.github.dovecoteescapee.byedpi.activities.MainActivity
import io.github.dovecoteescapee.byedpi.core.ByeDpiProxy
import io.github.dovecoteescapee.byedpi.core.ByeDpiProxyPreferences
import io.github.dovecoteescapee.byedpi.core.TProxyService
import io.github.dovecoteescapee.byedpi.data.*
import io.github.dovecoteescapee.byedpi.utility.*
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -268,6 +269,7 @@ class ByeDpiVpnService : LifecycleVpnService() {
)

private fun createBuilder(dns: String): Builder {
Log.d(TAG, "DNS: $dns")
val builder = Builder()
builder.setSession("ByeDPI")
builder.setConfigureIntent(
Expand All @@ -282,7 +284,9 @@ class ByeDpiVpnService : LifecycleVpnService() {
builder.addAddress("10.10.10.10", 32)
builder.addRoute("0.0.0.0", 0)
builder.addRoute("0:0:0:0:0:0:0:0", 0)
builder.addDnsServer(dns)
if (dns.isNotBlank()) {
builder.addDnsServer(dns)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.setMetered(false)
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/jni/Application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
APP_OPTIM := release
APP_PLATFORM := android-21
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
APP_CFLAGS := -O3 -DPKGNAME=io/github/dovecoteescapee/byedpi/services
APP_CFLAGS := -O3 -DPKGNAME=io/github/dovecoteescapee/byedpi/core
APP_CPPFLAGS := -O3 -std=c++11
NDK_TOOLCHAIN_VERSION := clang
11 changes: 11 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@
<item name="fake">fake</item>
<item name="oob">oob</item>
</array>

<array name="byedpi_hosts_modes">
<item name="disable">Disable</item>
<item name="blacklist">Blacklist</item>
<item name="whitelist">Whitelist</item>
</array>
<array name="byedpi_hosts_modes_entries">
<item name="disable">disable</item>
<item name="blacklist">blacklist</item>
<item name="whitelist">whitelist</item>
</array>
</resources>
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@
<string name="sni_of_fake_packet">SNI of fake packet</string>
<string name="byedpi_proxy">Proxy</string>
<string name="byedpi_desync">Desync</string>
</resources>
<string name="command_line_arguments">Command line arguments</string>
<string name="byedpi_hosts_mode_setting">Hosts</string>
<string name="byedpi_hosts_blacklist_setting">Hosts blacklist</string>
<string name="byedpi_hosts_whitelist_setting">Hosts whitelist</string>
</resources>
4 changes: 2 additions & 2 deletions app/src/main/res/xml/byedpi_cmd_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_cmd_args"
android:title="Command line arguments"
android:dialogTitle="Command line arguments"
android:title="@string/command_line_arguments"
android:dialogTitle="@string/command_line_arguments"
android:inputType="textMultiLine"
app:useSimpleSummaryProvider="true" />

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/res/xml/byedpi_ui_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@
<androidx.preference.PreferenceCategory
android:title="@string/byedpi_desync">

<DropDownPreference
android:key="byedpi_hosts_mode"
android:title="@string/byedpi_hosts_mode_setting"
android:entries="@array/byedpi_hosts_modes"
android:entryValues="@array/byedpi_hosts_modes_entries"
android:defaultValue="disable"
app:useSimpleSummaryProvider="true" />

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_hosts_blacklist"
android:title="@string/byedpi_hosts_blacklist_setting"
android:dialogTitle="@string/byedpi_hosts_blacklist_setting"
android:inputType="textMultiLine"
app:useSimpleSummaryProvider="true" />

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_hosts_whitelist"
android:title="@string/byedpi_hosts_whitelist_setting"
android:dialogTitle="@string/byedpi_hosts_whitelist_setting"
android:inputType="textMultiLine"
app:useSimpleSummaryProvider="true" />

<com.takisoft.preferencex.EditTextPreference
android:key="byedpi_default_ttl"
android:title="@string/byedpi_default_ttl_setting"
Expand Down

0 comments on commit 88e1b75

Please sign in to comment.