From fd99d655a04fb0c8e4e8b71e5e8e09e5165bfc54 Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Tue, 20 Dec 2022 09:57:32 +0100 Subject: [PATCH] Fix http + Add regex --- app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 3 +- .../com/gaurav/avnc/ui/ldc/KioskActivity.kt | 42 ++++++++++++++++--- .../main/res/xml/network_security_config.xml | 6 +++ 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/xml/network_security_config.xml diff --git a/app/build.gradle b/app/build.gradle index 89631ba..aa4a061 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,11 +9,11 @@ android { defaultConfig { applicationId "com.ldc.kiosk" - minSdkVersion 21 + minSdkVersion 24 targetSdkVersion 30 versionCode 14 //versionName "1.9.2" - versionName "0.0.1" + versionName "0.0.3" javaCompileOptions { annotationProcessorOptions { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 02f456b..fabb885 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,7 +15,8 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/AppTheme"> + android:theme="@style/AppTheme" + android:networkSecurityConfig="@xml/network_security_config"> diff --git a/app/src/main/java/com/gaurav/avnc/ui/ldc/KioskActivity.kt b/app/src/main/java/com/gaurav/avnc/ui/ldc/KioskActivity.kt index 2075000..90a3957 100644 --- a/app/src/main/java/com/gaurav/avnc/ui/ldc/KioskActivity.kt +++ b/app/src/main/java/com/gaurav/avnc/ui/ldc/KioskActivity.kt @@ -38,6 +38,8 @@ import java.io.BufferedReader import java.io.IOException import java.io.Reader import kotlin.collections.ArrayList +import java.util.regex.Matcher +import java.util.regex.Pattern class KioskActivity : AppCompatActivity(), LifecycleObserver { @@ -137,14 +139,14 @@ class KioskActivity : AppCompatActivity(), LifecycleObserver { for (csvRecord: CSVRecord in parser) { val identifier = csvRecord.get(0); // Si le premier caractère est un dieze la ligne est commentée donc on passe - if (identifier[0] == '#') { + if (identifier[0] == '#' || identifier.isEmpty()) { continue; } - val ip = csvRecord.get(3); - val port = csvRecord.get(4); // On test si l´Identifiant TID correspond à celle du Device if (identifier.length == 11 && identifier.equals(getIdentifier())) { - if (ip!!.isEmpty()) { + val ip = csvRecord.get(3); + val port = csvRecord.get(4); + if (ip!!.isEmpty() || !isValidIpAddress(ip)) { runOnUiThread(Runnable { Toast.makeText( context, @@ -155,7 +157,7 @@ class KioskActivity : AppCompatActivity(), LifecycleObserver { manageButtons("enable") return } - if (port!!.isEmpty()) { + if (port!!.isEmpty() || !isValidPort(port)) { runOnUiThread(Runnable { Toast.makeText( context, @@ -391,4 +393,34 @@ class KioskActivity : AppCompatActivity(), LifecycleObserver { return networkInfo.isConnected } } + + private fun isValidIpAddress(str: String): Boolean { + if (str.isEmpty()) + { + return false + } + // Regex for IPV4 and IPV6 + val regex: String = + "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.(?!\$)|\$)){4}$" + val p: Pattern = Pattern.compile(regex) + val m: Matcher = p.matcher(str) + return m.matches() + } + + private fun isValidPort(str: String): Boolean { + if (str.isEmpty()) + { + return false + } + try { + str.toInt(); + } catch (e: NumberFormatException) { + return false + } + val regex: String = + "^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))\$" + val p: Pattern = Pattern.compile(regex) + val m: Matcher = p.matcher(str) + return m.matches() + } } \ No newline at end of file diff --git a/app/src/main/res/xml/network_security_config.xml b/app/src/main/res/xml/network_security_config.xml new file mode 100644 index 0000000..603f317 --- /dev/null +++ b/app/src/main/res/xml/network_security_config.xml @@ -0,0 +1,6 @@ + + + + localtid + + \ No newline at end of file