Skip to content

Commit b01997f

Browse files
committed
feat: Support profile-update-interval header for auto update
1 parent d4ba9fd commit b01997f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

service/src/main/java/com/github/kr328/clash/service/ProfileManager.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.kr328.clash.service
22

33
import android.content.Context
4+
import com.github.kr328.clash.common.log.Log
45
import com.github.kr328.clash.service.data.Database
56
import com.github.kr328.clash.service.data.Imported
67
import com.github.kr328.clash.service.data.ImportedDao
@@ -24,6 +25,7 @@ import okhttp3.Request
2425
import java.io.FileNotFoundException
2526
import java.math.BigDecimal
2627
import java.util.*
28+
import java.util.concurrent.TimeUnit
2729

2830
class ProfileManager(private val context: Context) : IProfileManager,
2931
CoroutineScope by CoroutineScope(Dispatchers.IO) {
@@ -151,10 +153,21 @@ class ProfileManager(private val context: Context) : IProfileManager,
151153
var download: Long = 0
152154
var total: Long = 0
153155
var expire: Long = 0
156+
var interval: Long = old.interval
154157

155158
val userinfo = response.headers["subscription-userinfo"]
156-
if (response.isSuccessful && userinfo != null) {
159+
val updateInterval = response.headers["profile-update-interval"]
160+
161+
if (updateInterval != null) {
162+
try {
163+
val minutes = updateInterval.toInt() * 60 // Convert hours to minutes
164+
interval = TimeUnit.MINUTES.toMillis(minutes.toLong())
165+
} catch (e: NumberFormatException) {
166+
Log.w("Invalid profile update interval value: $updateInterval", e)
167+
}
168+
}
157169

170+
if (response.isSuccessful && userinfo != null) {
158171
val flags = userinfo.split(";")
159172
for (flag in flags) {
160173
val info = flag.split("=")
@@ -182,7 +195,7 @@ class ProfileManager(private val context: Context) : IProfileManager,
182195
old.name,
183196
old.type,
184197
old.source,
185-
old.interval,
198+
interval,
186199
upload,
187200
download,
188201
total,

service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ object ProfileProcessor {
7272
var download: Long = 0
7373
var total: Long = 0
7474
var expire: Long = 0
75+
var interval = snapshot.interval
76+
7577
if (snapshot?.type == Profile.Type.Url) {
7678
if (snapshot.source.startsWith("https://", true)) {
7779
val client = OkHttpClient()
@@ -82,6 +84,17 @@ object ProfileProcessor {
8284

8385
client.newCall(request).execute().use { response ->
8486
val userinfo = response.headers["subscription-userinfo"]
87+
val updateInterval = response.headers["profile-update-interval"]
88+
89+
if (updateInterval != null) {
90+
try {
91+
val minutes = updateInterval.toInt() * 60 // Convert hours to minutes
92+
interval = TimeUnit.MINUTES.toMillis(minutes.toLong())
93+
} catch (e: NumberFormatException) {
94+
Log.w("Invalid profile update interval value: $updateInterval", e)
95+
}
96+
}
97+
8598
if (response.isSuccessful && userinfo != null) {
8699
val flags = userinfo.split(";")
87100
for (flag in flags) {
@@ -108,7 +121,7 @@ object ProfileProcessor {
108121
snapshot.name,
109122
snapshot.type,
110123
snapshot.source,
111-
snapshot.interval,
124+
interval,
112125
upload,
113126
download,
114127
total,

0 commit comments

Comments
 (0)