Skip to content

Commit

Permalink
Add insecure mode
Browse files Browse the repository at this point in the history
Closes: #29
  • Loading branch information
iovxw committed Apr 29, 2020
1 parent 3549a2c commit d0144ad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ cargo build --release

```
USAGE:
rssbot [OPTIONS] <token>
rssbot [FLAGS] [OPTIONS] <token>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
-h, --help Prints help information
--insecure DANGER: Insecure mode, accept invalid TLS certificates
-V, --version Prints version information
OPTIONS:
-d, --database <database> Path to database [default: ./rssbot.json]
--max-interval <max-interval> [default: 43200]
--min-interval <min-interval> [default: 300]
--max-interval <max-interval> Maximum fetch interval, seconds [default: 43200]
--min-interval <min-interval> Minimum fetch interval, seconds [default: 300]
ARGS:
<token> Telegram bot token
Expand Down
78 changes: 39 additions & 39 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::sync::{Arc, Once};
use std::time::Duration;

use once_cell::sync::OnceCell;
use reqwest::{
self,
header::{HeaderValue, CONTENT_TYPE},
Expand All @@ -12,6 +12,8 @@ use crate::feed::Rss;

const RESP_SIZE_LIMIT: usize = 2 * 1024 * 1024;

static CLIENT: OnceCell<reqwest::Client> = OnceCell::new();

#[derive(Error, Debug)]
pub enum FeedError {
#[error("network error")]
Expand All @@ -36,7 +38,13 @@ impl FeedError {
}

pub async fn pull_feed(url: &str) -> Result<Rss, FeedError> {
let mut resp = client().get(url).send().await?.error_for_status()?;
let mut resp = CLIENT
.get()
.expect("CLIENT not initialized")
.get(url)
.send()
.await?
.error_for_status()?;
if let Some(len) = resp.content_length() {
if len > RESP_SIZE_LIMIT as u64 {
return Err(FeedError::TooLarge);
Expand Down Expand Up @@ -64,45 +72,37 @@ pub async fn pull_feed(url: &str) -> Result<Rss, FeedError> {
Ok(crate::feed::fix_relative_url(feed, url))
}

fn client() -> Arc<reqwest::Client> {
static mut CLIENT: Option<Arc<reqwest::Client>> = None;
static INIT: Once = Once::new();

INIT.call_once(|| {
let mut headers = reqwest::header::HeaderMap::new();
let ua = format!(
concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
" (+https://t.me/{})"
),
crate::BOT_NAME.get().expect("BOT_NAME not initialized")
);
headers.insert(
reqwest::header::USER_AGENT,
reqwest::header::HeaderValue::from_str(&ua).unwrap(),
);
let mut client_builder = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.default_headers(headers)
.redirect(reqwest::redirect::Policy::limited(5));

if env::var("RSSBOT_DONT_PROXY_FEEDS")
.or_else(|_| env::var("rssbot_dont_proxy_feeds"))
.is_ok()
{
client_builder = client_builder.no_proxy();
}

let client = client_builder.build().unwrap();
pub fn init_client(bot_name: &str, insecue: bool) {
let mut headers = reqwest::header::HeaderMap::new();
let ua = format!(
concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
" (+https://t.me/{})"
),
bot_name
);
headers.insert(
reqwest::header::USER_AGENT,
reqwest::header::HeaderValue::from_str(&ua).unwrap(),
);
let mut client_builder = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.default_headers(headers)
.danger_accept_invalid_certs(insecue)
.redirect(reqwest::redirect::Policy::limited(5));

if env::var("RSSBOT_DONT_PROXY_FEEDS")
.or_else(|_| env::var("rssbot_dont_proxy_feeds"))
.is_ok()
{
client_builder = client_builder.no_proxy();
}

unsafe {
CLIENT = Some(Arc::new(client));
}
});
let client = client_builder.build().unwrap();

unsafe { CLIENT.clone() }.unwrap()
CLIENT.set(client).expect("CLIENT already initialized");
}

fn content_type_is_json(value: &HeaderValue) -> bool {
Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ struct Opt {
/// Path to database
#[structopt(short = "d", long, default_value = "./rssbot.json")]
database: PathBuf,

#[structopt(long, default_value = "300", parse(try_from_str = parse_interval))] // 5 minutes
/// Minimum fetch interval, seconds
#[structopt(long, default_value = "300", parse(try_from_str = parse_interval))]
// 5 minutes
min_interval: u32,

#[structopt(long, default_value = "43200", parse(try_from_str = parse_interval))] // 12 hours
/// Maximum fetch interval, seconds
#[structopt(long, default_value = "43200", parse(try_from_str = parse_interval))]
// 12 hours
max_interval: u32,
/// DANGER: Insecure mode, accept invalid TLS certificates
#[structopt(long)]
insecure: bool,
}

fn parse_interval(s: &str) -> Result<u32, String> {
Expand Down Expand Up @@ -88,7 +93,10 @@ async fn main() -> anyhow::Result<()> {
.await
.context("Initialization failed, check your network and Telegram token")?;

BOT_NAME.set(me.user.username.clone().unwrap()).unwrap();
let bot_name = me.user.username.clone().unwrap();
crate::client::init_client(&bot_name, opt.insecure);

BOT_NAME.set(bot_name).unwrap();
BOT_ID.set(me.user.id).unwrap();

gardener::start_pruning(bot.clone(), db.clone());
Expand Down

0 comments on commit d0144ad

Please sign in to comment.