diff --git a/Android/app/build.gradle.kts b/Android/app/build.gradle.kts index c96c5a4..0abadac 100644 --- a/Android/app/build.gradle.kts +++ b/Android/app/build.gradle.kts @@ -11,8 +11,8 @@ android { applicationId = "com.krxkli.scut_router" minSdk = 24 targetSdk = 34 - versionCode = 3 - versionName = "1.1.1" + versionCode = 4 + versionName = "1.1.2" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" @@ -63,6 +63,7 @@ dependencies { implementation(libs.androidx.preference) implementation(libs.androidx.activity) implementation(libs.androidx.legacy.support.v4) + implementation("commons-net:commons-net:3.11.1") testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) diff --git a/Android/app/release/app-release.apk b/Android/app/release/app-release.apk index c5f849b..3a4fd94 100644 Binary files a/Android/app/release/app-release.apk and b/Android/app/release/app-release.apk differ diff --git a/Android/app/release/baselineProfiles/0/app-release.dm b/Android/app/release/baselineProfiles/0/app-release.dm index 202b5df..d65c5e9 100644 Binary files a/Android/app/release/baselineProfiles/0/app-release.dm and b/Android/app/release/baselineProfiles/0/app-release.dm differ diff --git a/Android/app/release/baselineProfiles/1/app-release.dm b/Android/app/release/baselineProfiles/1/app-release.dm index 5f0568a..de07cbb 100644 Binary files a/Android/app/release/baselineProfiles/1/app-release.dm and b/Android/app/release/baselineProfiles/1/app-release.dm differ diff --git a/Android/app/release/output-metadata.json b/Android/app/release/output-metadata.json index 8edb9e6..2bcc6c6 100644 --- a/Android/app/release/output-metadata.json +++ b/Android/app/release/output-metadata.json @@ -11,8 +11,8 @@ "type": "SINGLE", "filters": [], "attributes": [], - "versionCode": 3, - "versionName": "1.1.1", + "versionCode": 4, + "versionName": "1.1.2", "outputFile": "app-release.apk" } ], diff --git a/Android/app/src/main/java/com/example/scut_router/MainActivity.kt b/Android/app/src/main/java/com/example/scut_router/MainActivity.kt index 284b7fd..9392281 100644 --- a/Android/app/src/main/java/com/example/scut_router/MainActivity.kt +++ b/Android/app/src/main/java/com/example/scut_router/MainActivity.kt @@ -17,6 +17,9 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import com.example.scut_router.databinding.ActivityMainBinding +import java.net.Inet4Address +import java.net.NetworkInterface +import org.apache.commons.net.util.SubnetUtils class MainActivity : AppCompatActivity() { @@ -89,10 +92,26 @@ class MainActivity : AppCompatActivity() { Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath initLibSSHCommand(downloadPath) - Toast.makeText(this, getString(R.string.core_loaded_successfully), Toast.LENGTH_SHORT).show() + Toast.makeText(this, getString(R.string.core_loaded_successfully), Toast.LENGTH_SHORT) + .show() + } + + private fun htonl(value: Int): Int { + return (value shl 24) or + ((value and 0x00FF0000) shr 8) or + ((value and 0x0000FF00) shl 8) or + (value shr 24) + } + + private fun ntohl(value: Int): Int { + return (value shl 24) or + ((value and 0x00FF0000) shr 8) or + ((value and 0x0000FF00) shl 8) or + (value shr 24) } private fun checkIsValidConn(): Boolean { + // 分析是否与 已知子网 吻合 // 检查是否正确连接路由器 val context: Context = this val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager @@ -102,11 +121,33 @@ class MainActivity : AppCompatActivity() { val ipStr = Formatter.formatIpAddress(ipAddr) println("IP 地址: $ipStr") - if (ipStr != this.getString(R.string.default_router_conn)) { - return false - } else { - return true + val interfaces = NetworkInterface.getNetworkInterfaces() + while (interfaces.hasMoreElements()) { + val ifa = interfaces.nextElement() + if (!ifa.isUp || ifa.isLoopback) { + continue + } + if (ifa.name == getString(R.string.wlanIfaName)) { + val addresses = ifa.interfaceAddresses + for (addr in addresses) { + if (addr.address is Inet4Address) { + val prefixLen = addr.networkPrefixLength + val subnetUtils = SubnetUtils("$ipStr/$prefixLen") + val subNet = subnetUtils.info + + println("IP 子网: ${subNet.networkAddress}") + + if (subNet.networkAddress == this.getString(R.string.default_router_conn)) { + return true + } else { + return false + } + } + } + } } + + return false } diff --git a/Android/app/src/main/res/values/strings.xml b/Android/app/src/main/res/values/strings.xml index d498545..dd7a4b2 100644 --- a/Android/app/src/main/res/values/strings.xml +++ b/Android/app/src/main/res/values/strings.xml @@ -19,11 +19,12 @@ gateway DNS 1 DNS 2 - 192.168.0.101 + 192.168.0.0 The router may not be connected Whether to exit the application Yes No Loading Core Core Loaded successfully + wlan0 \ No newline at end of file