4
0

subscription.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api // import "miniflux.app/v2/internal/api"
  4. import (
  5. json_parser "encoding/json"
  6. "net/http"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/http/request"
  9. "miniflux.app/v2/internal/http/response/json"
  10. "miniflux.app/v2/internal/model"
  11. "miniflux.app/v2/internal/proxyrotator"
  12. "miniflux.app/v2/internal/reader/fetcher"
  13. "miniflux.app/v2/internal/reader/subscription"
  14. "miniflux.app/v2/internal/validator"
  15. )
  16. func (h *handler) discoverSubscriptions(w http.ResponseWriter, r *http.Request) {
  17. var subscriptionDiscoveryRequest model.SubscriptionDiscoveryRequest
  18. if err := json_parser.NewDecoder(r.Body).Decode(&subscriptionDiscoveryRequest); err != nil {
  19. json.BadRequest(w, r, err)
  20. return
  21. }
  22. if validationErr := validator.ValidateSubscriptionDiscovery(&subscriptionDiscoveryRequest); validationErr != nil {
  23. json.BadRequest(w, r, validationErr.Error())
  24. return
  25. }
  26. var rssbridgeURL string
  27. var rssbridgeToken string
  28. intg, err := h.store.Integration(request.UserID(r))
  29. if err == nil && intg != nil && intg.RSSBridgeEnabled {
  30. rssbridgeURL = intg.RSSBridgeURL
  31. rssbridgeToken = intg.RSSBridgeToken
  32. }
  33. requestBuilder := fetcher.NewRequestBuilder()
  34. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  35. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  36. requestBuilder.WithCustomFeedProxyURL(subscriptionDiscoveryRequest.ProxyURL)
  37. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  38. requestBuilder.UseCustomApplicationProxyURL(subscriptionDiscoveryRequest.FetchViaProxy)
  39. requestBuilder.WithUserAgent(subscriptionDiscoveryRequest.UserAgent, config.Opts.HTTPClientUserAgent())
  40. requestBuilder.WithCookie(subscriptionDiscoveryRequest.Cookie)
  41. requestBuilder.WithUsernameAndPassword(subscriptionDiscoveryRequest.Username, subscriptionDiscoveryRequest.Password)
  42. requestBuilder.IgnoreTLSErrors(subscriptionDiscoveryRequest.AllowSelfSignedCertificates)
  43. requestBuilder.DisableHTTP2(subscriptionDiscoveryRequest.DisableHTTP2)
  44. subscriptions, localizedError := subscription.NewSubscriptionFinder(requestBuilder).FindSubscriptions(
  45. subscriptionDiscoveryRequest.URL,
  46. rssbridgeURL,
  47. rssbridgeToken,
  48. )
  49. if localizedError != nil {
  50. json.ServerError(w, r, localizedError.Error())
  51. return
  52. }
  53. if len(subscriptions) == 0 {
  54. json.NotFound(w, r)
  55. return
  56. }
  57. json.OK(w, r, subscriptions)
  58. }