V0.1.7 (#5)
* save url when connected and allow toggling connection on app start (#2) * mask ip in logs, reduce keep-alive logs frequency * update comment about debug link, migrate prefs to ktx * auto-connect for telemost * fix ice ipv6 resolving * auto-connect for vk * refactor hooks names * regenerate android resources * remove stale telemost dc * kick disconnected user from vk, refactor relay on mobile client, prevent double hook injection * refactor constants * set low importance for notification to avoid signals * stop vpn/relay at app destroy, reset state at vpn disconnect * prevent vk session close at video tunnel * kick disconnected user from vk, refactor relay on mobile client, prevent double hook injection * multiple tabs/calls, write android logs in file * update ux, fix fd lifecycle in tun2socks * increment release version --------- Co-authored-by: HukuToc2288 <hukutoc2288@yandex.ru> Co-authored-by: kulikov <user@Mac.lan>
This commit is contained in:
1 parent
e8da07179a
commit
84da73c324
52 files changed
+1510
-479
No files matched your search
@@ -0,0 +1,10 @@
|
||||
package bypass.whitelist
|
||||
|
||||
import android.app.Application
|
||||
|
||||
class App : Application() {
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
Prefs.init(this)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package bypass.whitelist
|
||||
|
||||
object Ports {
|
||||
const val SOCKS = 1080L
|
||||
const val DC_WS = 9000L
|
||||
const val PION_SIGNALING = 9001L
|
||||
}
|
||||
|
||||
object PrefsKeys {
|
||||
const val CONNECT_ON_START = "connect_on_start"
|
||||
const val URL = "url"
|
||||
const val TUNNEL_MODE = "tunnel_mode"
|
||||
const val SHOW_LOGS = "show_logs"
|
||||
}
|
||||
|
||||
object Vpn {
|
||||
const val ADDRESS = "10.0.0.2"
|
||||
const val PREFIX_LENGTH = 32
|
||||
const val ROUTE = "0.0.0.0"
|
||||
const val MTU = 1500
|
||||
const val DNS_PRIMARY = "8.8.8.8"
|
||||
const val DNS_SECONDARY = "8.8.4.4"
|
||||
const val SESSION_NAME = "WhitelistBypass"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package bypass.whitelist
|
||||
|
||||
import android.app.Activity
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
|
||||
fun Activity.hideKeyboard() {
|
||||
val imm = getSystemService(InputMethodManager::class.java)
|
||||
currentFocus?.let { imm.hideSoftInputFromWindow(it.windowToken, 0) }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package bypass.whitelist
|
||||
|
||||
private val reUrl = Regex("(https?://[^/]+/)(.+)")
|
||||
private val reIp4 = Regex("\\d+\\.\\d+\\.\\d+\\.\\d+")
|
||||
private val reIp6 = Regex("[0-9a-fA-F]{1,4}(?::[0-9a-fA-F]{1,4}){2,7}|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}")
|
||||
|
||||
fun maskUrl(url: String): String {
|
||||
return url.replace(reUrl) { m ->
|
||||
m.groupValues[1] + m.groupValues[2].take(4) + "***"
|
||||
}.replace(reIp4) { m ->
|
||||
val p = m.value.split(".")
|
||||
"${p[0]}.${p[1]}.x.x"
|
||||
}.replace(reIp6, "x::x")
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package bypass.whitelist
|
||||
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.ArrayDeque
|
||||
import java.util.Locale
|
||||
|
||||
class LogWriter(cacheDir: File, private val maxDisplayLines: Int = 100) {
|
||||
|
||||
val file: File get() = logFile
|
||||
|
||||
private val logFile = File(cacheDir, "tunnel.log")
|
||||
private var writer: FileWriter? = null
|
||||
private val recentLines = ArrayDeque<String>(maxDisplayLines)
|
||||
private val timeFmt = SimpleDateFormat("HH:mm:ss.SSS", Locale.US)
|
||||
|
||||
@Synchronized
|
||||
fun reset() {
|
||||
writer?.close()
|
||||
writer = FileWriter(logFile, false)
|
||||
recentLines.clear()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun append(msg: String): AppendResult {
|
||||
val line = "${timeFmt.format(System.currentTimeMillis())} ${msg.replace("[HOOK] ", "")}"
|
||||
writer?.appendLine(line)
|
||||
writer?.flush()
|
||||
val evicted = recentLines.size >= maxDisplayLines
|
||||
if (evicted) recentLines.pollFirst()
|
||||
recentLines.addLast(line)
|
||||
return AppendResult(line, evicted)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun displayText(): String {
|
||||
return recentLines.joinToString("\n")
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun close() {
|
||||
writer?.close()
|
||||
writer = null
|
||||
}
|
||||
|
||||
data class AppendResult(val line: String, val evicted: Boolean)
|
||||
}
|
||||
@@ -1,40 +1,51 @@
|
||||
package bypass.whitelist
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.VpnService
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.webkit.*
|
||||
import android.widget.AdapterView
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.ImageButton
|
||||
import android.widget.Spinner
|
||||
import android.widget.PopupMenu
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import mobile.LogCallback
|
||||
import mobile.Mobile
|
||||
import java.io.File
|
||||
import java.net.Inet4Address
|
||||
import java.net.InetAddress
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private var tunnelMode = TunnelMode.DC
|
||||
private var pionProcess: Process? = null
|
||||
private var connected = false
|
||||
private var showLogs = false
|
||||
|
||||
private val logWriter by lazy { LogWriter(cacheDir) }
|
||||
private val relay by lazy {
|
||||
RelayController(
|
||||
nativeLibDir = applicationInfo.nativeLibraryDir,
|
||||
onLog = ::appendLog,
|
||||
onStatus = ::onVpnStatus,
|
||||
)
|
||||
}
|
||||
|
||||
private lateinit var webView: WebView
|
||||
private lateinit var logView: TextView
|
||||
private lateinit var urlInput: EditText
|
||||
private lateinit var goButton: Button
|
||||
private lateinit var statusBar: TextView
|
||||
private lateinit var logContainer: View
|
||||
|
||||
private var previousUrl = ""
|
||||
|
||||
private val vpnPrepLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
@@ -47,10 +58,10 @@ class MainActivity : AppCompatActivity() {
|
||||
else appendLog("VPN permission denied")
|
||||
}
|
||||
|
||||
private val hookVk by lazy { assets.open("joiner-vk.js").bufferedReader().readText() }
|
||||
private val hookTelemost by lazy { assets.open("joiner-telemost.js").bufferedReader().readText() }
|
||||
private val hookPionVk by lazy { assets.open("pion-vk.js").bufferedReader().readText() }
|
||||
private val hookPionTelemost by lazy { assets.open("pion-telemost.js").bufferedReader().readText() }
|
||||
private val hookVk by lazy { assets.open("dc-joiner-vk.js").bufferedReader().readText() }
|
||||
private val hookTelemost by lazy { assets.open("dc-joiner-telemost.js").bufferedReader().readText() }
|
||||
private val hookPionVk by lazy { assets.open("video-vk.js").bufferedReader().readText() }
|
||||
private val hookPionTelemost by lazy { assets.open("video-telemost.js").bufferedReader().readText() }
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@@ -66,26 +77,37 @@ class MainActivity : AppCompatActivity() {
|
||||
logView = findViewById(R.id.logView)
|
||||
urlInput = findViewById(R.id.urlInput)
|
||||
webView = findViewById(R.id.webView)
|
||||
goButton = findViewById(R.id.goButton)
|
||||
statusBar = findViewById(R.id.statusBar)
|
||||
logContainer = findViewById(R.id.logContainer)
|
||||
|
||||
logWriter.reset()
|
||||
|
||||
previousUrl = Prefs.lastUrl
|
||||
urlInput.setText(previousUrl)
|
||||
tunnelMode = Prefs.tunnelMode
|
||||
statusBar.text = getString(R.string.status_format, tunnelMode.label, getString(R.string.status_idle))
|
||||
showLogs = Prefs.showLogs
|
||||
logContainer.visibility = if (showLogs) View.VISIBLE else View.GONE
|
||||
|
||||
setupWebView()
|
||||
setupModeSpinner()
|
||||
|
||||
val goButton = findViewById<Button>(R.id.goButton)
|
||||
goButton.setOnClickListener {
|
||||
val url = urlInput.text.toString().trim()
|
||||
if (url.isNotEmpty()) {
|
||||
stopRelay()
|
||||
startRelay()
|
||||
appendLog("Loading: $url")
|
||||
webView.loadUrl(url)
|
||||
goButton.setOnClickListener { onGoPressed() }
|
||||
|
||||
findViewById<ImageButton>(R.id.shareLogsButton).setOnClickListener {
|
||||
val uri = FileProvider.getUriForFile(this, "$packageName.fileprovider", logWriter.file)
|
||||
val share = Intent(Intent.ACTION_SEND).apply {
|
||||
type = "text/plain"
|
||||
putExtra(Intent.EXTRA_STREAM, uri)
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
startActivity(Intent.createChooser(share, getString(R.string.share_logs)))
|
||||
}
|
||||
|
||||
findViewById<ImageButton>(R.id.copyLogsButton).setOnClickListener {
|
||||
val clip = ClipData.newPlainText("logs", logView.text)
|
||||
(getSystemService(CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(clip)
|
||||
Toast.makeText(this, "Logs copied", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
findViewById<ImageButton>(R.id.gearButton).setOnClickListener { showGearMenu(it) }
|
||||
findViewById<View>(R.id.clearButton).setOnClickListener { urlInput.setText("") }
|
||||
|
||||
TunnelVpnService.onDisconnect = { runOnUiThread { resetState() } }
|
||||
|
||||
VpnService.prepare(this)?.let { vpnPrepLauncher.launch(it) }
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
|
||||
@@ -95,97 +117,158 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
if (CALL_LINK.isNotEmpty()) {
|
||||
urlInput.setText(CALL_LINK)
|
||||
goButton.performClick()
|
||||
onGoPressed()
|
||||
} else if (Prefs.connectOnStart && previousUrl.isNotEmpty()) {
|
||||
onGoPressed()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
TunnelVpnService.onDisconnect = null
|
||||
stopRelay()
|
||||
TunnelVpnService.instance?.stopSelf()
|
||||
TunnelVpnService.instance?.stop()
|
||||
logWriter.close()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun setupModeSpinner() {
|
||||
val spinner = findViewById<Spinner>(R.id.modeSpinner)
|
||||
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, TunnelMode.entries.map { it.label })
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
spinner.adapter = adapter
|
||||
spinner.setSelection(TunnelMode.entries.indexOf(tunnelMode))
|
||||
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
var init = true
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: android.view.View?, pos: Int, id: Long) {
|
||||
if (init) { init = false; return }
|
||||
tunnelMode = TunnelMode.entries[pos]
|
||||
appendLog("Mode: ${tunnelMode.label}")
|
||||
stopRelay()
|
||||
startRelay()
|
||||
private fun onGoPressed() {
|
||||
val url = urlInput.text.toString().trim()
|
||||
if (url.isEmpty()) return
|
||||
logWriter.reset()
|
||||
logView.text = ""
|
||||
stopRelay()
|
||||
startRelay()
|
||||
hideKeyboard()
|
||||
urlInput.clearFocus()
|
||||
setConnected(false)
|
||||
setStatus(VpnStatus.CONNECTING)
|
||||
appendLog("Loading: ${maskUrl(url)}")
|
||||
if (previousUrl != url) {
|
||||
previousUrl = url
|
||||
Prefs.lastUrl = url
|
||||
}
|
||||
webView.loadUrl(url)
|
||||
}
|
||||
|
||||
private enum class MenuItem(val id: Int, val stringRes: Int) {
|
||||
MODE(99, R.string.menu_mode),
|
||||
RECONNECT_ON_START(100, R.string.menu_reconnect_on_start),
|
||||
SHOW_LOGS(101, R.string.menu_show_logs),
|
||||
SHARE_LOGS(102, R.string.menu_share_logs),
|
||||
RESET(200, R.string.menu_reset),
|
||||
}
|
||||
|
||||
private fun showGearMenu(anchor: View) {
|
||||
val popup = PopupMenu(this, anchor)
|
||||
val menu = popup.menu
|
||||
|
||||
menu.add(0, MenuItem.MODE.id, 0, getString(MenuItem.MODE.stringRes, tunnelMode.label))
|
||||
|
||||
menu.add(0, MenuItem.RECONNECT_ON_START.id, 0, MenuItem.RECONNECT_ON_START.stringRes).apply {
|
||||
isCheckable = true
|
||||
isChecked = Prefs.connectOnStart
|
||||
}
|
||||
menu.add(0, MenuItem.SHOW_LOGS.id, 0, MenuItem.SHOW_LOGS.stringRes).apply {
|
||||
isCheckable = true
|
||||
isChecked = showLogs
|
||||
}
|
||||
menu.add(0, MenuItem.SHARE_LOGS.id, 0, MenuItem.SHARE_LOGS.stringRes)
|
||||
menu.add(0, MenuItem.RESET.id, 0, MenuItem.RESET.stringRes)
|
||||
|
||||
popup.setOnMenuItemClickListener { item ->
|
||||
when (item.itemId) {
|
||||
MenuItem.RECONNECT_ON_START.id -> {
|
||||
Prefs.connectOnStart = !item.isChecked
|
||||
true
|
||||
}
|
||||
MenuItem.SHOW_LOGS.id -> {
|
||||
showLogs = !item.isChecked
|
||||
Prefs.showLogs = showLogs
|
||||
logContainer.visibility = if (showLogs) View.VISIBLE else View.GONE
|
||||
true
|
||||
}
|
||||
MenuItem.SHARE_LOGS.id -> {
|
||||
findViewById<ImageButton>(R.id.shareLogsButton).performClick()
|
||||
true
|
||||
}
|
||||
MenuItem.RESET.id -> {
|
||||
resetState()
|
||||
TunnelVpnService.instance?.stop()
|
||||
true
|
||||
}
|
||||
MenuItem.MODE.id -> {
|
||||
showModeDialog()
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
||||
}
|
||||
popup.show()
|
||||
}
|
||||
|
||||
private fun showModeDialog() {
|
||||
val modes = TunnelMode.entries
|
||||
val labels = modes.map { it.label }.toTypedArray()
|
||||
val current = modes.indexOf(tunnelMode)
|
||||
android.app.AlertDialog.Builder(this)
|
||||
.setSingleChoiceItems(labels, current) { dialog, which ->
|
||||
dialog.dismiss()
|
||||
val mode = modes[which]
|
||||
if (mode != tunnelMode) {
|
||||
tunnelMode = mode
|
||||
Prefs.tunnelMode = mode
|
||||
resetState()
|
||||
TunnelVpnService.instance?.stop()
|
||||
}
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun setConnected(value: Boolean) {
|
||||
connected = value
|
||||
goButton.setText(if (value) R.string.btn_disconnect else R.string.btn_go)
|
||||
goButton.setOnClickListener {
|
||||
if (connected) {
|
||||
resetState()
|
||||
TunnelVpnService.instance?.stop()
|
||||
} else {
|
||||
onGoPressed()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setStatus(status: VpnStatus) {
|
||||
statusBar.text = getString(R.string.status_format, tunnelMode.label, getString(status.labelRes))
|
||||
val colorRes = when (status) {
|
||||
VpnStatus.TUNNEL_ACTIVE -> R.color.status_active
|
||||
VpnStatus.CONNECTING,
|
||||
VpnStatus.CALL_CONNECTED,
|
||||
VpnStatus.DATACHANNEL_OPEN -> R.color.status_connecting
|
||||
VpnStatus.TUNNEL_LOST,
|
||||
VpnStatus.DATACHANNEL_LOST -> R.color.status_warning
|
||||
VpnStatus.CALL_DISCONNECTED,
|
||||
VpnStatus.CALL_FAILED -> R.color.status_error
|
||||
VpnStatus.STARTING -> R.color.status_idle
|
||||
}
|
||||
statusBar.setBackgroundColor(getColor(colorRes))
|
||||
}
|
||||
|
||||
private fun onVpnStatus(status: VpnStatus) {
|
||||
if (!relay.isRunning) return
|
||||
TunnelVpnService.instance?.updateStatus(status)
|
||||
runOnUiThread {
|
||||
setStatus(status)
|
||||
if (status == VpnStatus.TUNNEL_ACTIVE) setConnected(true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun startRelay() {
|
||||
Log.d("RELAY", "startRelay mode=${tunnelMode.label}")
|
||||
if (tunnelMode.isPion) startPionRelay() else startDcRelay()
|
||||
val isTelemost = urlInput.text.toString().contains("telemost")
|
||||
relay.start(tunnelMode, isTelemost)
|
||||
}
|
||||
|
||||
private fun stopRelay() {
|
||||
pionProcess?.let {
|
||||
it.destroy()
|
||||
it.waitFor()
|
||||
}
|
||||
pionProcess = null
|
||||
}
|
||||
|
||||
private fun startDcRelay() {
|
||||
val cb = LogCallback { msg ->
|
||||
appendLog(msg)
|
||||
if (msg.contains("browser connected")) updateVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
else if (msg.contains("ws read error")) updateVpnStatus(VpnStatus.TUNNEL_LOST)
|
||||
}
|
||||
Thread {
|
||||
try {
|
||||
Mobile.startJoiner(9000, 1080, cb)
|
||||
} catch (e: Exception) {
|
||||
appendLog("Relay error: ${e.message}")
|
||||
}
|
||||
}.start()
|
||||
appendLog("Relay started DC mode (SOCKS5 :1080, WS :9000)")
|
||||
}
|
||||
|
||||
private fun startPionRelay() {
|
||||
val relayBin = File(applicationInfo.nativeLibraryDir, "librelay.so")
|
||||
if (!relayBin.exists()) {
|
||||
appendLog("Pion relay binary not found")
|
||||
return
|
||||
}
|
||||
Thread {
|
||||
try {
|
||||
val isTelemost = urlInput.text.toString().contains("telemost")
|
||||
val mode = tunnelMode.relayMode(isTelemost)
|
||||
val pb = ProcessBuilder(
|
||||
relayBin.absolutePath, "--mode", mode, "--ws-port", "9001", "--socks-port", "1080"
|
||||
)
|
||||
pb.redirectErrorStream(true)
|
||||
val proc = pb.start()
|
||||
pionProcess = proc
|
||||
appendLog("Pion relay started mode=$mode (signaling :9001, SOCKS5 :1080)")
|
||||
proc.inputStream.bufferedReader().forEachLine { line ->
|
||||
Log.d("RELAY", line)
|
||||
appendLog(line)
|
||||
if (line.contains("CONNECTED")) updateVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
else if (line.contains("session cleaned up")) updateVpnStatus(VpnStatus.TUNNEL_LOST)
|
||||
}
|
||||
appendLog("Pion relay exited: ${proc.exitValue()}")
|
||||
} catch (e: Exception) {
|
||||
Log.e("RELAY", "Pion relay error", e)
|
||||
appendLog("Pion relay error: ${e.message}")
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
private fun updateVpnStatus(status: VpnStatus) {
|
||||
TunnelVpnService.instance?.updateStatus(status)
|
||||
relay.stop()
|
||||
}
|
||||
|
||||
private fun requestVpn() {
|
||||
@@ -196,7 +279,7 @@ class MainActivity : AppCompatActivity() {
|
||||
private fun startVpnService() {
|
||||
startService(Intent(this, TunnelVpnService::class.java))
|
||||
appendLog("VPN started")
|
||||
updateVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
onVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
}
|
||||
|
||||
private fun hookForUrl(url: String): String {
|
||||
@@ -236,14 +319,15 @@ class MainActivity : AppCompatActivity() {
|
||||
if (text.contains("[HOOK]")) {
|
||||
appendLog(text)
|
||||
when {
|
||||
text.contains("CALL CONNECTED") -> updateVpnStatus(VpnStatus.CALL_CONNECTED)
|
||||
text.contains("DataChannel open") -> updateVpnStatus(VpnStatus.DATACHANNEL_OPEN)
|
||||
text.contains("DataChannel closed") -> updateVpnStatus(VpnStatus.DATACHANNEL_LOST)
|
||||
text.contains("WebSocket connected") -> updateVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
text.contains("WebSocket disconnected") -> updateVpnStatus(VpnStatus.TUNNEL_LOST)
|
||||
text.contains("Connection state: connecting") -> updateVpnStatus(VpnStatus.CONNECTING)
|
||||
text.contains("Connection state: disconnected") -> updateVpnStatus(VpnStatus.CALL_DISCONNECTED)
|
||||
text.contains("Connection state: failed") -> updateVpnStatus(VpnStatus.CALL_FAILED)
|
||||
//TODO - use js bridge
|
||||
text.contains("CALL CONNECTED") -> onVpnStatus(VpnStatus.CALL_CONNECTED)
|
||||
text.contains("DataChannel open") -> onVpnStatus(VpnStatus.DATACHANNEL_OPEN)
|
||||
text.contains("DataChannel closed") -> onVpnStatus(VpnStatus.DATACHANNEL_LOST)
|
||||
text.contains("WebSocket connected") -> onVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
text.contains("WebSocket disconnected") -> onVpnStatus(VpnStatus.TUNNEL_LOST)
|
||||
text.contains("Connection state: connecting") -> onVpnStatus(VpnStatus.CONNECTING)
|
||||
text.contains("Connection state: disconnected") -> onVpnStatus(VpnStatus.CALL_DISCONNECTED)
|
||||
text.contains("Connection state: failed") -> onVpnStatus(VpnStatus.CALL_FAILED)
|
||||
}
|
||||
}
|
||||
return true
|
||||
@@ -287,15 +371,26 @@ if(oac){var nac=function(){var c=new oac();c.suspend();
|
||||
}
|
||||
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
appendLog("Page loaded, injecting hook for $url")
|
||||
view.evaluateJavascript(hookForUrl(url), null)
|
||||
view.evaluateJavascript("!!window.__hookInstalled") { result ->
|
||||
if (result == "true") {
|
||||
Log.d("HOOK", "Hook already injected, skipping")
|
||||
return@evaluateJavascript
|
||||
}
|
||||
appendLog("Page loaded, injecting hook for ${maskUrl(url)}")
|
||||
view.evaluateJavascript(hookForUrl(url), null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun appendLog(msg: String) {
|
||||
val (line, evicted) = logWriter.append(msg)
|
||||
runOnUiThread {
|
||||
logView.append("${msg.replace("[HOOK] ", "")}\n")
|
||||
if (evicted) {
|
||||
logView.text = logWriter.displayText()
|
||||
} else {
|
||||
logView.append("$line\n")
|
||||
}
|
||||
val scrollAmount = logView.layout?.let {
|
||||
it.getLineTop(logView.lineCount) - logView.height
|
||||
} ?: 0
|
||||
@@ -310,7 +405,7 @@ if(oac){var nac=function(){var c=new oac();c.suspend();
|
||||
val props = cm.getLinkProperties(network) ?: return ""
|
||||
for (addr in props.linkAddresses) {
|
||||
val ip = addr.address
|
||||
if (!ip.isLoopbackAddress && ip is java.net.Inet4Address) {
|
||||
if (!ip.isLoopbackAddress && ip is Inet4Address) {
|
||||
return ip.hostAddress ?: ""
|
||||
}
|
||||
}
|
||||
@@ -320,6 +415,17 @@ if(oac){var nac=function(){var c=new oac();c.suspend();
|
||||
return ""
|
||||
}
|
||||
|
||||
private fun resetState() {
|
||||
stopRelay()
|
||||
webView.loadUrl("about:blank")
|
||||
logWriter.reset()
|
||||
logView.text = ""
|
||||
logView.scrollTo(0, 0)
|
||||
setConnected(false)
|
||||
statusBar.text = getString(R.string.status_format, tunnelMode.label, getString(R.string.status_idle))
|
||||
statusBar.setBackgroundColor(getColor(R.color.status_idle))
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
inner class JsBridge {
|
||||
@JavascriptInterface
|
||||
@@ -330,29 +436,26 @@ if(oac){var nac=function(){var c=new oac();c.suspend();
|
||||
|
||||
@JavascriptInterface
|
||||
fun resolveHost(hostname: String): String = try {
|
||||
java.net.InetAddress.getByName(hostname).hostAddress ?: ""
|
||||
} catch (_: Exception) { "" }
|
||||
val all = InetAddress.getAllByName(hostname)
|
||||
val v4 = all.firstOrNull { it is Inet4Address }
|
||||
val addr = v4 ?: all.first()
|
||||
val ip = addr.hostAddress ?: ""
|
||||
Log.d("RELAY", "resolveHost: $hostname -> $ip (${addr.javaClass.simpleName}, ${all.size} addrs)")
|
||||
ip
|
||||
} catch (e: Exception) {
|
||||
Log.d("RELAY", "resolveHost: $hostname -> FAILED: ${e.message}")
|
||||
""
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
fun onTunnelReady() {
|
||||
appendLog("Tunnel ready, starting VPN...")
|
||||
updateVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
onVpnStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
runOnUiThread { requestVpn() }
|
||||
}
|
||||
}
|
||||
|
||||
private enum class TunnelMode(val label: String, val relayArg: String, val isPion: Boolean) {
|
||||
DC("DC", "joiner", false),
|
||||
PION_VIDEO("Pion Video", "video", true);
|
||||
|
||||
fun relayMode(isTelemost: Boolean): String {
|
||||
if (!isPion) return "joiner"
|
||||
val platform = if (isTelemost) "telemost" else "vk"
|
||||
return "$platform-$relayArg-joiner"
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val CALL_LINK = "" // Open call page on app start
|
||||
private const val CALL_LINK = "" // Open call page on app start (do not delete - I need it for debug)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package bypass.whitelist
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
|
||||
object Prefs {
|
||||
|
||||
private lateinit var prefs: SharedPreferences
|
||||
|
||||
fun init(context: Context) {
|
||||
prefs = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
var connectOnStart: Boolean
|
||||
get() = prefs.getBoolean(PrefsKeys.CONNECT_ON_START, false)
|
||||
set(value) = prefs.edit { putBoolean(PrefsKeys.CONNECT_ON_START, value) }
|
||||
|
||||
var lastUrl: String
|
||||
get() = prefs.getString(PrefsKeys.URL, "")!!
|
||||
set(value) = prefs.edit { putString(PrefsKeys.URL, value) }
|
||||
|
||||
var tunnelMode: TunnelMode
|
||||
get() {
|
||||
val name = prefs.getString(PrefsKeys.TUNNEL_MODE, TunnelMode.DC.name)!!
|
||||
return try {
|
||||
TunnelMode.valueOf(name)
|
||||
} catch (_: IllegalArgumentException) {
|
||||
TunnelMode.DC
|
||||
}
|
||||
}
|
||||
set(value) = prefs.edit { putString(PrefsKeys.TUNNEL_MODE, value.name) }
|
||||
|
||||
var showLogs: Boolean
|
||||
get() = prefs.getBoolean(PrefsKeys.SHOW_LOGS, false)
|
||||
set(value) = prefs.edit { putBoolean(PrefsKeys.SHOW_LOGS, value) }
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package bypass.whitelist
|
||||
|
||||
import android.util.Log
|
||||
import mobile.LogCallback
|
||||
import mobile.Mobile
|
||||
import java.io.File
|
||||
|
||||
class RelayController(
|
||||
private val nativeLibDir: String,
|
||||
private val onLog: (String) -> Unit,
|
||||
private val onStatus: (VpnStatus) -> Unit,
|
||||
) {
|
||||
private var dcThread: Thread? = null
|
||||
private var pionThread: Thread? = null
|
||||
private var pionProcess: Process? = null
|
||||
|
||||
@Volatile
|
||||
var isRunning = false
|
||||
private set
|
||||
|
||||
@Synchronized
|
||||
fun start(mode: TunnelMode, isTelemost: Boolean) {
|
||||
stop()
|
||||
isRunning = true
|
||||
if (mode.isPion) startPion(mode, isTelemost) else startDc()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun stop() {
|
||||
isRunning = false
|
||||
|
||||
pionProcess?.let {
|
||||
it.destroy()
|
||||
it.waitFor()
|
||||
}
|
||||
pionProcess = null
|
||||
pionThread?.interrupt()
|
||||
pionThread = null
|
||||
|
||||
Mobile.stopJoiner()
|
||||
dcThread?.interrupt()
|
||||
dcThread = null
|
||||
}
|
||||
|
||||
private fun startDc() {
|
||||
val cb = LogCallback { msg ->
|
||||
onLog(msg)
|
||||
if (msg.contains("browser connected")) onStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
else if (msg.contains("ws read error")) onStatus(VpnStatus.TUNNEL_LOST)
|
||||
}
|
||||
dcThread = Thread {
|
||||
try {
|
||||
Mobile.startJoiner(Ports.DC_WS, Ports.SOCKS, cb)
|
||||
} catch (e: Exception) {
|
||||
if (isRunning) onLog("Relay error: ${e.message}")
|
||||
}
|
||||
}.also { it.start() }
|
||||
onLog("Relay started DC mode (SOCKS5 :${Ports.SOCKS}, WS :${Ports.DC_WS})")
|
||||
}
|
||||
|
||||
private fun startPion(mode: TunnelMode, isTelemost: Boolean) {
|
||||
val relayBin = File(nativeLibDir, "librelay.so")
|
||||
if (!relayBin.exists()) {
|
||||
onLog("Pion relay binary not found")
|
||||
return
|
||||
}
|
||||
val relayMode = mode.relayMode(isTelemost)
|
||||
pionThread = Thread {
|
||||
try {
|
||||
val pb = ProcessBuilder(
|
||||
relayBin.absolutePath,
|
||||
"--mode", relayMode,
|
||||
"--ws-port", "${Ports.PION_SIGNALING}",
|
||||
"--socks-port", "${Ports.SOCKS}"
|
||||
)
|
||||
pb.redirectErrorStream(true)
|
||||
val proc = pb.start()
|
||||
synchronized(this) { pionProcess = proc }
|
||||
onLog("Pion relay started mode=$relayMode (signaling :${Ports.PION_SIGNALING}, SOCKS5 :${Ports.SOCKS})")
|
||||
proc.inputStream.bufferedReader().forEachLine { line ->
|
||||
Log.d("RELAY", line)
|
||||
onLog(line)
|
||||
if (line.contains("CONNECTED")) onStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
else if (line.contains("session cleaned up")) onStatus(VpnStatus.TUNNEL_LOST)
|
||||
}
|
||||
onLog("Pion relay exited: ${proc.exitValue()}")
|
||||
} catch (e: Exception) {
|
||||
if (isRunning) {
|
||||
Log.e("RELAY", "Pion relay error", e)
|
||||
onLog("Pion relay error: ${e.message}")
|
||||
}
|
||||
}
|
||||
}.also { it.start() }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package bypass.whitelist
|
||||
|
||||
enum class TunnelMode(val label: String, val relayArg: String, val isPion: Boolean) {
|
||||
DC("DC", "dc", false),
|
||||
PION_VIDEO("Video", "video", true);
|
||||
|
||||
fun relayMode(isTelemost: Boolean): String {
|
||||
if (!isPion) return "dc-joiner"
|
||||
val platform = if (isTelemost) "telemost" else "vk"
|
||||
return "$platform-$relayArg-joiner"
|
||||
}
|
||||
}
|
||||
@@ -11,32 +11,20 @@ import android.os.ParcelFileDescriptor
|
||||
import android.util.Log
|
||||
import mobile.Mobile
|
||||
|
||||
enum class VpnStatus(val label: String) {
|
||||
STARTING("Starting..."),
|
||||
CONNECTING("Connecting..."),
|
||||
CALL_CONNECTED("Call connected"),
|
||||
DATACHANNEL_OPEN("DataChannel open"),
|
||||
DATACHANNEL_LOST("DataChannel lost"),
|
||||
TUNNEL_ACTIVE("Tunnel active"),
|
||||
TUNNEL_LOST("Tunnel lost, reconnecting..."),
|
||||
CALL_DISCONNECTED("Call disconnected"),
|
||||
CALL_FAILED("Call failed")
|
||||
}
|
||||
|
||||
class TunnelVpnService : VpnService() {
|
||||
|
||||
companion object {
|
||||
const val TAG = "TunnelVPN"
|
||||
const val SOCKS_PORT = 1080
|
||||
const val MTU = 1500
|
||||
const val CHANNEL_ID = "vpn_channel"
|
||||
const val NOTIFICATION_ID = 1
|
||||
const val ACTION_STOP = "bypass.whitelist.STOP_VPN"
|
||||
var isRunning = false
|
||||
var instance: TunnelVpnService? = null
|
||||
var onDisconnect: (() -> Unit)? = null
|
||||
}
|
||||
|
||||
private var vpnFd: ParcelFileDescriptor? = null
|
||||
private var tun2socksThread: Thread? = null
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
@@ -59,19 +47,24 @@ class TunnelVpnService : VpnService() {
|
||||
|
||||
fun updateStatus(status: VpnStatus) {
|
||||
val nm = getSystemService(NotificationManager::class.java)
|
||||
nm.notify(NOTIFICATION_ID, buildNotification(status.label))
|
||||
nm.notify(NOTIFICATION_ID, buildNotification(getString(status.labelRes)))
|
||||
}
|
||||
|
||||
private fun startForegroundNotification() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(
|
||||
CHANNEL_ID, "VPN Tunnel", NotificationManager.IMPORTANCE_DEFAULT
|
||||
)
|
||||
val nm = getSystemService(NotificationManager::class.java)
|
||||
nm.createNotificationChannel(channel)
|
||||
fun stop() {
|
||||
if (!isRunning) return
|
||||
isRunning = false
|
||||
try {
|
||||
Mobile.stopTun2Socks()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "tun2socks stop error: ${e.message}")
|
||||
}
|
||||
|
||||
startForeground(NOTIFICATION_ID, buildNotification(VpnStatus.STARTING.label))
|
||||
tun2socksThread?.join(3000)
|
||||
tun2socksThread = null
|
||||
vpnFd = null
|
||||
@Suppress("DEPRECATION")
|
||||
stopForeground(true)
|
||||
stopSelf()
|
||||
onDisconnect?.invoke()
|
||||
}
|
||||
|
||||
private fun start() {
|
||||
@@ -80,12 +73,12 @@ class TunnelVpnService : VpnService() {
|
||||
startForegroundNotification()
|
||||
|
||||
val builder = Builder()
|
||||
.setSession("WhitelistBypass")
|
||||
.addAddress("10.0.0.2", 32)
|
||||
.addRoute("0.0.0.0", 0)
|
||||
.addDnsServer("8.8.8.8")
|
||||
.addDnsServer("8.8.4.4")
|
||||
.setMtu(MTU)
|
||||
.setSession(Vpn.SESSION_NAME)
|
||||
.addAddress(Vpn.ADDRESS, Vpn.PREFIX_LENGTH)
|
||||
.addRoute(Vpn.ROUTE, 0)
|
||||
.addDnsServer(Vpn.DNS_PRIMARY)
|
||||
.addDnsServer(Vpn.DNS_SECONDARY)
|
||||
.setMtu(Vpn.MTU)
|
||||
|
||||
try {
|
||||
builder.addDisallowedApplication(packageName)
|
||||
@@ -105,31 +98,30 @@ class TunnelVpnService : VpnService() {
|
||||
Log.i(TAG, "VPN established, fd=$fd")
|
||||
updateStatus(VpnStatus.TUNNEL_ACTIVE)
|
||||
|
||||
Thread {
|
||||
tun2socksThread = Thread {
|
||||
try {
|
||||
Mobile.startTun2Socks(fd.toLong(), MTU.toLong(), SOCKS_PORT.toLong())
|
||||
Mobile.startTun2Socks(fd.toLong(), Vpn.MTU.toLong(), Ports.SOCKS)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "tun2socks error: ${e.message}")
|
||||
isRunning = false
|
||||
}
|
||||
}.start()
|
||||
}.also { it.start() }
|
||||
}
|
||||
|
||||
private fun stop() {
|
||||
isRunning = false
|
||||
try {
|
||||
Mobile.stopTun2Socks()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "tun2socks stop error: ${e.message}")
|
||||
private fun startForegroundNotification() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(
|
||||
CHANNEL_ID, "VPN Tunnel", NotificationManager.IMPORTANCE_LOW
|
||||
)
|
||||
val nm = getSystemService(NotificationManager::class.java)
|
||||
nm.createNotificationChannel(channel)
|
||||
}
|
||||
vpnFd = null
|
||||
@Suppress("DEPRECATION")
|
||||
stopForeground(true)
|
||||
stopSelf()
|
||||
|
||||
startForeground(NOTIFICATION_ID, buildNotification(getString(VpnStatus.STARTING.labelRes)))
|
||||
}
|
||||
|
||||
private fun buildNotification(text: String): Notification {
|
||||
val openIntent = Intent(this, bypass.whitelist.MainActivity::class.java).apply {
|
||||
val openIntent = Intent(this, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
val openPending = PendingIntent.getActivity(
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package bypass.whitelist
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
|
||||
enum class VpnStatus(@StringRes val labelRes: Int) {
|
||||
STARTING(R.string.vpn_starting),
|
||||
CONNECTING(R.string.vpn_connecting),
|
||||
CALL_CONNECTED(R.string.vpn_call_connected),
|
||||
DATACHANNEL_OPEN(R.string.vpn_datachannel_open),
|
||||
DATACHANNEL_LOST(R.string.vpn_datachannel_lost),
|
||||
TUNNEL_ACTIVE(R.string.vpn_tunnel_active),
|
||||
TUNNEL_LOST(R.string.vpn_tunnel_lost),
|
||||
CALL_DISCONNECTED(R.string.vpn_call_disconnected),
|
||||
CALL_FAILED(R.string.vpn_call_failed)
|
||||
}
|
||||
Reference in new issue
Block a user