Skip to content

Commit

Permalink
Merge pull request #3 from Jolanrensen/usingSecureSettingsPermission
Browse files Browse the repository at this point in the history
Using secure settings permission
  • Loading branch information
Jolanrensen authored Oct 14, 2019
2 parents 80305e0 + 1272508 commit ce2a138
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 142 deletions.
4 changes: 2 additions & 2 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "nl.jolanrensen.permanentproxy"
minSdkVersion 23
targetSdkVersion 29
versionCode 5
versionName "2.1"
versionCode 6
versionName "3.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name">Permanent Proxy</string>
<string name="wrapper_app">This app is just a phone companion of Permanent Proxy. Please install Permanent Proxy from the Play Store on you Wear OS watch.</string>
<string name="instructions"><![CDATA[To get started, first enable the Developer Options of your watch, which can be achieved by going to Settings -> System -> About and tap the Build number until you are a \"developer\".\nNext, go to Settings -> Developer options and enable \"ADB debugging\" and \"Debug over Bluetooth\".\nFinally start up Permanent Proxy, request permission and press \"Allow\" or \"Always allow this Computer\" if prompted.\nAfter the app has gotten permission, ADB can be turned off again.\n\nNow you can get started! Simply enter a proxy address and port, enable it (and on boot if you like) and you\'re done!\n\nProxy services can be found online and can be from any country. However, do make sure you completely trust the proxy you chose before you enter it! All the data of your watch might be sent through that proxy, even sensitive data, so act at your own risk.]]></string>
<string name="instructions"><![CDATA[Please read carefully:\nTo get started, first enable the Developer Options of your watch, which can be achieved by going to Settings -> System -> About and tap the Build number until you are a \"developer\".\nNext, go to Settings -> Developer options and enable \"ADB debugging\" and \"Debug over Bluetooth\".\nFinally start up Permanent Proxy, request permission and press \"Allow\" or \"Always allow this Computer\" if prompted.\nAfter the app has gotten permission, ADB can be turned off again unless you want to disable the proxy.\n\nNow you can get started! Simply enter a proxy address and port, enable it and you\'re done! If you reboot, the proxy will remain.\n\nProxy services can be found online and can be from any country. However, do make sure your proxy works and you completely trust the proxy you chose before you enter it! All the data of your watch might be sent through that proxy, even sensitive data, so act at your own risk.]]></string>
</resources>
4 changes: 2 additions & 2 deletions wear/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "nl.jolanrensen.permanentproxy"
minSdkVersion 23
targetSdkVersion 29
versionCode 5
versionName "2.1"
versionCode 6
versionName "3.0"
}
buildTypes {
release {
Expand Down
Binary file modified wear/release/mobile-release.apk
Binary file not shown.
Binary file modified wear/release/wear-release.aab
Binary file not shown.
Binary file modified wear/release/wear-release.apk
Binary file not shown.
12 changes: 4 additions & 8 deletions wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="nl.jolanrensen.permanentproxy">

<uses-permission android:name="android.permission.WAKE_LOCK" />
Expand All @@ -9,7 +10,9 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission
android:name="android.permission.WRITE_SECURE_SETTINGS"
tools:ignore="ProtectedPermissions" />


<application
Expand Down Expand Up @@ -44,13 +47,6 @@
</intent-filter>
</activity>

<receiver
android:name=".OnBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

</manifest>
100 changes: 82 additions & 18 deletions wear/src/main/java/nl/jolanrensen/permanentproxy/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ package nl.jolanrensen.permanentproxy

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.core.content.edit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.net.URL
import kotlin.concurrent.thread

object Constants {
const val LOGTAG = "PermanentProxy"
const val PORT = 7272 // 5555

fun logD(message: String) = Log.d(LOGTAG, message)
fun logI(message: String) = Log.i(LOGTAG, message)
Expand All @@ -22,38 +31,93 @@ object Constants {
fun getTurnOnProxyCommand(address: String, port: Int) =
"settings put global http_proxy $address:$port\n"

fun getTurnOffProxyCommand() =
"settings delete global http_proxy; settings delete global global_http_proxy_host; settings delete global global_http_proxy_port"
val turnOffProxyCommand =
"settings delete global http_proxy; \\\nsettings delete global global_http_proxy_host; \\\nsettings delete global global_http_proxy_port; \\\nsettings delete global global_http_proxy_exclusion_list; \\\nsettings delete global global_proxy_pac_url"

fun Context.startProxy(
p: SharedPreferences? = null,
address: String,
port: Int,
updateGooglePay: Boolean = true
) {

p?.edit(commit = true) {
putBoolean("onBoot", true)
}
Settings.Global.putString(contentResolver, Settings.Global.HTTP_PROXY, "$address:$port")
if (updateGooglePay) sendBroadcast(
Intent("android.server.checkin.CHECKIN")
)
}

fun Context.stopProxy(updateGooglePay: Boolean = true) {
Settings.Secure.putString(
contentResolver,
Settings.Global.HTTP_PROXY, "127.0.0.1:8007")
if (updateGooglePay) sendBroadcast(
Intent("android.server.checkin.CHECKIN")
)
fun Context.stopProxy(
p: SharedPreferences,
onFailure: (() -> Unit)? = null
) {
p.edit(commit = true) {
putBoolean("onBoot", false)
}
thread(start = true) {
val logs = arrayListOf<String>()
try {
SendSingleCommand(
logs = logs,
context = this,
ip = "localhost",
port = PORT,
command = turnOffProxyCommand,
timeout = 500,
ctrlC = false
) {
logD(it.toString())

SendSingleCommand(
logs = logs,
context = this,
ip = "localhost",
port = PORT,
command = "reboot",
timeout = 500,
ctrlC = false
) {
logD(it.toString())

}
}
} catch (e: Exception) {
logE("$logs", e)
onFailure?.invoke()
}

}
}

fun Context.getCurrentProxy() = try {
(Settings.Global.getString(contentResolver, "global_http_proxy_host") + ":" +
Settings.Global.getString(contentResolver, "global_http_proxy_port")).let {
if (it == "127.0.0.1:8007") null else it
val Context.currentProxy
get() = try {
(Settings.Global.getString(contentResolver, "global_http_proxy_host") + ":" +
Settings.Global.getString(contentResolver, "global_http_proxy_port")).let {
if (it == "null:null") null else it
}
} catch (e: Exception) {
logE("proxy", e)
null
}

fun getCurrentIP(callback: (String?) -> Unit) {
val process = GlobalScope.launch(Dispatchers.IO) {
callback(
JSONObject(URL("https://api.ipify.org?format=json").readText()).getString("ip")
)
}

GlobalScope.launch {
delay(5)
if (process.isActive) {
process.cancel()
callback(null)
}
}
} catch (e: Exception) {
logE("proxy", e)
null
}
}



}
Loading

0 comments on commit ce2a138

Please sign in to comment.