- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Support static IP configuration
Fixes #193.
Showing
13 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
mobile/src/main/java/be/mygod/vpnhotspot/StaticIpSetter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package be.mygod.vpnhotspot | ||
|
||
import android.content.Context | ||
import androidx.core.content.edit | ||
import be.mygod.vpnhotspot.App.Companion.app | ||
import be.mygod.vpnhotspot.net.Routing | ||
import be.mygod.vpnhotspot.root.RoutingCommands | ||
import be.mygod.vpnhotspot.util.Event0 | ||
import be.mygod.vpnhotspot.util.RootSession | ||
import be.mygod.vpnhotspot.widget.SmartSnackbar | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.parcelize.Parcelize | ||
import timber.log.Timber | ||
import java.io.IOException | ||
import java.net.NetworkInterface | ||
import java.net.SocketException | ||
import java.security.SecureRandom | ||
|
||
@Parcelize | ||
class StaticIpSetter : BootReceiver.Startable { | ||
companion object { | ||
private const val IFACE = "staticip" | ||
private const val KEY = "service.staticIp" | ||
|
||
val ifaceEvent = Event0() | ||
|
||
val iface get() = try { | ||
NetworkInterface.getByName(IFACE) | ||
} catch (_: SocketException) { | ||
null | ||
} catch (e: Exception) { | ||
Timber.w(e) | ||
null | ||
} | ||
|
||
var ips: String | ||
get() { | ||
app.pref.getString(KEY, null)?.let { return it } | ||
val octets = ByteArray(3) | ||
SecureRandom.getInstanceStrong().nextBytes(octets) | ||
return "10.${octets.joinToString(".") { it.toUByte().toString() }}".also { ips = it } | ||
} | ||
set(value) = app.pref.edit { putString(KEY, value) } | ||
|
||
fun enable(enabled: Boolean) = GlobalScope.launch { | ||
val success = try { | ||
RootSession.use { | ||
try { | ||
if (enabled) { | ||
it.exec("${Routing.IP} link add $IFACE type dummy") | ||
ips.lineSequence().forEach { ip -> | ||
it.exec("${Routing.IP} addr add $ip dev $IFACE") | ||
} | ||
it.exec("${Routing.IP} link set $IFACE up") | ||
true | ||
} else { | ||
it.exec("${Routing.IP} link del $IFACE") | ||
false | ||
} | ||
} catch (e: RoutingCommands.UnexpectedOutputException) { | ||
if (Routing.shouldSuppressIpError(e, enabled)) return@use false | ||
Timber.w(IOException("Failed to add link", e)) | ||
SmartSnackbar.make(e).show() | ||
false | ||
} | ||
} | ||
} catch (_: CancellationException) { | ||
false | ||
} catch (e: Exception) { | ||
Timber.w(e) | ||
SmartSnackbar.make(e).show() | ||
false | ||
} | ||
if (success) BootReceiver.add<StaticIpSetter>(StaticIpSetter()) else BootReceiver.delete<StaticIpSetter>() | ||
ifaceEvent() | ||
} | ||
} | ||
|
||
override fun start(context: Context) { | ||
enable(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
mobile/src/main/java/be/mygod/vpnhotspot/manage/StaticIpManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package be.mygod.vpnhotspot.manage | ||
|
||
import android.content.DialogInterface | ||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import android.text.method.LinkMovementMethod | ||
import android.view.WindowManager | ||
import android.widget.EditText | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.databinding.BaseObservable | ||
import androidx.databinding.Bindable | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.recyclerview.widget.RecyclerView | ||
import be.mygod.vpnhotspot.AlertDialogFragment | ||
import be.mygod.vpnhotspot.BR | ||
import be.mygod.vpnhotspot.R | ||
import be.mygod.vpnhotspot.StaticIpSetter | ||
import be.mygod.vpnhotspot.databinding.ListitemStaticIpBinding | ||
import be.mygod.vpnhotspot.util.formatAddresses | ||
import be.mygod.vpnhotspot.util.showAllowingStateLoss | ||
import kotlinx.parcelize.Parcelize | ||
|
||
class StaticIpManager(private val parent: TetheringFragment) : Manager(), DefaultLifecycleObserver { | ||
class ViewHolder(val binding: ListitemStaticIpBinding) : RecyclerView.ViewHolder(binding.root) { | ||
init { | ||
binding.text.movementMethod = LinkMovementMethod.getInstance() | ||
} | ||
} | ||
|
||
inner class Data : BaseObservable() { | ||
private var iface = StaticIpSetter.iface | ||
val active: Boolean @Bindable get() = iface != null | ||
val addresses: CharSequence @Bindable get() = iface?.formatAddresses() ?: "" | ||
|
||
fun onChanged() { | ||
iface = StaticIpSetter.iface | ||
notifyPropertyChanged(BR.serviceStarted) | ||
notifyPropertyChanged(BR.addresses) | ||
} | ||
|
||
fun configure() = ConfigureDialogFragment().apply { | ||
key() | ||
arg(ConfigureData(StaticIpSetter.ips)) | ||
}.showAllowingStateLoss(parent.parentFragmentManager) | ||
|
||
fun toggle() { | ||
StaticIpSetter.enable(!active) | ||
onChanged() | ||
} | ||
} | ||
|
||
@Parcelize | ||
data class ConfigureData(val ips: String) : Parcelable | ||
class ConfigureDialogFragment : AlertDialogFragment<ConfigureData, ConfigureData>() { | ||
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) { | ||
setTitle(R.string.tethering_static_ip) | ||
setView(R.layout.dialog_static_ip) | ||
setPositiveButton(android.R.string.ok, listener) | ||
setNegativeButton(android.R.string.cancel, null) | ||
} | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?) = super.onCreateDialog(savedInstanceState).apply { | ||
create() | ||
findViewById<EditText>(android.R.id.edit)!!.setText(arg.ips) | ||
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) | ||
} | ||
|
||
override val ret get() = ConfigureData(dialog!!.findViewById<EditText>(android.R.id.edit)!!.text!!.toString()) | ||
} | ||
|
||
override val type get() = VIEW_TYPE_STATIC_IP | ||
private val data = Data() | ||
|
||
init { | ||
parent.lifecycle.addObserver(this) | ||
AlertDialogFragment.setResultListener<ConfigureDialogFragment, ConfigureData>(parent) { which, ret -> | ||
if (which == DialogInterface.BUTTON_POSITIVE) StaticIpSetter.ips = ret!!.ips.trim() | ||
} | ||
} | ||
|
||
override fun onCreate(owner: LifecycleOwner) { | ||
StaticIpSetter.ifaceEvent[this] = data::onChanged | ||
} | ||
|
||
override fun bindTo(viewHolder: RecyclerView.ViewHolder) { | ||
(viewHolder as ViewHolder).binding.data = data | ||
} | ||
|
||
override fun onDestroy(owner: LifecycleOwner) { | ||
StaticIpSetter.ifaceEvent -= this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp"> | ||
|
||
<path android:fillColor="@android:color/white" android:fillType="evenOdd" android:pathData="M16,9V4l1,0c0.55,0 1,-0.45 1,-1v0c0,-0.55 -0.45,-1 -1,-1H7C6.45,2 6,2.45 6,3v0c0,0.55 0.45,1 1,1l1,0v5c0,1.66 -1.34,3 -3,3h0v2h5.97v7l1,1l1,-1v-7H19v-2h0C17.34,12 16,10.66 16,9z"/> | ||
|
||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<com.google.android.material.textfield.TextInputLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
tools:viewBindingIgnore="true"> | ||
|
||
<com.google.android.material.textfield.TextInputEditText | ||
android:id="@android:id/edit" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginHorizontal="8dp" | ||
android:hint="@null" | ||
android:imeOptions="flagNoExtractUi" | ||
android:importantForAutofill="no" | ||
android:inputType="textUri|textMultiLine" | ||
android:minHeight="@dimen/touch_target_min" | ||
tools:text="10.9.8.7\nfce5::1"> | ||
|
||
<requestFocus /> | ||
</com.google.android.material.textfield.TextInputEditText> | ||
</com.google.android.material.textfield.TextInputLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
<data> | ||
<variable | ||
name="data" | ||
type="be.mygod.vpnhotspot.manage.StaticIpManager.Data"/> | ||
</data> | ||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:focusable="true" | ||
android:background="?android:attr/selectableItemBackground" | ||
android:paddingStart="16dp" | ||
android:paddingEnd="16dp" | ||
android:onClick="@{_ -> data.configure()}"> | ||
|
||
<ImageView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center_vertical" | ||
android:importantForAccessibility="no" | ||
android:src="@drawable/ic_content_push_pin" | ||
android:tint="?android:attr/textColorPrimary"/> | ||
|
||
<Space | ||
android:layout_width="16dp" | ||
android:layout_height="0dp"/> | ||
|
||
<LinearLayout | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:layout_marginTop="16dp" | ||
android:layout_marginBottom="16dp" | ||
android:orientation="vertical" | ||
android:layout_gravity="center_vertical"> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="@string/tethering_static_ip" | ||
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/> | ||
|
||
<be.mygod.vpnhotspot.widget.AutoCollapseTextView | ||
android:id="@+id/text" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@{data.addresses}" | ||
android:textIsSelectable="true" | ||
tools:text="01:23:45:ab:cd:ef\n10.9.8.7"/> | ||
</LinearLayout> | ||
|
||
<com.google.android.material.materialswitch.MaterialSwitch | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center_vertical" | ||
android:checked="@{data.active}" | ||
android:ellipsize="end" | ||
android:gravity="center_vertical" | ||
android:onClick="@{_ -> data.toggle()}"/> | ||
|
||
</LinearLayout> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters