Skip to main content

rss_proxy/
server.rs

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