Skip to content

Commit

Permalink
Adding root support (#1)
Browse files Browse the repository at this point in the history
* A: root support, DHCP setup avoid the user to set manually the proxy on the device he wants to connect to the hotspot

* U: README.md
  • Loading branch information
geekywoman authored and shinil7 committed Nov 2, 2018
1 parent 5966ae3 commit ca2a2fc
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 101 deletions.
Binary file added .idea/caches/build_file_checksums.ser
Binary file not shown.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Share internet via Wifi direct on Android
# Share internet via Wifi direct on Android [Root]

## Direct Net Share

Expand All @@ -9,7 +9,7 @@ Requirements
------------
- Launch the app in the host device.
- Connect to the host device using the `ssid` and `password` displayed in the app.
- On the other device, go to `Settings` -> `Wifi` -> Long click the current network -> `Modify network` -> `Advanced Options` -> and use the proxy settings displayed in the app.
- No need to setup a proxy on the device(s) you wish to connect. The ip is forwarded thanks to the root rights.

Repository overview
-------------------
Expand Down
11 changes: 11 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 26
Expand All @@ -15,9 +16,19 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
kotlin {
experimental {
coroutines 'enable'
}
}
}

dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
compile project(':direct-share')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5"
}
repositories {
mavenCentral()
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shinil.direct_share">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
36 changes: 0 additions & 36 deletions app/src/main/java/shinil/direct_share/BaseActivity.java

This file was deleted.

38 changes: 0 additions & 38 deletions app/src/main/java/shinil/direct_share/MainActivity.java

This file was deleted.

74 changes: 74 additions & 0 deletions app/src/main/java/shinil/direct_share/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package shinil.direct_share

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.SwitchCompat
import android.util.Log
import android.widget.CompoundButton
import android.widget.TextView
import kotlinx.coroutines.experimental.launch
import shinil.direct.share.DirectNetShare

class MainActivity : AppCompatActivity() {
private lateinit var infoTv: TextView
private val rootManager = RootManager()
private val groupCreatedListener = DirectNetShare.GroupCreatedListener { ssid, password -> infoTv.text = String.format("SSID : %s\nPassword : %s", ssid, password) }
private lateinit var share: DirectNetShare
private val onCheckedChangeListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
if (isChecked) {
checkWifiAndStart()
} else {
stopShare()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
infoTv = findViewById(R.id.info)
(findViewById<SwitchCompat>(R.id.wifi_switch)).setOnCheckedChangeListener(onCheckedChangeListener)
}

override fun onDestroy() {
super.onDestroy()
stopShare()
}

private fun checkWifiAndStart() {
if (!Utils.isWifiEnabled(applicationContext)) {
registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != null && intent.action == ConnectivityManager.CONNECTIVITY_ACTION) {
val noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)
if (!noConnectivity) {
startShare()
unregisterReceiver(this)
}
}
}
}, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
Utils.enableWifi(applicationContext)
} else {
startShare()
}
}

private fun startShare() {
share = DirectNetShare(this@MainActivity, groupCreatedListener)
share.start()
launch {
val success = rootManager.dhcpSetup()
Log.d("MainActivity", "DHCP setup successful? $success")
}
}

private fun stopShare() {
share.stop()
infoTv.text = ""
}
}
32 changes: 32 additions & 0 deletions app/src/main/java/shinil/direct_share/RootManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package shinil.direct_share

import android.util.Log
import java.util.*

class RootManager {
suspend fun dhcpSetup(): Boolean {
var success = true
success = success && runRootCommand("echo 1 > /proc/sys/net/ipv4/ip_forward")
success = success && runRootCommand("iptables -F")
success = success && runRootCommand("iptables -t nat -A POSTROUTING -j MASQUERADE")
success = success && runRootCommand("iptables -A FORWARD -j ACCEPT")
success = success && runRootCommand("iptables -t nat -A PREROUTING -p udp --dport 53 -j DNAT --to-destination 8.8.8.8:53")
success = success && runRootCommand("iptables -A FORWARD -p udp -d 8.8.8.8 --dport 53 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT")
return success
}

/**
* Warning: keep the 'suspend' here.
* waitFor documentation: Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.
*/
private suspend fun runRootCommand(command: String): Boolean {
val commands = arrayOf("su", "-c", command)
val start: Long = System.currentTimeMillis()
val process: Process = Runtime.getRuntime().exec(commands)
val result = process.waitFor()
val end: Long = System.currentTimeMillis()
Log.d("RootManager", "Command ${Arrays.toString(commands)} executed in ${end - start} ms " +
" with result $result")
return result == 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author shinilms
*/

public final class U {
public final class Utils {

public static boolean enableWifi(Context context) {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().
Expand Down
27 changes: 12 additions & 15 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="shinil.direct_share.MainActivity"
android:padding="16dp">
<TextView
android:id="@+id/text_view_info"
android:orientation="vertical"
android:padding="16dp"
tools:context="shinil.direct_share.MainActivity">

<android.support.v7.widget.SwitchCompat
android:id="@+id/wifi_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:text="Creating group.."/>
android:text="Enable Wifi" />

<TextView
android:id="@+id/text_view_hint"
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_info"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"/>
</RelativeLayout>
android:layout_marginTop="40dp" />
</LinearLayout>
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

ext.kotlin_version = '1.2.41'

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'

classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"


// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 11 12:55:17 IST 2017
#Thu May 24 11:27:11 GMT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

0 comments on commit ca2a2fc

Please sign in to comment.