feat: show subscription

This commit is contained in:
metacubex
2022-12-10 02:05:45 +08:00
parent d2f5503330
commit e06c901bf5
12 changed files with 280 additions and 38 deletions

View File

@@ -19,6 +19,11 @@ dependencies {
implementation(libs.androidx.room.ktx)
implementation(libs.kaidl.runtime)
implementation(libs.rikkax.multiprocess)
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))
// define any required OkHttp artifacts without version
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
}
afterEvaluate {

View File

@@ -2,6 +2,7 @@ package com.github.kr328.clash.service
import android.content.Context
import com.github.kr328.clash.service.data.Database
import com.github.kr328.clash.service.data.Imported
import com.github.kr328.clash.service.data.ImportedDao
import com.github.kr328.clash.service.data.Pending
import com.github.kr328.clash.service.data.PendingDao
@@ -13,10 +14,13 @@ import com.github.kr328.clash.service.util.directoryLastModified
import com.github.kr328.clash.service.util.generateProfileUUID
import com.github.kr328.clash.service.util.importedDir
import com.github.kr328.clash.service.util.pendingDir
import com.github.kr328.clash.service.util.sendProfileChanged
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.FileNotFoundException
import java.util.*
@@ -40,6 +44,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
type = type,
source = source,
interval = 0,
upload = 0,
total = 0,
download = 0,
expire = 0,
)
PendingDao().insert(pending)
@@ -68,6 +76,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
type = Profile.Type.File,
source = imported.source,
interval = imported.interval,
upload = imported.upload,
total = imported.total,
download = imported.download,
expire = imported.expire,
)
cloneImportedFiles(uuid, newUUID)
@@ -93,13 +105,21 @@ class ProfileManager(private val context: Context) : IProfileManager,
type = imported.type,
source = source,
interval = interval,
upload = 0,
total = 0,
download = 0,
expire = 0,
)
)
} else {
val newPending = pending.copy(
name = name,
source = source,
interval = interval
interval = interval,
upload = 0,
total = 0,
download = 0,
expire = 0,
)
PendingDao().update(newPending)
@@ -108,6 +128,81 @@ class ProfileManager(private val context: Context) : IProfileManager,
override suspend fun update(uuid: UUID) {
scheduleUpdate(uuid, true)
ImportedDao().queryByUUID(uuid)?.let {
if (it.type == Profile.Type.Url) {
updateFlow(it)
}
}
}
suspend fun updateFlow(old: Imported) {
val client = OkHttpClient()
try {
val request = Request.Builder()
.url(old.source)
.header("User-Agent", "ClashforWindows/0.19.23")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful || response.headers["subscription-userinfo"] == null) return
var upload: Long = 0
var download: Long = 0
var total: Long = 0
var expire: Long = 0
val userinfo = response.headers["subscription-userinfo"]
if (response.isSuccessful && userinfo != null) {
val flags = userinfo.split(";")
for (flag in flags) {
val info = flag.split("=")
when {
info[0].contains("upload") -> upload =
info[1].toLong()
info[0].contains("download") -> download =
info[1].toLong()
info[0].contains("total") -> total =
info[1].toLong()
info[0].contains("expire") -> {
if (info[1].isNotEmpty()) {
expire = info[1].toLong()
}
}
}
}
}
val new = Imported(
old.uuid,
old.name,
old.type,
old.source,
old.interval,
upload,
download,
total,
expire,
old?.createdAt ?: System.currentTimeMillis()
)
if (old != null) {
ImportedDao().update(new)
} else {
ImportedDao().insert(new)
}
PendingDao().remove(new.uuid)
context.sendProfileChanged(new.uuid)
// println(response.body!!.string())
}
} catch (e: Exception) {
System.out.println(e)
}
}
override suspend fun commit(uuid: UUID, callback: IFetchObserver?) {
@@ -163,6 +258,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
val type = pending?.type ?: imported?.type ?: return null
val source = pending?.source ?: imported?.source ?: return null
val interval = pending?.interval ?: imported?.interval ?: return null
val upload = pending?.upload ?: imported?.upload ?: return null
val download = pending?.download ?: imported?.download ?: return null
val total = pending?.total ?: imported?.total ?: return null
val expire = pending?.expire ?: imported?.expire ?: return null
return Profile(
uuid,
@@ -171,6 +270,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
source,
active != null && imported?.uuid == active,
interval,
upload,
download,
total,
expire,
resolveUpdatedAt(uuid),
imported != null,
pending != null

View File

@@ -19,6 +19,8 @@ import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.util.*
import java.util.concurrent.TimeUnit
@@ -65,28 +67,93 @@ object ProfileProcessor {
.copyRecursively(context.importedDir.resolve(snapshot.uuid.toString()))
val old = ImportedDao().queryByUUID(snapshot.uuid)
var upload: Long = 0
var download: Long = 0
var total: Long = 0
var expire: Long = 0
if (snapshot?.type == Profile.Type.Url) {
val client = OkHttpClient()
val request = Request.Builder()
.url(snapshot.source)
.header("User-Agent", "ClashforWindows/0.19.23")
.build()
val new = Imported(
snapshot.uuid,
snapshot.name,
snapshot.type,
snapshot.source,
snapshot.interval,
old?.createdAt ?: System.currentTimeMillis()
)
client.newCall(request).execute().use { response ->
val userinfo = response.headers["subscription-userinfo"]
if (response.isSuccessful && userinfo != null) {
val flags = userinfo.split(";")
for (flag in flags) {
val info = flag.split("=")
when {
info[0].contains("upload") -> upload =
info[1].toLong()
if (old != null) {
ImportedDao().update(new)
} else {
ImportedDao().insert(new)
info[0].contains("download") -> download =
info[1].toLong()
info[0].contains("total") -> total =
info[1].toLong()
info[0].contains("expire") -> {
if (info[1].isNotEmpty()) {
expire = info[1].toLong()
}
}
}
}
}
val new = Imported(
snapshot.uuid,
snapshot.name,
snapshot.type,
snapshot.source,
snapshot.interval,
upload,
download,
total,
expire,
old?.createdAt ?: System.currentTimeMillis()
)
if (old != null) {
ImportedDao().update(new)
} else {
ImportedDao().insert(new)
}
PendingDao().remove(snapshot.uuid)
context.pendingDir.resolve(snapshot.uuid.toString())
.deleteRecursively()
context.sendProfileChanged(snapshot.uuid)
}
} else if (snapshot?.type == Profile.Type.File) {
val new = Imported(
snapshot.uuid,
snapshot.name,
snapshot.type,
snapshot.source,
snapshot.interval,
upload,
download,
total,
expire,
old?.createdAt ?: System.currentTimeMillis()
)
if (old != null) {
ImportedDao().update(new)
} else {
ImportedDao().insert(new)
}
PendingDao().remove(snapshot.uuid)
context.pendingDir.resolve(snapshot.uuid.toString())
.deleteRecursively()
context.sendProfileChanged(snapshot.uuid)
}
PendingDao().remove(snapshot.uuid)
context.pendingDir.resolve(snapshot.uuid.toString())
.deleteRecursively()
context.sendProfileChanged(snapshot.uuid)
}
}
}
@@ -181,10 +248,13 @@ object ProfileProcessor {
when {
name.isBlank() ->
throw IllegalArgumentException("Empty name")
source.isEmpty() && type != Profile.Type.File ->
throw IllegalArgumentException("Invalid url")
source.isNotEmpty() && scheme != "https" && scheme != "http" && scheme != "content" ->
throw IllegalArgumentException("Unsupported url $source")
interval != 0L && TimeUnit.MILLISECONDS.toMinutes(interval) < 15 ->
throw IllegalArgumentException("Invalid interval")
}

View File

@@ -14,5 +14,9 @@ data class Imported(
@ColumnInfo(name = "type") val type: Profile.Type,
@ColumnInfo(name = "source") val source: String,
@ColumnInfo(name = "interval") val interval: Long,
@ColumnInfo(name = "upload") val upload: Long,
@ColumnInfo(name = "download") val download: Long,
@ColumnInfo(name = "total") val total: Long,
@ColumnInfo(name = "expire") val expire: Long,
@ColumnInfo(name = "createdAt") val createdAt: Long,
)

View File

@@ -14,5 +14,9 @@ data class Pending(
@ColumnInfo(name = "type") val type: Profile.Type,
@ColumnInfo(name = "source") val source: String,
@ColumnInfo(name = "interval") val interval: Long,
@ColumnInfo(name = "upload") val upload: Long,
@ColumnInfo(name = "download") val download: Long,
@ColumnInfo(name = "total") val total: Long,
@ColumnInfo(name = "expire") val expire: Long,
@ColumnInfo(name = "createdAt") val createdAt: Long = System.currentTimeMillis(),
)

View File

@@ -92,7 +92,8 @@ private suspend fun migrationFromLegacy234(
type = newType,
source = if (newType != Profile.Type.File) cursor.getString(uri) else "",
interval = if (version == 2) intervalValue * 1000 else intervalValue,
)
0,0,0,0
)
val base = context.pendingDir.resolve(pending.uuid.toString())
@@ -165,6 +166,7 @@ private suspend fun migrationFromLegacy1(context: Context, legacy: SQLiteDatabas
type = newType,
source = source,
interval = 0,
0,0,0,0
)
val base = context.pendingDir.resolve(pending.uuid.toString())

View File

@@ -133,7 +133,8 @@ class Picker(private val context: Context) {
imported.name,
imported.type,
imported.source,
imported.interval
imported.interval,
0,0,0,0
)
)

View File

@@ -18,6 +18,11 @@ data class Profile(
val source: String,
val active: Boolean,
val interval: Long,
val upload: Long,
var download: Long,
val total: Long,
val expire: Long,
val updatedAt: Long,
val imported: Boolean,