Skip to content

Support profile-update-interval header for auto update #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.kr328.clash.service

import android.content.Context
import com.github.kr328.clash.common.log.Log
import com.github.kr328.clash.service.data.Database
import com.github.kr328.clash.service.data.Imported
import com.github.kr328.clash.service.data.ImportedDao
Expand All @@ -24,6 +25,7 @@ import okhttp3.Request
import java.io.FileNotFoundException
import java.math.BigDecimal
import java.util.*
import java.util.concurrent.TimeUnit

class ProfileManager(private val context: Context) : IProfileManager,
CoroutineScope by CoroutineScope(Dispatchers.IO) {
Expand Down Expand Up @@ -151,10 +153,21 @@ class ProfileManager(private val context: Context) : IProfileManager,
var download: Long = 0
var total: Long = 0
var expire: Long = 0
var interval: Long = old.interval

val userinfo = response.headers["subscription-userinfo"]
if (response.isSuccessful && userinfo != null) {
val updateInterval = response.headers["profile-update-interval"]

if (updateInterval != null) {
try {
val minutes = updateInterval.toInt() * 60 // Convert hours to minutes
interval = TimeUnit.MINUTES.toMillis(minutes.toLong())
} catch (e: NumberFormatException) {
Log.w("Invalid profile update interval value: $updateInterval", e)
}
}

if (response.isSuccessful && userinfo != null) {
val flags = userinfo.split(";")
for (flag in flags) {
val info = flag.split("=")
Expand Down Expand Up @@ -182,7 +195,7 @@ class ProfileManager(private val context: Context) : IProfileManager,
old.name,
old.type,
old.source,
old.interval,
interval,
upload,
download,
total,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ object ProfileProcessor {
var download: Long = 0
var total: Long = 0
var expire: Long = 0
var interval = snapshot.interval

if (snapshot?.type == Profile.Type.Url) {
if (snapshot.source.startsWith("https://", true)) {
val client = OkHttpClient()
Expand All @@ -82,6 +84,17 @@ object ProfileProcessor {

client.newCall(request).execute().use { response ->
val userinfo = response.headers["subscription-userinfo"]
val updateInterval = response.headers["profile-update-interval"]

if (updateInterval != null) {
try {
val minutes = updateInterval.toInt() * 60 // Convert hours to minutes
interval = TimeUnit.MINUTES.toMillis(minutes.toLong())
} catch (e: NumberFormatException) {
Log.w("Invalid profile update interval value: $updateInterval", e)
}
}

if (response.isSuccessful && userinfo != null) {
val flags = userinfo.split(";")
for (flag in flags) {
Expand All @@ -108,7 +121,7 @@ object ProfileProcessor {
snapshot.name,
snapshot.type,
snapshot.source,
snapshot.interval,
interval,
upload,
download,
total,
Expand Down