SpoofedSimInfoRepository.kt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package com.example.modifier.repo
  2. import android.Manifest
  3. import android.annotation.SuppressLint
  4. import android.content.Context
  5. import android.telephony.SubscriptionManager
  6. import android.util.Log
  7. import androidx.core.content.ContextCompat
  8. import androidx.datastore.preferences.core.booleanPreferencesKey
  9. import androidx.datastore.preferences.core.edit
  10. import androidx.datastore.preferences.core.stringPreferencesKey
  11. import androidx.datastore.preferences.preferencesDataStore
  12. import com.example.modifier.BuildConfig
  13. import com.example.modifier.Utils
  14. import com.example.modifier.constants.PACKAGE_GMS
  15. import com.example.modifier.constants.PACKAGE_MESSAGING
  16. import com.example.modifier.model.SpoofedSimInfo
  17. import com.example.modifier.serializer.Json
  18. import com.example.modifier.utils.genICCID
  19. import com.example.modifier.utils.genIMEI
  20. import com.example.modifier.utils.genIMSI
  21. import com.example.modifier.utils.hasPermission
  22. import com.example.modifier.utils.isOldVersion
  23. import com.example.modifier.utils.resumePackage
  24. import com.example.modifier.utils.shellRun
  25. import com.example.modifier.utils.suspendPackage
  26. import com.google.gson.Gson
  27. import kotlinx.coroutines.CoroutineScope
  28. import kotlinx.coroutines.flow.map
  29. import kotlinx.coroutines.flow.stateIn
  30. import kotlinx.serialization.decodeFromString
  31. import org.apache.commons.io.FileUtils
  32. import java.io.File
  33. import kotlin.coroutines.coroutineContext
  34. val Context.simInfoDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.simInfo")
  35. class SpoofedSimInfoRepository(private val context: Context) {
  36. private object PreferencesKeys {
  37. val NUMBER = stringPreferencesKey("number")
  38. val MCC = stringPreferencesKey("mcc")
  39. val MNC = stringPreferencesKey("mnc")
  40. val ICCID = stringPreferencesKey("iccid")
  41. val IMSI = stringPreferencesKey("imsi")
  42. val IMEI = stringPreferencesKey("imei")
  43. val COUNTRY = stringPreferencesKey("country")
  44. val AREA_CODE = stringPreferencesKey("area_code")
  45. val AVAILABLE = booleanPreferencesKey("available")
  46. val CARRIER_ID = stringPreferencesKey("carrier_id")
  47. val CARRIER_NAME = stringPreferencesKey("carrier_name")
  48. }
  49. val simInfoFlow = context.simInfoDataStore.data.map {
  50. val file = File(ContextCompat.getDataDir(context), "config.json")
  51. val old = file.exists().let {
  52. if (it) {
  53. try {
  54. return@let Json.decodeFromString<SpoofedSimInfo>(
  55. FileUtils.readFileToString(
  56. file,
  57. "UTF-8"
  58. )
  59. )
  60. } catch (e: Exception) {
  61. e.printStackTrace()
  62. }
  63. }
  64. null
  65. }
  66. val number = it[PreferencesKeys.NUMBER] ?: old?.number ?: ""
  67. val mcc = it[PreferencesKeys.MCC] ?: old?.mcc ?: ""
  68. val mnc = it[PreferencesKeys.MNC] ?: old?.mnc ?: ""
  69. val iccid = it[PreferencesKeys.ICCID] ?: old?.iccid ?: ""
  70. val imsi = it[PreferencesKeys.IMSI] ?: old?.imsi ?: ""
  71. val imei = it[PreferencesKeys.IMEI] ?: old?.imei ?: ""
  72. val country = it[PreferencesKeys.COUNTRY] ?: old?.country ?: ""
  73. val areaCode = it[PreferencesKeys.AREA_CODE] ?: old?.areaCode ?: ""
  74. val available = it[PreferencesKeys.AVAILABLE] ?: old?.available ?: false
  75. val carrierId = it[PreferencesKeys.CARRIER_ID] ?: old?.carrierId ?: ""
  76. val carrierName = it[PreferencesKeys.CARRIER_NAME] ?: old?.carrierName ?: ""
  77. SpoofedSimInfo(
  78. number = number,
  79. mcc = mcc,
  80. mnc = mnc,
  81. iccid = iccid,
  82. imsi = imsi,
  83. imei = imei,
  84. country = country,
  85. areaCode = areaCode,
  86. available = available,
  87. carrierId = carrierId,
  88. carrierName = carrierName
  89. )
  90. }
  91. suspend fun stateFlow() = simInfoFlow.stateIn(CoroutineScope(coroutineContext))
  92. suspend fun value() {
  93. }
  94. @SuppressLint("MissingPermission")
  95. suspend fun updateSpoofedSimInfo(spoofedSimInfo: SpoofedSimInfo, suspend: Boolean? = true) {
  96. context.simInfoDataStore.edit {
  97. it[PreferencesKeys.NUMBER] = spoofedSimInfo.number
  98. it[PreferencesKeys.MCC] = spoofedSimInfo.mcc
  99. it[PreferencesKeys.MNC] = spoofedSimInfo.mnc
  100. it[PreferencesKeys.ICCID] = spoofedSimInfo.iccid
  101. it[PreferencesKeys.IMSI] = spoofedSimInfo.imsi
  102. it[PreferencesKeys.IMEI] = spoofedSimInfo.imei
  103. it[PreferencesKeys.COUNTRY] = spoofedSimInfo.country
  104. it[PreferencesKeys.AREA_CODE] = spoofedSimInfo.areaCode
  105. it[PreferencesKeys.AVAILABLE] = spoofedSimInfo.available
  106. it[PreferencesKeys.CARRIER_ID] = spoofedSimInfo.carrierId
  107. it[PreferencesKeys.CARRIER_NAME] = spoofedSimInfo.carrierName
  108. }
  109. try {
  110. if (suspend == true) {
  111. suspendPackage(PACKAGE_GMS, PACKAGE_MESSAGING)
  112. }
  113. shellRun(
  114. "setprop persist.spoof.number ${spoofedSimInfo.number}",
  115. "setprop persist.spoof.mcc ${spoofedSimInfo.mcc}",
  116. "setprop persist.spoof.mnc ${spoofedSimInfo.mnc}",
  117. "setprop persist.spoof.iccid ${spoofedSimInfo.iccid}",
  118. "setprop persist.spoof.imsi ${spoofedSimInfo.imsi}",
  119. "setprop persist.spoof.imei ${spoofedSimInfo.imei}",
  120. "setprop persist.spoof.country ${spoofedSimInfo.country}",
  121. "setprop persist.spoof.carrier.id ${
  122. spoofedSimInfo.carrierId.replace(
  123. "^\\W*\$".toRegex(),
  124. "''"
  125. )
  126. }",
  127. "setprop persist.spoof.carrier.name ${
  128. spoofedSimInfo.carrierName.replace(
  129. "^\\W*\$".toRegex(),
  130. "''"
  131. )
  132. }",
  133. )
  134. val context = Utils.getContext()
  135. val subscriptionManager: SubscriptionManager =
  136. context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
  137. if (hasPermission(Manifest.permission.READ_PHONE_STATE)) {
  138. val simCount = subscriptionManager.activeSubscriptionInfoCountMax
  139. for (i in 0 until simCount) {
  140. val info = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(i)
  141. if (info != null) {
  142. val mcc = info.mccString
  143. val mnc = info.mncString
  144. Log.i(com.example.modifier.TAG, "mccmnc spoofed: $mcc$mnc")
  145. }
  146. }
  147. }
  148. if (suspend == true) {
  149. resumePackage(PACKAGE_GMS, PACKAGE_MESSAGING)
  150. }
  151. } catch (e: Exception) {
  152. e.printStackTrace()
  153. }
  154. }
  155. suspend fun mock() {
  156. if (isOldVersion(context)) {
  157. val content = Utils.getContext().assets.open("us_numbers.txt").bufferedReader().use {
  158. it.readText()
  159. }
  160. // get random number
  161. content.split("\n")
  162. .filter { it.isNotBlank() }
  163. .shuffled()
  164. .firstOrNull()
  165. ?.let {
  166. updateSpoofedSimInfo(
  167. SpoofedSimInfo(
  168. number = it,
  169. mcc = "310",
  170. mnc = "150",
  171. iccid = genICCID("310", "1"),
  172. imsi = genIMSI("310150"),
  173. imei = genIMEI(),
  174. country = "us",
  175. areaCode = "1",
  176. available = false,
  177. carrierId = "1779",
  178. carrierName = "Cricket Wireless"
  179. )
  180. )
  181. }
  182. } else {
  183. val content = Utils.getContext().assets.open("us_numbers.txt").bufferedReader().use {
  184. it.readText()
  185. }
  186. // get random number
  187. content.split("\n")
  188. .filter { it.isNotBlank() }
  189. .shuffled()
  190. .firstOrNull()
  191. ?.let {
  192. updateSpoofedSimInfo(
  193. SpoofedSimInfo(
  194. number = it,
  195. mcc = "310",
  196. mnc = "240",
  197. iccid = genICCID("310", "1"),
  198. imsi = genIMSI("310240"),
  199. imei = genIMEI(),
  200. country = "us",
  201. areaCode = "1",
  202. available = false,
  203. carrierId = "1",
  204. carrierName = "T-Mobile"
  205. )
  206. )
  207. }
  208. }
  209. }
  210. }