done
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package com.example.netshareecohub.routing
|
||||
|
||||
import com.example.netshareecohub.data.logging.Logger
|
||||
|
||||
class RootRoutingManager(
|
||||
val shellExecutor: ShellExecutor = DefaultShellExecutor()
|
||||
) {
|
||||
private val tag = "RootRoutingManager"
|
||||
|
||||
@Volatile
|
||||
private var isRoutingActive = false
|
||||
|
||||
private val configuredRules = mutableListOf<String>()
|
||||
|
||||
fun isRootAvailable(): Boolean {
|
||||
return shellExecutor.isRootAvailable()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun startRouting(
|
||||
lanInterface: String,
|
||||
upstreamInterface: String,
|
||||
dnsServer: String = "8.8.8.8",
|
||||
gatewayIpCidr: String = "192.168.50.1/24",
|
||||
dhcpFallbackPort: Int = 6700
|
||||
): Boolean {
|
||||
if (!isRootAvailable()) {
|
||||
Logger.error(tag, "Root access is not available")
|
||||
return false
|
||||
}
|
||||
|
||||
Logger.info(tag, "Starting Root IP Tables routing ($lanInterface -> $upstreamInterface, Gateway IP: $gatewayIpCidr, DNS: $dnsServer)")
|
||||
|
||||
val commands = mutableListOf<String>()
|
||||
|
||||
// 1. Enable IP Forwarding
|
||||
commands.add("sysctl -w net.ipv4.ip_forward=1 || echo 1 > /proc/sys/net/ipv4/ip_forward")
|
||||
|
||||
// 2. Assign Gateway IP address to LAN Interface (eth0)
|
||||
commands.add("ip addr add $gatewayIpCidr dev $lanInterface 2>/dev/null || ip addr replace $gatewayIpCidr dev $lanInterface")
|
||||
commands.add("ip link set $lanInterface up")
|
||||
|
||||
// 3. iptables -t nat -A POSTROUTING -o <upstream> -j MASQUERADE
|
||||
val masqueradeCmd = "iptables -t nat -A POSTROUTING -o $upstreamInterface -j MASQUERADE"
|
||||
commands.add(masqueradeCmd)
|
||||
|
||||
// 4. iptables -A FORWARD -i <lan_iface> -o <upstream> -j ACCEPT
|
||||
val forwardLanUpstreamCmd = "iptables -A FORWARD -i $lanInterface -o $upstreamInterface -j ACCEPT"
|
||||
commands.add(forwardLanUpstreamCmd)
|
||||
|
||||
// 5. iptables -A FORWARD -i <upstream> -o <lan_iface> -m state --state RELATED,ESTABLISHED -j ACCEPT
|
||||
val forwardUpstreamLanCmd = "iptables -A FORWARD -i $upstreamInterface -o $lanInterface -m state --state RELATED,ESTABLISHED -j ACCEPT"
|
||||
commands.add(forwardUpstreamLanCmd)
|
||||
|
||||
// 6. Redirect DHCP port 67 UDP packets on lanInterface to local fallback port
|
||||
val dhcpRedirectCmd = "iptables -t nat -A PREROUTING -i $lanInterface -p udp --dport 67 -j REDIRECT --to-ports $dhcpFallbackPort"
|
||||
commands.add(dhcpRedirectCmd)
|
||||
|
||||
// 7. DNS DNAT Redirect if dnsServer is provided
|
||||
val dnsRedirectCmd = if (dnsServer.isNotBlank()) {
|
||||
"iptables -t nat -A PREROUTING -i $lanInterface -p udp --dport 53 -j DNAT --to-destination $dnsServer:53"
|
||||
} else null
|
||||
|
||||
dnsRedirectCmd?.let { commands.add(it) }
|
||||
|
||||
val result = shellExecutor.executeSu(commands)
|
||||
if (result.isSuccess) {
|
||||
configuredRules.clear()
|
||||
configuredRules.add(masqueradeCmd)
|
||||
configuredRules.add(forwardLanUpstreamCmd)
|
||||
configuredRules.add(forwardUpstreamLanCmd)
|
||||
configuredRules.add(dhcpRedirectCmd)
|
||||
dnsRedirectCmd?.let { configuredRules.add(it) }
|
||||
|
||||
isRoutingActive = true
|
||||
Logger.info(tag, "Root routing successfully configured and active. Output: ${result.stdout}")
|
||||
return true
|
||||
} else {
|
||||
Logger.error(tag, "Failed to configure root routing. Error output: ${result.stderr}")
|
||||
stopRouting(lanInterface, upstreamInterface, dnsServer, dhcpFallbackPort)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun stopRouting(
|
||||
lanInterface: String,
|
||||
upstreamInterface: String,
|
||||
dnsServer: String = "8.8.8.8",
|
||||
dhcpFallbackPort: Int = 6700
|
||||
): Boolean {
|
||||
Logger.info(tag, "Stopping Root routing ($lanInterface -> $upstreamInterface)")
|
||||
|
||||
val commands = mutableListOf<String>()
|
||||
|
||||
// Remove created rules using -D
|
||||
commands.add("iptables -t nat -D PREROUTING -i $lanInterface -p udp --dport 67 -j REDIRECT --to-ports $dhcpFallbackPort")
|
||||
commands.add("iptables -t nat -D POSTROUTING -o $upstreamInterface -j MASQUERADE")
|
||||
commands.add("iptables -D FORWARD -i $lanInterface -o $upstreamInterface -j ACCEPT")
|
||||
commands.add("iptables -D FORWARD -i $upstreamInterface -o $lanInterface -m state --state RELATED,ESTABLISHED -j ACCEPT")
|
||||
if (dnsServer.isNotBlank()) {
|
||||
commands.add("iptables -t nat -D PREROUTING -i $lanInterface -p udp --dport 53 -j DNAT --to-destination $dnsServer:53")
|
||||
}
|
||||
|
||||
shellExecutor.executeSu(commands)
|
||||
configuredRules.clear()
|
||||
isRoutingActive = false
|
||||
Logger.info(tag, "Root routing teardown completed")
|
||||
return true
|
||||
}
|
||||
|
||||
fun isRoutingActive(): Boolean = isRoutingActive
|
||||
|
||||
fun getConfiguredRules(): List<String> = configuredRules.toList()
|
||||
}
|
||||
Reference in New Issue
Block a user