Skip to content

Commit

Permalink
Fix Subnet Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
krxkli committed Sep 4, 2024
1 parent 9e62f9a commit 558a2a0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
Binary file modified Android/app/release/app-release.apk
Binary file not shown.
Binary file modified Android/app/release/baselineProfiles/0/app-release.dm
Binary file not shown.
Binary file modified Android/app/release/baselineProfiles/1/app-release.dm
Binary file not shown.
4 changes: 2 additions & 2 deletions Android/app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 3,
"versionName": "1.1.1",
"versionCode": 4,
"versionName": "1.1.2",
"outputFile": "app-release.apk"
}
],
Expand Down
51 changes: 46 additions & 5 deletions Android/app/src/main/java/com/example/scut_router/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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
Expand All @@ -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
}


Expand Down
3 changes: 2 additions & 1 deletion Android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
<string name="gateway">gateway</string>
<string name="dns_1" translatable="false">DNS 1</string>
<string name="dns_2" translatable="false">DNS 2</string>
<string name="default_router_conn" translatable="false">192.168.0.101</string>
<string name="default_router_conn" translatable="false">192.168.0.0</string>
<string name="validConnTitle">The router may not be connected</string>
<string name="validConnMsg">Whether to exit the application</string>
<string name="validConnYes">Yes</string>
<string name="validConnNo">No</string>
<string name="loadCore">Loading Core</string>
<string name="core_loaded_successfully">Core Loaded successfully</string>
<string name="wlanIfaName" translatable="false">wlan0</string>
</resources>

0 comments on commit 558a2a0

Please sign in to comment.