From 2c65797e0261616b58f87b0a863c36b27299bfd5 Mon Sep 17 00:00:00 2001 From: SyS Date: Wed, 15 Apr 2020 12:39:30 +0530 Subject: [PATCH 1/3] support for proxy #2 --- include/tgconnector.inc | 4 ++-- src/api.rs | 32 +++++++++++++++++++++++--------- src/http.rs | 26 +++++++++++++++++++++++--- src/internals.rs | 8 ++++++-- src/natives.rs | 23 +++++++++++++++++++---- 5 files changed, 73 insertions(+), 20 deletions(-) diff --git a/include/tgconnector.inc b/include/tgconnector.inc index f0ea2d6..cb3d36c 100644 --- a/include/tgconnector.inc +++ b/include/tgconnector.inc @@ -30,8 +30,8 @@ enum TGUserStatus { }; //basic -native TGBot:TGConnect(const token[],thread_limit=3); -native TGBot:TGConnectFromEnv(const variable[],thread_limit=3); +native TGBot:TGConnect(const token[],thread_limit=3,proxy[]=""); +native TGBot:TGConnectFromEnv(const variable[],thread_limit=3,proxy[]=""); native TGSendMessage(TGBot:bot,const TGChatId:chatid[],const text[],TGMessage:reply_id=INVALID_MESSAGE_ID,TGParseMode:parse_mode=TGParseMode:-1,callback[]=""); native TGDeleteMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid); native TGEditMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid,text[],TGParseMode:parse_mode=TGParseMode:-1); diff --git a/src/api.rs b/src/api.rs index e84a972..ff5de3e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -17,10 +17,11 @@ pub struct BOT { pub send_message_reciever: Option>, pub send_message_sender: Option>, pub pool: ThreadPool, + pub proxy_url: Option, } impl BOT { - pub fn new(bot_token: String, thread_count: i32) -> Self { + pub fn new(bot_token: String, thread_count: i32, proxy_url: Option) -> Self { let (update_sender, update_reciever) = channel(); let (send_message_sender, send_message_reciever) = channel(); @@ -32,14 +33,16 @@ impl BOT { send_message_reciever: Some(send_message_reciever), send_message_sender: Some(send_message_sender), pool: ThreadPool::new(thread_count as usize), + proxy_url, } } - pub fn connect(&mut self) -> bool { + pub fn connect(&mut self, proxy_url: Option) -> bool { let request = HttpRequest { url: format!("{}/getme", self.api_request_link), method: HttpMethod::Get, body: None, + proxy_url, }; match request.make_request() { @@ -65,11 +68,12 @@ impl BOT { fn get_updates(&self) { let update_move = self.update_sender.clone(); let api_link = self.api_request_link.clone(); + let proxy_url = self.proxy_url.clone(); let mut getupdate = GetUpdates { offset: -2 }; self.pool.execute(move || loop { let update: Result>, String> = - telegram_request("getUpdates", &api_link, &getupdate); + telegram_request("getUpdates", &api_link, &getupdate, &proxy_url); match update { Ok(update) => { let mut check_result: VecDeque = match update.body { @@ -104,10 +108,11 @@ impl BOT { pub fn send_message(&self, send_message_obj: SendMessage, callback: Option) { let send_message_move = self.send_message_sender.clone(); let api_link = self.api_request_link.clone(); + let proxy_url = self.proxy_url.clone(); self.pool.execute(move || { let response: Result, String> = - telegram_request("sendmessage", &api_link, &send_message_obj); + telegram_request("sendmessage", &api_link, &send_message_obj, &proxy_url); match response { Ok(response) => { if !response.ok { @@ -128,10 +133,11 @@ impl BOT { pub fn delete_message(&self, delete_message_obj: DeleteMessage) { let api_link = self.api_request_link.clone(); + let proxy_url = self.proxy_url.clone(); self.pool.execute(move || { let response: Result, String> = - telegram_request("deletemessage", &api_link, &delete_message_obj); + telegram_request("deletemessage", &api_link, &delete_message_obj, &proxy_url); match response { Ok(response) => { @@ -152,10 +158,11 @@ impl BOT { pub fn edit_message(&self, edit_message_obj: EditMessageText) { let api_link = self.api_request_link.clone(); + let proxy_url = self.proxy_url.clone(); self.pool.execute(move || { let response: Result, String> = - telegram_request("editmessagetext", &api_link, &edit_message_obj); + telegram_request("editmessagetext", &api_link, &edit_message_obj, &proxy_url); match response { Ok(response) => { if !response.ok { @@ -174,8 +181,12 @@ impl BOT { } pub fn get_chat_member(&self, getchatmember: GetChatMember) -> Option { - let response: Result, String> = - telegram_request("getchatmember", &self.api_request_link, &getchatmember); + let response: Result, String> = telegram_request( + "getchatmember", + &self.api_request_link, + &getchatmember, + &self.proxy_url, + ); match response { Ok(response) => { @@ -199,6 +210,7 @@ impl BOT { "getchatmemberscount", &self.api_request_link, &getchatmemberscount, + &self.proxy_url, ); match response { @@ -220,7 +232,7 @@ impl BOT { pub fn get_chat(&self, getchat: GetChat) -> Option { let response: Result, String> = - telegram_request("getchat", &self.api_request_link, &getchat); + telegram_request("getchat", &self.api_request_link, &getchat, &self.proxy_url); match response { Ok(response) => { @@ -244,11 +256,13 @@ fn telegram_request( endpoint: &str, api_link: &str, body: B, + proxy_url: &Option, ) -> Result, String> { let request = HttpRequest { url: format!("{}/{}", api_link, endpoint), method: HttpMethod::Post, body: Some(to_string(&body).unwrap()), + proxy_url: proxy_url.clone(), }; match request.make_request() { diff --git a/src/http.rs b/src/http.rs index 0997dcb..645ce6e 100644 --- a/src/http.rs +++ b/src/http.rs @@ -10,6 +10,7 @@ pub struct HttpRequest { pub url: String, pub method: HttpMethod, pub body: Option, + pub proxy_url: Option, } impl HttpRequest { @@ -22,7 +23,18 @@ impl HttpRequest { } }; let method = match self.method { - HttpMethod::Get => requests_obj.get(), + HttpMethod::Get => { + if let Some(proxy_url) = &self.proxy_url { + match requests_obj.proxy(&proxy_url) { + Ok(method) => method.get(), + Err(err) => { + return Err(format!("Error connecting to proxy server \n{:?}", err)); + } + } + } else { + requests_obj.get() + } + } HttpMethod::Post => { let body = &self.body.clone().unwrap(); @@ -30,10 +42,18 @@ impl HttpRequest { let mut headers = HashMap::new(); headers.insert("Content-Type".to_string(), "application/json".to_string()); requests_obj.headers(headers); - requests_obj.post() + if let Some(proxy_url) = &self.proxy_url { + match requests_obj.proxy(&proxy_url) { + Ok(method) => method.post(), + Err(err) => { + return Err(format!("Error connecting to proxy server \n{:?}", err)); + } + } + } else { + requests_obj.post() + } } }; - match method.send() { Ok(data) => Ok(data.text()), diff --git a/src/internals.rs b/src/internals.rs index 123f0d8..5b2ba89 100644 --- a/src/internals.rs +++ b/src/internals.rs @@ -138,8 +138,12 @@ pub fn on_send_message_process(plugin: &mut super::TgConnector) { } } -pub fn create_bot(plugin: &mut super::TgConnector, mut api: BOT) -> AmxResult { - if api.connect() { +pub fn create_bot( + plugin: &mut super::TgConnector, + mut api: BOT, + proxy_url: Option, +) -> AmxResult { + if api.connect(proxy_url) { plugin.bots.insert(plugin.bot_context_id, api); plugin.bot_context_id += 1; Ok(plugin.bot_context_id as i32 - 1) diff --git a/src/natives.rs b/src/natives.rs index 0745ad5..2e03f75 100644 --- a/src/natives.rs +++ b/src/natives.rs @@ -13,9 +13,17 @@ impl super::TgConnector { _amx: &Amx, token: AmxString, thread_count: i32, + proxy_url: AmxString, ) -> AmxResult { - let api = BOT::new(token.to_string(), thread_count); - create_bot(self, api) + let proxy_url = proxy_url.to_string(); + let proxy_url = if proxy_url.is_empty() { + None + } else { + Some(proxy_url) + }; + + let api = BOT::new(token.to_string(), thread_count, proxy_url.clone()); + create_bot(self, api, proxy_url) } #[native(name = "TGConnectFromEnv")] @@ -23,10 +31,17 @@ impl super::TgConnector { &mut self, _amx: &Amx, variable: AmxString, + proxy_url: AmxString, thread_count: i32, ) -> AmxResult { let variable = variable.to_string(); let token = std::env::var_os(&variable); + let proxy_url = proxy_url.to_string(); + let proxy_url = if proxy_url.is_empty() { + None + } else { + Some(proxy_url) + }; if token == None { error!("Environment variable {:?} is not set", variable); @@ -34,9 +49,9 @@ impl super::TgConnector { } let token = token.unwrap().into_string().unwrap(); - let api = BOT::new(token, thread_count); + let api = BOT::new(token, thread_count, proxy_url.clone()); - create_bot(self, api) + create_bot(self, api, proxy_url) } #[native(name = "TGSendMessage")] From 5d5c152aef8600d9f0d9e05895711dadb8f25090 Mon Sep 17 00:00:00 2001 From: SyS Date: Thu, 16 Apr 2020 11:02:28 +0530 Subject: [PATCH 2/3] parameter order changed --- include/tgconnector.inc | 4 ++-- src/natives.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/tgconnector.inc b/include/tgconnector.inc index cb3d36c..c718614 100644 --- a/include/tgconnector.inc +++ b/include/tgconnector.inc @@ -30,8 +30,8 @@ enum TGUserStatus { }; //basic -native TGBot:TGConnect(const token[],thread_limit=3,proxy[]=""); -native TGBot:TGConnectFromEnv(const variable[],thread_limit=3,proxy[]=""); +native TGBot:TGConnect(const token[],proxy[]="",thread_limit=3); +native TGBot:TGConnectFromEnv(const variable[],proxy[]="",thread_limit=3); native TGSendMessage(TGBot:bot,const TGChatId:chatid[],const text[],TGMessage:reply_id=INVALID_MESSAGE_ID,TGParseMode:parse_mode=TGParseMode:-1,callback[]=""); native TGDeleteMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid); native TGEditMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid,text[],TGParseMode:parse_mode=TGParseMode:-1); diff --git a/src/natives.rs b/src/natives.rs index 2e03f75..8debe66 100644 --- a/src/natives.rs +++ b/src/natives.rs @@ -12,8 +12,8 @@ impl super::TgConnector { &mut self, _amx: &Amx, token: AmxString, - thread_count: i32, proxy_url: AmxString, + thread_count: i32, ) -> AmxResult { let proxy_url = proxy_url.to_string(); let proxy_url = if proxy_url.is_empty() { From 8881ebafcd6add7b09e5b2b5fe9a7d072136e77d Mon Sep 17 00:00:00 2001 From: SyS Date: Thu, 16 Apr 2020 11:04:09 +0530 Subject: [PATCH 3/3] const correction --- include/tgconnector.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/tgconnector.inc b/include/tgconnector.inc index c718614..2341e89 100644 --- a/include/tgconnector.inc +++ b/include/tgconnector.inc @@ -30,8 +30,8 @@ enum TGUserStatus { }; //basic -native TGBot:TGConnect(const token[],proxy[]="",thread_limit=3); -native TGBot:TGConnectFromEnv(const variable[],proxy[]="",thread_limit=3); +native TGBot:TGConnect(const token[],const proxy[]="",thread_limit=3); +native TGBot:TGConnectFromEnv(const variable[],const proxy[]="",thread_limit=3); native TGSendMessage(TGBot:bot,const TGChatId:chatid[],const text[],TGMessage:reply_id=INVALID_MESSAGE_ID,TGParseMode:parse_mode=TGParseMode:-1,callback[]=""); native TGDeleteMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid); native TGEditMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid,text[],TGParseMode:parse_mode=TGParseMode:-1);