Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/main/java/io/nekohasekai/sagernet/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ object Key {
const val BYPASS_LAN_IN_CORE = "bypassLanInCore"

const val MIXED_PORT = "mixedPort"
const val MIXED_USERNAME = "mixedUsername"
const val MIXED_PASSWORD = "mixedPassword"
const val ALLOW_ACCESS = "allowAccess"
const val SPEED_INTERVAL = "speedInterval"
const val SHOW_DIRECT_SPEED = "showDirectSpeed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import io.nekohasekai.sagernet.ktx.string
import io.nekohasekai.sagernet.ktx.stringToInt
import io.nekohasekai.sagernet.ktx.stringToIntIfExists
import moe.matsuri.nb4a.TempDatabase
import moe.matsuri.nb4a.utils.Util

object DataStore : OnPreferenceDataStoreChangeListener {

Expand Down Expand Up @@ -128,11 +129,17 @@ object DataStore : OnPreferenceDataStoreChangeListener {
var mixedPort: Int
get() = getLocalPort(Key.MIXED_PORT, 2080)
set(value) = saveLocalPort(Key.MIXED_PORT, value)
var mixedUsername by configurationStore.string(Key.MIXED_USERNAME) { "User" }
var mixedPassword by configurationStore.string(Key.MIXED_PASSWORD) { Util.generateCryptoSecurePassword() }

fun initGlobal() {
if (configurationStore.getString(Key.MIXED_PORT) == null) {
mixedPort = mixedPort
}

if (configurationStore.getString(Key.MIXED_PASSWORD) == null) {
mixedPassword = Util.generateCryptoSecurePassword()
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ fun buildConfig(
domain_strategy = genDomainStrategy(DataStore.resolveDestination)
sniff = needSniff
sniff_override_destination = needSniffOverride
users = listOf(
User().apply {
username = DataStore.mixedUsername
password = DataStore.mixedPassword
},
)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
true
}
val mixedPort = findPreference<EditTextPreference>(Key.MIXED_PORT)!!
val mixedUsername = findPreference<EditTextPreference>(Key.MIXED_USERNAME)!!
val mixedPassword = findPreference<EditTextPreference>(Key.MIXED_PASSWORD)!!
val serviceMode = findPreference<Preference>(Key.SERVICE_MODE)!!
val allowAccess = findPreference<Preference>(Key.ALLOW_ACCESS)!!
val appendHttpProxy = findPreference<SwitchPreference>(Key.APPEND_HTTP_PROXY)!!
Expand Down Expand Up @@ -149,6 +151,8 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
}

mixedPort.onPreferenceChangeListener = reloadListener
mixedUsername.onPreferenceChangeListener = reloadListener
mixedPassword.onPreferenceChangeListener = reloadListener
appendHttpProxy.onPreferenceChangeListener = reloadListener
showDirectSpeed.onPreferenceChangeListener = reloadListener
trafficSniffing.onPreferenceChangeListener = reloadListener
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/moe/matsuri/nb4a/utils/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,12 @@ object Util {
val encoded = match?.groupValues?.get(1) ?: ""
return URLDecoder.decode(encoded, StandardCharsets.UTF_8.name())
}

fun generateCryptoSecurePassword(length: Int = 10): String {
val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()"
val secureRandom = java.security.SecureRandom()
return (1..length)
.map { chars[secureRandom.nextInt(chars.length)] }
.joinToString("")
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/xml/global_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@
app:key="mixedPort"
app:title="@string/port_proxy"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
app:defaultValue="User"
app:key="mixedUsername"
app:title="Proxy Username"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
app:icon="@drawable/ic_settings_password"
app:key="mixedPassword"
app:title="Proxy Password"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
app:defaultValue="false"
app:key="appendHttpProxy"
Expand Down