forked from ReVanced/revanced-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: keystore import/export (ReVanced#30)
- Loading branch information
Showing
11 changed files
with
266 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package app.revanced.manager.di | ||
|
||
import app.revanced.manager.patcher.SignerService | ||
import app.revanced.manager.domain.manager.KeystoreManager | ||
import app.revanced.manager.util.PM | ||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val managerModule = module { | ||
singleOf(::SignerService) | ||
singleOf(::KeystoreManager) | ||
singleOf(::PM) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/src/main/java/app/revanced/manager/domain/manager/KeystoreManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package app.revanced.manager.domain.manager | ||
|
||
import android.app.Application | ||
import app.revanced.manager.util.signing.Signer | ||
import app.revanced.manager.util.signing.SigningOptions | ||
import java.io.File | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.nio.file.Files | ||
import java.nio.file.StandardCopyOption | ||
import kotlin.io.path.exists | ||
|
||
class KeystoreManager(app: Application, private val prefs: PreferencesManager) { | ||
companion object { | ||
/** | ||
* Default common name and password for the keystore. | ||
*/ | ||
const val DEFAULT = "ReVanced" | ||
|
||
/** | ||
* The default password used by the Flutter version. | ||
*/ | ||
const val FLUTTER_MANAGER_PASSWORD = "s3cur3p@ssw0rd" | ||
} | ||
|
||
private val keystorePath = app.dataDir.resolve("manager.keystore").toPath() | ||
private fun options( | ||
cn: String = prefs.keystoreCommonName!!, | ||
pass: String = prefs.keystorePass!! | ||
) = SigningOptions(cn, pass, keystorePath) | ||
|
||
private fun updatePrefs(cn: String, pass: String) { | ||
prefs.keystoreCommonName = cn | ||
prefs.keystorePass = pass | ||
} | ||
|
||
fun sign(input: File, output: File) = Signer(options()).signApk(input, output) | ||
|
||
init { | ||
if (!keystorePath.exists()) { | ||
regenerate() | ||
} | ||
} | ||
|
||
fun regenerate() = Signer(options(DEFAULT, DEFAULT)).regenerateKeystore().also { | ||
updatePrefs(DEFAULT, DEFAULT) | ||
} | ||
|
||
fun import(cn: String, pass: String, keystore: InputStream) { | ||
// TODO: check if the user actually provided the correct password | ||
Files.copy(keystore, keystorePath, StandardCopyOption.REPLACE_EXISTING) | ||
|
||
updatePrefs(cn, pass) | ||
} | ||
|
||
fun export(target: OutputStream) { | ||
Files.copy(keystorePath, target) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
app/src/main/java/app/revanced/manager/patcher/SignerService.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/app/revanced/manager/ui/viewmodel/ImportExportViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app.revanced.manager.ui.viewmodel | ||
|
||
|
||
import android.app.Application | ||
import android.net.Uri | ||
import androidx.lifecycle.ViewModel | ||
import app.revanced.manager.R | ||
import app.revanced.manager.domain.manager.KeystoreManager | ||
import app.revanced.manager.util.toast | ||
|
||
class ImportExportViewModel(private val app: Application, private val keystoreManager: KeystoreManager) : ViewModel() { | ||
private val contentResolver = app.contentResolver | ||
|
||
fun import(content: Uri, cn: String, pass: String) = | ||
keystoreManager.import(cn, pass, contentResolver.openInputStream(content)!!) | ||
|
||
fun export(target: Uri) = keystoreManager.export(contentResolver.openOutputStream(target)!!) | ||
|
||
fun regenerate() = keystoreManager.regenerate().also { | ||
app.toast(app.getString(R.string.regenerate_keystore_success)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.