diff --git a/bind/clash/app_store_route.go b/bind/clash/app_store_route.go new file mode 100644 index 00000000..36af10c8 --- /dev/null +++ b/bind/clash/app_store_route.go @@ -0,0 +1,620 @@ +package clash + +import ( + "encoding/json" + "errors" + "io" + "io/fs" + "net" + "os" + "path/filepath" + "sort" + "strings" + + C "github.com/ProjectClash/Clash/constant" + "github.com/ProjectClash/Clash/hub/route" + "github.com/metacubex/chi" + mhttp "github.com/metacubex/http" +) + +// The App owns store/profiles.json and store/active.json in its working +// directory. These routes only read and rewrite the input fields the App +// consumes on its next connection; they never rebuild derived files. +const ( + appStoreDirectoryName = "store" + appStoreProfilesFile = "profiles.json" + appStoreActiveFile = "active.json" + + appStoreProfilesRoute = "/app/profiles" + appStoreProfileRulesRoute = "/app/profiles/{id}/rules" + appStoreProfileDNSPolicyRoute = "/app/profiles/{id}/dns-policy" + + appStoreRequestBodyBytes = 4 << 20 + + appStoreMaximumAppendRules = 5000 + appStoreMaximumRuleBytes = 512 + appStoreMaximumDomainBytes = 253 + appStoreMaximumNameserverBytes = 512 +) + +var ( + errAppStoreIndexMissing = errors.New("the App has not published its profile index yet") + errAppStoreProfileMissing = errors.New("no such App profile") +) + +func init() { + route.Register(func(router chi.Router) { + for _, prefix := range privateRoutePrefixes { + router.Get(prefix+appStoreProfilesRoute, serveAppProfiles) + router.Get(prefix+appStoreProfileRulesRoute, serveAppProfileRules) + router.Put(prefix+appStoreProfileRulesRoute, serveAppProfileRulesUpdate) + router.Get(prefix+appStoreProfileDNSPolicyRoute, serveAppProfileDNSPolicy) + router.Put(prefix+appStoreProfileDNSPolicyRoute, serveAppProfileDNSPolicyUpdate) + } + }) +} + +type appStoreActive struct { + ProfileID string `json:"profileID"` + Revision string `json:"revision"` +} + +type appStoreProfileSummary struct { + ID string `json:"id"` + Label string `json:"label"` + Order int `json:"order"` + ActiveRevision string `json:"activeRevision"` + Active bool `json:"active"` + AppendedRules int `json:"appendedRules"` + PrependedRules bool `json:"prependedRules"` + Patched bool `json:"patched"` +} + +type appStoreIndex struct { + Active appStoreActive `json:"active"` + Profiles []appStoreProfileSummary `json:"profiles"` +} + +type appStoreRules struct { + ID string `json:"id"` + PrependRules bool `json:"prependRules"` + AppendRules []string `json:"appendRules"` + PatchJSON string `json:"patchJSON"` +} + +type appStoreRulesUpdate struct { + PrependRules *bool `json:"prependRules"` + AppendRules *[]string `json:"appendRules"` +} + +type appStoreDNSPolicy struct { + NameserverPolicy map[string][]string `json:"nameserverPolicy"` + FakeIPFilter []string `json:"fakeIPFilter"` +} + +type appStoreDNSPolicyUpdate struct { + NameserverPolicy *map[string][]string `json:"nameserverPolicy"` + FakeIPFilter *[]string `json:"fakeIPFilter"` +} + +func appStoreRoot() string { + return filepath.Join(C.Path.HomeDir(), appStoreDirectoryName) +} + +func appStoreGuard(writer mhttp.ResponseWriter, request *mhttp.Request) bool { + if !overAppGroupSocket(request) && !route.RequestFromManagedListener(request) { + mhttp.NotFound(writer, request) + return false + } + return true +} + +func serveAppProfiles(writer mhttp.ResponseWriter, request *mhttp.Request) { + if !appStoreGuard(writer, request) { + return + } + index, err := loadAppStoreIndexAt(appStoreRoot()) + if err != nil { + appStoreRefusal(writer, err) + return + } + writeControllerJSON(writer, index) +} + +func serveAppProfileRules(writer mhttp.ResponseWriter, request *mhttp.Request) { + if !appStoreGuard(writer, request) { + return + } + rules, err := loadAppStoreRulesAt(appStoreRoot(), chi.URLParam(request, "id")) + if err != nil { + appStoreRefusal(writer, err) + return + } + writeControllerJSON(writer, rules) +} + +func serveAppProfileRulesUpdate(writer mhttp.ResponseWriter, request *mhttp.Request) { + if !appStoreGuard(writer, request) { + return + } + var update appStoreRulesUpdate + if err := json.NewDecoder(io.LimitReader(request.Body, appStoreRequestBodyBytes)).Decode(&update); err != nil { + mhttp.Error(writer, "invalid rules payload", mhttp.StatusBadRequest) + return + } + if update.AppendRules != nil { + if err := validateAppStoreRules(*update.AppendRules); err != nil { + mhttp.Error(writer, err.Error(), mhttp.StatusBadRequest) + return + } + } + if err := saveAppStoreRulesAt(appStoreRoot(), chi.URLParam(request, "id"), update); err != nil { + appStoreRefusal(writer, err) + return + } + writer.Header().Set("Cache-Control", "no-store") + writer.WriteHeader(mhttp.StatusNoContent) +} + +func serveAppProfileDNSPolicy(writer mhttp.ResponseWriter, request *mhttp.Request) { + if !appStoreGuard(writer, request) { + return + } + policy, err := loadAppStoreDNSPolicyAt(appStoreRoot(), chi.URLParam(request, "id")) + if err != nil { + appStoreRefusal(writer, err) + return + } + writeControllerJSON(writer, policy) +} + +func serveAppProfileDNSPolicyUpdate(writer mhttp.ResponseWriter, request *mhttp.Request) { + if !appStoreGuard(writer, request) { + return + } + var update appStoreDNSPolicyUpdate + if err := json.NewDecoder(io.LimitReader(request.Body, appStoreRequestBodyBytes)).Decode(&update); err != nil { + mhttp.Error(writer, "invalid DNS policy payload", mhttp.StatusBadRequest) + return + } + if update.NameserverPolicy != nil { + if err := validateAppStoreNameserverPolicy(*update.NameserverPolicy); err != nil { + mhttp.Error(writer, err.Error(), mhttp.StatusBadRequest) + return + } + } + if update.FakeIPFilter != nil { + if err := validateAppStoreDomains(*update.FakeIPFilter); err != nil { + mhttp.Error(writer, err.Error(), mhttp.StatusBadRequest) + return + } + } + if err := saveAppStoreDNSPolicyAt(appStoreRoot(), chi.URLParam(request, "id"), update); err != nil { + appStoreRefusal(writer, err) + return + } + writer.Header().Set("Cache-Control", "no-store") + writer.WriteHeader(mhttp.StatusNoContent) +} + +func appStoreRefusal(writer mhttp.ResponseWriter, err error) { + switch { + case errors.Is(err, errAppStoreProfileMissing): + mhttp.Error(writer, err.Error(), mhttp.StatusNotFound) + case errors.Is(err, errAppStoreIndexMissing): + mhttp.Error(writer, err.Error(), mhttp.StatusServiceUnavailable) + default: + mhttp.Error(writer, "the App's profile store cannot be read", mhttp.StatusInternalServerError) + } +} + +func loadAppStoreIndexAt(directory string) (appStoreIndex, error) { + entries, err := readAppStoreProfilesAt(directory) + if err != nil { + return appStoreIndex{}, err + } + active, err := readAppStoreActiveAt(directory) + if err != nil { + return appStoreIndex{}, err + } + summaries := make([]appStoreProfileSummary, 0, len(entries)) + for _, entry := range entries { + override := appStoreOverride(entry) + var patchJSON string + _ = json.Unmarshal(override["patchJSON"], &patchJSON) + var appendRules []string + _ = json.Unmarshal(override["appendRules"], &appendRules) + var prependRules bool + _ = json.Unmarshal(override["prependRules"], &prependRules) + id := appStoreFieldString(entry, "id") + summaries = append(summaries, appStoreProfileSummary{ + ID: id, + Label: appStoreFieldString(entry, "label"), + Order: appStoreFieldInt(entry, "order"), + ActiveRevision: appStoreFieldString(entry, "activeRevision"), + Active: id != "" && id == active.ProfileID, + AppendedRules: len(appendRules), + PrependedRules: prependRules, + Patched: strings.TrimSpace(patchJSON) != "", + }) + } + sort.SliceStable(summaries, func(i, j int) bool { + if summaries[i].Order != summaries[j].Order { + return summaries[i].Order < summaries[j].Order + } + return summaries[i].Label < summaries[j].Label + }) + return appStoreIndex{Active: active, Profiles: summaries}, nil +} + +func loadAppStoreRulesAt(directory, id string) (appStoreRules, error) { + entries, err := readAppStoreProfilesAt(directory) + if err != nil { + return appStoreRules{}, err + } + index := appStoreEntryIndex(entries, id) + if index < 0 { + return appStoreRules{}, errAppStoreProfileMissing + } + override := appStoreOverride(entries[index]) + rules := appStoreRules{ID: id, AppendRules: []string{}} + _ = json.Unmarshal(override["prependRules"], &rules.PrependRules) + _ = json.Unmarshal(override["appendRules"], &rules.AppendRules) + _ = json.Unmarshal(override["patchJSON"], &rules.PatchJSON) + if rules.AppendRules == nil { + rules.AppendRules = []string{} + } + return rules, nil +} + +// saveAppStoreRulesAt rewrites only override.appendRules and override.prependRules. +// Every other field, known or unknown, is carried over byte for byte. +func saveAppStoreRulesAt(directory, id string, update appStoreRulesUpdate) error { + entries, err := readAppStoreProfilesAt(directory) + if err != nil { + return err + } + index := appStoreEntryIndex(entries, id) + if index < 0 { + return errAppStoreProfileMissing + } + entry := entries[index] + override, err := appStoreOverrideObject(entry) + if err != nil { + return err + } + if update.AppendRules != nil { + rules := *update.AppendRules + if rules == nil { + rules = []string{} + } + if err := assignAppStoreRaw(override, "appendRules", rules); err != nil { + return err + } + } + if update.PrependRules != nil { + if err := assignAppStoreRaw(override, "prependRules", *update.PrependRules); err != nil { + return err + } + } + if err := assignAppStoreRaw(entry, "override", override); err != nil { + return err + } + return writeAppStoreProfilesAt(directory, entries) +} + +func loadAppStoreDNSPolicyAt(directory, id string) (appStoreDNSPolicy, error) { + policy := appStoreDNSPolicy{NameserverPolicy: map[string][]string{}, FakeIPFilter: []string{}} + patch, err := loadAppStoreRuntimePatchAt(directory, id) + if err != nil { + return appStoreDNSPolicy{}, err + } + dns := appStoreNestedObject(patch, "dns") + _ = json.Unmarshal(dns["nameserver-policy"], &policy.NameserverPolicy) + _ = json.Unmarshal(dns["fake-ip-filter"], &policy.FakeIPFilter) + if policy.NameserverPolicy == nil { + policy.NameserverPolicy = map[string][]string{} + } + if policy.FakeIPFilter == nil { + policy.FakeIPFilter = []string{} + } + return policy, nil +} + +// saveAppStoreDNSPolicyAt rewrites only the dns keys it was given inside +// legacyRuntimePatchJSON, keeping the rest of that patch and every other field. +func saveAppStoreDNSPolicyAt(directory, id string, update appStoreDNSPolicyUpdate) error { + entries, err := readAppStoreProfilesAt(directory) + if err != nil { + return err + } + index := appStoreEntryIndex(entries, id) + if index < 0 { + return errAppStoreProfileMissing + } + entry := entries[index] + patch, err := appStorePatchObject(entry) + if err != nil { + return err + } + dns := appStoreNestedObject(patch, "dns") + if update.NameserverPolicy != nil { + policy := *update.NameserverPolicy + if policy == nil { + policy = map[string][]string{} + } + if err := assignAppStoreRaw(dns, "nameserver-policy", policy); err != nil { + return err + } + } + if update.FakeIPFilter != nil { + filter := *update.FakeIPFilter + if filter == nil { + filter = []string{} + } + if err := assignAppStoreRaw(dns, "fake-ip-filter", filter); err != nil { + return err + } + } + if err := assignAppStoreRaw(patch, "dns", dns); err != nil { + return err + } + encoded, err := json.Marshal(patch) + if err != nil { + return err + } + if err := assignAppStoreRaw(entry, "legacyRuntimePatchJSON", string(encoded)); err != nil { + return err + } + return writeAppStoreProfilesAt(directory, entries) +} + +func loadAppStoreRuntimePatchAt(directory, id string) (map[string]json.RawMessage, error) { + entries, err := readAppStoreProfilesAt(directory) + if err != nil { + return nil, err + } + index := appStoreEntryIndex(entries, id) + if index < 0 { + return nil, errAppStoreProfileMissing + } + return appStorePatchObject(entries[index]) +} + +func appStorePatchObject(entry map[string]json.RawMessage) (map[string]json.RawMessage, error) { + patch := map[string]json.RawMessage{} + var legacy string + if raw, ok := entry["legacyRuntimePatchJSON"]; ok && string(raw) != "null" { + if err := json.Unmarshal(raw, &legacy); err != nil { + return nil, err + } + } + if strings.TrimSpace(legacy) == "" { + return patch, nil + } + if err := json.Unmarshal([]byte(legacy), &patch); err != nil { + return nil, err + } + if patch == nil { + patch = map[string]json.RawMessage{} + } + return patch, nil +} + +func appStoreNestedObject(parent map[string]json.RawMessage, key string) map[string]json.RawMessage { + nested := map[string]json.RawMessage{} + if raw, ok := parent[key]; ok { + if err := json.Unmarshal(raw, &nested); err != nil || nested == nil { + nested = map[string]json.RawMessage{} + } else { + return nested + } + } + parent[key] = json.RawMessage(`{}`) + return nested +} + +func assignAppStoreRaw(target map[string]json.RawMessage, key string, value any) error { + encoded, err := json.Marshal(value) + if err != nil { + return err + } + target[key] = encoded + return nil +} + +func appStoreOverride(entry map[string]json.RawMessage) map[string]json.RawMessage { + override, _ := appStoreOverrideObject(entry) + return override +} + +func appStoreOverrideObject(entry map[string]json.RawMessage) (map[string]json.RawMessage, error) { + override := map[string]json.RawMessage{} + if raw, ok := entry["override"]; ok && string(raw) != "null" { + if err := json.Unmarshal(raw, &override); err != nil { + return nil, err + } + if override == nil { + override = map[string]json.RawMessage{} + } + } + return override, nil +} + +func appStoreEntryIndex(entries []map[string]json.RawMessage, id string) int { + if id == "" { + return -1 + } + for i, entry := range entries { + if appStoreFieldString(entry, "id") == id { + return i + } + } + return -1 +} + +func appStoreFieldString(entry map[string]json.RawMessage, key string) string { + var value string + _ = json.Unmarshal(entry[key], &value) + return value +} + +func appStoreFieldInt(entry map[string]json.RawMessage, key string) int { + var value int + _ = json.Unmarshal(entry[key], &value) + return value +} + +func readAppStoreProfilesAt(directory string) ([]map[string]json.RawMessage, error) { + data, err := os.ReadFile(filepath.Join(directory, appStoreProfilesFile)) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + return nil, errAppStoreIndexMissing + } + return nil, err + } + var entries []map[string]json.RawMessage + if err := json.Unmarshal(data, &entries); err != nil { + return nil, err + } + return entries, nil +} + +func readAppStoreActiveAt(directory string) (appStoreActive, error) { + var active appStoreActive + data, err := os.ReadFile(filepath.Join(directory, appStoreActiveFile)) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + return active, nil + } + return active, err + } + if err := json.Unmarshal(data, &active); err != nil { + return active, err + } + return active, nil +} + +func writeAppStoreProfilesAt(directory string, entries []map[string]json.RawMessage) error { + if entries == nil { + entries = []map[string]json.RawMessage{} + } + // The App writes this index indented; matching it keeps the file readable + // and the diff small when the App rewrites it after the next connection. + payload, err := json.MarshalIndent(entries, "", " ") + if err != nil { + return err + } + return writeAppStoreFileAtomically(filepath.Join(directory, appStoreProfilesFile), append(payload, '\n')) +} + +func writeAppStoreFileAtomically(path string, payload []byte) error { + directory := filepath.Dir(path) + file, err := os.CreateTemp(directory, ".app-store-") + if err != nil { + return err + } + temporaryPath := file.Name() + committed := false + defer func() { + _ = file.Close() + if !committed { + _ = os.Remove(temporaryPath) + } + }() + if err := file.Chmod(0o600); err != nil { + return err + } + if _, err := file.Write(payload); err != nil { + return err + } + if err := file.Sync(); err != nil { + return err + } + if err := file.Close(); err != nil { + return err + } + if err := os.Rename(temporaryPath, path); err != nil { + return err + } + committed = true + return nil +} + +func validateAppStoreRules(rules []string) error { + if len(rules) > appStoreMaximumAppendRules { + return errors.New("too many appended rules") + } + for _, rule := range rules { + if len(rule) == 0 || len(rule) > appStoreMaximumRuleBytes { + return errors.New("each appended rule must be 1..512 bytes") + } + if strings.ContainsAny(rule, "\n\r\x00") { + return errors.New("an appended rule must not contain a line break or a NUL") + } + } + return nil +} + +func validateAppStoreNameserverPolicy(policy map[string][]string) error { + for domain, servers := range policy { + if err := validateAppStoreDomain(domain); err != nil { + return err + } + for _, server := range servers { + if err := validateAppStoreNameserver(server); err != nil { + return err + } + } + } + return nil +} + +func validateAppStoreDomains(domains []string) error { + for _, domain := range domains { + if err := validateAppStoreDomain(domain); err != nil { + return err + } + } + return nil +} + +func validateAppStoreDomain(domain string) error { + if domain == "" || len(domain) > appStoreMaximumDomainBytes { + return errors.New("each DNS policy domain must be 1..253 bytes") + } + if strings.ContainsAny(domain, "\n\r\x00") { + return errors.New("a DNS policy domain must not contain a line break or a NUL") + } + return nil +} + +var appStoreNameserverSchemes = []string{"https://", "tls://", "quic://", "h3://", "dhcp://"} + +func validateAppStoreNameserver(server string) error { + if len(server) == 0 || len(server) > appStoreMaximumNameserverBytes { + return errors.New("each nameserver must be 1..512 bytes") + } + for _, candidate := range strings.Split(server, ",") { + candidate = strings.TrimSpace(candidate) + if validateAppStoreNameserverAddress(candidate) != nil { + return errors.New("a nameserver must be an IP address or an https://, tls://, quic://, h3:// or dhcp:// address") + } + } + return nil +} + +func validateAppStoreNameserverAddress(address string) error { + if address == "" { + return errors.New("empty nameserver") + } + if net.ParseIP(address) != nil { + return nil + } + for _, scheme := range appStoreNameserverSchemes { + if strings.HasPrefix(address, scheme) && len(address) > len(scheme) { + return nil + } + } + return errors.New("unsupported nameserver address") +} diff --git a/bind/clash/app_store_route_test.go b/bind/clash/app_store_route_test.go new file mode 100644 index 00000000..2f506679 --- /dev/null +++ b/bind/clash/app_store_route_test.go @@ -0,0 +1,430 @@ +package clash + +import ( + "bytes" + "encoding/json" + "errors" + "net/http" + "os" + "path/filepath" + "strings" + "testing" + + C "github.com/ProjectClash/Clash/constant" + mhttp "github.com/metacubex/http" +) + +const appStoreFixtureProfiles = `[ + { + "id": "p-late", + "label": "Zeta", + "order": 2, + "activeRevision": "r2", + "override": {"appendRules": ["DOMAIN,q,DIRECT"], "prependRules": false, "patchJSON": "", "unknownOverride": true}, + "legacyRuntimePatchJSON": "{\"tun\":{\"enable\":true}}", + "source": {"kind": "url"}, + "unknownTop": "keep" + }, + { + "id": "p-active", + "label": "XSUS", + "order": -1, + "activeRevision": "r1", + "override": {"appendRules": ["DOMAIN,a,DIRECT"], "prependRules": true, "patchJSON": "{\"dns\":{}}", "unknownOverride": 7}, + "legacyRuntimePatchJSON": "{\"dns\":{\"nameserver-policy\":{\"+.didichuxing.com\":[\"10.78.226.116\"]},\"fake-ip-filter\":[\"+.example.com\"],\"enhanced-mode\":\"fake-ip\"},\"tun\":{\"enable\":true}}", + "source": {}, + "unknownTop": {"nested": [1, 2]} + } +]` + +const appStoreFixtureActive = `{"profileID":"p-active","revision":"r1","schemaVersion":1}` + +func appStoreFixture(t *testing.T) string { + t.Helper() + home := t.TempDir() + previous := C.Path.HomeDir() + C.SetHomeDir(home) + t.Cleanup(func() { C.SetHomeDir(previous) }) + store := filepath.Join(home, appStoreDirectoryName) + if err := os.MkdirAll(store, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(store, appStoreProfilesFile), []byte(appStoreFixtureProfiles), 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(store, appStoreActiveFile), []byte(appStoreFixtureActive), 0o600); err != nil { + t.Fatal(err) + } + return store +} + +// appStoreCompact normalizes formatting so assertions survive re-indentation. +func appStoreCompact(t *testing.T, raw json.RawMessage) string { + t.Helper() + var buffer bytes.Buffer + if err := json.Compact(&buffer, raw); err != nil { + t.Fatalf("compact %s: %v", raw, err) + } + return buffer.String() +} + +func appStoreRawEntries(t *testing.T, store string) []map[string]json.RawMessage { + t.Helper() + data, err := os.ReadFile(filepath.Join(store, appStoreProfilesFile)) + if err != nil { + t.Fatal(err) + } + var entries []map[string]json.RawMessage + if err := json.Unmarshal(data, &entries); err != nil { + t.Fatalf("profiles.json is not readable after the write: %v", err) + } + return entries +} + +func appStoreEntryOf(t *testing.T, entries []map[string]json.RawMessage, id string) map[string]json.RawMessage { + t.Helper() + index := appStoreEntryIndex(entries, id) + if index < 0 { + t.Fatalf("profile %q vanished", id) + } + return entries[index] +} + +func TestAppStoreIndexReadsTheAppStore(t *testing.T) { + store := appStoreFixture(t) + index, err := loadAppStoreIndexAt(store) + if err != nil { + t.Fatalf("loadAppStoreIndexAt: %v", err) + } + if index.Active.ProfileID != "p-active" || index.Active.Revision != "r1" { + t.Fatalf("active = %+v", index.Active) + } + if len(index.Profiles) != 2 { + t.Fatalf("profiles = %+v", index.Profiles) + } + first, second := index.Profiles[0], index.Profiles[1] + if first.ID != "p-active" || !first.Active || first.AppendedRules != 1 || !first.PrependedRules || !first.Patched { + t.Fatalf("active profile summary = %+v", first) + } + if second.ID != "p-late" || second.Active || second.AppendedRules != 1 || second.PrependedRules || second.Patched { + t.Fatalf("second profile summary = %+v", second) + } +} + +func TestAppStoreRulesUpdateKeepsUnknownFields(t *testing.T) { + store := appStoreFixture(t) + prepend := false + appendRules := []string{"DOMAIN-SUFFIX,x,DIRECT", "IP-CIDR,10.0.0.0/8,DIRECT"} + if err := saveAppStoreRulesAt(store, "p-active", appStoreRulesUpdate{PrependRules: &prepend, AppendRules: &appendRules}); err != nil { + t.Fatalf("saveAppStoreRulesAt: %v", err) + } + rules, err := loadAppStoreRulesAt(store, "p-active") + if err != nil { + t.Fatalf("loadAppStoreRulesAt: %v", err) + } + if rules.PrependRules || len(rules.AppendRules) != 2 || rules.AppendRules[0] != appendRules[0] || rules.PatchJSON != `{"dns":{}}` { + t.Fatalf("rules = %+v", rules) + } + + entries := appStoreRawEntries(t, store) + entry := appStoreEntryOf(t, entries, "p-active") + if appStoreCompact(t, entry["unknownTop"]) != `{"nested":[1,2]}` { + t.Errorf("unknown top-level field = %s", entry["unknownTop"]) + } + override := appStoreOverride(entry) + if appStoreCompact(t, override["unknownOverride"]) != "7" { + t.Errorf("unknown override field = %s", override["unknownOverride"]) + } + if string(override["patchJSON"]) != `"{\"dns\":{}}"` { + t.Errorf("patchJSON = %s", override["patchJSON"]) + } + if string(entry["legacyRuntimePatchJSON"]) == "" || string(entry["legacyRuntimePatchJSON"]) == "null" { + t.Errorf("legacyRuntimePatchJSON = %s", entry["legacyRuntimePatchJSON"]) + } + other := appStoreEntryOf(t, entries, "p-late") + if appStoreCompact(t, other["unknownTop"]) != `"keep"` || appStoreCompact(t, appStoreOverride(other)["unknownOverride"]) != "true" { + t.Errorf("the untouched profile changed: %v", other) + } + + left, err := os.ReadDir(store) + if err != nil { + t.Fatal(err) + } + for _, entry := range left { + if strings.HasPrefix(entry.Name(), ".app-store-") { + t.Errorf("a staging file was left behind: %s", entry.Name()) + } + } + if _, err := loadAppStoreIndexAt(store); err != nil { + t.Errorf("the rewritten index no longer loads: %v", err) + } +} + +func TestAppStoreRulesUpdateRejectsInvalidRules(t *testing.T) { + for name, rules := range map[string][]string{ + "empty rule": {""}, + "line break": {"DOMAIN,a,\nDIRECT"}, + "carriage": {"DOMAIN,a,\rDIRECT"}, + "nul": {"DOMAIN,a,\x00DIRECT"}, + "oversized": {strings.Repeat("a", appStoreMaximumRuleBytes+1)}, + "too many": make([]string, appStoreMaximumAppendRules+1), + } { + if err := validateAppStoreRules(rules); err == nil { + t.Errorf("%s was admitted", name) + } + } + if err := validateAppStoreRules([]string{"DOMAIN-SUFFIX,x,DIRECT"}); err != nil { + t.Errorf("a valid rule was rejected: %v", err) + } + + store := appStoreFixture(t) + before, err := os.ReadFile(filepath.Join(store, appStoreProfilesFile)) + if err != nil { + t.Fatal(err) + } + if err := saveAppStoreRulesAt(store, "no-such-profile", appStoreRulesUpdate{AppendRules: &[]string{}}); !errors.Is(err, errAppStoreProfileMissing) { + t.Errorf("unknown id error = %v", err) + } + after, err := os.ReadFile(filepath.Join(store, appStoreProfilesFile)) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(before, after) { + t.Error("a refused write changed profiles.json") + } +} + +func TestAppStoreDNSPolicyKeepsOtherPatchKeys(t *testing.T) { + store := appStoreFixture(t) + policy := map[string][]string{"+.didichuxing.com": {"10.78.226.116", "https://dns.example/dns-query"}} + if err := saveAppStoreDNSPolicyAt(store, "p-active", appStoreDNSPolicyUpdate{NameserverPolicy: &policy}); err != nil { + t.Fatalf("saveAppStoreDNSPolicyAt: %v", err) + } + read, err := loadAppStoreDNSPolicyAt(store, "p-active") + if err != nil { + t.Fatalf("loadAppStoreDNSPolicyAt: %v", err) + } + if len(read.NameserverPolicy["+.didichuxing.com"]) != 2 || len(read.FakeIPFilter) != 1 || read.FakeIPFilter[0] != "+.example.com" { + t.Fatalf("policy = %+v", read) + } + + entries := appStoreRawEntries(t, store) + entry := appStoreEntryOf(t, entries, "p-active") + patch, err := appStorePatchObject(entry) + if err != nil { + t.Fatalf("the rewritten patch no longer parses: %v", err) + } + if string(patch["tun"]) != `{"enable":true}` { + t.Errorf("the tun key was dropped: %s", patch["tun"]) + } + dns := appStoreNestedObject(patch, "dns") + if string(dns["enhanced-mode"]) != `"fake-ip"` { + t.Errorf("the enhanced-mode key was dropped: %s", dns["enhanced-mode"]) + } + + if err := saveAppStoreDNSPolicyAt(store, "p-active", appStoreDNSPolicyUpdate{NameserverPolicy: &map[string][]string{}}); err != nil { + t.Fatalf("clearing nameserverPolicy: %v", err) + } + cleared, err := loadAppStoreDNSPolicyAt(store, "p-active") + if err != nil { + t.Fatalf("loadAppStoreDNSPolicyAt: %v", err) + } + if len(cleared.NameserverPolicy) != 0 || len(cleared.FakeIPFilter) != 1 { + t.Fatalf("cleared policy = %+v", cleared) + } + entries = appStoreRawEntries(t, store) + patch, err = appStorePatchObject(appStoreEntryOf(t, entries, "p-active")) + if err != nil { + t.Fatal(err) + } + if string(patch["tun"]) != `{"enable":true}` { + t.Errorf("clearing the policy dropped tun: %s", patch["tun"]) + } +} + +func TestAppStoreDNSPolicyDefaultsWhenAbsent(t *testing.T) { + store := t.TempDir() + if err := os.WriteFile(filepath.Join(store, appStoreProfilesFile), []byte(`[{"id":"p","label":"P","order":0}]`), 0o600); err != nil { + t.Fatal(err) + } + policy, err := loadAppStoreDNSPolicyAt(store, "p") + if err != nil { + t.Fatalf("loadAppStoreDNSPolicyAt: %v", err) + } + if policy.NameserverPolicy == nil || len(policy.NameserverPolicy) != 0 { + t.Errorf("nameserverPolicy = %v", policy.NameserverPolicy) + } + if policy.FakeIPFilter == nil || len(policy.FakeIPFilter) != 0 { + t.Errorf("fakeIPFilter = %v", policy.FakeIPFilter) + } + rules, err := loadAppStoreRulesAt(store, "p") + if err != nil { + t.Fatalf("loadAppStoreRulesAt: %v", err) + } + if rules.AppendRules == nil || rules.PrependRules || rules.PatchJSON != "" { + t.Errorf("rules = %+v", rules) + } + if _, err := loadAppStoreRulesAt(store, "nope"); !errors.Is(err, errAppStoreProfileMissing) { + t.Errorf("unknown id error = %v", err) + } + if _, err := loadAppStoreRulesAt(t.TempDir(), "p"); !errors.Is(err, errAppStoreIndexMissing) { + t.Errorf("missing index error = %v", err) + } +} + +func TestAppStoreDNSValidation(t *testing.T) { + for name, policy := range map[string]map[string][]string{ + "empty domain": {"": {"1.1.1.1"}}, + "long domain": {strings.Repeat("a", appStoreMaximumDomainBytes+1): {"1.1.1.1"}}, + "bare hostname": {"+.example.com": {"dns.example.com"}}, + "empty server": {"+.example.com": {""}}, + "long server": {"+.example.com": {"https://" + strings.Repeat("a", appStoreMaximumNameserverBytes)}}, + } { + if err := validateAppStoreNameserverPolicy(policy); err == nil { + t.Errorf("%s was admitted", name) + } + } + if err := validateAppStoreNameserverPolicy(map[string][]string{ + "+.example.com": {"1.1.1.1", "tls://dns.example", "quic://dns.example", "h3://dns.example", "dhcp://en0"}, + }); err != nil { + t.Errorf("valid nameservers were rejected: %v", err) + } + if err := validateAppStoreDomains([]string{"+.example.com", "geosite:cn"}); err != nil { + t.Errorf("valid fake-ip-filter entries were rejected: %v", err) + } +} + +func TestAppStoreRoutesAnswerTheAppSocket(t *testing.T) { + store := appStoreFixture(t) + socket := liveControlPlane(t, controllerConfig(t, "127.0.0.1:"+freeLoopbackPort(t))) + client := unixHTTPClient(socket) + base := "http://unix/clash/v1/app/profiles" + + code, body := requestOver(t, client, http.MethodGet, base, "", "", nil) + var index struct { + Active struct { + ProfileID string `json:"profileID"` + Revision string `json:"revision"` + } `json:"active"` + Profiles []appStoreProfileSummary `json:"profiles"` + } + if code != http.StatusOK || json.Unmarshal(body, &index) != nil || index.Active.ProfileID != "p-active" || len(index.Profiles) != 2 { + t.Fatalf("GET profiles: %d %s", code, body) + } + + code, body = requestOver(t, client, http.MethodGet, base+"/no-such-profile/rules", "", "", nil) + if code != http.StatusNotFound { + t.Fatalf("GET rules for an unknown id: %d %s", code, body) + } + + update := []byte(`{"prependRules":false,"appendRules":["DOMAIN-SUFFIX,x,DIRECT"]}`) + code, body = requestOver(t, client, http.MethodPut, base+"/p-active/rules", "", "application/json", update) + if code != http.StatusNoContent { + t.Fatalf("PUT rules: %d %s", code, body) + } + code, body = requestOver(t, client, http.MethodGet, base+"/p-active/rules", "", "", nil) + var rules appStoreRules + if code != http.StatusOK || json.Unmarshal(body, &rules) != nil || rules.PrependRules || len(rules.AppendRules) != 1 || rules.AppendRules[0] != "DOMAIN-SUFFIX,x,DIRECT" { + t.Fatalf("GET rules: %d %s", code, body) + } + before, err := os.ReadFile(filepath.Join(store, appStoreProfilesFile)) + if err != nil { + t.Fatal(err) + } + code, body = requestOver(t, client, http.MethodPut, base+"/p-active/rules", "", "application/json", + []byte(`{"prependRules":true,"appendRules":["DOMAIN,a,\nDIRECT"]}`)) + if code != http.StatusBadRequest { + t.Fatalf("PUT an invalid rule: %d %s", code, body) + } + after, err := os.ReadFile(filepath.Join(store, appStoreProfilesFile)) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(before, after) { + t.Error("a refused PUT changed profiles.json") + } + + code, body = requestOver(t, client, http.MethodGet, base+"/p-active/dns-policy", "", "", nil) + var policy appStoreDNSPolicy + if code != http.StatusOK || json.Unmarshal(body, &policy) != nil || policy.NameserverPolicy["+.didichuxing.com"][0] != "10.78.226.116" { + t.Fatalf("GET dns-policy: %d %s", code, body) + } + code, body = requestOver(t, client, http.MethodPut, base+"/p-active/dns-policy", "", "application/json", + []byte(`{"nameserverPolicy":{"+.example.com":["1.1.1.1"]}}`)) + if code != http.StatusNoContent { + t.Fatalf("PUT dns-policy: %d %s", code, body) + } + code, body = requestOver(t, client, http.MethodPut, base+"/p-active/dns-policy", "", "application/json", + []byte(`{"nameserverPolicy":{"+.example.com":["not an address"]}}`)) + if code != http.StatusBadRequest { + t.Fatalf("PUT an invalid nameserver: %d %s", code, body) + } +} + +func TestAppStoreRefusesOtherSocketsAndMissingIndex(t *testing.T) { + recorder := &appStoreRecorder{} + request, err := mhttp.NewRequest(http.MethodGet, "http://127.0.0.1:9090/clash/v1/app/profiles", nil) + if err != nil { + t.Fatal(err) + } + serveAppProfiles(recorder, request) + if recorder.code != http.StatusNotFound { + t.Errorf("a TCP request got HTTP %d", recorder.code) + } + + home := t.TempDir() + previous := C.Path.HomeDir() + C.SetHomeDir(home) + t.Cleanup(func() { C.SetHomeDir(previous) }) + socket := liveControlPlane(t, controllerConfig(t, "127.0.0.1:"+freeLoopbackPort(t))) + code, body := requestOver(t, unixHTTPClient(socket), http.MethodGet, "http://unix/clash/v1/app/profiles", "", "", nil) + if code != http.StatusServiceUnavailable || !strings.Contains(string(body), "has not published its profile index yet") { + t.Fatalf("GET profiles without an index: %d %s", code, body) + } + if err := os.MkdirAll(filepath.Join(home, appStoreDirectoryName), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(home, appStoreDirectoryName, appStoreProfilesFile), []byte("{"), 0o600); err != nil { + t.Fatal(err) + } + code, body = requestOver(t, unixHTTPClient(socket), http.MethodGet, "http://unix/clash/v1/app/profiles", "", "", nil) + if code != http.StatusInternalServerError { + t.Fatalf("GET profiles with a broken index: %d %s", code, body) + } +} + +type appStoreRecorder struct { + header mhttp.Header + code int + body bytes.Buffer +} + +func (r *appStoreRecorder) Header() mhttp.Header { + if r.header == nil { + r.header = mhttp.Header{} + } + return r.header +} + +func (r *appStoreRecorder) Write(payload []byte) (int, error) { + if r.code == 0 { + r.code = http.StatusOK + } + return r.body.Write(payload) +} + +func (r *appStoreRecorder) WriteHeader(code int) { + if r.code == 0 { + r.code = code + } +} + +func TestAppStoreCapabilityIsAdvertised(t *testing.T) { + found := false + for _, capability := range controllerCapabilities { + found = found || capability == "app-store-v1" + } + if !found { + t.Errorf("controllerCapabilities lacks app-store-v1: %v", controllerCapabilities) + } +} diff --git a/bind/clash/remote_control_route.go b/bind/clash/remote_control_route.go index ba684e05..95b59c1d 100644 --- a/bind/clash/remote_control_route.go +++ b/bind/clash/remote_control_route.go @@ -22,6 +22,7 @@ var controllerCapabilities = []string{ "mode-stream-v1", "stun-v1", "provider-health-check-v1", + "app-store-v1", } func init() {