Skip to content

Commit

Permalink
Read config constants from config.rs and minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zolagonano committed May 1, 2024
1 parent 74cd002 commit dbf5884
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 70 deletions.
37 changes: 37 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub const MTPROTO_SOURCES: &[&str] = &["https://t.me/s/NextGenProxy", "https://t.me/s/MTP_roto"];
pub const SHADOWSOCKS_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

pub const VMESS_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

pub const VLESS_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

pub const TROJAN_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

pub const HELP_MESSAGE: &str = "Fire Ninja Bot allows you to access proxies to bypass firewalls and access blocked content. Currently, only the following commands are available:
- /help: Shows this message.
- /mtproxy: Fetches and provides a list of MTProto proxies.
- /shadowsocks: Fetches and provides a list of Shadowsocks proxies.
- /vmess: Fetches and provides a list of VMess proxies.
- /vless: Fetches and provides a list of VLess proxies.
- /trojan: Fetches and provides a list of Trojan proxies.
";

pub const INVALID_COMMAND_MESSAGE: &str =
"Invalid command, use /help to get list of available commands.";

pub const NO_PROXIES_FOUND_MESSAGE: &str = "No proxies were found.";

pub const LINE_SEPERATOR: &str = "\n﹌﹌﹌\n";

pub const MTPROXY_COMMAND: &str = "/mtproxy";
pub const SHADOWSOCKS_COMMAND: &str = "/shadowsocks";
pub const VMESS_COMMAND: &str = "/vmess";
pub const VLESS_COMMAND: &str = "/vless";
pub const TROJAN_COMMAND: &str = "/trojan";
pub const HELP_COMMAND: &str = "/help";
pub const START_COMMAND: &str = "/start";
87 changes: 17 additions & 70 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,13 @@
use serde::{Deserialize, Serialize};
use serde_json::json;
mod config;
mod telegram;
mod utils;

use crate::config::*;
use crate::telegram::*;
use crate::utils::*;
use std::collections::HashSet;
use worker::*;

// TODO: Read Sources from config file
const MTPROTO_SOURCES: &[&str] = &["https://t.me/s/NextGenProxy", "https://t.me/s/MTP_roto"];
const SHADOWSOCKS_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

const VMESS_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

const VLESS_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

const TROJAN_SOURCES: &[&str] =
&["https://raw.githubusercontent.com/barry-far/V2ray-Configs/main/All_Configs_Sub.txt"];

const HELP_MESSAGE: &str = "Fire Ninja Bot allows you to access proxies to bypass firewalls and access blocked content. Currently, only the following commands are available:
- /help: Shows this message.
- /mtproxy: Fetches and provides a list of MTProto proxies.
- /shadowsocks: Fetches and provides a list of Shadowsocks proxies.
- /vmess: Fetches and provides a list of VMess proxies.
- /vless: Fetches and provides a list of VLess proxies.
- /trojan: Fetches and provides a list of Trojan proxies.
";

async fn fetch_sources(sources: &[&str]) -> String {
let mut content = String::new();
for source in sources {
if let Ok(response) = reqwest::get(*source).await {
if let Ok(text) = response.text().await {
content.push_str(&text);
}
}
}

content
}

#[derive(Debug, Deserialize, Serialize)]
struct TelegramUpdate {
message: Option<TelegramMessage>,
}

#[derive(Debug, Deserialize, Serialize)]
struct TelegramMessage {
chat: TelegramChat,
text: String,
}

#[derive(Debug, Deserialize, Serialize)]
struct TelegramChat {
id: i64,
}

enum Command {
MTProxy,
Shadowsocks,
Expand All @@ -68,12 +20,12 @@ enum Command {
impl Command {
pub fn from_str(command: &str) -> Option<Self> {
match command {
"/mtproxy" => Some(Self::MTProxy),
"/ss" | "/shadowsocks" => Some(Self::Shadowsocks),
"/vmess" => Some(Self::VMess),
"/vless" => Some(Self::VLess),
"/trojan" => Some(Self::Trojan),
"/start" | "/help" => Some(Self::Help),
MTPROXY_COMMAND => Some(Self::MTProxy),
SHADOWSOCKS_COMMAND => Some(Self::Shadowsocks),
VMESS_COMMAND => Some(Self::VMess),
VLESS_COMMAND => Some(Self::VLess),
TROJAN_COMMAND => Some(Self::Trojan),
START_COMMAND | HELP_COMMAND => Some(Self::Help),
_ => None,
}
}
Expand Down Expand Up @@ -128,11 +80,11 @@ impl Command {
.into_iter()
.take(8)
.collect::<Vec<_>>()
.join("\n﹌﹌﹌\n"),
.join(LINE_SEPERATOR),
};

if result.is_empty() {
return "No proxies were found! ".to_string();
return NO_PROXIES_FOUND_MESSAGE.to_string();
}

result
Expand All @@ -154,15 +106,10 @@ async fn main(mut req: Request, _env: Env, _ctx: Context) -> Result<Response> {

let response_text = match Command::from_str(text.as_str()) {
Some(command) => command.fetch_and_scrape().await,
None => "Invalid command, use /help to get list of available commands.".to_string(),
None => INVALID_COMMAND_MESSAGE.to_string(),
};

let response = json!({
"method": "sendMessage",
"chat_id": chat_id,
"text": response_text,
"parse_mode": "Markdown",
});
let response = TelegramSendMessage::new_md(chat_id, response_text).to_json();

return Response::from_json(&response);
}
Expand Down
44 changes: 44 additions & 0 deletions src/telegram.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue};

#[derive(Debug, Deserialize, Serialize)]
pub struct TelegramUpdate {
pub message: Option<TelegramMessage>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TelegramMessage {
pub chat: TelegramChat,
pub text: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TelegramChat {
pub id: i64,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TelegramSendMessage {
pub chat_id: i64,
pub text: String,
pub parse_mode: String,
}

impl TelegramSendMessage {
pub fn new_md(chat_id: i64, text: String) -> Self {
TelegramSendMessage {
chat_id,
text,
parse_mode: "Markdown".to_string(),
}
}

pub fn to_json(&self) -> JsonValue {
json!({
"method": "sendMessage",
"chat_id": self.chat_id,
"text": self.text,
"parse_mode": self.parse_mode,
})
}
}
12 changes: 12 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub async fn fetch_sources(sources: &[&str]) -> String {
let mut content = String::new();
for source in sources {
if let Ok(response) = reqwest::get(*source).await {
if let Ok(text) = response.text().await {
content.push_str(&text);
}
}
}

content
}

0 comments on commit dbf5884

Please sign in to comment.