Skip to content

Commit

Permalink
Implement client blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Oct 1, 2018
1 parent 2b9bee7 commit 53f4d14
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ class LocalOnlyInterfaceManager(private val owner: Context, val downstream: Stri
forward()
if (app.masquerade) masquerade()
dnsRedirect(dns)
commit()
} catch (e: Exception) {
revert()
throw e
} finally {
commit()
} // otw nothing needs to be done
}
} catch (e: Exception) {
Expand Down
3 changes: 1 addition & 2 deletions mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ class TetheringService : IpNeighbourMonitoringService(), UpstreamMonitor.Callbac
if (app.masquerade) masquerade()
if (upstream != null) dnsRedirect(dns)
if (disableIpv6) disableIpv6()
commit()
} catch (e: Exception) {
revert()
throw e
} finally {
commit()
}
}
} catch (e: Exception) {
Expand Down
9 changes: 8 additions & 1 deletion mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package be.mygod.vpnhotspot.client

import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.StrikethroughSpan
import androidx.recyclerview.widget.DiffUtil
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R
Expand Down Expand Up @@ -27,7 +30,11 @@ abstract class Client {
val record by lazy { AppDatabase.instance.clientRecordDao.lookup(mac.macToLong()) }

open val icon get() = TetherType.ofInterface(iface).icon
val title get() = record.nickname.onEmpty(macIface)
val title: CharSequence get() {
val result = SpannableStringBuilder(record.nickname.onEmpty(macIface))
if (record.blocked) result.setSpan(StrikethroughSpan(), 0, result.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
return result
}
val description: String get() {
val result = StringBuilder(if (record.nickname.isEmpty()) "" else "$macIface\n")
ip.entries.forEach { (ip, state) ->
Expand Down
15 changes: 11 additions & 4 deletions mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.databinding.FragmentClientsBinding
import be.mygod.vpnhotspot.databinding.ListitemClientBinding
import be.mygod.vpnhotspot.net.IpNeighbourMonitor
import be.mygod.vpnhotspot.room.AppDatabase
import be.mygod.vpnhotspot.room.lookup
import be.mygod.vpnhotspot.room.macToLong
import be.mygod.vpnhotspot.room.*
import be.mygod.vpnhotspot.util.ServiceForegroundConnector

class ClientsFragment : Fragment(), ServiceConnection {
Expand All @@ -53,7 +51,7 @@ class ClientsFragment : Fragment(), ServiceConnection {
override fun onClick(di: DialogInterface?, which: Int) {
AppDatabase.instance.clientRecordDao.lookup(mac.macToLong()).apply {
nickname = dialog.findViewById<EditText>(android.R.id.edit).text
check(AppDatabase.instance.clientRecordDao.update(this) == mac)
AppDatabase.instance.clientRecordDao.update(this)
}
IpNeighbourMonitor.instance?.flush()
}
Expand All @@ -68,6 +66,7 @@ class ClientsFragment : Fragment(), ServiceConnection {
override fun onClick(v: View) {
PopupMenu(binding.root.context, binding.root).apply {
menuInflater.inflate(R.menu.popup_client, menu)
menu.removeItem(if (binding.client!!.record.blocked) R.id.block else R.id.unblock)
setOnMenuItemClickListener(this@ClientViewHolder)
show()
}
Expand All @@ -83,6 +82,14 @@ class ClientsFragment : Fragment(), ServiceConnection {
}.show(fragmentManager, "NicknameDialogFragment")
true
}
R.id.block, R.id.unblock -> {
val client = binding.client ?: return false
client.record.apply {
AppDatabase.instance.clientRecordDao.update(ClientRecord(mac, nickname, !blocked))
}
IpNeighbourMonitor.instance?.flush()
true
}
else -> false
}
}
Expand Down
29 changes: 19 additions & 10 deletions mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import be.mygod.vpnhotspot.util.RootSession
import be.mygod.vpnhotspot.util.computeIfAbsentCompat
import be.mygod.vpnhotspot.util.debugLog
import be.mygod.vpnhotspot.util.stopAndUnbind
import be.mygod.vpnhotspot.widget.SmartSnackbar
import com.crashlytics.android.Crashlytics
import java.net.*

/**
Expand Down Expand Up @@ -156,16 +158,23 @@ class Routing(private val owner: Context, val upstream: String?, private val dow
toRemove?.remove(ip)
subtransactions.computeIfAbsentCompat(ip) {
RootSession.beginTransaction().apply {
if (!strict) {
// otw allow downstream packets to be redirected to anywhere
// because we don't wanna keep track of default network changes
transaction.iptablesInsert("vpnhotspot_fwd -i $downstream -s $address -j ACCEPT")
transaction.iptablesInsert("vpnhotspot_fwd -o $downstream -d $address -m state --state ESTABLISHED,RELATED -j ACCEPT")
} else if (upstream != null) {
transaction.iptablesInsert("vpnhotspot_fwd -i $downstream -s $address -o $upstream -j ACCEPT")
transaction.iptablesInsert("vpnhotspot_fwd -i $upstream -o $downstream -d $address -m state --state ESTABLISHED,RELATED -j ACCEPT")
} // else nothing needs to be done
commit()
try {
if (!strict) {
// otw allow downstream packets to be redirected to anywhere
// because we don't wanna keep track of default network changes
iptablesInsert("vpnhotspot_fwd -i $downstream -s $address -j ACCEPT")
iptablesInsert("vpnhotspot_fwd -o $downstream -d $address -m state --state ESTABLISHED,RELATED -j ACCEPT")
} else if (upstream != null) {
iptablesInsert("vpnhotspot_fwd -i $downstream -s $address -o $upstream -j ACCEPT")
iptablesInsert("vpnhotspot_fwd -i $upstream -o $downstream -d $address -m state --state ESTABLISHED,RELATED -j ACCEPT")
} // else nothing needs to be done
commit()
} catch (e: Exception) {
revert()
Crashlytics.logException(e)
e.printStackTrace()
SmartSnackbar.make(e.localizedMessage).show()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ data class ClientRecord(@PrimaryKey
fun lookupOrNull(mac: Long): ClientRecord?

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun update(value: ClientRecord): Long
fun updateInternal(value: ClientRecord): Long
}
}

fun ClientRecord.Dao.lookup(mac: Long) = lookupOrNull(mac) ?: ClientRecord(mac)
fun ClientRecord.Dao.update(value: ClientRecord) = check(updateInternal(value) == value.mac)
4 changes: 4 additions & 0 deletions mobile/src/main/res/menu/popup_client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nickname"
android:title="Nickname..."/>
<item android:id="@+id/block"
android:title="Block"/>
<item android:id="@+id/unblock"
android:title="Unblock"/>
</menu>

0 comments on commit 53f4d14

Please sign in to comment.