owenclave / app/src/main/java/io/nekohasekai/sagernet/ui/profile/SSHSettingsActivity.kt

github - dev - back to tree - upstream
/****************************************************************************** * * * Copyright (C) 2021 by nekohasekai * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * * * ******************************************************************************/ package io.nekohasekai.sagernet.ui.profile import android.os.Bundle import androidx.preference.EditTextPreference import androidx.preference.ListPreference import androidx.preference.PreferenceFragmentCompat import io.nekohasekai.sagernet.Key import io.nekohasekai.sagernet.R import io.nekohasekai.sagernet.database.DataStore import io.nekohasekai.sagernet.database.preference.EditTextPreferenceModifiers import io.nekohasekai.sagernet.fmt.ssh.SSHBean import io.nekohasekai.sagernet.ktx.unwrapIDN class SSHSettingsActivity : ProfileSettingsActivity() { override fun createEntity() = SSHBean() override fun SSHBean.init() { DataStore.profileName = name DataStore.serverAddress = serverAddress DataStore.serverPort = serverPort DataStore.serverUsername = username DataStore.serverAuthType = authType DataStore.serverPassword = password DataStore.serverPrivateKey = privateKey DataStore.serverPassword1 = privateKeyPassphrase DataStore.serverCertificates = publicKey DataStore.serverSSHKeepaliveInterval = keepaliveInterval } override fun SSHBean.serialize() { name = DataStore.profileName serverAddress = DataStore.serverAddress.unwrapIDN() serverPort = DataStore.serverPort username = DataStore.serverUsername authType = DataStore.serverAuthType when (authType) { SSHBean.AUTH_TYPE_NONE -> { } SSHBean.AUTH_TYPE_PASSWORD -> { password = DataStore.serverPassword } SSHBean.AUTH_TYPE_PUBLIC_KEY -> { privateKey = DataStore.serverPrivateKey privateKeyPassphrase = DataStore.serverPassword1 } } publicKey = DataStore.serverCertificates keepaliveInterval = DataStore.serverSSHKeepaliveInterval } override fun PreferenceFragmentCompat.createPreferences( savedInstanceState: Bundle?, rootKey: String?, ) { addPreferencesFromResource(R.xml.ssh_preferences) findPreference(Key.SERVER_PORT)!!.apply { setOnBindEditTextListener(EditTextPreferenceModifiers.Port) } val password = findPreference(Key.SERVER_PASSWORD)!!.apply { summaryProvider = PasswordSummaryProvider } val privateKey = findPreference(Key.SERVER_PRIVATE_KEY)!!.apply { summaryProvider = PasswordSummaryProvider } val privateKeyPassphrase = findPreference(Key.SERVER_PASSWORD1)!!.apply { summaryProvider = PasswordSummaryProvider } findPreference(Key.SERVER_SSH_KEEPALIVE_INTERVAL)!!.apply { setOnBindEditTextListener(EditTextPreferenceModifiers.Number) } val authType = findPreference(Key.SERVER_AUTH_TYPE)!! fun updateAuthType(type: Int = DataStore.serverAuthType) { password.isVisible = type == SSHBean.AUTH_TYPE_PASSWORD privateKey.isVisible = type == SSHBean.AUTH_TYPE_PUBLIC_KEY privateKeyPassphrase.isVisible = type == SSHBean.AUTH_TYPE_PUBLIC_KEY } updateAuthType() authType.setOnPreferenceChangeListener { _, newValue -> updateAuthType((newValue as String).toInt()) true } } }