proxy activity

This commit is contained in:
Kr328 committed 2019-11-26 23:14:46 +08:00
1 parent 7fba8b09cc
commit ceae371826
12 files changed
+100 -43

No files matched your search

+15 -5
View File
@@ -16,7 +16,9 @@
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
@@ -25,17 +27,25 @@
<activity
android:name=".ProfilesActivity"
android:label="@string/clash_profiles"
android:exported="false" />
android:exported="false"
android:screenOrientation="portrait" />
<activity
android:name=".CreateProfileActivity"
android:label="@string/clash_new_profile"
android:exported="false" />
android:exported="false"
android:screenOrientation="portrait"/>
<activity
android:name=".ImportFileActivity"
android:label="@string/clash_import_from_file"
android:exported="false" />
android:exported="false"
android:screenOrientation="portrait"/>
<activity
android:name=".ClashStatusActivity"
android:exported="false" />
android:exported="false"
android:screenOrientation="portrait"/>
<activity android:name=".ProxyActivity"
android:label="@string/clash_profiles"
android:exported="false"
android:screenOrientation="portrait"/>
</application>
</manifest>
@@ -0,0 +1,5 @@
package com.github.kr328.clash
class ProxyActivity : BaseActivity() {
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
+3 -3
View File
@@ -6,11 +6,11 @@
<string name="clash_status_started">Running</string>
<string name="clash_status_forwarded_traffic">%s Forwarded</string>
<string name="clash_proxy_manage">Proxy</string>
<string name="clash_proxy_manage">Proxies</string>
<string name="clash_proxy_manage_summary">%d Items</string>
<string name="clash_profiles">Profile</string>
<string name="clash_profiles_summary_selected">%s Active</string>
<string name="clash_profiles">Profiles</string>
<string name="clash_profiles_summary_selected">%s Activated</string>
<string name="clash_profiles_summary_unselected">Unselected</string>
<string name="clash_logs">Logs</string>
@@ -9,6 +9,7 @@ import com.github.kr328.clash.core.event.ProcessEvent
import com.github.kr328.clash.core.event.ProxyChangedEvent
import com.github.kr328.clash.core.event.TrafficEvent
import com.github.kr328.clash.core.model.Proxy
import com.github.kr328.clash.core.model.ProxyPacket
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import org.json.JSONObject
@@ -56,14 +57,14 @@ class Clash(
}
}
fun queryProxies(): Proxy.Packet {
fun queryProxies(): ProxyPacket {
return runControl(COMMAND_QUERY_PROXIES) { _, input, _ ->
val data = input.readString()
Log.d(TAG, data)
Json(JsonConfiguration.Stable.copy(strictMode = false))
.parse(Proxy.Packet.serializer(), data)
.parse(ProxyPacket.serializer(), data)
}
}
@@ -8,7 +8,8 @@ import kotlinx.serialization.Serializable
@Serializable
data class ErrorEvent(val type: Type, val message: String) : Event, Parcelable {
enum class Type {
START_FAILURE
START_FAILURE,
QUERY_PROXY_FAILURE
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
@@ -6,25 +6,7 @@ import com.github.kr328.clash.core.serialization.Parcels
import kotlinx.serialization.Serializable
@Serializable
data class Proxy(val type: String) : Parcelable {
@Serializable
data class Packet(val proxies: Map<String, Proxy>)
override fun writeToParcel(parcel: Parcel, flags: Int) {
Parcels.dump(serializer(), this, parcel)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Proxy> {
override fun createFromParcel(parcel: Parcel): Proxy {
return Parcels.load(serializer(), parcel)
}
override fun newArray(size: Int): Array<Proxy?> {
return arrayOfNulls(size)
}
}
}
data class Proxy(val type: String,
val all: List<String> = emptyList(),
val now: String = "",
val history: List<String> = emptyList())
@@ -0,0 +1,28 @@
package com.github.kr328.clash.core.model
import android.os.Parcel
import android.os.Parcelable
import com.github.kr328.clash.core.serialization.Parcels
import kotlinx.serialization.Serializable
@Serializable
data class ProxyPacket(val proxies: Map<String, Proxy>) : Parcelable {
override fun writeToParcel(parcel: Parcel, flags: Int) {
Parcels.dump(serializer(), this, parcel)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ProxyPacket> {
override fun createFromParcel(parcel: Parcel): ProxyPacket {
return Parcels.load(serializer(), parcel)
}
override fun newArray(size: Int): Array<ProxyPacket?> {
return arrayOfNulls(size)
}
}
}
@@ -0,0 +1,3 @@
package com.github.kr328.clash.core.model;
parcelable ProxyPacket;
@@ -4,6 +4,7 @@ import com.github.kr328.clash.service.IClashEventObserver;
import com.github.kr328.clash.service.IClashEventService;
import com.github.kr328.clash.service.IClashProfileService;
import com.github.kr328.clash.core.event.Event;
import com.github.kr328.clash.core.model.ProxyPacket;
interface IClashService {
// Services
@@ -18,4 +19,7 @@ interface IClashService {
void stopTunDevice();
void start();
void stop();
// Query
ProxyPacket queryAllProxies();
}
@@ -11,8 +11,10 @@ import android.os.IInterface
import android.os.ParcelFileDescriptor
import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.core.event.*
import com.github.kr328.clash.core.model.ProxyPacket
import com.github.kr328.clash.core.utils.Log
import com.github.kr328.clash.service.data.ClashDatabase
import java.io.EOFException
import java.io.File
import java.io.IOException
import java.util.concurrent.Executors
@@ -36,10 +38,24 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
clash.stopTunDevice()
}
override fun queryAllProxies(): ProxyPacket {
return try {
ProxyPacket(clash.queryProxies().proxies.mapValues {
it.value.copy(history = it.value.history.take(1))
})
} catch (e: Exception) {
this@ClashService.eventService.preformErrorEvent(
ErrorEvent(ErrorEvent.Type.QUERY_PROXY_FAILURE, e.toString())
)
ProxyPacket(emptyMap())
}
}
override fun start() {
try {
clash.process.start()
} catch (e: IOException) {
} catch (e: Exception) {
Log.e("Start failure", e)
this@ClashService.eventService.preformErrorEvent(
@@ -58,7 +74,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
clash.startTunDevice(fd.fileDescriptor, mtu)
fd.close()
} catch (e: IOException) {
} catch (e: Exception) {
Log.e("Start tun failure", e)
this@ClashService.eventService.preformErrorEvent(
@@ -49,15 +49,10 @@ class DefaultNetworkObserver(val context: Context, val listener: (Network?) -> U
private fun rebuildNetworkList(): Network? {
return try {
connectivity.allNetworks
.map {
connectivity.getNetworkCapabilities(it) to it
}
.filter {
it.first != null
}
.map {
it.first!! to it.second
.flatMap { network ->
connectivity.getNetworkCapabilities(network)?.let { listOf(it to network) } ?: emptyList()
}
.asSequence()
.filterNot {
it.first.hasTransport(NetworkCapabilities.TRANSPORT_VPN) ||
!it.first.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)