-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
5966ae3
commit ca2a2fc
Showing
16 changed files
with
180 additions
and
101 deletions.
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 = "" | ||
} | ||
} |
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,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 | ||
} | ||
} |
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 |
---|---|---|
@@ -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> |
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 |
---|---|---|
@@ -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 |