1use crate::config::{AppConfig, FeedConfig, FeedFilterConfig};
2use crate::fetcher::FeedFetcher;
3use crate::opml::{generate_grouped_opml, ExportableFeed};
4use crate::plugin::builtin::{
5 AgeFilterPlugin, FeedCleanerPlugin, KeywordFilterPlugin, LimitPlugin, LinkCleanerPlugin,
6 NopHandlerPlugin, RegexFilterPlugin,
7};
8use crate::plugin::rhai_plugin::RhaiPlugin;
9use crate::plugin::{Plugin, PluginPipeline};
10use axum::{
11 extract::{Path, Query, State},
12 http::{header, HeaderValue, StatusCode},
13 response::{IntoResponse, Response},
14 routing::get,
15 Json, Router,
16};
17use serde::{Deserialize, Serialize};
18use std::collections::HashSet;
19use std::net::IpAddr;
20use std::path::PathBuf;
21use std::sync::Arc;
22use tokio::sync::RwLock;
23use tracing::{error, info};
24use url::Url;
25
26pub struct AppState {
27 pub config: Arc<RwLock<AppConfig>>,
28 pub config_path: PathBuf,
29 pub fetcher: FeedFetcher,
30}
31
32fn validate_url_for_ssrf(url: &str) -> Result<(), &'static str> {
34 let parsed = Url::parse(url).map_err(|_| "Invalid URL")?;
35
36 if parsed.scheme() != "http" && parsed.scheme() != "https" {
38 return Err("URL must use http or https scheme");
39 }
40
41 let host = parsed.host_str().ok_or("URL must have a valid host")?;
43
44 if host == "localhost" || host == "127.0.0.1" || host == "::1" || host == "[::1]" {
46 return Err("URL cannot point to localhost or loopback address");
47 }
48
49 if let Ok(ip) = host.parse::<IpAddr>() {
51 match ip {
53 IpAddr::V4(ipv4) => {
54 if ipv4.is_loopback()
55 || ipv4.is_private()
56 || ipv4.is_link_local()
57 || ipv4.is_broadcast()
58 || ipv4 == std::net::Ipv4Addr::new(169, 254, 169, 254)
59 {
61 return Err("URL cannot point to private or internal IP address");
62 }
63 }
64 IpAddr::V6(ipv6) => {
65 if ipv6.is_loopback() || ipv6.is_unique_local() || ipv6.is_unicast_link_local() {
66 return Err("URL cannot point to private or internal IP address");
67 }
68 }
69 }
70 }
71
72 Ok(())
73}
74
75#[derive(Debug, Deserialize)]
76pub struct ProxyQuery {
77 pub url: String,
78 pub group: Option<String>,
79 pub include: Option<String>,
80 pub exclude: Option<String>,
81 pub include_regex: Option<String>,
82 pub exclude_regex: Option<String>,
83 pub clean_links: Option<bool>,
84 pub max_age_days: Option<i64>,
85 pub limit: Option<usize>,
86 pub script: Option<String>,
87}
88
89pub fn create_router(state: Arc<AppState>) -> Router {
90 Router::new()
91 .route("/", get(handle_root))
92 .route("/health", get(handle_health))
93 .route("/plugins", get(handle_list_plugins))
94 .route("/opml", get(handle_export_opml))
95 .route("/feeds", get(handle_all_feeds))
96 .route("/feeds/", get(handle_all_feeds))
97 .route("/feeds/{slug}", get(handle_configured_feed))
98 .route("/feeds/{slug}/", get(handle_configured_feed))
99 .route("/groups/{group}", get(handle_group_feed))
100 .route("/groups/{group}/", get(handle_group_feed))
101 .route("/categories/{group}", get(handle_group_feed))
102 .route("/categories/{group}/", get(handle_group_feed))
103 .route("/proxy", get(handle_proxy_feed))
104 .with_state(state)
105}
106
107#[derive(Serialize)]
108struct StatusResponse {
109 status: &'static str,
110 version: &'static str,
111 configured_feeds: Vec<String>,
112 groups: Vec<String>,
113 available_plugins: Vec<String>,
114 auto_register_proxy_feeds: bool,
115}
116
117async fn handle_root(State(state): State<Arc<AppState>>) -> Json<StatusResponse> {
118 let (configured_feeds, groups, plugins_dir, auto_register) = {
119 let cfg = state.config.read().await;
120 let feeds = cfg.feeds.keys().cloned().collect();
121 let mut groups_set = HashSet::new();
122 for f in cfg.feeds.values() {
123 if let Some(ref g) = f.group {
124 groups_set.insert(g.clone());
125 }
126 }
127 let mut groups: Vec<String> = groups_set.into_iter().collect();
128 groups.sort();
129 (
130 feeds,
131 groups,
132 cfg.plugins_dir.clone(),
133 cfg.auto_register_proxy_feeds,
134 )
135 };
136
137 let plugins = RhaiPlugin::load_directory(&plugins_dir);
138 let available_plugins = plugins.iter().map(|p| p.name().to_string()).collect();
139
140 Json(StatusResponse {
141 status: "running",
142 version: env!("CARGO_PKG_VERSION"),
143 configured_feeds,
144 groups,
145 available_plugins,
146 auto_register_proxy_feeds: auto_register,
147 })
148}
149
150async fn handle_health() -> &'static str {
151 "OK"
152}
153
154async fn handle_list_plugins(State(state): State<Arc<AppState>>) -> Json<Vec<String>> {
155 let plugins_dir = {
156 let cfg = state.config.read().await;
157 cfg.plugins_dir.clone()
158 };
159 let plugins = RhaiPlugin::load_directory(&plugins_dir);
160 Json(plugins.into_iter().map(|p| p.name().to_string()).collect())
161}
162
163async fn handle_export_opml(State(state): State<Arc<AppState>>) -> Response {
164 let feeds = {
165 let cfg = state.config.read().await;
166 let base = format!("http://{}:{}", cfg.server.host, cfg.server.port);
167 let list: Vec<ExportableFeed> = cfg
168 .feeds
169 .iter()
170 .map(|(slug, f)| {
171 let title = f.title_override.clone().unwrap_or_else(|| slug.clone());
172 let proxy_url = format!("{}/feeds/{}", base, slug);
173 ExportableFeed {
174 title,
175 xml_url: proxy_url,
176 html_url: None,
177 group: f.group.clone(),
178 }
179 })
180 .collect();
181 list
182 };
183
184 let opml_xml = generate_grouped_opml("RSS Proxy Filtered Subscriptions", &feeds);
185
186 (
187 StatusCode::OK,
188 [
189 (header::CONTENT_TYPE, "text/x-opml; charset=utf-8"),
190 (
191 header::CONTENT_DISPOSITION,
192 "attachment; filename=\"rss-proxy-subscriptions.opml\"",
193 ),
194 ],
195 opml_xml,
196 )
197 .into_response()
198}
199
200fn build_pipeline_for_feed(
201 feed_cfg: &FeedConfig,
202 plugins_dir: &str,
203) -> Result<PluginPipeline, String> {
204 let mut pipeline = PluginPipeline::new();
205
206 pipeline.add_plugin(Box::new(FeedCleanerPlugin::new()));
208
209 pipeline.add_plugin(Box::new(NopHandlerPlugin::new()));
211
212 if !feed_cfg.filters.include_keywords.is_empty()
214 || !feed_cfg.filters.exclude_keywords.is_empty()
215 {
216 pipeline.add_plugin(Box::new(KeywordFilterPlugin::new(
217 feed_cfg.filters.include_keywords.clone(),
218 feed_cfg.filters.exclude_keywords.clone(),
219 )));
220 }
221
222 if !feed_cfg.filters.include_regex.is_empty() || !feed_cfg.filters.exclude_regex.is_empty() {
224 match RegexFilterPlugin::new(
225 &feed_cfg.filters.include_regex,
226 &feed_cfg.filters.exclude_regex,
227 ) {
228 Ok(plugin) => pipeline.add_plugin(Box::new(plugin)),
229 Err(e) => return Err(format!("Invalid regex configuration: {}", e)),
230 }
231 }
232
233 if feed_cfg.filters.clean_links {
235 pipeline.add_plugin(Box::new(LinkCleanerPlugin::new()));
236 }
237
238 if let Some(days) = feed_cfg.filters.max_age_days {
240 pipeline.add_plugin(Box::new(AgeFilterPlugin::from_days(days)));
241 }
242
243 for script_name in &feed_cfg.filters.scripts {
245 let script_file = if script_name.ends_with(".rhai") {
246 script_name.clone()
247 } else {
248 format!("{}.rhai", script_name)
249 };
250 let path = PathBuf::from(plugins_dir).join(&script_file);
251 match RhaiPlugin::from_file(&path) {
252 Ok(plugin) => pipeline.add_plugin(Box::new(plugin)),
253 Err(e) => {
254 error!("Failed to load plugin script {}: {}", path.display(), e);
255 }
256 }
257 }
258
259 if let Some(limit) = feed_cfg.filters.max_items {
261 pipeline.add_plugin(Box::new(LimitPlugin::new(limit)));
262 }
263
264 Ok(pipeline)
265}
266
267async fn handle_all_feeds(State(state): State<Arc<AppState>>) -> Response {
269 let (feeds_list, host, port, plugins_dir) = {
270 let cfg = state.config.read().await;
271 let list: Vec<(String, FeedConfig)> = cfg
272 .feeds
273 .iter()
274 .map(|(k, v)| (k.clone(), v.clone()))
275 .collect();
276 (
277 list,
278 cfg.server.host.clone(),
279 cfg.server.port,
280 cfg.plugins_dir.clone(),
281 )
282 };
283
284 render_aggregated_feed(
285 &state,
286 "RSS Proxy - All Feeds",
287 &feeds_list,
288 &host,
289 port,
290 &plugins_dir,
291 )
292 .await
293}
294
295async fn handle_group_feed(
297 Path(group_param): Path<String>,
298 State(state): State<Arc<AppState>>,
299) -> Response {
300 let clean_group = group_param.trim_end_matches('/').to_lowercase();
301 let (matching_feeds, host, port, plugins_dir) = {
302 let cfg = state.config.read().await;
303 let list: Vec<(String, FeedConfig)> = cfg
304 .feeds
305 .iter()
306 .filter(|(_, f)| {
307 f.group
308 .as_ref()
309 .map(|g| g.to_lowercase() == clean_group)
310 .unwrap_or(false)
311 })
312 .map(|(k, v)| (k.clone(), v.clone()))
313 .collect();
314 (
315 list,
316 cfg.server.host.clone(),
317 cfg.server.port,
318 cfg.plugins_dir.clone(),
319 )
320 };
321
322 if matching_feeds.is_empty() {
323 return (
324 StatusCode::NOT_FOUND,
325 format!("No feeds found in group '{}'", group_param),
326 )
327 .into_response();
328 }
329
330 let group_title = matching_feeds
331 .first()
332 .and_then(|(_, cfg)| cfg.group.clone())
333 .unwrap_or(group_param);
334
335 render_aggregated_feed(
336 &state,
337 &format!("RSS Proxy - {}", group_title),
338 &matching_feeds,
339 &host,
340 port,
341 &plugins_dir,
342 )
343 .await
344}
345
346async fn render_aggregated_feed(
347 state: &Arc<AppState>,
348 feed_title: &str,
349 feeds_to_include: &[(String, FeedConfig)],
350 host: &str,
351 port: u16,
352 plugins_dir: &str,
353) -> Response {
354 if feeds_to_include.is_empty() {
355 let empty_feed = crate::model::Feed {
356 title: feed_title.to_string(),
357 link: format!("http://{}:{}/feeds", host, port),
358 description: "No feeds in this view".to_string(),
359 language: Some("en".to_string()),
360 items: Vec::new(),
361 };
362 return match empty_feed.to_rss_xml() {
363 Ok(xml) => (
364 StatusCode::OK,
365 [(header::CONTENT_TYPE, "application/rss+xml; charset=utf-8")],
366 xml,
367 )
368 .into_response(),
369 Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
370 };
371 }
372
373 let mut all_items = Vec::new();
374
375 for (slug, feed_cfg) in feeds_to_include {
376 if feed_cfg.nop {
378 continue;
379 }
380
381 let mut feed = match state.fetcher.fetch(&feed_cfg.url).await {
382 Ok(f) => f,
383 Err(e) => {
384 error!("Skipping failed feed in aggregate view {}: {}", slug, e);
386 continue;
387 }
388 };
389
390 if let Some(ref override_title) = feed_cfg.title_override {
391 feed.title = override_title.clone();
392 }
393
394 let pipeline = match build_pipeline_for_feed(feed_cfg, plugins_dir) {
395 Ok(p) => p,
396 Err(e) => {
397 error!("Error building pipeline for {}: {}", slug, e);
398 continue;
399 }
400 };
401
402 if let Err(e) = pipeline.process(&mut feed).await {
403 error!("Error processing pipeline for {}: {}", slug, e);
404 continue;
405 }
406
407 all_items.extend(feed.items);
408 }
409
410 all_items.sort_by(|a, b| {
412 let date_a = a.published.or(a.updated);
413 let date_b = b.published.or(b.updated);
414 date_b.cmp(&date_a)
415 });
416
417 let mut seen_ids = HashSet::new();
419 all_items.retain(|item| {
420 let key = if !item.id.is_empty() {
421 item.id.clone()
422 } else {
423 item.link.clone()
424 };
425 seen_ids.insert(key)
426 });
427
428 if all_items.len() > 100 {
429 all_items.truncate(100);
430 }
431
432 let aggregate_feed = crate::model::Feed {
433 title: feed_title.to_string(),
434 link: format!("http://{}:{}/feeds", host, port),
435 description: format!("Aggregated and filtered feeds for {}", feed_title),
436 language: Some("en".to_string()),
437 items: all_items,
438 };
439
440 match aggregate_feed.to_rss_xml() {
441 Ok(xml) => (
442 StatusCode::OK,
443 [(header::CONTENT_TYPE, "application/rss+xml; charset=utf-8")],
444 xml,
445 )
446 .into_response(),
447 Err(e) => (
448 StatusCode::INTERNAL_SERVER_ERROR,
449 format!("XML generation error: {}", e),
450 )
451 .into_response(),
452 }
453}
454
455async fn handle_configured_feed(
456 Path(slug): Path<String>,
457 State(state): State<Arc<AppState>>,
458) -> Response {
459 let clean_slug = slug.trim_end_matches('/');
460
461 let (feed_cfg, plugins_dir) = {
462 let cfg = state.config.read().await;
463 match cfg.feeds.get(clean_slug) {
464 Some(c) => (c.clone(), cfg.plugins_dir.clone()),
465 None => {
466 return (
467 StatusCode::NOT_FOUND,
468 format!("Feed with slug '{}' not found in config", clean_slug),
469 )
470 .into_response();
471 }
472 }
473 };
474
475 let feed_title = feed_cfg.title_override.as_deref().unwrap_or(clean_slug);
476
477 if feed_cfg.nop {
479 let reason = feed_cfg
480 .nop_reason
481 .as_deref()
482 .unwrap_or("Marked as NOP in configuration");
483 let nop_feed = NopHandlerPlugin::make_nop_feed(&feed_cfg.url, feed_title, reason);
484 return match nop_feed.to_rss_xml() {
485 Ok(xml) => (
486 StatusCode::OK,
487 [(header::CONTENT_TYPE, "application/rss+xml; charset=utf-8")],
488 xml,
489 )
490 .into_response(),
491 Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
492 };
493 }
494
495 let pipeline = match build_pipeline_for_feed(&feed_cfg, &plugins_dir) {
497 Ok(p) => p,
498 Err(e) => {
499 error!("Invalid config for feed {}: {}", clean_slug, e);
500 return (StatusCode::INTERNAL_SERVER_ERROR, e).into_response();
501 }
502 };
503
504 let mut feed = match state.fetcher.fetch(&feed_cfg.url).await {
506 Ok(f) => f,
507 Err(e) => {
508 if let Some(nop_feed) = pipeline
509 .handle_fetch_error(&feed_cfg.url, feed_title, &e)
510 .await
511 {
512 return match nop_feed.to_rss_xml() {
513 Ok(xml) => (
514 StatusCode::OK,
515 [(header::CONTENT_TYPE, "application/rss+xml; charset=utf-8")],
516 xml,
517 )
518 .into_response(),
519 Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err).into_response(),
520 };
521 }
522
523 error!("Failed to fetch upstream feed {}: {}", feed_cfg.url, e);
524 return (
525 StatusCode::BAD_GATEWAY,
526 format!("Failed to fetch upstream feed: {}", e),
527 )
528 .into_response();
529 }
530 };
531
532 if let Some(ref override_title) = feed_cfg.title_override {
533 feed.title = override_title.clone();
534 }
535
536 if let Err(e) = pipeline.process(&mut feed).await {
538 error!("Pipeline processing failed for {}: {}", clean_slug, e);
539 return (
540 StatusCode::INTERNAL_SERVER_ERROR,
541 format!("Pipeline error: {}", e),
542 )
543 .into_response();
544 }
545
546 match feed.to_rss_xml() {
548 Ok(xml) => (
549 StatusCode::OK,
550 [(header::CONTENT_TYPE, "application/rss+xml; charset=utf-8")],
551 xml,
552 )
553 .into_response(),
554 Err(e) => (
555 StatusCode::INTERNAL_SERVER_ERROR,
556 format!("XML generation error: {}", e),
557 )
558 .into_response(),
559 }
560}
561
562async fn handle_proxy_feed(
563 Query(params): Query<ProxyQuery>,
564 State(state): State<Arc<AppState>>,
565) -> Response {
566 if params.url.trim().is_empty() {
567 return (
568 StatusCode::BAD_REQUEST,
569 "Missing required 'url' query parameter",
570 )
571 .into_response();
572 }
573
574 if let Err(e) = validate_url_for_ssrf(¶ms.url) {
576 return (
577 StatusCode::BAD_REQUEST,
578 format!("Invalid or unsafe URL: {}", e),
579 )
580 .into_response();
581 }
582
583 let mut pipeline = PluginPipeline::new();
584
585 pipeline.add_plugin(Box::new(FeedCleanerPlugin::new()));
587 pipeline.add_plugin(Box::new(NopHandlerPlugin::new()));
588
589 let include_kws: Vec<String> = params
591 .include
592 .map(|s| s.split(',').map(|k| k.trim().to_string()).collect())
593 .unwrap_or_default();
594 let exclude_kws: Vec<String> = params
595 .exclude
596 .map(|s| s.split(',').map(|k| k.trim().to_string()).collect())
597 .unwrap_or_default();
598
599 if !include_kws.is_empty() || !exclude_kws.is_empty() {
600 pipeline.add_plugin(Box::new(KeywordFilterPlugin::new(
601 include_kws.clone(),
602 exclude_kws.clone(),
603 )));
604 }
605
606 let include_regex = params.include_regex.map(|r| vec![r]).unwrap_or_default();
608 let exclude_regex = params.exclude_regex.map(|r| vec![r]).unwrap_or_default();
609
610 if !include_regex.is_empty() || !exclude_regex.is_empty() {
611 match RegexFilterPlugin::new(&include_regex, &exclude_regex) {
612 Ok(p) => pipeline.add_plugin(Box::new(p)),
613 Err(e) => {
614 return (
615 StatusCode::BAD_REQUEST,
616 format!("Invalid regex parameter: {}", e),
617 )
618 .into_response();
619 }
620 }
621 }
622
623 if params.clean_links.unwrap_or(true) {
625 pipeline.add_plugin(Box::new(LinkCleanerPlugin::new()));
626 }
627
628 if let Some(days) = params.max_age_days {
630 pipeline.add_plugin(Box::new(AgeFilterPlugin::from_days(days)));
631 }
632
633 let plugins_dir = {
635 let cfg = state.config.read().await;
636 cfg.plugins_dir.clone()
637 };
638
639 if let Some(ref script_name) = params.script {
640 let script_file = if script_name.ends_with(".rhai") {
641 script_name.clone()
642 } else {
643 format!("{}.rhai", script_name)
644 };
645 let path = PathBuf::from(&plugins_dir).join(&script_file);
646 match RhaiPlugin::from_file(&path) {
647 Ok(p) => pipeline.add_plugin(Box::new(p)),
648 Err(e) => {
649 return (
650 StatusCode::BAD_REQUEST,
651 format!("Failed to load plugin '{}': {}", script_file, e),
652 )
653 .into_response();
654 }
655 }
656 }
657
658 if let Some(limit) = params.limit {
660 pipeline.add_plugin(Box::new(LimitPlugin::new(limit)));
661 }
662
663 let mut feed = match state.fetcher.fetch(¶ms.url).await {
665 Ok(f) => f,
666 Err(e) => match pipeline
667 .handle_fetch_error(¶ms.url, ¶ms.url, &e)
668 .await
669 {
670 Some(nop_feed) => nop_feed,
671 None => {
672 return (
673 StatusCode::BAD_GATEWAY,
674 format!("Failed to fetch upstream feed: {}", e),
675 )
676 .into_response();
677 }
678 },
679 };
680
681 let auto_register = {
683 let cfg = state.config.read().await;
684 cfg.auto_register_proxy_feeds
685 };
686
687 let mut registered_slug = None;
688
689 if auto_register {
690 let mut cfg = state.config.write().await;
691 let existing = cfg
692 .feeds
693 .iter()
694 .find(|(_, f)| f.url == params.url)
695 .map(|(s, _)| s.clone());
696 if let Some(slug) = existing {
697 registered_slug = Some(slug);
698 } else {
699 let base_slug = crate::opml::slugify(&feed.title);
700 let existing_slugs: HashSet<String> = cfg.feeds.keys().cloned().collect();
701 let slug = crate::opml::unique_slug(&base_slug, &existing_slugs);
702
703 let feed_config = FeedConfig {
704 url: params.url.clone(),
705 title_override: Some(feed.title.clone()),
706 group: params.group.clone().or_else(|| Some("Proxied".to_string())),
707 nop: false,
708 nop_reason: None,
709 filters: FeedFilterConfig {
710 include_keywords: include_kws,
711 exclude_keywords: exclude_kws,
712 include_regex,
713 exclude_regex,
714 clean_links: params.clean_links.unwrap_or(true),
715 max_age_days: params.max_age_days,
716 max_items: params.limit,
717 scripts: params.script.into_iter().collect(),
718 },
719 };
720
721 cfg.feeds.insert(slug.clone(), feed_config);
722 registered_slug = Some(slug.clone());
723
724 if let Ok(yaml) = serde_yaml::to_string(&*cfg) {
725 if let Err(e) = std::fs::write(&state.config_path, yaml) {
726 error!(
727 "Failed to persist auto-registered feed to {:?}: {}",
728 state.config_path, e
729 );
730 } else {
731 info!(
732 "Auto-registered new feed '{}' as '/feeds/{}' in {:?}",
733 feed.title, slug, state.config_path
734 );
735 }
736 }
737 }
738 }
739
740 if let Err(e) = pipeline.process(&mut feed).await {
742 return (
743 StatusCode::INTERNAL_SERVER_ERROR,
744 format!("Pipeline error: {}", e),
745 )
746 .into_response();
747 }
748
749 match feed.to_rss_xml() {
751 Ok(xml) => {
752 let mut response = (
753 StatusCode::OK,
754 [(header::CONTENT_TYPE, "application/rss+xml; charset=utf-8")],
755 xml,
756 )
757 .into_response();
758
759 if let Some(slug) = registered_slug {
760 if let Ok(val) = HeaderValue::from_str(&slug) {
761 response.headers_mut().insert("X-Feed-Slug", val);
762 }
763 }
764
765 response
766 }
767 Err(e) => (
768 StatusCode::INTERNAL_SERVER_ERROR,
769 format!("XML generation error: {}", e),
770 )
771 .into_response(),
772 }
773}