Auto detect prefix length as well
Unfortunately NetworkInterface requires INTERNET permission. (this can actually be bypassed very easily but let's prefer usage of public API)
This commit is contained in:
@@ -16,6 +16,7 @@ import android.support.v4.content.LocalBroadcastManager
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import java.net.NetworkInterface
|
||||
|
||||
class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
||||
companion object {
|
||||
@@ -63,26 +64,7 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
||||
val info = intent.getParcelableExtra<WifiP2pInfo>(WifiP2pManager.EXTRA_WIFI_P2P_INFO)
|
||||
val net = intent.getParcelableExtra<NetworkInfo>(WifiP2pManager.EXTRA_NETWORK_INFO)
|
||||
val group = intent.getParcelableExtra<WifiP2pGroup>(WifiP2pManager.EXTRA_WIFI_P2P_GROUP)
|
||||
val hostAddress = info.groupOwnerAddress?.hostAddress
|
||||
val downstream = group.`interface`
|
||||
if (info.groupFormed && info.isGroupOwner &&
|
||||
downstream != null && hostAddress != null && this.downstream == null) {
|
||||
this.downstream = downstream
|
||||
this.hostAddress = hostAddress
|
||||
if (noisySu("echo 1 >/proc/sys/net/ipv4/ip_forward",
|
||||
"ip route add default dev $upstream scope link table 62",
|
||||
"ip route add $hostAddress/$subnetPrefixLength dev $downstream scope link table 62",
|
||||
"ip route add broadcast 255.255.255.255 dev $downstream scope link table 62",
|
||||
"ip rule add from $hostAddress/$subnetPrefixLength lookup 62",
|
||||
"iptables -N vpnhotspot_fwd",
|
||||
"iptables -A vpnhotspot_fwd -i $upstream -o $downstream -m state --state ESTABLISHED,RELATED -j ACCEPT",
|
||||
"iptables -A vpnhotspot_fwd -i $downstream -o $upstream -j ACCEPT",
|
||||
"iptables -I FORWARD -j vpnhotspot_fwd",
|
||||
"iptables -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns",
|
||||
"iptables -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")) {
|
||||
doStart(group)
|
||||
} else startFailure("Something went wrong, please check logcat.")
|
||||
}
|
||||
if (downstream == null) onGroupCreated(info, group)
|
||||
this@HotspotService.group = group
|
||||
binder.data?.onGroupChanged()
|
||||
showNotification(group)
|
||||
@@ -99,7 +81,7 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
||||
* https://android.googlesource.com/platform/frameworks/base/+/android-4.0.1_r1/wifi/java/android/net/wifi/p2p/WifiP2pService.java#1028
|
||||
* https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/a8d5e40/service/java/com/android/server/wifi/p2p/WifiP2pServiceImpl.java#2547
|
||||
*/
|
||||
private val subnetPrefixLength get() = app.pref.getString("service.subnetPrefixLength", "24")
|
||||
private var subnetPrefixLength: Short = 24
|
||||
private val dns get() = app.pref.getString("service.dns", "8.8.8.8:53")
|
||||
|
||||
var status = Status.IDLE
|
||||
@@ -183,6 +165,35 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
||||
Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))
|
||||
.build())
|
||||
|
||||
private fun onGroupCreated(info: WifiP2pInfo, group: WifiP2pGroup) {
|
||||
val owner = info.groupOwnerAddress
|
||||
val hostAddress = owner?.hostAddress
|
||||
val downstream = group.`interface`
|
||||
if (!info.groupFormed || !info.isGroupOwner || downstream == null || hostAddress == null) return
|
||||
this.downstream = downstream
|
||||
this.hostAddress = hostAddress
|
||||
var subnetPrefixLength = NetworkInterface.getByName(downstream)?.interfaceAddresses
|
||||
?.singleOrNull { it.address == owner }?.networkPrefixLength
|
||||
if (subnetPrefixLength == null) {
|
||||
Log.w(TAG, "Unable to find prefix length of interface $downstream, 24 is assumed")
|
||||
subnetPrefixLength = 24
|
||||
}
|
||||
this.subnetPrefixLength = subnetPrefixLength
|
||||
if (noisySu("echo 1 >/proc/sys/net/ipv4/ip_forward",
|
||||
"ip route add default dev $upstream scope link table 62",
|
||||
"ip route add $hostAddress/$subnetPrefixLength dev $downstream scope link table 62",
|
||||
"ip route add broadcast 255.255.255.255 dev $downstream scope link table 62",
|
||||
"ip rule add from $hostAddress/$subnetPrefixLength lookup 62",
|
||||
"iptables -N vpnhotspot_fwd",
|
||||
"iptables -A vpnhotspot_fwd -i $upstream -o $downstream -m state --state ESTABLISHED,RELATED -j ACCEPT",
|
||||
"iptables -A vpnhotspot_fwd -i $downstream -o $upstream -j ACCEPT",
|
||||
"iptables -I FORWARD -j vpnhotspot_fwd",
|
||||
"iptables -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns",
|
||||
"iptables -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")) {
|
||||
doStart(group)
|
||||
} else startFailure("Something went wrong, please check logcat.")
|
||||
}
|
||||
|
||||
private fun clean() {
|
||||
if (receiverRegistered) {
|
||||
unregisterReceiver(receiver)
|
||||
|
||||
Reference in New Issue
Block a user