From bb5b9e8f13a8458acd03bdaf79e596f558481505 Mon Sep 17 00:00:00 2001 From: Pavlov Vladimir Date: Fri, 27 Nov 2026 17:43:11 +0300 Subject: [PATCH] Show subscription announcements on profile cards --- core/src/main/golang/native/config/fetch.go | 27 +++++++- .../main/golang/native/config/fetch_test.go | 68 +++++++++++++++++++ .../kr328/clash/core/model/FetchStatus.kt | 1 + .../src/main/res/layout/adapter_profile.xml | 9 +++ .../kr328/clash/service/ProfileManager.kt | 1 + .../kr328/clash/service/ProfileProcessor.kt | 15 ++-- .../kr328/clash/service/data/Database.kt | 2 +- .../kr328/clash/service/data/Imported.kt | 1 + .../service/data/migrations/Migrations.kt | 7 ++ .../kr328/clash/service/model/Profile.kt | 1 + 10 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 core/src/main/golang/native/config/fetch_test.go diff --git a/core/src/main/golang/native/config/fetch.go b/core/src/main/golang/native/config/fetch.go index c89cf4a847..b43c5dfe07 100644 --- a/core/src/main/golang/native/config/fetch.go +++ b/core/src/main/golang/native/config/fetch.go @@ -2,6 +2,7 @@ package config import ( "context" + "encoding/base64" "encoding/json" "fmt" "io" @@ -13,6 +14,7 @@ import ( "strconv" "strings" "time" + "unicode/utf8" "cfa/native/app" @@ -31,11 +33,13 @@ type Status struct { SubTotal *int64 `json:"subTotal,omitempty"` SubExpire *int64 `json:"subExpire,omitempty"` SubUpdateInterval *int64 `json:"subUpdateInterval,omitempty"` + SubAnnounce string `json:"subAnnounce,omitempty"` } type fetchHeader struct { SubscriptionUserInfo string ProfileUpdateInterval string + Announce string } func openUrl(ctx context.Context, url string) (io.ReadCloser, fetchHeader, error) { @@ -48,6 +52,7 @@ func openUrl(ctx context.Context, url string) (io.ReadCloser, fetchHeader, error return response.Body, fetchHeader{ SubscriptionUserInfo: response.Header.Get("subscription-userinfo"), ProfileUpdateInterval: response.Header.Get("profile-update-interval"), + Announce: response.Header.Get("Announce"), }, nil } @@ -117,10 +122,29 @@ func parseProfileUpdateInterval(value string) (int64, bool) { return int64(interval / time.Millisecond), true } +func parseAnnounce(value string) string { + value = strings.TrimSpace(value) + if encoded, ok := strings.CutPrefix(value, "base64:"); ok { + decoded, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + return "" + } + value = strings.TrimSpace(string(decoded)) + } + + // Profile metadata is sent to the UI over Binder. + const maxAnnounceBytes = 4096 + if len(value) > maxAnnounceBytes || !utf8.ValidString(value) { + return "" + } + return value +} + func reportSubscriptionInfo(header fetchHeader, reportStatus func(string)) { userinfo := header.SubscriptionUserInfo updateIntervalHeader := header.ProfileUpdateInterval - if userinfo == "" && updateIntervalHeader == "" { + announce := parseAnnounce(header.Announce) + if userinfo == "" && updateIntervalHeader == "" && announce == "" { return } @@ -129,6 +153,7 @@ func reportSubscriptionInfo(header fetchHeader, reportStatus func(string)) { Args: []string{}, Progress: -1, MaxProgress: -1, + SubAnnounce: announce, } if userinfo != "" { diff --git a/core/src/main/golang/native/config/fetch_test.go b/core/src/main/golang/native/config/fetch_test.go new file mode 100644 index 0000000000..19be41b70b --- /dev/null +++ b/core/src/main/golang/native/config/fetch_test.go @@ -0,0 +1,68 @@ +package config + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "net/url" + "path/filepath" + "strings" + "testing" +) + +func TestFetchSubscriptionAnnouncement(t *testing.T) { + tests := []struct { + name string + header string + userInfo string + want string + }{ + {name: "plain text", header: "Scheduled maintenance", want: "Scheduled maintenance"}, + {name: "base64 UTF-8", header: "base64:0JLQtdGA0YHQuNGPINC/0L7QtNC/0LjRgdC60Lg6IHYxLjEuMQ==", want: "Версия подписки: v1.1.1"}, + {name: "missing"}, + {name: "blank", header: "base64:ICA="}, + {name: "malformed base64", header: "base64:???", userInfo: "upload=7; download=12; total=100; expire=0"}, + {name: "invalid UTF-8", header: "base64:/w=="}, + {name: "too large", header: strings.Repeat("a", 4097)}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if tt.header != "" { + w.Header().Set("Announce", tt.header) + } + if tt.userInfo != "" { + w.Header().Set("Subscription-Userinfo", tt.userInfo) + } + _, _ = io.WriteString(w, "proxies: []\n") + })) + defer server.Close() + + source, err := url.Parse(server.URL) + if err != nil { + t.Fatal(err) + } + header, err := fetch(source, filepath.Join(t.TempDir(), "config.yaml")) + if err != nil { + t.Fatal(err) + } + + var status struct { + Announce string `json:"subAnnounce"` + Upload int64 `json:"subUpload"` + } + reportSubscriptionInfo(header, func(payload string) { + if err := json.Unmarshal([]byte(payload), &status); err != nil { + t.Fatal(err) + } + }) + if status.Announce != tt.want { + t.Errorf("announcement = %q, want %q", status.Announce, tt.want) + } + if tt.userInfo != "" && status.Upload != 7 { + t.Errorf("upload = %d, want 7", status.Upload) + } + }) + } +} diff --git a/core/src/main/java/com/github/kr328/clash/core/model/FetchStatus.kt b/core/src/main/java/com/github/kr328/clash/core/model/FetchStatus.kt index 6f7626e6e1..c63c48fd5f 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/FetchStatus.kt +++ b/core/src/main/java/com/github/kr328/clash/core/model/FetchStatus.kt @@ -16,6 +16,7 @@ data class FetchStatus( val subTotal: Long? = null, val subExpire: Long? = null, val subUpdateInterval: Long? = null, + val subAnnounce: String? = null, ) : Parcelable { enum class Action { FetchConfiguration, diff --git a/design/src/main/res/layout/adapter_profile.xml b/design/src/main/res/layout/adapter_profile.xml index 53f5218b6b..835514f747 100644 --- a/design/src/main/res/layout/adapter_profile.xml +++ b/design/src/main/res/layout/adapter_profile.xml @@ -65,6 +65,15 @@ android:layout_marginTop="@dimen/item_text_margin" android:text="@{profile.pending ? @string/format_type_unsaved(I18nKt.toString(profile.type, context)) : I18nKt.toString(profile.type, context)}" android:textAppearance="@style/TextAppearance.MaterialComponents.Body2" /> + + + = arrayOf( MIGRATION_1_2, + MIGRATION_2_3, ) val LEGACY_MIGRATION = ::migrationFromLegacy diff --git a/service/src/main/java/com/github/kr328/clash/service/model/Profile.kt b/service/src/main/java/com/github/kr328/clash/service/model/Profile.kt index 436894ad0a..f085be9275 100644 --- a/service/src/main/java/com/github/kr328/clash/service/model/Profile.kt +++ b/service/src/main/java/com/github/kr328/clash/service/model/Profile.kt @@ -26,6 +26,7 @@ data class Profile( val imported: Boolean, val pending: Boolean, val ageSecretKey: String? = null, + val announce: String? = null, ) : Parcelable { enum class Type { File, Url, External