Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Feature: Support global account login
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKongA committed Dec 23, 2023
1 parent d0d88ea commit b3aa536
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
20 changes: 12 additions & 8 deletions app/src/main/java/top/yukonga/update/logic/utils/InfoUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,43 @@ import java.util.Base64

object InfoUtils {

private const val recoveryUrl = "https://update.miui.com/updates/miotaV3.php"
private const val cnRecoveryUrl = "https://update.miui.com/updates/miotaV3.php"
private const val intlRecoveryUrl = "https://update.intl.miui.com/updates/miotaV3.php"
private const val fastbootUrl = "https://update.miui.com/updates/miota-fullrom.php"
private var securityKey = "miuiotavalided11"

private fun generateJson(device: String, version: String, android: String, userId: String): String {
private fun generateJson(device: String, region: String, version: String, android: String, userId: String): String {
val data = mutableMapOf<String, Any>()
data["id"] = userId
data["c"] = android
data["d"] = device
data["f"] = "1"
data["ov"] = version
data["l"] = if (!device.contains("_global")) "zh_CN" else "en_US"
data["r"] = if (!device.contains("_global")) "CN" else "GL"
data["r"] = region.replace("GL", "MI").replace("EEA", "EU")
data["v"] = "miui-${version}"
return Gson().toJson(data)
}

fun getRecoveryRomInfo(context: Context, codename: String, romVersion: String, androidVersion: String): String {
fun getRecoveryRomInfo(context: Context, codeName: String, regionCode: String, romVersion: String, androidVersion: String): String {
var userId = ""
var accountType = "CN"
var securityKey = securityKey.toByteArray(Charsets.UTF_8)
var serviceToken = ""
var port = "1"
val cookiesFile = readFile(context, "cookies.json")
if (cookiesFile.isNotEmpty()) {
val cookies = Gson().fromJson(cookiesFile, Map::class.java)
userId = cookies["userId"] as String
securityKey = Base64.getDecoder().decode((cookies["ssecurity"] as String))
serviceToken = cookies["serviceToken"] as String
userId = cookies["userId"].toString()
accountType = if (cookies["accountType"].toString() != "") cookies["accountType"].toString() else "CN"
securityKey = Base64.getDecoder().decode((cookies["ssecurity"].toString()))
serviceToken = cookies["serviceToken"].toString()
port = "2"
}
val jsonData = generateJson(codename, romVersion, androidVersion, userId)
val jsonData = generateJson(codeName, regionCode, romVersion, androidVersion, userId)
val encryptedText = miuiEncrypt(jsonData, securityKey)
val requestBody = FormBody.Builder().add("q", encryptedText).add("t", serviceToken).add("s", port).build()
val recoveryUrl = if (accountType == "GL") intlRecoveryUrl else cnRecoveryUrl
val postRequest = postRequest(recoveryUrl, requestBody)
val requestedEncryptedText = postRequest.body?.string() ?: ""
return miuiDecrypt(requestedEncryptedText, securityKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LoginUtils {
private val mediaType = "application/x-www-form-urlencoded".toMediaType()
private val gson = GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER).disableHtmlEscaping().create()

suspend fun login(context: Context, account: String, password: String): Boolean {
suspend fun login(context: Context, account: String, password: String, global: String): Boolean {
withContext(Dispatchers.Main) {
if (account.isEmpty() || password.isEmpty()) {
showStringToast(context, context.getString(R.string.account_or_password_empty), 0)
Expand All @@ -44,7 +44,9 @@ class LoginUtils {
return false
}

val data = "_json=true&bizDeviceType=&user=$account&hash=$passwordHash&sid=miuiromota&_sign=$_sign&_locale=zh_CN"
val sid = if (global == "1") "miuiota_intl" else "miuiromota"
val _locale = if (global == "1") "en_US" else "zh_CN"
val data = "_json=true&bizDeviceType=&user=$account&hash=$passwordHash&sid=$sid&_sign=$_sign&_locale=$_locale"
val requestBody = data.toRequestBody(mediaType)
val response2 = postRequest(loginAuth2Url, requestBody)

Expand All @@ -54,6 +56,7 @@ class LoginUtils {
val ssecurity = auth["ssecurity"].toString()
val location = auth["location"].toString()
val userId = auth["userId"].toString()
val accountType = if (global == "1") "GL" else "CN"

if (description != "成功") {
withContext(Dispatchers.Main) {
Expand All @@ -79,6 +82,7 @@ class LoginUtils {

val json = mutableMapOf<String, String>()
json["description"] = description
json["accountType"] = accountType
json["userId"] = userId
json["ssecurity"] = ssecurity
json["serviceToken"] = serviceToken
Expand Down
25 changes: 24 additions & 1 deletion app/src/main/java/top/yukonga/update/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.core.view.isVisible
import androidx.preference.PreferenceManager
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.materialswitch.MaterialSwitch
import com.google.android.material.textfield.MaterialAutoCompleteTextView
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
Expand Down Expand Up @@ -240,6 +241,7 @@ class MainActivity : AppCompatActivity() {
val recoveryRomInfo = InfoUtils.getRecoveryRomInfo(
this@MainActivity,
codeNameTextExtR,
regionsText,
systemVersionTextExt,
androidVersionText
).parseJSON<RecoveryRomInfoHelper.RomInfo>()
Expand Down Expand Up @@ -378,21 +380,42 @@ class MainActivity : AppCompatActivity() {
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
orientation = LinearLayout.VERTICAL
}
val switch = MaterialSwitch(this@MainActivity).apply {
text = prefs.getString("global", "")?.let {
if (it == "1") getString(R.string.global) else getString(R.string.china)
} ?: getString(R.string.china)
isChecked = prefs.getString("global", "") == "1"
textSize = 16f
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply {
setMargins(28.dp, 8.dp, 28.dp, 0.dp)
}
}
switch.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
buttonView.text = getString(R.string.global)
prefs.edit().putString("global", "1").apply()
} else {
buttonView.text = getString(R.string.china)
prefs.edit().putString("global", "0").apply()
}
}
val inputAccountLayout = createTextInputLayout(getString(R.string.account))
val inputAccount = createTextInputEditText()
inputAccountLayout.addView(inputAccount)
val inputPasswordLayout = createTextInputLayout(getString(R.string.password), TextInputLayout.END_ICON_PASSWORD_TOGGLE)
val inputPassword = createTextInputEditText(InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD)
inputPasswordLayout.addView(inputPassword)
view.addView(switch)
view.addView(inputAccountLayout)
view.addView(inputPasswordLayout)
val builder = MaterialAlertDialogBuilder(this@MainActivity)
builder.setTitle(getString(R.string.login)).setView(view).setNegativeButton(getString(R.string.cancel)) { dialog, _ -> dialog.dismiss() }
builder.setPositiveButton(getString(R.string.login)) { _, _ ->
val global = prefs.getString("global", "") ?: ""
val mInputAccount = inputAccount.text.toString()
val mInputPassword = inputPassword.text.toString()
CoroutineScope(Dispatchers.Default).launch {
val isValid = LoginUtils().login(this@MainActivity, mInputAccount, mInputPassword)
val isValid = LoginUtils().login(this@MainActivity, mInputAccount, mInputPassword, global)
if (isValid) {
withContext(Dispatchers.Main) {
mainContentBinding.apply {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
<string name="filemd5">文件MD5</string>
<string name="regions_code">区域代号</string>
<string name="login_expired">检测到登录已过期,建议重新登录</string>
<string name="china">国区账号</string>
<string name="global">海外账号</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
<string name="filemd5">檔案MD5</string>
<string name="regions_code">區域代號</string>
<string name="login_expired">偵測到登陸已過期,建議重新登陸</string>
<string name="china">國區帳戶</string>
<string name="global">海外帳戶</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@
<string name="filemd5">File MD5</string>
<string name="regions_code">Regions code</string>
<string name="login_expired">Checked that the login has expired. It is recommended to log in again</string>
<string name="china">Chinese account</string>
<string name="global">Global account</string>
</resources>

0 comments on commit b3aa536

Please sign in to comment.