Build Android configuration from one preferences and key snapshot
This commit is contained in:
1 parent
f5db3bdf4a
commit
e634126121
1 file changed
+149
-109
@@ -131,8 +131,7 @@ class ConfigProvider(private val context: Context) {
|
||||
File(keysFilePath()).delete()
|
||||
}
|
||||
|
||||
private fun readKeysFromFile(): Pair<String, String>? {
|
||||
val f = File(keysFilePath())
|
||||
private fun readKeysFromFile(f: File): Pair<String, String>? {
|
||||
if (!f.exists()) return null
|
||||
try {
|
||||
val lines = f.readLines()
|
||||
@@ -144,7 +143,10 @@ class ConfigProvider(private val context: Context) {
|
||||
}
|
||||
}
|
||||
if (pub.length == 64 && priv.length == 64) return Pair(pub, priv)
|
||||
} catch (_: Exception) {}
|
||||
LogManager.addLog("WARN", "CFG", "keys file has incomplete key pair path=${f.path}")
|
||||
} catch (e: Exception) {
|
||||
LogManager.addLog("ERROR", "CFG", "keys file read failed path=${f.path}: ${e.javaClass.simpleName}: ${e.message}")
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -168,8 +170,12 @@ class ConfigProvider(private val context: Context) {
|
||||
}
|
||||
|
||||
fun getCallVideoBitrateRange(): CallVideoBitrateRange = runBlocking {
|
||||
val value = context.dataStore.data.first()[callVideoBitrateRange] ?: return@runBlocking CallVideoBitrateRange()
|
||||
CallVideoBitrateRange.parse(value) ?: run {
|
||||
videoBitrateRange(context.dataStore.data.first())
|
||||
}
|
||||
|
||||
private fun videoBitrateRange(prefs: Preferences): CallVideoBitrateRange {
|
||||
val value = prefs[callVideoBitrateRange] ?: return CallVideoBitrateRange()
|
||||
return CallVideoBitrateRange.parse(value) ?: run {
|
||||
LogManager.addLog("WARN", "CallSettings", "Invalid video bitrate range '$value'; using 300:4000")
|
||||
CallVideoBitrateRange()
|
||||
}
|
||||
@@ -220,56 +226,65 @@ class ConfigProvider(private val context: Context) {
|
||||
}
|
||||
|
||||
fun getString(key: String): String? = runBlocking {
|
||||
when {
|
||||
key == "node.private_key" -> readKeysFromFile()?.second ?: context.dataStore.data.first()[nodePrivateKey]
|
||||
key == "node.public_key" -> readKeysFromFile()?.first ?: context.dataStore.data.first()[nodePublicKey]
|
||||
key == "node.name" -> context.dataStore.data.first()[nodeName] ?: getDeviceName()
|
||||
key == "db.path" -> context.dataStore.data.first()[dbPath] ?: context.filesDir.absolutePath
|
||||
key == "control.bind_ip" -> context.dataStore.data.first()[controlBindIp] ?: "127.0.0.1"
|
||||
key == "debug.console_level" -> context.dataStore.data.first()[debugConsoleLevel] ?: "info"
|
||||
key == "debug.log_file" -> context.dataStore.data.first()[debugLogFile] ?: "utun.log"
|
||||
key == "debug.categories" -> context.dataStore.data.first()[debugCategories] ?: ""
|
||||
key == "log_udp.ip" -> context.dataStore.data.first()[logUdpIp] ?: "192.168.40.250"
|
||||
else -> null
|
||||
}
|
||||
val prefs = context.dataStore.data.first()
|
||||
val keys = if (key == "node.private_key" || key == "node.public_key")
|
||||
readKeysFromFile(File(prefs[dbPath] ?: context.filesDir.absolutePath, "keys.conf")) else null
|
||||
stringValue(key, prefs, keys)
|
||||
}
|
||||
|
||||
private fun stringValue(key: String, prefs: Preferences, keys: Pair<String, String>?): String? =
|
||||
when (key) {
|
||||
"node.private_key" -> keys?.second ?: prefs[nodePrivateKey]
|
||||
"node.public_key" -> keys?.first ?: prefs[nodePublicKey]
|
||||
"node.name" -> prefs[nodeName] ?: getDeviceName()
|
||||
"db.path" -> prefs[dbPath] ?: context.filesDir.absolutePath
|
||||
"control.bind_ip" -> prefs[controlBindIp] ?: "127.0.0.1"
|
||||
"debug.console_level" -> prefs[debugConsoleLevel] ?: "info"
|
||||
"debug.log_file" -> prefs[debugLogFile] ?: "utun.log"
|
||||
"debug.categories" -> prefs[debugCategories] ?: ""
|
||||
"log_udp.ip" -> prefs[logUdpIp] ?: "192.168.40.250"
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun getInt(key: String): Int = runBlocking {
|
||||
when {
|
||||
key == "gui.utun_enabled" -> context.dataStore.data.first()[guiUtunEnabled] ?: 0
|
||||
key == "node.listen_port" -> context.dataStore.data.first()[nodeListenPort] ?: 12345
|
||||
key == "control.port" -> context.dataStore.data.first()[controlPort] ?: 9999
|
||||
key == "log_udp.port" -> context.dataStore.data.first()[logUdpPort] ?: 9999
|
||||
key == "chat.storage_autoload" -> context.dataStore.data.first()[chatStorageAutoload] ?: 1
|
||||
key == "chat.storage_autoload_maxsize_mb" -> context.dataStore.data.first()[chatStorageAutoloadMaxsizeMb] ?: 10
|
||||
key == "chat.storage_maxsize_gb" -> context.dataStore.data.first()[chatStorageMaxsizeGb] ?: 1
|
||||
key == "chat.opus_codec_preset" -> context.dataStore.data.first()[chatOpusCodecPreset] ?: 1
|
||||
key == "chat.stream_subblocks" -> context.dataStore.data.first()[streamSubblocks] ?: 8
|
||||
key == "chat.stream_rs_redundant" -> context.dataStore.data.first()[streamRsRedundant] ?: 0
|
||||
key == "chat.call_jitter_max_reserve_ms" -> context.dataStore.data.first()[callJitterMaxReserveMs] ?: 1000
|
||||
key == "chat.compressor_enabled" -> context.dataStore.data.first()[chatCompressorEnabled] ?: 1
|
||||
key == "chat.compressor_max_gain_db" -> context.dataStore.data.first()[chatCompressorMaxGainDb] ?: 15
|
||||
key == "chat.compressor_rise_rate" -> context.dataStore.data.first()[chatCompressorRiseRate] ?: 6
|
||||
key == "chat.media_download_max_peers" -> context.dataStore.data.first()[chatMediaDownloadMaxPeers] ?: 3
|
||||
key == "chat.standby_active_sec" -> context.dataStore.data.first()[chatStandbyActiveSec] ?: 2
|
||||
key == "chat.standby_sleep_sec" -> context.dataStore.data.first()[chatStandbySleepSec] ?: 60
|
||||
key == "chat.standby_min_sleep_sec" -> context.dataStore.data.first()[chatStandbyMinSleepSec] ?: 15
|
||||
key == "chat.standby_keepalive_ms" -> context.dataStore.data.first()[chatStandbyKeepaliveMs] ?: 30000
|
||||
key == "chat.group_autoconnect" -> context.dataStore.data.first()[chatGroupAutoconnect] ?: 1
|
||||
key == "chat.local_invites_enabled" -> context.dataStore.data.first()[chatLocalInvitesEnabled] ?: 1
|
||||
key == "chat.local_broadcasts_enabled" -> context.dataStore.data.first()[chatLocalBroadcastsEnabled] ?: 1
|
||||
key == "radio.auto_enabled" -> context.dataStore.data.first()[radioAutoEnabledKey] ?: 0
|
||||
key == "radio.vad_threshold" -> context.dataStore.data.first()[radioVadThresholdKey] ?: 50
|
||||
key == "radio.auto_hangover_ms" -> context.dataStore.data.first()[radioAutoHangoverMsKey] ?: 200
|
||||
key == "radio.vad_compressor_max_gain_db" -> context.dataStore.data.first()[radioVadCompressorMaxGainDbKey] ?: 15
|
||||
key == "radio.opus_mode" -> context.dataStore.data.first()[radioOpusModeKey] ?: 0
|
||||
key == "radio.opus_bitrate" -> context.dataStore.data.first()[radioOpusBitrateKey] ?: 32
|
||||
key == "radio.capture_stereo" -> context.dataStore.data.first()[radioCaptureStereoKey] ?: 0
|
||||
key == "radio.compressor_enabled" -> context.dataStore.data.first()[radioCompressorEnabledKey] ?: 1
|
||||
key == "radio.compressor_max_gain_db" -> context.dataStore.data.first()[radioCompressorMaxGainDbKey] ?: 15
|
||||
intValue(key, context.dataStore.data.first())
|
||||
}
|
||||
|
||||
private fun intValue(key: String, prefs: Preferences): Int =
|
||||
when (key) {
|
||||
"gui.utun_enabled" -> prefs[guiUtunEnabled] ?: 0
|
||||
"node.listen_port" -> prefs[nodeListenPort] ?: 12345
|
||||
"control.port" -> prefs[controlPort] ?: 9999
|
||||
"log_udp.port" -> prefs[logUdpPort] ?: 9999
|
||||
"chat.storage_autoload" -> prefs[chatStorageAutoload] ?: 1
|
||||
"chat.storage_autoload_maxsize_mb" -> prefs[chatStorageAutoloadMaxsizeMb] ?: 10
|
||||
"chat.storage_maxsize_gb" -> prefs[chatStorageMaxsizeGb] ?: 1
|
||||
"chat.opus_codec_preset" -> prefs[chatOpusCodecPreset] ?: 1
|
||||
"chat.stream_subblocks" -> prefs[streamSubblocks] ?: 8
|
||||
"chat.stream_rs_redundant" -> prefs[streamRsRedundant] ?: 0
|
||||
"chat.call_jitter_max_reserve_ms" -> prefs[callJitterMaxReserveMs] ?: 1000
|
||||
"chat.compressor_enabled" -> prefs[chatCompressorEnabled] ?: 1
|
||||
"chat.compressor_max_gain_db" -> prefs[chatCompressorMaxGainDb] ?: 15
|
||||
"chat.compressor_rise_rate" -> prefs[chatCompressorRiseRate] ?: 6
|
||||
"chat.media_download_max_peers" -> prefs[chatMediaDownloadMaxPeers] ?: 3
|
||||
"chat.standby_active_sec" -> prefs[chatStandbyActiveSec] ?: 2
|
||||
"chat.standby_sleep_sec" -> prefs[chatStandbySleepSec] ?: 60
|
||||
"chat.standby_min_sleep_sec" -> prefs[chatStandbyMinSleepSec] ?: 15
|
||||
"chat.standby_keepalive_ms" -> prefs[chatStandbyKeepaliveMs] ?: 30000
|
||||
"chat.group_autoconnect" -> prefs[chatGroupAutoconnect] ?: 1
|
||||
"chat.local_invites_enabled" -> prefs[chatLocalInvitesEnabled] ?: 1
|
||||
"chat.local_broadcasts_enabled" -> prefs[chatLocalBroadcastsEnabled] ?: 1
|
||||
"radio.auto_enabled" -> prefs[radioAutoEnabledKey] ?: 0
|
||||
"radio.vad_threshold" -> prefs[radioVadThresholdKey] ?: 50
|
||||
"radio.auto_hangover_ms" -> prefs[radioAutoHangoverMsKey] ?: 200
|
||||
"radio.vad_compressor_max_gain_db" -> prefs[radioVadCompressorMaxGainDbKey] ?: 15
|
||||
"radio.opus_mode" -> prefs[radioOpusModeKey] ?: 0
|
||||
"radio.opus_bitrate" -> prefs[radioOpusBitrateKey] ?: 32
|
||||
"radio.capture_stereo" -> prefs[radioCaptureStereoKey] ?: 0
|
||||
"radio.compressor_enabled" -> prefs[radioCompressorEnabledKey] ?: 1
|
||||
"radio.compressor_max_gain_db" -> prefs[radioCompressorMaxGainDbKey] ?: 15
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setValue(key: String, value: String) {
|
||||
context.dataStore.edit { prefs ->
|
||||
@@ -434,8 +449,12 @@ class ConfigProvider(private val context: Context) {
|
||||
}
|
||||
|
||||
fun getAutoSocketProtocols(): AutoSocketProtocols = runBlocking {
|
||||
val value = context.dataStore.data.first()[autoSocketProtocolsKey] ?: return@runBlocking AutoSocketProtocols.BOTH
|
||||
AutoSocketProtocols.values().firstOrNull { it.configValue == value } ?: run {
|
||||
autoSocketProtocols(context.dataStore.data.first())
|
||||
}
|
||||
|
||||
private fun autoSocketProtocols(prefs: Preferences): AutoSocketProtocols {
|
||||
val value = prefs[autoSocketProtocolsKey] ?: return AutoSocketProtocols.BOTH
|
||||
return AutoSocketProtocols.values().firstOrNull { it.configValue == value } ?: run {
|
||||
LogManager.addLog("WARN", "CFG", "invalid auto_socket_protocols='$value', using tcp+udp")
|
||||
AutoSocketProtocols.BOTH
|
||||
}
|
||||
@@ -597,10 +616,10 @@ class ConfigProvider(private val context: Context) {
|
||||
|
||||
/* ── Server entries ── */
|
||||
|
||||
suspend fun detectInterfaces(saved: List<ServerEntry> = emptyList()): List<ServerEntry> {
|
||||
suspend fun detectInterfaces(saved: List<ServerEntry> = emptyList(),
|
||||
protocols: AutoSocketProtocols = getAutoSocketProtocols()): List<ServerEntry> {
|
||||
val servers = mutableListOf<ServerEntry>()
|
||||
val seen = mutableSetOf<String>()
|
||||
val protocols = getAutoSocketProtocols()
|
||||
try {
|
||||
val nis = NetworkInterface.getNetworkInterfaces()
|
||||
while (nis.hasMoreElements()) {
|
||||
@@ -632,9 +651,21 @@ class ConfigProvider(private val context: Context) {
|
||||
return "$dir/utun.cfg"
|
||||
}
|
||||
|
||||
private data class ConfigSnapshot(val preferences: Preferences, val directory: File, val keys: Pair<String, String>?)
|
||||
|
||||
/** Одно чтение DataStore и одна пара keys.conf на весь сохраняемый/передаваемый снимок. */
|
||||
private suspend fun configSnapshot(): ConfigSnapshot {
|
||||
val prefs = context.dataStore.data.first()
|
||||
val directory = File(prefs[dbPath] ?: context.filesDir.absolutePath)
|
||||
return ConfigSnapshot(prefs, directory, readKeysFromFile(File(directory, "keys.conf")))
|
||||
}
|
||||
|
||||
/** Читает проверенные listen-секции; IP берёт с текущего интерфейса либо оставляет wildcard для отсутствующего. */
|
||||
fun loadServersFromConfig(includeCommented: Boolean = false): List<ServerEntry> {
|
||||
val file = File(configFilePath())
|
||||
return loadServers(File(configFilePath()), includeCommented)
|
||||
}
|
||||
|
||||
private fun loadServers(file: File, includeCommented: Boolean): List<ServerEntry> {
|
||||
if (!file.exists()) return emptyList()
|
||||
val saved = try {
|
||||
file.bufferedReader().use { reader ->
|
||||
@@ -658,33 +689,40 @@ class ConfigProvider(private val context: Context) {
|
||||
}
|
||||
|
||||
fun saveServersToConfig(servers: List<ServerEntry>, commented: List<ServerEntry> = emptyList()) {
|
||||
val text = buildConfigFromServers(servers, commented)
|
||||
File(configFilePath()).writeText(text)
|
||||
saveConfig(runBlocking { configSnapshot() }, servers, commented)
|
||||
}
|
||||
|
||||
private fun saveConfig(snapshot: ConfigSnapshot, servers: List<ServerEntry>, commented: List<ServerEntry>) {
|
||||
val text = buildConfigFromServers(snapshot, servers, commented)
|
||||
File(snapshot.directory, "utun.cfg").writeText(text)
|
||||
LogManager.addLog("INFO", "CFG", "saveServersToConfig: ${servers.size} active, ${commented.size} commented, " +
|
||||
"storage_unit_size=${getInt("chat.storage_autoload_maxsize_mb")}M storage_total_size=${getInt("chat.storage_maxsize_gb")}G; " +
|
||||
"storage_unit_size=${intValue("chat.storage_autoload_maxsize_mb", snapshot.preferences)}M " +
|
||||
"storage_total_size=${intValue("chat.storage_maxsize_gb", snapshot.preferences)}G; " +
|
||||
servers.joinToString(", ") { "${it.ifName}=${it.protocol}:${it.port}" })
|
||||
}
|
||||
|
||||
/** Build complete INI text from server list (without touching config file) */
|
||||
private fun buildConfigFromServers(servers: List<ServerEntry>, commented: List<ServerEntry> = emptyList()): String {
|
||||
val pubKey = getString("node.public_key") ?: ""
|
||||
val privKey = getString("node.private_key") ?: ""
|
||||
val nodeName = getString("node.name") ?: getDeviceName()
|
||||
val dbDir = getString("db.path") ?: context.filesDir.absolutePath
|
||||
/** Формирует INI из готового снимка; не читает DataStore/файлы и не сохраняет результат. */
|
||||
private fun buildConfigFromServers(snapshot: ConfigSnapshot, servers: List<ServerEntry>,
|
||||
commented: List<ServerEntry> = emptyList()): String {
|
||||
val prefs = snapshot.preferences
|
||||
val pubKey = stringValue("node.public_key", prefs, snapshot.keys) ?: ""
|
||||
val privKey = stringValue("node.private_key", prefs, snapshot.keys) ?: ""
|
||||
val nodeName = stringValue("node.name", prefs, snapshot.keys) ?: getDeviceName()
|
||||
val dbDir = stringValue("db.path", prefs, snapshot.keys) ?: context.filesDir.absolutePath
|
||||
return buildString {
|
||||
appendLine("[global]")
|
||||
if (nodeName.isNotBlank()) appendLine("my_node_name=$nodeName")
|
||||
if (pubKey.length == 64) appendLine("my_public_key=$pubKey")
|
||||
if (privKey.length == 64) appendLine("my_private_key=$privKey")
|
||||
appendLine("db_path=$dbDir")
|
||||
appendLine("debug_level=${getString("debug.console_level") ?: "info"}")
|
||||
appendLine("debug_level=${stringValue("debug.console_level", prefs, snapshot.keys) ?: "info"}")
|
||||
appendLine("db_sync_enabled=1")
|
||||
appendLine("chatserver_enabled=1")
|
||||
appendLine("auto_sockets=android")
|
||||
appendLine("auto_socket_protocols=${getAutoSocketProtocols().configValue}")
|
||||
appendLine("auto_socket_protocols=${autoSocketProtocols(prefs).configValue}")
|
||||
appendLine()
|
||||
appendLine("[gui]")
|
||||
appendLine("utun_enabled=${getInt("gui.utun_enabled")}")
|
||||
appendLine("utun_enabled=${intValue("gui.utun_enabled", prefs)}")
|
||||
appendLine()
|
||||
for (srv in servers) appendServerSection(this, srv, false)
|
||||
for (srv in commented) appendServerSection(this, srv, true)
|
||||
@@ -695,27 +733,27 @@ class ConfigProvider(private val context: Context) {
|
||||
appendLine("enabled=no")
|
||||
appendLine()
|
||||
appendLine("[chat]")
|
||||
val incoming = runBlocking { incomingCallOptions.first() }
|
||||
val incoming = incomingCallKeys.map { prefs[it] ?: false }
|
||||
val policy = incoming.withIndex().fold(0) { mask, (index, value) -> mask or if (value) (1 shl index) else 0 }
|
||||
appendLine("incoming_call_policy=$policy")
|
||||
val chatAutoload = getInt("chat.storage_autoload")
|
||||
val chatAutoloadMaxMb = getInt("chat.storage_autoload_maxsize_mb")
|
||||
val chatMaxGb = getInt("chat.storage_maxsize_gb")
|
||||
val chatOpusPreset = getInt("chat.opus_codec_preset")
|
||||
val radioOpusMode = getInt("radio.opus_mode")
|
||||
val radioOpusBitrate = getInt("radio.opus_bitrate")
|
||||
val radioCaptureStereo = getInt("radio.capture_stereo")
|
||||
val radioCompEnabled = getInt("radio.compressor_enabled")
|
||||
val radioCompMaxGain = getInt("radio.compressor_max_gain_db")
|
||||
val chatCompEnabled = getInt("chat.compressor_enabled")
|
||||
val chatCompMaxGain = getInt("chat.compressor_max_gain_db")
|
||||
val chatCompRiseRate = getInt("chat.compressor_rise_rate")
|
||||
val chatDlMaxPeers = getInt("chat.media_download_max_peers")
|
||||
val chatAutoload = intValue("chat.storage_autoload", prefs)
|
||||
val chatAutoloadMaxMb = intValue("chat.storage_autoload_maxsize_mb", prefs)
|
||||
val chatMaxGb = intValue("chat.storage_maxsize_gb", prefs)
|
||||
val chatOpusPreset = intValue("chat.opus_codec_preset", prefs)
|
||||
val radioOpusMode = intValue("radio.opus_mode", prefs)
|
||||
val radioOpusBitrate = intValue("radio.opus_bitrate", prefs)
|
||||
val radioCaptureStereo = intValue("radio.capture_stereo", prefs)
|
||||
val radioCompEnabled = intValue("radio.compressor_enabled", prefs)
|
||||
val radioCompMaxGain = intValue("radio.compressor_max_gain_db", prefs)
|
||||
val chatCompEnabled = intValue("chat.compressor_enabled", prefs)
|
||||
val chatCompMaxGain = intValue("chat.compressor_max_gain_db", prefs)
|
||||
val chatCompRiseRate = intValue("chat.compressor_rise_rate", prefs)
|
||||
val chatDlMaxPeers = intValue("chat.media_download_max_peers", prefs)
|
||||
appendLine("storage_autoload=$chatAutoload")
|
||||
appendLine("storage_unit_size=${chatAutoloadMaxMb}M")
|
||||
appendLine("storage_total_size=${chatMaxGb}G")
|
||||
appendLine("opus_codec_preset=$chatOpusPreset")
|
||||
val radioAuto = getRadioAutoSettings()
|
||||
val radioAuto = radioAutoSettings(prefs)
|
||||
appendLine("radio_auto_enabled=${if (radioAuto.enabled) 1 else 0}")
|
||||
appendLine("radio_auto_mode=${radioAuto.mode}")
|
||||
appendLine("radio_level_on_dbfs=${radioAuto.levelOn}")
|
||||
@@ -728,32 +766,32 @@ class ConfigProvider(private val context: Context) {
|
||||
appendLine("radio_capture_stereo=$radioCaptureStereo")
|
||||
appendLine("radio_compressor_enabled=$radioCompEnabled")
|
||||
appendLine("radio_compressor_max_gain_db=$radioCompMaxGain")
|
||||
appendLine("stream_subblocks=${getInt("chat.stream_subblocks")}")
|
||||
appendLine("stream_rs_redundant=${getInt("chat.stream_rs_redundant")}")
|
||||
appendLine("call_jitter_max_reserve_ms=${getInt("chat.call_jitter_max_reserve_ms")}")
|
||||
appendLine("call_video_bitrate_range_kbps=${getCallVideoBitrateRange().encode()}")
|
||||
appendLine("stream_subblocks=${intValue("chat.stream_subblocks", prefs)}")
|
||||
appendLine("stream_rs_redundant=${intValue("chat.stream_rs_redundant", prefs)}")
|
||||
appendLine("call_jitter_max_reserve_ms=${intValue("chat.call_jitter_max_reserve_ms", prefs)}")
|
||||
appendLine("call_video_bitrate_range_kbps=${videoBitrateRange(prefs).encode()}")
|
||||
appendLine("compressor_enabled=$chatCompEnabled")
|
||||
appendLine("compressor_max_gain_db=$chatCompMaxGain")
|
||||
appendLine("compressor_rise_rate=$chatCompRiseRate")
|
||||
appendLine("media_download_max_peers=$chatDlMaxPeers")
|
||||
appendLine("standby_active_sec=${getInt("chat.standby_active_sec")}")
|
||||
appendLine("standby_sleep_sec=${getInt("chat.standby_sleep_sec")}")
|
||||
appendLine("standby_min_sleep_sec=${getInt("chat.standby_min_sleep_sec")}")
|
||||
appendLine("standby_keepalive_ms=${getInt("chat.standby_keepalive_ms")}")
|
||||
appendLine("group_autoconnect=${getInt("chat.group_autoconnect")}")
|
||||
appendLine("local_invites_enabled=${getInt("chat.local_invites_enabled")}")
|
||||
appendLine("local_broadcasts_enabled=${getInt("chat.local_broadcasts_enabled")}")
|
||||
appendLine("standby_active_sec=${intValue("chat.standby_active_sec", prefs)}")
|
||||
appendLine("standby_sleep_sec=${intValue("chat.standby_sleep_sec", prefs)}")
|
||||
appendLine("standby_min_sleep_sec=${intValue("chat.standby_min_sleep_sec", prefs)}")
|
||||
appendLine("standby_keepalive_ms=${intValue("chat.standby_keepalive_ms", prefs)}")
|
||||
appendLine("group_autoconnect=${intValue("chat.group_autoconnect", prefs)}")
|
||||
appendLine("local_invites_enabled=${intValue("chat.local_invites_enabled", prefs)}")
|
||||
appendLine("local_broadcasts_enabled=${intValue("chat.local_broadcasts_enabled", prefs)}")
|
||||
appendLine()
|
||||
appendLine("[debug]")
|
||||
val dbgCat = getString("debug.categories") ?: ""
|
||||
val dbgCat = stringValue("debug.categories", prefs, snapshot.keys) ?: ""
|
||||
if (dbgCat.isNotEmpty()) {
|
||||
for (entry in dbgCat.split(",")) {
|
||||
val trimmed = entry.trim()
|
||||
if (trimmed.isNotEmpty()) appendLine(trimmed)
|
||||
}
|
||||
}
|
||||
val udpIp = getString("log_udp.ip") ?: ""
|
||||
val udpPort = getInt("log_udp.port")
|
||||
val udpIp = stringValue("log_udp.ip", prefs, snapshot.keys) ?: ""
|
||||
val udpPort = intValue("log_udp.port", prefs)
|
||||
if (udpIp.isNotEmpty() && udpPort > 0) {
|
||||
appendLine()
|
||||
appendLine("[log_udp]")
|
||||
@@ -806,11 +844,13 @@ class ConfigProvider(private val context: Context) {
|
||||
|
||||
/** Build full INI config text for NativeLib.start() */
|
||||
fun buildConfigText(): String {
|
||||
if (getConnectionMode() == "auto") {
|
||||
val cfgFile = File(configFilePath())
|
||||
val saved = if (cfgFile.exists()) loadServersFromConfig(includeCommented = true) else emptyList()
|
||||
val detected = runBlocking { detectInterfaces(saved) }
|
||||
LogManager.addLog("INFO", "CFG", "buildConfigText: auto mode protocols=${getAutoSocketProtocols().configValue}, ${detected.size} sockets: " +
|
||||
val snapshot = runBlocking { configSnapshot() }
|
||||
val protocols = autoSocketProtocols(snapshot.preferences)
|
||||
if ((snapshot.preferences[connectionModeKey] ?: "auto") == "auto") {
|
||||
val cfgFile = File(snapshot.directory, "utun.cfg")
|
||||
val saved = if (cfgFile.exists()) loadServers(cfgFile, includeCommented = true) else emptyList()
|
||||
val detected = runBlocking { detectInterfaces(saved, protocols) }
|
||||
LogManager.addLog("INFO", "CFG", "buildConfigText: auto mode protocols=${protocols.configValue}, ${detected.size} sockets: " +
|
||||
detected.joinToString(", ") { "${it.ifName}=${it.protocol}:${it.port}" })
|
||||
val upKeys = detected.map { it.socketKey }.toSet()
|
||||
val down = saved.filter { it.socketKey !in upKeys }.distinctBy { it.socketKey }
|
||||
@@ -818,19 +858,19 @@ class ConfigProvider(private val context: Context) {
|
||||
LogManager.addLog("INFO", "CFG", "buildConfigText: ${down.size} sockets inactive, ports preserved: " +
|
||||
down.joinToString(", ") { "${it.ifName}=${it.protocol}:${it.port}" })
|
||||
}
|
||||
saveServersToConfig(detected, down)
|
||||
return buildConfigFromServers(detected)
|
||||
saveConfig(snapshot, detected, down)
|
||||
return buildConfigFromServers(snapshot, detected)
|
||||
}
|
||||
val cfgFile = File(configFilePath())
|
||||
val existing = if (cfgFile.exists()) loadServersFromConfig() else emptyList()
|
||||
val cfgFile = File(snapshot.directory, "utun.cfg")
|
||||
val existing = if (cfgFile.exists()) loadServers(cfgFile, false) else emptyList()
|
||||
LogManager.addLog("INFO", "CFG", "buildConfigText: exists=${cfgFile.exists()} srv=${existing.size}" +
|
||||
existing.joinToString(", ") { "${it.ifName}=${it.protocol}" })
|
||||
if (!cfgFile.exists() || existing.isEmpty()) {
|
||||
val detected = runBlocking { detectInterfaces() }
|
||||
val detected = runBlocking { detectInterfaces(emptyList(), protocols) }
|
||||
LogManager.addLog("WARN", "CFG", "buildConfigText: auto-detecting ${detected.size} interfaces")
|
||||
saveServersToConfig(detected)
|
||||
return buildConfigFromServers(detected)
|
||||
saveConfig(snapshot, detected, emptyList())
|
||||
return buildConfigFromServers(snapshot, detected)
|
||||
}
|
||||
return buildConfigFromServers(existing)
|
||||
return buildConfigFromServers(snapshot, existing)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user