Skip to content

Commit

Permalink
Various bugfix for Wi-Fi direct mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Jan 4, 2018
1 parent 7e30ddf commit 62449a9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
39 changes: 20 additions & 19 deletions mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,9 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
val service get() = this@HotspotService
var data: MainActivity.Data? = null

fun shutdown() {
when (status) {
Status.ACTIVE_P2P -> p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener {
override fun onSuccess() = clean()
override fun onFailure(reason: Int) {
if (reason == WifiP2pManager.BUSY) clean() else { // assuming it's already gone
Toast.makeText(this@HotspotService, "Failed to remove P2P group (${formatReason(reason)})",
Toast.LENGTH_SHORT).show()
LocalBroadcastManager.getInstance(this@HotspotService)
.sendBroadcast(Intent(STATUS_CHANGED))
}
}
})
else -> clean()
}
fun shutdown() = when (status) {
Status.ACTIVE_P2P -> removeGroup()
else -> clean()
}
}

Expand Down Expand Up @@ -199,10 +187,10 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
return START_NOT_STICKY
}

private fun startFailure(msg: String) {
private fun startFailure(msg: String, group: WifiP2pGroup? = null) {
Toast.makeText(this@HotspotService, msg, Toast.LENGTH_SHORT).show()
showNotification()
clean()
if (group != null) removeGroup() else clean()
}
private fun doStart() = p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener {
override fun onFailure(reason: Int) = startFailure("Failed to create P2P group (${formatReason(reason)})")
Expand Down Expand Up @@ -234,15 +222,28 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
val routing = try {
Routing(upstream, downstream, owner)
} catch (_: Routing.InterfaceNotFoundException) {
startFailure(getString(R.string.exception_interface_not_found))
startFailure(getString(R.string.exception_interface_not_found), group)
return
}.p2pRule().forward().dnsRedirect(dns)
if (routing.start()) {
this.routing = routing
doStart(group)
} else startFailure("Something went wrong, please check logcat.")
} else startFailure("Something went wrong, please check logcat.", group)
}

private fun removeGroup() {
p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener {
override fun onSuccess() = clean()
override fun onFailure(reason: Int) {
if (reason == WifiP2pManager.BUSY) clean() else { // assuming it's already gone
Toast.makeText(this@HotspotService, "Failed to remove P2P group (${formatReason(reason)})",
Toast.LENGTH_SHORT).show()
status = Status.ACTIVE_P2P
LocalBroadcastManager.getInstance(this@HotspotService).sendBroadcast(Intent(STATUS_CHANGED))
}
}
})
}
private fun unregisterReceiver() {
if (receiverRegistered) {
unregisterReceiver(receiver)
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class MainActivity : AppCompatActivity(), ServiceConnection, Toolbar.OnMenuItemC
}
holder.binding.device = device
holder.binding.ipAddress = when (position) {
0 -> binder?.service?.routing?.hostAddress
0 -> binder?.service?.routing?.hostAddress?.hostAddress
else -> arpCache[device?.deviceAddress]
}
holder.binding.executePendingBindings()
Expand Down
18 changes: 14 additions & 4 deletions mobile/src/main/java/be/mygod/vpnhotspot/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,36 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress

class InterfaceNotFoundException : IOException()

val hostAddress: String
val hostAddress: InetAddress
private val subnetPrefixLength: Short
private val startScript = LinkedList<String>()
private val stopScript = LinkedList<String>()
init {
val address = NetworkInterface.getByName(downstream)?.interfaceAddresses
?.singleOrNull { if (ownerAddress == null) it.address is Inet4Address else it.address == ownerAddress }
?: throw InterfaceNotFoundException()
hostAddress = address.address.hostAddress
hostAddress = address.address
subnetPrefixLength = address.networkPrefixLength
}

fun p2pRule(): Routing {
// clear suffix bits
val address = hostAddress.address
var done = subnetPrefixLength.toInt()
while (done < address.size shl 3) {
val index = done shr 3
address[index] = (address[index].toInt() and (0x7f00 shr (done and 7))).toByte()
done = (index + 1) shl 3
}
val hostAddress = InetAddress.getByAddress(address).hostAddress
startScript.add("echo 1 >/proc/sys/net/ipv4/ip_forward") // Wi-Fi direct doesn't enable ip_forward
startScript.add("ip route add default dev $upstream scope link table 62")
startScript.add("ip route add $hostAddress/$subnetPrefixLength dev $downstream scope link table 62")
startScript.add("ip route add broadcast 255.255.255.255 dev $downstream scope link table 62")
startScript.add("ip rule add iif $downstream lookup 62")
stopScript.addFirst("ip route del default dev $upstream scope link table 62")
stopScript.addFirst("ip route del $hostAddress/$subnetPrefixLength dev $downstream scope link table 62")
stopScript.addFirst("ip route del broadcast 255.255.255.255 dev $downstream scope link table 62")
// removing each rule may fail if downstream is already removed
stopScript.addFirst("ip route flush table 62")
stopScript.addFirst("ip rule del iif $downstream lookup 62")
return this
}
Expand All @@ -65,6 +74,7 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
}

fun dnsRedirect(dns: String): Routing {
val hostAddress = hostAddress.hostAddress
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
stopScript.addFirst("iptables -t nat -D PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
Expand Down
4 changes: 2 additions & 2 deletions mobile/src/main/java/be/mygod/vpnhotspot/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fun Bundle.put(key: String, map: Array<String>): Bundle {
return this
}

const val NOISYSU_TAG = "NoisySU"
const val NOISYSU_SUFFIX = "SUCCESS\n"
private const val NOISYSU_TAG = "NoisySU"
private const val NOISYSU_SUFFIX = "SUCCESS\n"
fun loggerSuStream(command: String): InputStream {
val process = ProcessBuilder("su", "-c", command)
.redirectErrorStream(true)
Expand Down

0 comments on commit 62449a9

Please sign in to comment.