Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@
android:configChanges="uiMode"
android:exported="false"
android:label="@string/meta_features" />
<activity
android:name=".LandingProxySettingsActivity"
android:configChanges="uiMode"
android:exported="false"
android:label="@string/landing_proxy" />
<activity
android:name=".AccessControlActivity"
android:configChanges="uiMode"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.kr328.clash

import com.github.kr328.clash.design.LandingProxySettingsDesign
import com.github.kr328.clash.service.store.ServiceStore
import com.github.kr328.clash.service.util.sendOverrideChanged
import com.github.kr328.clash.util.withClash
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import kotlinx.coroutines.withContext

class LandingProxySettingsActivity : BaseActivity<LandingProxySettingsDesign>() {
override suspend fun main() {
val store = ServiceStore(this)
val initial = withContext(Dispatchers.IO) { store.landingProxyJson() }

val design = LandingProxySettingsDesign(this, store)

setContentDesign(design)

defer {
val changed = withContext(Dispatchers.IO) { store.landingProxyJson() } != initial

// reload the running profile; a preview picks the change up on its next load
if (changed && clashRunning) {
sendOverrideChanged()
}
}

while (isActive) {
select<Unit> {
events.onReceive {

}
design.requests.onReceive {
when (it) {
LandingProxySettingsDesign.Request.PickFrontGroup -> {
design.requestFrontGroup(queryGroupNames())
}
}
}
}
}
}

private suspend fun queryGroupNames(): List<String> {
return try {
withClash {
if (preparePreview() != null) {
return@withClash emptyList()
}

try {
queryProxyGroupNames(false).filter { it != "GLOBAL" }
} finally {
releasePreview()
}
}
} catch (e: Exception) {
emptyList()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.github.kr328.clash

import android.content.ActivityNotFoundException
import android.content.Intent
import android.provider.Settings
import com.github.kr328.clash.common.util.intent
import com.github.kr328.clash.design.NetworkSettingsDesign
import com.github.kr328.clash.remote.StatusClient
import com.github.kr328.clash.service.store.ServiceStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import kotlinx.coroutines.withContext

class NetworkSettingsActivity : BaseActivity<NetworkSettingsDesign>() {
override suspend fun main() {
val vpnState = withContext(Dispatchers.IO) {
StatusClient(this@NetworkSettingsActivity).vpnState()
}

val design = NetworkSettingsDesign(
this,
uiStore,
ServiceStore(this),
clashRunning,
vpnState,
)

setContentDesign(design)
Expand All @@ -30,6 +41,12 @@ class NetworkSettingsActivity : BaseActivity<NetworkSettingsDesign>() {
when (it) {
NetworkSettingsDesign.Request.StartAccessControlList ->
startActivity(AccessControlActivity::class.intent)
NetworkSettingsDesign.Request.OpenSystemVpnSettings ->
try {
startActivity(Intent(Settings.ACTION_VPN_SETTINGS))
} catch (e: ActivityNotFoundException) {
startActivity(Intent(Settings.ACTION_WIRELESS_SETTINGS))
}
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/com/github/kr328/clash/ProxyActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.kr328.clash

import android.widget.Toast
import com.github.kr328.clash.common.util.intent
import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.core.model.Proxy
Expand All @@ -14,6 +15,20 @@ import kotlinx.coroutines.sync.withPermit

class ProxyActivity : BaseActivity<ProxyDesign>() {
override suspend fun main() {
// Load the active profile without starting the service (no-op while running),
// so proxies can be browsed and tested while stopped.
val previewError = withClash { preparePreview() }

if (previewError != null) {
Toast.makeText(this, previewError, Toast.LENGTH_LONG).show()

return finish()
}

defer {
withClash { releasePreview() }
}

val mode = withClash { queryOverride(Clash.OverrideSlot.Session).mode }
val names = withClash { queryProxyGroupNames(uiStore.proxyExcludeNotSelectable) }
val states = List(names.size) { ProxyState("?") }
Expand Down Expand Up @@ -46,6 +61,33 @@ class ProxyActivity : BaseActivity<ProxyDesign>() {
finish()
}
}
Event.ClashStop -> {
// the runtime reset the core, load a preview into it again
val error = withClash { reloadPreview() }

if (error != null) {
Toast.makeText(this@ProxyActivity, error, Toast.LENGTH_LONG).show()

finish()
} else {
val newNames = withClash {
queryProxyGroupNames(uiStore.proxyExcludeNotSelectable)
}

if (newNames != names) {
startActivity(ProxyActivity::class.intent)

finish()
} else {
// the runtime also cleared the session override
design.updateMode(
withClash { queryOverride(Clash.OverrideSlot.Session).mode }
)

design.requests.send(ProxyDesign.Request.ReloadAll)
}
}
}
else -> Unit
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/github/kr328/clash/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class SettingsActivity : BaseActivity<SettingsDesign>() {
startActivity(AppSettingsActivity::class.intent)
SettingsDesign.Request.StartNetwork ->
startActivity(NetworkSettingsActivity::class.intent)
SettingsDesign.Request.StartLandingProxy ->
startActivity(LandingProxySettingsActivity::class.intent)
SettingsDesign.Request.StartOverride ->
startActivity(OverrideSettingsActivity::class.intent)
SettingsDesign.Request.StartMetaFeature ->
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/github/kr328/clash/remote/StatusClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ class StatusClient(private val context: Context) {
.build()
}

/** Always-on / lockdown state, only known while the VPN service is running (API 29+). */
fun vpnState(): StatusProvider.VpnState? {
return try {
val result = context.contentResolver.call(
uri,
StatusProvider.METHOD_VPN_STATE,
null,
null
) ?: return null

StatusProvider.VpnState(
alwaysOn = result.getBoolean(StatusProvider.KEY_ALWAYS_ON),
lockdown = result.getBoolean(StatusProvider.KEY_LOCKDOWN),
)
} catch (e: Exception) {
Log.w("Query vpn state: $e", e)

null
}
}

fun currentProfile(): String? {
return try {
val result = context.contentResolver.call(
Expand Down
26 changes: 24 additions & 2 deletions core/src/main/cpp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,35 @@ Java_com_github_kr328_clash_core_bridge_Bridge_nativePatchSelector(JNIEnv *env,

JNIEXPORT void JNICALL
Java_com_github_kr328_clash_core_bridge_Bridge_nativeLoad(JNIEnv *env, jobject thiz,
jobject completable, jstring path) {
jobject completable, jstring path,
jboolean preview) {
TRACE_METHOD();

jobject _completable = new_global(completable);
scoped_string _path = get_string(path);

load(_completable, _path);
load(_completable, _path, preview);
}

JNIEXPORT jstring JNICALL
Java_com_github_kr328_clash_core_bridge_Bridge_nativeSetLandingProxy(JNIEnv *env, jobject thiz,
jstring content) {
TRACE_METHOD();

scoped_string response = NULL;

if (content == NULL) {
response = setLandingProxy(NULL);
} else {
scoped_string _content = get_string(content);

response = setLandingProxy(_content);
}

if (response == NULL)
return NULL;

return new_string(response);
}

JNIEXPORT void JNICALL
Expand Down
17 changes: 13 additions & 4 deletions core/src/main/golang/native/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ func fetchAndValid(callback unsafe.Pointer, path, url C.c_string, force C.int) {
}

//export load
func load(completable unsafe.Pointer, path C.c_string) {
go func(path string) {
C.complete(completable, marshalString(config.Load(path)))
func load(completable unsafe.Pointer, path C.c_string, preview C.int) {
go func(path string, preview bool) {
C.complete(completable, marshalString(config.Load(path, preview)))

C.release_object(completable)

runtime.GC()
}(C.GoString(path))
}(C.GoString(path), preview != 0)
}

//export setLandingProxy
func setLandingProxy(content C.c_string) *C.char {
if content == nil {
return marshalString(config.SetLandingProxy(""))
}

return marshalString(config.SetLandingProxy(C.GoString(content)))
}

//export readOverride
Expand Down
Loading