Skip to content

Commit

Permalink
take uri fragment as profile name (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaeedDev94 committed Sep 10, 2024
1 parent d4394cc commit 05eb8d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import io.github.saeeddev94.xray.helper.ProfileTouchHelper
import io.github.saeeddev94.xray.service.TProxyService
import org.json.JSONException
import org.json.JSONObject
import java.net.URI
import java.net.URISyntaxException

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

Expand Down Expand Up @@ -310,19 +312,30 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}

private fun processLink(link: String) {
if (link.startsWith("http://") || link.startsWith("https://")) {
getConfig(link)
val uri = try {
URI(link)
} catch (error: URISyntaxException) {
null
}
if (uri == null) {
Toast.makeText(applicationContext, "Invalid Uri", Toast.LENGTH_SHORT).show()
return
}
if (uri.scheme == "http" || uri.scheme == "https") {
getConfig(uri)
return
}
val linkHelper = LinkHelper(link)
if (!linkHelper.isValid()) {
Toast.makeText(applicationContext, "Invalid Link", Toast.LENGTH_SHORT).show()
return
}
profileLauncher.launch(profileIntent(name = linkHelper.remark(), config = linkHelper.json()))
val name = LinkHelper.remark(uri)
val json = linkHelper.json()
profileLauncher.launch(profileIntent(name = name, config = json))
}

private fun getConfig(link: String) {
private fun getConfig(uri: URI) {
val dialogView = LayoutInflater.from(this).inflate(R.layout.loading_dialog, LinearLayout(this))
val dialog = MaterialAlertDialogBuilder(this)
.setView(dialogView)
Expand All @@ -331,12 +344,13 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
dialog.show()
Thread {
try {
val config = HttpHelper().get(link)
val config = HttpHelper().get(uri.toString())
runOnUiThread {
dialog.dismiss()
try {
val json = JSONObject(config)
profileLauncher.launch(profileIntent(config = json.toString(2)))
val name = LinkHelper.remark(uri)
val json = JSONObject(config).toString(2)
profileLauncher.launch(profileIntent(name = name, config = json))
} catch (error: JSONException) {
Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT).show()
}
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/java/io/github/saeeddev94/xray/helper/LinkHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.saeeddev94.xray.Settings
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.net.URI

class LinkHelper(link: String) {

Expand All @@ -23,12 +24,15 @@ class LinkHelper(link: String) {
outbound = if (outbounds.length() > 0) outbounds[0] as JSONObject else null
}

fun isValid(): Boolean {
return success && outbound != null
companion object {
fun remark(uri: URI): String {
val name = uri.fragment ?: ""
return name.ifEmpty { "New Profile" }
}
}

fun remark(): String {
return outbound!!.optString("name", "New Profile")
fun isValid(): Boolean {
return success && outbound != null
}

fun json(): String {
Expand Down

0 comments on commit 05eb8d4

Please sign in to comment.