MCPcopy Create free account
hub / github.com/AandStep/ResultV / AddSubscription

Method AddSubscription

app.go:2346–2440  ·  view source on GitHub ↗

AddSubscription stores a new subscription. allowInsecure=true must be passed explicitly for http:// URLs after the user has confirmed the warning. The consent is persisted on the Subscription record so RefreshSubscription doesn't need to re-prompt.

(name, subURL string, allowInsecure bool, source string)

Source from the content-addressed store, hash-verified

2344// warning. The consent is persisted on the Subscription record so
2345// RefreshSubscription doesn't need to re-prompt.
2346func (a *App) AddSubscription(name, subURL string, allowInsecure bool, source string) ([]config.ProxyEntry, error) {
2347 if a.config == nil {
2348 return nil, fmt.Errorf("config manager not initialized")
2349 }
2350
2351 cfg := a.config.GetConfig()
2352 staleIdx := -1
2353 for i, s := range cfg.Subscriptions {
2354 if s.URL != subURL {
2355 continue
2356 }
2357 hasProxies := false
2358 for _, p := range cfg.Proxies {
2359 if p.SubscriptionURL == subURL {
2360 hasProxies = true
2361 break
2362 }
2363 }
2364 if hasProxies {
2365 return nil, fmt.Errorf("подписка с этим URL уже добавлена")
2366 }
2367 // Subscription record is orphaned (proxies were deleted but the
2368 // subscription metadata stayed). Drop it so the new import can take
2369 // its place.
2370 staleIdx = i
2371 break
2372 }
2373 if staleIdx >= 0 {
2374 cfg.Subscriptions = append(cfg.Subscriptions[:staleIdx], cfg.Subscriptions[staleIdx+1:]...)
2375 if err := a.config.SaveConfig(cfg); err != nil {
2376 return nil, fmt.Errorf("clearing stale subscription: %w", err)
2377 }
2378 }
2379
2380 entries, up, down, tot, exp, iconURL, profileTitle, err := a.fetchSubscriptionFromURL(subURL, allowInsecure)
2381 if err != nil {
2382 return nil, err
2383 }
2384
2385 displayName := name
2386 if profileTitle != "" {
2387 displayName = profileTitle
2388 }
2389
2390 sub := config.Subscription{
2391 ID: fmt.Sprintf("%d", time.Now().UnixMilli()),
2392 Name: displayName,
2393 URL: subURL,
2394 UpdatedAt: time.Now().Format(time.RFC3339),
2395 TrafficUpload: up,
2396 TrafficDownload: down,
2397 TrafficTotal: tot,
2398 ExpireUnix: exp,
2399 IconURL: iconURL,
2400 Source: strings.TrimSpace(source),
2401 // Only mark as allow-insecure when the URL actually is plaintext —
2402 // no need to flag https:// subscriptions, which would be misleading.
2403 AllowInsecure: allowInsecure && isInsecureSubURL(subURL),

Callers

nothing calls this directly

Calls 5

isInsecureSubURLFunction · 0.85
SuccessMethod · 0.80
GetConfigMethod · 0.45
SaveConfigMethod · 0.45

Tested by

no test coverage detected